code wiki / _hdl_build / nx_reader_attn_gate.nx

nx_reader_attn_gate.nx

buildroot/runtime/_hdl_build/nx_reader_attn_gate.nx

12197 B250 linesdepth 3pulls 3 transitivereach 0 importersview sourcekind gate/prooftopic reader
docsdependenciesstructsconstsfunctions

about

nx_reader_attn_gate.nx -- RUNG 1 of the NEURAL PASSAGE READER arc (deep-research R2). Proves DOT-PRODUCT ATTENTION delivers context-ALIGNMENT -- the capability the candidate-ranking reader lacks (its 251/148/101 vs oracle 727/509/317 gap = it scores candidates in ISOLATION). Task = a LOOKUP: a passage of (key,value) slots + a query key; the answer is the value paired with the queried key. To answer you MUST attend to the matching key's position and read ITS value. A NO-ATTENTION baseline (query-blind mean of the values) provably CANNOT -> liar-kill. All integer Q16 => deterministic, $0 on this laptop. Self-contained (nano_mlp_lm style); the production reader composes the verified nfa_* stack (nx_nofloat_autograd, gradcheck-GREEN). T1 attention P(correct) rises > 0.7 (learns the query->key->value lookup) T2 no-attention baseline stays ~ 1/V chance = attention wins (liar-kill) T3 GRADCHECK: analytic grad == finite-difference on a Kemb weight T4 DETERMINISTIC: re-run bit-identical expect_exit: 0 license_tier: ORIGINAL Sovereign: nx_syscalls.

dependencies 2 imports · 0 importers

nx_syscalls.nx nx_g_puts_lib.nx nx_reader_attn_gate.nx

imports: nx_syscalls.nxnx_g_puts_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 gen_example lcg fwd_attn fxmul smax exp_fx fxmul ↻ fxmul ↻ g_pn sys_mmap ↻ sys_write ↻ ck g_puts ↻ smax ↻

structs

none

consts

19const Q: i64 = 16
20const ONE: i64 = 65536
21const V: i64 = 4 // vocab (keys and values share it)
22const E: i64 = 4 // embedding dim
23const N: i64 = 3 // passage slots
24const LOG2E: i64 = 94548
25const C1: i64 = 45426
26const C2: i64 = 15743

functions

16func g_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 1: main calls 2: sys_mmapsys_write
17func ck(name: *u8, c: i64) -> i64 { if c==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) } g_puts(name); g_puts("\n" as *u8); return c }
called by 1: main calls 1: g_puts
27func fxmul(a: i64, b: i64) -> i64 { return (a*b)>>Q }
called by 3: exp_fxfwd_attnmain
28func exp_fx(x: i64) -> i64 { var xx: i64=x; if xx>0 { xx=0 } let yabs: i64=fxmul(0-xx,LOG2E); let nabs: i64=yabs>>Q; let fabs: i64=yabs-(nabs<<Q); let f2: i64=fxmul(fabs,fabs); let p: i64=ONE+fxmul(fabs,C1)+fxmul(f2,C2); let inv: i64=(ONE*ONE)/p; if nabs>=31 { return 0 } return inv>>nabs }
called by 1: smax calls 1: fxmul
30func smax(logits: *i64, probs: *i64, n: i64, escr: *i64) -> i64 { var mx: i64=logits[0]; var i: i64=1; while i<n { if logits[i]>mx { mx=logits[i] } i=i+1 } var sum: i64=0; i=0; while i<n { escr[i]=exp_fx(logits[i]-mx); sum=sum+escr[i]; i=i+1 } if sum<1 { sum=1 } i=0; while i<n { probs[i]=(escr[i]*ONE)/sum; i=i+1 } return 0 }
called by 2: fwd_attnmain calls 1: exp_fx
32func lcg(x: i64) -> i64 { return ((x*1103515245+12345) & 2147483647) }
called by 1: gen_example
34func gen_example(s: i64, keyid: *i64, valid: *i64, qout: *i64, goldout: *i64) -> i64
called by 1: main calls 1: lcg
49func fwd_attn(Kemb: *i64, Vemb: *i64, Wo: *i64, keyid: *i64, valid: *i64, q: i64, att: *i64, ctx: *i64, scr: *i64, probs: *i64, lg: *i64, es: *i64) -> i64
called by 1: main calls 2: fxmulsmax
79func main() -> i64