okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd19246dc
main
1// src/prefill/lat_q3k_fusedv5_test.cpp - the equivalence gate of moe_fused_q3k.cu (v5's kernel with the q3k decode) 2// against the frozen chain lat_q3k_gu -> lat_q3k_swiglu_q8 -> lat_q3k_down, on the same input, byte for byte: 3// shape 1: 128 groups x 4 entries (the unit tests' shape) 4// shape 2: the engine's 512 groups with the dumped off[]/cnt[] (42450 entries, off[] not monotonic) 5#include "strata/prefill/moe_fused_q3k.hpp" 6#include "strata/prefill/lat_q3k_fused.hpp" 7#include "strata/prefill/lat_q3k_gu.hpp" 8#include "strata/prefill/lat_q3k_swiglu.hpp" 9#include "strata/prefill/lat_q3k_down.hpp" 10 11#include <cuda_runtime.h> 12#include <ggml.h> 13 14#include <algorithm> 15#include <cmath> 16#include <cstdio> 17#include <cstdlib> 18#include <cstring> 19#include <random> 20#include <string> 21#include <vector> 22 23#define CK (e ) do { const cudaError_t r_ = (e); if (r_ != cudaSuccess) { \ 24 std::printf("FAIL %s: %s\n", #e, cudaGetErrorString(r_)); std::exit(1); } } while (0) 25 26namespace { 27constexpr int64_t ELL = 1280 ,FF = 640 ; 28constexpr int64_t GUB = 1280 * 550 ,DNB = ELL * FF /32 * 18 ; 29constexpr int kXq = (int ) (ELL /32 )* 36 ; 30constexpr int kHq = (int ) strata::prefill::lat_q3k_fused_hbuf_row ; 31constexpr int NBLOB = 4 ; 32constexpr int NG = 512 ,E = 42450 ;// the engine's entry space (the dumps' off[NG]) 33const std::string DUMP = "/home/user/lat-s26/q3k/dump_ref/" ; 34 35template < class T > 36std::vector < T > rd (const char * name ,size_t expect ) { 37 std::vector < T > v (expect ); 38 std::FILE * f = std::fopen ((DUMP + name ).c_str (),"rb" ); 39if (f == nullptr ) { std::printf ("FAIL cannot open %s\n" ,name ); std::exit (1 ); } 40const size_t n = std::fread (v .data (),sizeof (T ),expect ,f ); 41 std::fclose (f ); 42if (n != expect ) { std::printf ("FAIL %s: %zu of %zu\n" ,name ,n ,expect ); std::exit (1 ); } 43return v ; 44} 45 46template < class T > 47void upl (T * dst ,const std::vector < T >& src ) { 48CK (cudaMemcpy (dst ,src .data (),src .size ()* sizeof (T ),cudaMemcpyHostToDevice )); 49} 50}// namespace 51 52static void step (const char * m ) { std::printf ("[step] %s\n" ,m ); std::fflush (stdout ); } 53 54int main () { 55 std::mt19937 rng (101 ); 56step ("start" ); 57 std::normal_distribution < float > nw (0.f ,0.05f ); 58 std::vector < uint8_t > blob ((size_t )NBLOB * (GUB + DNB )); 59for (int i = 0 ;i < NBLOB ;++ i ) { 60uint8_t * b = blob .data ()+ (size_t )i * (GUB + DNB ); 61 std::vector < float > w ((size_t )1280 * ELL ); 62for (auto & v :w )v = nw (rng ); 63if ((size_t )ggml_quantize_chunk (GGML_TYPE_Q3_K ,w .data (),b ,0 ,1280 ,ELL ,nullptr )!= (size_t )GUB ) { 64 std::printf ("FAIL gu quantize\n" );return 1 ; 65 } 66 std::vector < float > wd ((size_t )ELL * FF ); 67for (auto & v :wd )v = nw (rng ); 68if ((size_t )ggml_quantize_chunk (GGML_TYPE_IQ4_NL ,wd .data (),b + GUB ,0 ,ELL ,FF ,nullptr )!= (size_t )DNB ) { 69 std::printf ("FAIL dn quantize\n" );return 1 ; 70 } 71 } 72step ("blobs quantized" ); 73const size_t EB = (size_t ) (GUB + DNB ); 74uint8_t * dall = nullptr ; 75CK (cudaMalloc (& dall ,blob .size ())); 76CK (cudaMemcpy (dall ,blob .data (),blob .size (),cudaMemcpyHostToDevice )); 77 78// 512 experts, one blob each (the v5 batch holds the whole layer) 79 std::vector < const uint8_t *> bp ((size_t )NG ); 80for (int g = 0 ;g < NG ;++ g )bp [(size_t )g ]= dall + (size_t ) (g %NBLOB )* EB ; 81 strata::prefill::fused::WideBatch wb ; 82wb .e0 = 0 ;wb .e1 = NG ; 83for (int g = 0 ;g < NG ;++ g )wb .blob [g ]= bp [(size_t )g ]; 84const uint8_t ** dblob = nullptr ; 85CK (cudaMalloc (& dblob , (size_t )NG * sizeof (uint8_t * ))); 86CK (cudaMemcpy (dblob ,bp .data (), (size_t )NG * sizeof (uint8_t * ),cudaMemcpyHostToDevice )); 87 88step ("blobs on device" ); 89 std::vector < uint8_t > xq ((size_t )E * kXq ); 90 { 91 std::uniform_int_distribution < int > u8 (-127 ,127 ); 92for (size_t i = 0 ;i < xq .size ();++ i )xq [i ]= (uint8_t )u8 (rng ); 93for (int t = 0 ;t < E ;++ t ) 94for (int k = 0 ;k < (int ) (ELL /32 );++ k ) { 95uint8_t * b = xq .data ()+ (size_t )t * kXq + 36 * k ; 96const uint16_t d16 = ggml_fp32_to_fp16 (0.01f + 1e-4f * ((t + k ) %97 )),s16 = 0 ; 97 std::memcpy (b ,& d16 ,2 ); std::memcpy (b + 2 ,& s16 ,2 ); 98 } 99 } 100uint8_t * dxq = nullptr ; 101CK (cudaMalloc (& dxq ,xq .size ())); 102CK (cudaMemcpy (dxq ,xq .data (),xq .size (),cudaMemcpyHostToDevice )); 103 std::vector < int32_t > ident ((size_t )E ); 104for (int e = 0 ;e < E ;++ e )ident [(size_t )e ]= e ; 105int32_t * dident = nullptr ; 106CK (cudaMalloc (& dident , (size_t )E * 4 )); 107upl (dident ,ident ); 108 109step ("xq ready" ); 110int32_t * dgs = nullptr ,* dcnt = nullptr ,* dng = nullptr ; 111CK (cudaMalloc (& dgs , (size_t ) (NG + 1 )* 4 )); 112CK (cudaMalloc (& dcnt , (size_t )NG * 4 )); 113CK (cudaMalloc (& dng ,4 )); 114float * dgate = nullptr ;float * dup = nullptr ; 115CK (cudaMalloc (& dgate , (size_t )E * FF * 4 ));CK (cudaMalloc (& dup , (size_t )E * FF * 4 )); 116uint8_t * dhq = nullptr ;uint8_t * dhbuf = nullptr ; 117CK (cudaMalloc (& dhq , (size_t )E * kHq ));CK (cudaMalloc (& dhbuf , (size_t )E * kHq )); 118float * dref = nullptr ;float * dv5 = nullptr ; 119CK (cudaMalloc (& dref , (size_t )E * ELL * 4 ));CK (cudaMalloc (& dv5 , (size_t )E * ELL * 4 )); 120// the v5 path's grouping tables (group_latent: the same counts/offsets, the 64-row tile list per expert) 121void * htables = nullptr ; 122CK (cudaMalloc (& htables , strata::prefill::fused::latent_group_bytes (E ,NG ))); 123void * dtables = nullptr ; 124CK (cudaMalloc (& dtables , strata::prefill::fused::latent_group_bytes (E ,NG ))); 125const strata::prefill::fused::LatentGeom lg {1280 ,640 ,550 ,360 ,352000 ,704000 ,0 }; 126 127 std::vector < float > yref ((size_t )E * ELL ),yv5 ((size_t )E * ELL ); 128 std::vector < uint8_t > href ((size_t )E * kHq ),hv5 ((size_t )E * kHq ); 129 130step ("buffers ready" ); 131auto run_shape = [& ](const char * tag ,const std::vector < int32_t >& off ,const std::vector < int32_t >& cnt )-> int { 132step (tag ); 133const int ng = (int )cnt .size (); 134upl (dgs ,off );upl (dcnt ,cnt ); 135CK (cudaMemcpy (dng ,& ng ,4 ,cudaMemcpyHostToDevice )); 136int total = 0 ; 137for (int c :cnt )total += c ; 138step (" reference" ); 139// reference: the frozen chain 140CK (cudaMemset (dhq ,0 , (size_t )E * kHq )); 141CK (cudaMemset (dref ,0 , (size_t )E * ELL * 4 )); 142 strata::prefill::lat_q3k_gu (dblob ,dgs ,ng ,dxq ,dgate ,dup ,FF ,nullptr ,dcnt ,nullptr ); 143 strata::prefill::lat_q3k_swiglu_q8 (dgate ,dup ,E ,dhq ,nullptr ,nullptr ); 144 strata::prefill::lat_q3k_down (dblob ,dgs ,ng ,dhq ,dref ,ELL ,nullptr ,dcnt ,nullptr ); 145step (" tables" ); 146// under test: moe_fused_q3k (v5's kernel + the q3k decode) 147CK (cudaMemset (htables ,0 , strata::prefill::fused::latent_group_bytes (E ,NG ))); 148 { 149 std::vector < char > t (strata::prefill::fused::latent_group_bytes (E ,NG )); 150 strata::prefill::fused::group_latent (cnt .data (),off .data (),NG ,t .data (),t .size ()); 151CK (cudaMemcpy (dtables ,t .data (),t .size (),cudaMemcpyHostToDevice )); 152 } 153CK (cudaMemset (dhbuf ,0 , (size_t )E * kHq )); 154CK (cudaMemset (dv5 ,0 , (size_t )E * ELL * 4 )); 155step (" v5 launch" ); 156 strata::prefill::fused_q3k::experts_latent_q3k (wb ,lg ,NG ,E ,dtables ,dxq ,dident ,dhbuf ,dv5 ,nullptr ); 157CK (cudaDeviceSynchronize ()); 158step (" copies" ); 159CK (cudaMemcpy (yref .data (),dref ,yref .size ()* 4 ,cudaMemcpyDeviceToHost )); 160CK (cudaMemcpy (yv5 .data (),dv5 ,yv5 .size ()* 4 ,cudaMemcpyDeviceToHost )); 161CK (cudaMemcpy (href .data (),dhq ,href .size (),cudaMemcpyDeviceToHost )); 162CK (cudaMemcpy (hv5 .data (),dhbuf ,hv5 .size (),cudaMemcpyDeviceToHost )); 163 164long long hdiff = 0 ,odiff = 0 ,h_first = -1 ,o_first = -1 ; 165double omax = 0 ,oabs = 0 ,oref = 0 ; 166 std::vector < char > mine ((size_t )E ,0 ); 167for (int g = 0 ;g < ng ;++ g ) 168for (int e = off [(size_t )g ];e < off [(size_t )g ]+ cnt [(size_t )g ];++ e )mine [(size_t )e ]= 1 ; 169for (int e = 0 ;e < E ;++ e ) { 170if (!mine [(size_t )e ])continue ; 171for (size_t b = 0 ;b < (size_t )kHq ;++ b ) { 172const size_t at = (size_t )e * kHq + b ; 173if (href [at ]!= hv5 [at ]) {++ hdiff ;if (h_first < 0 )h_first = (long long )at ; } 174 } 175for (int r = 0 ;r < ELL ;++ r ) { 176const size_t at = (size_t )e * ELL + r ; 177const double a = yref [at ],b2 = yv5 [at ]; 178if (a != b2 ) { 179++ odiff ;if (o_first < 0 )o_first = (long long )at ; 180const double d = std::fabs (a - b2 ); 181omax = std::max (omax ,d );oabs += d ; 182 } 183oref += std::fabs (b2 ); 184 } 185 } 186 {// is either side non-finite, and what does the first difference look like? 187long long nf_ref = 0 ,nf_v5 = 0 ; 188for (int e = 0 ;e < E ;++ e ) { 189if (!mine [(size_t )e ])continue ; 190for (int r = 0 ;r < ELL ;++ r ) { 191const float a = yref [(size_t )e * ELL + r ],b2 = yv5 [(size_t )e * ELL + r ]; 192if (!std::isfinite (a ))++ nf_ref ; 193if (!std::isfinite (b2 ))++ nf_v5 ; 194 } 195 } 196 std::printf ("%-30s ng=%d rows=%d non-finite: ref %lld, v5 %lld\n" ,tag ,ng ,total ,nf_ref ,nf_v5 ); 197if (o_first >=0 ) { 198const int e = (int ) (o_first /ELL ),r = (int ) (o_first %ELL ); 199 std::printf (" first diff at entry %d row %d: ref %.6g v5 %.6g (ref[0:4] %.6g %.6g %.6g %.6g)\n" ,e ,r , 200 (double )yref [(size_t )o_first ], (double )yv5 [(size_t )o_first ], 201 (double )yref [(size_t )e * ELL ], (double )yref [(size_t )e * ELL + 1 ], 202 (double )yref [(size_t )e * ELL + 2 ], (double )yref [(size_t )e * ELL + 3 ]); 203 } 204 } 205 std::printf (" hbuf: differing %lld / %lld B first at byte %lld\n" ,hdiff , (long long )total * kHq ,h_first ); 206 std::printf (" out : differing %lld / %lld values max|d| %.3e rel %.3e first at value %lld\n" ,odiff , 207 (long long )total * ELL ,omax ,oref > 0 ?oabs /oref :0.0 ,o_first ); 208return (hdiff == 0 && odiff == 0 ) ?0 :1 ; 209 }; 210 211int fails = 0 ; 212 {// shape 1: 128 groups x 4 entries - padded to the pack's 512 experts, which is what group_latent walks 213 std::vector < int32_t > off ((size_t )NG + 1 ),cnt ((size_t )NG ,0 ); 214for (int g = 0 ;g < 128 ;++ g )cnt [(size_t )g ]= 4 ; 215for (int g = 0 ;g <=NG ;++ g )off [(size_t )g ]= 4 * std::min (g ,128 ); 216fails += run_shape ("128 groups x 4 entries" ,off ,cnt ); 217 } 218 {// shape 2: the engine's own table (already 512 experts) 219 std::vector < int32_t > off = rd < int32_t > ("g-off.bin" ,NG + 1 ); 220 std::vector < int32_t > cnt = rd < int32_t > ("g-cnt.bin" ,NG ); 221fails += run_shape ("engine table (512 groups)" ,off ,cnt ); 222 } 223 224// timing (reported only after the gate passes) 225if (fails == 0 ) { 226 std::vector < int32_t > off = rd < int32_t > ("g-off.bin" ,NG + 1 ); 227 std::vector < int32_t > cnt = rd < int32_t > ("g-cnt.bin" ,NG ); 228upl (dgs ,off );upl (dcnt ,cnt ); 229CK (cudaMemcpy (dng ,& NG ,4 ,cudaMemcpyHostToDevice )); 230cudaEvent_t t0 ,t1 ;CK (cudaEventCreate (& t0 ));CK (cudaEventCreate (& t1 )); 231 strata::prefill::fused_q3k::experts_latent_q3k (wb ,lg ,NG ,E ,dtables ,dxq ,dident ,dhbuf ,dv5 ,nullptr ); 232CK (cudaDeviceSynchronize ()); 233CK (cudaEventRecord (t0 )); 234for (int i = 0 ;i < 20 ;++ i ) 235 strata::prefill::fused_q3k::experts_latent_q3k (wb ,lg ,NG ,E ,dtables ,dxq ,dident ,dhbuf ,dv5 ,nullptr ); 236CK (cudaEventRecord (t1 ));CK (cudaEventSynchronize (t1 )); 237float ms = 0 ;CK (cudaEventElapsedTime (& ms ,t0 ,t1 )); 238 std::printf ("experts_latent_q3k: %.3f ms / 20 = %.4f ms %.4f us/entry (engine shape, %d entries)\n" ,ms , 239ms /20.0 ,ms /20.0 * 1000.0 /E ,E ); 240 std::printf ("frozen chain for comparison:" ); 241CK (cudaEventRecord (t0 )); 242for (int i = 0 ;i < 20 ;++ i ) { 243 strata::prefill::lat_q3k_gu (dblob ,dgs ,NG ,dxq ,dgate ,dup ,FF ,nullptr ,dcnt ,nullptr ); 244 strata::prefill::lat_q3k_swiglu_q8 (dgate ,dup ,E ,dhq ,nullptr ,nullptr ); 245 strata::prefill::lat_q3k_down (dblob ,dgs ,NG ,dhq ,dref ,ELL ,nullptr ,dcnt ,nullptr ); 246 } 247CK (cudaEventRecord (t1 ));CK (cudaEventSynchronize (t1 )); 248CK (cudaEventElapsedTime (& ms ,t0 ,t1 )); 249 std::printf (" %.4f ms %.4f us/entry\n" ,ms /20.0 ,ms /20.0 * 1000.0 /E ); 250CK (cudaEventDestroy (t0 ));CK (cudaEventDestroy (t1 )); 251 } 252 253 std::printf ("%s: moe_fused_q3k equivalent to the frozen chain (%d shapes differ)\n" ,fails == 0 ?"PASS" :"FAIL" , 254fails ); 255return fails == 0 ?0 :1 ; 256}