nx_nofloat_olmoe_gate.nx source
↩ module page · 221 lines · 10717 B
1// nx_nofloat_olmoe_gate.nx -- MoE rung 4 (2026-07-15): a FAITHFUL OLMoE transformer LAYER on REAL blk.0
2// weights, end to end in deterministic no-float. Attention (pre-norm -> QKV -> QK-norm over full 2048 ->
3// RoPE base 1e4 -> 16-head causal MHA -> Wo -> residual) then MoE-FFN (ffn_norm -> lazy top-8 SwiGLU mix ->
4// residual). T=2 tokens (proves per-token routing + causal attention). Teeth:
5// T1 DETERMINISM: whole-layer output byte-identical on repeat
6// T2 REAL TRANSFORMATION: layer output differs from input on >half the cells (both tokens)
7// T3 PER-TOKEN ROUTING: the two tokens select DIFFERENT expert sets (real router, real per-token MoE)
8// T4 QK-NORM MATTERS (neg-control): skipping q_norm (gamma=1) changes the output -- proves QK-norm is live
9// T5 LAZY FETCH: exactly K=8 experts dequanted per token (cnt sum == T*K), attention output nonzero
10// Requires /home/elderwesto/nx_stage/nx_moe_model.gguf. ~4.2GB mapped (zero-copy). Return from main.
11// No hw writes (Rule 26). expect_exit: 0 license_tier: ORIGINAL
12import "nx_syscalls.nx"
13import "nx_tier.nx"
14import "nx_le.nx"
15import "nx_tensor.nx"
16import "nx_gguf.nx"
17import "nx_gguf_load.nx"
18import "nx_gguf_meta.nx"
19import "nx_nofloat_llm.nx"
20import "nx_nofloat_arch.nx"
21import "nx_nofloat_olmoe.nx"
22import "nx_gate_verdict.nx"
23
24func og_w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
25func og_n(v: i64) -> i64 {
26 var m: i64 = v
27 if m < 0 { og_w("-" as *u8); m = 0 - m }
28 let t: *u8 = sys_mmap(24)
29 var k: i64 = 0
30 if m == 0 { t[0] = 48 as u8; k = 1 }
31 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
32 let o: *u8 = sys_mmap(24)
33 var i: i64 = 0
34 while i < k { o[i] = t[k - 1 - i]; i = i + 1 }
35 sys_write(1, o, k)
36 return 0
37}
38func og_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
39func og_det(i: i64) -> i64 { return ((i * 2654435761) % 8191) - 4095 }
40// dequant a full 2-D weight tensor by name into an i64 buffer (attn Q/K/V/O are small enough to materialize).
41func og_load(buf: *u8, hdr: *NxGgufHeader, name: *u8, out: *i64, nvals: i64) -> i64 {
42 let idx: nx_int = nx_gguf_find_tensor(hdr, name, og_slen(name))
43 if idx < 0 { return 0 - 1 }
44 let ti: *NxGgufTensorInfo = nx_gguf_tensor_at(hdr, idx)
45 let hl: *NxGgufHeader = hdr
46 return dequant_to_q16(buf, hl.data_off + ti.offset, ti.ggml_type, nvals, out)
47}
48func og_expbase(buf: *u8, hdr: *NxGgufHeader, name: *u8, eb: *i64, slot: i64) -> i64 {
49 let idx: nx_int = nx_gguf_find_tensor(hdr, name, og_slen(name))
50 if idx < 0 { return 0 - 1 }
51 let ti: *NxGgufTensorInfo = nx_gguf_tensor_at(hdr, idx)
52 let hl: *NxGgufHeader = hdr
53 eb[slot] = hl.data_off + ti.offset
54 eb[slot + 1] = ti.ggml_type
55 return 0
56}
57
58func main() -> i64 {
59 og_w("=== NX-NOFLOAT-OLMOE -- faithful OLMoE layer (QK-norm attn + lazy MoE-FFN) on real blk.0 ===\n" as *u8)
60 let ln: *i64 = sys_mmap(8) as *i64
61 ln[0] = 0
62 let buf: *u8 = sys_map_file("/home/elderwesto/nx_stage/nx_moe_model.gguf" as *u8, ln)
63 if (buf as i64) == 0 { og_w("MODEL ABSENT\n" as *u8); return 1 }
64 let hdr: *NxGgufHeader = sys_mmap(NX_GGUF_HDR_BYTES) as *NxGgufHeader
65 if nx_gguf_parse(buf, ln[0], hdr) != NX_GGUF_OK { og_w("PARSE FAIL\n" as *u8); return 1 }
66 let cfg: *i64 = sys_mmap(16*8) as *i64
67 let arch: *u8 = sys_mmap(48)
68 if nac_read_config(buf, ln[0], hdr, cfg, arch) != 0 { og_w("ARCH FAIL\n" as *u8); return 1 }
69 let D: i64 = cfg[0]
70 let nh: i64 = cfg[2]
71 let hd: i64 = cfg[4]
72 let scale: i64 = cfg[8]
73 let ff: i64 = cfg[7]
74 let E: i64 = 64
75 let K: i64 = 8
76 og_w("[cfg] D=" as *u8); og_n(D); og_w(" heads=" as *u8); og_n(nh); og_w(" hd=" as *u8); og_n(hd)
77 og_w(" scale=" as *u8); og_n(scale); og_w(" ff=" as *u8); og_n(ff); og_w(" base=" as *u8); og_n(cfg[9]); og_w("\n" as *u8)
78
79 // per-layer weights (materialize the small attn set; experts stay lazy)
80 let gA: *i64 = sys_mmap(D*8) as *i64
81 let gQ: *i64 = sys_mmap(D*8) as *i64
82 let gK: *i64 = sys_mmap(D*8) as *i64
83 let gF: *i64 = sys_mmap(D*8) as *i64
84 let Wq: *i64 = sys_mmap(D*D*8) as *i64
85 let Wk: *i64 = sys_mmap(D*D*8) as *i64
86 let Wv: *i64 = sys_mmap(D*D*8) as *i64
87 let Wo: *i64 = sys_mmap(D*D*8) as *i64
88 let Wr: *i64 = sys_mmap(E*D*8) as *i64
89 og_load(buf, hdr, "blk.0.attn_norm.weight" as *u8, gA, D)
90 og_load(buf, hdr, "blk.0.attn_q_norm.weight" as *u8, gQ, D)
91 og_load(buf, hdr, "blk.0.attn_k_norm.weight" as *u8, gK, D)
92 og_load(buf, hdr, "blk.0.ffn_norm.weight" as *u8, gF, D)
93 og_load(buf, hdr, "blk.0.attn_q.weight" as *u8, Wq, D*D)
94 og_load(buf, hdr, "blk.0.attn_k.weight" as *u8, Wk, D*D)
95 og_load(buf, hdr, "blk.0.attn_v.weight" as *u8, Wv, D*D)
96 og_load(buf, hdr, "blk.0.attn_output.weight" as *u8, Wo, D*D)
97 og_load(buf, hdr, "blk.0.ffn_gate_inp.weight" as *u8, Wr, E*D)
98 let eb: *i64 = sys_mmap(8*8) as *i64
99 og_expbase(buf, hdr, "blk.0.ffn_gate_exps.weight" as *u8, eb, 0)
100 og_expbase(buf, hdr, "blk.0.ffn_up_exps.weight" as *u8, eb, 2)
101 og_expbase(buf, hdr, "blk.0.ffn_down_exps.weight" as *u8, eb, 4)
102
103 let freqs: *i64 = sys_mmap((hd/2)*8) as *i64
104 rope_freqs_base(freqs, hd, cfg[11]) // base 1e4 rope (cfg[11]=ln(base))
105
106 let T: i64 = 2
107 let x: *i64 = sys_mmap(T*D*8) as *i64
108 var i: i64 = 0
109 while i < T*D { x[i] = og_det(i*7 + 13); i = i + 1 }
110
111 let attnout: *i64 = sys_mmap(T*D*8) as *i64
112 let xmid: *i64 = sys_mmap(T*D*8) as *i64
113 let moeout: *i64 = sys_mmap(T*D*8) as *i64
114 let out1: *i64 = sys_mmap(T*D*8) as *i64
115 let out2: *i64 = sys_mmap(T*D*8) as *i64
116 let ascr: *i64 = sys_mmap((4*T*D + 2*T + T*D + 64)*8) as *i64
117 let mscr: *i64 = sys_mmap((D + E + 2*K + 2*ff*D + D*ff + 3*ff + D + 64)*8) as *i64
118 let cnt: *i64 = sys_mmap(E*8) as *i64
119 let selmask: *i64 = sys_mmap(T*8) as *i64
120
121 let ap: *i64 = sys_mmap(16*8) as *i64
122 let mp: *i64 = sys_mmap(16*8) as *i64
123
124 // ---- run the full layer (attn + residual, moe + residual) into `outp` ----
125 // (inlined twice for determinism; second run into out2)
126 var run: i64 = 0
127 while run < 2 {
128 var outp: *i64 = out1
129 if run == 1 { outp = out2 }
130 var cz: i64 = 0
131 while cz < E { cnt[cz] = 0; cz = cz + 1 }
132 ap[0]=x as i64; ap[1]=attnout as i64; ap[2]=T; ap[3]=D; ap[4]=nh; ap[5]=hd; ap[6]=scale
133 ap[7]=gA as i64; ap[8]=Wq as i64; ap[9]=Wk as i64; ap[10]=Wv as i64; ap[11]=Wo as i64
134 ap[12]=gQ as i64; ap[13]=gK as i64; ap[14]=freqs as i64; ap[15]=ascr as i64
135 nolmoe_attn(ap)
136 var d: i64 = 0
137 while d < T*D { xmid[d] = x[d] + attnout[d]; d = d + 1 }
138 mp[0]=xmid as i64; mp[1]=moeout as i64; mp[2]=T; mp[3]=D; mp[4]=ff; mp[5]=E; mp[6]=K
139 mp[7]=gF as i64; mp[8]=Wr as i64; mp[9]=buf as i64; mp[10]=eb as i64; mp[11]=mscr as i64; mp[12]=cnt as i64
140 nolmoe_moe(mp)
141 d = 0
142 while d < T*D { outp[d] = xmid[d] + moeout[d]; d = d + 1 }
143 run = run + 1
144 }
145 // record per-token expert masks from a fresh routed pass
146 var mt: i64 = 0
147 while mt < T {
148 var cz2: i64 = 0
149 while cz2 < E { cnt[cz2] = 0; cz2 = cz2 + 1 }
150 mp[0]=((xmid as i64) + mt*D*8); mp[1]=((moeout as i64) + mt*D*8); mp[2]=1
151 nolmoe_moe(mp)
152 var msk: i64 = 0
153 cz2 = 0
154 while cz2 < E { if cnt[cz2] > 0 { msk = msk | (1 << cz2) } cz2 = cz2 + 1 }
155 selmask[mt] = msk
156 og_w(" tok" as *u8); og_n(mt); og_w(" expert-mask=" as *u8); og_n(msk); og_w("\n" as *u8)
157 mt = mt + 1
158 }
159 mp[2] = T
160
161 // ---- teeth ----
162 var pass: i64 = 0
163 var ttl: i64 = 0
164 ttl = ttl + 1
165 var repm: i64 = 0
166 i = 0
167 while i < T*D { if out1[i] != out2[i] { repm = repm + 1 } i = i + 1 }
168 og_w(" T1 whole-layer determinism (repeat byte-identical, mism " as *u8); og_n(repm); og_w("): " as *u8)
169 if repm == 0 { pass = pass + 1; og_w("PASS\n" as *u8) } else { og_w("FAIL\n" as *u8) }
170 ttl = ttl + 1
171 var neq: i64 = 0
172 i = 0
173 while i < T*D { if out1[i] != x[i] { neq = neq + 1 } i = i + 1 }
174 og_w(" T2 real transformation (out!=in on " as *u8); og_n(neq); og_w("/" as *u8); og_n(T*D); og_w("): " as *u8)
175 if neq > T*D/2 { pass = pass + 1; og_w("PASS\n" as *u8) } else { og_w("FAIL\n" as *u8) }
176 ttl = ttl + 1
177 og_w(" T3 per-token routing (tok0 mask " as *u8); og_n(selmask[0]); og_w(" != tok1 mask " as *u8); og_n(selmask[1]); og_w("): " as *u8)
178 if selmask[0] != selmask[1] { pass = pass + 1; og_w("PASS\n" as *u8) } else { og_w("FAIL\n" as *u8) }
179 ttl = ttl + 1
180 // T4 QK-norm neg-control: run attention with gQ = all-ones (Q16) -> attn output must differ
181 let ones: *i64 = sys_mmap(D*8) as *i64
182 i = 0
183 while i < D { ones[i] = 65536; i = i + 1 }
184 let attn2: *i64 = sys_mmap(T*D*8) as *i64
185 ap[1]=attn2 as i64
186 ap[12]=ones as i64
187 nolmoe_attn(ap)
188 ap[1]=attnout as i64
189 ap[12]=gQ as i64
190 var qkdiff: i64 = 0
191 i = 0
192 while i < T*D { if attn2[i] != attnout[i] { qkdiff = qkdiff + 1 } i = i + 1 }
193 og_w(" T4 QK-norm matters (skip q_norm -> attn differs on " as *u8); og_n(qkdiff); og_w(" cells): " as *u8)
194 if qkdiff > 0 { pass = pass + 1; og_w("PASS\n" as *u8) } else { og_w("FAIL\n" as *u8) }
195 ttl = ttl + 1
196 // T5 lazy fetch count == T*K on a fresh full pass + attn nonzero
197 var cz3: i64 = 0
198 while cz3 < E { cnt[cz3] = 0; cz3 = cz3 + 1 }
199 mp[0]=xmid as i64; mp[1]=moeout as i64; mp[2]=T
200 nolmoe_moe(mp)
201 var fetched: i64 = 0
202 cz3 = 0
203 while cz3 < E { fetched = fetched + cnt[cz3]; cz3 = cz3 + 1 }
204 var anz: i64 = 0
205 i = 0
206 while i < T*D { if attnout[i] != 0 { anz = anz + 1 } i = i + 1 }
207 og_w(" T5 lazy fetch total=" as *u8); og_n(fetched); og_w(" (want " as *u8); og_n(T*K); og_w("), attn nonzero " as *u8); og_n(anz); og_w(": " as *u8)
208 var ok5: i64 = ((fetched == T*K) as i64) & ((anz > T*D/2) as i64)
209 if ok5 == 1 { pass = pass + 1; og_w("PASS\n" as *u8) } else { og_w("FAIL\n" as *u8) }
210
211 og_w("NX-NOFLOAT-OLMOE-GATE passed " as *u8); og_n(pass); og_w("/" as *u8); og_n(ttl)
212 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
213 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
214 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
215 let ctr__dry: *i64 = gv_ctr()
216 ctr__dry[0] = pass
217 ctr__dry[1] = ttl
218 let rc__dry: i64 = gv_verdict("NOFLOAT-OLMOE-GATE" as *u8, ctr__dry, "real OLMoE layer: QK-norm attention + per-token lazy MoE, deterministic integer)" as *u8)
219 sys_exit(rc__dry)
220 return rc__dry
221}