code wiki / _hdl_build / nx_autograd_gate.nx

nx_autograd_gate.nx

buildroot/runtime/_hdl_build/nx_autograd_gate.nx

7321 B108 linesdepth 3pulls 3 transitivereach 0 importersview sourcekind gate/prooftopic autograd
docsdependenciesstructsconstsfunctions

about

nx_autograd_gate.nx -- SOVEREIGN general reverse-mode AUTOGRAD v1 (the R3.5 rung the substrate audit exposed: nx_nn_train only does an ANALYTIC linear-layer gradient; deep learned transforms need general backprop through nonlinearities). This is a fixed-point (Q16) computational-graph autodiff: a tape of nodes (val,grad,op,inputs), eager forward on build, and a reverse pass that accumulates gradients via the chain rule through ADD/MUL/RELU. Reverse-mode autodiff is foundational 1970s math (Linnainmaa) -- unpatentable -- so this is FTO-clean by construction and research-independent. LIAR-KILL = NUMERICAL GRADIENT CHECK: autograd grads must match central finite differences (a wrong backward cannot match). No FPU/GPU/3rd-party autograd; nx_cc->nxasm. license_tier: ORIGINAL

dependencies 2 imports · 0 importers

nx_syscalls.nx nx_gate_emit_lib.nx nx_autograd_gate.nx

imports: nx_syscalls.nxnx_gate_emit_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 build_L ag_leaf ag_mul ag_add ag_relu ag_backward numgrad_a build_L ↻ numgrad_b build_L ↻ g_num sys_mmap ↻ sys_write ↻ g_check g_puts ↻ close iabs sys_openat_append g_w sys_write ↻ g_wn sys_mmap ↻ sys_write ↻ sys_close sys_exit

structs

none

consts

13const Q: i64 = 65536

functions

15func g_num(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=(48 as u8);k=1}; while m>0{t[k]=((48+(m%10)) as u8);m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(1,bb,k); return 0 }
called by 1: main calls 2: sys_mmapsys_write
16func g_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 }
called by 1: main calls 1: sys_write
17func g_wn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=(48 as u8);k=1}; while m>0{t[k]=((48+(m%10)) as u8);m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(fd,bb,k); return 0 }
called by 1: main calls 2: sys_mmapsys_write
18func iabs(v: i64) -> i64 { if v<0 { return 0-v } return v }
called by 1: close
21func ag_leaf(tp: *i64, np: *i64, v: i64) -> i64 { let k: i64=np[0]; tp[k*5]=v; tp[k*5+2]=0; tp[k*5+3]=0-1; tp[k*5+4]=0-1; np[0]=k+1; return k }
called by 1: build_L
22func ag_add(tp: *i64, np: *i64, i: i64, j: i64) -> i64 { let k: i64=np[0]; tp[k*5]=tp[i*5]+tp[j*5]; tp[k*5+2]=1; tp[k*5+3]=i; tp[k*5+4]=j; np[0]=k+1; return k }
called by 1: build_L
23func ag_mul(tp: *i64, np: *i64, i: i64, j: i64) -> i64 { let k: i64=np[0]; tp[k*5]=(tp[i*5]*tp[j*5])>>16; tp[k*5+2]=2; tp[k*5+3]=i; tp[k*5+4]=j; np[0]=k+1; return k }
called by 1: build_L
24func ag_relu(tp: *i64, np: *i64, i: i64) -> i64 { let k: i64=np[0]; var v: i64=tp[i*5]; if v<0 { v=0 } tp[k*5]=v; tp[k*5+2]=3; tp[k*5+3]=i; tp[k*5+4]=0-1; np[0]=k+1; return k }
called by 1: build_L
26func ag_backward(tp: *i64, np: *i64, out: i64) -> i64
called by 1: main
40func build_L(tp: *i64, np: *i64, av: i64, bv: i64) -> i64
51func numgrad_a(tp: *i64, np: *i64, av: i64, bv: i64, eps: i64) -> i64
called by 1: main calls 1: build_L
56func numgrad_b(tp: *i64, np: *i64, av: i64, bv: i64, eps: i64) -> i64
called by 1: main calls 1: build_L
62func close(auto: i64, num: i64) -> i64 { let d: i64=iabs(auto-num); let tol: i64=(iabs(auto)*5)/100 + 64; if d<=tol { return 1 } return 0 }
called by 1: main calls 1: iabs
64func main() -> i64