code wiki / _hdl_build / nx_intfp_attention_gradcheck_gate.nx

nx_intfp_attention_gradcheck_gate.nx

buildroot/runtime/_hdl_build/nx_intfp_attention_gradcheck_gate.nx

8424 B117 linesdepth 2pulls 2 transitivereach 0 importersview sourcekind gate/prooftopic intfp
docsdependenciesstructsconstsfunctions

about

nx_intfp_attention_gradcheck_gate.nx -- THE COMPOSITION PROOF: a full single-head CAUSAL self-attention block (Q,K,V = X@W ; scores = scale*QK^T ; A = softmax(scores) causal ; O = A@V ; L = sum(O^2)) with the COMPLETE backward, done ENTIRELY in Q16 integer, gradchecked (integer finite-diff) on all three projection matrices Wq/Wk/Wv. Wq/Wk gradients flow through the softmax Jacobian AND two matmul-transpose backprops -- if THESE pass, the integer ops compose correctly and the full transformer tape is assemblable. No float anywhere. scale = 1/sqrt(d); d=4 -> scale=0.5 exactly (Q16 32768). license_tier: ORIGINAL

dependencies 1 imports · 0 importers

nx_syscalls.nx nx_intfp_attention_gradcheck_g

imports: nx_syscalls.nx

imported by: nobody (leaf or entry point)

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

main w sys_write sys_mmap attn_fwd fp_exp_q16 attn_bwd sys_mmap ↻ wn sys_write ↻ sys_mmap ↻ gcheck iabs attn_fwd ↻ w ↻ wn ↻

structs

none

consts

13const S: i64 = 1048576 // Q20 -- the BACKWARD needs more than Q16 for small gradients through the softmax Jacobian
14const T: i64 = 3
15const DM: i64 = 4
16const SCALE: i64 = 524288 // 1/sqrt(4) = 0.5 in Q20

functions

9func w(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 2: gcheckmain calls 1: sys_write
10func wn(v: i64) -> i64 { if v==0 { sys_write(1,"0" as *u8,1); return 0 } var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} let t: *u8=sys_mmap(24); var k: i64=0; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} let o: *u8=sys_mmap(24); var q: i64=k-1; var i: i64=0; while q>=0{o[i]=t[q];i=i+1;q=q-1} sys_write(1,o,i); return 0 }
called by 2: gcheckmain calls 2: sys_writesys_mmap
11func iabs(v: i64) -> i64 { if v<0 { return 0-v } return v }
called by 1: gcheck
19func fp_exp_q16(xq: i64) -> i64
called by 1: attn_fwd
32func attn_fwd(X: *i64, Wq: *i64, Wk: *i64, Wv: *i64, Q: *i64, K: *i64, V: *i64, A: *i64, O: *i64) -> i64
called by 2: gcheckmain calls 1: fp_exp_q16
53func attn_bwd(X: *i64, Q: *i64, K: *i64, V: *i64, A: *i64, O: *i64, dWq: *i64, dWk: *i64, dWv: *i64) -> i64
called by 1: main calls 1: sys_mmap
75func gcheck(name: *u8, X: *i64, Wq: *i64, Wk: *i64, Wv: *i64, Wtarget: *i64, dW: *i64, Q: *i64, K: *i64, V: *i64, A: *i64, O: *i64) -> i64
called by 1: main calls 4: iabsattn_fwdwwn
93func main() -> i64