code wiki / _hdl_build / nx_nofloat_sample_gate.nx
nx_nofloat_sample_gate.nx
buildroot/runtime/_hdl_build/nx_nofloat_sample_gate.nx
about
nx_nofloat_sample_gate.nx -- TEMPERATURE SAMPLING decode in pure integer Q16 (CAP-NF-SAMPLE). Real LLMs
GENERATE by sampling from the softmax distribution (with a temperature), not just greedy argmax. The no-float
stack had only argmax; this adds a CORRECT fixed-point sampler: a deterministic LCG PRNG + softmax-with-
temperature (scale logits by 1/T, max-subtract, fxexp, normalize) + inverse-CDF pick. All integer -> the
sampling is REPRODUCIBLE given a seed (the no-float determinism exceed: same seed+weights -> same text, which
float/GPU sampling cannot guarantee bit-for-bit).
T1 LOW temperature concentrates on the argmax (>= 90% of draws) -- approaches greedy as T->0.
T2 at temperature=1 the EMPIRICAL sample frequencies match softmax(logits) (a CORRECT sampler, measured).
T3 REPRODUCIBLE: two runs with the same seed produce byte-identical samples (no-float determinism).
T4 HIGH temperature is measurably MORE UNIFORM than low (argmax-frequency drops) -- temperature has teeth.
Evidence -> knowledge/status/nofloat_sample.log. Sovereign: nx_nofloat_autograd (nfa_fxexp/nfa_qmul) + nx_syscalls.
expect_exit: 0 license_tier: ORIGINAL
dependencies 3 imports · 0 importers
imports: nx_nofloat_autograd.nxnx_syscalls.nxnx_gate_emit_lib.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
| 18 | const Q16: i64 = 65536 |
functions
| 21 | func g_abs(v: i64) -> i64 { if v<0 { return 0-v } return v } called by 1: main |
| 22 | func s_ws(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 } |
| 23 | func s_wn(fd: i64, v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(fd,"-" as *u8,1);m=0-m} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;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(fd,b,k); return 0 } |
| 26 | func lcg(st: *i64) -> i64 { st[0] = (st[0]*1103515245 + 12345) & 2147483647; return (st[0] >> 15) & 65535 } called by 1: main |
| 28 | func softmax_temp(logits: *i64, n: i64, invtemp: i64, out: *i64) -> i64 |
| 41 | func sample_idx(probs: *i64, n: i64, u: i64) -> i64 called by 1: main |
| 46 | func argmax_of(probs: *i64, n: i64) -> i64 { var b: i64=0; var bv: i64=probs[0]; var i: i64=1; while i<n { if probs[i]>bv { bv=probs[i]; b=i } i=i+1 } return b } called by 1: main |
| 48 | func main() -> i64 |