code wiki / (root) / nx_reader_span_gate.nx

nx_reader_span_gate.nx

buildroot/runtime/nx_reader_span_gate.nx

14481 B291 linesdepth 3pulls 3 transitivereach 0 importersview sourcekind gate/prooftopic reader
docsdependenciesstructsconstsfunctions

about

nx_reader_span_gate.nx -- RUNG 2 of the NEURAL PASSAGE READER arc (deep-research R2): the READER-SHAPED MODEL trains END-TO-END on the verified sovereign no-float stack. Composes nx_nofloat_autograd's PROVEN transformer block (the exact blk_fwd shape from the green blockfloat gates: embed -> rmsnorm -> Q/K/V -> RoPE -> QK^T -> causal softmax -> A.V -> Wo -> residual -> rmsnorm -> SwiGLU FFN -> residual) + a SPAN HEAD (matmul_nt of a trained probe against the contextual states -> [1,L] position logits) + fused softmax-CE. TASK (question-conditioned span pointing = the reader mechanism): passage = 3 (key,value) pairs in random order; question = one key token; GOLD SPAN = the position where the queried key occurs in the passage. To point there the model must ALIGN the question to the context (attention); a QUESTION-BLIND ablation (same params/budget, attention removed -> h_i sees only its own token, exactly the isolated-candidate reader that plateaued at 251/148/101) can at best fire on one fixed key = ~333 permille -> liar-kill. T1 reader (attention block) span accuracy > 700 permille held-out T2 question-blind ablation < 450 permille AND reader beats it by > 250 (liar-kill) T3 descent: one nfa_backward+nfa_sgd step on fresh weights DECREASES that example's loss (real gradients) T4 deterministic re-eval (integer stack -> bit-identical) expect_exit: 0 license_tier: ORIGINAL Sovereign: nx_nofloat_autograd + nx_syscalls.

dependencies 2 imports · 0 importers

nx_nofloat_autograd.nx nx_syscalls.nx nx_reader_span_gate.nx

imports: nx_nofloat_autograd.nxnx_syscalls.nx

imported by: nobody (leaf or entry point)

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

main rs_puts sys_write sys_mmap rs_perms rs_init rs_gen rs_lcg rs_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

24const RS_V: i64 = 7 // tokens: keys 0..2, values 3..5, SEP 6
25const RS_E: i64 = 8 // model dim (even, for RoPE)
26const RS_F: i64 = 16 // FFN dim
27const RS_L: i64 = 8 // sequence: [q, SEP, k,v, k,v, k,v]
28const RS_SCALE: i64 = 23170 // 1/sqrt(E) in Q16
30const OFF_EMB: i64 = 0 // [V,E] 56
31const OFF_WQ: i64 = 56 // [E,E] 64
32const OFF_WK: i64 = 120
33const OFF_WV: i64 = 184
34const OFF_WO: i64 = 248
35const OFF_WG: i64 = 312 // [E,F] 128
36const OFF_WU: i64 = 440 // [E,F] 128
37const OFF_WD: i64 = 568 // [F,E] 128
38const OFF_U: i64 = 696 // [1,E] 8 (span probe)
39const RS_NP: i64 = 704

functions

20func rs_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
called by 3: rs_ckrs_trainmain calls 1: sys_write
21func rs_pn(v: i64) -> i64 { let b: *u8=sys_mmap(28); var x: i64=v; if x<0{b[0]=45;sys_write(1,b,1);x=0-x} if x==0{b[0]=48;sys_write(1,b,1);return 0} var d: i64=0; var y: i64=x; while y>0{d=d+1;y=y/10} var i: i64=d-1; y=x; while i>=0{b[i]=(48+(y%10)) as u8;y=y/10;i=i-1} sys_write(1,b,d); return 0 }
called by 2: rs_trainmain calls 2: sys_mmapsys_write
22func rs_ck(name: *u8, c: i64) -> i64 { if c==1 { rs_puts(" PASS " as *u8) } else { rs_puts(" FAIL " as *u8) } rs_puts(name); rs_puts("\n" as *u8); return c }
called by 1: main calls 1: rs_puts
41func rs_lcg(x: i64) -> i64 { return ((x*1103515245+12345) & 2147483647) }
called by 1: rs_gen
44func rs_perms(pm: *i64) -> i64
called by 1: main
51func rs_gen(s: i64, pm: *i64, ids: *i64, tgt: *i64) -> i64
called by 3: rs_trainrs_evalmain calls 1: rs_lcg
71func rs_init(W: *i64) -> i64
called by 1: main
96func rs_fwd(tape: *i64, vals: *i64, st: *i64, W: *i64, ids: *i64, tgt: *i64, useattn: i64, out: *i64) -> i64
150func rs_grab(tape: *i64, grads: *i64, out: *i64, G: *i64) -> i64
called by 2: rs_trainmain
174func rs_train(tape: *i64, vals: *i64, grads: *i64, st: *i64, W: *i64, pm: *i64, useattn: i64, EP: i64, lr: i64, verbose: i64) -> i64
204func rs_eval(tape: *i64, vals: *i64, st: *i64, W: *i64, pm: *i64, useattn: i64) -> i64
called by 1: main calls 4: sys_mmaprs_genrs_fwdnfa_val
224func main() -> i64