nx_nofloat_moe_real_gate.nx source
↩ module page · 313 lines · 15128 B
1// nx_nofloat_moe_real_gate.nx -- MoE rung 3 (2026-07-15): REAL MoE WEIGHTS, LAZILY. Loads a real sparse-MoE
2// gguf (OLMoE-1B-7B: 64 experts, top-8, per-expert ff=1024, D=2048 -- the canonical small real MoE) and runs
3// ONE real MoE-FFN through our integer machinery with LAZY PER-SELECTED-EXPERT dequant -- the design the
4// operator's environment correction settled (dequant-once-everything is the WRONG shape for MoE; sparsity
5// means only K experts' weights are touched per token, so fetch exactly those). Composes the proven pieces:
6// nac_read_config (+ the MoE metadata keys), nx_gguf 3-D tensor info, dequant_row / dequant_to_q16 windows,
7// mm_out_in / fx_exp / silu / qmul. HONEST SCOPE: this rung proves REAL-WEIGHT ROUTING + LAZY FETCH + THE
8// MIX MECHANISM on blk.0 -- NOT a faithful OLMoE model output (full arch = QK-norm etc., a later rung).
9// T1 MoE METADATA read from the model itself: expert_count=64, expert_used_count=8 (olmoe.* keys)
10// T2 3-D expert tensors located, dims EXACT (gate/up [D,ff,E]; down [ff,D,E]; router [D,E] 2-D)
11// T3 LAZY-SLICE LIAR-KILLER: bulk expert-slice dequant (type-aware block offset) == per-row dequant_row
12// on sampled rows x sampled experts x gate+down, EXACT (0 mismatches) -- kills offset-math bugs
13// T4 REAL ROUTING: top-8 = 8 distinct ids; deterministic repeat; a second input selects a DIFFERENT set
14// (the real router discriminates -- the neg-control against constant routing)
15// T5 REAL MIX: K experts lazily fetched (counter == 8), output dense-nonzero, byte-identical repeat
16// Requires /home/elderwesto/nx_stage/nx_moe_model.gguf. ~4.4GB read; slices ~400MB. Return from main.
17// No hw writes (Rule 26). expect_exit: 0 license_tier: ORIGINAL
18import "nx_syscalls.nx"
19import "nx_tier.nx"
20import "nx_le.nx"
21import "nx_tensor.nx"
22import "nx_gguf.nx"
23import "nx_gguf_load.nx"
24import "nx_gguf_meta.nx"
25import "nx_nofloat_llm.nx"
26import "nx_nofloat_arch.nx"
27import "nx_gate_verdict.nx"
28import "nx_stage_path.nx"
29
30func mr_w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
31func mr_n(v: i64) -> i64 {
32 var m: i64 = v
33 if m < 0 { mr_w("-" as *u8); m = 0 - m }
34 let t: *u8 = sys_mmap(24)
35 var k: i64 = 0
36 if m == 0 { t[0] = 48 as u8; k = 1 }
37 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
38 let o: *u8 = sys_mmap(24)
39 var i: i64 = 0
40 while i < k { o[i] = t[k - 1 - i]; i = i + 1 }
41 sys_write(1, o, k)
42 return 0
43}
44func mr_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
45func mr_det(i: i64) -> i64 { return ((i * 2654435761) % 8191) - 4095 }
46
47// byte offset of value index `voff` inside a quantized tensor blob -- rides the CANONICAL type-stride
48// table (nf_type_stride, debt-eaten 2026-07-15: full mainstream family, -1 on unknown). Valid only when
49// voff is block-aligned (expert slices are: ff*D is a multiple of 256 for OLMoE).
50func mr_val_byteoff(ty: i64, voff: i64) -> i64 {
51 let vb: *i64 = sys_mmap(16) as *i64
52 if nf_type_stride(ty, vb) != 0 { return 0 - 1 }
53 return (voff / vb[0]) * vb[1]
54}
55
56func main() -> i64 {
57 mr_w("=== NX-NOFLOAT-MOE-REAL -- real sparse-MoE weights (OLMoE 64-expert top-8), lazy per-expert fetch ===\n" as *u8)
58 let path: *u8 = sp_path("nx_moe_model.gguf" as *u8, sys_mmap(SP_PATH_MAX))
59 sp_skip_unless("NOFLOAT-MOE-REAL-GATE" as *u8, path)
60 let len_out: *i64 = sys_mmap(8) as *i64
61 len_out[0] = 0
62 mr_w("[load] mapping gguf (zero-copy, read-only)...\n" as *u8)
63 let t0: i64 = sys_now_ms()
64 let buf: *u8 = sys_map_file(path, len_out)
65 let t1: i64 = sys_now_ms()
66 if (buf as i64) == 0 { mr_w("MODEL ABSENT\n" as *u8); return 1 }
67 mr_w("[load] mapped in " as *u8); mr_n(t1 - t0); mr_w(" ms (only touched pages will become resident)\n" as *u8)
68 let hdr: *NxGgufHeader = sys_mmap(NX_GGUF_HDR_BYTES) as *NxGgufHeader
69 if nx_gguf_parse(buf, len_out[0], hdr) != NX_GGUF_OK { mr_w("PARSE FAIL\n" as *u8); return 1 }
70 let hloc: *NxGgufHeader = hdr
71 mr_w("[load] bytes=" as *u8); mr_n(len_out[0]); mr_w(" tensors=" as *u8); mr_n(hloc.tensor_count); mr_w("\n" as *u8)
72
73 // arch config + MoE keys from METADATA
74 let cfg: *i64 = sys_mmap(16*8) as *i64
75 let arch: *u8 = sys_mmap(48)
76 if nac_read_config(buf, len_out[0], hdr, cfg, arch) != 0 { mr_w("ARCH CONFIG FAIL\n" as *u8); return 1 }
77 let D: i64 = cfg[0]
78 let FF: i64 = cfg[7]
79 let alen: i64 = mr_slen(arch)
80 let EC: i64 = nac_u32(buf, len_out[0], hdr, arch, alen, ".expert_count" as *u8)
81 let EU: i64 = nac_u32(buf, len_out[0], hdr, arch, alen, ".expert_used_count" as *u8)
82 mr_w("[arch] " as *u8); mr_w(arch); mr_w(" D=" as *u8); mr_n(D); mr_w(" ff=" as *u8); mr_n(FF)
83 mr_w(" experts=" as *u8); mr_n(EC); mr_w(" used=" as *u8); mr_n(EU); mr_w("\n" as *u8)
84
85 // locate blk.0 router + 3-D expert tensors
86 let nR: *u8 = "blk.0.ffn_gate_inp.weight" as *u8
87 let nG: *u8 = "blk.0.ffn_gate_exps.weight" as *u8
88 let nU: *u8 = "blk.0.ffn_up_exps.weight" as *u8
89 let nD: *u8 = "blk.0.ffn_down_exps.weight" as *u8
90 let iR: nx_int = nx_gguf_find_tensor(hdr, nR, mr_slen(nR))
91 let iG: nx_int = nx_gguf_find_tensor(hdr, nG, mr_slen(nG))
92 let iU: nx_int = nx_gguf_find_tensor(hdr, nU, mr_slen(nU))
93 let iD: nx_int = nx_gguf_find_tensor(hdr, nD, mr_slen(nD))
94 if iR < 0 { mr_w("router tensor ABSENT\n" as *u8); return 1 }
95 if iG < 0 { mr_w("gate_exps ABSENT\n" as *u8); return 1 }
96 if iU < 0 { mr_w("up_exps ABSENT\n" as *u8); return 1 }
97 if iD < 0 { mr_w("down_exps ABSENT\n" as *u8); return 1 }
98 let tR: *NxGgufTensorInfo = nx_gguf_tensor_at(hloc, iR)
99 let tG: *NxGgufTensorInfo = nx_gguf_tensor_at(hloc, iG)
100 let tU: *NxGgufTensorInfo = nx_gguf_tensor_at(hloc, iU)
101 let tD: *NxGgufTensorInfo = nx_gguf_tensor_at(hloc, iD)
102 let bR: i64 = hloc.data_off + tR.offset
103 let bG: i64 = hloc.data_off + tG.offset
104 let bU: i64 = hloc.data_off + tU.offset
105 let bD: i64 = hloc.data_off + tD.offset
106 mr_w("[tensors] gate_exps dims=" as *u8); mr_n(tG.dim_0); mr_w("x" as *u8); mr_n(tG.dim_1); mr_w("x" as *u8); mr_n(tG.dim_2)
107 mr_w(" ty=" as *u8); mr_n(tG.ggml_type)
108 mr_w(" | down_exps dims=" as *u8); mr_n(tD.dim_0); mr_w("x" as *u8); mr_n(tD.dim_1); mr_w("x" as *u8); mr_n(tD.dim_2)
109 mr_w(" ty=" as *u8); mr_n(tD.ggml_type); mr_w("\n" as *u8)
110
111 // T1 metadata teeth
112 var pass: i64 = 0
113 var ttl: i64 = 0
114 ttl = ttl + 1
115 var ok1: i64 = 0
116 if EC == 64 { if EU == 8 { ok1 = 1 } }
117 mr_w(" T1 MoE metadata from the model (expert_count=64, expert_used_count=8): " as *u8)
118 if ok1 == 1 { pass = pass + 1; mr_w("PASS\n" as *u8) } else { mr_w("FAIL\n" as *u8) }
119
120 // T2 dims teeth: gate/up [D,ff,E]; down [ff,D,E]; router 2-D [D,E]
121 ttl = ttl + 1
122 var ok2: i64 = 1
123 if tG.n_dims != 3 { ok2 = 0 }
124 if tG.dim_0 != D { ok2 = 0 }
125 if tG.dim_1 != FF { ok2 = 0 }
126 if tG.dim_2 != EC { ok2 = 0 }
127 if tU.n_dims != 3 { ok2 = 0 }
128 if tU.dim_0 != D { ok2 = 0 }
129 if tD.n_dims != 3 { ok2 = 0 }
130 if tD.dim_0 != FF { ok2 = 0 }
131 if tD.dim_1 != D { ok2 = 0 }
132 if tR.n_dims != 2 { ok2 = 0 }
133 if tR.dim_0 != D { ok2 = 0 }
134 if tR.dim_1 != EC { ok2 = 0 }
135 mr_w(" T2 tensor shapes exact (3-D experts as 2-D row views): " as *u8)
136 if ok2 == 1 { pass = pass + 1; mr_w("PASS\n" as *u8) } else { mr_w("FAIL\n" as *u8) }
137
138 // T3 LAZY-SLICE LIAR-KILLER: bulk expert-slice dequant == per-row dequant_row, sampled
139 let tmp: *i64 = sys_mmap(64*256*8) as *i64
140 let rowbuf: *i64 = sys_mmap(D*8) as *i64
141 let slice: *i64 = sys_mmap(FF*D*8) as *i64
142 var t3_mism: i64 = 0
143 var se: i64 = 0
144 while se < 2 {
145 var e: i64 = 3
146 if se == 1 { e = 41 }
147 // gate slice: rows [e*FF, (e+1)*FF) of the [EC*FF, D] 2-D view
148 let voffG: i64 = e * FF * D
149 let boffG: i64 = mr_val_byteoff(tG.ggml_type, voffG)
150 if boffG < 0 { t3_mism = t3_mism + 1000000 } else {
151 dequant_to_q16(buf, bG + boffG, tG.ggml_type, FF*D, slice)
152 var sr: i64 = 0
153 while sr < 4 {
154 var r2: i64 = 0
155 if sr == 1 { r2 = 1 }
156 if sr == 2 { r2 = FF/2 }
157 if sr == 3 { r2 = FF - 1 }
158 dequant_row(buf, bG, tG.ggml_type, e*FF + r2, D, rowbuf, tmp)
159 var d: i64 = 0
160 while d < D { if rowbuf[d] != slice[r2*D + d] { t3_mism = t3_mism + 1 } d = d + 1 }
161 sr = sr + 1
162 }
163 }
164 // down slice: rows [e*D, (e+1)*D) of the [EC*D, FF] 2-D view
165 let voffD: i64 = e * D * FF
166 let boffD: i64 = mr_val_byteoff(tD.ggml_type, voffD)
167 if boffD < 0 { t3_mism = t3_mism + 1000000 } else {
168 dequant_to_q16(buf, bD + boffD, tD.ggml_type, D*FF, slice)
169 var sr2: i64 = 0
170 while sr2 < 4 {
171 var r3: i64 = 0
172 if sr2 == 1 { r3 = 1 }
173 if sr2 == 2 { r3 = D/2 }
174 if sr2 == 3 { r3 = D - 1 }
175 dequant_row(buf, bD, tD.ggml_type, e*D + r3, FF, rowbuf, tmp)
176 var f: i64 = 0
177 while f < FF { if rowbuf[f] != slice[r3*FF + f] { t3_mism = t3_mism + 1 } f = f + 1 }
178 sr2 = sr2 + 1
179 }
180 }
181 se = se + 1
182 }
183 ttl = ttl + 1
184 let ok3: i64 = (t3_mism == 0) as i64
185 mr_w(" T3 lazy-slice dequant == per-row window, sampled rows x 2 experts x gate+down (mism " as *u8); mr_n(t3_mism); mr_w("): " as *u8)
186 if ok3 == 1 { pass = pass + 1; mr_w("PASS\n" as *u8) } else { mr_w("FAIL\n" as *u8) }
187
188 // T4 REAL ROUTING on two inputs
189 let Wr: *i64 = sys_mmap(EC*D*8) as *i64
190 dequant_to_q16(buf, bR, tR.ggml_type, EC*D, Wr)
191 let x1: *i64 = sys_mmap(D*8) as *i64
192 let x2: *i64 = sys_mmap(D*8) as *i64
193 var i: i64 = 0
194 while i < D { x1[i] = mr_det(i + 7); x2[i] = mr_det(i*3 + 501); i = i + 1 }
195 let r: *i64 = sys_mmap(EC*8) as *i64
196 let sel1: *i64 = sys_mmap(EU*8) as *i64
197 let sel2: *i64 = sys_mmap(EU*8) as *i64
198 let selr: *i64 = sys_mmap(EU*8) as *i64
199 var pi: i64 = 0
200 while pi < 3 {
201 var xp: *i64 = x1
202 var sp: *i64 = sel1
203 if pi == 1 { xp = x2; sp = sel2 }
204 if pi == 2 { xp = x1; sp = selr }
205 mm_out_in(xp, Wr, r, 1, D, EC, 0)
206 var ki: i64 = 0
207 while ki < EU {
208 var best: i64 = 0 - 1
209 var bestv: i64 = 0
210 var e2: i64 = 0
211 while e2 < EC {
212 var taken: i64 = 0
213 var q: i64 = 0
214 while q < ki { if sp[q] == e2 { taken = 1 } q = q + 1 }
215 if taken == 0 { if best < 0 { best = e2; bestv = r[e2] } else { if r[e2] > bestv { best = e2; bestv = r[e2] } } }
216 e2 = e2 + 1
217 }
218 sp[ki] = best
219 ki = ki + 1
220 }
221 pi = pi + 1
222 }
223 mr_w(" [route x1] experts:" as *u8)
224 i = 0
225 while i < EU { mr_w(" " as *u8); mr_n(sel1[i]); i = i + 1 }
226 mr_w("\n [route x2] experts:" as *u8)
227 i = 0
228 while i < EU { mr_w(" " as *u8); mr_n(sel2[i]); i = i + 1 }
229 mr_w("\n" as *u8)
230 var distinct1: i64 = 1
231 i = 0
232 while i < EU { var j2: i64 = i + 1; while j2 < EU { if sel1[i] == sel1[j2] { distinct1 = 0 } j2 = j2 + 1 } i = i + 1 }
233 var det_ok: i64 = 1
234 i = 0
235 while i < EU { if sel1[i] != selr[i] { det_ok = 0 } i = i + 1 }
236 var differs: i64 = 0
237 i = 0
238 while i < EU { var found: i64 = 0; var j3: i64 = 0; while j3 < EU { if sel2[j3] == sel1[i] { found = 1 } j3 = j3 + 1 } if found == 0 { differs = 1 } i = i + 1 }
239 ttl = ttl + 1
240 var ok4: i64 = distinct1 & det_ok
241 ok4 = ok4 & differs
242 mr_w(" T4 real routing: 8 distinct, deterministic repeat, second input differs: " as *u8)
243 if ok4 == 1 { pass = pass + 1; mr_w("PASS\n" as *u8) } else { mr_w("FAIL\n" as *u8) }
244
245 // T5 REAL MIX: lazily fetch the EU selected experts' gate/up/down slices, SwiGLU mix (softmax over selected)
246 let gs: *i64 = sys_mmap(FF*D*8) as *i64
247 let us: *i64 = sys_mmap(FF*D*8) as *i64
248 let ds: *i64 = sys_mmap(D*FF*8) as *i64
249 let g: *i64 = sys_mmap(FF*8) as *i64
250 let u: *i64 = sys_mmap(FF*8) as *i64
251 let h: *i64 = sys_mmap(FF*8) as *i64
252 let eo: *i64 = sys_mmap(D*8) as *i64
253 let out1: *i64 = sys_mmap(D*8) as *i64
254 let out2: *i64 = sys_mmap(D*8) as *i64
255 let wgt: *i64 = sys_mmap(EU*8) as *i64
256 var fetched: i64 = 0
257 var rep: i64 = 0
258 while rep < 2 {
259 var outp: *i64 = out1
260 if rep == 1 { outp = out2 }
261 mm_out_in(x1, Wr, r, 1, D, EC, 0)
262 var m: i64 = r[sel1[0]]
263 i = 1
264 while i < EU { if r[sel1[i]] > m { m = r[sel1[i]] } i = i + 1 }
265 var sum: i64 = 0
266 i = 0
267 while i < EU { let ev: i64 = fx_exp(r[sel1[i]] - m); wgt[i] = ev; sum = sum + ev; i = i + 1 }
268 if sum < 1 { sum = 1 }
269 i = 0
270 while i < EU { wgt[i] = (wgt[i] << 16) / sum; i = i + 1 }
271 var d3: i64 = 0
272 while d3 < D { outp[d3] = 0; d3 = d3 + 1 }
273 i = 0
274 while i < EU {
275 let e3: i64 = sel1[i]
276 dequant_to_q16(buf, bG + mr_val_byteoff(tG.ggml_type, e3*FF*D), tG.ggml_type, FF*D, gs)
277 dequant_to_q16(buf, bU + mr_val_byteoff(tU.ggml_type, e3*FF*D), tU.ggml_type, FF*D, us)
278 dequant_to_q16(buf, bD + mr_val_byteoff(tD.ggml_type, e3*D*FF), tD.ggml_type, D*FF, ds)
279 if rep == 0 { fetched = fetched + 1 }
280 mm_out_in(x1, gs, g, 1, D, FF, 0)
281 mm_out_in(x1, us, u, 1, D, FF, 0)
282 var j4: i64 = 0
283 while j4 < FF { h[j4] = qmul(silu(g[j4]), u[j4]); j4 = j4 + 1 }
284 mm_out_in(h, ds, eo, 1, FF, D, 0)
285 d3 = 0
286 while d3 < D { outp[d3] = outp[d3] + qmul(wgt[i], eo[d3]); d3 = d3 + 1 }
287 i = i + 1
288 }
289 rep = rep + 1
290 }
291 var nz: i64 = 0
292 var rep_mism: i64 = 0
293 i = 0
294 while i < D { if out1[i] != 0 { nz = nz + 1 } if out1[i] != out2[i] { rep_mism = rep_mism + 1 } i = i + 1 }
295 ttl = ttl + 1
296 var ok5: i64 = ((fetched == EU) as i64) & ((nz >= D/2) as i64)
297 ok5 = ok5 & ((rep_mism == 0) as i64)
298 mr_w(" T5 real mix: fetched " as *u8); mr_n(fetched); mr_w("/" as *u8); mr_n(EU)
299 mr_w(" experts lazily, nz " as *u8); mr_n(nz); mr_w("/" as *u8); mr_n(D)
300 mr_w(", repeat mism " as *u8); mr_n(rep_mism); mr_w(": " as *u8)
301 if ok5 == 1 { pass = pass + 1; mr_w("PASS\n" as *u8) } else { mr_w("FAIL\n" as *u8) }
302
303 mr_w("NX-NOFLOAT-MOE-REAL-GATE passed " as *u8); mr_n(pass); mr_w("/" as *u8); mr_n(ttl)
304 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
305 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
306 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
307 let ctr__dry: *i64 = gv_ctr()
308 ctr__dry[0] = pass
309 ctr__dry[1] = ttl
310 let rc__dry: i64 = gv_verdict("NOFLOAT-MOE-REAL-GATE" as *u8, ctr__dry, "real 64-expert MoE routes + lazily fetches + mixes in deterministic integer)" as *u8)
311 sys_exit(rc__dry)
312 return rc__dry
313}