okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
01529aaa8
main
1// src/kernels/s2_gemv_parity.cpp - P2.S2's parity test for the S2 GEMV. 2// 3// The reference is the same CHAIN as the decode test: `strata::dequantize_q2_0` is a scalar transcription that 4// `bench/micro/dequant_xcheck` checks against **ggml's own** dequantizer, so comparing the GPU GEMV against a 5// dot product built on that scalar decode compares against ggml through one proven hop. 6// 7// WHY A TOLERANCE AND NOT BIT EQUALITY. The decode test is bit-exact because S2's decode is exact 8// integer-to-float arithmetic. A dot product is not: the compiler is free to contract `a*b + acc` into an 9// FMA, which changes the result in the last bits, and nvcc does. So this asserts a RELATIVE error, measures 10// what it actually is, and prints the number - the phase spec allows 1e-3 for FP16 paths, and if the measured 11// error were near that the tolerance would be hiding something rather than bounding rounding. 12#include "strata/artifact/dequant.hpp" 13#include "strata/kernels/s2_gemv.hpp" 14 15#include <cuda_runtime.h> 16 17#include <cmath> 18#include <cstdint> 19#include <cstdio> 20#include <cstring> 21#include <random> 22#include <vector> 23 24namespace { 25 26constexpr int QK = 64 ; 27 28void check (cudaError_t e ,const char * what ) { 29if (e != cudaSuccess ) { 30 std::fprintf (stderr ,"%s: %s\n" ,what ,cudaGetErrorString (e )); 31 std::exit (1 ); 32 } 33} 34 35// FP16 patterns, and they are deliberately NOT all powers of two. 36// 37// The first version of this table was {1.0, 0.5, 0.25, ...} - every value a power of two. The test then 38// reported a worst relative error of EXACTLY zero, which is not a property of the kernel: (code-1) is in 39// {-1,0,1,2} and a power-of-two times an fp16 is exact, so every product was exact and no rounding could 40// occur in either implementation. A test whose arithmetic cannot round cannot exercise its own tolerance, and 41// it would have hidden an accumulation-order or contraction bug for ever. 42// 43// The added patterns have full 11-bit mantissas, so products round and the two implementations' 44// rounding/contraction behaviour actually matters. 45const uint16_t kScales []= { 460x3C00 ,0x3800 ,0x3400 ,0x3000 ,0x2C00 ,0x2800 ,0x2000 ,// 1, 0.5, 0.25, ... powers of two 470xBC00 ,0xB800 ,0xB400 ,0xB000 ,0xAC00 ,0x1800 ,0x1400 , 480x3E00 ,0x3555 ,0x3C01 ,0x4248 ,0x4123 ,0x2AAA ,0x4A2B ,// 1.75, 0.333.., 1.0009765625, ... 490xBE00 ,0xB555 ,0xC248 ,0x2AAB ,0x4A2C ,0x2AAB }; 50 51}// namespace 52 53int main (int argc ,char ** argv ) { 54long long n_in = 2560 ;// the real n_embd, so the row length is the engine's 55long long n_out = 640 ;// the real expert intermediate width 56double tol = 1e-5 ; 57bool selftest = false; 58for (int i = 1 ;i < argc ;++ i ) { 59if (std::strcmp (argv [i ],"--selftest" )== 0 )selftest = true; 60else if (std::strcmp (argv [i ],"--n-in" )== 0 && i + 1 < argc )n_in = std::atoll (argv [++ i ]); 61else if (std::strcmp (argv [i ],"--n-out" )== 0 && i + 1 < argc )n_out = std::atoll (argv [++ i ]); 62else if (std::strcmp (argv [i ],"--tol" )== 0 && i + 1 < argc )tol = std::atof (argv [++ i ]); 63else { 64 std::fprintf (stderr ,"usage: s2_gemv_parity [--selftest] [--n-in N] [--n-out N] [--tol T]\n" ); 65return 2 ; 66 } 67 } 68if (n_in %QK != 0 ) { std::fprintf (stderr ,"--n-in must be a multiple of %d\n" ,QK );return 2 ; } 69const long long nb = n_in /QK ; 70 71 std::mt19937 rng (999 ); 72const int n_scales = (int ) (sizeof (kScales ) /sizeof (kScales [0 ])); 73 74// FP16 activations. Patterns are drawn from the same finite/exact table as the decode test, because a 75// random 16-bit pattern can be NaN and a NaN makes the comparison fail for the wrong reason. 76 std::vector < uint16_t > x ((size_t )n_in ); 77for (long long i = 0 ;i < n_in ;++ i )x [(size_t )i ]= kScales [rng () %n_scales ]; 78 79// S2 weight planes for an [n_in, n_out] matrix: row o occupies a contiguous run of nb blocks. 80 std::vector < uint8_t > codes ((size_t )n_out * nb * 16 ); 81 std::vector < float > scales ((size_t )n_out * nb ); 82for (size_t i = 0 ;i < codes .size ();++ i )codes [i ]= (uint8_t ) (rng ()& 0xFF ); 83for (size_t i = 0 ;i < scales .size ();++ i ) { 84scales [i ]= strata::fp16_to_fp32 (kScales [rng () %n_scales ]); 85 } 86 87// CPU reference: rebuild each row's RAW Q2_0 blocks from the planes, decode with the scalar path that 88// dequant_xcheck proved equal to ggml, and accumulate in the same order the kernel does. 89 std::vector < float > ref ((size_t )n_out ,0.0f ); 90 std::vector < uint8_t > raw (18 ); 91 std::vector < float > dec (QK ); 92for (long long o = 0 ;o < n_out ;++ o ) { 93float acc = 0.0f ; 94for (long long b = 0 ;b < nb ;++ b ) { 95const float d = scales [(size_t ) (o * nb + b )]; 96// write `d` back as the fp16 pattern the plane came from: the plane stores a WIDENED fp16, so 97// re-narrowing is exact and the scalar decoder sees the same value the kernel does 98const uint16_t d_bits = (uint16_t ) [& ] { 99for (uint16_t c :kScales ) { 100if (strata::fp16_to_fp32 (c )== d )return c ; 101 } 102return (uint16_t )0x3C00 ; 103 }(); 104raw [0 ]= (uint8_t ) (d_bits & 0xFF ); 105raw [1 ]= (uint8_t ) (d_bits >>8 ); 106 std::memcpy (& raw [2 ],& codes [(size_t ) ((o * nb + b )* 16 )],16 ); 107 strata::dequantize_q2_0 (raw .data (),dec .data ()); 108for (int j = 0 ;j < QK ;++ j ) { 109acc += dec [(size_t )j ]* strata::fp16_to_fp32 (x [(size_t ) (b * QK + j )]); 110 } 111 } 112ref [(size_t )o ]= acc ; 113 } 114 115uint16_t * d_x = nullptr ; 116uint8_t * d_codes = nullptr ; 117float * d_scales = nullptr ,* d_y = nullptr ; 118check (cudaMalloc (& d_x ,x .size ()* sizeof (uint16_t )),"cudaMalloc x" ); 119check (cudaMalloc (& d_codes ,codes .size ()),"cudaMalloc codes" ); 120check (cudaMalloc (& d_scales ,scales .size ()* sizeof (float )),"cudaMalloc scales" ); 121check (cudaMalloc (& d_y , (size_t )n_out * sizeof (float )),"cudaMalloc y" ); 122check (cudaMemcpy (d_x ,x .data (),x .size ()* sizeof (uint16_t ),cudaMemcpyHostToDevice ),"copy x" ); 123check (cudaMemcpy (d_codes ,codes .data (),codes .size (),cudaMemcpyHostToDevice ),"copy codes" ); 124check (cudaMemcpy (d_scales ,scales .data (),scales .size ()* sizeof (float ),cudaMemcpyHostToDevice ), 125"copy scales" ); 126 127 strata::kernels::s2_gemv (d_x ,d_codes ,d_scales ,d_y ,n_in ,n_out ); 128 129 std::vector < float > got ((size_t )n_out ); 130check (cudaMemcpy (got .data (),d_y ,got .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"copy back" ); 131 132long long bad = 0 ,first_bad = -1 ; 133double worst = 0.0 ; 134for (long long o = 0 ;o < n_out ;++ o ) { 135const double a = ref [(size_t )o ],b = got [(size_t )o ]; 136const double denom = std::fabs (a )> 1e-30 ? std::fabs (a ) :1e-30 ; 137const double rel = std::fabs (a - b ) /denom ; 138if (!(rel <=worst ))worst = rel ; 139if (!(rel <=tol )) { 140if (first_bad < 0 )first_bad = o ; 141++ bad ; 142 } 143 } 144 145// NON-VACUITY: a zero matrix and a zero activation vector agree at zero. The reference must actually vary 146// and its magnitudes must be non-trivial, or the comparison cannot distinguish anything. 147double lo = ref [0 ],hi = ref [0 ]; 148for (float v :ref ) { 149lo = std::fmin (lo ,v ); 150hi = std::fmax (hi ,v ); 151 } 152const double spread = hi - lo ; 153 std::printf ("s2_gemv: n_in %lld, n_out %lld, %lld rows over tolerance, worst rel %.3e (tol %.1e)\n" ,n_in , 154n_out ,bad ,worst ,tol ); 155 std::printf (" reference spread [%.4g, %.4g]\n" ,lo ,hi ); 156if (spread <=0.0 ) { 157 std::fprintf (stderr ,"VACUOUS: the reference is constant, so agreement proves nothing\n" ); 158return 1 ; 159 } 160if (bad ) { 161 std::printf (" first bad row %lld: ref %.9g got %.9g\n" ,first_bad , (double )ref [(size_t )first_bad ], 162 (double )got [(size_t )first_bad ]); 163return 1 ; 164 } 165 std::printf (" agrees with the scalar decode that dequant_xcheck proved equal to ggml\n" ); 166if (selftest ) std::printf ("s2_gemv_parity OK\n" ); 167 168cudaFree (d_x ); 169cudaFree (d_codes ); 170cudaFree (d_scales ); 171cudaFree (d_y ); 172return 0 ; 173}