okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
91e8d2122
main
1// src/kernels/s_gemv_q8k_parity.cpp - the Q8_K activation path of `s_gemv`. 2// 3// THE POINT OF THIS KERNEL IS A CONTRACT, so the test is built around the contract rather than around the 4// arithmetic: 5// 6// 1. the kernel against a host reference that dequantizes the SAME Q8_K bytes and the SAME S-form planes, 7// in double - so what is under test is the indexing, not the format; 8// 2. the Q8_K activation is produced by the ACTUAL `quantize_q8_K`, which is byte-exact against ggml 9// (round 198). Building the activation by hand here would make this a test of two transcriptions; 10// 3. **THE Q8_K-vs-FP16 GAP IS MEASURED**, because that is the number the whole activation-contract 11// decision rests on and it has only ever been measured on Q2_0 weights (0.6175%) and on BF16 ones 12// (0.141-0.156%). A K-quant weight is a third case and it is the one the dense projections use. 13#include "strata/kernels/f16_bits.hpp" 14#include "strata/kernels/quantize_act.hpp" 15#include "strata/kernels/s_gemv.hpp" 16 17#include <cuda_runtime.h> 18 19#include <cmath> 20#include <cstdio> 21#include <cstdlib> 22#include <cstring> 23#include <random> 24#include <vector> 25 26namespace { 27 28using strata::kernels::SForm ; 29using strata::kernels::Codebook ; 30 31void check (cudaError_t e ,const char * what ) { 32if (e != cudaSuccess ) { 33 std::fprintf (stderr ,"%s: %s\n" ,what ,cudaGetErrorString (e )); 34 std::exit (1 ); 35 } 36} 37 38/// The host decode, transcribed from `docs/pack-format.md`'s rule: `value = cb[code] * scale + offset`, with 39/// the integer bias applied INSIDE the integer domain before the one multiply. 40const signed char kIq4Nl [16 ]= {-127 ,-104 ,-83 ,-65 ,-49 ,-35 ,-22 ,-10 ,1 ,13 ,25 ,38 ,53 ,69 ,89 ,113 }; 41 42double weight_at (const std::vector < uint8_t >& codes ,const std::vector < float >& scales , 43const std::vector < float >& offset ,const SForm & f ,long long n_in ,long long o ,long long i ) { 44const int per_byte = 8 /f .code_bits ; 45const long long codes_per_row = n_in /per_byte ; 46const long long n_groups = n_in /f .group_elems ; 47const uint8_t * c = codes .data ()+ o * codes_per_row ; 48const int code = (c [i /per_byte ] >> ((int ) (i %per_byte )* f .code_bits ))& ((1 <<f .code_bits )- 1 ); 49const double v = (f .codebook == Codebook ::Iq4Nl ) ? (double )kIq4Nl [code & 0x0F ] 50 : (double ) (code + f .code_bias ); 51const long long g = i /f .group_elems ; 52double w = v * (double )scales [(size_t ) (o * n_groups + g )]; 53if (f .has_offset )w += (double )offset [(size_t ) (o * n_groups + g )]; 54return w ; 55} 56 57/// Dequantize a block_q8_K buffer on the host: 292 bytes per 256 elements. 58double q8k_at (const std::vector < uint8_t >& x ,long long i ) { 59const uint8_t * blk = x .data ()+ (size_t ) (i /256 )* 292 ; 60float d ; 61 std::memcpy (& d ,blk ,4 ); 62const int8_t q = ((const int8_t * ) (blk + 4 ))[i %256 ]; 63return (double )d * (double )q ; 64} 65 66struct Case { 67const char * what ; 68int bits ; 69int bias ; 70int group ; 71Codebook cb ; 72bool has_offset ; 73}; 74 75/// THE RAGGED LAST BLOCK. The split kernels give each warp one row and a block eight warps, so with 76/// `n_out % 8 != 0` the last block holds warps that have no row. Those warps used to return BEFORE the 77/// codebook barrier - undefined behaviour that every real shape (a multiple of 8) avoided. Here `n_out` is 78/// deliberately ragged, every row is checked against the host reference over the same bytes, and a guard 79/// band past `n_out` must come back untouched: a warp without a row must neither hang the block nor write. 80/// (`compute-sanitizer --tool synccheck` on this binary is the direct check of the barrier itself.) 81int ragged_rows (const Case & cs ,long long n_out ) { 82const long long n_in = 512 ; 83const long long guard = 64 ; 84 std::mt19937 rng (77 + (unsigned )n_out ); 85 std::normal_distribution < float > g (0.0f ,1.0f ); 86 std::vector < float > xf ((size_t )n_in ); 87for (auto & v :xf )v = g (rng ); 88SForm f ; 89f .code_bits = cs .bits ; 90f .code_bias = cs .bias ; 91f .group_elems = cs .group ; 92f .codebook = cs .cb ; 93f .has_offset = cs .has_offset ; 94const int per_byte = 8 /cs .bits ; 95 std::vector < uint8_t > codes ((size_t ) (n_out * n_in /per_byte )); 96for (auto & v :codes )v = (uint8_t ) (rng ()& 0xFF ); 97const long long n_groups = n_in /cs .group ; 98 std::vector < float > scales ((size_t ) (n_out * n_groups )),offs ((size_t ) (n_out * n_groups )); 99for (auto & v :scales )v = 0.01f + 0.001f * (float ) (rng () %20 ); 100for (auto & v :offs )v = 0.2f * g (rng ); 101 102float * d_xf = nullptr ; 103uint8_t * d_xk = nullptr ,* d_x0 = nullptr ,* d_codes = nullptr ; 104float * d_scales = nullptr ,* d_offs = nullptr ,* d_y = nullptr ; 105 std::vector < uint8_t > xk ((size_t ) (n_in /256 )* 292 ),x0 ((size_t ) (n_in /32 )* 34 ); 106check (cudaMalloc (& d_xf , (size_t )n_in * 4 ),"rxf" ); 107check (cudaMalloc (& d_xk ,xk .size ()),"rxk" ); 108check (cudaMalloc (& d_x0 ,x0 .size ()),"rx0" ); 109check (cudaMalloc (& d_codes ,codes .size ()),"rcodes" ); 110check (cudaMalloc (& d_scales ,scales .size ()* 4 ),"rscales" ); 111check (cudaMalloc (& d_offs ,offs .size ()* 4 ),"roffs" ); 112check (cudaMalloc (& d_y , (size_t ) (n_out + guard )* 4 ),"ry" ); 113check (cudaMemcpy (d_xf ,xf .data (), (size_t )n_in * 4 ,cudaMemcpyHostToDevice ),"rcxf" ); 114check (cudaMemcpy (d_codes ,codes .data (),codes .size (),cudaMemcpyHostToDevice ),"rcc" ); 115check (cudaMemcpy (d_scales ,scales .data (),scales .size ()* 4 ,cudaMemcpyHostToDevice ),"rcs" ); 116check (cudaMemcpy (d_offs ,offs .data (),offs .size ()* 4 ,cudaMemcpyHostToDevice ),"rco" ); 117 strata::kernels::quantize_q8_K (d_xf ,d_xk ,n_in ,nullptr ); 118 strata::kernels::quantize_q8_0 (d_xf ,d_x0 ,n_in ,nullptr ); 119check (cudaMemcpy (xk .data (),d_xk ,xk .size (),cudaMemcpyDeviceToHost ),"rcxk" ); 120check (cudaMemcpy (x0 .data (),d_x0 ,x0 .size (),cudaMemcpyDeviceToHost ),"rcx0" ); 121 122int bad = 0 ; 123for (int kind = 0 ;kind < 2 ;++ kind ) {// 0: Q8_K activation, 1: Q8_0 activation 124 std::vector < float > want ((size_t )n_out ); 125 std::vector < double > mag ((size_t )n_out );// sum |w_i a_i| per row: a row whose sum cancels near 0 126for (long long o = 0 ;o < n_out ;++ o ) {// must not turn f32 rounding into a relative failure 127double acc = 0 ,am = 0 ; 128for (long long i = 0 ;i < n_in ;++ i ) { 129double a ; 130if (kind == 0 ) { 131a = q8k_at (xk ,i ); 132 }else { 133const uint8_t * blk = x0 .data ()+ (size_t ) (i /32 )* 34 ; 134uint16_t dbits ; 135 std::memcpy (& dbits ,blk ,2 ); 136a = (double ) strata::kernels::f32_from_f16 (dbits )* (double ) ((const int8_t * ) (blk + 2 ))[i %32 ]; 137 } 138const double wa = weight_at (codes ,scales ,offs ,f ,n_in ,o ,i )* a ; 139acc += wa ; 140am += std::fabs (wa ); 141 } 142want [(size_t )o ]= (float )acc ; 143mag [(size_t )o ]= am ; 144 } 145// the guard band holds a NaN pattern; any write there shows up as a changed bit 146 std::vector < uint32_t > sentinel ((size_t ) (n_out + guard ),0x7FC0DEADu ); 147check (cudaMemcpy (d_y ,sentinel .data (),sentinel .size ()* 4 ,cudaMemcpyHostToDevice ),"rsent" ); 148if (kind == 0 ) 149 strata::kernels::s_gemv_q8k_split (d_xk ,d_codes ,d_scales ,cs .has_offset ?d_offs :nullptr ,d_y ,n_in , 150n_out ,f ,nullptr ); 151else 152 strata::kernels::s_gemv_q8_0_split (d_x0 ,d_codes ,d_scales ,cs .has_offset ?d_offs :nullptr ,d_y ,n_in , 153n_out ,f ,nullptr ); 154check (cudaDeviceSynchronize (),"ragged split" ); 155 std::vector < uint32_t > got_bits (sentinel .size ()); 156check (cudaMemcpy (got_bits .data (),d_y ,got_bits .size ()* 4 ,cudaMemcpyDeviceToHost ),"rcy" ); 157double m = 0 ,d = 0 ; 158for (long long o = 0 ;o < n_out ;++ o ) { 159float v ; 160 std::memcpy (& v ,& got_bits [(size_t )o ],4 ); 161m += mag [(size_t )o ]; 162d += std::fabs ((double )want [(size_t )o ]- (double )v ); 163 } 164long long touched = 0 ; 165for (long long o = n_out ;o < n_out + guard ;++ o )touched += got_bits [(size_t )o ]!= 0x7FC0DEADu ; 166const double rel = d / (m > 1e-30 ?m :1e-30 ); 167const bool ok = rel <=1e-5 && touched == 0 ; 168 std::printf (" %-28s %s n_out %3lld: rel %.3e, guard words written %lld %s\n" ,cs .what , 169kind == 0 ?"Q8_K" :"Q8_0" ,n_out ,rel ,touched ,ok ?"ok" :"*** WRONG ***" ); 170if (!ok )++ bad ; 171 } 172cudaFree (d_xf );cudaFree (d_xk );cudaFree (d_x0 );cudaFree (d_codes );cudaFree (d_scales );cudaFree (d_offs ); 173cudaFree (d_y ); 174return bad ; 175} 176 177}// namespace 178 179int main (int argc ,char ** argv ) { 180bool selftest = false; 181for (int i = 1 ;i < argc ;++ i ) { 182if (std::string (argv [i ])== "--selftest" )selftest = true; 183else { std::fprintf (stderr ,"usage: s_gemv_q8k_parity [--selftest]\n" );return 2 ; } 184 } 185int bad = 0 ; 186 187// The forms the DENSE weights actually use, taken from the manifest rather than invented: 188// attn_qkv.weight IQ4_XS S4 group 32, no offset (the GDN mixer's qkv) 189// attn_gate.weight Q4_K S4 group 32, offset (the GDN mixer's z) 190// ssm_out.weight Q5_K S8 group 32, offset (the GDN mixer's output) 191const Case cases []= { 192 {"IQ4_XS S4 g32 no-offset" ,4 ,-8 ,32 ,Codebook ::Affine , false}, 193 {"Q4_K S4 g32 offset" ,4 ,0 ,32 ,Codebook ::Affine , true}, 194 {"Q5_K S8 g32 offset" ,8 ,0 ,32 ,Codebook ::Affine , true}, 195 }; 196 197for (const Case & cs :cases ) { 198const long long n_in = 512 ,n_out = 64 ;// 2 Q8_K blocks wide, small enough for a host reference 199 std::mt19937 rng (2024 ); 200 std::normal_distribution < float > g (0.0f ,1.0f ); 201 std::vector < float > xf ((size_t )n_in ); 202for (auto & v :xf )v = g (rng ); 203 204SForm f ; 205f .code_bits = cs .bits ; 206f .code_bias = cs .bias ; 207f .group_elems = cs .group ; 208f .codebook = cs .cb ; 209f .has_offset = cs .has_offset ; 210 211const int per_byte = 8 /cs .bits ; 212 std::vector < uint8_t > codes ((size_t ) (n_out * n_in /per_byte )); 213for (auto & v :codes )v = (uint8_t ) (rng ()& 0xFF ); 214const long long n_groups = n_in /cs .group ; 215 std::vector < float > scales ((size_t ) (n_out * n_groups )),offs ((size_t ) (n_out * n_groups )); 216for (auto & v :scales )v = 0.01f + 0.001f * (float ) (rng () %20 ); 217for (auto & v :offs )v = 0.2f * g (rng ); 218 219// ---- the activation, from the REAL quantizer 220 std::vector < uint8_t > xq8k ((size_t ) (n_in /256 )* 292 ); 221float * d_xf = nullptr ; 222uint8_t * d_xq = nullptr ; 223check (cudaMalloc (& d_xf , (size_t )n_in * 4 ),"xf" ); 224check (cudaMalloc (& d_xq ,xq8k .size ()),"xq" ); 225check (cudaMemcpy (d_xf ,xf .data (), (size_t )n_in * 4 ,cudaMemcpyHostToDevice ),"cxf" ); 226 strata::kernels::quantize_q8_K (d_xf ,d_xq ,n_in ,nullptr ); 227check (cudaMemcpy (xq8k .data (),d_xq ,xq8k .size (),cudaMemcpyDeviceToHost ),"cxq" ); 228 229// ---- host reference in double, over the same bytes 230 std::vector < float > want ((size_t )n_out ,0.0f ); 231for (long long o = 0 ;o < n_out ;++ o ) { 232double acc = 0 ; 233for (long long i = 0 ;i < n_in ;++ i ) 234acc += weight_at (codes ,scales ,offs ,f ,n_in ,o ,i )* q8k_at (xq8k ,i ); 235want [(size_t )o ]= (float )acc ; 236 } 237 238// ---- device 239uint8_t * d_codes = nullptr ; 240float * d_scales = nullptr ,* d_offs = nullptr ,* d_y = nullptr ; 241check (cudaMalloc (& d_codes ,codes .size ()),"codes" ); 242check (cudaMalloc (& d_scales ,scales .size ()* 4 ),"scales" ); 243check (cudaMalloc (& d_offs ,offs .size ()* 4 ),"offs" ); 244check (cudaMalloc (& d_y , (size_t )n_out * 4 ),"y" ); 245check (cudaMemcpy (d_codes ,codes .data (),codes .size (),cudaMemcpyHostToDevice ),"cc" ); 246check (cudaMemcpy (d_scales ,scales .data (),scales .size ()* 4 ,cudaMemcpyHostToDevice ),"cs" ); 247check (cudaMemcpy (d_offs ,offs .data (),offs .size ()* 4 ,cudaMemcpyHostToDevice ),"co" ); 248 249 std::vector < float > naive ((size_t )n_out ),warp ((size_t )n_out ); 250 strata::kernels::s_gemv_q8k (d_xq ,d_codes ,d_scales ,cs .has_offset ?d_offs :nullptr ,d_y ,n_in ,n_out , 251f ,nullptr ); 252check (cudaMemcpy (naive .data (),d_y , (size_t )n_out * 4 ,cudaMemcpyDeviceToHost ),"cy1" ); 253 strata::kernels::s_gemv_q8k_split (d_xq ,d_codes ,d_scales ,cs .has_offset ?d_offs :nullptr ,d_y ,n_in , 254n_out ,f ,nullptr ); 255check (cudaMemcpy (warp .data (),d_y , (size_t )n_out * 4 ,cudaMemcpyDeviceToHost ),"cy2" ); 256 257double mag = 0 ,d1 = 0 ,d2 = 0 ; 258for (long long o = 0 ;o < n_out ;++ o ) { 259mag += std::fabs ((double )want [(size_t )o ]); 260d1 += std::fabs ((double )want [(size_t )o ]- (double )naive [(size_t )o ]); 261d2 += std::fabs ((double )want [(size_t )o ]- (double )warp [(size_t )o ]); 262 } 263 std::printf (" %-28s naive %.3e warp %.3e\n" ,cs .what ,d1 /mag ,d2 /mag ); 264// The kernel accumulates in f32, the reference in f64, over 512 terms with values of similar size. 265// 1e-5 is the arithmetic's floor, not a structural allowance. 266if (!(d1 /mag <=1e-5 && d2 /mag <=1e-5 )) { std::printf (" *** over 1e-5 ***\n" );++ bad ; } 267 268// ---- 2b. THE Q8_0 ACTIVATION, on the SAME weight planes. `s_gemv_q8_0_split` exists because the 269// legacy block formats want Q8_0 and `ffn_down_shexp` cannot use Q8_K at all (n_in = 640 is not a 270// multiple of 256). It shares every line with the Q8_K kernel except the activation loader, so what 271// this checks is the LOADER - the 34-byte block, the fp16 `d`, and the element index inside it. 272 { 273 std::vector < uint8_t > xq0 ((size_t ) (n_in /32 )* 34 ); 274uint8_t * d_x0 = nullptr ; 275check (cudaMalloc (& d_x0 ,xq0 .size ()),"x0" ); 276 strata::kernels::quantize_q8_0 (d_xf ,d_x0 ,n_in ,nullptr ); 277check (cudaMemcpy (xq0 .data (),d_x0 ,xq0 .size (),cudaMemcpyDeviceToHost ),"cx0" ); 278 279// the host reference over the SAME Q8_0 bytes, with the same weight decode 280 std::vector < float > want0 ((size_t )n_out ,0.0f ); 281for (long long o = 0 ;o < n_out ;++ o ) { 282double acc = 0 ; 283for (long long i = 0 ;i < n_in ;++ i ) { 284const uint8_t * blk = xq0 .data ()+ (size_t ) (i /32 )* 34 ; 285uint16_t dbits ; 286 std::memcpy (& dbits ,blk ,2 ); 287const double d = (double ) strata::kernels::f32_from_f16 (dbits ); 288const int8_t q = ((const int8_t * ) (blk + 2 ))[i %32 ]; 289acc += weight_at (codes ,scales ,offs ,f ,n_in ,o ,i )* d * (double )q ; 290 } 291want0 [(size_t )o ]= (float )acc ; 292 } 293 strata::kernels::s_gemv_q8_0_split (d_x0 ,d_codes ,d_scales ,cs .has_offset ?d_offs :nullptr ,d_y , 294n_in ,n_out ,f ,nullptr ); 295 std::vector < float > got0 ((size_t )n_out ); 296check (cudaMemcpy (got0 .data (),d_y , (size_t )n_out * 4 ,cudaMemcpyDeviceToHost ),"cy0" ); 297double m0 = 0 ,dd0 = 0 ; 298for (long long o = 0 ;o < n_out ;++ o ) { 299m0 += std::fabs ((double )want0 [(size_t )o ]); 300dd0 += std::fabs ((double )want0 [(size_t )o ]- (double )got0 [(size_t )o ]); 301 } 302 std::printf (" %-28s Q8_0 act %.3e\n" ,cs .what ,dd0 / (m0 > 1e-30 ?m0 :1e-30 )); 303if (!(dd0 / (m0 > 1e-30 ?m0 :1e-30 ) <=1e-5 )) { 304 std::printf (" *** the Q8_0 activation loader is WRONG ***\n" ); 305++ bad ; 306 } 307check (cudaFree (d_x0 ),"fx0" ); 308 } 309 310// ---- 3. THE Q8_K vs FP16 GAP, on a K-quant weight - the case the contract decision needs 311 { 312 std::vector < uint16_t > x16 ((size_t )n_in ); 313for (long long i = 0 ;i < n_in ;++ i )x16 [(size_t )i ]= strata::kernels::f16_from_f32 (xf [(size_t )i ]); 314uint16_t * d_x16 = nullptr ; 315check (cudaMalloc (& d_x16 , (size_t )n_in * 2 ),"x16" ); 316check (cudaMemcpy (d_x16 ,x16 .data (), (size_t )n_in * 2 ,cudaMemcpyHostToDevice ),"cx16" ); 317 std::vector < float > fp16out ((size_t )n_out ); 318 strata::kernels::s_gemv (d_x16 ,d_codes ,d_scales ,cs .has_offset ?d_offs :nullptr ,d_y ,n_in ,n_out , 319f ); 320check (cudaMemcpy (fp16out .data (),d_y , (size_t )n_out * 4 ,cudaMemcpyDeviceToHost ),"cy3" ); 321double dd = 0 ; 322for (long long o = 0 ;o < n_out ;++ o ) 323dd += std::fabs ((double )naive [(size_t )o ]- (double )fp16out [(size_t )o ]); 324const double rel = dd /mag ; 325const bool visible = rel > 1e-3 ; 326 std::printf (" %-24s %s (%.4f%% apart, tolerance 0.1%%)\n" ,"Q8_K vs fp16 activation" , 327visible ?"yes" :"*** NO ***" ,rel * 100 ); 328if (!visible )++ bad ; 329cudaFree (d_x16 ); 330 } 331cudaFree (d_xf );cudaFree (d_xq );cudaFree (d_codes );cudaFree (d_scales );cudaFree (d_offs );cudaFree (d_y ); 332 } 333 334// ---- 4. output widths that are NOT a multiple of the eight rows per block 335 std::printf ("\n" ); 336for (const Case & cs :cases ) 337for (long long n_out : {1LL ,7LL ,9LL ,61LL })bad += ragged_rows (cs ,n_out ); 338 339 std::printf ("\ns_gemv_q8k: %d failures\n" ,bad ); 340if (bad )return 1 ; 341if (selftest ) std::printf ("s_gemv_q8k_parity OK\n" ); 342return 0 ; 343}