okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd19246dc
main
1// src/prefill/lat_q3k_engine_shape_test.cpp - "our output differs by how much", per shape. 2// ONE input, two paths, byte for byte the same arguments: the frozen serial chain 3// (lat_q3k_gu -> lat_q3k_swiglu_q8 -> lat_q3k_down) and lat_q3k_fused. hbuf is compared on its own, so a difference 4// points at the fused epilogue (hbuf) or at the down (out). 5// Shapes: the engine's own 512-group table from the dump (dump_ref/g-off.bin, g-cnt.bin, g-tok.bin), the single groups 6// the engine failed on, nb=2, the tile boundaries, and the 128 x 4 regression. 7#include "strata/prefill/lat_q3k_fused.hpp" 8#include "strata/prefill/lat_q3k_gu.hpp" 9#include "strata/prefill/lat_q3k_swiglu.hpp" 10#include "strata/prefill/lat_q3k_down.hpp" 11#include "strata/kernels/iq_kernels.hpp" 12 13#include <cuda_runtime.h> 14#include <ggml.h> 15 16#include <algorithm> 17#include <cmath> 18#include <cstdio> 19#include <cstdlib> 20#include <cstring> 21#include <random> 22#include <string> 23#include <vector> 24 25#define CK (e ) do { const cudaError_t r_ = (e); if (r_ != cudaSuccess) { \ 26 std::printf("FAIL %s: %s\n", #e, cudaGetErrorString(r_)); std::exit(1); } } while (0) 27 28namespace { 29constexpr int64_t ELL = 1280 ,FF = 640 ; 30constexpr int64_t GUB = 1280 * 550 ,DNB = ELL * FF /32 * 18 ; 31constexpr int kXq = (int ) (ELL /32 )* 36 ; 32constexpr int kHq = (int ) strata::prefill::lat_q3k_fused_hbuf_row ; 33constexpr int NBLOB = 4 ; 34constexpr int E = 42450 ;// the engine's entry space (the dumps' off[NG]) 35constexpr int NG = 512 ; 36const std::string DUMP = "/home/user/lat-s26/q3k/dump_ref/" ; 37 38template < class T > 39std::vector < T > rd (const char * name ,size_t expect ) { 40 std::vector < T > v (expect ); 41 std::FILE * f = std::fopen ((DUMP + name ).c_str (),"rb" ); 42if (f == nullptr ) { std::printf ("FAIL cannot open %s\n" ,name ); std::exit (1 ); } 43const size_t n = std::fread (v .data (),sizeof (T ),expect ,f ); 44 std::fclose (f ); 45if (n != expect ) { std::printf ("FAIL %s: %zu of %zu\n" ,name ,n ,expect ); std::exit (1 ); } 46return v ; 47} 48}// namespace 49 50struct Shape { 51 std::string tag ; 52 std::vector < int32_t > starts ;// ng + 1 53 std::vector < int32_t > counts ;// ng 54int ng ()const {return (int )counts .size (); } 55}; 56 57static bool hbuf_at_ok (const std::vector < uint8_t >& a ,const std::vector < uint8_t >& b ,size_t at ) { 58return a [at ]== b [at ]; 59} 60 61int main () { 62 std::vector < int32_t > off = rd < int32_t > ("g-off.bin" ,NG + 1 ); 63 std::vector < int32_t > cnt = rd < int32_t > ("g-cnt.bin" ,NG ); 64 std::vector < int32_t > tok = rd < int32_t > ("g-tok.bin" , (size_t )E ); 65if (off [NG ]!= E ) { std::printf ("FAIL off[NG] %d != %d\n" ,off [NG ],E );return 1 ; } 66 67// 4 different experts (never one blob replicated: a wrong row range would still write the right values) 68 std::mt19937 rng (91 ); 69 std::normal_distribution < float > nw (0.f ,0.05f ); 70 std::vector < uint8_t > blob ((size_t )NBLOB * (GUB + DNB )); 71for (int i = 0 ;i < NBLOB ;++ i ) { 72uint8_t * b = blob .data ()+ (size_t )i * (GUB + DNB ); 73 std::vector < float > w ((size_t )1280 * ELL ); 74for (auto & v :w )v = nw (rng ); 75if ((size_t )ggml_quantize_chunk (GGML_TYPE_Q3_K ,w .data (),b ,0 ,1280 ,ELL ,nullptr )!= (size_t )GUB ) { 76 std::printf ("FAIL gu quantize\n" );return 1 ; 77 } 78 std::vector < float > wd ((size_t )ELL * FF ); 79for (auto & v :wd )v = nw (rng ); 80if ((size_t )ggml_quantize_chunk (GGML_TYPE_IQ4_NL ,wd .data (),b + GUB ,0 ,ELL ,FF ,nullptr )!= (size_t )DNB ) { 81 std::printf ("FAIL dn quantize\n" );return 1 ; 82 } 83 } 84const size_t EB = (size_t ) (GUB + DNB ); 85uint8_t * dall = nullptr ; 86CK (cudaMalloc (& dall ,blob .size ())); 87CK (cudaMemcpy (dall ,blob .data (),blob .size (),cudaMemcpyHostToDevice )); 88 std::vector < const uint8_t *> bp ((size_t )NG ); 89for (int g = 0 ;g < NG ;++ g )bp [(size_t )g ]= dall + (size_t ) (g %NBLOB )* EB ; 90const uint8_t ** dblob = nullptr ; 91CK (cudaMalloc (& dblob , (size_t )NG * sizeof (uint8_t * ))); 92CK (cudaMemcpy (dblob ,bp .data (), (size_t )NG * sizeof (uint8_t * ),cudaMemcpyHostToDevice )); 93 94int ntok = 0 ; 95for (int32_t t :tok )ntok = std::max (ntok , (int )t ); 96++ ntok ; 97 std::vector < uint8_t > xq ((size_t )ntok * kXq ); 98 { 99 std::uniform_int_distribution < int > u8 (-127 ,127 ); 100for (size_t i = 0 ;i < xq .size ();++ i )xq [i ]= (uint8_t )u8 (rng ); 101for (int t = 0 ;t < ntok ;++ t ) 102for (int k = 0 ;k < (int ) (ELL /32 );++ k ) { 103uint8_t * b = xq .data ()+ (size_t )t * kXq + 36 * k ; 104const uint16_t d16 = ggml_fp32_to_fp16 (0.01f + 1e-4f * ((t + k ) %97 )),s16 = 0 ; 105 std::memcpy (b ,& d16 ,2 ); std::memcpy (b + 2 ,& s16 ,2 ); 106 } 107 } 108uint8_t * dxq = nullptr ; 109CK (cudaMalloc (& dxq ,xq .size ())); 110CK (cudaMemcpy (dxq ,xq .data (),xq .size (),cudaMemcpyHostToDevice )); 111int32_t * dtok = nullptr ; 112CK (cudaMalloc (& dtok , (size_t )E * 4 )); 113CK (cudaMemcpy (dtok ,tok .data (),tok .size ()* 4 ,cudaMemcpyHostToDevice )); 114 std::vector < int32_t > ident ((size_t )E ); 115for (int e = 0 ;e < E ;++ e )ident [(size_t )e ]= e ; 116int32_t * dident = nullptr ; 117CK (cudaMalloc (& dident , (size_t )E * 4 )); 118CK (cudaMemcpy (dident ,ident .data (),ident .size ()* 4 ,cudaMemcpyHostToDevice )); 119 120int32_t * dgs = nullptr ,* dcnt = nullptr ,* dng = nullptr ; 121CK (cudaMalloc (& dgs , (size_t ) (NG + 1 )* 4 )); 122CK (cudaMalloc (& dcnt , (size_t )NG * 4 )); 123CK (cudaMalloc (& dng ,4 )); 124float * dgate = nullptr ;float * dup = nullptr ; 125CK (cudaMalloc (& dgate , (size_t )E * FF * 4 ));CK (cudaMalloc (& dup , (size_t )E * FF * 4 )); 126uint8_t * dhq = nullptr ;uint8_t * dhbuf = nullptr ; 127CK (cudaMalloc (& dhq , (size_t )E * kHq ));CK (cudaMalloc (& dhbuf , (size_t )E * kHq )); 128float * dfu = nullptr ;float * dse = nullptr ; 129CK (cudaMalloc (& dfu , (size_t )E * ELL * 4 ));CK (cudaMalloc (& dse , (size_t )E * ELL * 4 )); 130 131int sms = 0 ; 132 {int dev = 0 ;cudaGetDevice (& dev );cudaDeviceGetAttribute (& sms ,cudaDevAttrMultiProcessorCount ,dev ); } 133const int kItems = (int ) (ELL /256 );// the launchers' fb count (n_embd / 256 for the down, 5 here) 134int gz_last = 1 ; 135 136 std::vector < float > yfu ((size_t )E * ELL ),yse ((size_t )E * ELL ); 137 std::vector < uint8_t > hfu ((size_t )E * kHq ),hse ((size_t )E * kHq ); 138 139// one shape: identical arguments to both paths, hbuf and out compared separately 140auto run = [& ](const Shape & sh ,bool print_only )-> int { 141const int ng = sh .ng (); 142CK (cudaMemcpy (dgs ,sh .starts .data (), (size_t ) (ng + 1 )* 4 ,cudaMemcpyHostToDevice )); 143CK (cudaMemcpy (dcnt ,sh .counts .data (), (size_t )ng * 4 ,cudaMemcpyHostToDevice )); 144CK (cudaMemcpy (dng ,& ng ,4 ,cudaMemcpyHostToDevice )); 145gz_last = (sms > 0 && ng * kItems < sms ) ?8 :1 ; 146CK (cudaMemset (dhbuf ,0x00 , (size_t )E * kHq )); 147CK (cudaMemset (dhq ,0x00 , (size_t )E * kHq )); 148CK (cudaMemset (dfu ,0 , (size_t )E * ELL * 4 )); 149CK (cudaMemset (dse ,0 , (size_t )E * ELL * 4 )); 150 strata::prefill::lat_q3k_fused (dblob ,dgs ,ng ,dxq ,dhbuf ,dfu ,ELL ,nullptr ,dcnt ,dtok ,dident ); 151 strata::prefill::lat_q3k_gu (dblob ,dgs ,ng ,dxq ,dgate ,dup ,FF ,nullptr ,dcnt ,dtok ); 152 strata::prefill::lat_q3k_swiglu_q8 (dgate ,dup ,E ,dhq ,nullptr ,nullptr ); 153 strata::prefill::lat_q3k_down (dblob ,dgs ,ng ,dhq ,dse ,ELL ,nullptr ,dcnt ,dident ); 154CK (cudaDeviceSynchronize ()); 155CK (cudaMemcpy (yfu .data (),dfu ,yfu .size ()* 4 ,cudaMemcpyDeviceToHost )); 156CK (cudaMemcpy (yse .data (),dse ,yse .size ()* 4 ,cudaMemcpyDeviceToHost )); 157CK (cudaMemcpy (hfu .data (),dhbuf ,hfu .size (),cudaMemcpyDeviceToHost )); 158CK (cudaMemcpy (hse .data (),dhq ,hse .size (),cudaMemcpyDeviceToHost )); 159 160// the rows this shape owns 161 std::vector < char > mine ((size_t )E ,0 ); 162for (int g = 0 ;g < ng ;++ g ) 163for (int e = sh .starts [(size_t )g ];e < sh .starts [(size_t )g ]+ sh .counts [(size_t )g ];++ e )mine [(size_t )e ]= 1 ; 164 165// hbuf: per entry, the 68 bytes the down reads (codes + the two d's) 166long long hb_diff = 0 ,hb_tot = 0 ,hb_pad = 0 ; 167int hb_first_e = -1 ,hb_first_c = -1 ,hb_first_b = -1 ,hb_zdiff = 0 ; 168for (int e = 0 ;e < E ;++ e ) { 169if (!mine [(size_t )e ])continue ; 170for (int c = 0 ;c < 10 ;++ c ) 171for (int b = 0 ;b < 80 ;++ b ) { 172const size_t at = (size_t )e * kHq + (size_t )c * 80 + (size_t )b ; 173++ hb_tot ; 174if (hbuf_at_ok (hfu ,hse ,at ))continue ; 175if (b >=68 ) {++ hb_pad ;continue ; } 176++ hb_diff ; 177if (hb_first_e < 0 ) {hb_first_e = e ;hb_first_c = c ;hb_first_b = b ; } 178 } 179 } 180// out: 1280 floats an entry 181long long o_diff = 0 ,o_tot = 0 ; 182double o_max = 0 ,o_abs = 0 ,o_ref = 0 ; 183int o_first_e = -1 ,o_first_r = -1 ; 184for (int e = 0 ;e < E ;++ e ) { 185if (!mine [(size_t )e ])continue ; 186for (int r = 0 ;r < ELL ;++ r ) { 187const size_t at = (size_t )e * ELL + r ; 188const double a = yfu [at ],b2 = yse [at ]; 189++ o_tot ; 190if (a == b2 )continue ; 191++ o_diff ; 192const double d = std::fabs (a - b2 ); 193o_max = std::max (o_max ,d );o_abs += d ;o_ref += std::fabs (b2 ); 194if (o_first_e < 0 ) {o_first_e = e ;o_first_r = r ; } 195 } 196 } 197// where the first hbuf difference sits in the tiles (which z slice would own that row) 198int rowin = -1 ,zs = -1 ,gid = -1 ; 199if (hb_first_e >=0 ) 200for (int g = 0 ;g < ng ;++ g ) 201if (hb_first_e >=sh .starts [(size_t )g ]&& hb_first_e < sh .starts [(size_t )g ]+ sh .counts [(size_t )g ]) { 202gid = g ;rowin = hb_first_e - sh .starts [(size_t )g ];zs = (rowin /64 ) %gz_last ;break ; 203 } 204 std::printf ("%-34s ng=%-4d rows=%-6zu gz=%d\n" ,sh .tag .c_str (),ng , (size_t ) std::count (mine .begin (),mine .end (),1 ), 205gz_last ); 206 std::printf (" hbuf: differing %lld / %lld bytes (pad %lld) first at entry %d chunk %d byte %d" 207" (group %d, row %d, z %d)\n" ,hb_diff ,hb_tot ,hb_pad ,hb_first_e ,hb_first_c ,hb_first_b ,gid , 208rowin ,zs ); 209 std::printf (" out : differing %lld / %lld values max|d| %.3e rel %.3e first at entry %d row %d\n" ,o_diff , 210o_tot ,o_max ,o_ref > 0 ?o_abs /o_ref :0.0 ,o_first_e ,o_first_r ); 211return (hb_diff == 0 && o_diff == 0 ) ?0 :1 ; 212 }; 213 214// ---- the shape table --------------------------------------------------------------------------------------- 215 std::vector < Shape > shapes ; 216 {// 1. the engine's own table 217Shape s ;s .tag = "engine table (512 groups, dump)" ; 218s .starts = off ;s .counts = cnt ; 219shapes .push_back (s ); 220 } 221 {// 2. the single groups the engine failed on 222const int32_t one [][2 ]= {{468 ,30567 }, {41 ,31035 }, {2749 ,32028 }, {400 ,34777 }, {2376 ,37469 }}; 223for (const auto & v :one ) { 224Shape s ; 225char t [64 ]; std::snprintf (t ,sizeof t ,"nb=1 cnt=%d start=%d" ,v [0 ],v [1 ]); 226s .tag = t ;s .starts = {v [1 ],v [1 ]+ v [0 ]};s .counts = {v [0 ]}; 227shapes .push_back (s ); 228 } 229 } 230 {// 3. nb=2 231Shape s ;s .tag = "nb=2 cnt={468,41} start={30567,31035}" ; 232s .starts = {30567 ,31035 ,31076 };s .counts = {468 ,41 }; 233shapes .push_back (s ); 234 } 235 {// 4. every count from 1 to 64, then the boundaries and the engine's big sizes 236 std::vector < int > sizes ; 237for (int c = 1 ;c <=64 ;++ c )sizes .push_back (c ); 238for (int c : {65 ,128 ,256 ,512 ,1024 ,1375 ,2062 ,2748 ,2749 ,3098 })sizes .push_back (c ); 239for (int c :sizes ) { 240Shape s ; 241char t [64 ]; std::snprintf (t ,sizeof t ,"nb=1 sweep cnt=%d" ,c ); 242s .tag = t ;s .starts = {0 ,c };s .counts = {c }; 243shapes .push_back (s ); 244 } 245 } 246 {// 5. the 128 x 4 regression 247Shape s ;s .tag = "regression 128 groups x 4 entries" ; 248for (int g = 0 ;g <=128 ;++ g )s .starts .push_back (g * 4 ); 249s .counts .assign (128 ,4 ); 250shapes .push_back (s ); 251 } 252 253int fails = 0 ; 254for (const Shape & sh :shapes )fails += run (sh , false); 255 std::printf ("%s: %zu shapes, %d with a difference\n" ,fails == 0 ?"PASS" :"FAIL" ,shapes .size (),fails ); 256return fails == 0 ?0 :1 ; 257}