okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0d2ee8def
main
1// bench/gdn_chunk_bench.cpp - v100/gdn-chunk: the chunked GDN prompt recurrence against the token recurrence, 2// real GDN geometry (S=128, 16 q/k heads, 48 v heads, C=10240), CUDA events. 3// 4// One call of `gdn_recurrence` per side (the whole prompt chunk for one GDN layer: conv output -> out norm), 5// A/B'd with STRATA_GDN_CHUNK=0 / =1 so the dispatch, the tail and the norm are timed exactly as the engine 6// runs them. The two paths are not bit-exact (the chunked one reorders the sums); the bench reports the max 7// relative difference it saw so a silent divergence cannot hide behind a speedup. 8// 9// usage: gdn_chunk_bench [T ...] (default 1024 4096 8192) 10#include "strata/prefill/kernels.hpp" 11 12#include <cuda_runtime.h> 13 14#include <cstdint> 15#include <cstdio> 16#include <cstdlib> 17#include <cstring> 18#include <vector> 19 20#if defined(_WIN32 )// MSVC has no setenv / unsetenv 21#define setenv (k ,v ,o ) _putenv_s((k), (v)) 22#define unsetenv (k ) _putenv_s((k), "") 23#endif 24 25namespace { 26 27constexpr int S = 128 ,HV = 48 ,C = 10240 ; 28 29void check (cudaError_t e ,const char * what ) { 30if (e != cudaSuccess ) { 31 std::fprintf (stderr ,"%s: %s\n" ,what ,cudaGetErrorString (e )); 32 std::exit (1 ); 33 } 34} 35 36struct Rng { 37uint64_t s = 12345 ; 38float uni () { 39s = s * 6364136223846793005ULL + 1442695040888963407ULL ; 40return (float ) ((s >>40 )* (1.0 /16777216.0 )); 41 } 42}; 43 44float timed (const char * mode ,const char * warp ,float * st ,float * st0 ,float * h ,float * g ,float * b ,float * z , 45float * ga ,float * y ,uint16_t * y16 ,int64_t T ,int iters ) { 46setenv ("STRATA_GDN_CHUNK" ,mode ,1 ); 47setenv ("STRATA_GDN_REC_WARP" ,warp ,1 ); 48cudaEvent_t e0 ,e1 ; 49check (cudaEventCreate (& e0 ),"event" ); 50check (cudaEventCreate (& e1 ),"event" ); 51check (cudaMemcpy (st ,st0 , (size_t )S * HV * S * 4 ,cudaMemcpyDeviceToDevice ),"reset" ); 52 strata::prefill::gdn_recurrence (st ,h ,g ,b ,z ,ga ,1e-5f ,y ,y16 ,T ,nullptr ); 53check (cudaDeviceSynchronize (),"warmup" ); 54check (cudaEventRecord (e0 ),"record" ); 55for (int i = 0 ;i < iters ;++ i ) { 56check (cudaMemcpy (st ,st0 , (size_t )S * HV * S * 4 ,cudaMemcpyDeviceToDevice ),"reset" ); 57 strata::prefill::gdn_recurrence (st ,h ,g ,b ,z ,ga ,1e-5f ,y ,y16 ,T ,nullptr ); 58 } 59check (cudaEventRecord (e1 ),"record" ); 60check (cudaEventSynchronize (e1 ),"sync" ); 61float ms = 0 ; 62check (cudaEventElapsedTime (& ms ,e0 ,e1 ),"elapsed" ); 63cudaEventDestroy (e0 ); 64cudaEventDestroy (e1 ); 65return ms /iters ; 66} 67 68}// namespace 69 70int main (int argc ,char ** argv ) { 71 std::vector < int64_t > ts ; 72for (int i = 1 ;i < argc ;++ i )ts .push_back (std::atoll (argv [i ])); 73if (ts .empty ())ts = {1024 ,4096 ,8192 }; 74int64_t tmax = 0 ; 75for (int64_t t :ts )tmax = t > tmax ?t :tmax ; 76 77int dev = 0 ; 78cudaDeviceProp p {}; 79check (cudaGetDevice (& dev ),"device" ); 80check (cudaGetDeviceProperties (& p ,dev ),"props" ); 81const int cc = p .major * 10 + p .minor ; 82 std::printf ("%s (cc %d) GDN prompt recurrence: chunked vs token-by-token\n" ,p .name ,cc ); 83 84Rng rng ; 85const size_t n_h = (size_t )tmax * C ,n_y = (size_t )tmax * HV * S ,n_g = (size_t )tmax * HV ; 86float * d_h ,* d_y ,* d_z ,* d_g ,* d_b ,* d_ga ,* d_st ,* d_st0 ; 87uint16_t * d_y16 ; 88check (cudaMalloc (& d_h ,n_h * 4 ),"malloc" ); 89check (cudaMalloc (& d_y ,n_y * 4 ),"malloc" ); 90check (cudaMalloc (& d_z ,n_y * 4 ),"malloc" ); 91check (cudaMalloc (& d_g ,n_g * 4 ),"malloc" ); 92check (cudaMalloc (& d_b ,n_g * 4 ),"malloc" ); 93check (cudaMalloc (& d_ga ,S * 4 ),"malloc" ); 94check (cudaMalloc (& d_st , (size_t )S * HV * S * 4 ),"malloc" ); 95check (cudaMalloc (& d_st0 , (size_t )S * HV * S * 4 ),"malloc" ); 96check (cudaMalloc (& d_y16 ,n_y * 2 ),"malloc" ); 97 98 std::vector < float > tmp ((size_t )tmax * C ); 99for (size_t i = 0 ;i < tmp .size ();++ i )tmp [i ]= rng .uni ()- 0.5f ; 100// q/k rows are L2-normalised rows (as gdn_conv leaves them); v rows stay O(1). A degenerate near-zero 101// v would make the out-norm amplify rounding noise by 1/sqrt(eps) and the diff column meaningless. 102for (int64_t t = 0 ;t < tmax ;++ t ) 103for (int row = 0 ;row < 2 * 16 ;++ row ) { 104float * r = tmp .data ()+ (size_t )t * C + row * S ; 105double ss = 0 ; 106for (int i = 0 ;i < S ;++ i )ss += (double )r [i ]* r [i ]; 107const float nrm = (float ) (1.0 / std::sqrt (ss + 1e-5 )); 108for (int i = 0 ;i < S ;++ i )r [i ] *=nrm ; 109 } 110check (cudaMemcpy (d_h ,tmp .data (),n_h * 4 ,cudaMemcpyHostToDevice ),"H2D" ); 111tmp .resize (n_g ); 112for (size_t i = 0 ;i < n_g ;++ i )tmp [i ]= -0.02f - 0.3f * rng .uni (); 113check (cudaMemcpy (d_g ,tmp .data (),n_g * 4 ,cudaMemcpyHostToDevice ),"H2D" ); 114for (size_t i = 0 ;i < n_g ;++ i )tmp [i ]= 1.0f / (1.0f + std::exp (- (4.0f * rng .uni ()- 2.0f ))); 115check (cudaMemcpy (d_b ,tmp .data (),n_g * 4 ,cudaMemcpyHostToDevice ),"H2D" ); 116tmp .resize (n_y ); 117for (size_t i = 0 ;i < n_y ;++ i )tmp [i ]= 2.0f * rng .uni ()- 1.0f ; 118check (cudaMemcpy (d_z ,tmp .data (),n_y * 4 ,cudaMemcpyHostToDevice ),"H2D" ); 119tmp .resize (S ); 120for (int i = 0 ;i < S ;++ i )tmp [i ]= 0.5f + 2.0f * rng .uni (); 121check (cudaMemcpy (d_ga ,tmp .data (),S * 4 ,cudaMemcpyHostToDevice ),"H2D" ); 122tmp .resize ((size_t )S * HV * S ); 123for (size_t i = 0 ;i < tmp .size ();++ i )tmp [i ]= 0.1f * (rng .uni ()- 0.5f ); 124check (cudaMemcpy (d_st0 ,tmp .data (),tmp .size ()* 4 ,cudaMemcpyHostToDevice ),"H2D" ); 125 126 std::printf ("%8s %12s %12s %12s %12s %12s %s\n" ,"T" ,"pipe(rec)" ,"warp(rec)" ,"fast(rec)" ,"chunk FMA" , 127"chunk wmma" ,"max abs diff vs pipe" ); 128for (int64_t T :ts ) { 129const int iters = T >=4096 ?5 :20 ; 130const float rec = timed ("0" ,"0" ,d_st ,d_st0 ,d_h ,d_g ,d_b ,d_z ,d_ga ,d_y ,d_y16 ,T ,iters ); 131 std::vector < float > y0 ((size_t )T * HV * S ); 132check (cudaMemcpy (y0 .data (),d_y ,y0 .size ()* 4 ,cudaMemcpyDeviceToHost ),"D2H" ); 133const float wrp = timed ("0" ,"1" ,d_st ,d_st0 ,d_h ,d_g ,d_b ,d_z ,d_ga ,d_y ,d_y16 ,T ,iters ); 134const float fst = timed ("0" ,"2" ,d_st ,d_st0 ,d_h ,d_g ,d_b ,d_z ,d_ga ,d_y ,d_y16 ,T ,iters ); 135const float fma = timed ("2" ,"0" ,d_st ,d_st0 ,d_h ,d_g ,d_b ,d_z ,d_ga ,d_y ,d_y16 ,T ,iters ); 136const float ch = timed ("1" ,"0" ,d_st ,d_st0 ,d_h ,d_g ,d_b ,d_z ,d_ga ,d_y ,d_y16 ,T ,iters ); 137 std::vector < float > y1 ((size_t )T * HV * S ); 138check (cudaMemcpy (y1 .data (),d_y ,y1 .size ()* 4 ,cudaMemcpyDeviceToHost ),"D2H" ); 139double absd = 0 ; 140for (size_t i = 0 ;i < y0 .size ();++ i ) { 141const double d = std::fabs ((double )y1 [i ]- (double )y0 [i ]); 142if (d > absd )absd = d ; 143 } 144 std::printf ("%8lld %11.3fms %11.3fms %11.3fms %11.3fms %11.3fms %.2e (wmma vs pipe)\n" , (long long )T , 145rec ,wrp ,fst ,fma ,ch ,absd ); 146 } 147return 0 ; 148}