nx_gen_linear_verify.nx
buildroot/runtime/nx_gen_linear_verify.nx
about
nx_gen_linear_verify.nx -- SOVEREIGN linear projection (matmul), verified vs a reference.
y == x @ W^T
This is the core compute of every diffusion and transformer architecture: qkv projection,
attention out-projection, every FFN matrix, every text-encoder layer. It is also where the
entire sovereignty gap lives -- correctness here was proven cheaply; THROUGHPUT is the wall.
Usage: nx_gen_linear_verify <model_id> <x_name> <w_name> <y_name> [rows] [pass_idx] [accum] [nworkers]
Names are ABSOLUTE fixture names -- weights carry their own checkpoint name
("model.diffusion_model.layers.0.attention.qkv.weight"), so nothing here is tied to one
architecture or one block. Shapes are read from the manifest, never assumed.
LAYOUT (ggml): ggml_mul_mat(w, x) with w[ne0=in, ne1=out] and x[ne0=in, ne1=tokens] gives
y[ne0=out, ne1=tokens]. ne[0] is the contiguous dim, so
y[t][o] = sum_i x[t*in + i] * w[o*in + i]
TWO REFERENCE TIERS (measured 2026-08-06, not a stylistic choice):
The oracle's mul_mat on a Q8_0 weight does NOT compute dequant(W) @ x -- it quantizes the
ACTIVATIONS too and runs a blocked integer dot. An exact f64 reference deviates from the
oracle by ~0.52% median / 43% max on the Z-Image qkv projection. So grade against
- `<y>.exact` (f64 reference) -> "is our arithmetic right", tight band
- `<y>` (the oracle itself) -> only at a loose band; it is the LOWER-precision side
* A TOLERANCE IS A CLAIM ABOUT THE REFERENCE'S PRECISION, NOT THE IMPLEMENTATION'S.
ACCUM MODES -- every one stays A/B-able, because a speed or accuracy claim that cannot be
re-measured against its predecessor is an opinion:
0 naive sequential f32
1 Kahan compensated
2 __f32x8_dot (8-wide, but horizontally reduces EVERY 8 elements)
3 __f32x8_dot + Kahan on the chunk sum
4 __f32x8_fma (fused 8-wide multiply-add into a 32-byte accumulator, hsum ONCE)
5 __f32x8_fma + multicore (forked workers over an out_dim band, shared result buffer)
6 __f32x8_fma x4 independent accumulators (breaks the FMA latency chain)
7 mode 6 + multicore
8 Q8_0 dequant-dot (int8 weight x f32 activation via __f32_i8dot32a) -- reads the RAW
quantized weight, 3.77x less memory traffic than the f32 weight
9 mode 8 + multicore
10 Q8_0 with per-block f16 scales pre-decoded once (software f16 decode out of the hot loop)
dependencies 9 imports · 0 importers
imports: nx_syscalls.nxnx_le.nxnx_f32.nxnx_f32_div.nxnx_f32_cvt.nxnx_f16.nxnx_strconv.nxnx_genfix.nxnx_genver.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
| none |
functions
| 59 | func zl_strlen(s: *u8) -> i64 called by 1: main |
| 71 | func zl_band(mode: i64, x: *u8, w: *u8, res: *i64, scales: *i64, |
| 256 | func main(argc: i64, argv: *i64) -> i64 |