okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd19246dc
main
1// src/prefill/lat_q3k_onegroup_test.cpp - the shape the engine actually runs when it walks one expert at a time: 2// ONE group with thousands of rows (so the launcher's blockIdx.z fill is active), and the boundaries around a tile. 3// The reference is native_expert_grouped, which has no z fill at all - the serial chain of my own kernels cannot be 4// the reference here, because the gu and the down both carry the same z fill and a defect in it cancels out. 5#include "strata/prefill/lat_q3k_fused.hpp" 6#include "strata/prefill/lat_q3k_gu.hpp" 7#include "strata/prefill/lat_q3k_swiglu.hpp" 8#include "strata/prefill/lat_q3k_down.hpp" 9#include "strata/kernels/iq_kernels.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 E = 42450 ;// the engine's entry space (the dumps' off[NG]) 33constexpr int NTOK = E ;// one xl row an entry here (the engine shares rows; the maps are tested elsewhere) 34 35long long n_diff (const float * a ,const float * b ,size_t n ) { 36long long d = 0 ; 37for (size_t i = 0 ;i < n ;++ i )d += (a [i ]!= b [i ]); 38return d ; 39} 40}// namespace 41 42int main () { 43 std::mt19937 rng (83 ); 44 std::normal_distribution < float > nw (0.f ,0.05f ); 45 std::vector < uint8_t > blob ((size_t )NBLOB * (GUB + DNB )); 46for (int i = 0 ;i < NBLOB ;++ i ) { 47uint8_t * b = blob .data ()+ (size_t )i * (GUB + DNB ); 48 std::vector < float > w ((size_t )1280 * ELL ); 49for (auto & v :w )v = nw (rng ); 50if ((size_t )ggml_quantize_chunk (GGML_TYPE_Q3_K ,w .data (),b ,0 ,1280 ,ELL ,nullptr )!= (size_t )GUB ) { 51 std::printf ("FAIL gu quantize\n" );return 1 ; 52 } 53 std::vector < float > wd ((size_t )ELL * FF ); 54for (auto & v :wd )v = nw (rng ); 55if ((size_t )ggml_quantize_chunk (GGML_TYPE_IQ4_NL ,wd .data (),b + GUB ,0 ,ELL ,FF ,nullptr )!= (size_t )DNB ) { 56 std::printf ("FAIL dn quantize\n" );return 1 ; 57 } 58 } 59const size_t EB = (size_t ) (GUB + DNB ); 60uint8_t * dall = nullptr ; 61CK (cudaMalloc (& dall ,blob .size ())); 62CK (cudaMemcpy (dall ,blob .data (),blob .size (),cudaMemcpyHostToDevice )); 63 std::vector < const uint8_t *> bp ((size_t )8 ); 64for (int g = 0 ;g < 8 ;++ g )bp [(size_t )g ]= dall + (size_t ) (g %NBLOB )* EB ; 65const uint8_t ** dblob = nullptr ; 66CK (cudaMalloc (& dblob ,8 * sizeof (uint8_t * ))); 67CK (cudaMemcpy (dblob ,bp .data (),8 * sizeof (uint8_t * ),cudaMemcpyHostToDevice )); 68 69 std::vector < uint8_t > xq ((size_t )NTOK * kXq ); 70 { 71 std::uniform_int_distribution < int > u8 (-127 ,127 ); 72for (size_t i = 0 ;i < xq .size ();++ i )xq [i ]= (uint8_t )u8 (rng ); 73for (int t = 0 ;t < NTOK ;++ t ) 74for (int k = 0 ;k < (int ) (ELL /32 );++ k ) { 75uint8_t * b = xq .data ()+ (size_t )t * kXq + 36 * k ; 76const uint16_t d16 = ggml_fp32_to_fp16 (0.01f + 1e-4f * ((t + k ) %97 )),s16 = 0 ; 77 std::memcpy (b ,& d16 ,2 ); std::memcpy (b + 2 ,& s16 ,2 ); 78 } 79 } 80uint8_t * dxq = nullptr ; 81CK (cudaMalloc (& dxq ,xq .size ())); 82CK (cudaMemcpy (dxq ,xq .data (),xq .size (),cudaMemcpyHostToDevice )); 83 84int32_t * dgs = nullptr ,* dcnt = nullptr ,* dident = nullptr ,* dng = nullptr ; 85CK (cudaMalloc (& dgs ,9 * 4 ));CK (cudaMalloc (& dcnt ,8 * 4 )); 86CK (cudaMalloc (& dident , (size_t )E * 4 ));CK (cudaMalloc (& dng ,4 )); 87 std::vector < int32_t > ident ((size_t )E ); 88for (int e = 0 ;e < E ;++ e )ident [(size_t )e ]= e ; 89CK (cudaMemcpy (dident ,ident .data (),ident .size ()* 4 ,cudaMemcpyHostToDevice )); 90int32_t * dident_tok = nullptr ;// native dereferences ent_tok as well: the identity, so entry e reads row e 91CK (cudaMalloc (& dident_tok , (size_t )E * 4 )); 92CK (cudaMemcpy (dident_tok ,ident .data (),ident .size ()* 4 ,cudaMemcpyHostToDevice )); 93 94float * dgate = nullptr ;float * dup = nullptr ; 95CK (cudaMalloc (& dgate , (size_t )E * FF * 4 ));CK (cudaMalloc (& dup , (size_t )E * FF * 4 )); 96uint8_t * dhq = nullptr ;uint8_t * dhbuf = nullptr ; 97CK (cudaMalloc (& dhq , (size_t )E * kHq ));CK (cudaMalloc (& dhbuf , (size_t )E * kHq )); 98float * dy = nullptr ,* dy2 = nullptr ,* dyn = nullptr ; 99CK (cudaMalloc (& dy , (size_t )E * ELL * 4 ));CK (cudaMalloc (& dy2 , (size_t )E * ELL * 4 )); 100CK (cudaMalloc (& dyn , (size_t )E * ELL * 4 )); 101const auto L = strata::kernels::native_expert_layout (GGML_TYPE_Q3_K ,GGML_TYPE_IQ4_NL ,ELL ,FF ); 102void * scratch = nullptr ; 103CK (cudaMalloc (& scratch , strata::kernels::native_expert_scratch_bytes (E ,FF ))); 104 105 std::vector < float > ys ((size_t )E * ELL ),yf ((size_t )E * ELL ),yn ((size_t )E * ELL ); 106 107// one case: ng groups with (starts, counts); every row zeroed first so an unwritten row shows 108auto run_case = [& ](const char * tag ,int ng ,const int32_t * starts ,const int32_t * counts ,bool vs_native )-> int { 109CK (cudaMemcpy (dgs ,starts , (size_t ) (ng + 1 )* 4 ,cudaMemcpyHostToDevice )); 110CK (cudaMemcpy (dcnt ,counts , (size_t )ng * 4 ,cudaMemcpyHostToDevice )); 111CK (cudaMemcpy (dng ,& ng ,4 ,cudaMemcpyHostToDevice )); 112int total = 0 ; 113for (int g = 0 ;g < ng ;++ g )total += counts [g ]; 114// fused 115CK (cudaMemset (dy2 ,0 , (size_t )E * ELL * 4 )); 116 strata::prefill::lat_q3k_fused (dblob ,dgs ,ng ,dxq ,dhbuf ,dy2 ,ELL ,nullptr ,dcnt ,nullptr ,dident ); 117// serial, same buffers 118CK (cudaMemset (dy ,0 , (size_t )E * ELL * 4 )); 119 strata::prefill::lat_q3k_gu (dblob ,dgs ,ng ,dxq ,dgate ,dup ,FF ,nullptr ,dcnt ,nullptr ); 120 strata::prefill::lat_q3k_swiglu_q8 (dgate ,dup ,E ,dhq ,nullptr ,nullptr ); 121 strata::prefill::lat_q3k_down (dblob ,dgs ,ng ,dhq ,dy ,ELL ,nullptr ,dcnt ,dident ); 122CK (cudaDeviceSynchronize ()); 123CK (cudaMemcpy (ys .data (),dy ,ys .size ()* 4 ,cudaMemcpyDeviceToHost )); 124CK (cudaMemcpy (yf .data (),dy2 ,yf .size ()* 4 ,cudaMemcpyDeviceToHost )); 125const long long dser = n_diff (ys .data (),yf .data (),ys .size ()); 126double rel = 0 ,cosmin = 1.0 ; 127int bad = 0 ,first_bad = -1 ,first_rel = -1 ; 128if (vs_native ) { 129 strata::kernels::native_expert_set_mode (-1 ,0 ); 130CK (cudaMemset (dyn ,0 , (size_t )E * ELL * 4 )); 131 strata::kernels::native_expert_grouped (L , (const unsigned long long * )dblob ,dgs ,dng ,dident ,dident_tok , 132ng ,E ,dxq ,scratch ,dyn ,nullptr ,0 ); 133CK (cudaDeviceSynchronize ()); 134CK (cudaMemcpy (yn .data (),dyn ,yn .size ()* 4 ,cudaMemcpyDeviceToHost )); 135double sabs = 0 ,sref = 0 ; 136for (int g = 0 ;g < ng ;++ g ) 137for (int e = starts [g ];e < starts [g ]+ counts [g ];++ e ) { 138double num = 0 ,aa = 0 ,bb = 0 ; 139for (int r = 0 ;r < ELL ;++ r ) { 140const double a = yf [(size_t )e * ELL + r ],b = yn [(size_t )e * ELL + r ]; 141sabs += std::fabs (a - b );sref += std::fabs (b ); 142num += a * b ;aa += a * a ;bb += b * b ; 143 } 144const double c = num / (std::sqrt (aa )* std::sqrt (bb )+ 1e-30 ); 145cosmin = std::min (cosmin ,c ); 146if (c < 0.99 ) { 147++ bad ; 148if (first_bad < 0 )first_bad = e - starts [g ]; 149if (first_rel < 0 && std::fabs (yf [(size_t )e * ELL ]- yn [(size_t )e * ELL ])> 1e-9 ) 150first_rel = e - starts [g ]; 151 } 152 } 153rel = sref > 0 ?sabs /sref :0.0 ; 154 } 155 std::printf ("%-28s ng=%d rows=%5d | fused vs serial: %lld diff | vs native: rel %.3e cos_min %.9f bad %d " 156"(first bad row-in-group %d)\n" ,tag ,ng ,total ,dser ,rel ,cosmin ,bad ,first_bad ); 157const bool ok = dser == 0 && (!vs_native || (rel <=1e-3 && cosmin >=0.9999 && bad == 0 )); 158if (!ok ) { 159 std::printf (" FAIL %s\n" ,tag ); 160for (int g = 0 ;g < ng ;++ g ) { 161long long dg = 0 ; 162int fb = -1 ; 163for (int e = starts [g ];e < starts [g ]+ counts [g ];++ e ) { 164const long long d = n_diff (yf .data ()+ (size_t )e * ELL ,ys .data ()+ (size_t )e * ELL ,ELL ); 165if (d && fb < 0 )fb = e - starts [g ]; 166dg += d ; 167 } 168 std::printf (" group %d (start %d, cnt %d): %lld floats differ from serial, first at row %d\n" ,g , 169starts [g ],counts [g ],dg ,fb ); 170 } 171 } 172return ok ?0 :1 ; 173 }; 174 175int fails = 0 ; 176// the engine's real big groups, one group a launch (what the per-expert walk does) 177 { 178const int32_t s1 [2 ]= {32028 ,32028 + 2749 },c1 [1 ]= {2749 }; 179fails += run_case ("nb=1 cnt=2749 (group 327)" ,1 ,s1 ,c1 , true); 180const int32_t s2 [2 ]= {34777 ,34777 + 400 },c2 [1 ]= {400 }; 181fails += run_case ("nb=1 cnt=400 (group 334)" ,1 ,s2 ,c2 , true); 182const int32_t s3 [2 ]= {37469 ,37469 + 2376 },c3 [1 ]= {2376 }; 183fails += run_case ("nb=1 cnt=2376 (group 434)" ,1 ,s3 ,c3 , true); 184const int32_t s4 [3 ]= {30567 ,31035 ,31076 },c4 [2 ]= {468 ,41 }; 185fails += run_case ("nb=2 cnt={468,41} (253/254)" ,2 ,s4 ,c4 , true); 186 } 187// the boundaries around a tile, and the counts the engine really reaches 188 { 189const int sizes []= {1 ,2 ,63 ,64 ,65 ,127 ,128 ,129 ,255 ,256 ,257 ,468 ,1231 ,2748 ,2749 ,3098 }; 190for (int c :sizes ) { 191const int32_t s [2 ]= {0 ,c },ct [1 ]= {c }; 192char tag [64 ]; 193 std::snprintf (tag ,sizeof tag ,"nb=1 sweep cnt=%d" ,c ); 194fails += run_case (tag ,1 ,s ,ct , (c == 2749 || c == 3098 )); 195 } 196 } 197 std::printf ("%s: one-group / z-fill / tile boundaries (fails %d)\n" ,fails == 0 ?"PASS" :"FAIL" ,fails ); 198return fails == 0 ?0 :1 ; 199}