code wiki / _hdl_build / nx_research_sim_gate.nx
nx_research_sim_gate.nx source
↩ module page · 140 lines · 8256 B
1// nx_research_sim_gate.nx -- the RESEARCH-ENGINE reproducibility loop, end to end and sovereign (operator:
2// "discover new capabilities through different math/chemistry/physics via simulations we run... s class exceed
3// on research capabilities AND their reproducibility"). The loop:
4// RESEARCHER -> locates the relevant knowledge in our foundationed no-link-rot library (the AlphaTensor doc:
5// "Discovering faster matrix multiplication").
6// TESTING-TEAM -> runs a SOVEREIGN simulation that REPRODUCES a KNOWN published result -- Strassen's 1969
7// algorithm: a 2x2 matrix product in 7 scalar multiplications instead of the naive 8 -- and
8// proves it bit-exact against the canonical hand-checkable answer AND over many random
9// matrices, while COUNTING the multiplications (7 vs 8 = the discovered efficiency).
10// VERIFIER -> the exact-comparison check that a discovery loop needs: a BROKEN candidate scheme is
11// REJECTED (liar-kill), so "this scheme is correct" can never be rubber-stamped.
12// Pure integer algebra (no float), deterministic PRNG (reproducible by anyone running it), no 3rd party.
13// This is the substrate the alphatensor-class search needs: propose a low-mult scheme -> verify exactness here.
14// GREEN iff 6/6. license_tier: ORIGINAL
15import "nx_syscalls.nx"
16import "nx_library.nx"
17
18func g_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
19func g_n(v: i64) -> i64 { var m: i64=v; if m<0{g_w("-");m=0-m} let t:*u8=sys_mmap(24); var k:i64=0; if m==0{t[0]=48 as u8;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i:i64=0; let o:*u8=sys_mmap(24); while i<k{o[i]=t[k-1-i];i=i+1}; sys_write(1,o,k); return 0 }
20func g_row(id: *u8, ok: i64, pass: *i64) -> i64 { g_w(" "); g_w(id); g_w(": "); if ok==1 { g_w("OK\n"); pass[0]=pass[0]+1 } else { g_w("FAIL\n") } return 0 }
21
22// substring search in a buffer
23func rs_has(buf: *u8, n: i64, needle: *u8) -> i64 {
24 var nl: i64=0; while needle[nl]!=(0 as u8){nl=nl+1}
25 if nl==0 { return 0 }
26 var i: i64=0
27 while i+nl<=n { var j: i64=0; var ok: i64=1; while j<nl { if buf[i+j]!=needle[j]{ok=0;j=nl}else{j=j+1} } if ok==1 {return 1} i=i+1 }
28 return 0
29}
30// RESEARCHER: does ANY foundationed library doc contain `needle`? (locate the research in our no-link-rot corpus)
31func rs_lib_contains(needle: *u8) -> i64 {
32 let cids: *i64 = sys_mmap(8*256) as *i64
33 let cnt: i64 = lib_list(cids, 256)
34 let pp: *i64 = sys_mmap(8) as *i64; let ll: *i64 = sys_mmap(8) as *i64
35 var d: i64 = 0
36 while d < cnt {
37 if lib_get(cids[d] as *u8, pp, ll) == 1 { if rs_has(pp[0] as *u8, ll[0], needle) == 1 { return 1 } }
38 d = d + 1
39 }
40 return 0
41}
42
43// counted multiply -- every scalar multiplication bumps cnt, so we can prove 7 vs 8.
44func rs_mul(cnt: *i64, a: i64, b: i64) -> i64 { cnt[0] = cnt[0] + 1; return a * b }
45
46// naive 2x2 matmul: 8 multiplications. a,b,c are [00,01,10,11].
47func mm_naive(a: *i64, b: *i64, c: *i64, cnt: *i64) -> i64 {
48 c[0] = rs_mul(cnt,a[0],b[0]) + rs_mul(cnt,a[1],b[2])
49 c[1] = rs_mul(cnt,a[0],b[1]) + rs_mul(cnt,a[1],b[3])
50 c[2] = rs_mul(cnt,a[2],b[0]) + rs_mul(cnt,a[3],b[2])
51 c[3] = rs_mul(cnt,a[2],b[1]) + rs_mul(cnt,a[3],b[3])
52 return 0
53}
54// Strassen 2x2 matmul: 7 multiplications (the published result we reproduce).
55func mm_strassen(a: *i64, b: *i64, c: *i64, cnt: *i64) -> i64 {
56 let m1: i64 = rs_mul(cnt, a[0]+a[3], b[0]+b[3])
57 let m2: i64 = rs_mul(cnt, a[2]+a[3], b[0])
58 let m3: i64 = rs_mul(cnt, a[0], b[1]-b[3])
59 let m4: i64 = rs_mul(cnt, a[3], b[2]-b[0])
60 let m5: i64 = rs_mul(cnt, a[0]+a[1], b[3])
61 let m6: i64 = rs_mul(cnt, a[2]-a[0], b[0]+b[1])
62 let m7: i64 = rs_mul(cnt, a[1]-a[3], b[2]+b[3])
63 c[0] = m1 + m4 - m5 + m7
64 c[1] = m3 + m5
65 c[2] = m2 + m4
66 c[3] = m1 - m2 + m3 + m6
67 return 0
68}
69// a BROKEN candidate (c[3] uses +m2 instead of -m2) -- the verifier MUST reject this.
70func mm_strassen_broken(a: *i64, b: *i64, c: *i64, cnt: *i64) -> i64 {
71 let m1: i64 = rs_mul(cnt, a[0]+a[3], b[0]+b[3])
72 let m2: i64 = rs_mul(cnt, a[2]+a[3], b[0])
73 let m3: i64 = rs_mul(cnt, a[0], b[1]-b[3])
74 let m4: i64 = rs_mul(cnt, a[3], b[2]-b[0])
75 let m5: i64 = rs_mul(cnt, a[0]+a[1], b[3])
76 let m6: i64 = rs_mul(cnt, a[2]-a[0], b[0]+b[1])
77 let m7: i64 = rs_mul(cnt, a[1]-a[3], b[2]+b[3])
78 c[0] = m1 + m4 - m5 + m7
79 c[1] = m3 + m5
80 c[2] = m2 + m4
81 c[3] = m1 + m2 + m3 + m6
82 return 0
83}
84// deterministic xorshift64 PRNG (reproducible -- same seed, same sequence, anywhere).
85func rs_rng(state: *i64) -> i64 { var x: i64 = state[0]; x = x ^ (x << 13); x = x ^ (x >> 7); x = x ^ (x << 17); state[0] = x; return x }
86func mm_eq(x: *i64, y: *i64) -> i64 { if x[0]!=y[0]{return 0} if x[1]!=y[1]{return 0} if x[2]!=y[2]{return 0} if x[3]!=y[3]{return 0} return 1 }
87
88func main() -> i64 {
89 let pass: *i64 = sys_mmap(8) as *i64; pass[0] = 0
90 g_w("=== NX-RESEARCH-SIM GATE (reproduce Strassen's faster matmul from the foundationed library) ===\n")
91
92 // --- RESEARCHER: locate the matmul-discovery research in our no-link-rot library ---
93 let found: i64 = rs_lib_contains("matrix multiplication" as *u8)
94
95 // --- TESTING-TEAM: KAT against the canonical hand-checkable product ---
96 let a: *i64 = sys_mmap(64) as *i64; a[0]=1; a[1]=2; a[2]=3; a[3]=4
97 let b: *i64 = sys_mmap(64) as *i64; b[0]=5; b[1]=6; b[2]=7; b[3]=8
98 let exp: *i64 = sys_mmap(64) as *i64; exp[0]=19; exp[1]=22; exp[2]=43; exp[3]=50
99 let cN: *i64 = sys_mmap(64) as *i64; let cS: *i64 = sys_mmap(64) as *i64; let cB: *i64 = sys_mmap(64) as *i64
100 let knt: *i64 = sys_mmap(8) as *i64
101 knt[0]=0; mm_naive(a, b, cN, knt); let mulsN: i64 = knt[0]
102 knt[0]=0; mm_strassen(a, b, cS, knt); let mulsS: i64 = knt[0]
103 knt[0]=0; mm_strassen_broken(a, b, cB, knt)
104
105 g_w(" researcher: 'matrix multiplication' research in library = "); g_n(found); g_w("\n")
106 g_w(" KAT [[1,2],[3,4]]x[[5,6],[7,8]] known=[19,22,43,50]\n")
107 g_w(" naive -> ["); g_n(cN[0]); g_w(","); g_n(cN[1]); g_w(","); g_n(cN[2]); g_w(","); g_n(cN[3]); g_w("] muls="); g_n(mulsN); g_w("\n")
108 g_w(" Strassen -> ["); g_n(cS[0]); g_w(","); g_n(cS[1]); g_w(","); g_n(cS[2]); g_w(","); g_n(cS[3]); g_w("] muls="); g_n(mulsS); g_w("\n")
109 g_w(" broken -> ["); g_n(cB[0]); g_w(","); g_n(cB[1]); g_w(","); g_n(cB[2]); g_w(","); g_n(cB[3]); g_w("]\n")
110
111 // --- REPRODUCIBILITY: Strassen == naive bit-exact over N deterministic-random matrices ---
112 let st: *i64 = sys_mmap(8) as *i64; st[0] = 0x2545F4914F6CDD1D
113 let ra: *i64 = sys_mmap(64) as *i64; let rb: *i64 = sys_mmap(64) as *i64
114 let r1: *i64 = sys_mmap(64) as *i64; let r2: *i64 = sys_mmap(64) as *i64
115 let dump: *i64 = sys_mmap(8) as *i64
116 var iter: i64 = 0; var mism: i64 = 0; let N: i64 = 500
117 while iter < N {
118 var q: i64 = 0
119 while q < 4 { ra[q] = (rs_rng(st) & 0x7f) - 64; q = q + 1 }
120 q = 0; while q < 4 { rb[q] = (rs_rng(st) & 0x7f) - 64; q = q + 1 }
121 dump[0]=0; mm_naive(ra, rb, r1, dump)
122 dump[0]=0; mm_strassen(ra, rb, r2, dump)
123 if mm_eq(r1, r2) == 0 { mism = mism + 1 }
124 iter = iter + 1
125 }
126 g_w(" reproducibility: "); g_n(N); g_w(" random matrices, Strassen!=naive mismatches="); g_n(mism); g_w("\n")
127
128 g_row("RESEARCHER: matmul-discovery research foundationed + retrievable in library" as *u8, found, pass)
129 g_row("KAT: naive reproduces the known product [19,22,43,50]" as *u8, mm_eq(cN, exp), pass)
130 g_row("KAT: Strassen reproduces the SAME known product (bit-exact)" as *u8, mm_eq(cS, exp), pass)
131 g_row("REPRODUCIBILITY: Strassen==naive over 500 random matrices (0 mismatch)" as *u8, (mism == 0) as i64, pass)
132 var eff: i64 = 0
133 if mulsN == 8 { if mulsS == 7 { eff = 1 } }
134 g_row("EFFICIENCY: Strassen=7 muls vs naive=8 (the discovered capability, counted)" as *u8, eff, pass)
135 g_row("LIAR-KILL: a BROKEN candidate scheme is REJECTED by exact comparison" as *u8, (mm_eq(cB, exp) == 0) as i64, pass)
136
137 g_w("RESEARCH-SIM-GATE rows=6 pass="); g_n(pass[0])
138 if pass[0] == 6 { g_w(" verdict=GREEN\n"); sys_exit(0); return 0 }
139 g_w(" verdict=RED\n"); sys_exit(1); return 1
140}