nx_q4k_q24_gate.nx source
↩ module page · 28 lines · 1454 B
1// nx_q4k_q24_gate.nx -- prove the exact Q24 super-scale decode on the REAL Q4_K super-scales.
2//
3// Guards Fix A+B of the Q4_K dequant tech-debt: `_gguf_f16_to_q24` must decode the real blk.0.attn_q
4// super-scales (d_raw=0x062e, dmin_raw=0x1078) with ZERO loss and reproduce the ggml-correct element0.
5// Golden (from the ggml forensic oracle): d->1582, dmin->9152, element0 = d*sc0*q0 - dmin*m0
6// = 1582*33*7 - 9152*37 = 26818 (Q24) = +0.0015985. Q10 gave 0 (underflow), Q14 gave -65 (sign flip).
7// license_tier: ORIGINAL
8import "nx_syscalls.nx"
9import "nx_tier.nx"
10import "nx_le.nx"
11import "nx_gguf_load.nx"
12
13func main() -> i64 {
14 if _gguf_f16_to_q24(0x062e) != 1582 { return 10 } // real super-scale d, exact
15 if _gguf_f16_to_q24(0x1078) != 9152 { return 11 } // real super-min dmin, exact
16 if _gguf_f16_to_q24(0x3C00) != 16777216 { return 12 } // 1.0 -> 2^24
17 if _gguf_f16_to_q24(0x3800) != 8388608 { return 13 } // 0.5 -> 2^23
18 if _gguf_f16_to_q24(0x862e) != (0 - 1582) { return 14 } // -d (sign preserved)
19 if _gguf_f16_to_q24(0x0000) != 0 { return 15 } // +0
20
21 // reproduce the ggml-correct element0 of the real block (sc0=33, q0=7, m0=37), in Q24
22 let d: i64 = _gguf_f16_to_q24(0x062e)
23 let dmin: i64 = _gguf_f16_to_q24(0x1078)
24 let e0: i64 = d * 33 * 7 - dmin * 37
25 if e0 != 26818 { return 20 } // = +0.0015985 * 2^24
26
27 return 0
28}