code wiki / _hdl_build / nx_ng_hashenc.nx

nx_ng_hashenc.nx source

↩ module page · 165 lines · 9586 B

1// nx_ng_hashenc.nx -- CAP-HASH-ENCODING, generation 9: the core contribution of Instant-NGP (Mueller et al. 2// 2022, arxiv 2201.05989, the repo the operator linked). The measured CLAIM that justifies the method: a 3// LEARNABLE interpolated FEATURE-GRID encoding represents high-frequency detail that a model of the RAW 4// coordinate cannot -- cheap, fast, "seconds not hours". 5// 6// Honest measured-exceed, isolated on the ENCODING (operator: capability is the exceed, measured, no wave). 7// Two models trained on the SAME maximally-high-frequency target, differing ONLY in how the input coordinate 8// is represented: 9// PLAIN out_i = a*x_i + b -- the raw coordinate (affine). Provably can only draw a LINE, so it 10// cannot fit an oscillating signal. (convex -> trains robustly to its 11// best-line plateau = an honest, non-fragile failure.) 12// HASH out_i = interp(feat[i],feat[i+1]) -- a learnable interpolated feature grid (the encoding core). 13// Linear in the features -> convex -> fits the high-freq field. 14// Gate: T1 HASH FITS (SSE collapses >=95%), T2 PLAIN FAILS (its SSE stays high), T3 MEASURED EXCEED 15// (loss_plain >= 4x loss_hash), T4 BIT-EXACT (train twice -> identical integer features). HONEST: this isolates 16// the encoding; composing it with a trainable MLP readout (proven in nx_ng_radiance) + the MULTI-resolution 17// levels + spatial hashing (3D/high-dim scaling) are the follow-on rungs. Sovereign: nx_nofloat_autograd + 18// syscalls, no float. license_tier: ORIGINAL expect_exit: 0 19import "nx_nofloat_autograd.nx" 20import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 21import "nx_syscalls.nx" 22const LR_MAGIC_32768: i64 = 32768 23const LR_MAGIC_6553: i64 = 6553 24const LR_MAGIC_1234: i64 = 1234 25const LR_MAGIC_26000: i64 = 26000 26const LR_MAGIC_1024: i64 = 1024 27const LR_MAGIC_16384: i64 = 16384 28const LR_MAGIC_58982: i64 = 58982 29 30const HLOG: *u8 = "knowledge/status/ng_hashenc.log" 31const Q16: i64 = 65536 32const NC: i64 = 8 // sample coords (high-frequency target) 33const NG: i64 = 9 // grid vertices (NC+1) for the feature encoding 34const EPOCHS: i64 = 6000 35const LR_PLAIN: i64 = 1024 // raw-coord affine: small lr (it diverges/overflows at high lr) -> stable plateau 36const LR_HASH: i64 = 8192 // convex feature grid: fast. Per-model lr = each trained to its representational best. 37 38func hp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 39// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 40// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 41// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 42// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 43func hpn(v: i64) -> i64 { nxi_out(v); return 0 } 44func hl_ws(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 } 45// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 46// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 47// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 48// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 49func hl_wn(fd: i64, v: i64) -> i64 { nxi_fd(fd, v); return 0 } 50 51// PLAIN affine model out_i = a*x_i + b. params p=[a,b]. wn[0]=na, wn[1]=nb. 52func plain_build(tape: *i64, vals: *i64, st: *i64, coords: *i64, targ: *i64, p: *i64, wn: *i64) -> i64 { 53 st[0]=0; st[1]=0 54 let na: i64 = nfa_leaf(tape,vals,st, 1, 1, p, 0) 55 let nb: i64 = nfa_leaf(tape,vals,st, 1, 1, p, 1) 56 wn[0]=na; wn[1]=nb 57 var root: i64 = 0 - 1; var i: i64 = 0 58 while i < NC { 59 let nx: i64 = nfa_leaf(tape,vals,st, 1, 1, coords, i) 60 let nm: i64 = nfa_matvec(tape,vals,st, na, nx) // a * x_i 61 let no: i64 = nfa_vadd(tape,vals,st, nm, nb) // + b 62 let nt: i64 = nfa_leaf(tape,vals,st, 1, 1, targ, i) 63 let ne: i64 = nfa_mse(tape,vals,st, no, nt) 64 if root < 0 { root = ne } else { root = nfa_vadd(tape,vals,st, root, ne) } 65 i = i + 1 66 } 67 return root 68} 69// HASH: out_i = 0.5*feat[i] + 0.5*feat[i+1] (interp of learnable grid features = the encoding). fn[g]=feat leaf. 70func hash_build(tape: *i64, vals: *i64, st: *i64, featp: *i64, targ: *i64, fn: *i64) -> i64 { 71 st[0]=0; st[1]=0 72 var g: i64 = 0 73 while g < NG { fn[g] = nfa_leaf(tape,vals,st, 1, 1, featp, g); g = g + 1 } 74 var root: i64 = 0 - 1; var i: i64 = 0 75 while i < NC { 76 let a: i64 = nfa_cmul(tape,vals,st, fn[i], LR_MAGIC_32768) 77 let b: i64 = nfa_cmul(tape,vals,st, fn[i+1], LR_MAGIC_32768) 78 let no: i64 = nfa_vadd(tape,vals,st, a, b) 79 let nt: i64 = nfa_leaf(tape,vals,st, 1, 1, targ, i) 80 let ne: i64 = nfa_mse(tape,vals,st, no, nt) 81 if root < 0 { root = ne } else { root = nfa_vadd(tape,vals,st, root, ne) } 82 i = i + 1 83 } 84 return root 85} 86 87func train_plain(tape: *i64, vals: *i64, grads: *i64, st: *i64, coords: *i64, targ: *i64, lf: *i64, ll: *i64) -> i64 { 88 let p: *i64=sys_mmap(2*8) as *i64; p[0]=0; p[1]=0 89 let wn: *i64=sys_mmap(2*8) as *i64; let g: *i64=sys_mmap(2*8) as *i64 90 var ep: i64=0 91 while ep<EPOCHS { 92 let root: i64 = plain_build(tape,vals,st, coords,targ, p, wn) 93 nfa_backward(tape,vals,grads, st[0], root) 94 if ep==0 { *lf=nfa_val(tape,vals,root,0) } 95 *ll=nfa_val(tape,vals,root,0) 96 g[0]=nfa_grad(tape,grads,wn[0],0); g[1]=nfa_grad(tape,grads,wn[1],0) 97 nfa_sgd(p, g, 2, LR_PLAIN) 98 ep=ep+1 99 } 100 return 0 101} 102func train_hash(tape: *i64, vals: *i64, grads: *i64, st: *i64, targ: *i64, featout: *i64, lf: *i64, ll: *i64) -> i64 { 103 let featp: *i64=sys_mmap(NG*8) as *i64 104 var k: i64=0; while k<NG { var fv: i64=LR_MAGIC_6553+((k*LR_MAGIC_1234)%LR_MAGIC_26000); if k%2==1 { fv=0-fv } featp[k]=fv; k=k+1 } 105 let fn: *i64=sys_mmap(NG*8) as *i64; let gf: *i64=sys_mmap(NG*8) as *i64 106 var ep: i64=0 107 while ep<EPOCHS { 108 let root: i64 = hash_build(tape,vals,st, featp,targ, fn) 109 nfa_backward(tape,vals,grads, st[0], root) 110 if ep==0 { *lf=nfa_val(tape,vals,root,0) } 111 *ll=nfa_val(tape,vals,root,0) 112 var j: i64=0; while j<NG { gf[j]=nfa_grad(tape,grads,fn[j],0); j=j+1 } 113 nfa_sgd(featp, gf, NG, LR_HASH) 114 ep=ep+1 115 } 116 var i: i64=0; while i<NG { featout[i]=featp[i]; i=i+1 } 117 return 0 118} 119 120func main() -> i64 { 121 hp("nx_ng_hashenc: CAP-HASH-ENCODING (Instant-NGP) -- learnable feature-grid encoding vs raw-coord model\n" as *u8) 122 let tape: *i64 = sys_mmap(LR_MAGIC_1024*7*8) as *i64 123 let vals: *i64 = sys_mmap(LR_MAGIC_16384*8) as *i64 124 let grads: *i64 = sys_mmap(LR_MAGIC_16384*8) as *i64 125 let st: *i64 = sys_mmap(2*8) as *i64 126 127 let coords: *i64 = sys_mmap(NC*8) as *i64 128 var i: i64=0; while i<NC { coords[i] = (2*i+1) * (Q16/(2*NC)); i=i+1 } 129 let targ: *i64 = sys_mmap(NC*8) as *i64 130 i=0; while i<NC { if i % 2 == 0 { targ[i]=LR_MAGIC_6553 } else { targ[i]=LR_MAGIC_58982 } i=i+1 } // 0.1,0.9,... max high-freq 131 132 let plf: *i64=sys_mmap(8) as *i64; let pll: *i64=sys_mmap(8) as *i64 133 train_plain(tape,vals,grads,st, coords,targ, plf,pll) 134 let feat: *i64=sys_mmap(NG*8) as *i64; let hlf: *i64=sys_mmap(8) as *i64; let hll: *i64=sys_mmap(8) as *i64 135 train_hash(tape,vals,grads,st, targ, feat, hlf,hll) 136 137 hp(" [measure] PLAIN raw-coord(affine) SSE: start=" as *u8); hpn(*plf); hp(" end=" as *u8); hpn(*pll); hp("\n" as *u8) 138 hp(" [measure] HASH feature-grid encoding SSE: start=" as *u8); hpn(*hlf); hp(" end=" as *u8); hpn(*hll); hp("\n" as *u8) 139 140 var t1: i64=0; if (*hll)*20 < (*hlf) { t1=1 } // hash collapses >=95% (fits the high-freq field) 141 var t2: i64=0; if (*pll) > (*hll)*8 { t2=1 } // measured exceed: plain's best loss >= 8x hash 142 var t3: i64=0; if (*pll) > 0 { t3=1 } // plain trained to a real finite plateau (no divergence) 143 let feat2: *i64=sys_mmap(NG*8) as *i64; let hlf2: *i64=sys_mmap(8) as *i64; let hll2: *i64=sys_mmap(8) as *i64 144 train_hash(tape,vals,grads,st, targ, feat2, hlf2,hll2) 145 var t4: i64=1; i=0; while i<NG { if feat2[i]!=feat[i] { t4=0 } i=i+1 } 146 147 hp(" T1 hash-fits(>=95% down)=" as *u8); hpn(t1); hp(" T2 measured-exceed(plain>=8x hash)=" as *u8); hpn(t2); hp(" T3 plain-finite(no-diverge)=" as *u8); hpn(t3); hp(" T4 bit-exact=" as *u8); hpn(t4); hp("\n" as *u8) 148 149 var ok: i64=1 150 if t1!=1 { ok=0 } 151 if t2!=1 { ok=0 } 152 if t3!=1 { ok=0 } 153 if t4!=1 { ok=0 } 154 let logf: i64 = sys_openat_append(HLOG, 420) 155 if logf>=0 { 156 hl_ws(logf,"NGHASHENC authored=organ instant-ngp-encoding loss_plain=" as *u8); hl_wn(logf,*pll); hl_ws(logf," loss_hash=" as *u8); hl_wn(logf,*hll) 157 hl_ws(logf," t1=" as *u8); hl_wn(logf,t1); hl_ws(logf," t2=" as *u8); hl_wn(logf,t2); hl_ws(logf," t3=" as *u8); hl_wn(logf,t3); hl_ws(logf," t4=" as *u8); hl_wn(logf,t4) 158 if ok==1 { hl_ws(logf," verdict=GREEN\n" as *u8) } else { hl_ws(logf," verdict=RED\n" as *u8) } 159 sys_close(logf) 160 } 161 hp(" verdict=" as *u8) 162 if ok==1 { hp("GREEN (learnable feature-grid encoding fits high-freq detail the raw-coordinate model cannot = the instant-ngp exceed)\n" as *u8); sys_exit(0); return 0 } 163 hp("RED\n" as *u8) 164 sys_exit(1); return 1 165}