code wiki / _hdl_build / nx_intfp_softmax_gradcheck_gate.nx

nx_intfp_softmax_gradcheck_gate.nx

buildroot/runtime/_hdl_build/nx_intfp_softmax_gradcheck_gate.nx

5633 B85 linesdepth 2pulls 2 transitivereach 0 importersview sourcekind gate/prooftopic intfp
docsdependenciesstructsconstsfunctions

about

nx_intfp_softmax_gradcheck_gate.nx -- KEYSTONE op for integer ATTENTION training: fixed-point SOFTMAX with a fixed-point EXP and the full Jacobian backward, gradchecked (integer finite-difference). exp is the crux -- it's exactly where the software-float tape calls nx_f32_exp. Here exp is done in Q16 via the 2^x decomposition (exp(x)=2^(x*log2e); split into shift 2^-ui and a quadratic for 2^-uf), NO float. Then softmax s_i=e_i/sum, loss L=sum(s_i^2), analytic backward dL/dx_i = s_i*(g_i - sum_j s_j*g_j) with g_i=2*s_i. The max-subtraction is a stop-gradient constant (softmax is shift-invariant), so we fix the shift at the base point -> the forward is smooth and finite-diff matches analytic. If this gradchecks, integer attention is trainable. license_tier: ORIGINAL

dependencies 1 imports · 0 importers

nx_syscalls.nx nx_intfp_softmax_gradcheck_gat

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 wn sys_write ↻ sys_mmap fp_exp_q16 sys_mmap ↻ softmax_loss sys_mmap ↻ fp_exp_q16 ↻ iabs

structs

none

consts

14const S: i64 = 65536
15const N: i64 = 5

functions

10func 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 1: main calls 1: sys_write
11func 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 1: main calls 2: sys_writesys_mmap
12func iabs(v: i64) -> i64 { if v<0 { return 0-v } return v }
called by 1: main
19func fp_exp_q16(xq: i64) -> i64
called by 2: softmax_lossmain
36func softmax_loss(x: *i64, shift: i64, s: *i64) -> i64
called by 1: main calls 2: sys_mmapfp_exp_q16
45func main() -> i64