code wiki / _hdl_build / nx_intfp_train_gate.nx
nx_intfp_train_gate.nx
buildroot/runtime/_hdl_build/nx_intfp_train_gate.nx
about
nx_intfp_train_gate.nx -- THIRD brick of integer training: prove an integer model actually LEARNS.
A Q16 2-layer MLP (W1->ReLU->W2) trained by INTEGER SGD to fit NEX input->target examples, loss L=sum(y-t)^2.
Everything -- forward, backprop, AND the weight update -- is fixed-point integer, NO float. If the loss falls
by >10x over the run, the sovereign integer optimizer works end-to-end (the last unknown before the full tape).
Q16 (S=2^16): fwd as before; gy=2*(y-t); grads accumulate over examples; SGD W_q -= (lr_q * gW_q) >> 16.
This is where fixed-point can bite: small gradients can UNDERFLOW the update (truncate to 0). We MEASURE whether
it converges anyway -- honest data on integer training dynamics, not an assertion. 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
| 13 | const S: i64 = 65536 |
| 14 | const K: i64 = 3 |
| 15 | const H: i64 = 8 |
| 16 | const O: i64 = 2 |
| 17 | const NEX: i64 = 3 |
| 18 | const STEPS: i64 = 2000 |
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 } |
| 21 | func train_step(W1: *i64, X: *i64, W2: *i64, T: *i64, gW1: *i64, gW2: *i64, hb: *i64, ab: *i64, yb: *i64) -> i64 |
| 47 | func main() -> i64 |