code wiki / _hdl_build / nx_nofloat_attn_gate.nx

nx_nofloat_attn_gate.nx

buildroot/runtime/_hdl_build/nx_nofloat_attn_gate.nx

13804 B244 linesdepth 3pulls 4 transitivereach 0 importersview sourcekind gate/prooftopic nofloat
docsdependenciesstructsconstsfunctions

about

nx_nofloat_attn_gate.nx -- HARD-EVIDENCE gate for the ATTENTION-CORE backward (CAP-NF-ATTN-CORE): a single-head CAUSAL self-attention block backprops end-to-end in PURE INTEGER Q16. This is the heart of CAP-NF-TRAIN-ATTN. New ops proven: matmul (C=A.B), matmul_nt (S=Q.K^T), cmul (1/sqrt(d) scale), causal softmax_rows. A1 matmul gradcheck : C=A.B, loss=mse(C,t); tape grad dL/dA == central finite difference. A2 matmul_nt gradcheck : S=A.B^T (the Q.K^T form); tape grad == finite difference. A3 attention-core gradcheck: full path X->{Q,K,V}=X.W -> S=Q.K^T -> scale -> CAUSAL softmax rows -> O=A.V -> mse; gradcheck dL/dWq, the gradient that flows THROUGH softmax + both matmuls (the real attention backward). D neg-control teeth : a deliberately wrong matmul grad is rejected. B attention LEARNS : with Wq,Wk fixed (attention pattern A fixed), the VALUE path O=A.(X.Wv) is linear -> train Wv from zero to a realizable target; assert loss collapses + Wv converges (the value projection trains). C bit-exact : train twice -> identical integer Wv (determinism is structural for integer). Evidence -> knowledge/status/nofloat_attn.log. Sovereign: imports nx_nofloat_autograd (pure integer) + nx_syscalls. HONEST scope: this is the attention CORE (matmuls + causal softmax). RoPE + output-projection + multi-head are the next sub-rung (CAP-NF-TRAIN-ATTN full). license_tier: ORIGINAL expect_exit: 0

dependencies 3 imports · 0 importers

nx_nofloat_autograd.nx nx_syscalls.nx nx_gate_emit_lib.nx nx_nofloat_attn_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 mm_gradcheck sys_mmap ↻ mm_loss nfa_leaf nfa_new nfa_matmul nfa_new ↻ nfa_matmul_nt nfa_new ↻ nfa_mse nfa_new ↻ nfa_backward nfa_qmul nfa_silud nfa_sigmoid nfa_fxexp nfa_qmul ↻ nfa_isqrt nfa_fxexp ↻ nfa_cosf nfa_reduce2pi nfa_cosq nfa_qmul ↻ nfa_sinf nfa_reduce2pi ↻ nfa_sinq nfa_qmul ↻ nfa_grad mm_fd sys_mmap ↻ mm_lossval sys_mmap ↻ mm_loss ↻ nfa_val g_abs g_pn

structs

none

consts

21const ALOG: *u8 = "knowledge/status/nofloat_attn.log"
22const Q16: i64 = 65536

functions

25func g_abs(v: i64) -> i64 { if v < 0 { return 0 - v } return v }
called by 2: mm_gradcheckmain
26func q_milli(q: i64) -> i64 { var neg: i64=0; var a: i64=q; if a<0 { neg=1; a=0-a } let m: i64=(a*1000)/Q16; if neg==1 { return 0-m } return m }
27func a_ws(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 }
called by 1: main calls 1: sys_write
28func a_wn(fd: i64, v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(fd,"-" as *u8,1);m=0-m} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(fd,b,k); return 0 }
called by 1: main calls 2: sys_mmapsys_write
31func mm_loss(tape: *i64, vals: *i64, st: *i64, op: i64, As: *i64, Bs: *i64, Ts: *i64, leaves: *i64) -> i64
43func mm_lossval(tape: *i64, vals: *i64, st: *i64, op: i64, As: *i64, Bs: *i64, Ts: *i64) -> i64
called by 1: mm_fd calls 3: sys_mmapmm_lossnfa_val
48func mm_fd(tape: *i64, vals: *i64, st: *i64, op: i64, As: *i64, Bs: *i64, Ts: *i64, pi: i64, h: i64) -> i64
called by 2: mm_gradcheckmain calls 2: sys_mmapmm_lossval
58func mm_gradcheck(tape: *i64, vals: *i64, grads: *i64, st: *i64, op: i64, As: *i64, Bs: *i64, Ts: *i64, h: i64, tol_q: i64, floor_q: i64, worst: *i64) -> i64
79func attn_fwd(tape: *i64, vals: *i64, st: *i64, Xs: *i64, Wqs: *i64, Wks: *i64, Wvs: *i64, T: i64, d: i64, scale: i64, leaves: *i64) -> i64
95func attn_loss(tape: *i64, vals: *i64, st: *i64, Xs: *i64, Wqs: *i64, Wks: *i64, Wvs: *i64, Ts: *i64, T: i64, d: i64, scale: i64, leaves: *i64) -> i64
100func attn_lossval(tape: *i64, vals: *i64, st: *i64, Xs: *i64, Wqs: *i64, Wks: *i64, Wvs: *i64, Ts: *i64, T: i64, d: i64, scale: i64) -> i64
called by 1: main calls 3: sys_mmapattn_lossnfa_val
106func attn_out(tape: *i64, vals: *i64, st: *i64, Xs: *i64, Wqs: *i64, Wks: *i64, Wvs: *i64, T: i64, d: i64, scale: i64, outO: *i64) -> i64
called by 1: main calls 3: sys_mmapattn_fwdnfa_val
114func main() -> i64