code wiki / _hdl_build / nx_nofloat_attn_gate.nx
nx_nofloat_attn_gate.nx
buildroot/runtime/_hdl_build/nx_nofloat_attn_gate.nx
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
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
structs
| none |
consts
| 21 | const ALOG: *u8 = "knowledge/status/nofloat_attn.log" |
| 22 | const Q16: i64 = 65536 |
functions
| 25 | func g_abs(v: i64) -> i64 { if v < 0 { return 0 - v } return v } |
| 26 | func 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 } |
| 27 | func 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 } |
| 28 | func 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 } |
| 31 | func mm_loss(tape: *i64, vals: *i64, st: *i64, op: i64, As: *i64, Bs: *i64, Ts: *i64, leaves: *i64) -> i64 |
| 43 | func mm_lossval(tape: *i64, vals: *i64, st: *i64, op: i64, As: *i64, Bs: *i64, Ts: *i64) -> i64 |
| 48 | func mm_fd(tape: *i64, vals: *i64, st: *i64, op: i64, As: *i64, Bs: *i64, Ts: *i64, pi: i64, h: i64) -> i64 |
| 58 | func 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 |
| 79 | func 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 |
| 95 | func 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 |
| 100 | func 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 |
| 106 | func 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 |
| 114 | func main() -> i64 |