code wiki / _hdl_build / nx_machine_gen.nx
nx_machine_gen.nx source
↩ module page · 99 lines · 9232 B
1// nx_machine_gen.nx -- MACHINE GENERATOR (toward the team authoring its own substrate):
2// from a compact DATA spec (op-fragments + (a,b,y) examples) it EMITS a complete, working
3// evo-deriver to runtime/_hdl_build/nx_gen_machine.nx. The emitted machine, when built+run,
4// SEARCHES op-sequences to fit the examples and self-verifies on held-out -- i.e. a NEW
5// machine is authored from DATA, not hand-written. Claude authors THIS generator (the meta-
6// substrate, like the compiler/genesis seed); machines become data-authored = the real
7// reduction of Claude from the substrate. Demo spec derives f(a,b)=a*b+a. license_tier: ORIGINAL
8import "nx_syscalls.nx"
9import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
10const K_MAGIC_32768: i64 = 32768
11
12const NOPS: i64 = 6
13const NEX: i64 = 10
14
15// THE SPEC (data the team supplies): op-fragments + examples. Change these = a new machine.
16func mg_op(k: i64) -> *u8 {
17 if k == 0 { return "acc = a" as *u8 }
18 if k == 1 { return "acc = b" as *u8 }
19 if k == 2 { return "acc = acc * a" as *u8 }
20 if k == 3 { return "acc = acc * b" as *u8 }
21 if k == 4 { return "acc = acc + a" as *u8 }
22 return "acc = acc + b" as *u8
23}
24func mg_xa(i: i64) -> i64 { if i==0 {return 2} if i==1 {return 4} if i==2 {return 3} if i==3 {return 5} if i==4 {return 6} if i==5 {return 1} if i==6 {return 7} if i==7 {return 3} if i==8 {return 8} return 4 }
25func mg_xb(i: i64) -> i64 { if i==0 {return 3} if i==1 {return 5} if i==2 {return 2} if i==3 {return 1} if i==4 {return 2} if i==5 {return 7} if i==6 {return 2} if i==7 {return 3} if i==8 {return 1} return 4 }
26func mg_xy(i: i64) -> i64 { if i==0 {return 8} if i==1 {return 24} if i==2 {return 9} if i==3 {return 10} if i==4 {return 18} if i==5 {return 8} if i==6 {return 21} if i==7 {return 12} if i==8 {return 16} return 20 }
27
28func mg_w(buf: *u8, off: i64, s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { buf[off+i] = s[i]; i = i + 1 } return off + i }
29func mg_wn(buf: *u8, off: i64, v: i64) -> i64 {
30 var o: i64 = off
31 if v == 0 { buf[o] = 48 as u8; return o + 1 }
32 var m: i64 = v; let t: *u8 = sys_mmap(28); var k: i64 = 0
33 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
34 var i: i64 = 0
35 while i < k { buf[o+i] = t[k-1-i]; i = i + 1 }
36 return o + k
37}
38func mg_pr(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 mg_prn(v: i64) -> i64 { nxi_out(v); return 0 }
44
45// emit a "func <name>(i: i64) -> i64 { if i==K { return <val(K)> } ... return 0 }" table.
46func mg_table(buf: *u8, off: i64, name: *u8, which: i64) -> i64 {
47 var o: i64 = off
48 o = mg_w(buf, o, "func " as *u8); o = mg_w(buf, o, name); o = mg_w(buf, o, "(i: i64) -> i64 {\n" as *u8)
49 var i: i64 = 0
50 while i < NEX {
51 o = mg_w(buf, o, " if i == " as *u8); o = mg_wn(buf, o, i); o = mg_w(buf, o, " { return " as *u8)
52 var v: i64 = 0
53 if which == 0 { v = mg_xa(i) }
54 if which == 1 { v = mg_xb(i) }
55 if which == 2 { v = mg_xy(i) }
56 o = mg_wn(buf, o, v); o = mg_w(buf, o, " }\n" as *u8)
57 i = i + 1
58 }
59 o = mg_w(buf, o, " return 0\n}\n" as *u8)
60 return o
61}
62
63func main() -> i64 {
64 let buf: *u8 = sys_mmap(K_MAGIC_32768); var o: i64 = 0
65 o = mg_w(buf, o, "// AUTHORED BY nx_machine_gen from a DATA spec (op-fragments + examples) -- a machine the team authored from DATA, not hand-written. license_tier: ORIGINAL\n" as *u8)
66 o = mg_w(buf, o, "import \"nx_syscalls.nx\"\n" as *u8)
67 o = mg_w(buf, o, "const GLEN: i64 = 4\nconst EVO_P: i64 = 256\nconst EVO_G: i64 = 2000\nconst EVO_T: i64 = 5\nconst NTRAIN: i64 = 6\nconst NHELD: i64 = 4\n" as *u8)
68 o = mg_w(buf, o, "func gm_rand(s: *i64) -> i64 { s[0] = s[0] * 2862933555777941757 + 3037000493; return (s[0] >> 17) & 0x3fffffff }\n" as *u8)
69 o = mg_w(buf, o, "func gm_abs(x: i64) -> i64 { if x < 0 { return 0 - x } return x }\n" as *u8)
70 o = mg_table(buf, o, "gm_xa" as *u8, 0)
71 o = mg_table(buf, o, "gm_xb" as *u8, 1)
72 o = mg_table(buf, o, "gm_xy" as *u8, 2)
73 o = mg_w(buf, o, "func gm_eval(pop: *i64, base: i64, a: i64, b: i64) -> i64 {\n var acc: i64 = 0\n var j: i64 = 0\n while j < GLEN {\n let op: i64 = pop[base + j]\n" as *u8)
74 var k: i64 = 0
75 while k < NOPS {
76 o = mg_w(buf, o, " if op == " as *u8); o = mg_wn(buf, o, k); o = mg_w(buf, o, " { " as *u8); o = mg_w(buf, o, mg_op(k)); o = mg_w(buf, o, " }\n" as *u8)
77 k = k + 1
78 }
79 o = mg_w(buf, o, " j = j + 1\n }\n return acc\n}\n" as *u8)
80 o = mg_w(buf, o, "func gm_fit(pop: *i64, base: i64) -> i64 {\n var err: i64 = 0\n var i: i64 = 0\n while i < NTRAIN {\n let r: i64 = gm_eval(pop, base, gm_xa(i), gm_xb(i))\n var d: i64 = 2000000\n if r >= 0 { if r <= 1000000 { d = gm_abs(r - gm_xy(i)) } }\n err = err + d\n i = i + 1\n }\n return err\n}\n" as *u8)
81 o = mg_w(buf, o, "func gm_tourney(fit: *i64, state: *i64) -> i64 {\n var bi: i64 = gm_rand(state) % EVO_P\n var bd: i64 = fit[bi]\n var k: i64 = 1\n while k < EVO_T { let i: i64 = gm_rand(state) % EVO_P; if fit[i] < bd { bd = fit[i]; bi = i } k = k + 1 }\n return bi\n}\n" as *u8)
82 o = mg_w(buf, o, "func gm_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }\n" as *u8)
83 o = mg_w(buf, o, "func gm_pn(v: i64) -> i64 { if v == 0 { sys_write(1, \"0\" as *u8, 1); return 0 } var m: i64 = v; var k: i64 = 0; let t: *u8 = sys_mmap(32); while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } while k > 0 { k = k - 1; sys_write(1, (((t as i64)+k) as *u8), 1) } return 0 }\n" as *u8)
84 o = mg_w(buf, o, "func main() -> i64 {\n let state: *i64 = sys_mmap(8) as *i64\n state[0] = 7 * 2654435761 + 12345\n let pop: *i64 = sys_mmap(EVO_P * GLEN * 8) as *i64\n let nxt: *i64 = sys_mmap(EVO_P * GLEN * 8) as *i64\n let fit: *i64 = sys_mmap(EVO_P * 8) as *i64\n let best: *i64 = sys_mmap(GLEN * 8) as *i64\n" as *u8)
85 o = mg_w(buf, o, " var p: i64 = 0\n while p < EVO_P { var j: i64 = 0; while j < GLEN { pop[p*GLEN + j] = gm_rand(state) % 6; j = j + 1 } p = p + 1 }\n" as *u8)
86 o = mg_w(buf, o, " var best_err: i64 = 1000000000\n var found: i64 = 0\n var g: i64 = 0\n while g < EVO_G {\n var gbest: i64 = 1000000000\n var gbp: i64 = 0\n p = 0\n while p < EVO_P { let e: i64 = gm_fit(pop, p*GLEN); fit[p] = e; if e < gbest { gbest = e; gbp = p } p = p + 1 }\n if gbest < best_err { best_err = gbest; var j: i64 = 0; while j < GLEN { best[j] = pop[gbp*GLEN + j]; j = j + 1 } }\n if best_err == 0 { found = 1; g = EVO_G } else {\n" as *u8)
87 o = mg_w(buf, o, " var j2: i64 = 0\n while j2 < GLEN { nxt[j2] = best[j2]; j2 = j2 + 1 }\n p = 1\n while p < EVO_P {\n let pa: i64 = gm_tourney(fit, state) * GLEN\n let pb: i64 = gm_tourney(fit, state) * GLEN\n let cut: i64 = (gm_rand(state) % (GLEN - 1)) + 1\n var jj: i64 = 0\n while jj < GLEN { if jj < cut { nxt[p*GLEN + jj] = pop[pa + jj] } else { nxt[p*GLEN + jj] = pop[pb + jj] } jj = jj + 1 }\n let slot: i64 = gm_rand(state) % GLEN\n nxt[p*GLEN + slot] = gm_rand(state) % 6\n p = p + 1\n }\n var c: i64 = 0\n while c < EVO_P * GLEN { pop[c] = nxt[c]; c = c + 1 }\n g = g + 1\n }\n }\n" as *u8)
88 o = mg_w(buf, o, " var held_ok: i64 = 0\n var hi: i64 = 0\n while hi < NHELD { let idx: i64 = NTRAIN + hi; if gm_eval(best, 0, gm_xa(idx), gm_xb(idx)) == gm_xy(idx) { held_ok = held_ok + 1 } hi = hi + 1 }\n" as *u8)
89 o = mg_w(buf, o, " gm_p(\"GENMACHINE train_err=\" as *u8); gm_pn(best_err); gm_p(\" heldout=\" as *u8); gm_pn(held_ok); gm_p(\"/\" as *u8); gm_pn(NHELD)\n" as *u8)
90 o = mg_w(buf, o, " if found == 1 { if held_ok == NHELD { gm_p(\" GENERALIZES ops=\" as *u8) } else { gm_p(\" OVERFIT ops=\" as *u8) } } else { gm_p(\" NEAR ops=\" as *u8) }\n" as *u8)
91 o = mg_w(buf, o, " var jg: i64 = 0\n while jg < GLEN { gm_pn(best[jg]); gm_p(\" \" as *u8); jg = jg + 1 }\n gm_p(\"\\n\" as *u8)\n" as *u8)
92 o = mg_w(buf, o, " if found == 1 { if held_ok == NHELD { sys_exit(0); return 0 } }\n sys_exit(1)\n return 1\n}\n" as *u8)
93
94 let fd: i64 = sys_openat_wr("runtime/_hdl_build/nx_gen_machine.nx\x00" as *u8, 420)
95 if fd < 0 { mg_pr("MACHINEGEN RED: champion-unwritable\n" as *u8); return 1 }
96 sys_write(fd, buf, o); sys_close(fd)
97 mg_pr("MACHINEGEN: emitted runtime/_hdl_build/nx_gen_machine.nx (" as *u8); mg_prn(o); mg_pr(" bytes) from DATA spec (6 op-fragments + 10 examples). Build+run it to derive the organ.\n" as *u8)
98 return 0
99}