code wiki / (root) / nx_autograd_tensor.nx

nx_autograd_tensor.nx

buildroot/runtime/nx_autograd_tensor.nx

39566 B932 linesdepth 5pulls 14 transitivereach 14 importersview sourcekind tooltopic autograd
docsdependenciesstructsconstsfunctions

about

nx_autograd_tensor.nx -- TRAIN-R2 (T6): TENSOR reverse-mode autograd -- the identities that scale. Rung-1's tape held scalars; real models hold ARRAYS. Same tape shape, now stride-7: {op, ai, bi, rows, cols, valp, gradp} -- valp/gradp index a per-build BUMP ARENA of f32 cells (st[1] counter; no allocator, no fragmentation, deterministic). Forward EAGER, backward ONE reverse sweep. Four backward identities (learn these and every architecture MLP->transformer->DiT is composition): 1. matvec y=W*x (W r*c, x c): dW[i][j] += gy[i]*x[j] (outer product); dx[j] += sum_i W[i][j]*gy[i] (W^T gy) 2. vadd y=x+b: both inputs receive gy unchanged (bias gradients are free) 3. relu elementwise: pass gy[i] iff the INPUT cell was > 0 4. mse L=(1/n)sum(p-t)^2: dp_i += gL*(2/n)*(p_i - t_i); target grad nobody reads Scalar ops (sum, average) compose for free: matvec(1x1, 1x1) = scalar multiply, vadd of 1-cell nodes = add. genealogy_id: linnainmaa_1970_reverse_mode_ad + the matmul-backward identity (dA=dC B^T, dB=A^T dC) lineage_id: sovereign_tensor_tape_autograd_v1 license_tier: ORIGINAL verdict: GREEN (nx_train_r2_gate 2026-06-14: MLP gradcheck + affine-recover + bit-exact + AdamW)

dependencies 8 imports · 14 importers

nx_f32.nx nx_f32_div.nx nx_f32_cvt.nx nx_f32_exp.nx nx_f32_log.nx nx_f32_sincos.nx nx_fft_f32.nx nx_syscalls.nx nx_autograd_tensor.nx nx_f32_attn_charlm_gate.nx nx_f32_miniqwen_train_gate.nx nx_f32_qwen2_train_gate.nx nx_fnet_mlm_gate.nx nx_fnet_model_gate.nx nx_fnet_train_gate.nx nx_reader_quantize_gate.nx nx_reader_squad_f32_gate.nx nx_ssm_gate.nx nx_ssm_lm_gate.nx

diagram shows first 10 each side; +0 more imports, +4 more importers in the complete lists below.

imports: nx_f32.nxnx_f32_div.nxnx_f32_cvt.nxnx_f32_exp.nxnx_f32_log.nxnx_f32_sincos.nxnx_fft_f32.nxnx_syscalls.nx

imported by: nx_f32_attn_charlm_gate.nxnx_f32_miniqwen_train_gate.nxnx_f32_qwen2_train_gate.nxnx_fnet_mlm_gate.nxnx_fnet_model_gate.nxnx_fnet_train_gate.nxnx_reader_quantize_gate.nxnx_reader_squad_f32_gate.nxnx_ssm_gate.nxnx_ssm_lm_gate.nxnx_ta_parallel_grad_gate.nxnx_ta_transformer_gradcheck_gate.nxnx_train_r2_gate.nxnx_train_r3_gate.nx

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

main sys_mmap nx_i32_to_f32 ta_leaf ta_new ta_matvec ta_new ↻ nx_f32_add nx_f32_classify nx_f32_exp_field nx_f32_mant_field nx_f32_sign nx_f32_mant_field ↻ nx_f32_exp_field ↻ nx_f32_mul nx_f32_classify ↻ nx_f32_sign ↻ nx_f32_mant_field ↻ nx_f32_exp_field ↻ ta_backward nx_f32_add ↻ nx_f32_mul ↻ nx_f32_gt nx_f32_lt nx_f32_is_nan nx_f32_classify ↻ nx_f32_is_zero nx_f32_classify ↻ nx_f32_div nx_f32_classify ↻ nx_f32_sign ↻ nx_f32_mant_field ↻ nx_f32_exp_field ↻ nx_i32_to_f32 ↻ nx_f32_sub nx_f32_add ↻ nx_f32_neg nx_f32_exp nx_f32_classify ↻ nx_f32_sign ↻

structs

none

consts

23const TA_MAGIC_1000000: i64 = 1000000
24const TA_MAGIC_1024: i64 = 1024
26const TA_LEAF: i64 = 0
27const TA_MATVEC: i64 = 1
28const TA_VADD: i64 = 2
29const TA_RELU: i64 = 3
30const TA_MSE: i64 = 4
31const TA_SOFTMAX: i64 = 5
32const TA_SOFTCE: i64 = 6
33const TA_FNET: i64 = 7
34const TA_SSM: i64 = 8
35const TA_SLICE: i64 = 9
37const TA_MATMUL: i64 = 10 // C[m,p] = A[m,k] . B[k,p]
38const TA_MATMUL_NT: i64 = 11 // S[m,p] = A[m,k] . B[p,k]^T (the Q.K^T contraction)
39const TA_CMUL: i64 = 12 // y = a * c, c an f32 constant stored in bi (the 1/sqrt(d) scale)
40const TA_SOFTMAX_ROWS: i64 = 13 // per-row softmax; bi=1 -> CAUSAL (row i over j<=i)
41const TA_ROPE: i64 = 14 // rotary position embedding on [T,hd]; parameter-free, orthogonal
42const TA_HADAMARD: i64 = 15 // y = a (*) b elementwise (the SwiGLU gate)
43const TA_SILU: i64 = 16 // y = x*sigmoid(x)
44const TA_RMSNORM_ROWS: i64 = 17 // per-row y = x/sqrt(mean(x^2)+eps)
45const TA_SOFTCE_ROWS: i64 = 18 // fused per-row softmax-CE vs integer target ids (bi = ids ptr)
46const TA_ROPE_TAB: i64 = 19 // RoPE from a PRECOMPUTED cos/sin table (bi = table ptr) -- nx_f32_sin/cos
49const TA_F32_ZERO: i64 = 0 // IEEE-754 binary32 +0.0
50const TA_F32_ONE: i64 = 1065353216 // IEEE-754 binary32 1.0

functions

53func ta_constf(num: i64, den: i64) -> i64 { return nx_f32_div(nx_i32_to_f32(num), nx_i32_to_f32(den)) }
56func ta_det_init(dst: *i64, n: i64, seed: i64) -> i64
called by 3: ml_trainmo_trainlm_train calls 1: ta_constf
66func ta_new(tape: *i64, st: *i64, op: i64, ai: i64, bi: i64, rows: i64, cols: i64) -> i64
78func ta_leaf(tape: *i64, vals: *i64, st: *i64, rows: i64, cols: i64, src: *i64, soff: i64) -> i64
88func ta_matvec(tape: *i64, vals: *i64, st: *i64, aW: i64, bx: i64) -> i64
107func ta_vadd(tape: *i64, vals: *i64, st: *i64, a: i64, b: i64) -> i64
121func ta_relu(tape: *i64, vals: *i64, st: *i64, a: i64) -> i64
139func ta_mse(tape: *i64, vals: *i64, st: *i64, pred: i64, target: i64) -> i64
157func ta_softmax(tape: *i64, vals: *i64, st: *i64, a: i64) -> i64
175func ta_softce(tape: *i64, vals: *i64, st: *i64, logits: i64, target: i64) -> i64
198func ta_fnet(tape: *i64, vals: *i64, st: *i64, a: i64) -> i64
219func ta_ssm(tape: *i64, vals: *i64, st: *i64, a_node: i64, x_node: i64) -> i64
243func ta_slice(tape: *i64, vals: *i64, st: *i64, x_node: i64, row: i64) -> i64
called by 2: lm_buildlm_predict calls 1: ta_new
255func ta_matmul(tape: *i64, vals: *i64, st: *i64, a: i64, b: i64) -> i64
278func ta_matmul_nt(tape: *i64, vals: *i64, st: *i64, a: i64, b: i64) -> i64
301func ta_cmul(tape: *i64, vals: *i64, st: *i64, a: i64, c_f: i64) -> i64
313func ta_softmax_rows(tape: *i64, vals: *i64, st: *i64, a: i64, causal: i64) -> i64
339func ta_rope_ang(t: i64, i: i64, np: i64) -> i64
346func ta_rope(tape: *i64, vals: *i64, st: *i64, a: i64) -> i64
373func ta_rope_build_tab(tab: *i64, tmax: i64, np: i64) -> i64
390func ta_rope_tab(tape: *i64, vals: *i64, st: *i64, a: i64, tab: *i64) -> i64
415func ta_hadamard(tape: *i64, vals: *i64, st: *i64, a: i64, b: i64) -> i64
428func ta_sigmoid(x: i64) -> i64
433func ta_silu(tape: *i64, vals: *i64, st: *i64, a: i64) -> i64
445func ta_rmsnorm_rows(tape: *i64, vals: *i64, st: *i64, a: i64) -> i64
467func ta_softce_rows(tape: *i64, vals: *i64, st: *i64, logits: i64, tgt: *i64) -> i64
491func ta_val(tape: *i64, vals: *i64, k: i64, c: i64) -> i64 { return vals[tape[7 * k + 5] + c] }
492func ta_grad(tape: *i64, grads: *i64, k: i64, c: i64) -> i64 { return grads[tape[7 * k + 6] + c] }
495func ta_backward(tape: *i64, vals: *i64, grads: *i64, n: i64, root: i64) -> i64
899func ta_f32_to_milli(v: i64) -> i64
917func main() -> i64