code wiki / _hdl_build / nx_nano_lm.nx

nx_nano_lm.nx

buildroot/runtime/_hdl_build/nx_nano_lm.nx

7582 B112 linesdepth 3pulls 4 transitivereach 0 importersview sourcekind tool
docsdependenciesstructsconstsfunctions

about

nx_nano_lm.nx -- OUR OWN SOVEREIGN nanoGPT (the bigram/char rung where nanoGPT+makemore start), built from the Nishi rung up. A char-level language model that ACTUALLY TRAINS (integer gradient descent) and GENERATES text back. Model: a V*V logit table W (W[prev][next]); forward = softmax(W[prev]); cross-entropy gradient = probs - onehot (NO log needed -- the softmax+CE gradient is just probs minus the target one-hot). 100% integer Q16 => bit-exact deterministic, $0 on this laptop CPU (no GPU/CUDA/PyTorch). Trains on a tiny cyclic corpus, learns the transitions, and generates the pattern back. T1 P(correct next char) INCREASES across training (it learns the statistics). T2 GENERATION reproduces the learned pattern (from 'a' -> a,b,c,a,b,c). T3 (EXCEED) deterministic bit-identical. T4 argmax(W[prev]) == the correct next char for every char. expect_exit: 0 Sovereign: nx_syscalls.

dependencies 3 imports · 0 importers

nx_syscalls.nx nx_itoa_lib.nx nx_g_puts_lib.nx nx_nano_lm.nx

imports: nx_syscalls.nxnx_itoa_lib.nxnx_g_puts_lib.nx

imported by: nobody (leaf or entry point)

call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown

main g_puts sys_write sys_mmap g_pn nxi_out nxi_fd sys_mmap ↻ ccz_cat_num sys_write ↻ sys_munmap train sys_mmap ↻ softmax sys_mmap ↻ exp_fx fxmul fxmul ↻ ck g_puts ↻ gen sys_mmap ↻ argmax g_ch sys_mmap ↻ sys_write ↻ argmax ↻ sys_openat_append sys_write ↻ sys_close sys_exit

structs

none

consts

23const Q: i64 = 16
24const ONE: i64 = 65536
25const V: i64 = 3 // vocab: a,b,c
26const LOG2E: i64 = 94548
27const C1: i64 = 45426
28const C2: i64 = 15743

functions

19func g_pn(v: i64) -> i64 { nxi_out(v); return 0 }
called by 1: main calls 1: nxi_out
20func g_ch(id: i64) -> i64 { let b: *u8=sys_mmap(1); b[0]=(97+id) as u8; sys_write(1,b,1); return 0 } // 0->a,1->b,2->c
called by 1: main calls 2: sys_mmapsys_write
21func ck(name: *u8, c: i64) -> i64 { if c==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) } g_puts(name); g_puts("\n" as *u8); return c }
called by 1: main calls 1: g_puts
29func fxmul(a: i64, b: i64) -> i64 { return (a*b)>>Q }
called by 2: exp_fxtrain
30func exp_fx(x: i64) -> i64
called by 1: softmax calls 1: fxmul
36func softmax(logits: *i64, probs: *i64) -> i64
called by 1: train calls 2: sys_mmapexp_fx
41func argmax(a: *i64) -> i64 { var bi: i64=0; var bv: i64=a[0]; var i: i64=1; while i<V { if a[i]>bv { bv=a[i]; bi=i } i=i+1 } return bi }
called by 2: genmain
44func train(W: *i64, corpus: *i64, n: i64, epochs: i64, lr: i64, pcorr: *i64) -> i64
called by 1: main calls 3: sys_mmapsoftmaxfxmul
64func gen(W: *i64, start: i64, outids: *i64, steps: i64) -> i64
called by 1: main calls 2: sys_mmapargmax
70func main() -> i64