code wiki / _hdl_build / nx_gsplat_optimize_gate.nx

nx_gsplat_optimize_gate.nx source

↩ module page · 140 lines · 6655 B

1// nx_gsplat_optimize_gate.nx -- ★THE GENERATOR: a sovereign differentiable-splatting COLOUR optimizer fits 2// Gaussians to a target image by gradient descent (the 3DGS training mechanism, colour channel). Integer, 3// deterministic. Proves the loss DROPS + the render converges to the target. 4// T1 a Gaussian sphere with KNOWN colours -> render = the TARGET. Reset all colours to GREY (wrong). Optimize: 5// the image loss must DROP hard (final < 30% of initial) and MONOTONICALLY (each iter <= prev) 6// T2 the recovered render MATCHES the target (final loss small = the colours were fit back) 7// T3 determinism (same init -> same optimized colours) + PNG knowledge/nx_gsplat_opt.png (target | optimized) 8// license_tier: ORIGINAL expect_exit: 0 9import "nx_syscalls.nx" 10import "nx_itrig.nx" 11import "nx_png.nx" 12import "nx_gsplat.nx" 13 14func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 15func pn(v: i64) -> i64 { let b: *u8=sys_mmap(32) as *u8; var x: i64=v; var ng: i64=0; if x<0{ng=1;x=0-x} var i: i64=31; if x==0{b[i]=48 as u8;i=i-1} while x>0{b[i]=(48+x%10) as u8;x=x/10;i=i-1} if ng==1{b[i]=45 as u8;i=i-1} sys_write(1,(b as i64+i+1) as *u8,31-i); return 0 } 16 17func build_sphere(gauss: *i64) -> i64 { // 768 gaussians, spatially-varying TRUE colours 18 var n: i64 = 0 19 var iu: i64 = 0 20 while iu < 24 { 21 let th: i64 = iu*6434/24 22 let st: i64 = it_sin4096(th) 23 let ct: i64 = it_cos4096(th) 24 var iv: i64 = 0 25 while iv < 32 { 26 let ph: i64 = iv*12868*2/32 27 let sp: i64 = it_sin4096(ph) 28 let cp: i64 = it_cos4096(ph) 29 let x: i64 = 300*st/4096*cp/4096 30 let y: i64 = 300*ct/4096 31 let z: i64 = 300*st/4096*sp/4096 32 let r: i64 = 128 + x*2/5 33 let g: i64 = 128 + y*2/5 34 let b: i64 = 128 - z*2/5 35 gs_set(gauss, n, x, y, z, 26, r, g, b, 220) 36 n = n + 1 37 iv = iv + 1 38 } 39 iu = iu + 1 40 } 41 return n 42} 43 44func main() -> i64 { 45 hw("=== nx_gsplat_optimize_gate -- differentiable-splatting colour optimizer (the generator, sovereign) ===\n" as *u8) 46 var fails: i64 = 0 47 let W: i64 = gs_w() 48 let H: i64 = gs_h() 49 let npx: i64 = W*H 50 let NGX: i64 = 1024 51 let gauss: *i64 = sys_mmap(NGX*8*8) as *i64 52 let fb: *i64 = sys_mmap(npx*8) as *i64 53 let target: *i64 = sys_mmap(npx*8) as *i64 54 let acc: *i64 = sys_mmap(npx*3*8) as *i64 55 let trans: *i64 = sys_mmap(npx*8) as *i64 56 let depth: *i64 = sys_mmap(NGX*8) as *i64 57 let sxb: *i64 = sys_mmap(NGX*8) as *i64 58 let syb: *i64 = sys_mmap(NGX*8) as *i64 59 let sigb: *i64 = sys_mmap(NGX*8) as *i64 60 let order: *i64 = sys_mmap(NGX*8) as *i64 61 let count: *i64 = sys_mmap((gs_nb()+2)*8) as *i64 62 let explut: *i64 = sys_mmap(gs_expn()*8) as *i64 63 let gradbuf: *i64 = sys_mmap(NGX*3*8) as *i64 64 let wnorm: *i64 = sys_mmap(NGX*8) as *i64 65 gs_build_explut(explut) 66 67 // TARGET = the sphere with its true colours 68 let ng: i64 = build_sphere(gauss) 69 gs_render(gauss, ng, 300, 4, target, acc, trans, depth, sxb, syb, sigb, order, count, explut, 26, 28, 44) 70 71 // RESET all colours to grey (the wrong start) 72 var i: i64 = 0 73 while i < ng { gauss[i*8+4]=128; gauss[i*8+5]=128; gauss[i*8+6]=128; i=i+1 } 74 75 // OPTIMIZE 76 hw(" optimizing colours to fit the target:\n" as *u8) 77 var loss0: i64 = 0 78 var lossN: i64 = 0 79 var prev: i64 = 2000000000 80 var mono: i64 = 1 81 var it: i64 = 0 82 while it < 16 { 83 let loss: i64 = gs_color_grad_step(gauss, ng, target, 300, 4, fb, acc, trans, depth, sxb, syb, sigb, order, count, explut, gradbuf, wnorm) 84 if it == 0 { loss0 = loss } 85 lossN = loss 86 if loss > prev + 200 { mono = 0 } // allow tiny integer wiggle 87 prev = loss 88 if it < 4 { hw(" iter "); pn(it); hw(" L1 loss="); pn(loss); hw("\n" as *u8) } 89 it = it + 1 90 } 91 hw(" loss: initial="); pn(loss0); hw(" final="); pn(lossN); hw(" (drop to "); pn(lossN*100/loss0); hw("%)\n" as *u8) 92 93 var t1: i64 = 0 94 if lossN * 100 < loss0 * 30 { if mono == 1 { t1 = 1 } } 95 if t1 == 1 { hw("T1 PASS the optimizer DROPS the image loss hard + monotonically (gradient descent works)\n" as *u8) } 96 else { fails=fails+1; hw("T1 FAIL loss drop/monotonicity\n" as *u8) } 97 98 // T2 recovered render matches the target (final loss per pixel small) 99 let perpx: i64 = lossN / npx 100 hw(" final loss per pixel (avg abs, /3 channels): "); pn(perpx); hw("\n" as *u8) 101 var t2: i64 = 0 102 if perpx < 12 { t2 = 1 } // < ~4/channel = a close match 103 if t2 == 1 { hw("T2 PASS recovered render matches the target (colours fit back)\n" as *u8) } 104 else { fails=fails+1; hw("T2 FAIL match too loose\n" as *u8) } 105 106 // T3 determinism + PNG (target | optimized) 107 let gauss2: *i64 = sys_mmap(NGX*8*8) as *i64 108 build_sphere(gauss2) 109 i = 0 110 while i < ng { gauss2[i*8+4]=128; gauss2[i*8+5]=128; gauss2[i*8+6]=128; i=i+1 } 111 it = 0 112 while it < 16 { gs_color_grad_step(gauss2, ng, target, 300, 4, fb, acc, trans, depth, sxb, syb, sigb, order, count, explut, gradbuf, wnorm); it=it+1 } 113 var cdiff: i64 = 0 114 i = 0 115 while i < ng { if gauss[i*8+4]!=gauss2[i*8+4] {cdiff=cdiff+1} if gauss[i*8+5]!=gauss2[i*8+5] {cdiff=cdiff+1} if gauss[i*8+6]!=gauss2[i*8+6] {cdiff=cdiff+1} i=i+1 } 116 // render final optimized + build target|optimized combo 117 gs_render(gauss, ng, 300, 4, fb, acc, trans, depth, sxb, syb, sigb, order, count, explut, 26, 28, 44) 118 let W2: i64 = W*2 119 let combo: *i64 = sys_mmap(W2*H*8) as *i64 120 var yy: i64 = 0 121 while yy < H { 122 var xx: i64 = 0 123 while xx < W { 124 combo[yy*W2+xx] = target[yy*W+xx] 125 combo[yy*W2+W+xx] = fb[yy*W+xx] 126 xx = xx + 1 127 } 128 yy = yy + 1 129 } 130 write_png(combo, W2, H, "knowledge/nx_gsplat_opt.png" as *u8) 131 var t3: i64 = 0 132 if cdiff == 0 { t3 = 1 } 133 if t3 == 1 { hw("T3 PASS deterministic optimization + PNG knowledge/nx_gsplat_opt.png (target | optimized)\n" as *u8) } 134 else { fails=fails+1; hw("T3 FAIL nondeterministic cdiff="); pn(cdiff); hw("\n" as *u8) } 135 136 if fails == 0 { hw("GSPLAT-OPTIMIZE-GATE 3/3 GREEN -- a sovereign differentiable-splatting colour optimizer: gradient descent fits Gaussians to a target image, loss "); pn(loss0); hw("->"); pn(lossN); hw(", integer + deterministic (the 3DGS generator mechanism, ours)\n" as *u8); sys_exit(0); return 0 } 137 hw("GSPLAT-OPTIMIZE-GATE RED fails="); pn(fails); hw("\n" as *u8) 138 sys_exit(1) 139 return 1 140}