code wiki / _hdl_build / nx_intfp_layer_gradcheck_gate.nx
nx_intfp_layer_gradcheck_gate.nx
buildroot/runtime/_hdl_build/nx_intfp_layer_gradcheck_gate.nx
about
nx_intfp_layer_gradcheck_gate.nx -- FULL integer transformer LAYER: two pre-norm residual blocks stacked
x2 = x + Attn(RMSNorm1(x)) [block 1]
y = x2 + SwiGLU(RMSNorm2(x2)) [block 2]
with the COMPLETE end-to-end integer backward (dX chains block2 -> block1 through the residual stream), Q20, no
float. Gradchecked (integer finite-diff, max-|grad| metric) across BOTH blocks (g1,Wq,g2,Wg,Wd) and the input X,
then TRAINED (loss drops). This is the whole layer the real model stacks. Modular fns to fit the 16-arg budget.
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 = 1048576 |
| 15 | const T: i64 = 3 |
| 16 | const DM: i64 = 4 |
| 17 | const HF: i64 = 6 |
| 18 | const SCALE: i64 = 524288 |
| 19 | const EPS: i64 = 1048576 |
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: gcheck |
| 21 | func isqrt(n: i64) -> i64 { if n<=0 { return 0 } var bit: i64=1; while bit*4<=n { bit=bit*4 } var res: i64=0; var num: i64=n; while bit!=0 { if num>=res+bit { num=num-(res+bit); res=(res/2)+bit } else { res=res/2 } bit=bit/4 } return res } called by 1: rmsnorm_fwd |
| 22 | func fp_exp(xq: i64) -> i64 { let y: i64=(xq*1512776)/S; var yi: i64=0; if y>=0 { yi=y/S } else { yi=0-(((0-y)+S-1)/S) } let yf: i64=y-yi*S; var p: i64=10085; p=58197+(p*yf)/S; p=251882+(p*yf)/S; p=726817+(p*yf)/S; p=S+(p*yf)/S; if yi>=0 { if yi>=31 { return 2000000000 } return p*(1<<yi) } let k: i64=0-yi; if k>=31 { return 0 } return p/(1<<k) } |
| 23 | func sigmoid(z: i64) -> i64 { let e: i64=fp_exp(0-z); return (S*S)/(S+e) } |
| 25 | func rmsnorm_fwd(x: *i64, gm: *i64, out: *i64, rms: *i64) -> i64 |
| 32 | func rmsnorm_bwd(x: *i64, gm: *i64, dh: *i64, rms: *i64, dxacc: *i64, dgm: *i64) -> i64 called by 1: layer_bwd |
| 43 | func attn_fwd(h: *i64, Wq: *i64, Wk: *i64, Wv: *i64, Q: *i64, K: *i64, V: *i64, A: *i64, O: *i64) -> i64 |
| 52 | func attn_bwd(h: *i64, Wq: *i64, Wk: *i64, Wv: *i64, Q: *i64, K: *i64, V: *i64, A: *i64, dO: *i64, dWq: *i64, dWk: *i64, dWv: *i64, dh: *i64) -> i64 |
| 65 | func swiglu_fwd(h: *i64, Wg: *i64, Wu: *i64, Wd: *i64, g: *i64, a: *i64, u: *i64, m: *i64, ffn: *i64) -> i64 |
| 72 | func swiglu_bwd(h: *i64, Wg: *i64, Wu: *i64, Wd: *i64, g: *i64, a: *i64, u: *i64, m: *i64, dOut: *i64, dWg: *i64, dWu: *i64, dWd: *i64, dh: *i64) -> i64 |
| 83 | func layer_fwd(P: *i64) -> i64 |
| 93 | func layer_bwd(P: *i64) -> i64 |
| 109 | func gcheck(name: *u8, P: *i64, tgtslot: i64, gslot: i64, ncell: i64) -> i64 |
| 128 | func main() -> i64 |