okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6ca60e9a0
main
1// src/kernels/tq_pascal_bench.cpp - the Gyro trellis decode kernels (TQ2_T 144, TQK6 145, TQK7 146 and Gyro-L's TQP5-7 147-149) on synthetic blocks. 2// 3// build/tq_pascal_bench checks: the decode-once kernels against the per-column ones (bitwise), and a 4// checksum of every output, so two builds can be compared byte for byte 5// build/tq_pascal_bench --bench the checks, then microseconds per call 6// 7// Random bytes are valid trellis codes (any 15-bit state indexes the codebook); only the fp16 block scales are set, 8// small enough that the grouped path's SwiGLU output keeps a finite fp16 q8_1 scale. No model, needs the GPU. 9// The checksum lines ("SUM ...") are what two builds are compared by: same lines, same bits. 10#include "strata/kernels/f16_bits.hpp" 11#include "strata/kernels/iq_kernels.hpp" 12#include "strata/kernels/native_mmvq.hpp" 13 14#include "ggml-common-tq.h" // tq_lut_f16: the codebook, as host data 15 16#include <cuda_runtime.h> 17 18#include <algorithm> 19#include <cmath> 20#include <cstdint> 21#include <cstdio> 22#include <cstdlib> 23#include <cstring> 24#include <numeric> 25#include <random> 26#include <string> 27#include <vector> 28 29namespace k= strata::kernels; 30 31namespace { 32 33int g_fail = 0 ; 34 35void ck (cudaError_t e ,const char * w ) { 36if (e != cudaSuccess ) { 37 std::fprintf (stderr ,"CUDA error in %s: %s\n" ,w ,cudaGetErrorString (e )); 38 std::exit (2 ); 39 } 40} 41 42template < typename T > 43T * dalloc (size_t n ) { 44T * p = nullptr ; 45ck (cudaMalloc (& p ,n * sizeof (T )+ 256 ),"malloc" ); 46ck (cudaMemset (p ,0 ,n * sizeof (T )+ 256 ),"memset" ); 47return p ; 48} 49 50const char * name_of (int t ) { 51return t == 144 ?"TQ2_T" :t == 145 ?"TQK6" :t == 146 ?"TQK7" :t == 147 ?"TQP5" :t == 148 ?"TQP6" :t == 149 ?"TQP7" 52 :t == 42 ?"Q2_0" :"?" ; 53} 54 55uint64_t fnv (const void * p ,size_t n ,uint64_t h = 1469598103934665603ull ) { 56const uint8_t * b = (const uint8_t * )p ; 57for (size_t i = 0 ;i < n ;++ i )h = (h ^b [i ])* 1099511628211ull ; 58return h ; 59} 60 61std::vector < uint8_t > random_rows (int t ,int64_t rows ,int64_t n , std::mt19937 & rng ) { 62const int bv = t == 42 ?64 :128 ; 63const size_t rb = k::iq_row_bytes (t ,n ),bs = k::iq_row_bytes (t ,bv ); 64 std::vector < uint8_t > w ((size_t )rows * rb ); 65 std::uniform_int_distribution < int > byte (0 ,255 ),ex (2 ,8 ),man (0 ,1023 ),sgn (0 ,3 ); 66for (auto & b :w )b = (uint8_t )byte (rng ); 67for (size_t o = 0 ;o < w .size ();o += bs ) { 68const uint16_t h = (uint16_t ) ((sgn (rng )== 0 ?0x8000 :0 ) | (ex (rng ) <<10 ) |man (rng ));// 2^-13 .. 2^-6 69 std::memcpy (& w [o ],& h ,2 ); 70 } 71return w ; 72} 73 74std::vector < float > random_x (size_t n , std::mt19937 & rng ) { 75 std::normal_distribution < float > nd (0.f ,1.f ); 76 std::vector < float > x (n ); 77for (auto & v :x )v = nd (rng ); 78return x ; 79} 80 81void check_mmvq (int t ,int n_in ,int n_out ,cudaStream_t s , std::mt19937 & rng ) { 82const auto w = random_rows (t ,n_out ,n_in ,rng ); 83uint8_t * dw = dalloc < uint8_t > (w .size ()); 84ck (cudaMemcpy (dw ,w .data (),w .size (),cudaMemcpyHostToDevice ),"w" ); 85const int max_cols = 8 ; 86const auto x = random_x ((size_t )max_cols * n_in ,rng ); 87float * dx = dalloc < float > (x .size ()); 88ck (cudaMemcpy (dx ,x .data (),x .size ()* 4 ,cudaMemcpyHostToDevice ),"x" ); 89uint8_t * dxq = dalloc < uint8_t > ((size_t )max_cols * (n_in /32 )* 36 ); 90 k::quantize_q8_1_rows (dx ,max_cols ,n_in ,dxq ,s ); 91float * dy_old = dalloc < float > ((size_t )max_cols * n_out ); 92float * dy_new = dalloc < float > ((size_t )max_cols * n_out ); 93 std::vector < float > y_old ((size_t )max_cols * n_out ),y_new (y_old .size ()); 94uint64_t sum = 1469598103934665603ull ; 95int bad = 0 ; 96for (int nc = 1 ;nc <=max_cols ;++ nc ) { 97ck (cudaMemset (dy_old ,0xFF ,y_old .size ()* 4 ),"memset" ); 98ck (cudaMemset (dy_new ,0xFF ,y_new .size ()* 4 ),"memset" ); 99 k::iq_set_old_kernels (true); 100 k::iq_mmvq (t ,dw ,dxq ,dy_old ,n_in ,n_out ,nc ,s ); 101 k::iq_set_old_kernels (false); 102 k::iq_mmvq (t ,dw ,dxq ,dy_new ,n_in ,n_out ,nc ,s ); 103ck (cudaStreamSynchronize (s ),"sync" ); 104ck (cudaMemcpy (y_old .data (),dy_old ,y_old .size ()* 4 ,cudaMemcpyDeviceToHost ),"y_old" ); 105ck (cudaMemcpy (y_new .data (),dy_new ,y_new .size ()* 4 ,cudaMemcpyDeviceToHost ),"y_new" ); 106size_t diff = 0 ; 107bool finite = true; 108for (size_t i = 0 ;i < (size_t )nc * n_out ;++ i ) { 109diff += std::memcmp (& y_old [i ],& y_new [i ],4 )!= 0 ; 110finite = finite && std::isfinite (y_new [i ]); 111 } 112if (diff || !finite ) { std::printf (" %s mmvq nc %d: %zu differ%s FAIL\n" ,name_of (t ),nc ,diff ,finite ?"" :", non-finite" );++ bad ; } 113sum = fnv (y_new .data (), (size_t )nc * n_out * 4 ,sum ); 114 } 115 std::printf ("SUM mmvq %-6s %5d x %5d %016llx %s\n" ,name_of (t ),n_out ,n_in , (unsigned long long )sum , 116bad ?"FAIL" :"decode-once == per-column" ); 117g_fail += bad ; 118cudaFree (dw );cudaFree (dx );cudaFree (dxq );cudaFree (dy_old );cudaFree (dy_new ); 119} 120 121struct Grouped { 122 k::NativeExpertLayout L ; 123int n_groups = 0 ,n_ent = 0 ,cap_groups = 0 ,cap_ent = 0 ,n_tok = 0 ; 124 std::vector < uint8_t *> blobs ; 125unsigned long long * dptr = nullptr ; 126int32_t * dstart = nullptr ,* dn = nullptr ,* ddst = nullptr ,* dtok = nullptr ; 127float * dx = nullptr ; 128uint8_t * dxq = nullptr ,* dscr = nullptr ; 129float * dout = nullptr ; 130size_t out_floats = 0 ; 131 132Grouped (int gu ,int dt ,int64_t H ,int64_t FF ,const std::vector < int >& counts ,int tokens , std::mt19937 & rng ) { 133L = k::native_expert_layout (gu ,dt ,H ,FF ); 134n_groups = (int )counts .size (); 135cap_groups = n_groups + 2 ; 136n_tok = tokens ; 137 std::vector < int32_t > start (1 ,0 ),tok ,dst ; 138for (int c :counts )start .push_back (start .back ()+ c ); 139n_ent = start .back (); 140cap_ent = n_ent + 3 ; 141 std::uniform_int_distribution < int > tk (0 ,tokens - 1 ); 142for (int e = 0 ;e < n_ent ;++ e )tok .push_back (tk (rng )); 143dst .resize (n_ent ); 144 std::iota (dst .begin (),dst .end (),0 ); 145 std::shuffle (dst .begin (),dst .end (),rng ); 146 std::vector < unsigned long long > ptr ; 147for (int g = 0 ;g < n_groups ;++ g ) { 148auto b = random_rows (gu ,2 * FF ,H ,rng ); 149const auto d = random_rows (dt ,H ,FF ,rng ); 150b .insert (b .end (),d .begin (),d .end ()); 151if (b .size ()!= L .bytes ) { std::fprintf (stderr ,"blob size %zu != %zu\n" ,b .size (),L .bytes ); std::exit (2 ); } 152uint8_t * db = dalloc < uint8_t > (b .size ()); 153ck (cudaMemcpy (db ,b .data (),b .size (),cudaMemcpyHostToDevice ),"blob" ); 154blobs .push_back (db ); 155ptr .push_back ((unsigned long long )db ); 156 } 157dptr = dalloc < unsigned long long > (cap_groups ); 158dstart = dalloc < int32_t > (cap_groups + 1 ); 159dn = dalloc < int32_t > (1 ); 160ddst = dalloc < int32_t > (cap_ent ); 161dtok = dalloc < int32_t > (cap_ent ); 162ck (cudaMemcpy (dptr ,ptr .data (),ptr .size ()* 8 ,cudaMemcpyHostToDevice ),"ptr" ); 163ck (cudaMemcpy (dstart ,start .data (),start .size ()* 4 ,cudaMemcpyHostToDevice ),"start" ); 164ck (cudaMemcpy (dn ,& n_groups ,4 ,cudaMemcpyHostToDevice ),"n" ); 165if (n_ent ) { 166ck (cudaMemcpy (ddst ,dst .data (),dst .size ()* 4 ,cudaMemcpyHostToDevice ),"dst" ); 167ck (cudaMemcpy (dtok ,tok .data (),tok .size ()* 4 ,cudaMemcpyHostToDevice ),"tok" ); 168 } 169const auto x = random_x ((size_t )tokens * H ,rng ); 170dx = dalloc < float > (x .size ()); 171ck (cudaMemcpy (dx ,x .data (),x .size ()* 4 ,cudaMemcpyHostToDevice ),"x" ); 172dxq = dalloc < uint8_t > ((size_t )tokens * (H /32 )* 36 ); 173dscr = dalloc < uint8_t > (k::native_expert_scratch_bytes (cap_ent ,FF )); 174out_floats = (size_t )cap_ent * H ; 175dout = dalloc < float > (out_floats ); 176 } 177 ~Grouped () { 178for (auto * b :blobs )cudaFree (b ); 179cudaFree (dptr );cudaFree (dstart );cudaFree (dn );cudaFree (ddst );cudaFree (dtok ); 180cudaFree (dx );cudaFree (dxq );cudaFree (dscr );cudaFree (dout ); 181 } 182void run (bool old ,cudaStream_t s ) { 183 k::iq_set_old_kernels (old ); 184 k::quantize_q8_1_rows (dx ,n_tok ,L .n_embd ,dxq ,s ); 185 k::native_expert_grouped (L ,dptr ,dstart ,dn ,ddst ,dtok ,cap_groups ,cap_ent ,dxq ,dscr ,dout ,s ); 186 k::iq_set_old_kernels (false); 187 } 188 std::vector < float > result (bool old ,cudaStream_t s ) { 189ck (cudaMemset (dout ,0xFF ,out_floats * 4 ),"memset" ); 190ck (cudaMemset (dscr ,0 , k::native_expert_scratch_bytes (cap_ent ,L .n_ff )),"memset" ); 191run (old ,s ); 192ck (cudaStreamSynchronize (s ),"sync" ); 193 std::vector < float > o (out_floats ); 194ck (cudaMemcpy (o .data (),dout ,o .size ()* 4 ,cudaMemcpyDeviceToHost ),"out" ); 195return o ; 196 } 197}; 198 199void check_grouped (int gu ,int dt ,int64_t H ,int64_t FF ,cudaStream_t s , std::mt19937 & rng ) { 200Grouped G (gu ,dt ,H ,FF , {1 ,3 ,4 ,5 ,0 ,8 ,2 ,11 ,1 },8 ,rng ); 201const auto a = G .result (true,s ),b = G .result (false,s ); 202size_t diff = 0 ; 203bool finite = true; 204for (size_t i = 0 ;i < a .size ();++ i ) { 205diff += std::memcmp (& a [i ],& b [i ],4 )!= 0 ; 206if (i < (size_t )G .n_ent * H )finite = finite && std::isfinite (b [i ]); 207 } 208const bool ok = diff == 0 && finite ; 209 std::printf ("SUM grouped %-6s/%-6s %5lld x %4lld %016llx %s\n" ,name_of (gu ),name_of (dt ), (long long )H , (long long )FF , 210 (unsigned long long )fnv (b .data (),b .size ()* 4 ),ok ?"decode-once == per-column" :"FAIL" ); 211if (!ok ) { std::printf (" %zu of %zu floats differ%s\n" ,diff ,a .size (),finite ?"" :", non-finite outputs" );++ g_fail ; } 212} 213 214// ---- Gyro-L pair trellis (TQP5/6/7, ggml 147-149) against a reference written from the format definition (no ggml): 215// state t of a block = the 16 stream bits that start at bit (63 - t) * K, LSB first, circular over 64 * K bits (15 bits 216// are used); the step's two weights are the first pair of the state's codebook entry, times the block's fp16 scale. 217uint32_t tqp_ref_state (const uint8_t * qs ,int K ,int t ) { 218const int nbits = 64 * K ; 219uint32_t s = 0 ; 220for (int b = 0 ;b < 16 ;++ b ) { 221const int pos = ((63 - t )* K + b ) %nbits ; 222s |= (uint32_t ) ((qs [pos >>3 ] >> (pos & 7 ))& 1 ) <<b ; 223 } 224return s & 0x7FFFu ; 225} 226 227float half_to_f (uint16_t h ) {// normal and subnormal fp16 (no inf / nan in these tests) 228const int e = (h >>10 )& 31 ,m = h & 1023 ; 229const float v = e == 0 ? std::ldexp ((float )m ,-24 ) : std::ldexp ((float ) (1024 + m ),e - 25 ); 230return (h & 0x8000 ) ?- v :v ; 231} 232 233uint16_t f_to_half_rn (float f ) {// round to nearest even, the GPU's __float2half_rn 234uint32_t x ; 235 std::memcpy (& x ,& f ,4 ); 236const uint32_t sign = (x >>16 )& 0x8000u ; 237const int e = (int ) ((x >>23 )& 0xFF )- 127 + 15 ; 238uint32_t m = x & 0x7FFFFFu ; 239if (e >=31 )return (uint16_t ) (sign |0x7C00u ); 240if (e <=0 ) { 241if (e < -10 )return (uint16_t )sign ; 242m |=0x800000u ; 243const int shift = 14 - e ; 244uint32_t half = m >>shift ; 245const uint32_t rem = m & ((1u <<shift )- 1 ),mid = 1u << (shift - 1 ); 246if (rem > mid || (rem == mid && (half & 1u )))++ half ; 247return (uint16_t ) (sign |half ); 248 } 249uint32_t half = ((uint32_t )e <<10 ) | (m >>13 ); 250const uint32_t rem = m & 0x1FFFu ; 251if (rem > 0x1000u || (rem == 0x1000u && (half & 1u )))++ half ; 252return (uint16_t ) (sign |half ); 253} 254 255// the weights of `rows` rows of `n` values, as floats (d * codebook, unrounded) 256std::vector < float > tqp_ref_weights (int t ,const std::vector < uint8_t >& w ,int64_t rows ,int64_t n ) { 257const int K = t - 142 ;// 147 -> 5, 148 -> 6, 149 -> 7 258const size_t bs = k::iq_row_bytes (t ,128 ); 259 std::vector < float > out ((size_t ) (rows * n )); 260for (size_t b = 0 ;b < (size_t ) (rows * n /128 );++ b ) { 261const uint8_t * blk = & w [b * bs ]; 262uint16_t hd ; 263 std::memcpy (& hd ,blk ,2 ); 264const float d = half_to_f (hd ); 265for (int step = 0 ;step < 64 ;++ step ) { 266const uint32_t s = tqp_ref_state (blk + 2 ,K ,step ); 267const uint32_t x = (s & 0x7FFFu )* 0x9e3779b1u ; 268const uint32_t pt = x >>21 ; 269out [b * 128 + 2 * step + 0 ]= d * half_to_f (tq_lut_f16 [2 * pt + 0 ]); 270out [b * 128 + 2 * step + 1 ]= d * half_to_f (tq_lut_f16 [2 * pt + 1 ]); 271 } 272 } 273return out ; 274} 275 276void check_tqp_reference (int t ,int n_in ,int n_out ,cudaStream_t s , std::mt19937 & rng ) { 277const auto w = random_rows (t ,n_out ,n_in ,rng ); 278const auto ref = tqp_ref_weights (t ,w ,n_out ,n_in ); 279uint8_t * dw = dalloc < uint8_t > (w .size ()); 280ck (cudaMemcpy (dw ,w .data (),w .size (),cudaMemcpyHostToDevice ),"w" ); 281// (1) the prompt path's fp16 dequantizer: bit for bit the fp16 of the reference weights 282const int64_t nv = (int64_t )n_out * n_in ; 283uint16_t * dh = dalloc < uint16_t > ((size_t )nv ); 284 k::iq_dequant_f16 (t ,dw ,nv ,dh ,s ); 285ck (cudaStreamSynchronize (s ),"sync" ); 286 std::vector < uint16_t > h ((size_t )nv ); 287ck (cudaMemcpy (h .data (),dh ,h .size ()* 2 ,cudaMemcpyDeviceToHost ),"h" ); 288size_t bad = 0 ; 289for (size_t i = 0 ;i < h .size ();++ i )bad += h [i ]!= f_to_half_rn (ref [i ]); 290 std::printf ("tqp ref %-6s dequant_f16 %5d x %5d: %zu of %zu fp16 values differ from the definition %s\n" ,name_of (t ),n_out ,n_in , 291bad ,h .size (),bad ?"FAIL" :"bitwise equal" ); 292g_fail += bad != 0 ; 293// (2) the decode kernels (int8 codebook, q8_1 activations): relative L1 error against the float weights . the 294// dequantized q8_1 activations, in double, below the 3e-2 every expert type gets in native_expert_parity 295const int nc = 3 ; 296const auto x = random_x ((size_t )nc * n_in ,rng ); 297float * dx = dalloc < float > (x .size ()); 298ck (cudaMemcpy (dx ,x .data (),x .size ()* 4 ,cudaMemcpyHostToDevice ),"x" ); 299uint8_t * dxq = dalloc < uint8_t > ((size_t )nc * (n_in /32 )* 36 ); 300 k::quantize_q8_1_rows (dx ,nc ,n_in ,dxq ,s ); 301float * dy = dalloc < float > ((size_t )nc * n_out ); 302 k::iq_mmvq (t ,dw ,dxq ,dy ,n_in ,n_out ,nc ,s ); 303ck (cudaStreamSynchronize (s ),"sync" ); 304 std::vector < float > y ((size_t )nc * n_out ); 305ck (cudaMemcpy (y .data (),dy ,y .size ()* 4 ,cudaMemcpyDeviceToHost ),"y" ); 306 std::vector < uint8_t > xq ((size_t )nc * (n_in /32 )* 36 ); 307ck (cudaMemcpy (xq .data (),dxq ,xq .size (),cudaMemcpyDeviceToHost ),"xq" ); 308double l1 = 0 ,l1ref = 0 ; 309for (int c = 0 ;c < nc ;++ c ) { 310 std::vector < double > xd ((size_t )n_in ); 311for (int b = 0 ;b < n_in /32 ;++ b ) { 312const uint8_t * blk = & xq [((size_t )c * (n_in /32 )+ b )* 36 ]; 313uint16_t hd ; 314 std::memcpy (& hd ,blk ,2 ); 315const double d = half_to_f (hd ); 316for (int j = 0 ;j < 32 ;++ j )xd [(size_t )b * 32 + j ]= d * (double ) (int8_t )blk [4 + j ]; 317 } 318for (int r = 0 ;r < n_out ;++ r ) { 319double acc = 0 ; 320for (int i = 0 ;i < n_in ;++ i )acc += (double )ref [(size_t )r * n_in + i ]* xd [(size_t )i ]; 321l1 += std::fabs ((double )y [(size_t )c * n_out + r ]- acc ); 322l1ref += std::fabs (acc ); 323 } 324 } 325const double rel = l1 /l1ref ; 326 std::printf ("tqp ref %-6s mmvq %5d x %5d x %d cols: relative L1 error %.2e (bound 3e-2) %s\n" ,name_of (t ),n_out ,n_in ,nc ,rel , 327rel < 3e-2 ?"ok" :"FAIL" ); 328g_fail += !(rel < 3e-2 ); 329cudaFree (dw );cudaFree (dh );cudaFree (dx );cudaFree (dxq );cudaFree (dy ); 330} 331 332float time_ms (cudaStream_t s ,int it ,const auto & fn ) { 333cudaEvent_t e0 ,e1 ; 334cudaEventCreate (& e0 ); 335cudaEventCreate (& e1 ); 336fn (); 337ck (cudaStreamSynchronize (s ),"warm" ); 338cudaEventRecord (e0 ,s ); 339for (int i = 0 ;i < it ;++ i )fn (); 340cudaEventRecord (e1 ,s ); 341ck (cudaEventSynchronize (e1 ),"bench" ); 342float ms = 0 ; 343cudaEventElapsedTime (& ms ,e0 ,e1 ); 344cudaEventDestroy (e0 ); 345cudaEventDestroy (e1 ); 346return ms /it ; 347} 348 349void bench (cudaStream_t s , std::mt19937 & rng ) { 350 std::printf ("\n--bench: microseconds per call (idle GPU assumed)\n" ); 351const int n_in = 2560 ,n_out = 8192 ,it = 100 ; 352for (int t : {144 ,145 ,146 ,42 }) { 353const auto w = random_rows (t ,n_out ,n_in ,rng ); 354uint8_t * dw = dalloc < uint8_t > (w .size ()); 355ck (cudaMemcpy (dw ,w .data (),w .size (),cudaMemcpyHostToDevice ),"w" ); 356const auto x = random_x ((size_t )8 * n_in ,rng ); 357float * dx = dalloc < float > (x .size ()); 358ck (cudaMemcpy (dx ,x .data (),x .size ()* 4 ,cudaMemcpyHostToDevice ),"x" ); 359uint8_t * dxq = dalloc < uint8_t > ((size_t )8 * (n_in /32 )* 36 ); 360 k::quantize_q8_1_rows (dx ,8 ,n_in ,dxq ,s ); 361float * dy = dalloc < float > ((size_t )8 * n_out ); 362const double gb = (double )w .size () /1e9 ; 363 std::printf ("BENCH mmvq %-6s %d x %d (%.3f GB):" ,name_of (t ),n_out ,n_in ,gb ); 364for (int nc : {1 ,2 ,3 ,4 ,8 }) { 365const float us = 1e3f * time_ms (s ,it , [& ] { k::iq_mmvq (t ,dw ,dxq ,dy ,n_in ,n_out ,nc ,s ); }); 366 std::printf (" nc%d %.1f us (%.0f GB/s)" ,nc ,us ,gb / (us * 1e-6 )); 367 } 368 std::printf ("\n" ); 369cudaFree (dw );cudaFree (dx );cudaFree (dxq );cudaFree (dy ); 370 } 371// the same 21M weights as rows of different lengths: a per-row cost shows as a falling time with longer rows 372for (int t : {145 }) { 373for (auto rc : {std::pair < int ,int > {8192 ,2560 }, {4096 ,5120 }, {2048 ,10240 }, {1024 ,20480 }}) { 374const int ni = rc .second ,no = rc .first ; 375const auto w = random_rows (t ,no ,ni ,rng ); 376uint8_t * dw = dalloc < uint8_t > (w .size ()); 377ck (cudaMemcpy (dw ,w .data (),w .size (),cudaMemcpyHostToDevice ),"w" ); 378const auto x = random_x ((size_t )8 * ni ,rng ); 379float * dx = dalloc < float > (x .size ()); 380ck (cudaMemcpy (dx ,x .data (),x .size ()* 4 ,cudaMemcpyHostToDevice ),"x" ); 381uint8_t * dxq = dalloc < uint8_t > ((size_t )8 * (ni /32 )* 36 ); 382 k::quantize_q8_1_rows (dx ,8 ,ni ,dxq ,s ); 383float * dy = dalloc < float > ((size_t )8 * no ); 384const float us = 1e3f * time_ms (s ,it , [& ] { k::iq_mmvq (t ,dw ,dxq ,dy ,ni ,no ,1 ,s ); }); 385 std::printf ("BENCH rows %-6s %5d x %5d nc1 %.1f us\n" ,name_of (t ),no ,ni ,us ); 386cudaFree (dw );cudaFree (dx );cudaFree (dxq );cudaFree (dy ); 387 } 388 } 389// the verify window's shape: 2560 x 640 experts, 16 groups of m entries each 390for (auto gd : {std::pair < int ,int > {145 ,146 }, {145 ,144 }, {144 ,144 }, {144 ,146 }}) { 391 std::printf ("BENCH grouped %-6s/%-6s 16 groups of m entries:" ,name_of (gd .first ),name_of (gd .second )); 392for (int m : {1 ,2 ,3 ,4 ,8 }) { 393Grouped G (gd .first ,gd .second ,2560 ,640 , std::vector < int > (16 ,m ),8 ,rng ); 394const float us = 1e3f * time_ms (s ,30 , [& ] {G .run (false,s ); }); 395const double gb = 16.0 * (double )G .L .bytes /1e9 ; 396 std::printf (" m=%d %.1f us (%.0f GB/s)" ,m ,us ,gb / (us * 1e-6 )); 397 } 398 std::printf ("\n" ); 399 } 400} 401 402}// namespace 403 404int main (int argc ,char ** argv ) { 405const bool do_bench = argc > 1 && std::string (argv [1 ])== "--bench" ; 406cudaStream_t s ; 407ck (cudaStreamCreate (& s ),"stream" ); 408cudaDeviceProp prop ; 409cudaGetDeviceProperties (& prop ,0 ); 410 std::printf ("device %s cc %d.%d\n" ,prop .name ,prop .major ,prop .minor ); 411 std::mt19937 rng (18 ); 412// Gyro-S / Gyro-M types first, on the generator they always had: their "SUM" lines are the same bits as before the pair types 413for (int t : {144 ,145 ,146 }) { 414check_mmvq (t ,2560 ,67 ,s ,rng ); 415check_mmvq (t ,1024 ,5 ,s ,rng ); 416 } 417for (int gu : {144 ,145 }) 418for (int dt : {144 ,146 })check_grouped (gu ,dt ,2560 ,640 ,s ,rng ); 419// other geometries: a longer hidden row (the staged gate/up falls back from its 80-block activation buffer), a shorter 420// expert row (the 8-lane down rows need n_ff 640) 421for (int gu : {144 ,145 }) { 422check_grouped (gu ,146 ,5120 ,640 ,s ,rng ); 423check_grouped (gu ,144 ,2560 ,512 ,s ,rng ); 424check_grouped (gu ,146 ,2048 ,256 ,s ,rng ); 425 } 426// Gyro-L's pair trellis (own generator): decode-once == per-column, then a reference from the format definition 427// (fp16 dequantizer bit for bit, decode kernels to 3e-2) 428 std::mt19937 rng_p (19 ); 429for (int t : {147 ,148 ,149 }) { 430check_mmvq (t ,2560 ,67 ,s ,rng_p ); 431check_mmvq (t ,1024 ,5 ,s ,rng_p ); 432check_tqp_reference (t ,2560 ,67 ,s ,rng_p ); 433check_tqp_reference (t ,1024 ,5 ,s ,rng_p ); 434check_grouped (t ,t ,2560 ,640 ,s ,rng_p );// the pair trellis in both roles, as its file has it (tqp6 / tqp6) 435check_grouped (t ,t ,5120 ,640 ,s ,rng_p ); 436 } 437check_grouped (148 ,146 ,2560 ,640 ,s ,rng_p ); 438check_grouped (145 ,148 ,2560 ,640 ,s ,rng_p ); 439if (do_bench )bench (s ,rng ); 440 std::printf ("tq_pascal_bench: %d failures\n" ,g_fail ); 441cudaStreamDestroy (s ); 442return g_fail ?1 :0 ; 443}