code wiki / _hdl_build / nx_wakesleep_scale_gate.nx
nx_wakesleep_scale_gate.nx source
↩ module page · 131 lines · 8892 B
1import "nx_gate_gn.nx"
2import "nx_gate_base.nx"
3// nx_wakesleep_scale_gate.nx -- SCALE: the experience-grown library closes the cold-start gap across a CURRICULUM,
4// measured (DreamCoder's wake-sleep amortization, sovereign, no LLM). A CHEAP RECOGNITION signal (the target's
5// polynomial degree via finite differences -- DreamCoder's recognition model, here exact + sovereign) routes
6// straight to the ONE candidate primitive, so identifying a primitive costs 1 verify-fit, not a brute scan. The
7// amortization then lives where it really is: RETRIEVAL (going to an external source) happens ONCE per distinct
8// primitive (wake-sleep) vs EVERY problem (no library). Found the hard way: a naive library-scan that pays a full
9// fit per wrong candidate is NOT cheaper than re-mining -- the recognition router is what makes reuse cheap.
10// T1 FULL COVERAGE: wake-sleep solves the whole (coverable) curriculum.
11// T2 AMORTIZATION: retrievals with wake-sleep = #distinct primitives (3) << curriculum length (9).
12// T3 TOTAL WORK: fits + retrievals with the growing library < without it (retrievals amortized; fits equal).
13// T4 LEARNING CURVE: after the last distinct primitive is retrieved, retrievals drop to ZERO (curve flattens).
14// T5 HONEST CEILING: a truly out-of-grammar problem (2^x) escalates even with full experience (cold-start residual).
15// expect_exit: 0 license_tier: ORIGINAL
16import "nx_synth_fit.nx"
17import "nx_syscalls.nx"
18
19
20// CHEAP RECOGNITION: polynomial degree of a sequence via finite differences (no fit). returns n if non-polynomial.
21func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
22" as *u8); return ok }
23func seq_degree(ys: *i64, n: i64) -> i64 {
24 let d: *i64=sys_mmap(256) as *i64; var i: i64=0; while i<n { d[i]=ys[i]; i=i+1 }
25 var len: i64=n; var deg: i64=0
26 while len>1 {
27 var constant: i64=1; var j: i64=1; while j<len { if d[j]!=d[0] { constant=0 } j=j+1 }
28 if constant==1 { return deg }
29 var k2: i64=0; while k2<(len-1) { d[k2]=d[k2+1]-d[k2]; k2=k2+1 }
30 len=len-1; deg=deg+1
31 }
32 return deg
33}
34// build a power-sum target: ys[x]=powsum(k,x) for x=0..7. returns n=8.
35func mk_pow(k: i64, xs: *i64, ys: *i64) -> i64 { var i: i64=0; while i<8 { xs[i]=i; ys[i]=sf_powsum(k,i); i=i+1 } return 8 }
36
37// solve one problem: cheap recognition (degree) -> the needed primitive S_{deg-1}; retrieve from source ONLY if
38// not in the library (mining), then 1 verify-fit. Updates library. fits/retrievals counted separately.
39func solve_problem(xs: *i64, ys: *i64, n: i64, hy0: i64, hy1: i64, lib_k: *i64, lib_f: *i64, lib_nb: *i64, pool: *i64, pooln: i64, fits: *i64, retr: *i64) -> i64 {
40 let deg: i64=seq_degree(ys,n)
41 let needk: i64=deg-1 // powsum(k) has degree k+1
42 if needk<1 { return 0 }
43 var inlib: i64=0-1; var j: i64=0; while j<lib_nb[0] { if lib_k[j]==needk { inlib=j } j=j+1 }
44 if inlib<0 {
45 var inpool: i64=0; var p: i64=0; while p<pooln { if pool[p]==needk { inpool=1 } p=p+1 }
46 if inpool==0 { return 0 } // source can't provide it -> escalate (cold-start novelty)
47 retr[0]=retr[0]+1
48 lib_k[lib_nb[0]]=needk; lib_f[lib_nb[0]]=1; lib_nb[0]=lib_nb[0]+1
49 } else { lib_f[inlib]=lib_f[inlib]+1 }
50 fits[0]=fits[0]+1
51 if sf_keep(needk,xs,ys,n,8,hy0,9,hy1)==1 { return 1 }
52 return 0
53}
54
55func main() -> i64 {
56 gw("=== nx_wakesleep_scale: experience-grown library closes the cold-start gap across a curriculum (no LLM) ===\n" as *u8)
57 var pass: i64=0; var total: i64=0
58
59 let ks: *i64=sys_mmap(128) as *i64
60 ks[0]=3; ks[1]=4; ks[2]=3; ks[3]=5; ks[4]=4; ks[5]=3; ks[6]=5; ks[7]=4; ks[8]=5
61 let NP: i64=9
62 let pool: *i64=sys_mmap(64) as *i64; pool[0]=3; pool[1]=4; pool[2]=5
63 let pooln: i64=3
64
65 // ---- WAKE-SLEEP pass: the library PERSISTS and GROWS. ----
66 let lib_k: *i64=sys_mmap(128) as *i64; let lib_f: *i64=sys_mmap(128) as *i64
67 let lib_nb: *i64=sys_mmap(16) as *i64; lib_nb[0]=0
68 let fits_ws: *i64=sys_mmap(16) as *i64; fits_ws[0]=0
69 let retr_ws: *i64=sys_mmap(16) as *i64; retr_ws[0]=0
70 let curve: *i64=sys_mmap(128) as *i64
71 var solved_ws: i64=0
72 let xs: *i64=sys_mmap(64) as *i64; let ys: *i64=sys_mmap(64) as *i64
73 var pidx: i64=0
74 while pidx<NP {
75 let k: i64=ks[pidx]; let n: i64=mk_pow(k,xs,ys)
76 let before: i64=retr_ws[0]
77 let s: i64=solve_problem(xs,ys,n,sf_powsum(k,8),sf_powsum(k,9),lib_k,lib_f,lib_nb,pool,pooln,fits_ws,retr_ws)
78 curve[pidx]=retr_ws[0]-before
79 solved_ws=solved_ws+s
80 pidx=pidx+1
81 }
82
83 // ---- NO-PERSISTENCE pass: library reset each problem. ----
84 let fits_no: *i64=sys_mmap(16) as *i64; fits_no[0]=0
85 let retr_no: *i64=sys_mmap(16) as *i64; retr_no[0]=0
86 var solved_no: i64=0
87 var p2: i64=0
88 while p2<NP {
89 let k: i64=ks[p2]; let n: i64=mk_pow(k,xs,ys)
90 let lk: *i64=sys_mmap(64) as *i64; let lf: *i64=sys_mmap(64) as *i64; let ln: *i64=sys_mmap(16) as *i64; ln[0]=0
91 let s: i64=solve_problem(xs,ys,n,sf_powsum(k,8),sf_powsum(k,9),lk,lf,ln,pool,pooln,fits_no,retr_no)
92 solved_no=solved_no+s
93 p2=p2+1
94 }
95
96 let work_ws: i64=fits_ws[0]+retr_ws[0]
97 let work_no: i64=fits_no[0]+retr_no[0]
98
99 // T1: FULL COVERAGE.
100 total=total+1; if solved_ws==NP { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
101 gw("T1 FULL COVERAGE: wake-sleep solved " as *u8); gn(solved_ws); gw("/" as *u8); gn(NP); gw(" (library grew to {" as *u8); var li: i64=0; while li<lib_nb[0] { gw("S" as *u8); gn(lib_k[li]); if li<lib_nb[0]-1 { gw("," as *u8) } li=li+1 } gw("})\n" as *u8)
102
103 // T2: AMORTIZATION -- retrievals = #distinct primitives << curriculum length.
104 total=total+1; if retr_ws[0]==3 { if retr_ws[0]<NP { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) } } else { gw(" [FAIL] " as *u8) }
105 gw("T2 AMORTIZATION: retrievals wake-sleep=" as *u8); gn(retr_ws[0]); gw(" (=#distinct primitives) vs no-persistence=" as *u8); gn(retr_no[0]); gw(" (=every problem)\n" as *u8)
106
107 // T3: TOTAL WORK (fits + retrievals) -- wake-sleep < no-persistence; fits equal, retrievals amortized.
108 total=total+1; if work_ws<work_no { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
109 gw("T3 TOTAL WORK: wake-sleep=" as *u8); gn(work_ws); gw(" (fits " as *u8); gn(fits_ws[0]); gw("+retr " as *u8); gn(retr_ws[0]); gw(") vs no-persistence=" as *u8); gn(work_no); gw(" (fits " as *u8); gn(fits_no[0]); gw("+retr " as *u8); gn(retr_no[0]); gw(") -- recognition makes fits equal; retrievals amortize\n" as *u8)
110
111 // T4: LEARNING CURVE -- retrievals per problem flatten to 0 after coverage completes.
112 var tail: i64=0; var ci: i64=4; while ci<NP { tail=tail+curve[ci]; ci=ci+1 }
113 total=total+1; if tail==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
114 gw("T4 LEARNING CURVE: retrievals per problem = [" as *u8); var mi: i64=0; while mi<NP { gn(curve[mi]); if mi<NP-1 { gw("," as *u8) } mi=mi+1 } gw("] -> flattens to 0 once coverage is complete (tail=" as *u8); gn(tail); gw(")\n" as *u8)
115
116 // T5: HONEST CEILING -- 2^x is non-polynomial -> recognition routes to a degree the pool can't supply -> escalate.
117 let x2: *i64=sys_mmap(64) as *i64; let y2: *i64=sys_mmap(64) as *i64
118 var z: i64=0; while z<8 { x2[z]=z; y2[z]=sf_pow_i(2,z); z=z+1 }
119 let f2: *i64=sys_mmap(16) as *i64; f2[0]=0; let r2: *i64=sys_mmap(16) as *i64; r2[0]=0
120 let s2: i64=solve_problem(x2,y2,8,sf_pow_i(2,8),sf_pow_i(2,9),lib_k,lib_f,lib_nb,pool,pooln,f2,r2)
121 total=total+1; if s2==0 { pass=pass+1; gw(" [PASS] " as *u8) } else { gw(" [FAIL] " as *u8) }
122 gw("T5 HONEST CEILING: 2^x (non-polynomial, recognized degree=" as *u8); gn(seq_degree(y2,8)); gw(") -> pool can't supply it -> escalate (solved=" as *u8); gn(s2); gw(") = cold-start novelty, the only residual\n" as *u8)
123
124 gw("\n SCALE RESULT: cold start -> the library grew from experience to FULL coverage; a cheap RECOGNITION signal (degree) routes\n" as *u8)
125 gw(" to the right primitive so reuse costs 1 verify-fit, and RETRIEVAL (the expensive external source lookup) is AMORTIZED to once\n" as *u8)
126 gw(" per distinct primitive. The learning curve flattens. = DreamCoder's wake-sleep, sovereign + measured, NO LLM. The only\n" as *u8)
127 gw(" residual is cold-start novelty (T5) -- which shrinks as the library/prior/concept-table grow across MORE domains.\n" as *u8)
128 gw("WAKESLEEP-SCALE verdict=" as *u8)
129 if pass==total { gw("GREEN passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw(" -- experience-grown coverage closes the cold-start gap at scale, measured, no LLM\n" as *u8); sys_exit(0); return 0 }
130 gw("RED passes=" as *u8); gn(pass); gw("/" as *u8); gn(total); gw("\n" as *u8); sys_exit(1); return 1
131}