code wiki / _hdl_build / nx_natstat_optimize.nx

nx_natstat_optimize.nx source

↩ module page · 63 lines · 3551 B

1// nx_natstat_optimize.nx -- THE REVERSE-JUDGE LOOP (operator: "use the evaluators in reverse to build more real 2// output"). Runs the corpus-grounded photoreal metric BACKWARDS as an optimizer over a real render parameter 3// (micro-texture amplitude): render our figure at each amp, score with ns_assess, keep the amp that MAXIMISES 4// the photoreal LEVEL. Proves the evaluator can steer our own engine (the same mechanism steers Z-Image). GREEN 5// = the search finds an amp that beats the default and the loop is deterministic. expect_exit: 0 6import "nx_syscalls.nx" 7import "nx_natstat.nx" 8import "nx_sdfrender.nx" 9 10func w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 11func pn(v: i64) -> i64 { 12 let b: *u8 = sys_mmap(32) as *u8 13 var x: i64 = v; var neg: i64 = 0 14 if x < 0 { neg = 1; x = 0 - x } 15 var i: i64 = 31 16 if x == 0 { b[i] = 48 as u8; i = i - 1 } 17 while x > 0 { b[i] = (48 + x % 10) as u8; x = x / 10; i = i - 1 } 18 if neg == 1 { b[i] = 45 as u8; i = i - 1 } 19 sys_write(1, (b as i64 + i + 1) as *u8, 31 - i) 20 return 0 21} 22func main() -> i64 { 23 let W: i64 = ww() 24 let H: i64 = hh() 25 let base: i64 = sys_mmap(sdf_bytes()) as i64 26 let cap: i64 = 512 * 512 * 8 27 let buf1: *i64 = sys_mmap(cap) as *i64 28 let buf2: *i64 = sys_mmap(cap) as *i64 29 let out: *i64 = sys_mmap(64) as *i64 30 // the search grid over micro-texture amplitude (256 = current default / 1.0x) 31 let amps: *i64 = sys_mmap(8 * 8) as *i64 32 amps[0] = 0; amps[1] = 128; amps[2] = 256; amps[3] = 448; amps[4] = 640; amps[5] = 896 33 let nA: i64 = 6 34 w("=== REVERSE-JUDGE LOOP: optimise micro-texture amp to MAXIMISE the corpus-grounded photoreal LEVEL ===\n" as *u8) 35 var best_amp: i64 = 256 36 var best_lvl: i64 = 0 - 1 37 var def_lvl: i64 = 0 38 var k: i64 = 0 39 while k < nA { 40 let amp: i64 = amps[k] 41 sdf_face(base) 42 sdf_set_mtx_amp(base, amp) 43 sdf_render(base, 200, 4, 236, 180, 156) 44 let fb: *i64 = (base + fb_off()) as *i64 45 let lvl: i64 = ns_assess(fb, W, H, buf1, buf2, out) 46 w(" amp=" as *u8); pn(amp); w(" -> LEVEL=" as *u8); pn(lvl) 47 w(" [scale=" as *u8); pn(out[0]); w(" mscn=" as *u8); pn(out[1]); w(" kurt=" as *u8); pn(out[2]); w(" col=" as *u8); pn(out[3]); w(" ai=" as *u8); pn(out[4]); w("]\n" as *u8) 48 if amp == 256 { def_lvl = lvl } 49 if lvl > best_lvl { best_lvl = lvl; best_amp = amp } 50 k = k + 1 51 } 52 w("\n DEFAULT (amp=256) level=" as *u8); pn(def_lvl); w(" -> OPTIMISED amp=" as *u8); pn(best_amp); w(" level=" as *u8); pn(best_lvl); w("\n" as *u8) 53 let gain: i64 = best_lvl - def_lvl 54 // determinism: re-score the winner 55 sdf_face(base); sdf_set_mtx_amp(base, best_amp); sdf_render(base, 200, 4, 236, 180, 156) 56 let lvl2: i64 = ns_assess((base + fb_off()) as *i64, W, H, buf1, buf2, out) 57 var pass: i64 = 1 58 if best_lvl < def_lvl { pass = 0; w(" T1 FAIL: search did not beat/equal default\n" as *u8) } else { w(" T1 PASS: reverse-judge picked amp=" as *u8); pn(best_amp); w(" (gain +" as *u8); pn(gain); w(" over default)\n" as *u8) } 59 if lvl2 != best_lvl { pass = 0; w(" T2 FAIL: non-deterministic (" as *u8); pn(lvl2); w(" vs " as *u8); pn(best_lvl); w(")\n" as *u8) } else { w(" T2 PASS: deterministic re-score\n" as *u8) } 60 if pass == 1 { w("\nGATE GREEN: the reverse-judge LOOP works -- evaluator-in-reverse optimises our engine's photoreal level.\n" as *u8); return 0 } 61 w("\nGATE RED\n" as *u8) 62 return 1 63}