okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7c5e84612
main
1// The prompt path's block scores on the thread-per-head kernel (an active-block count) against block_scores_kernel 2// (no count, more than 8 queries), every score of blocks 0..n_bid bitwise; then the time of each. GPU, synthetic. 3#include "strata/kernels/qsa.hpp" 4#include "strata/kernels/qsa_select.hpp" 5#include <cuda_runtime.h> 6#include <cmath> 7#include <cstdint> 8#include <cstdio> 9#include <cstdlib> 10#include <cstring> 11#include <random> 12#include <vector> 13 14namespace k= strata::kernels; 15namespace { 16void ck (cudaError_t e ) { 17if (e != cudaSuccess ) { 18 std::fprintf (stderr ,"CUDA: %s\n" ,cudaGetErrorString (e )); 19 std::exit (2 ); 20 } 21} 22// ctx: the last query's position + 1; queries: a prompt chunk ending there; mode 0 normal values, 1 wide magnitudes 23// and exact zeros, 2 small integers (many ties and cancellations) 24struct Case {int64_t ctx ,queries ;int mode ;bool time ; }; 25bool check (Case c ) { 26const auto s = k::qsa_real_shapes (); 27const int64_t D = s .idx_dim ,H = s .idx_n_head ,max_blocks = c .ctx /s .idx_block + 2 ; 28 std::mt19937 rng (91731 + (unsigned )c .ctx + 7 * (unsigned )c .queries + 101 * (unsigned )c .mode ); 29 std::normal_distribution < float > nd (0.f ,1.f ); 30 std::uniform_int_distribution < int > small (-3 ,3 ),expo (-20 ,20 ),coin (0 ,9 ); 31auto value = [& ] { 32if (c .mode == 1 )return coin (rng )== 0 ?0.f : std::ldexp (nd (rng ),expo (rng )); 33if (c .mode == 2 )return (float )small (rng ); 34return nd (rng ); 35 }; 36 std::vector < float > pooled ((size_t ) (max_blocks * D )),dead ((size_t )D ),q ((size_t ) (c .queries * H * D )); 37for (auto & v :pooled )v = value (); 38for (auto & v :dead )v = value (); 39for (auto & v :q )v = value (); 40 std::vector < int32_t > steps ((size_t ) (c .queries * k::kStepCount )); 41for (int64_t i = 0 ;i < c .queries ;++ i ) { 42int32_t * st = steps .data ()+ i * k::kStepCount ; 43const int64_t pos = c .ctx - c .queries + i ; 44st [k::kStepPos ]= (int32_t )pos ; 45st [k::kStepNKv ]= (int32_t ) (pos + 1 ); 46st [k::kStepNBid ]= (int32_t ) ((pos + 1 ) /s .idx_block ); 47st [k::kStepWidth ]= (int32_t ) k::qsa_selection_width (pos + 1 ,s ); 48 } 49const int64_t active = steps [(size_t ) ((c .queries - 1 )* k::kStepCount + k::kStepNBid )]+ 1 ; 50float * dp = nullptr ,* dd = nullptr ,* dq = nullptr ,* da = nullptr ,* db = nullptr ; 51int32_t * dt = nullptr ; 52const size_t n_out = (size_t ) (c .queries * max_blocks ); 53ck (cudaMalloc (& dp ,pooled .size ()* 4 ));ck (cudaMalloc (& dd ,dead .size ()* 4 ));ck (cudaMalloc (& dq ,q .size ()* 4 )); 54ck (cudaMalloc (& dt ,steps .size ()* 4 ));ck (cudaMalloc (& da ,n_out * 4 ));ck (cudaMalloc (& db ,n_out * 4 )); 55ck (cudaMemcpy (dp ,pooled .data (),pooled .size ()* 4 ,cudaMemcpyHostToDevice )); 56ck (cudaMemcpy (dd ,dead .data (),dead .size ()* 4 ,cudaMemcpyHostToDevice )); 57ck (cudaMemcpy (dq ,q .data (),q .size ()* 4 ,cudaMemcpyHostToDevice )); 58ck (cudaMemcpy (dt ,steps .data (),steps .size ()* 4 ,cudaMemcpyHostToDevice )); 59ck (cudaMemset (da ,0x7f ,n_out * 4 ));// different fill in each: an unwritten score cannot match by chance 60ck (cudaMemset (db ,0x3e ,n_out * 4 )); 61 k::qsa_block_scores (dp ,dd ,dq ,dt ,c .queries ,max_blocks ,s ,da ,nullptr );// block_scores_kernel 62 k::qsa_block_scores (dp ,dd ,dq ,dt ,c .queries ,max_blocks ,s ,db ,nullptr ,active );// the thread kernel 63ck (cudaDeviceSynchronize ()); 64 std::vector < uint32_t > a (n_out ),b (n_out ); 65ck (cudaMemcpy (a .data (),da ,n_out * 4 ,cudaMemcpyDeviceToHost )); 66ck (cudaMemcpy (b .data (),db ,n_out * 4 ,cudaMemcpyDeviceToHost )); 67bool ok = true; 68int64_t checked = 0 ; 69for (int64_t i = 0 ;i < c .queries && ok ;++ i ) { 70const int64_t n_bid = steps [(size_t ) (i * k::kStepCount + k::kStepNBid )]; 71for (int64_t j = 0 ;j <=n_bid ;++ j ,++ checked ) { 72const size_t at = (size_t ) (i * max_blocks + j ); 73if (a [at ]!= b [at ]) { 74float fa ,fb ; 75 std::memcpy (& fa ,& a [at ],4 ); std::memcpy (& fb ,& b [at ],4 ); 76 std::fprintf (stderr ,"FAIL ctx=%lld queries=%lld mode=%d query=%lld block=%lld: %.9g vs %.9g\n" , 77 (long long )c .ctx , (long long )c .queries ,c .mode , (long long )i , (long long )j ,fa ,fb ); 78ok = false; 79break ; 80 } 81 } 82 } 83if (ok ) std::printf ("ok ctx=%7lld queries=%5lld mode=%d: %lld scores identical\n" , (long long )c .ctx , 84 (long long )c .queries ,c .mode , (long long )checked ); 85if (ok && c .time ) { 86cudaEvent_t e0 ,e1 ,e2 ; 87ck (cudaEventCreate (& e0 ));ck (cudaEventCreate (& e1 ));ck (cudaEventCreate (& e2 )); 88constexpr int N = 5 ; 89ck (cudaEventRecord (e0 )); 90for (int r = 0 ;r < N ;++ r ) k::qsa_block_scores (dp ,dd ,dq ,dt ,c .queries ,max_blocks ,s ,da ,nullptr ); 91ck (cudaEventRecord (e1 )); 92for (int r = 0 ;r < N ;++ r ) k::qsa_block_scores (dp ,dd ,dq ,dt ,c .queries ,max_blocks ,s ,db ,nullptr ,active ); 93ck (cudaEventRecord (e2 )); 94ck (cudaEventSynchronize (e2 )); 95float t_old = 0.f ,t_new = 0.f ; 96ck (cudaEventElapsedTime (& t_old ,e0 ,e1 ));ck (cudaEventElapsedTime (& t_new ,e1 ,e2 )); 97 std::printf (" time per call: warp kernel %.2f ms, thread kernel %.2f ms (%.2fx)\n" ,t_old /N ,t_new /N , 98t_old /t_new ); 99cudaEventDestroy (e0 );cudaEventDestroy (e1 );cudaEventDestroy (e2 ); 100 } 101ck (cudaFree (dp ));ck (cudaFree (dd ));ck (cudaFree (dq ));ck (cudaFree (dt ));ck (cudaFree (da ));ck (cudaFree (db )); 102return ok ; 103} 104}// namespace 105 106int main () { 107int devices = 0 ; 108if (cudaGetDeviceCount (& devices )!= cudaSuccess || devices == 0 ) { 109 std::puts ("SKIP: no CUDA device" ); 110return 77 ; 111 } 112const Case cases []= { 113 {1111 ,9 ,0 , false}, {1111 ,64 ,1 , false}, {8192 ,65 ,0 , false}, {8195 ,200 ,2 , false}, 114 {32768 ,256 ,0 , false}, {32771 ,1000 ,1 , false}, {131072 ,64 ,2 , false}, {131075 ,333 ,0 , false}, 115 {262144 ,129 ,1 , false}, {40000 ,2048 ,0 , true}, {140000 ,1024 ,0 , true}, {200000 ,1024 ,0 , true}, 116 }; 117for (const auto c :cases )if (!check (c ))return 1 ; 118 std::puts ("PASS: block scores of the thread kernel bitwise identical to block_scores_kernel" ); 119return 0 ; 120}