code wiki / _hdl_build / nx_nano_lm.nx
nx_nano_lm.nx
buildroot/runtime/_hdl_build/nx_nano_lm.nx
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
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
structs
| none |
consts
| 23 | const Q: i64 = 16 |
| 24 | const ONE: i64 = 65536 |
| 25 | const V: i64 = 3 // vocab: a,b,c |
| 26 | const LOG2E: i64 = 94548 |
| 27 | const C1: i64 = 45426 |
| 28 | const C2: i64 = 15743 |
functions
| 19 | func g_pn(v: i64) -> i64 { nxi_out(v); return 0 } |
| 20 | func 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 |
| 21 | func 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 } |
| 29 | func fxmul(a: i64, b: i64) -> i64 { return (a*b)>>Q } |
| 30 | func exp_fx(x: i64) -> i64 |
| 36 | func softmax(logits: *i64, probs: *i64) -> i64 |
| 41 | func 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 } |
| 44 | func train(W: *i64, corpus: *i64, n: i64, epochs: i64, lr: i64, pcorr: *i64) -> i64 |
| 64 | func gen(W: *i64, start: i64, outids: *i64, steps: i64) -> i64 |
| 70 | func main() -> i64 |