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