okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
58f81f20e
main
1// tests/hip/prefill_gr_write_bench.cpp - the hyper-connection write-back (gr_write_norm_rs: R += bo * 2 sigmoid(inj / 4), then the next 2// half's norm row scale and BF16 image) timed at the prompt path's shapes, with the bytes it moves, and a checksum of its 3// outputs so a variant can be compared bit for bit (run twice with STRATA_GRW_V=0 / 1). 4// gr_write_bench [T=16384] [reps=20] 5#include "strata/prefill/kernels.hpp" 6 7#include <cuda_runtime.h> 8 9#include <cstdint> 10#include <cstdio> 11#include <cstdlib> 12#include <cstring> 13#include <random> 14#include <vector> 15 16namespace { 17constexpr int N = 2560 ,HC = 4 ,D = N * HC ; 18bool ck (cudaError_t e ,const char * what ) { 19if (e != cudaSuccess ) { std::printf ("FAIL: %s: %s\n" ,what ,cudaGetErrorString (e ));return false; } 20return true; 21} 22uint64_t fnv (const void * p ,size_t n ,uint64_t h = 1469598103934665603ull ) { 23const uint8_t * b = (const uint8_t * )p ; 24for (size_t i = 0 ;i < n ;++ i ) {h ^=b [i ];h *=1099511628211ull ; } 25return h ; 26} 27}// namespace 28 29int main (int argc ,char ** argv ) { 30const int64_t T = argc > 1 ? std::atoll (argv [1 ]) :16384 ; 31const int reps = argc > 2 ? std::atoi (argv [2 ]) :20 ; 32 std::mt19937 rng (7 ); 33 std::normal_distribution < float > nd (0.f ,1.f ); 34 std::vector < float > R0 ((size_t )T * D ),bo ((size_t )T * N ),inj ((size_t )T * HC ),w ((size_t )HC * N ); 35for (auto & v :R0 )v = nd (rng ); 36for (auto & v :bo )v = nd (rng ); 37for (auto & v :inj )v = nd (rng ); 38for (auto & v :w )v = 0.5f + 0.1f * nd (rng ); 39float * dR = nullptr ,* dR0 = nullptr ,* dbo = nullptr ,* dinj = nullptr ,* dw = nullptr ,* drs = nullptr ; 40uint16_t * dx = nullptr ; 41bool ok = ck (cudaMalloc (& dR ,R0 .size ()* 4 ),"R" )&& ck (cudaMalloc (& dR0 ,R0 .size ()* 4 ),"R0" )&& 42ck (cudaMalloc (& dbo ,bo .size ()* 4 ),"bo" )&& ck (cudaMalloc (& dinj ,inj .size ()* 4 ),"inj" )&& 43ck (cudaMalloc (& dw ,w .size ()* 4 ),"w" )&& ck (cudaMalloc (& drs , (size_t )T * HC * 4 ),"rs" )&& 44ck (cudaMalloc (& dx , (size_t )T * D * 2 ),"xn16" ); 45if (!ok )return 2 ; 46cudaMemcpy (dR0 ,R0 .data (),R0 .size ()* 4 ,cudaMemcpyHostToDevice ); 47cudaMemcpy (dbo ,bo .data (),bo .size ()* 4 ,cudaMemcpyHostToDevice ); 48cudaMemcpy (dinj ,inj .data (),inj .size ()* 4 ,cudaMemcpyHostToDevice ); 49cudaMemcpy (dw ,w .data (),w .size ()* 4 ,cudaMemcpyHostToDevice ); 50cudaStream_t s ; 51cudaStreamCreate (& s ); 52// one reference pass for the checksum 53cudaMemcpy (dR ,dR0 ,R0 .size ()* 4 ,cudaMemcpyDeviceToDevice ); 54 strata::prefill::gr_write_norm_rs (dR ,dbo ,dinj ,HC ,dw ,1e-6f ,drs ,dx ,T ,s ,nullptr ,D , false); 55cudaStreamSynchronize (s ); 56 std::vector < float > oR (R0 .size ()),ors ((size_t )T * HC ); 57 std::vector < uint16_t > ox ((size_t )T * D ); 58cudaMemcpy (oR .data (),dR ,oR .size ()* 4 ,cudaMemcpyDeviceToHost ); 59cudaMemcpy (ors .data (),drs ,ors .size ()* 4 ,cudaMemcpyDeviceToHost ); 60cudaMemcpy (ox .data (),dx ,ox .size ()* 2 ,cudaMemcpyDeviceToHost ); 61 std::printf ("checksum R %016llx rs %016llx xn16 %016llx\n" , (unsigned long long )fnv (oR .data (),oR .size ()* 4 ), 62 (unsigned long long )fnv (ors .data (),ors .size ()* 4 ), (unsigned long long )fnv (ox .data (),ox .size ()* 2 )); 63cudaEvent_t e0 ,e1 ; 64cudaEventCreate (& e0 ); 65cudaEventCreate (& e1 ); 66float best = 1e30f ,sum = 0 ; 67for (int r = 0 ;r < reps ;++ r ) { 68cudaMemcpy (dR ,dR0 ,R0 .size ()* 4 ,cudaMemcpyDeviceToDevice ); 69cudaStreamSynchronize (s ); 70cudaEventRecord (e0 ,s ); 71 strata::prefill::gr_write_norm_rs (dR ,dbo ,dinj ,HC ,dw ,1e-6f ,drs ,dx ,T ,s ,nullptr ,D , false); 72cudaEventRecord (e1 ,s ); 73cudaEventSynchronize (e1 ); 74float ms = 0 ; 75cudaEventElapsedTime (& ms ,e0 ,e1 ); 76best = ms < best ?ms :best ; 77sum += ms ; 78 } 79// bytes: R read + write, bo read (x4 rows, from cache after the first), inj, w, rs, xn16 write 80const double gb = ((double )T * D * 4 * 2 + (double )T * N * 4 + (double )T * D * 2 ) /1e9 ; 81 std::printf ("gr_write_norm_rs T=%lld: best %.3f ms, mean %.3f ms, %.0f GB/s at best (%.2f GB moved, bo counted once)\n" , 82 (long long )T ,best ,sum /reps ,gb / (best * 1e-3 ),gb ); 83return 0 ; 84}