nx_nofloat_softmax_gate.nx
buildroot/runtime/nx_nofloat_softmax_gate.nx
about
nx_nofloat_softmax_gate.nx -- a SYSTEM for the HARD no-float primitive: fixed-point exp + softmax.
Transformers / attention (LLM + coding-LLM) need softmax = exp(x)/sum(exp) -- but exp is a transcendental,
and float exp is NON-DETERMINISTIC. This builds it from the hardware rung up in pure INTEGER Q16:
exp(x) = 2^(x*log2 e); split x*log2e into integer + fraction; 2^int = bit-shift; 2^frac = a small
minimax-tuned integer Horner polynomial (endpoints pinned exact). Every op is integer -> BIT-EXACT
DETERMINISTIC (the no-float exceed [[nx_nofloat_exceed_gate]]). Accuracy is verified SELF-CONSISTENTLY
(no float oracle): exp(0)=1, exp(-ln2)=1/2, exp(-2ln2)=1/4, and the functional equation exp(a)exp(b)=
exp(a+b). Softmax determinism: forward-sum == reverse-sum, bit-identical (float cannot promise this).
This is the reusable approach for fixed-point transcendentals -> unlocks no-float attention/transformers.
No hw writes (Rule 26). expect_exit: 0 license_tier: ORIGINAL
dependencies 2 imports · 0 importers
imports: nx_syscalls.nxnx_gate_verdict.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
| 15 | const Q1: i64 = 65536 // 1.0 |
| 16 | const LOG2E: i64 = 94548 // log2(e) = 1.4426950 |
| 17 | const PC0: i64 = 65536 // 2^f Horner: pinned so 2^0=1, 2^1=2 exact |
| 18 | const PC1: i64 = 45426 |
| 19 | const PC2: i64 = 15743 |
| 20 | const PC3: i64 = 4367 |
| 21 | const LN2: i64 = 45426 // ln(2) = 0.6931472 |
functions
| 23 | func sm_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } |
| 24 | func sm_num(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 } |
| 25 | func absd(a: i64, b: i64) -> i64 { if a>b { return a-b } return b-a } called by 1: main |
| 28 | func fx_exp(x: i64) -> i64 |
| 45 | func fx_softmax(x: *i64, out: *i64, e: *i64, n: i64, rev: i64) -> i64 |
| 59 | func main() -> i64 |