okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
d96873fc5
main
1// pa_bench - the prompt path's sparse attention (qsa_prompt_attn_batch) on a recorded selection: a STRATA_QSA_DUMP 2// record of one QSA layer and chunk (int32 {layer, p0, T, cap}, T * cap cells, then T * kStepCount steps, written 3// with STRATA_QSA_DUMP_STEPS=1), random q and int8 K/V (the values do not change the work), one page per 4 cells. 4// usage: pa_bench DUMP [reps] - prints the median time and a hash of the output (for A/B of kernel variants) 5#include "strata/kernels/qsa.hpp" 6#include "strata/kernels/qsa_decode_attn.hpp" 7#include "strata/kernels/qsa_prompt_attn.hpp" 8 9#include <cuda_runtime.h> 10 11#include <algorithm> 12#include <cmath> 13#include <cstdint> 14#include <cstdio> 15#include <cstdlib> 16#include <cstring> 17#include <random> 18#include <vector> 19 20#define CK (x ) do { cudaError_t e_ = (x); if (e_ != cudaSuccess) { std::fprintf(stderr, "%s: %s\n", #x, cudaGetErrorString(e_)); return 1; } } while (0) 21 22int main (int argc ,char ** argv ) { 23if (argc < 2 ) { std::fprintf (stderr ,"usage: pa_bench DUMP [reps]\n" );return 2 ; } 24const int reps = argc > 2 ? std::atoi (argv [2 ]) :5 ; 25 std::FILE * f = std::fopen (argv [1 ],"rb" ); 26if (!f ) { std::perror (argv [1 ]);return 1 ; } 27int32_t hdr [4 ]; 28if (std::fread (hdr ,4 ,4 ,f )!= 4 )return 1 ; 29const int64_t p0 = hdr [1 ],T = hdr [2 ],cap = hdr [3 ]; 30 std::vector < int32_t > ids ((size_t ) (T * cap )),steps ((size_t ) (T * strata::kernels::kStepCount )); 31if (std::fread (ids .data (),4 ,ids .size (),f )!= ids .size ()|| std::fread (steps .data (),4 ,steps .size (),f )!= steps .size ()) { 32 std::fprintf (stderr ,"short dump (written without STRATA_QSA_DUMP_STEPS?)\n" ); 33return 1 ; 34 } 35 std::fclose (f ); 36 strata::kernels::QsaShapes s = strata::kernels::qsa_real_shapes (); 37const int64_t cells = p0 + T ,HD = s .head_dim ,KVH = s .n_head_kv ,NH = s .n_head ; 38const int64_t pages = (cells + s .page_size - 1 ) /s .page_size ; 39 std::printf ("layer %d p0 %lld T %lld cap %lld, %lld cells, %lld heads / %lld KV heads x %lld\n" ,hdr [0 ], (long long )p0 , 40 (long long )T , (long long )cap , (long long )cells , (long long )NH , (long long )KVH , (long long )HD ); 41 std::mt19937 rng (7 ); 42 std::normal_distribution < float > nd (0.0f ,1.0f ); 43 std::uniform_int_distribution < int > u8 (-127 ,127 ); 44 std::vector < float > q ((size_t ) (T * NH * HD )); 45for (auto & x :q )x = nd (rng ); 46const size_t kv_n = (size_t ) (pages * KVH * s .page_size * HD ); 47 std::vector < int8_t > kq (kv_n ),vq (kv_n ); 48for (auto & x :kq )x = (int8_t )u8 (rng ); 49for (auto & x :vq )x = (int8_t )u8 (rng ); 50const size_t sc_n = (size_t ) (pages * KVH * s .page_size * (HD /64 )); 51 std::vector < uint16_t > ks (sc_n ),vs (sc_n ); 52for (size_t i = 0 ;i < sc_n ;++ i ) {ks [i ]= 0x2000 + (uint16_t ) (i %512 );vs [i ]= 0x2000 + (uint16_t ) ((i * 7 ) %512 ); } 53 std::vector < int32_t > pt ((size_t )pages ); 54for (int64_t i = 0 ;i < pages ;++ i )pt [(size_t )i ]= (int32_t )i ; 55 56float * dq ,* dattn ; 57int8_t * dk ,* dv ; 58uint16_t * dks ,* dvs ; 59int32_t * dids ,* dsteps ,* dpt ; 60CK (cudaMalloc (& dq ,q .size ()* 4 )); 61CK (cudaMalloc (& dattn , (size_t ) (T * NH * HD )* 4 )); 62CK (cudaMalloc (& dk ,kv_n )); 63CK (cudaMalloc (& dv ,kv_n )); 64CK (cudaMalloc (& dks ,sc_n * 2 )); 65CK (cudaMalloc (& dvs ,sc_n * 2 )); 66CK (cudaMalloc (& dids ,ids .size ()* 4 )); 67CK (cudaMalloc (& dsteps ,steps .size ()* 4 )); 68CK (cudaMalloc (& dpt ,pt .size ()* 4 )); 69CK (cudaMemcpy (dq ,q .data (),q .size ()* 4 ,cudaMemcpyHostToDevice )); 70CK (cudaMemcpy (dk ,kq .data (),kv_n ,cudaMemcpyHostToDevice )); 71CK (cudaMemcpy (dv ,vq .data (),kv_n ,cudaMemcpyHostToDevice )); 72CK (cudaMemcpy (dks ,ks .data (),sc_n * 2 ,cudaMemcpyHostToDevice )); 73CK (cudaMemcpy (dvs ,vs .data (),sc_n * 2 ,cudaMemcpyHostToDevice )); 74CK (cudaMemcpy (dids ,ids .data (),ids .size ()* 4 ,cudaMemcpyHostToDevice )); 75CK (cudaMemcpy (dsteps ,steps .data (),steps .size ()* 4 ,cudaMemcpyHostToDevice )); 76CK (cudaMemcpy (dpt ,pt .data (),pt .size ()* 4 ,cudaMemcpyHostToDevice )); 77 strata::kernels::QsaAttnPools pools ; 78pools .k_q = dk ;pools .v_q = dv ;pools .k_scale = dks ;pools .v_scale = dvs ;pools .page_table = dpt ; 79cudaStream_t st ; 80CK (cudaStreamCreate (& st )); 81cudaEvent_t e0 ,e1 ; 82CK (cudaEventCreate (& e0 )); 83CK (cudaEventCreate (& e1 )); 84 std::vector < float > ms ; 85for (int r = 0 ;r < reps + 1 ;++ r ) { 86CK (cudaEventRecord (e0 ,st )); 87if (!strata::kernels::qsa_prompt_attn_batch (dq ,pools ,dids ,dsteps ,cap ,s ,dattn ,T ,st )) { 88 std::fprintf (stderr ,"qsa_prompt_attn_batch declined\n" ); 89return 1 ; 90 } 91CK (cudaEventRecord (e1 ,st )); 92CK (cudaEventSynchronize (e1 )); 93float t = 0 ; 94CK (cudaEventElapsedTime (& t ,e0 ,e1 )); 95if (r > 0 )ms .push_back (t ); 96 } 97 std::sort (ms .begin (),ms .end ()); 98 std::vector < float > out ((size_t ) (T * NH * HD )); 99CK (cudaMemcpy (out .data (),dattn ,out .size ()* 4 ,cudaMemcpyDeviceToHost )); 100uint64_t h = 1469598103934665603ull ; 101for (float x :out ) {uint32_t b ; std::memcpy (& b ,& x ,4 );h = (h ^b )* 1099511628211ull ; } 102double ss = 0 ; 103for (float x :out )ss += (double )x * x ; 104 std::printf ("prompt attention: median %.3f ms (min %.3f) over %d; output hash %016llx, rms %.6g\n" ,ms [ms .size () /2 ], 105ms [0 ],reps , (unsigned long long )h , std::sqrt (ss / (double )out .size ())); 106if (const char * rf = std::getenv ("PA_BENCH_REF" )) {// another variant's PA_BENCH_OUT: relative RMS and worst difference 107 std::vector < float > ref (out .size ()); 108if (std::FILE * g = std::fopen (rf ,"rb" )) { 109const size_t got = std::fread (ref .data (),4 ,ref .size (),g ); 110 std::fclose (g ); 111if (got == ref .size ()) { 112double e2 = 0 ,r2 = 0 ,worst = 0 ; 113for (size_t i = 0 ;i < out .size ();++ i ) { 114const double d = (double )out [i ]- ref [i ]; 115e2 += d * d ;r2 += (double )ref [i ]* ref [i ]; 116worst = std::max (worst , std::fabs (d )); 117 } 118 std::printf ("against %s: relative RMS %.3e, worst |diff| %.3e (reference rms %.4g)\n" ,rf , std::sqrt (e2 / std::max (r2 ,1e-30 )), 119worst , std::sqrt (r2 / (double )out .size ())); 120 }else std::printf ("PA_BENCH_REF %s: short file\n" ,rf ); 121 } 122 } 123if (const char * o = std::getenv ("PA_BENCH_OUT" )) {// the output, for an accuracy comparison of two variants 124if (std::FILE * g = std::fopen (o ,"wb" )) { std::fwrite (out .data (),4 ,out .size (),g ); std::fclose (g ); } 125 } 126return 0 ; 127}