code wiki / _hdl_build / nx_intfp_qknorm_gradcheck_gate.nx
nx_intfp_qknorm_gradcheck_gate.nx
buildroot/runtime/_hdl_build/nx_intfp_qknorm_gradcheck_gate.nx
about
nx_intfp_qknorm_gradcheck_gate.nx -- 2026 FRONTIER: QK-NORM (Qwen3.5) in Q24 INTEGER, gradchecked, no float.
Applies RMSNorm (with learned gamma) to Q and K per-token over the head dim BEFORE the attention dot product --
stabilizes attention logits at scale (kills the exploding-logit failure mode). Composes proven RMSNorm + causal
attention; backward = attention-bwd -> dQn/dKn -> RMSNorm-bwd -> dWq/dWk + dgq/dgk. Gradcheck Wq (thru Q-norm),
gq (Q-norm gamma, the novelty), Wv. 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
| 12 | const S: i64 = 16777216 // Q24 |
| 13 | const T: i64 = 3 |
| 14 | const DM: i64 = 4 |
| 15 | const SCALE: i64 = 8388608 |
| 16 | const EPS: i64 = 16777216 // eps in Q48 (tiny) |
functions
| 8 | 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 } |
| 9 | 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 } |
| 10 | func iabs(v: i64) -> i64 { if v<0 { return 0-v } return v } called by 1: gcheck |
| 18 | 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: qkn_fwd |
| 19 | func fp_exp(xq: i64) -> i64 { let y: i64=(xq*24204406)/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=161380; p=931144+(p*yf)/S; p=4030770+(p*yf)/S; p=11632166+(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) } called by 1: qkn_fwd |
| 22 | func qkn_fwd(P: *i64) -> i64 |
| 33 | func qkn_bwd(P: *i64) -> i64 |
| 63 | func gcheck(name: *u8, P: *i64, Wt: *i64, dW: *i64, ncell: i64) -> i64 |
| 80 | func main() -> i64 |