okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7c5e84612
main
1// Exact multi-row BF16/F32 GEMV checks against independent single-row calls. 2// Run with STRATA_MMVF_ROWS=1; synthetic data, no model or private prompts. 3#include "strata/kernels/bf16_gemv.hpp" 4#include <cuda_runtime.h> 5#include <cstdint> 6#include <cstdio> 7#include <cstdlib> 8#include <cstring> 9#include <random> 10#include <vector> 11 12namespace k= strata::kernels; 13static void ck (cudaError_t e ) { 14if (e != cudaSuccess ) { std::fprintf (stderr ,"%s\n" ,cudaGetErrorString (e )); std::exit (2 ); } 15} 16template < class T > static T * alloc (size_t n ) { 17T * p = nullptr ;ck (cudaMalloc (& p ,n * sizeof (T )));return p ; 18} 19int main () { 20const char * flag = std::getenv ("STRATA_MMVF_ROWS" ); 21if (!flag || flag [0 ]!= '1' ) { std::fprintf (stderr ,"set STRATA_MMVF_ROWS=1\n" );return 2 ; } 22cudaStream_t s ;ck (cudaStreamCreate (& s )); 23const int shapes [][2 ]= {{64 ,64 },{192 ,67 },{512 ,65 },{640 ,128 },{2560 ,48 }, 24 {2560 ,128 },{2560 ,512 },{2560 ,2560 },{10240 ,320 }}; 25size_t cases = 0 ,values = 0 ,mismatches = 0 ; 26for (const auto & shape :shapes )for (int nt = 1 ;nt <=8 ;++ nt ) 27for (int pad : {0 ,2 })for (int range : {0 ,1 ,2 }) { 28const int ni = shape [0 ],no = shape [1 ],ldx = ni + pad ,ldy = no + pad + 1 ; 29 std::mt19937 rng (1234 + nt * 17 + pad * 31 + range * 53 + ni + no ); 30 std::uniform_real_distribution < float > d (-1.f ,1.f ); 31const float xs = range == 0 ?1.f :range == 1 ?1.e-12f :1.e12f ; 32const float ws = range == 0 ?1.f :range == 1 ?1.e-8f :1.e8f ; 33 std::vector < float > x ((size_t )nt * ldx ); 34 std::vector < uint16_t > w ((size_t ) (no + 1 )* ni ); 35for (auto & v :x )v = d (rng )* xs ; 36for (auto & v :w ) {const float f = d (rng )* ws ;uint32_t b ; std::memcpy (& b ,& f ,4 );v = uint16_t (b >>16 ); } 37float * dx = alloc < float > (x .size ());uint16_t * dw = alloc < uint16_t > (w .size ()); 38const size_t count = (size_t )nt * ldy ; 39float * ref = alloc < float > (count );float * got = alloc < float > (count ); 40float * aux = alloc < float > ((size_t )nt * 3 );float * aref = alloc < float > ((size_t )nt * 3 ); 41ck (cudaMemcpy (dx ,x .data (),x .size ()* 4 ,cudaMemcpyHostToDevice )); 42ck (cudaMemcpy (dw ,w .data (),w .size ()* 2 ,cudaMemcpyHostToDevice )); 43ck (cudaMemset (ref ,0xff ,count * 4 ));ck (cudaMemset (got ,0xff ,count * 4 )); 44ck (cudaMemset (aux ,0xff ,(size_t )nt * 12 ));ck (cudaMemset (aref ,0xff ,(size_t )nt * 12 )); 45for (int t = 0 ;t < nt ;++ t ) { 46 k::bf16_gemv_fp32_mmvf (dx + (size_t )t * ldx ,dw ,ref + (size_t )t * ldy ,ni ,no ,s ); 47 k::bf16_gemv_fp32_mmvf (dx + (size_t )t * ldx ,dw + (size_t )no * ni ,aref + t * 3 ,ni ,1 ,s ); 48 } 49ck (cudaStreamSynchronize (s )); 50cudaGraph_t graph ;cudaGraphExec_t exec ; 51ck (cudaStreamBeginCapture (s ,cudaStreamCaptureModeThreadLocal )); 52 k::bf16_gemv_fp32_mmvf_multi (dx ,ldx ,dw ,got ,ldy ,ni ,no ,nt ,s ); 53ck (cudaStreamEndCapture (s ,& graph ));ck (cudaGraphInstantiate (& exec ,graph ,0 )); 54ck (cudaGraphLaunch (exec ,s ));ck (cudaStreamSynchronize (s )); 55auto compare = [& ](float * a ,float * b ,size_t n ) { 56 std::vector < uint32_t > aa (n ),bb (n ); 57ck (cudaMemcpy (aa .data (),a ,n * 4 ,cudaMemcpyDeviceToHost )); 58ck (cudaMemcpy (bb .data (),b ,n * 4 ,cudaMemcpyDeviceToHost )); 59size_t bad = 0 ;for (size_t i = 0 ;i < n ;++ i )bad += aa [i ]!= bb [i ]; 60values += n ;mismatches += bad ; 61if (bad ) std::printf ("FAIL ni=%d no=%d nt=%d pad=%d range=%d different=%zu\n" ,ni ,no ,nt ,pad ,range ,bad ); 62 }; 63compare (ref ,got ,count ); 64ck (cudaMemset (got ,0xff ,count * 4 )); 65const bool fused = k::bf16_gemv_fp32_mmvf_multi_aux (dx ,ldx ,dw ,got ,ldy ,ni ,no ,nt , 66dw + (size_t )no * ni ,aux ,3 ,s ); 67if (fused ) {ck (cudaStreamSynchronize (s ));compare (ref ,got ,count );compare (aref ,aux ,(size_t )nt * 3 ); } 68if (fused != (nt >=2 && no >=64 )) { std::printf ("FAIL eligibility\n" );++ mismatches ; } 69ck (cudaGraphExecDestroy (exec ));ck (cudaGraphDestroy (graph )); 70ck (cudaFree (dx ));ck (cudaFree (dw ));ck (cudaFree (ref ));ck (cudaFree (got ));ck (cudaFree (aux ));ck (cudaFree (aref )); 71++ cases ; 72 } 73ck (cudaStreamDestroy (s )); 74 std::printf ("mmvf_rows_parity: %zu cases, %zu values, %zu mismatches\n" ,cases ,values ,mismatches ); 75return mismatches ?1 :0 ; 76}