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
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
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
structs
| none |
consts
| 14 | const S: i64 = 65536 |
| 15 | const N: i64 = 5 |
functions
| 10 | func w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } |
| 11 | func 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 } |
| 12 | func iabs(v: i64) -> i64 { if v<0 { return 0-v } return v } called by 1: main |
| 19 | func fp_exp_q16(xq: i64) -> i64 |
| 36 | func softmax_loss(x: *i64, shift: i64, s: *i64) -> i64 |
| 45 | func main() -> i64 |