okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
01529aaa8
main
1// src/kernels/quantize_act_parity.cpp - P2.S2's parity test for the Q8_0 activation quantizer. 2// 3// THE CHECK IS ON THE BYTES, not on the round-tripped values. `ref/quant.py::q8_0`'s central finding is that 4// dividing by the fp16-rounded scale instead of the fp32 one changed 18 of 80 blocks' quantized integers 5// while leaving the dequantized values within a rounding step - so a comparison of VALUES would have passed 6// and the engine would still have disagreed with ggml. Comparing the 34-byte block catches that directly. 7// 8// The reference is a host transcription of `ref/quant.py::q8_0`: 9// 10// d32 = amax / 127.0f (f32) 11// d16 = fp16(d32) (stored, and used to DEQUANTIZE) 12// q = clip(rint(x / (double)d32), -128, 127) 13// round trip = q * d16 14#include "strata/kernels/quantize_act.hpp" 15#include "strata/kernels/f16_bits.hpp" 16 17#include <cuda_runtime.h> 18 19#include <algorithm> 20#include <cmath> 21#include <cstdint> 22#include <cstdio> 23#include <cstdlib> 24#include <cstring> 25#include <random> 26#include <vector> 27 28namespace { 29 30void check (cudaError_t e ,const char * what ) { 31if (e != cudaSuccess ) { 32 std::fprintf (stderr ,"%s: %s\n" ,what ,cudaGetErrorString (e )); 33 std::exit (1 ); 34 } 35} 36 37// THE CONVERSIONS COME FROM `f16_bits.hpp`, and this test used to carry its own. The private `f16_bits_to_f32` 38// below flushed fp16 SUBNORMALS to zero, and the private encoder returned NaN for finite fp16 overflow - both 39// invisible with the O(1) fixture this file has. Round 198 validated the shared pair against 395 numpy 40// float16 vectors and all 65,536 reverse patterns, so the test now shares the implementation it is checking 41// instead of guessing at it independently. Thin aliases keep the body below unchanged. 42using strata::kernels::f16_from_f32 ; 43using strata::kernels::f32_from_f16 ; 44inline uint16_t f32_to_f16_bits (float f ) {return f16_from_f32 (f ); } 45inline float f16_bits_to_f32 (uint16_t h ) {return f32_from_f16 (h ); } 46 47void reference_q8_0 (const float * x ,long long n , std::vector < uint8_t >& blocks , std::vector < float >& back ) { 48blocks .assign ((size_t ) (n /32 )* 34 ,0 ); 49back .assign ((size_t )n ,0.0f ); 50for (long long b = 0 ;b < n /32 ;++ b ) { 51const float * xb = x + b * 32 ; 52float amax = 0.0f ; 53for (int i = 0 ;i < 32 ;++ i )amax = std::fmax (amax , std::fabs (xb [i ])); 54uint8_t * out = & blocks [(size_t )b * 34 ]; 55if (amax == 0.0f )continue ; 56const float d32 = amax /127.0f ; 57const uint16_t dbits = f32_to_f16_bits (d32 ); 58out [0 ]= (uint8_t ) (dbits & 0xFF ); 59out [1 ]= (uint8_t ) (dbits >>8 ); 60const float d16 = f16_bits_to_f32 (dbits ); 61for (int i = 0 ;i < 32 ;++ i ) { 62double q = std::rint ((double )xb [i ] / (double )d32 ); 63if (q > 127.0 )q = 127.0 ; 64if (q < -128.0 )q = -128.0 ; 65out [2 + i ]= (uint8_t ) (int8_t )q ; 66back [(size_t ) (b * 32 + i )]= (float ) (int8_t )out [2 + i ]* d16 ; 67 } 68 } 69} 70 71int run_case (const char * name ,const std::vector < float >& x ,bool check_bytes ) { 72const long long n = (long long )x .size (); 73 std::vector < uint8_t > r_blocks ,g_blocks ((size_t ) (n /32 )* 34 ); 74 std::vector < float > r_back ,g_back ((size_t )n ); 75reference_q8_0 (x .data (),n ,r_blocks ,r_back ); 76 77float * d_x = nullptr ; 78uint8_t * d_b = nullptr ; 79float * d_back = nullptr ; 80check (cudaMalloc (& d_x , (size_t )n * sizeof (float )),"malloc x" ); 81check (cudaMalloc (& d_b ,g_blocks .size ()),"malloc blocks" ); 82check (cudaMalloc (& d_back , (size_t )n * sizeof (float )),"malloc back" ); 83check (cudaMemcpy (d_x ,x .data (), (size_t )n * sizeof (float ),cudaMemcpyHostToDevice ),"copy x" ); 84// PROBE: does the DEVICE hold what the host thinks it sent? The scale byte differs with the SAME 85// exponent and a zeroed mantissa, which says the kernel's amax differs from the host's on identical 86// input - so the first thing to rule out is the copy itself. 87 { 88 std::vector < float > rt ((size_t )n ); 89check (cudaMemcpy (rt .data (),d_x , (size_t )n * sizeof (float ),cudaMemcpyDeviceToHost ),"probe copy back" ); 90long long diff = 0 ; 91for (long long i = 0 ;i < n ;++ i )if (std::memcmp (& rt [(size_t )i ],& x [(size_t )i ],4 )!= 0 )++ diff ; 92if (diff ) std::printf (" PROBE: device x differs from host x in %lld of %lld elements\n" ,diff ,n ); 93 } 94 strata::kernels::quantize_q8_0 (d_x ,d_b ,n ,nullptr ); 95 strata::kernels::dequant_q8_0 (d_b ,d_back ,n ,nullptr ); 96check (cudaMemcpy (g_blocks .data (),d_b ,g_blocks .size (),cudaMemcpyDeviceToHost ),"back blocks" ); 97check (cudaMemcpy (g_back .data (),d_back , (size_t )n * sizeof (float ),cudaMemcpyDeviceToHost ),"back vals" ); 98 99long long byte_bad = 0 ,val_bad = 0 ; 100if (check_bytes ) { 101for (size_t i = 0 ;i < g_blocks .size ();++ i )if (g_blocks [i ]!= r_blocks [i ])++ byte_bad ; 102 } 103for (long long i = 0 ;i < n ;++ i ) { 104const float a = r_back [(size_t )i ],b = g_back [(size_t )i ]; 105// the round trip is `q * d16`: both sides use the SAME fp16 d, so this is exact when q agrees 106if (std::memcmp (& a ,& b ,sizeof (float ))!= 0 )++ val_bad ; 107 } 108if (byte_bad && check_bytes ) { 109for (size_t i = 0 ;i < g_blocks .size ();++ i ) { 110if (g_blocks [i ]!= r_blocks [i ]) { 111const size_t blk = i /34 ; 112const float amax = [& ] {float m = 0 ;for (int k = 0 ;k < 32 ;++ k )m = std::fmax (m , std::fabs (x [(size_t )(blk * 32 + k )]));return m ; }(); 113 std::printf (" first bad byte: block %zu offset %zu amax %.9g d32 %.9g\n" ,blk ,i %34 ,amax ,amax /127.0f ); 114 std::printf (" ref scale bytes %02x %02x got %02x %02x\n" ,r_blocks [blk * 34 ],r_blocks [blk * 34 + 1 ],g_blocks [blk * 34 ],g_blocks [blk * 34 + 1 ]); 115 std::printf (" ref[0..9]:" );for (int k = 0 ;k < 10 ;++ k ) std::printf (" %02x" ,r_blocks [blk * 34 + k ]); 116 std::printf ("\n got[0..9]:" );for (int k = 0 ;k < 10 ;++ k ) std::printf (" %02x" ,g_blocks [blk * 34 + k ]); 117 std::printf ("\n ref[10..17]:" );for (int k = 10 ;k < 18 ;++ k ) std::printf (" %02x" ,r_blocks [blk * 34 + k ]); 118 std::printf ("\n got[10..17]:" );for (int k = 10 ;k < 18 ;++ k ) std::printf (" %02x" ,g_blocks [blk * 34 + k ]); 119 std::printf ("\n x[0..3] = %.9g %.9g %.9g %.9g\n" , (double )x [(size_t )(blk * 32 )], (double )x [(size_t )(blk * 32 + 1 )], (double )x [(size_t )(blk * 32 + 2 )], (double )x [(size_t )(blk * 32 + 3 )]); 120break ; 121 } 122 } 123 } 124 std::printf (" %-26s blocks %s (%lld bad bytes) round trip %s (%lld differ)\n" ,name , 125 !check_bytes ?"not compared" : (byte_bad ?"*** WRONG ***" :"byte-exact" ),byte_bad , 126val_bad ?"*** WRONG ***" :"bit-exact" ,val_bad ); 127cudaFree (d_x ); 128cudaFree (d_b ); 129cudaFree (d_back ); 130return (int ) (byte_bad + val_bad ); 131} 132 133// A host transcription of ggml's `quantize_row_q8_K_ref` (ggml-quants.c L2768), including `nearest_int`. 134// 135// NOTE WHICH REFERENCE THIS IS. `ref/quant.py::q8_K` is the numpy transcription and it multiplies in 136// FLOAT64 (`iscale * blk.astype(np.float64)`), while ggml multiplies in FLOAT32 before `nearest_int`. The 137// oracle is ggml, so this follows ggml. The two agree except where an f32 product lands exactly on a tie or 138// a rounding boundary, which is exactly the regime the "exact ties" case below constructs. 139int nearest_int_host (float fval ) { 140const float val = fval + 12582912.0f ; 141int i ; 142 std::memcpy (& i ,& val ,4 ); 143return (i & 0x007fffff )- 0x00400000 ; 144} 145 146void reference_q8_K (const float * x ,long long n , std::vector < uint8_t >& blocks , std::vector < float >& back , 147float scale_num ) { 148const int QK = 256 ; 149blocks .assign ((size_t ) (n /QK )* 292 ,0 ); 150back .assign ((size_t )n ,0.0f ); 151for (long long b = 0 ;b < n /QK ;++ b ) { 152const float * xb = x + b * QK ; 153uint8_t * out = & blocks [(size_t )b * 292 ]; 154float max = 0.0f ,amax = 0.0f ; 155for (int j = 0 ;j < QK ;++ j ) { 156const float ax = std::fabs (xb [j ]); 157if (ax > amax ) {amax = ax ;max = xb [j ]; }// STRICTLY greater: ties keep the FIRST maximum 158 } 159if (amax == 0.0f )continue ;// d = 0, qs = 0, bsums = 0 (see the kernel note) 160const float iscale = scale_num /max ;// -127 by default; -128 is the rival reading 161int8_t * qs = (int8_t * ) (out + 4 ); 162for (int j = 0 ;j < QK ;++ j ) { 163const int v = nearest_int_host (iscale * xb [j ]); 164qs [j ]= (int8_t ) std::min (127 ,v ); 165 } 166int16_t * bsums = (int16_t * ) (out + 4 + QK ); 167for (int j = 0 ;j < QK /16 ;++ j ) { 168int sum = 0 ; 169for (int ii = 0 ;ii < 16 ;++ ii )sum += qs [j * 16 + ii ]; 170bsums [j ]= (int16_t )sum ; 171 } 172const float d = 1.0f /iscale ; 173 std::memcpy (out ,& d ,4 ); 174for (int j = 0 ;j < QK ;++ j )back [(size_t ) (b * QK + j )]= (float )qs [j ]* d ; 175 } 176} 177 178int run_case_k (const char * name ,const std::vector < float >& x ,bool check_bytes ,float scale_num = -127.0f ) { 179const long long n = (long long )x .size (); 180 std::vector < uint8_t > r_blocks ,g_blocks ((size_t ) (n /256 )* 292 ); 181 std::vector < float > r_back ,g_back ((size_t )n ); 182reference_q8_K (x .data (),n ,r_blocks ,r_back ,scale_num ); 183 184float * d_x = nullptr ; 185uint8_t * d_b = nullptr ; 186float * d_back = nullptr ; 187check (cudaMalloc (& d_x , (size_t )n * sizeof (float )),"malloc x" ); 188check (cudaMalloc (& d_b ,g_blocks .size ()),"malloc blocks" ); 189check (cudaMalloc (& d_back , (size_t )n * sizeof (float )),"malloc back" ); 190check (cudaMemcpy (d_x ,x .data (), (size_t )n * sizeof (float ),cudaMemcpyHostToDevice ),"copy x" ); 191 strata::kernels::quantize_q8_K (d_x ,d_b ,n ,nullptr ); 192 strata::kernels::dequant_q8_K (d_b ,d_back ,n ,nullptr ); 193check (cudaMemcpy (g_blocks .data (),d_b ,g_blocks .size (),cudaMemcpyDeviceToHost ),"back blocks" ); 194check (cudaMemcpy (g_back .data (),d_back , (size_t )n * sizeof (float ),cudaMemcpyDeviceToHost ),"back vals" ); 195 196long long byte_bad = 0 ,val_bad = 0 ,first_bad = -1 ; 197if (check_bytes ) 198for (size_t i = 0 ;i < g_blocks .size ();++ i ) 199if (g_blocks [i ]!= r_blocks [i ]) {if (first_bad < 0 )first_bad = (long long )i ;++ byte_bad ; } 200for (long long i = 0 ;i < n ;++ i ) 201if (std::memcmp (& r_back [(size_t )i ],& g_back [(size_t )i ],4 )!= 0 )++ val_bad ; 202 std::printf (" %-26s blocks %s (%lld bad bytes) round trip %s (%lld differ)\n" ,name , 203 !check_bytes ?"not compared" : (byte_bad ?"*** WRONG ***" :"byte-exact" ),byte_bad , 204val_bad ?"*** WRONG ***" :"bit-exact" ,val_bad ); 205if (first_bad >=0 ) { 206// Say WHERE and WHAT, because "1 element in 524288" is the signature of a rounding boundary and not 207// of a wrong formula - and the two need completely different responses. 208const size_t blk = (size_t )first_bad /292 ,off = (size_t )first_bad %292 ; 209const char * where = off < 4 ?"d (the f32 scale)" : (off < 260 ?"qs (the quants)" :"bsums" ); 210 std::printf (" first bad byte: block %zu offset %zu = %s ref %02x got %02x\n" ,blk ,off , 211where ,r_blocks [(size_t )first_bad ],g_blocks [(size_t )first_bad ]); 212if (off >=4 && off < 260 ) { 213const int j = (int )off - 4 ; 214const float xv = x [(size_t ) (blk * 256 + j )]; 215float mx = 0.0f ; 216for (int k = 0 ;k < 256 ;++ k ) {const float ax = std::fabs (x [(size_t ) (blk * 256 + k )]);if (ax > std::fabs (mx ))mx = x [(size_t ) (blk * 256 + k )]; } 217const float isc = -127.0f /mx ; 218const float prod = isc * xv ; 219 std::printf (" element %d: x %.9g max %.9g iscale %.9g iscale*x %.9g (frac %.6f)\n" , 220j , (double )xv , (double )mx , (double )isc , (double )prod , (double ) (prod - std::floor (prod ))); 221 }else if (off < 4 ) { 222float rd ,gd ; 223 std::memcpy (& rd ,& r_blocks [(size_t ) (blk * 292 )],4 ); 224 std::memcpy (& gd ,& g_blocks [(size_t ) (blk * 292 )],4 ); 225 std::printf (" d: ref %.9g got %.9g\n" , (double )rd , (double )gd ); 226 } 227 } 228cudaFree (d_x ); 229cudaFree (d_b ); 230cudaFree (d_back ); 231return (int ) (byte_bad + val_bad ); 232} 233 234}// namespace 235 236int main (int argc ,char ** argv ) { 237bool selftest = false; 238for (int i = 1 ;i < argc ;++ i ) { 239if (std::string (argv [i ])== "--selftest" )selftest = true; 240else { std::fprintf (stderr ,"usage: quantize_act_parity [--selftest]\n" );return 2 ; } 241 } 242 std::mt19937 rng (7 ); 243 std::normal_distribution < float > gauss (0.0f ,1.0f ); 244int bad = 0 ; 245const long long N = 32 * 4096 ; 246 247// ---- ordinary activations 248 std::vector < float > a ((size_t )N ); 249for (auto & v :a )v = gauss (rng ); 250bad += run_case ("random normal" ,a , true); 251 252// ---- an activation with a very small dynamic range, where d32 is tiny and the fp16 rounding of d is a 253// LARGE relative change - this is where dividing by d16 instead of d32 would show up in the bytes 254 std::vector < float > b ((size_t )N ); 255for (auto & v :b )v = gauss (rng )* 1e-6f ; 256bad += run_case ("small magnitude (1e-6)" ,b , true); 257 258// ---- an all-zero block must produce a zeroed block, not a division by zero 259 std::vector < float > c ((size_t )N ,0.0f ); 260bad += run_case ("all zero" ,c , true); 261 262// ---- values that land exactly on .5 boundaries, where rint's half-to-even and a float division's 263// rounding both decide the result 264 std::vector < float > d ((size_t )N ); 265for (long long i = 0 ;i < N ;++ i ) { 266// construct so that x / d32 is very close to a .5 boundary: d32 = amax/127, so pick amax = 127.0 and 267// x = k + 0.5 for integer k 268d [(size_t )i ]= ((i %200 )- 100 )+ 0.5f ; 269 } 270d [0 ]= 127.0f ; 271bad += run_case ("exact .5 boundaries" ,d , true); 272 273// ---- a single extreme value in each block, so d32 is set by it and every other quant is small 274 std::vector < float > e ((size_t )N ,0.0f ); 275for (long long blk = 0 ;blk < N /32 ;++ blk )e [(size_t ) (blk * 32 )]= 1000.0f + (float ) (blk %7 ); 276bad += run_case ("one extreme per block" ,e , true); 277 278 std::printf ("\nquantize_q8_0: %d failures over 5 distributions x %lld elements\n" ,bad ,N ); 279 280// ============================== Q8_K ============================== 281// 282// The K-quants' activation: 2.89 GiB of dense weights - the attention projections and `ssm_out`. The 283// oracle here is GGML's C reference, and the traps are the two the source comments warn about. 284// ============================== the fp16 conversion itself ============================== 285// 286// REGRESSION CHECK FOR A BUG THAT SHIPPED. The private encoder in `quantize_act.cu` - and the identical 287// one in `shared_expert.cu` - tested `if (exp >= 31)` to catch an out-of-range exponent, which conflates 288// an f32 INF/NAN (raw exponent 255) with a FINITE value too large for fp16. Every finite overflow came 289// back as a NaN instead of saturating to inf. **Neither file's fixture could see it, because every value 290// in both is O(1)** - the Q8_0 case above scales by a max that is never near 65504. Round 198's 291// numpy-generated oracle caught it on the first run. 292// 293// The decoder in this same test had the matching defect in the other direction: it flushed fp16 294// SUBNORMALS to zero, so 0x0001 decoded as 0.0 instead of 2^-24. 295// 296// These are the exact values, asserted against what numpy's float16 says. They are here rather than 297// only in `qsa_parity` because this is the file that OWNS the Q8_0/Q8_K scales, and a scale that is NaN 298// poisons a whole block silently. 299 { 300struct F16Case {float in ;uint16_t want ;const char * what ; }; 301const F16Case cases []= { 302 {1e30f ,0x7C00u ,"1e30 -> +inf (finite overflow)" }, 303 {-1e30f ,0xFC00u ,"-1e30 -> -inf" }, 304 {65536.0f ,0x7C00u ,"65536 -> +inf (just past fp16 max)" }, 305// FLT_MAX is 3.4028235e38, so `1e45f` is not even a legal literal; 3e38 is the largest finite 306// f32 that still overflows fp16, which is what this case is for. 307 {3.0e38f ,0x7C00u ,"3e38 (near FLT_MAX) -> +inf" }, 308 {65504.0f ,0x7BFFu ,"65504 -> the largest finite fp16, NOT inf" }, 309// The boundary, checked against numpy: 65520.0 is the exact tie between 65504 and the 310// "next" fp16 (65536 = inf) and rounds to EVEN, i.e. to inf; 65519.996 is below it and stays 311// finite. The first version of this table asserted inf for 65519.996 - the FIXTURE was wrong and 312// the kernel was right, which is why the expected bits are quoted from numpy rather than reasoned. 313 {65519.996f ,0x7BFFu ,"65519.996 -> just below the boundary, still finite" }, 314 {65520.0f ,0x7C00u ,"65520.0 -> the exact tie, rounds to even = inf" }, 315 {65536.0f ,0x7C00u ,"65536 -> past fp16 max" }, 316 {5.9604645e-8f ,0x0001u ,"2^-24 -> the smallest subnormal, NOT zero" }, 317 {1.0f ,0x3C00u ,"1.0" }, 318 {0.0f ,0x0000u ,"0.0" }, 319 }; 320int fp16_bad = 0 ; 321for (const F16Case & c :cases ) { 322const uint16_t got = strata::kernels::f16_from_f32 (c .in ); 323if (got != c .want ) { 324 std::printf (" *** %s: want 0x%04X got 0x%04X\n" ,c .what ,c .want ,got ); 325++ fp16_bad ; 326 } 327 } 328// and the reverse direction on the pattern that was flushed 329const float sub = strata::kernels::f32_from_f16 (0x0001u ); 330const bool sub_ok = (sub == 5.9604645e-8f ); 331 std::printf (" %-44s %s (%d of %d wrong)\n" ,"f32->f16 overflow saturates, not NaN" , 332fp16_bad ?"*** NO ***" :"yes" ,fp16_bad , (int ) (sizeof cases /sizeof cases [0 ])); 333 std::printf (" %-44s %s (0x0001 -> %.9g, want 2^-24)\n" ,"f16->f32 keeps subnormals" , 334sub_ok ?"yes" :"*** NO ***" , (double )sub ); 335if (fp16_bad )bad += fp16_bad ; 336if (!sub_ok )++ bad ; 337 } 338 339 std::printf ("\nQ8_K (ggml block_q8_K: 292 bytes / 256 elements, {f32 d; i8 qs[256]; i16 bsums[16]})\n" ); 340int bad_k = 0 ; 341const long long NK = 256 * 2048 ; 342 343 std::vector < float > ka ((size_t )NK ); 344for (auto & v :ka )v = gauss (rng ); 345bad_k += run_case_k ("random normal" ,ka , true); 346 347 std::vector < float > kb ((size_t )NK ); 348for (auto & v :kb )v = gauss (rng )* 1e-7f ;// tiny: the scale is far from fp16-representable 349bad_k += run_case_k ("small magnitude (1e-7)" ,kb , true); 350 351 std::vector < float > kc ((size_t )NK ,0.0f ); 352bad_k += run_case_k ("all zero" ,kc , true); 353 354// EXACT TIES. `nearest_int` is round-half-to-EVEN (the 12582912.0f magic number); `roundf` is 355// half-AWAY-FROM-ZERO. They differ only when `iscale*x` is exactly k+0.5, which never happens on real 356// data - so this fixture MANUFACTURES it: with max = 127.0 and iscale = -1.0, x = -(k+0.5) gives exactly 357// k+0.5, and the two rules disagree for every EVEN k. 358 std::vector < float > kd ((size_t )NK ,0.0f ); 359for (long long blk = 0 ;blk < NK /256 ;++ blk ) 360for (int j = 0 ;j < 256 ;++ j )kd [(size_t ) (blk * 256 + j )]= - (float ) ((j %100 )+ 0.5f ); 361for (long long blk = 0 ;blk < NK /256 ;++ blk )kd [(size_t ) (blk * 256 )]= 127.0f ;// fixes max = 127 362bad_k += run_case_k ("exact .5 ties (half-to-even)" ,kd , true); 363 { 364// assert the tie fixture can SEE the difference before trusting the comparison above 365 std::vector < uint8_t > rb ; 366 std::vector < float > rback ; 367reference_q8_K (kd .data (),NK ,rb ,rback ,-127.0f ); 368long long ties = 0 ,differ = 0 ; 369for (long long i = 0 ;i < NK ;++ i ) { 370const float t = - kd [(size_t )i ]; 371if (t - std::floor (t )== 0.5f ) { 372++ ties ; 373const int even = nearest_int_host (t ); 374const int away = (int ) std::round (t ); 375if (even != away )++ differ ; 376 } 377 } 378 std::printf (" %-26s %lld ties, %lld where half-to-even != half-away (%.1f%%)\n" , 379" tie fixture discriminates" ,ties ,differ ,ties ?100.0 * (double )differ /ties :0.0 ); 380if (differ < ties /3 ) { std::printf (" *** the tie fixture cannot see the rounding rule ***\n" );++ bad_k ; } 381 } 382 383// THE -127 TRAP. ggml uses `-127/max`; the `-128` version sits COMMENTED OUT in the source, and using it 384// is a 0.79% scaling error that leaves a working-looking activation. Computed with the rival constant 385// and required to differ. 386 { 387 std::vector < uint8_t > r127 ,r128 ; 388 std::vector < float > b127 ,b128 ; 389reference_q8_K (ka .data (),NK ,r127 ,b127 ,-127.0f ); 390reference_q8_K (ka .data (),NK ,r128 ,b128 ,-128.0f ); 391double d = 0 ,m = 0 ; 392long long qdiffer = 0 ; 393for (long long i = 0 ;i < NK ;++ i ) {d += std::fabs (b127 [(size_t )i ]- b128 [(size_t )i ]);m += std::fabs (b127 [(size_t )i ]); } 394for (size_t i = 4 ;i < r127 .size ();++ i )if (r127 [i ]!= r128 [i ])++ qdiffer ; 395// The expected size of the effect is exactly (1 - 127/128) = 0.78% of the magnitude, so the floor is 396// 0.5% - below what the rival constant must produce and far above any noise. The byte count is the 397// blunter evidence and is reported alongside it: 172,017 bytes differ, i.e. a third of the quants. 398 std::printf (" %-26s %-4s (%.3f%% of magnitude, %lld bytes differ)\n" ,"-127 vs -128 scale observable" , 399d /m > 0.005 ?"yes" :"*** NO ***" ,100.0 * d /m ,qdiffer ); 400if (!(d /m > 0.005 ))++ bad_k ; 401 } 402 403 std::printf ("\nquantize_act: %d failures over 5 Q8_0 + 4 Q8_K distributions\n" ,bad + bad_k ); 404if (bad + bad_k )return 1 ; 405if (selftest ) std::printf ("quantize_act_parity OK\n" ); 406return 0 ; 407}