nx_nofloat_autograd.nx
buildroot/runtime/nx_nofloat_autograd.nx
about
dependencies 1 imports · 47 importers
diagram shows first 10 each side; +0 more imports, +37 more importers in the complete lists below.
imports: nx_syscalls.nx
imported by: nx_blockfloat_block_gate.nxnx_blockfloat_generate_gate.nxnx_blockfloat_lm_gate.nxnx_blockfloat_mha_gate.nxnx_blockfloat_stack_gate.nxnx_ng_avatar.nxnx_ng_avatar_rig.nxnx_ng_gen3d.nxnx_ng_gsplat.nxnx_ng_hashenc.nxnx_ng_radiance.nxnx_nofloat_adamw32_gate.nxnx_nofloat_adamw_gate.nxnx_nofloat_attn_gate.nxnx_nofloat_autograd_ddp_gate.nxnx_nofloat_autograd_gate.nxnx_nofloat_block_gate.nxnx_nofloat_bpe_lm_gate.nxnx_nofloat_corpus_gate.nxnx_nofloat_deepstack_gate.nxnx_nofloat_distill_gate.nxnx_nofloat_generalize_gate.nxnx_nofloat_generate_gate.nxnx_nofloat_grammar_gate.nxnx_nofloat_incontext_gate.nxnx_nofloat_induction_gate.nxnx_nofloat_kvcache_gate.nxnx_nofloat_kvcache_int8_gate.nxnx_nofloat_landing_gate.nxnx_nofloat_lm_gate.nxnx_nofloat_multihead_gate.nxnx_nofloat_prose_gate.nxnx_nofloat_quant_gate.nxnx_nofloat_rope_gate.nxnx_nofloat_sample_gate.nxnx_nofloat_scale2_gate.nxnx_nofloat_scale_batch_gate.nxnx_nofloat_scale_ffn_gate.nxnx_nofloat_scale_gate.nxnx_nofloat_scale_wide_gate.nxnx_nofloat_train_exceed.nxnx_nofloat_vs_float_gate.nxnx_nofloat_weighttie_gate.nxnx_nofloat_xformer_gate.nxnx_reader_quantize_gate.nxnx_reader_span_gate.nxnx_reader_squad_gate.nx
structs
| none |
consts
| 29 | const NFA_MAGIC_5040: i64 = 5040 |
| 30 | const NFA_MAGIC_362880: i64 = 362880 |
| 31 | const NFA_MAGIC_40320: i64 = 40320 |
| 32 | const NFA_MAGIC_45426: i64 = 45426 |
| 34 | const NFA_LEAF: i64 = 0 |
| 35 | const NFA_MATVEC: i64 = 1 |
| 36 | const NFA_VADD: i64 = 2 |
| 37 | const NFA_RELU: i64 = 3 |
| 38 | const NFA_MSE: i64 = 4 |
| 39 | const NFA_SOFTMAX: i64 = 5 // attention nonlinearity |
| 40 | const NFA_SILU: i64 = 6 // FFN activation (x*sigmoid(x)) |
| 41 | const NFA_RMSNORM: i64 = 7 // transformer normalization |
| 42 | const NFA_MATMUL: i64 = 8 // C[m,p] = A[m,k] . B[k,p] |
| 43 | const NFA_MATMUL_NT: i64 = 9 // S[m,p] = A[m,k] . B[p,k]^T (the Q.K^T attention contraction) |
| 44 | const NFA_CMUL: i64 = 10 // elementwise scale by a Q16 constant (bi holds the constant, not a node) |
| 45 | const NFA_SOFTMAX_ROWS: i64 = 11 // per-row softmax (bi=1 -> CAUSAL: row i normalizes over j<=i only) |
| 46 | const NFA_ROPE: i64 = 12 // rotary position embedding on [T,hd] (parameter-free; pos = row index) |
| 47 | const NFA_HADAMARD: i64 = 13 // elementwise vector multiply a (*) b (the SwiGLU gate) |
| 48 | const NFA_RMSNORM_ROWS: i64 = 14 // PER-ROW (per-token) RMSNorm over the cols of an [T,d] node |
| 49 | const NFA_EMBED: i64 = 15 // gather rows of E[V,dm] by integer token ids -> X[T,dm] (bi = ids ptr) |
| 50 | const NFA_SOFTCE_ROWS: i64 = 16 // fused per-row softmax cross-entropy over targets (bi = target ids ptr) |
| 51 | const NFA_SLICE_COLS: i64 = 17 // extract cols [c0,c0+w) of an [T,C] node -> [T,w] (bi = c0); for multi-head |
| 52 | const NFA_CONCAT_COLS: i64 = 18 // concat two [T,*] nodes column-wise -> [T, wa+wb]; for multi-head |
| 53 | const NFA_Q16: i64 = 65536 // 1.0 in Q16 fixed-point (matches nx_nofloat_llm) |
| 58 | const NFA_LOG2E: i64 = 94548 |
| 59 | const NFA_PC0: i64 = 65536 |
| 60 | const NFA_PC1: i64 = 45426 |
| 61 | const NFA_PC2: i64 = 15743 |
| 62 | const NFA_PC3: i64 = 4367 |
| 64 | const NFA_HALF_PI: i64 = 102944 |
| 65 | const NFA_PI: i64 = 205887 |
| 66 | const NFA_3HALF_PI: i64 = 308831 |
| 67 | const NFA_TWO_PI: i64 = 411775 |
| 68 | const NFA_LN_BASE: i64 = 905421 |
functions
| 71 | func nfa_qmul(a: i64, b: i64) -> i64 { return (a * b) >> 16 } |
| 74 | func nfa_isqrt(v: i64) -> i64 { if v<=0 { return 0 } if v<4 { return 1 } var x: i64=v; var y: i64=(x+1)>>1; var go: i64=1; while go==1 { if y<x { x=y; y=(x+v/x)>>1 } else { go=0 } } return x } |
| 76 | func nfa_fxexp(x: i64) -> i64 { var xm: i64=0-x; if x>0 { xm=0 } let ym: i64=(xm*NFA_LOG2E)>>16; let yi: i64=ym>>16; let yf: i64=ym-(yi<<16); let g: i64=NFA_Q16-yf; var t: i64=NFA_PC3; t=NFA_PC2+((g*t)>>16); t=NFA_PC1+((g*t)>>16); t=NFA_PC0+((g*t)>>16); t=t>>1; if yi>=31 { return 0 } return t>>yi } |
| 77 | func nfa_sigmoid(x: i64) -> i64 { if x>=0 { let ex: i64=nfa_fxexp(0-x); return (NFA_Q16*NFA_Q16)/(NFA_Q16+ex) } let ex: i64=nfa_fxexp(x); let sp: i64=(NFA_Q16*NFA_Q16)/(NFA_Q16+ex); return NFA_Q16-sp } |
| 78 | func nfa_siluf(x: i64) -> i64 { return nfa_qmul(x, nfa_sigmoid(x)) } // silu(x) = x*sigmoid(x) |
| 79 | func nfa_silud(x: i64) -> i64 { let s: i64=nfa_sigmoid(x); return s + nfa_qmul(x, nfa_qmul(s, NFA_Q16 - s)) } // silu'(x) = s + x*s*(1-s) |
| 81 | func nfa_sinq(x: i64) -> i64 { let x2: i64=nfa_qmul(x,x); let x3: i64=nfa_qmul(x2,x); let x5: i64=nfa_qmul(x3,x2); let x7: i64=nfa_qmul(x5,x2); let x9: i64=nfa_qmul(x7,x2); return x - x3/6 + x5/120 - x7/NFA_MAGIC_5040 + x9/NFA_MAGIC_362880 } |
| 82 | func nfa_cosq(x: i64) -> i64 { let x2: i64=nfa_qmul(x,x); let x4: i64=nfa_qmul(x2,x2); let x6: i64=nfa_qmul(x4,x2); let x8: i64=nfa_qmul(x6,x2); return NFA_Q16 - x2/2 + x4/24 - x6/720 + x8/NFA_MAGIC_40320 } |
| 83 | func nfa_reduce2pi(a: i64) -> i64 { var t: i64=a; while t<0 { t=t+NFA_TWO_PI } while t>=NFA_TWO_PI { t=t-NFA_TWO_PI } return t } |
| 84 | func nfa_sinf(a: i64) -> i64 { let t: i64=nfa_reduce2pi(a); if t<NFA_HALF_PI { return nfa_sinq(t) } if t<NFA_PI { return nfa_sinq(NFA_PI-t) } if t<NFA_3HALF_PI { return 0-nfa_sinq(t-NFA_PI) } return 0-nfa_sinq(NFA_TWO_PI-t) } |
| 85 | func nfa_cosf(a: i64) -> i64 { let t: i64=nfa_reduce2pi(a); if t<NFA_HALF_PI { return nfa_cosq(t) } if t<NFA_PI { return 0-nfa_cosq(NFA_PI-t) } if t<NFA_3HALF_PI { return 0-nfa_cosq(t-NFA_PI) } return nfa_cosq(NFA_TWO_PI-t) } |
| 89 | func nfa_ln(x: i64) -> i64 |
| 105 | func nfa_new(tape: *i64, st: *i64, op: i64, ai: i64, bi: i64, rows: i64, cols: i64) -> i64 |
| 117 | func nfa_leaf(tape: *i64, vals: *i64, st: *i64, rows: i64, cols: i64, src: *i64, soff: i64) -> i64 |
| 127 | func nfa_matvec(tape: *i64, vals: *i64, st: *i64, aW: i64, bx: i64) -> i64 |
| 146 | func nfa_vadd(tape: *i64, vals: *i64, st: *i64, a: i64, b: i64) -> i64 |
| 160 | func nfa_relu(tape: *i64, vals: *i64, st: *i64, a: i64) -> i64 |
| 179 | func nfa_mse(tape: *i64, vals: *i64, st: *i64, pred: i64, target: i64) -> i64 |
| 198 | func nfa_softmax(tape: *i64, vals: *i64, st: *i64, a: i64) -> i64 |
| 212 | func nfa_silu(tape: *i64, vals: *i64, st: *i64, a: i64) -> i64 |
| 221 | func nfa_rmsnorm(tape: *i64, vals: *i64, st: *i64, a: i64) -> i64 |
| 236 | func nfa_matmul(tape: *i64, vals: *i64, st: *i64, a: i64, b: i64) -> i64 |
| 254 | func nfa_matmul_nt(tape: *i64, vals: *i64, st: *i64, a: i64, b: i64) -> i64 |
| 272 | func nfa_cmul(tape: *i64, vals: *i64, st: *i64, a: i64, c_q: i64) -> i64 |
| 281 | func nfa_softmax_rows(tape: *i64, vals: *i64, st: *i64, a: i64, causal: i64) -> i64 |
| 305 | func nfa_rope(tape: *i64, vals: *i64, st: *i64, a: i64) -> i64 |
| 326 | func nfa_hadamard(tape: *i64, vals: *i64, st: *i64, a: i64, b: i64) -> i64 |
| 335 | func nfa_rmsnorm_rows(tape: *i64, vals: *i64, st: *i64, a: i64) -> i64 |
| 354 | func nfa_embed(tape: *i64, vals: *i64, st: *i64, e_node: i64, ids: *i64, T: i64) -> i64 |
| 369 | func nfa_softce_rows(tape: *i64, vals: *i64, st: *i64, logits: i64, tgt: *i64) -> i64 |
| 389 | func nfa_slice_cols(tape: *i64, vals: *i64, st: *i64, a: i64, c0: i64, w: i64) -> i64 |
| 398 | func nfa_concat_cols(tape: *i64, vals: *i64, st: *i64, a: i64, b: i64) -> i64 |
| 413 | func nfa_val(tape: *i64, vals: *i64, k: i64, c: i64) -> i64 { return vals[tape[7*k+5] + c] } |
| 414 | func nfa_grad(tape: *i64, grads: *i64, k: i64, c: i64) -> i64 { return grads[tape[7*k+6] + c] } |
| 417 | func nfa_backward(tape: *i64, vals: *i64, grads: *i64, n: i64, root: i64) -> i64 |
| 727 | func nfa_sgd(w: *i64, g: *i64, n: i64, lr_q: i64) -> i64 |
| 738 | func nfa_sgd_ef(w: *i64, g: *i64, resid: *i64, n: i64, lr_q: i64) -> i64 called by 1: rq_train |
| 754 | func nfa_adamw(w: *i64, g: *i64, m: *i64, v: *i64, n: i64, lr: i64, b1: i64, b2: i64, eps: i64, wd: i64, t: i64) -> i64 |