okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7c5e84612
main
1// tests/hip/gdn_rec_head.cpp - the prompt GDN recurrence's four-lanes-per-column kernel + norm (gdn_recurrence_variant 1) 2// against the column-split kernels + the norm kernel (variant 0, the default before it): 3// y, its FP16 bits and the final state must be BITWISE equal, over chunk lengths around the 8-token staging and a 4// long one, from a nonzero state. Synthetic inputs in the model's ranges (unit q/k rows, log-decay gates < 0). 5#include "strata/prefill/kernels.hpp" 6 7#include <cuda_runtime.h> 8 9#include <cmath> 10#include <cstdint> 11#include <cstdio> 12#include <cstdlib> 13#include <cstring> 14#include <random> 15#include <vector> 16 17namespace { 18constexpr int S = 128 ,HK = 16 ,HV = 48 ,C = 10240 ; 19void ck (cudaError_t e ,const char * w ) { 20if (e != cudaSuccess ) { std::fprintf (stderr ,"%s: %s\n" ,w ,cudaGetErrorString (e )); std::exit (2 ); } 21} 22template < typename T > T * dev (const std::vector < T >& h ) { 23T * d = nullptr ; 24ck (cudaMalloc (& d ,h .size ()* sizeof (T )),"malloc" ); 25ck (cudaMemcpy (d ,h .data (),h .size ()* sizeof (T ),cudaMemcpyHostToDevice ),"upload" ); 26return d ; 27} 28template < typename T > std::vector < T > host (const T * d ,size_t n ) { 29 std::vector < T > h (n ); 30ck (cudaMemcpy (h .data (),d ,n * sizeof (T ),cudaMemcpyDeviceToHost ),"download" ); 31return h ; 32} 33}// namespace 34 35int main () { 36// variants 0 and 1 are the references here; STRATA_GDN_CHUNKED=1 would turn them into the opt-in chunked recurrence 37// (tests/hip/gdn_chunked.cpp), whose bits differ 38#ifdef _WIN32 39_putenv_s ("STRATA_GDN_CHUNKED" ,"" ); 40#else 41unsetenv ("STRATA_GDN_CHUNKED" ); 42#endif 43 std::mt19937 rng (11 ); 44 std::normal_distribution < float > nd (0.f ,1.f ); 45 std::uniform_real_distribution < float > ud (0.f ,1.f ); 46int failures = 0 ; 47const int64_t lens []= {1 ,7 ,8 ,9 ,17 ,100 ,2048 ,2051 }; 48for (const int64_t T :lens ) { 49 std::vector < float > h ((size_t )T * C ),gate ((size_t )T * HV ),beta ((size_t )T * HV ),z ((size_t )T * HV * S ), 50gamma (S ),state ((size_t )S * HV * S ); 51for (int64_t t = 0 ;t < T ;++ t ) { 52float * ht = h .data ()+ t * C ; 53for (int i = 0 ;i < C ;++ i )ht [i ]= nd (rng ); 54for (int hh = 0 ;hh < 2 * HK ;++ hh ) {// q and k heads: unit rows, as after gdn_conv's L2 norm 55double ss = 0 ; 56for (int d = 0 ;d < S ;++ d )ss += (double )ht [hh * S + d ]* ht [hh * S + d ]; 57for (int d = 0 ;d < S ;++ d )ht [hh * S + d ]= (float ) (ht [hh * S + d ] / std::sqrt (ss + 1e-6 )); 58 } 59 } 60for (auto & x :gate )x = -2.0f * ud (rng ); 61for (auto & x :beta )x = ud (rng ); 62for (auto & x :z )x = nd (rng ); 63for (auto & x :gamma )x = 0.5f + ud (rng ); 64for (auto & x :state )x = 0.1f * nd (rng ); 65const float * dh = dev (h ); 66const float * dg = dev (gate ); 67const float * db = dev (beta ); 68const float * dz = dev (z ); 69const float * dgm = dev (gamma ); 70 std::vector < float > y_ref ,st_ref ; 71 std::vector < uint16_t > y16_ref ; 72for (int variant : {0 ,3 ,1 ,4 ,5 }) { 73float * dst = dev (state ); 74float * dy = nullptr ; 75uint16_t * dy16 = nullptr ; 76ck (cudaMalloc (& dy , (size_t )T * HV * S * 4 ),"malloc" ); 77ck (cudaMalloc (& dy16 , (size_t )T * HV * S * 2 ),"malloc" ); 78 strata::prefill::gdn_recurrence_variant (variant ,dst ,dh ,dg ,db ,dz ,dgm ,1e-6f ,dy ,dy16 ,T ,nullptr ); 79ck (cudaDeviceSynchronize (),"run" ); 80auto y = host (dy , (size_t )T * HV * S ); 81auto y16 = host (dy16 , (size_t )T * HV * S ); 82auto st = host (dst ,state .size ()); 83if (variant == 0 ) { 84y_ref = y ;y16_ref = y16 ;st_ref = st ; 85 }else { 86size_t dy_n = 0 ,d16 = 0 ,dsn = 0 ; 87for (size_t i = 0 ;i < y .size ();++ i )dy_n += std::memcmp (& y [i ],& y_ref [i ],4 )!= 0 ; 88for (size_t i = 0 ;i < y16 .size ();++ i )d16 += y16 [i ]!= y16_ref [i ]; 89for (size_t i = 0 ;i < st .size ();++ i )dsn += std::memcmp (& st [i ],& st_ref [i ],4 )!= 0 ; 90// (y, the FP32 scratch, is not part of the contract: 0.1.39's norm kernel no longer stores the normalized value there, 91// while the head kernels store it unless STRATA_GDN_NOY; what the out projection reads is y16, and the state) 92const bool ok = d16 == 0 && dsn == 0 ; 93failures += !ok ; 94 std::printf ("%s T %lld variant %d vs 0: y %zu, y16 %zu, state %zu values differ\n" ,ok ?"PASS" :"FAIL" , 95 (long long )T ,variant ,dy_n ,d16 ,dsn ); 96 } 97cudaFree (dst );cudaFree (dy );cudaFree (dy16 ); 98 } 99// timing at the long lengths: variant 0 vs 1 100if (T >=2048 ) { 101float * dst = dev (state ); 102float * dy = nullptr ; 103uint16_t * dy16 = nullptr ; 104ck (cudaMalloc (& dy , (size_t )T * HV * S * 4 ),"malloc" ); 105ck (cudaMalloc (& dy16 , (size_t )T * HV * S * 2 ),"malloc" ); 106for (int variant : {0 ,1 }) { 107cudaEvent_t a ,b ; 108cudaEventCreate (& a );cudaEventCreate (& b ); 109 strata::prefill::gdn_recurrence_variant (variant ,dst ,dh ,dg ,db ,dz ,dgm ,1e-6f ,dy ,dy16 ,T ,nullptr ); 110cudaEventRecord (a ); 111for (int r = 0 ;r < 3 ;++ r ) 112 strata::prefill::gdn_recurrence_variant (variant ,dst ,dh ,dg ,db ,dz ,dgm ,1e-6f ,dy ,dy16 ,T ,nullptr ); 113cudaEventRecord (b ); 114ck (cudaEventSynchronize (b ),"time" ); 115float ms = 0 ; 116cudaEventElapsedTime (& ms ,a ,b ); 117 std::printf (" T %lld variant %d: %.3f ms per call (%.2f us per token)\n" , (long long )T ,variant ,ms /3 , 118ms /3 * 1000 / (double )T ); 119 } 120cudaFree (dst );cudaFree (dy );cudaFree (dy16 ); 121 } 122cudaFree ((void * )dh );cudaFree ((void * )dg );cudaFree ((void * )db );cudaFree ((void * )dz );cudaFree ((void * )dgm ); 123 } 124 std::printf ("FAILURES: %d\n" ,failures ); 125return failures ?1 :0 ; 126}