nx_reasoning_gate.nx source
↩ module page · 264 lines · 10368 B
1// nx_reasoning_gate.nx -- MEASURED gate for sovereign TEST-TIME COMPUTE
2// (self-consistency, Wang 2023) on the REAL model (Qwen2.5-0.5B-Instruct,
3// v4-lazy forward, ChatML path).
4//
5// controls pure: extractor (first/last/none), majority, tie, sentinel
6// liveness greedy is deterministic (Q0 twice -> byte-identical)
7// MEASURE 8 arithmetic questions x { greedy, self-consistency@5
8// (T=0.8, top-k=40, prime-spaced seeds) } -> accuracy tally
9//
10// The verdict reports whichever way the measurement lands (LIFT / TIE /
11// HURT) -- the gate PASSES (exit 0) when the harness ran mechanically
12// clean; accuracy is REPORTED, not gamed. license_tier: ORIGINAL
13// expect_exit: 0
14
15import "nx_syscalls.nx"
16import "nx_tier.nx"
17import "nx_le.nx"
18import "nx_bpe.nx"
19import "nx_gguf.nx"
20import "nx_gguf_load.nx"
21import "nx_gguf_meta.nx"
22import "nx_f32.nx"
23import "nx_f32_kv_cache.nx"
24import "nx_f32_lazy_weight.nx"
25import "nx_f32_llama_block.nx"
26import "nx_f32_llama_block_v4.nx"
27import "nx_f32_llama_stack_v4.nx"
28import "nx_f32_llama_layer_lazy_load.nx"
29import "nx_f32_llm.nx"
30import "nx_f32_llm_v4.nx"
31import "nx_f32_llm_read_dims.nx"
32import "nx_f32_bpe_load.nx"
33import "nx_f32_llm_special_tokens.nx"
34import "nx_f32_sampler.nx"
35import "nx_prng.nx"
36import "nx_reasoning.nx"
37
38const RG_M: nx_int = 8 // questions
39const RG_N: nx_int = 5 // self-consistency samples per question
40
41func rg_w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
42func rg_wn(v: i64) -> i64 {
43 var m: i64 = v
44 if m < 0 { rg_w("-" as *u8); m = 0 - m }
45 let t: *u8 = sys_mmap(28)
46 var k: i64 = 0
47 if m == 0 { t[0] = 48 as u8; k = 1 }
48 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
49 let o: *u8 = sys_mmap(28)
50 var i: i64 = 0
51 while i < k { o[i] = t[k - 1 - i]; i = i + 1 }
52 sys_write(1, o, k)
53 return 0
54}
55func rg_wtext(b: *u8, n: i64) -> i64 {
56 if n <= 0 { return 0 }
57 let t: *u8 = sys_mmap(n + 1)
58 var i: i64 = 0
59 while i < n {
60 var c: i64 = b[i] as i64
61 if c < 32 { c = 46 }
62 t[i] = c as u8
63 i = i + 1
64 }
65 sys_write(1, t, n)
66 return 0
67}
68func rg_bytes_eq(a: *u8, b: *u8, n: i64) -> i64 {
69 var i: i64 = 0
70 while i < n { if a[i] != b[i] { return 0 } i = i + 1 }
71 return 1
72}
73
74func rg_atoi(s: *u8) -> i64 {
75 var v: i64 = 0
76 var i: i64 = 0
77 while s[i] != (0 as u8) {
78 let d: i64 = s[i] as i64
79 if d < 48 { return v }
80 if d > 57 { return v }
81 v = v * 10 + (d - 48)
82 i = i + 1
83 }
84 return v
85}
86
87// Usage: nx_reasoning_gate [qlo qhi] -- bare = the canonical full 0..8
88// run; a range = a CHUNK (for flaky-host splitting: greedy is proven
89// byte-deterministic and SC seeds are per-question constants, so
90// chunked results are identical to the full run's rows).
91
92func main(argc: i64, argv: *i64) -> i64 {
93 var qlo: nx_int = 0
94 var qhi: nx_int = RG_M
95 if argc >= 3 {
96 let a1: i64 = rg_atoi(argv[1] as *u8)
97 let a2: i64 = rg_atoi(argv[2] as *u8)
98 if a2 > a1 { if a2 <= RG_M { qlo = a1; qhi = a2 } }
99 }
100 // ---- pure controls (no model) --------------------------------
101 let ct: *u8 = "abc def" as *u8
102 let c1: i64 = nx_reason_extract_last_int(ct, 7)
103 if c1 != NX_REASON_NONE { return 11 }
104 let ct2: *u8 = "47 + 38 = 85." as *u8
105 let c2f: i64 = nx_reason_extract_int(ct2, 13)
106 let c2l: i64 = nx_reason_extract_last_int(ct2, 13)
107 if c2f != 47 { return 11 }
108 if c2l != 85 { return 11 }
109 let ans: *i64 = sys_mmap(5 * 8) as *i64
110 ans[0] = 7; ans[1] = 3; ans[2] = 7; ans[3] = NX_REASON_NONE; ans[4] = 7
111 let m1: i64 = nx_reason_majority(ans, 5)
112 if m1 != 7 { return 12 }
113 ans[0] = 4; ans[1] = 4; ans[2] = 9; ans[3] = 9; ans[4] = NX_REASON_NONE
114 let m2: i64 = nx_reason_majority(ans, 5)
115 if m2 != 4 { return 12 } // tie -> earliest-seen
116 ans[0] = NX_REASON_NONE; ans[1] = NX_REASON_NONE; ans[2] = NX_REASON_NONE
117 let m3: i64 = nx_reason_majority(ans, 3)
118 if m3 != NX_REASON_NONE { return 13 }
119 rg_w("RG controls extractor+majority+tie+sentinel OK\n" as *u8)
120
121 // ---- load the real model once --------------------------------
122 let path: *u8 = "/tmp/nx_real_model.gguf" as *u8
123 let len_out: *i64 = sys_mmap(8) as *i64
124 let buf: *u8 = sys_read_file(path, len_out)
125 if buf == (0 as *u8) { return 10 }
126 let hdr: *NxGgufHeader = sys_mmap(NX_GGUF_HDR_BYTES) as *NxGgufHeader
127 if nx_gguf_parse(buf, len_out[0], hdr) != NX_GGUF_OK { return 20 }
128 let model: *NxF32LlamaModel = nx_f32_llama_model_alloc()
129 let out_err: *i64 = sys_mmap(8) as *i64
130 if nx_f32_llm_read_dims_from_gguf(buf, len_out[0], hdr, model, out_err) != NX_FLD_OK { return 30 }
131 if nx_f32_llm_load_weights_v4_from_gguf(buf, hdr, model, out_err) != NX_FLV4_OK { return 40 }
132 let vocab: *NxBpeVocab = nx_bpe_vocab_new(67108864, 262144, 524288)
133 let nt2: *i64 = sys_mmap(8) as *i64
134 let nm: *i64 = sys_mmap(8) as *i64
135 if nx_f32_bpe_load_from_gguf(buf, len_out[0], hdr, vocab, nt2, nm, out_err) != NX_FBL_OK { return 50 }
136 let eos: nx_int = nx_f32_llm_read_eos(buf, len_out[0], hdr)
137 let cache: *NxF32KVCache = nx_f32_kv_cache_alloc(
138 model.n_layers, model.n_kv_heads, 128, model.head_dim)
139
140 let rc: *NxReasonCfg = nx_reason_cfg_alloc()
141 rc.model = model
142 rc.vocab = vocab
143 rc.cache = cache
144 rc.max_new = 12
145 rc.inv_temp_f32 = 0x3FA00000 // 1/0.8
146 rc.top_k = 40
147 rc.eps = 0x358637BD // 1e-6 (chat-harness proven)
148 rc.attn_scale = 0x3E000000 // 0.125 (1/sqrt(head_dim=64))
149 rc.rope_log_base = 0x415D0EAB // ln(1e6)
150 rc.eos = eos
151 rc.im_start = 151644
152 rc.im_end = 151645
153
154 // ---- question set (mixed difficulty; data as data) -----------
155 let qa: *i64 = sys_mmap(RG_M * 8) as *i64
156 let qb: *i64 = sys_mmap(RG_M * 8) as *i64
157 let qo: *i64 = sys_mmap(RG_M * 8) as *i64
158 let qt: *i64 = sys_mmap(RG_M * 8) as *i64
159 qa[0] = 47; qo[0] = 0; qb[0] = 38; qt[0] = 85
160 qa[1] = 76; qo[1] = 0; qb[1] = 59; qt[1] = 135
161 qa[2] = 83; qo[2] = 1; qb[2] = 47; qt[2] = 36
162 qa[3] = 64; qo[3] = 1; qb[3] = 29; qt[3] = 35
163 qa[4] = 17; qo[4] = 2; qb[4] = 6; qt[4] = 102
164 qa[5] = 23; qo[5] = 2; qb[5] = 7; qt[5] = 161
165 qa[6] = 347; qo[6] = 0; qb[6] = 286; qt[6] = 633
166 qa[7] = 58; qo[7] = 2; qb[7] = 3; qt[7] = 174
167
168 // ---- determinism control: Q0 greedy twice (full/0-chunk only) --
169 let qbuf: *u8 = sys_mmap(96)
170 if qlo == 0 {
171 let nq0: nx_int = nx_reason_build_q_short(qbuf, qa[0], qo[0], qb[0])
172 let d1: *u8 = sys_mmap(128)
173 let d2: *u8 = sys_mmap(128)
174 let nd1: nx_int = nx_reason_chat_gen(rc, qbuf, nq0, 0 as *i64, d1, 128)
175 let nd2: nx_int = nx_reason_chat_gen(rc, qbuf, nq0, 0 as *i64, d2, 128)
176 if nd1 != nd2 { return 14 }
177 if nd1 > 0 { if rg_bytes_eq(d1, d2, nd1 as i64) != 1 { return 14 } }
178 rg_w("RG determinism greedy-x2 byte-identical OK\n" as *u8)
179 } else {
180 rg_w("RG determinism control done in the 0-chunk (skipped here)\n" as *u8)
181 }
182
183 // ---- the measurement ------------------------------------------
184 var greedy_ok: i64 = 0
185 var sc_ok: i64 = 0
186 var all_nonempty: i64 = 1
187 var diverse_q: i64 = 0
188 let sc_ans: *i64 = sys_mmap(RG_N * 8) as *i64
189 let sc_lens: *i64 = sys_mmap(RG_N * 8) as *i64
190 let sc_texts: *u8 = sys_mmap(RG_N * 96)
191 let gout: *u8 = sys_mmap(128)
192
193 var qi: nx_int = qlo
194 while qi < qhi {
195 let nq: nx_int = nx_reason_build_q_short(qbuf, qa[qi], qo[qi], qb[qi])
196
197 // greedy
198 let ngr: nx_int = nx_reason_chat_gen(rc, qbuf, nq, 0 as *i64, gout, 128)
199 if ngr <= 0 { all_nonempty = 0 }
200 let gv: i64 = nx_reason_extract_last_int(gout, ngr)
201
202 // self-consistency@N
203 let mv: i64 = nx_reason_selfconsist(rc, qbuf, nq, RG_N, 20260709,
204 sc_ans, sc_texts, 96, sc_lens)
205
206 // per-question diversity (distinct sampled answers)
207 var ndis: i64 = 0
208 var i: nx_int = 0
209 while i < RG_N {
210 var seen: i64 = 0
211 var j: nx_int = 0
212 while j < i {
213 if sc_ans[j] == sc_ans[i] { seen = 1 }
214 j = j + 1
215 }
216 if seen == 0 { ndis = ndis + 1 }
217 if sc_lens[i] <= 0 { all_nonempty = 0 }
218 i = i + 1
219 }
220 if ndis > 1 { diverse_q = diverse_q + 1 }
221
222 rg_w("Q" as *u8); rg_wn(qi as i64)
223 rg_w(" " as *u8); rg_wn(qa[qi])
224 if qo[qi] == 0 { rg_w("+" as *u8) }
225 if qo[qi] == 1 { rg_w("-" as *u8) }
226 if qo[qi] == 2 { rg_w("*" as *u8) }
227 rg_wn(qb[qi])
228 rg_w(" truth=" as *u8); rg_wn(qt[qi])
229 rg_w(" greedy=" as *u8); rg_wn(gv)
230 if gv == qt[qi] { greedy_ok = greedy_ok + 1; rg_w(" OK" as *u8) } else { rg_w(" NO" as *u8) }
231 rg_w(" | sc:" as *u8)
232 var s2: nx_int = 0
233 while s2 < RG_N { rg_w(" " as *u8); rg_wn(sc_ans[s2]); s2 = s2 + 1 }
234 rg_w(" -> maj=" as *u8); rg_wn(mv)
235 if mv == qt[qi] { sc_ok = sc_ok + 1; rg_w(" OK" as *u8) } else { rg_w(" NO" as *u8) }
236 rg_w(" distinct=" as *u8); rg_wn(ndis)
237 rg_w("\n" as *u8)
238 qi = qi + 1
239 }
240
241 // ---- tally + honest verdict -----------------------------------
242 let nq_run: i64 = (qhi - qlo) as i64
243 rg_w("TALLY range=" as *u8); rg_wn(qlo as i64)
244 rg_w(".." as *u8); rg_wn(qhi as i64)
245 rg_w(" greedy=" as *u8); rg_wn(greedy_ok)
246 rg_w("/" as *u8); rg_wn(nq_run)
247 rg_w(" selfconsistency@" as *u8); rg_wn(RG_N as i64)
248 rg_w("=" as *u8); rg_wn(sc_ok)
249 rg_w("/" as *u8); rg_wn(nq_run)
250 rg_w("\n" as *u8)
251 rg_w("LIAR-KILL controls=1 all-gens-nonempty=" as *u8)
252 rg_wn(all_nonempty)
253 rg_w(" diverse-questions=" as *u8); rg_wn(diverse_q)
254 rg_w("/" as *u8); rg_wn(nq_run)
255 rg_w("\n" as *u8)
256 rg_w("VERDICT test-time-compute self-consistency vs greedy: " as *u8)
257 if sc_ok > greedy_ok { rg_w("LIFT" as *u8) }
258 if sc_ok == greedy_ok { rg_w("TIE" as *u8) }
259 if sc_ok < greedy_ok { rg_w("HURT" as *u8) }
260 rg_w(" (measured, liar-killed)\n" as *u8)
261 rg_w("REASONING_GATE DONE\n" as *u8)
262 if all_nonempty != 1 { return 60 }
263 return 0
264}