okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd19246dc
main
1// lat_q3k_batch_self_test.cpp - L2a of the q3k acceptance sheet: the batch / row / blob assembly. 2// 3// Same inputs, two ways of cutting them: every routed expert in one WideBatch per kMaxBatch (the engine's walk, 4// resident blobs) versus one WideBatch per expert. The outputs must be bit-identical: if they are not, the 5// tables, the placement rows (off[]) or the blob-by-expert-id mapping is wrong, and no amount of comparing the 6// engine's rows against the native path can say which. 7// 8// The inputs are the engine's own: g-cnt/g-off/g-tok and d-xlq dumped from a real layer 0, and the pack's layer 0 9// blobs. Nothing here runs the engine. 10#include <cstdio> 11#include <cmath> 12#include <cstdlib> 13#include <cstring> 14#include <string> 15#include <vector> 16 17#include <cuda_runtime.h> 18 19#include "strata/prefill/moe_fused.hpp" 20#include "strata/prefill/moe_fused_q3k.hpp" 21 22namespace fused= strata::prefill::fused; 23namespace fused_q3k= strata::prefill::fused_q3k; 24 25namespace { 26constexpr int64_t kEll = 1280 ,kIff = 640 ; 27constexpr size_t kStride = 1164800 ;// one q3k expert's blob (gu 640*550 + up 352000 + down 640*360) 28const char * kDump = "/home/user/lat-s26/q3k/dumps4k" ;// g-cnt.bin g-off.bin g-tok.bin d-xlq.bin 29const char * kPack = "/home/user/models/v7_l1280_q3k_pack/latent_a_pack/experts.bin" ; 30constexpr int kLayer = 0 ;// native_experts.txt's first line: offset 0 31 32std::vector < uint8_t > slurp (const std::string & p ) { 33 std::vector < uint8_t > v ; 34if (std::FILE * f = std::fopen (p .c_str (),"rb" )) { 35 std::fseek (f ,0 ,SEEK_END ); 36v .resize ((size_t ) std::ftell (f )); 37 std::fseek (f ,0 ,SEEK_SET ); 38if (std::fread (v .data (),1 ,v .size (),f )!= v .size ())v .clear (); 39 std::fclose (f ); 40 } 41return v ; 42} 43 44void ck (cudaError_t e ,const char * what ) { 45if (e != cudaSuccess ) { 46 std::fprintf (stderr ,"FAIL %s: %s\n" ,what ,cudaGetErrorString (e )); 47 std::exit (2 ); 48 } 49} 50}// namespace 51 52int main () { 53auto cnt_b = slurp (std::string (kDump )+ "/g-cnt.bin" ); 54auto off_b = slurp (std::string (kDump )+ "/g-off.bin" ); 55auto tok_b = slurp (std::string (kDump )+ "/g-tok.bin" ); 56auto xlq_b = slurp (std::string (kDump )+ "/d-xlq.bin" ); 57if (cnt_b .empty ()|| off_b .empty ()|| tok_b .empty ()|| xlq_b .empty ()) { 58 std::fprintf (stderr ,"FAIL: the layer 0 dumps are missing under %s\n" ,kDump ); 59return 2 ; 60 } 61const int E = (int ) (cnt_b .size () /4 ); 62const int32_t * cnt = (const int32_t * )cnt_b .data (); 63const int32_t * off = (const int32_t * )off_b .data (); 64const int32_t * tok = (const int32_t * )tok_b .data (); 65const int64_t n = (int64_t ) (tok_b .size () /4 );// T * K entries 66const size_t xrow = (size_t )kEll /32 * 36 ; 67 std::printf ("L2a: %d experts, %lld entries, xlq %zu rows of %zu B, off[E] %d\n" ,E , (long long )n , 68xlq_b .size () /xrow ,xrow ,off [E ]); 69if (off [E ]!= n ) { 70 std::fprintf (stderr ,"FAIL: off[E] %d != T*K %lld (the placement does not cover the entries)\n" ,off [E ], 71 (long long )n ); 72return 2 ; 73 } 74 75// the pack's layer 0: whole experts, one stride each 76 std::vector < uint8_t > blobs ((size_t )E * kStride ); 77 { 78 std::FILE * f = std::fopen (kPack ,"rb" ); 79if (f == nullptr ) { 80 std::fprintf (stderr ,"FAIL: cannot open %s\n" ,kPack ); 81return 2 ; 82 } 83 std::fseek (f , (long )kLayer * 512 * (long )kStride ,SEEK_SET ); 84const size_t got = std::fread (blobs .data (),1 ,blobs .size (),f ); 85 std::fclose (f ); 86if (got != blobs .size ()) { 87 std::fprintf (stderr ,"FAIL: the pack gave %zu of %zu B\n" ,got ,blobs .size ()); 88return 2 ; 89 } 90 } 91 92// the device side: the tables, the H rows, the xl rows, the blobs and two sets of output rows 93const size_t tables_b = fused::latent_group_bytes (n ,E ); 94const size_t scratch_b = fused::latent_scratch_bytes (n ,E ,kIff ); 95const size_t lat_h = fused::latent_h_off (n ,E ); 96 std::vector < char > tables (tables_b ); 97 fused::group_latent (cnt ,off ,E ,tables .data (),tables .size ()); 98uint8_t * scratch_d = nullptr ; 99uint8_t * xlq_d = nullptr ; 100uint8_t * blobs_d = nullptr ; 101int32_t * tok_d = nullptr ; 102float * yA = nullptr ,* yB = nullptr ; 103ck (cudaMalloc (& scratch_d ,scratch_b ),"scratch" ); 104ck (cudaMalloc (& xlq_d ,xlq_b .size ()),"xlq" ); 105ck (cudaMalloc (& blobs_d ,blobs .size ()),"blobs" ); 106ck (cudaMalloc (& tok_d , (size_t )n * 4 ),"tok" ); 107ck (cudaMalloc (& yA , (size_t )n * kEll * 4 ),"yA" ); 108ck (cudaMalloc (& yB , (size_t )n * kEll * 4 ),"yB" ); 109ck (cudaMemcpy (scratch_d ,tables .data (),tables_b ,cudaMemcpyHostToDevice ),"tables" ); 110ck (cudaMemcpy (xlq_d ,xlq_b .data (),xlq_b .size (),cudaMemcpyHostToDevice ),"xlq" ); 111ck (cudaMemcpy (blobs_d ,blobs .data (),blobs .size (),cudaMemcpyHostToDevice ),"blobs" ); 112ck (cudaMemcpy (tok_d ,tok , (size_t )n * 4 ,cudaMemcpyHostToDevice ),"tok" ); 113ck (cudaMemset (yA ,0 , (size_t )n * kEll * 4 ),"yA zero" ); 114ck (cudaMemset (yB ,0 , (size_t )n * kEll * 4 ),"yB zero" ); 115const fused::LatentGeom lg {kEll ,kIff ,550 ,360 ,352000 ,704000 ,0 }; 116 117// A: the engine's walk - kMaxBatch experts a launch, every blob of the layer present 118 std::printf ("L2a: A = batches of %d experts, B = one expert a launch\n" , (int ) fused::kMaxBatch ); 119for (int e0 = 0 ;e0 < E ;e0 += (int ) fused::kMaxBatch ) { 120 fused::WideBatch fb ; 121fb .e0 = e0 ; 122fb .e1 = std::min (E ,e0 + (int ) fused::kMaxBatch ); 123for (int e = fb .e0 ;e < fb .e1 ;++ e )fb .blob [e - fb .e0 ]= blobs_d + (size_t )e * kStride ; 124 fused_q3k::experts_latent_q3k (fb ,lg ,E ,n ,scratch_d ,xlq_d ,tok_d ,scratch_d + lat_h ,yA ,nullptr ); 125 } 126ck (cudaDeviceSynchronize (),"A sync" ); 127 128// B: the same experts, one launch each 129for (int e = 0 ;e < E ;++ e ) { 130if (cnt [e ] <=0 )continue ; 131 fused::WideBatch fb ; 132fb .e0 = e ; 133fb .e1 = e + 1 ; 134fb .blob [0 ]= blobs_d + (size_t )e * kStride ; 135 fused_q3k::experts_latent_q3k (fb ,lg ,E ,n ,scratch_d ,xlq_d ,tok_d ,scratch_d + lat_h ,yB ,nullptr ); 136 } 137ck (cudaDeviceSynchronize (),"B sync" ); 138 139// compare row for row 140 std::vector < float > hA ((size_t )n * kEll ),hB ((size_t )n * kEll ); 141ck (cudaMemcpy (hA .data (),yA ,hA .size ()* 4 ,cudaMemcpyDeviceToHost ),"yA back" ); 142ck (cudaMemcpy (hB .data (),yB ,hB .size ()* 4 ,cudaMemcpyDeviceToHost ),"yB back" ); 143size_t bad_rows = 0 ,bad_vals = 0 ,first = (size_t )-1 ; 144double worst = 0 ; 145for (int64_t r = 0 ;r < n ;++ r ) { 146bool bad = false; 147for (int64_t c = 0 ;c < kEll ;++ c ) { 148const size_t i = (size_t )r * kEll + (size_t )c ; 149if (hA [i ]!= hB [i ]) { 150++ bad_vals ; 151bad = true; 152const double d = std::fabs ((double )hA [i ]- (double )hB [i ]); 153if (d > worst )worst = d ; 154 } 155 } 156if (bad ) { 157++ bad_rows ; 158if (first == (size_t )-1 )first = (size_t )r ; 159 } 160 } 161 std::printf ("L2a rows %lld: differing rows %zu, differing values %zu, first row %lld, max|d| %.3e\n" , 162 (long long )n ,bad_rows ,bad_vals , (long long )first ,worst ); 163if (bad_rows == 0 ) { 164 std::printf ("L2a PASS: the batch and the single-expert launches agree bit for bit\n" ); 165 }else { 166 std::printf ("L2a FAIL: the batch assembly changes the result (%zu rows)\n" ,bad_rows ); 167 } 168cudaFree (scratch_d );cudaFree (xlq_d );cudaFree (blobs_d );cudaFree (tok_d );cudaFree (yA );cudaFree (yB ); 169return bad_rows == 0 ?0 :1 ; 170}