code wiki / (root) / nx_nofloat_softmax_gate.nx

nx_nofloat_softmax_gate.nx

buildroot/runtime/nx_nofloat_softmax_gate.nx

6388 B107 linesdepth 3pulls 3 transitivereach 0 importersview sourcekind gate/prooftopic nofloat
docsdependenciesstructsconstsfunctions

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

nx_syscalls.nx nx_gate_verdict.nx nx_nofloat_softmax_gate.nx

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

main sm_puts sys_write fx_exp sm_num sys_mmap sys_write ↻ sys_mmap ↻ fx_softmax fx_exp ↻ absd gv_ctr sys_mmap ↻ gv_verdict gv_puts sys_write ↻ gv_num sys_mmap ↻ sys_write ↻ sys_munmap gv_journal sys_openat_append sys_mmap ↻ gv_catn sys_mmap ↻ sys_munmap ↻ sys_now_realtime_sec sys_mmap ↻ sys_clock_gettime_real gv_cat sys_write ↻ sys_close sys_munmap ↻ sys_exit

structs

none

consts

15const Q1: i64 = 65536 // 1.0
16const LOG2E: i64 = 94548 // log2(e) = 1.4426950
17const PC0: i64 = 65536 // 2^f Horner: pinned so 2^0=1, 2^1=2 exact
18const PC1: i64 = 45426
19const PC2: i64 = 15743
20const PC3: i64 = 4367
21const LN2: i64 = 45426 // ln(2) = 0.6931472

functions

23func 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 }
called by 1: main calls 1: sys_write
24func 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 }
called by 1: main calls 2: sys_mmapsys_write
25func absd(a: i64, b: i64) -> i64 { if a>b { return a-b } return b-a }
called by 1: main
28func fx_exp(x: i64) -> i64
called by 2: fx_softmaxmain
45func fx_softmax(x: *i64, out: *i64, e: *i64, n: i64, rev: i64) -> i64
called by 1: main calls 1: fx_exp
59func main() -> i64