okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
7c5e84612
main
1// src/kernels/b6_mmvq_bench.cpp - B6: what a verify row costs in the dense projections, per kernel, per card. 2// 3// build/b6_mmvq_bench [reps] [device, -1 = all] [B rows mode: 1 = 2 rows, or 2 / 4 / 8] 4// 5// The decode's dense projections at the model's own shapes and formats (qwen3.8-flash-next GSQ-RCO IQ3_XXS: 6// GDN qkv / gate / out, QSA q / out, shared expert, the head; the BF16 router), called with ncols = 1..5 and 8 as a 7// verify window of that many rows calls them. Per call: CUDA events around each launch, every launch queued back to 8// back (no host gap inside a measurement), the median of `reps` (default 500) after a warm-up. The weights rotate 9// over enough copies to exceed the L2, as the decode reads each layer's weights cold. Synthetic bytes with sane 10// block scales (as mmvq_multi_parity): the time does not depend on the values. 11// 12// Two layouts side by side: the default exact layout (A) and STRATA_B6_MMVQ_ROWS (B, native_mmvq_set_b6_rows). For 13// every case and ncols: B's output against A's, bit for bit (memcmp), and both against ncols single-column calls. 14// Exit code 1 if any comparison differs. GPU time only: immune to the host and to any generated text. 15#include "strata/kernels/bf16_gemv.hpp" 16#include "strata/kernels/iq_kernels.hpp" 17#include "strata/kernels/native_mmvq.hpp" 18 19#include <cuda_runtime.h> 20 21#include <algorithm> 22#include <cmath> 23#include <cstdint> 24#include <cstdio> 25#include <cstdlib> 26#include <cstring> 27#include <random> 28#include <vector> 29 30namespace K = strata::kernels; 31 32namespace { 33 34struct Case { 35const char * name ; 36int type ;// GGML type id; 30 = BF16 (bf16_gemv_fp32_mmvf_multi) 37int n_in ,n_out ; 38int block_elems ,block_bytes ; 39int scale_at [2 ];// byte offsets of the fp16 scales in a block; -1 = none 40}; 41 42const Case CASES []= { 43 {"gdn qkv Q6_K" ,14 ,2560 ,10240 ,256 ,210 , {208 ,-1 }}, 44 {"gdn qkv Q4_K" ,12 ,2560 ,10240 ,256 ,144 , {0 ,2 }}, 45 {"gdn qkv Q5_K" ,13 ,2560 ,10240 ,256 ,176 , {0 ,2 }}, 46 {"gdn qkv IQ4_XS" ,23 ,2560 ,10240 ,256 ,136 , {0 ,-1 }}, 47 {"gdn gate Q6_K" ,14 ,2560 ,6144 ,256 ,210 , {208 ,-1 }}, 48 {"gdn out Q6_K" ,14 ,6144 ,2560 ,256 ,210 , {208 ,-1 }}, 49 {"gdn out IQ4_XS" ,23 ,6144 ,2560 ,256 ,136 , {0 ,-1 }}, 50 {"gdn out Q4_K" ,12 ,6144 ,2560 ,256 ,144 , {0 ,2 }}, 51 {"qsa q IQ4_XS" ,23 ,2560 ,12288 ,256 ,136 , {0 ,-1 }}, 52 {"qsa q Q6_K" ,14 ,2560 ,12288 ,256 ,210 , {208 ,-1 }}, 53 {"shexp up IQ3_S" ,21 ,2560 ,640 ,256 ,110 , {0 ,-1 }}, 54 {"shexp gate Q4_K" ,12 ,2560 ,640 ,256 ,144 , {0 ,2 }}, 55 {"shexp down IQ4_NL" ,20 ,640 ,2560 ,32 ,18 , {0 ,-1 }}, 56 {"router BF16" ,30 ,2560 ,512 ,1 ,2 , {-1 ,-1 }}, 57 {"head Q5_K" ,13 ,2560 ,248320 ,256 ,176 , {0 ,2 }}, 58}; 59const int WIDTHS []= {1 ,2 ,3 ,4 ,5 ,8 }; 60 61bool ck (cudaError_t e ,const char * what ) { 62if (e == cudaSuccess )return true; 63 std::printf ("CUDA: %s: %s\n" ,what ,cudaGetErrorString (e )); 64return false; 65} 66 67uint16_t sane_half (std::mt19937 & rng ) { 68const uint32_t r = rng (); 69return (uint16_t ) (((r >>31 ) <<15 ) | ((5u + (r >>10 ) %5u ) <<10 ) | (r & 0x3ffu )); 70} 71uint16_t bf16 (float x ) { 72uint32_t u ; 73 std::memcpy (& u ,& x ,4 ); 74return (uint16_t ) ((u + 0x7fff + ((u >>16 )& 1 )) >>16 ); 75} 76 77struct Buf { 78 std::vector < void *> w ;// weight copies 79 std::size_t wbytes = 0 ; 80void * xq = nullptr ;// Q8_1 of 8 columns 81float * xf = nullptr ;// the float activations (BF16 path) 82float * y = nullptr ;// 8 columns of output 83}; 84 85// one call of the case at `ncols` on weight copy `k` into y 86void call (const Case & c ,const Buf & b ,int k ,int ncols ,float * y ,cudaStream_t s ) { 87if (c .type == 30 ) 88K ::bf16_gemv_fp32_mmvf_multi (b .xf ,c .n_in , (const uint16_t * )b .w [(size_t )k ],y ,c .n_out ,c .n_in ,c .n_out ,ncols ,s ); 89else 90K ::native_mmvq (c .type ,b .w [(size_t )k ],b .xq ,y ,c .n_in ,c .n_out ,ncols ,s ); 91} 92// column j alone (ncols = 1), into y + j * n_out 93void call_col (const Case & c ,const Buf & b ,int j ,float * y ,cudaStream_t s ) { 94if (c .type == 30 ) 95K ::bf16_gemv_fp32_mmvf_multi (b .xf + (size_t )j * c .n_in ,c .n_in , (const uint16_t * )b .w [0 ], 96y + (size_t )j * c .n_out ,c .n_out ,c .n_in ,c .n_out ,1 ,s ); 97else 98K ::native_mmvq (c .type ,b .w [0 ], (const uint8_t * )b .xq + K ::native_q8_1_bytes (c .n_in ,1 )* (size_t )j , 99y + (size_t )j * c .n_out ,c .n_in ,c .n_out ,1 ,s ); 100} 101 102double median_ms (const Case & c ,const Buf & b ,int ncols ,int reps ,cudaStream_t s , std::vector < cudaEvent_t >& ev ) { 103const int nk = (int )b .w .size (); 104for (int r = 0 ;r < 30 ;++ r )call (c ,b ,r %nk ,ncols ,b .y ,s );// warm-up 105for (int r = 0 ;r < reps ;++ r ) { 106cudaEventRecord (ev [(size_t )2 * r ],s ); 107call (c ,b ,r %nk ,ncols ,b .y ,s ); 108cudaEventRecord (ev [(size_t )2 * r + 1 ],s ); 109 } 110cudaStreamSynchronize (s ); 111 std::vector < float > t ((size_t )reps ); 112for (int r = 0 ;r < reps ;++ r )cudaEventElapsedTime (& t [(size_t )r ],ev [(size_t )2 * r ],ev [(size_t )2 * r + 1 ]); 113 std::nth_element (t .begin (),t .begin ()+ reps /2 ,t .end ()); 114return t [(size_t )reps /2 ]; 115} 116 117}// namespace 118 119int main (int argc ,char ** argv ) { 120setvbuf (stdout ,nullptr ,_IONBF ,0 ); 121const int reps = argc > 1 ? std::max (50 , std::atoi (argv [1 ])) :500 ; 122int n_dev = 0 ; 123if (!ck (cudaGetDeviceCount (& n_dev ),"device count" ))return 1 ; 124const int only = argc > 2 ? std::atoi (argv [2 ]) :-1 ; 125const int bmode = argc > 3 ? std::atoi (argv [3 ]) :1 ; 126long long bad = 0 ; 127for (int dev = 0 ;dev < n_dev ;++ dev ) { 128if (only >=0 && dev != only )continue ; 129cudaSetDevice (dev ); 130cudaDeviceProp pr {}; 131cudaGetDeviceProperties (& pr ,dev ); 132 std::printf ("\n=== device %d: %s (sm_%d%d, L2 %d KB), %d reps, median ms per call, B = rows mode %d ===\n" ,dev ,pr .name , 133pr .major ,pr .minor ,pr .l2CacheSize /1024 ,reps ,bmode ); 134 std::printf ("%-18s %4s %9s %9s %8s %8s %s\n" ,"case" ,"cols" ,"A ms" ,"B ms" ,"A +col" ,"B +col" , 135"bitwise B=A, A=1col, B=1col" ); 136cudaStream_t s ; 137if (!ck (cudaStreamCreate (& s ),"stream" ))return 1 ; 138 std::vector < cudaEvent_t > ev ((size_t )2 * reps ); 139for (auto & e :ev )cudaEventCreate (& e ); 140for (const Case & c :CASES ) { 141if (c .type != 30 && !K ::native_mmvq_supported (c .type )) { 142 std::printf ("%-18s unsupported type %d\n" ,c .name ,c .type ); 143continue ; 144 } 145 std::mt19937 rng (77u + (unsigned )c .type * 131u + (unsigned )c .n_out ); 146Buf b ; 147b .wbytes = c .type == 30 ? (size_t )c .n_in * c .n_out * 2 :K ::native_mmvq_weight_bytes (c .type ,c .n_in ,c .n_out ); 148 std::vector < uint8_t > w (b .wbytes ); 149if (c .type == 30 ) { 150 std::normal_distribution < float > nd (0.f ,0.05f ); 151auto * p = (uint16_t * )w .data (); 152for (size_t i = 0 ;i < b .wbytes /2 ;++ i )p [i ]= bf16 (nd (rng )); 153 }else { 154for (auto & x :w )x = (uint8_t ) (rng ()& 0xff ); 155const size_t nb = (size_t )c .n_out * (size_t ) (c .n_in /c .block_elems ); 156for (size_t k = 0 ;k < nb ;++ k ) 157for (int at :c .scale_at ) 158if (at >=0 ) { 159const uint16_t h = sane_half (rng ); 160 std::memcpy (& w [k * (size_t )c .block_bytes + (size_t )at ],& h ,2 ); 161 } 162 } 163const size_t copies = std::max < size_t > (1 , std::min < size_t > (8 , (96u <<20 ) /b .wbytes + 1 )); 164bool ok = true; 165for (size_t k = 0 ;k < copies && ok ;++ k ) { 166void * d = nullptr ; 167ok = ck (cudaMalloc (& d ,b .wbytes ),"malloc w" )&& ck (cudaMemcpy (d ,w .data (),b .wbytes ,cudaMemcpyHostToDevice ),"copy w" ); 168b .w .push_back (d ); 169 } 170 std::vector < float > x ((size_t )8 * c .n_in ); 171 std::normal_distribution < float > nx (0.f ,1.f ); 172for (auto & v :x )v = nx (rng ); 173const size_t ybytes = (size_t )8 * c .n_out * 4 ; 174ok = ok && ck (cudaMalloc (& b .xf ,x .size ()* 4 ),"malloc x" )&& 175ck (cudaMemcpy (b .xf ,x .data (),x .size ()* 4 ,cudaMemcpyHostToDevice ),"copy x" )&& 176ck (cudaMalloc (& b .y ,ybytes ),"malloc y" ); 177if (ok && c .type != 30 ) { 178ok = ck (cudaMalloc (& b .xq ,K ::native_q8_1_bytes (c .n_in ,8 )),"malloc xq" ); 179if (ok )K ::quantize_q8_1_rows (b .xf ,8 ,c .n_in ,b .xq ,s ); 180 } 181float * ya = nullptr ,* yb = nullptr ,* y1 = nullptr ; 182ok = ok && ck (cudaMalloc (& ya ,ybytes ),"ya" )&& ck (cudaMalloc (& yb ,ybytes ),"yb" )&& ck (cudaMalloc (& y1 ,ybytes ),"y1" ); 183if (!ok )return 1 ; 184double a1 = 0 ,b1 = 0 ; 185for (int T :WIDTHS ) { 186 std::vector < uint32_t > ha ((size_t )T * c .n_out ),hb (ha .size ()),h1 (ha .size ()); 187K ::native_mmvq_set_b6_rows (0 ); 188const double ta = median_ms (c ,b ,T ,reps ,s ,ev ); 189call (c ,b ,0 ,T ,ya ,s ); 190for (int j = 0 ;j < T ;++ j )call_col (c ,b ,j ,y1 ,s ); 191K ::native_mmvq_set_b6_rows (bmode ); 192const double tb = median_ms (c ,b ,T ,reps ,s ,ev ); 193call (c ,b ,0 ,T ,yb ,s ); 194K ::native_mmvq_set_b6_rows (0 ); 195if (!ck (cudaStreamSynchronize (s ),"run" ))return 1 ; 196cudaMemcpy (ha .data (),ya ,ha .size ()* 4 ,cudaMemcpyDeviceToHost ); 197cudaMemcpy (hb .data (),yb ,hb .size ()* 4 ,cudaMemcpyDeviceToHost ); 198cudaMemcpy (h1 .data (),y1 ,h1 .size ()* 4 ,cudaMemcpyDeviceToHost ); 199const bool ba = std::memcmp (ha .data (),hb .data (),ha .size ()* 4 )== 0 ; 200const bool a_1 = std::memcmp (ha .data (),h1 .data (),ha .size ()* 4 )== 0 ; 201const bool b_1 = std::memcmp (hb .data (),h1 .data (),hb .size ()* 4 )== 0 ; 202long long nonfinite = 0 ; 203for (uint32_t u :ha ) {float f ; std::memcpy (& f ,& u ,4 );if (!std::isfinite (f ))++ nonfinite ; } 204if (!ba || !b_1 || nonfinite )++ bad ; 205if (T == 1 ) {a1 = ta ;b1 = tb ; } 206 std::printf ("%-18s %4d %9.4f %9.4f %8.4f %8.4f %s %s %s%s\n" ,c .name ,T ,ta ,tb , 207T > 1 ? (ta - a1 ) / (T - 1 ) :0.0 ,T > 1 ? (tb - b1 ) / (T - 1 ) :0.0 ,ba ?"ok" :"DIFF" , 208a_1 ?"ok" :"diff" ,b_1 ?"ok" :"DIFF" ,nonfinite ?" NONFINITE" :"" ); 209 } 210for (void * d :b .w )cudaFree (d ); 211cudaFree (b .xq );cudaFree (b .xf );cudaFree (b .y );cudaFree (ya );cudaFree (yb );cudaFree (y1 ); 212 } 213for (auto & e :ev )cudaEventDestroy (e ); 214cudaStreamDestroy (s ); 215 } 216 std::printf ("\nb6_mmvq_bench: %s (%lld case/width comparisons failed)\n" ,bad ?"FAILED" :"ok" ,bad ); 217return bad ?1 :0 ; 218}