code wiki / _hdl_build / nx_train_r1_gate.nx
nx_train_r1_gate.nx
buildroot/runtime/_hdl_build/nx_train_r1_gate.nx
about
nx_train_r1_gate.nx -- GATE for TRAIN-R1: the scalar-tape autograd keystone. Proves, by RUNNING, that the
team can TRAIN a model sovereignly. Three exact gates (spec 2026-06-09-tutoring-training-substrate-rung1.md):
A GRADCHECK (oracle = mathematics): loss = (relu(w1*x+b1)*w2 + b2)^2 at params off the relu kink
(w1=2/3, b1=1/4, w2=3/2, b2=1/4, x=3/4). For each param: analytic grad (reverse-mode backward) vs
central finite difference (f(p+h)-f(p-h))/2h, h=1/128; relative error < 1/32 (floor 1/64). Catches
every sign/chain-rule defect a wrong autograd could ship.
B A MODEL PROVABLY LEARNS: fit y = 1.5x - 0.5 (8 points x=i/4, targets in-f32) by full-batch gradient
descent, lr=1/20, 500 epochs, MSE via the tape. Assert final loss < 1/1000 AND |w-1.5| < 1/16 AND
|b+0.5| < 1/16 AND final loss < first loss. The smallest EXACT statement of "we can train a model".
C BIT-EXACT REPRODUCIBLE: run the whole training twice from zero-init; assert the final (w,b) f32 BITS
are identical. GPU training is non-deterministic by default; the bits-up f32 substrate makes
byte-identical training a GATE -- the determinism exceed-axis carried into ML.
Evidence -> knowledge/status/train_r1.log (TRAINR1GATE authored=organ ... verdict=GREEN). license_tier: ORIGINAL
dependencies 2 imports · 0 importers
imports: nx_autograd.nxnx_syscalls.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
| 19 | const TR_LOG: *u8 = "knowledge/status/train_r1.log" |
functions
| 21 | func tr_w(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 } |
| 22 | func tr_wn(fd: i64, v: i64) -> i64 |
| 35 | func tr_f32_to_milli(v: i64) -> i64 |
| 54 | func tr_build_loss(tape: *i64, np: *i64, w1: i64, b1: i64, w2: i64, b2: i64, x: i64, leaves: *i64) -> i64 |
| 69 | func tr_loss_val(w1: i64, b1: i64, w2: i64, b2: i64, x: i64) -> i64 |
| 77 | func tr_analytic_grads(w1: i64, b1: i64, w2: i64, b2: i64, x: i64, gout: *i64) -> i64 |
| 89 | func tr_fd_grad(p: *i64, pi: i64, h: i64) -> i64 |
| 102 | func tr_train(epochs: i64, wout: *i64, bout: *i64, lfirst: *i64, llast: *i64) -> i64 |
| 149 | func tr_emit(fd: i64, r: *i64) -> i64 |
| 161 | func main() -> i64 |