code wiki / _hdl_build / nx_nofloat_quant_gate.nx

nx_nofloat_quant_gate.nx

buildroot/runtime/_hdl_build/nx_nofloat_quant_gate.nx

10643 B163 linesdepth 3pulls 4 transitivereach 0 importersview sourcekind gate/prooftopic nofloat
docsdependenciesstructsconstsfunctions

about

nx_nofloat_quant_gate.nx -- CAP-NF-QUANT: post-training QUANTIZATION for efficient sovereign inference, grounded in the BitNet research (nfs_bitnet158.raw / nfs_intquant.raw: ternary {-1,0,1} and integer-only inference match full precision). Train the near-optimal 3-category grammar LM (Q16), then quantize its weights to INT8 (per-tensor symmetric, scale=maxabs/127) and TERNARY {-1,0,1} (scale=mean|W|), and measure how much held-out quality (CE) survives. T1 INT8 preserves quality: int8 held-out CE ~= full-precision CE (within ~0.15 nats). T2 INT8 stays near-OPTIMAL: int8 CE << uniform ln(8)=2079 (efficient 8-bit inference works). T3 TERNARY carries the signal: ternary CE < uniform (1.58-bit weights still model the grammar; honest: ternary loses more than int8 on a tiny model -- the BitNet "matches at scale" claim is a SCALE result). Pure integer Q16. Sovereign: nx_nofloat_autograd + nx_syscalls. expect_exit: 0

dependencies 3 imports · 0 importers

nx_nofloat_autograd.nx nx_syscalls.nx nx_gate_emit_lib.nx nx_nofloat_quant_gate.nx

imports: nx_nofloat_autograd.nxnx_syscalls.nxnx_gate_emit_lib.nx

imported by: nobody (leaf or entry point)

call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown

main g_puts sys_write sys_mmap dini do_train make_stream lcg clm_fwd nfa_leaf nfa_new nfa_embed nfa_new ↻ nfa_rmsnorm_rows nfa_new ↻ nfa_qmul nfa_isqrt nfa_matmul nfa_new ↻ nfa_rope nfa_new ↻ nfa_fxexp nfa_cosf nfa_reduce2pi nfa_cosq nfa_qmul ↻ nfa_sinf nfa_reduce2pi ↻ nfa_sinq nfa_qmul ↻ nfa_qmul ↻ nfa_matmul_nt nfa_new ↻ nfa_cmul nfa_new ↻ nfa_qmul ↻ nfa_softmax_rows nfa_new ↻ nfa_fxexp ↻ nfa_vadd

structs

none

consts

14const Q16: i64 = 65536
15const UNIFORM_MNAT: i64 = 2079 // ln(8)
16const FLOOR_MNAT: i64 = 964 // avg(ln2,ln3,ln3)

functions

19func dini(a: *i64, n: i64, sd: i64) -> i64 { var i: i64=0; while i<n { a[i]=(((i*7+sd*13+1)%11)-5)*13107; i=i+1 } return 0 }
called by 1: main
20func lcg(st: *i64) -> i64 { st[0]=(st[0]*1103515245 + 12345) & 2147483647; return (st[0] >> 15) }
called by 1: make_stream
21func iabs(x: i64) -> i64 { if x<0 { return 0-x } return x }
22func qround(src: i64, scale: i64) -> i64 { if scale<=0 { return 0 } if src>=0 { return (src + scale/2)/scale } return (src - scale/2)/scale }
24func quant_int8(src: *i64, dst: *i64, n: i64) -> i64
called by 1: main calls 2: iabsqround
32func quant_ternary(src: *i64, dst: *i64, n: i64) -> i64
called by 2: do_train_qatmain calls 2: iabsqround
38func make_stream(S: *i64, tgt: *i64, P: i64, st: *i64) -> i64
called by 3: do_traindo_train_qateval_ce calls 1: lcg
44func clm_fwd(tape: *i64, vals: *i64, st: *i64, W: *i64, ids: *i64, tgt: *i64, T: i64, dm: i64, V: i64, scale: i64, leaves: *i64) -> i64
72func step_all(tape: *i64, grads: *i64, W: *i64, WN: *i64, leaves: *i64, lr: i64, clip: i64, gb: *i64) -> i64
77func do_train(tape: *i64, vals: *i64, grads: *i64, st: *i64, W: *i64, WN: *i64, S: *i64, tgt: *i64, P: i64, dm: i64, V: i64, scale: i64, leaves: *i64, gb: *i64, steps: i64, sdat: *i64) -> i64
85func do_train_qat(tape: *i64, vals: *i64, grads: *i64, st: *i64, W2: *i64, Wt: *i64, WN: *i64, S: *i64, tgt: *i64, P: i64, dm: i64, V: i64, scale: i64, leaves: *i64, gb: *i64, steps: i64, sdat: *i64) -> i64
97func eval_ce(tape: *i64, vals: *i64, st: *i64, W: *i64, S: *i64, tgt: *i64, P: i64, dm: i64, V: i64, scale: i64, leaves: *i64, N: i64, sdat: *i64) -> i64
called by 1: main calls 3: make_streamclm_fwdnfa_val
104func main() -> i64