nx_nofloat_w12a12_gate.nx
buildroot/runtime/nx_nofloat_w12a12_gate.nx
about
nx_nofloat_w12a12_gate.nx -- the referee for the W12A12 decode matmul (search R0l attribution, 2026-09-16).
SUBJECT: mm_pool_i8 in nx_nofloat_llm (the pooled SIMD matmul every decode projection goes through), driven
IN-PROCESS on synthetic data whose shape is the real one (in_dim 8960 = the Qwen2.5-1.5B ffn_down, the widest
projection, so the chunk bound is exercised at its limit) with activation OUTLIER channels planted, because a
per-tensor scale is exactly the quantiser an outlier defeats.
TEETH: the chunk bound is arithmetic (NF_CHUNK_K/8 * QMAX * QMAX <= i32 max) and 2048 is the planted bound-
breaker; the kernel's max relative error against the exact i64 dot is BELOW the old 8-bit per-tensor quantiser's
error on the same data (the old quantiser is re-implemented here as the CONTROL, so the improvement is measured,
never asserted); the kernel is exact on a row the quantisers cannot lose (all values on the grid); and the
chunked fold reproduces the unchunked sum on a short row (K < NF_CHUNK_K, one chunk).
license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
dependencies 3 imports · 0 importers
imports: nx_syscalls.nxnx_nofloat_llm.nxnx_gate_verdict.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 17 | const WG_IN: i64 = 8960 |
| 18 | const WG_OUT: i64 = 64 |
| 19 | const WG_SHORT_IN: i64 = 896 |
| 20 | const WG_SEED: i64 = 20260916 |
| 21 | const WG_LCG_A: i64 = 6364136223846793005 |
| 22 | const WG_LCG_C: i64 = 1442695040888963407 |
| 23 | const WG_MASK63: i64 = 0x7FFFFFFFFFFFFFFF |
| 24 | const WG_X_BODY: i64 = 65536 // typical Q16 activation magnitude (about 1.0) |
| 25 | const WG_X_OUTLIER: i64 = 4194304 // 64x the body: the outlier channel every Qwen-class model carries |
| 26 | const WG_OUTLIER_EVERY: i64 = 512 |
| 27 | const WG_W_MAG: i64 = 4096 |
| 28 | const WG_SHIFT: i64 = 16 |
| 29 | const WG_OLD_QMAX: i64 = 127 |
| 30 | const WG_PERMIL: i64 = 1000 |
| 31 | const WG_BREAKER_CHUNK: i64 = 2048 |
| 34 | const WG_GRID_SX: i64 = 16 |
| 35 | const WG_GRID_SPAN: i64 = 8191 // 2*QMAX+1 values, j in [-4095, 4095] |
| 36 | const WG_GRID_WMAX: i64 = 4095 |
| 37 | const WG_TAIL_IN: i64 = 912 // R0q: 14 x 64 + 16, a width whose last madd goes through the tail loop |
| 38 | const WG_TIMING_REPS: i64 = 40 // R0q: repeats per task for the printed throughput values |
| 39 | const WG_OUT_BIG: i64 = 1536 // R0q: the ffn down projection's output rows (27.5 MB of i16 weights per call) |
| 40 | const WG_BIG_REPS: i64 = 6 // R0q: repeats on the big shape |
| 41 | const WG_BYTE_SPAN: i64 = 256 // one byte of pseudo-random weight fill |
| 42 | const WG_BATCH_M: i64 = 32 // R0r: activation rows per batched call (the engine's block) |
| 43 | const WG_TAIL_M: i64 = 5 // R0r: an odd row count on the tail-width fixture |
| 44 | const WG_DOT_N: i64 = 1024 // R0r-b: one full chunk of lanes for the builtin KAT |
| 45 | const WG_DOT_HALF: i64 = 512 // R0r-b: centres the ramp so half the lanes are negative |
| 46 | const WG_DOT_PLANT: i64 = 777 // R0r-b: the lane the neg-control raises by one |
| 47 | const WG_DOT_REAL_N: i64 = 512 // R0r-b: lanes of real weight rows in the KAT (64 products per int32 lane, a 2^30 bound) |
| 48 | const WG_I16_MOD: i64 = 65536 |
| 49 | const WG_I16_HALF: i64 = 32768 |
| 50 | const WG_BYTE: i64 = 256 |
functions
| 52 | func wg_put_i16(p: *u8, i: i64, v: i64) -> i64 { var u: i64 = v; if u < 0 { u = u + WG_I16_MOD } p[i*2] = (u % WG_BYTE) as u8; p[i*2+1] = ((u / WG_BYTE) % WG_BYTE) as u8; return 0 } called by 1: main |
| 53 | func wg_get_i16(p: *u8, i: i64) -> i64 { var u: i64 = (p[i*2] as i64) + (p[i*2+1] as i64) * WG_BYTE; if u >= WG_I16_HALF { u = u - WG_I16_MOD } return u } |
| 54 | func wg_dot_ref(a: *u8, b: *u8, n: i64) -> i64 { var s: i64 = 0; var i: i64 = 0; while i < n { s = s + wg_get_i16(a, i) * wg_get_i16(b, i); i = i + 1 } return s } |
| 56 | func wg_lcg(st: *i64) -> i64 { st[0] = (st[0]*WG_LCG_A + WG_LCG_C) & WG_MASK63; return st[0] } |
| 57 | func wg_abs(v: i64) -> i64 { if v < 0 { return 0 - v } return v } |
| 58 | func wg_fill_x(x: *i64, n: i64, st: *i64) -> i64 |
| 69 | func wg_fill_w(w: *i64, n: i64, st: *i64) -> i64 { var k: i64 = 0; while k < n { let r: i64 = wg_lcg(st) % (2*WG_W_MAG+1); w[k] = r - WG_W_MAG; k = k + 1 } return 0 } |
| 71 | func wg_exact(x: *i64, w: *i64, ind: i64, out: i64, dst: *i64) -> i64 called by 1: main |
| 77 | func wg_old_path(x: *i64, w: *i64, ind: i64, out: i64, dst: *i64) -> i64 |
| 95 | func wg_max_err_permil(ref: *i64, got: *i64, n: i64, scored: *i64) -> i64 |
| 109 | func wg_quant_weights(w: *i64, ind: i64, out: i64, wi8: *u8, sw: *i64) -> i64 { return nf_quant_w_i8(w, wi8, sw, out, ind) } |
| 111 | func main(argc: i64, argv: *i64) -> i64 |