code wiki / _hdl_build / nx_intfp_gradcheck_gate.nx
nx_intfp_gradcheck_gate.nx
buildroot/runtime/_hdl_build/nx_intfp_gradcheck_gate.nx
about
nx_intfp_gradcheck_gate.nx -- FIRST verified brick of FIXED-POINT INTEGER TRAINING (the sovereign fix for the
measured 25x software-float tax / 8x training-speed loss vs PyTorch). A Q16 linear layer y = W@x with loss
L = sum(y^2), done ENTIRELY in integer fixed-point (scale S=2^16): forward, analytic backprop, and a
gold-standard INTEGER finite-difference gradcheck (numeric grad from perturbing the loss vs analytic grad).
No float anywhere -- if analytic == numeric within tolerance, integer fwd+bwd is numerically SOUND and the
full integer autograd tape can be built on it (=fast path, 358+ MMAC/s scalar / 8.4 Gop/s SIMD, deterministic).
Q-format bookkeeping (S=65536):
y_q[o] = (sum_k W_q*x_q) >> 16 (Q32 accum -> Q16)
L_q32 = sum_o y_q*y_q (Q16^2 = Q32, kept unshifted for precision)
analytic dL/dW[o,k] = 2*y*x -> gW_q = (2*y_q*x_q) >> 16 (Q16 of the real gradient)
numeric dL/dW[o,k] -> (L+_q32 - L-_q32) / (2*delta_q16) (also Q16 -- units cancel to match analytic)
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
| 19 | const S: i64 = 65536 // Q16 scale |
| 20 | const O: i64 = 3 |
| 21 | const K: i64 = 4 |
functions
| 15 | 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 } |
| 16 | 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 } |
| 17 | func iabs(v: i64) -> i64 { if v<0 { return 0-v } return v } called by 1: main |
| 24 | func fwd_loss(W: *i64, x: *i64, ybuf: *i64) -> i64 called by 1: main |
| 35 | func main() -> i64 |