nx_q8_0_dot.nx source
↩ module page · 69 lines · 2475 B
1// nx_q8_0_dot.nx -- sovereign Q8_0 dequant + fused integer dot, verified on a REAL Z-Image DiT block.
2//
3// Q8_0 (the DiT quant, simplest): block = 34 bytes = f16 scale d (2B) + 32 signed int8 quants. value = d*q.
4// Integer path: d_q24 = f16->Q24; dot += (d_q24 * q_int8) * col_q10 -> Q34. (col_q10 = col*1024.)
5// KAT: real cap_embedder.0.weight block0 (Python ggml oracle) dot with col[i]=(i%7)+1 = 6.159073.
6// dot_q34>>24 = dot in Q10 ~= 6.159073*1024 ~= 6307.
7// license_tier: ORIGINAL
8import "nx_syscalls.nx"
9import "nx_tier.nx"
10import "nx_le.nx"
11import "nx_strconv.nx"
12import "nx_gguf_load.nx"
13
14func q8_hexval(c: i64) -> i64 {
15 if c >= 0x30 { if c <= 0x39 { return c - 0x30 } }
16 return c - 0x61 + 10
17}
18
19// Q8_0 fused dot: buf holds n_blocks 34-byte blocks at block_off; col_q10 [n_blocks*32].
20func nx_q8_0_dot(buf: *u8, block_off: i64, n_blocks: i64, col_q10: *i64) -> i64 {
21 var dot: i64 = 0
22 var blk: i64 = 0
23 while blk < n_blocks {
24 let bo: i64 = block_off + blk * 34
25 let d_q24: i64 = _gguf_f16_to_q24(nx_le_read_u16(buf, bo))
26 var i: i64 = 0
27 while i < 32 {
28 var q: i64 = nx_le_read_u8(buf, bo + 2 + i)
29 if q >= 128 { q = q - 256 } // signed int8
30 dot = dot + (d_q24 * q) * col_q10[blk * 32 + i]
31 i = i + 1
32 }
33 blk = blk + 1
34 }
35 return dot
36}
37
38func main() -> i64 {
39 let hex: *u8 = "0c12fd555574007f7f1a0b08252a5f35224f4a35157f5f5f1a3afd7f5f036a2a4f4f" as *u8
40 let buf: *u8 = sys_mmap(64)
41 var i: i64 = 0
42 while i < 34 {
43 let hi: i64 = q8_hexval(hex[i * 2])
44 let lo: i64 = q8_hexval(hex[i * 2 + 1])
45 nx_le_write_u8(buf, i, hi * 16 + lo)
46 i = i + 1
47 }
48 let col: *i64 = sys_mmap(32 * 8) as *i64
49 i = 0
50 while i < 32 { col[i] = ((i - (i / 7) * 7) + 1) * 1024; i = i + 1 }
51
52 let dot_q34: i64 = nx_q8_0_dot(buf, 0, 1, col)
53 let dot_q10: i64 = dot_q34 >> 24
54
55 let ofd: i64 = sys_openat_wr("/tmp/q8_0_dot.txt" as *u8, 0x1a4)
56 if ofd >= 0 {
57 let dec: *u8 = sys_mmap(32)
58 let nd: i64 = nx_strconv_format_i64(dot_q10, dec)
59 sys_write(ofd, "dot_q10=" as *u8, 8)
60 sys_write(ofd, dec, nd)
61 sys_write(ofd, "\n" as *u8, 1)
62 sys_close(ofd)
63 }
64 // golden 6.159073*1024 = 6306.9 ; allow the f16->Q24 + >>24 truncation
65 let diff: i64 = dot_q10 - 6307
66 if diff < 0 { if diff < -15 { return 10 } }
67 if diff > 15 { return 10 }
68 return 0
69}