interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / tests / moe / caller_wide.c

367 lines · 22142 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* Native ABI caller for the _wide MoE host units (Qwen3.8-Flash-Next: 512 experts,
3 * 10 routed picks + shared = 11 contributions, hidden 2560, intermediate 640).
4 * Oracles: the unchanged legacy units at the Qwen3.6-35B shape, an independent
5 * C stable sort below, and the Omega rank verifier moe_verify_wide.om.
6 * Test logic only, not production. No GPU library or device node is opened. */
7#include "cudapp_native.h"
8#include <stdint.h>
9#include <stdio.h>
10#include <string.h>
11#include <stdlib.h>
12#define CHECK(x) do { if (!(x)) { fprintf(stderr,"FAIL line %d: %s\n",__LINE__,#x); exit(1); } } while (0)
13#define CANARY UINT64_C(0x5a5a5a5a5a5a5a5a)
14enum { IDS = 1280, WORK = 12672, SEG = 2052, MAPS = 2832, YB = 1416, DIRS = 1026, MAPW = 11328, SEGW = 16416 };
15/* Wide planner state. */
16static uint64_t cfg[8], meta[8], ids[IDS], work[WORK], seg[SEG], maps[MAPS], dirs[DIRS];
17static uint8_t mapwire[MAPW], segwire[SEGW], idwire[IDS * 4];
18/* Legacy planner state (the oracle at the 35B shape). */
19static uint64_t lcfg[8], lmeta[8], lids[1024], lwork[10368], lseg[1028], lmaps[2320], ldirs[514];
20static uint8_t lmapwire[9280], lsegwire[8224];
21/* Independent reference. */
22static uint64_t rwork[WORK], rseg[SEG], rmaps[MAPS], rmeta[8];
23static uint32_t order[1408];
24static uint64_t req[16];
25static uint32_t words[16], lwords[16];
26static uint64_t wcfg[8], wout[1024], wmeta[4], lwout[1024], lwmeta[4];
27static unsigned planned, refused;
28
29static uint64_t plan(void) {
30 uint64_t r = 99;
31 CHECK(omega_moe_plan_wide(&r,cfg,8,ids,IDS,work,WORK,seg,SEG,maps,MAPS,meta,8)==0);
32 return r;
33}
34static uint64_t verify(void) {
35 uint64_t r = 99;
36 CHECK(omega_moe_verify_wide(&r,cfg,8,ids,IDS,work,WORK,seg,SEG,maps,MAPS,meta,8)==0);
37 return r;
38}
39static void setup(uint32_t n, uint32_t slots, uint32_t experts) {
40 uint64_t c[8] = {2,n,slots,experts,experts,1408,513,1416}; memcpy(cfg,c,sizeof c);
41 memset(ids,0,sizeof ids); memset(work,0x5a,sizeof work); memset(seg,0x5a,sizeof seg);
42 memset(maps,0x5a,sizeof maps); memset(meta,0x5a,sizeof meta);
43}
44/* Independent oracle: insertion sort (stable) of contribution rows by expert.
45 * Row order before sorting: routed rows i = t*slots+s (absent skipped), then shared t. */
46static void reference(void) {
47 uint32_t n = (uint32_t)cfg[1], slots = (uint32_t)cfg[2], experts = (uint32_t)cfg[3], nw = 0, nr = 0;
48 for (uint32_t i = 0; i < n * slots; i++) if (ids[i] != UINT32_MAX) order[nw++] = i, nr++;
49 for (uint32_t t = 0; t < n; t++) order[nw++] = n * slots + t;
50#define EXPERT(x) ((x) < n * slots ? (uint32_t)ids[x] : experts)
51 for (uint32_t j = 1; j < nw; j++) {
52 uint32_t x = order[j], k = j;
53 while (k > 0 && EXPERT(order[k - 1]) > EXPERT(x)) { order[k] = order[k - 1]; k--; }
54 order[k] = x;
55 }
56 uint32_t ns = 0;
57 for (uint32_t p = 0; p < nw; p++) {
58 uint32_t e = EXPERT(order[p]);
59 if (p == 0 || EXPERT(order[p - 1]) != e) {
60 uint32_t c = 0;
61 while (p + c < nw && EXPERT(order[p + c]) == e) c++;
62 rseg[ns * 4] = e; rseg[ns * 4 + 1] = p; rseg[ns * 4 + 2] = c; rseg[ns * 4 + 3] = e == experts;
63 ns++;
64 }
65 }
66 for (uint32_t p = 0, z = 0; p < nw; p++) {
67 uint32_t x = order[p], e = EXPERT(x), t, s;
68 if (x < n * slots) { t = x / slots; s = x % slots; } else { t = x - n * slots; s = slots; }
69 while (rseg[z * 4] != e) z++;
70 uint64_t rec[9] = {e,t,t,s,s,rseg[z * 4 + 1],rseg[z * 4 + 2],(uint64_t)t * (slots + 1) + s,s};
71 memcpy(rwork + p * 9, rec, sizeof rec);
72 rmaps[p] = t; rmaps[YB + p] = (uint64_t)t * (slots + 1) + s;
73 }
74#undef EXPERT
75 for (uint32_t k = 0; k < 8; k++) rmaps[nw + k] = rmaps[YB + nw + k] = 0;
76 uint64_t m[8] = {nw,ns,nr,n,2,0,0,0}; memcpy(rmeta,m,sizeof m);
77}
78/* Planner output equals the reference exactly and every unused word keeps its canary. */
79static void compare_reference(void) {
80 reference();
81 uint32_t nw = (uint32_t)rmeta[0], ns = (uint32_t)rmeta[1];
82 CHECK(!memcmp(meta,rmeta,sizeof meta));
83 CHECK(!memcmp(work,rwork,nw * 9 * sizeof *work));
84 for (uint32_t j = nw * 9; j < WORK; j++) CHECK(work[j]==CANARY);
85 CHECK(!memcmp(seg,rseg,ns * 4 * sizeof *seg));
86 for (uint32_t j = ns * 4; j < SEG; j++) CHECK(seg[j]==CANARY);
87 for (uint32_t z = 0; z < 2; z++) {
88 CHECK(!memcmp(maps + z * YB,rmaps + z * YB,(nw + 8) * sizeof *maps));
89 for (uint32_t j = z * YB + nw + 8; j < (z + 1) * YB; j++) CHECK(maps[j]==CANARY);
90 }
91}
92static void fill_dirs(void) {
93 for (uint32_t j = 0; j < 513; j++) { dirs[j*2] = (uint64_t)j*256+4096; dirs[j*2+1] = dirs[j*2]+128; }
94}
95/* Byte-exact serializers against the reference words, with untouched canaries. */
96static void wires(void) {
97 uint64_t r = 99;
98 uint32_t nw = (uint32_t)meta[0], ns = (uint32_t)meta[1];
99 memset(mapwire,0x5a,sizeof mapwire); memset(segwire,0x5a,sizeof segwire); fill_dirs();
100 CHECK(omega_moe_maps32_wide(&r,meta,8,maps,MAPS,mapwire,MAPW)==0 && r==0);
101 for (uint32_t z = 0; z < 2; z++) {
102 for (uint32_t j = 0; j < nw + 8; j++) {
103 uint32_t v; memcpy(&v,mapwire + (z * YB + j) * 4,4);
104 CHECK(v==rmaps[z * YB + j]);
105 }
106 for (uint32_t b = (z * YB + nw + 8) * 4; b < (z + 1) * YB * 4; b++) CHECK(mapwire[b]==0x5a);
107 }
108 CHECK(omega_moe_segs32_wide(&r,seg,SEG,meta,8,dirs,DIRS,segwire,SEGW)==0 && r==0);
109 for (uint32_t z = 0; z < ns; z++) {
110 uint64_t addr[2], reserved; uint32_t range[2];
111 memcpy(addr,segwire+z*32,16); memcpy(range,segwire+z*32+16,8); memcpy(&reserved,segwire+z*32+24,8);
112 CHECK(addr[0]==(uint64_t)rseg[z*4]*256+4096 && addr[1]==addr[0]+128);
113 CHECK(range[0]==rseg[z*4+1] && range[1]==rseg[z*4+2] && reserved==0);
114 }
115 for (uint32_t b = ns * 32; b < SEGW; b++) CHECK(segwire[b]==0x5a);
116}
117/* The rank verifier must reject each single-word corruption, then accept the restored plan. */
118static void corrupt(void) {
119 uint64_t v = work[8]; work[8] = v + 1; CHECK(verify()!=0); work[8] = v;
120 uint32_t p = (uint32_t)meta[2] * 9; v = work[p]; work[p] = 0; CHECK(verify()!=0); work[p] = v;
121 v = maps[YB]; maps[YB] = UINT32_MAX; CHECK(verify()!=0); maps[YB] = v;
122 v = seg[2]; seg[2] = 0; CHECK(verify()!=0); seg[2] = v;
123 CHECK(verify()==0);
124}
125static uint32_t pick(uint32_t mode, uint32_t t, uint32_t s, uint32_t slots, uint32_t experts) {
126 switch (mode) {
127 case 0: return 7; /* all same, duplicate picks */
128 case 1: return (t * slots + s) % experts; /* distinct where possible */
129 case 2: return s % 3; /* duplicate within token */
130 case 3: return t % 2 ? experts - 1 : s; /* uneven occupancy */
131 case 4: return (t * 17 + s * 13) % experts; /* ragged segment sizes */
132 case 5: return (t + s) % 3 == 0 ? UINT32_MAX : (s % 2 ? experts - 1 : 0); /* extremes + absent */
133 default: return experts - 1; /* last routed beside shared */
134 }
135}
136static void dense(uint32_t n, uint32_t slots, uint32_t experts, uint32_t mode) {
137 setup(n,slots,experts);
138 for (uint32_t t=0;t<n;t++) for (uint32_t s=0;s<slots;s++) ids[t*slots+s]=pick(mode,t,s,slots,experts);
139 CHECK(plan()==0); CHECK(verify()==0);
140 compare_reference(); wires();
141 if (n) corrupt();
142 planned++;
143}
144/* (a) The 35B shape through the wide units equals the unchanged legacy units. */
145static void legacy_equal(uint32_t n, uint32_t mode) {
146 dense(n,8,256,mode);
147 uint64_t c[8] = {1,n,8,256,256,1152,257,1160}; memcpy(lcfg,c,sizeof c);
148 memset(lids,0,sizeof lids); memcpy(lids,ids,n * 8 * sizeof *ids);
149 memset(lwork,0x5a,sizeof lwork); memset(lseg,0x5a,sizeof lseg); memset(lmaps,0x5a,sizeof lmaps);
150 uint64_t r = 99;
151 CHECK(omega_moe_plan(&r,lcfg,8,lids,1024,lwork,10368,lseg,1028,lmaps,2320,lmeta,8)==0 && r==0);
152 uint32_t nw = (uint32_t)meta[0], ns = (uint32_t)meta[1];
153 CHECK(lmeta[0]==meta[0] && lmeta[1]==meta[1] && lmeta[2]==meta[2] && lmeta[3]==meta[3]);
154 CHECK(lmeta[4]==1 && meta[4]==2);
155 CHECK(!memcmp(lwork,work,nw * 9 * sizeof *work));
156 CHECK(!memcmp(lseg,seg,ns * 4 * sizeof *seg));
157 CHECK(!memcmp(lmaps,maps,(nw + 8) * sizeof *maps));
158 CHECK(!memcmp(lmaps + 1160,maps + YB,(nw + 8) * sizeof *maps));
159 for (uint32_t j = 0; j < 257; j++) { ldirs[j*2] = dirs[j*2]; ldirs[j*2+1] = dirs[j*2+1]; }
160 CHECK(omega_moe_maps32(&r,lmeta,8,lmaps,2320,lmapwire,9280)==0 && r==0);
161 CHECK(!memcmp(lmapwire,mapwire,(nw + 8) * 4));
162 CHECK(!memcmp(lmapwire + 1160 * 4,mapwire + YB * 4,(nw + 8) * 4));
163 CHECK(omega_moe_segs32(&r,lseg,1028,lmeta,8,ldirs,514,lsegwire,8224)==0 && r==0);
164 CHECK(!memcmp(lsegwire,segwire,ns * 32));
165}
166/* (d) Refusal: the exact code, and every output word unchanged. */
167static void refusal(uint64_t expected) {
168 static uint64_t w[WORK], s[SEG], p[MAPS], m[8];
169 memcpy(w,work,sizeof w); memcpy(s,seg,sizeof s); memcpy(p,maps,sizeof p); memcpy(m,meta,sizeof m);
170 CHECK(plan()==expected); CHECK(!memcmp(w,work,sizeof w)); CHECK(!memcmp(s,seg,sizeof s));
171 CHECK(!memcmp(p,maps,sizeof p)); CHECK(!memcmp(m,meta,sizeof m));
172 refused++;
173}
174/* Passing twin from the same run: one field away from the refusal, the plan is accepted. */
175static void twin(void) {
176 memset(work,0x5a,sizeof work); memset(seg,0x5a,sizeof seg); memset(maps,0x5a,sizeof maps);
177 CHECK(plan()==0); CHECK(verify()==0); compare_reference();
178}
179#define REFUSE(n,slots,experts,mode,field,bad,good,rc) do { \
180 dense(n,slots,experts,mode); cfg[field]=bad; refusal(rc); cfg[field]=good; twin(); } while (0)
181static uint64_t group(void) {
182 uint64_t r=99; CHECK(omega_moe_group_wide(&r,req,16,(uint8_t *)words,64)==0); return r;
183}
184static uint64_t lgroup(void) {
185 uint64_t r=99; CHECK(omega_moe_group(&r,req,16,(uint8_t *)lwords,64)==0); return r;
186}
187static void group_words(uint32_t stride, uint32_t tokens) {
188 for (unsigned i=0;i<7;i++) { CHECK(words[i*2]==(uint32_t)req[i]); CHECK(words[i*2+1]==(uint32_t)(req[i]>>32)); }
189 CHECK(words[14]==stride && words[15]==tokens);
190}
191/* (g) Windows for one profile: tokens 0..128 tile exactly, rows never exceed GROUP's 128. */
192static void windows(uint64_t h, uint64_t ei, uint64_t c, uint64_t take) {
193 uint64_t result = 99;
194 for (unsigned n=0;n<=128;n++) {
195 uint64_t wc[8]={1,n,h,ei,c,512,128,0}; memcpy(wcfg,wc,sizeof wc);
196 memset(wout,0x5a,sizeof wout);
197 CHECK(omega_moe_windows_wide(&result,wcfg,8,wout,1024,wmeta,4)==0 && result==0);
198 uint64_t first=0;
199 for (unsigned z=0;z<wmeta[0];z++) {
200 uint64_t *v=wout+z*8, left=n-first;
201 CHECK(v[0]==first && v[1]==(left<take?left:take) && v[2]==v[1]*c && v[2]<=128);
202 CHECK(v[3]==first*h*4 && v[4]==first*c*ei*2*4);
203 CHECK(v[5]==first*c*ei*4 && v[6]==first*c*h*4 && v[7]==first*512);
204 first+=v[1];
205 }
206 for (unsigned j=wmeta[0]*8;j<1024;j++) CHECK(wout[j]==CANARY);
207 CHECK(first==n && wmeta[0]==(n+take-1)/take && wmeta[1]==n && wmeta[2]==n*c && wmeta[3]==1);
208 if (c==9) {
209 CHECK(omega_moe_windows(&result,wcfg,8,lwout,1024,lwmeta,4)==0 && result==0);
210 CHECK(!memcmp(lwout,wout,wmeta[0]*8*sizeof *wout) && !memcmp(lwmeta,wmeta,sizeof wmeta));
211 } else {
212 /* The legacy unit refuses this profile; the wide unit is the only accepting path. */
213 CHECK(omega_moe_windows(&result,wcfg,8,lwout,1024,lwmeta,4)==0 && result==1);
214 }
215 }
216}
217static void window_refusal(unsigned field, uint64_t bad, uint64_t rc) {
218 uint64_t result = 99, ow[1024], om[4], save = wcfg[field];
219 CHECK(omega_moe_windows_wide(&result,wcfg,8,wout,1024,wmeta,4)==0 && result==0); /* twin */
220 memcpy(ow,wout,sizeof ow); memcpy(om,wmeta,sizeof om);
221 wcfg[field] = bad;
222 CHECK(omega_moe_windows_wide(&result,wcfg,8,wout,1024,wmeta,4)==0 && result==rc);
223 CHECK(!memcmp(ow,wout,sizeof ow) && !memcmp(om,wmeta,sizeof om));
224 wcfg[field] = save; refused++;
225}
226int main(void) {
227 /* (a) 35B shape: wide == legacy, for the legacy suite's sizes and modes. */
228 const uint32_t lsizes[]={0,1,2,3,7,14,15,31,127,128};
229 for (unsigned j=0;j<sizeof lsizes/sizeof *lsizes;j++) for (unsigned k=0;k<5;k++) legacy_equal(lsizes[j],k);
230 /* (b) Flash-Next shape: 512 experts, 10 routed picks, shared id 512. */
231 const uint32_t fsizes[]={0,1,2,3,7,10,11,12,31,127,128};
232 for (unsigned j=0;j<sizeof fsizes/sizeof *fsizes;j++) for (unsigned k=0;k<7;k++) dense(fsizes[j],10,512,k);
233 /* Routed expert 511 and shared expert 512 stay two segments; 128 tokens fill work exactly. */
234 dense(128,10,512,6);
235 CHECK(meta[0]==1408 && meta[1]==2 && meta[2]==1280);
236 CHECK(seg[0]==511 && seg[1]==0 && seg[2]==1280 && seg[3]==0);
237 CHECK(seg[4]==512 && seg[5]==1280 && seg[6]==128 && seg[7]==1);
238 /* All 512 routed experts plus shared: 513 segments, the segment capacity. */
239 dense(128,10,512,1); CHECK(meta[1]==513 && seg[511*4]==511 && seg[512*4]==512 && seg[512*4+3]==1);
240 /* Absent routes keep their original slots; all-absent leaves only the shared rows. */
241 setup(3,10,512); for (unsigned j=0;j<30;j++) ids[j]=j%4 ? UINT32_MAX : j*17;
242 CHECK(plan()==0); CHECK(verify()==0); compare_reference();
243 setup(128,10,512); for (unsigned j=0;j<IDS;j++) ids[j]=UINT32_MAX;
244 CHECK(plan()==0); CHECK(verify()==0); compare_reference(); CHECK(meta[0]==128 && meta[2]==0 && meta[1]==1);
245 setup(5,0,512); CHECK(plan()==0); CHECK(verify()==0); CHECK(meta[0]==5 && meta[2]==0);
246 /* (d) Capacity refusals (3), each with its passing twin. */
247 REFUSE(128,10,512,6,5,1407,1408,3);
248 REFUSE(128,10,512,6,7,1415,1416,3);
249 REFUSE(128,10,512,1,6,512,513,3);
250 /* Id refusals (2): the first id outside 0..511, a large u32, and a value above u32. */
251 dense(1,10,512,6); ids[3]=512; refusal(2); ids[3]=511; twin();
252 ids[3]=UINT32_MAX-1; refusal(2); ids[3]=0; twin();
253 ids[3]=UINT64_C(4294967296); refusal(2); ids[3]=UINT32_MAX; twin();
254 /* Config refusals (1). The wide unit is version 2 only. */
255 REFUSE(1,10,512,6,0,1,2,1);
256 REFUSE(1,10,512,6,1,129,128,1);
257 REFUSE(1,10,512,6,2,11,10,1);
258 REFUSE(1,10,512,6,3,0,512,1);
259 dense(1,10,512,6); cfg[3]=513; cfg[4]=513; refusal(1); cfg[3]=512; cfg[4]=512; twin();
260 REFUSE(1,10,512,6,4,511,512,1);
261 REFUSE(1,10,512,6,5,1409,1408,1);
262 REFUSE(1,10,512,6,6,514,513,1);
263 REFUSE(1,10,512,6,7,1417,1416,1);
264 /* The legacy unit refuses the Flash-Next config (version 2 and 10 slots / 512 experts). */
265 {
266 uint64_t r = 99, c[8] = {2,1,10,512,512,1152,257,1160};
267 memset(lwork,0x5a,sizeof lwork);
268 CHECK(omega_moe_plan(&r,c,8,lids,1024,lwork,10368,lseg,1028,lmaps,2320,lmeta,8)==0 && r==1);
269 c[0]=1; CHECK(omega_moe_plan(&r,c,8,lids,1024,lwork,10368,lseg,1028,lmaps,2320,lmeta,8)==0 && r==1);
270 CHECK(lwork[0]==CANARY);
271 }
272 /* Bad view lengths and aliased outputs are refused by the bridge before the unit runs. */
273 dense(1,10,512,6); uint64_t result=77;
274 CHECK(omega_moe_plan_wide(&result,cfg,8,ids,IDS,work,WORK,work,SEG,maps,MAPS,meta,8)==OMEGA_BAD_ARG);
275 CHECK(result==77);
276 CHECK(omega_moe_plan_wide(&result,cfg,8,ids,1024,work,WORK,seg,SEG,maps,MAPS,meta,8)==OMEGA_BAD_ARG);
277 CHECK(omega_moe_plan_wide(&result,cfg,8,ids,IDS,work,10368,seg,SEG,maps,MAPS,meta,8)==OMEGA_BAD_ARG);
278 CHECK(omega_moe_plan_wide(&result,cfg,8,ids,IDS,work,WORK,seg,SEG,maps,2320,meta,8)==OMEGA_BAD_ARG);
279 CHECK(result==77);
280 CHECK(omega_moe_plan_wide(&result,cfg,8,ids,IDS,work,WORK,seg,SEG,maps,MAPS,meta,8)==0 && result==0);
281 /* (e) ids32_wide: 1280 little-endian u32 picks, then the 1281st refused, ids unchanged. */
282 for (unsigned j=0;j<IDS;j++) { uint32_t v=(j*7919u)%513u; if (j==5) v=UINT32_MAX; memcpy(idwire+j*4,&v,4); }
283 memset(ids,0x5a,sizeof ids);
284 CHECK(omega_moe_ids32_wide(&result,idwire,IDS*4,IDS,ids,IDS)==0 && result==0);
285 for (unsigned j=0;j<IDS;j++) CHECK(ids[j]==(j==5 ? UINT32_MAX : (j*7919u)%513u));
286 memset(ids,0x5a,sizeof ids);
287 CHECK(omega_moe_ids32_wide(&result,idwire,IDS*4,IDS+1,ids,IDS)==0 && result==1);
288 for (unsigned j=0;j<IDS;j++) CHECK(ids[j]==CANARY);
289 CHECK(omega_moe_ids32_wide(&result,idwire,IDS*4,3,ids,IDS)==0 && result==0);
290 CHECK(ids[2]==(2*7919u)%513u && ids[3]==CANARY);
291 CHECK(omega_moe_ids32_wide(&result,idwire,4096,3,ids,IDS)==OMEGA_BAD_ARG);
292 /* maps32_wide refusals with twins: an out-of-u32 value (2), a work count above 1408 (1). */
293 dense(128,10,512,6); wires();
294 memset(mapwire,90,sizeof mapwire); maps[YB+1407]=UINT64_C(4294967296);
295 CHECK(omega_moe_maps32_wide(&result,meta,8,maps,MAPS,mapwire,MAPW)==0 && result==2);
296 for (unsigned j=0;j<sizeof mapwire;j++) CHECK(mapwire[j]==90);
297 maps[YB+1407]=UINT32_MAX;
298 CHECK(omega_moe_maps32_wide(&result,meta,8,maps,MAPS,mapwire,MAPW)==0 && result==0);
299 CHECK(mapwire[(YB+1407)*4]==255 && mapwire[(YB+1407)*4+3]==255);
300 dense(128,10,512,6); memset(mapwire,90,sizeof mapwire); meta[0]=1409;
301 CHECK(omega_moe_maps32_wide(&result,meta,8,maps,MAPS,mapwire,MAPW)==0 && result==1);
302 for (unsigned j=0;j<sizeof mapwire;j++) CHECK(mapwire[j]==90);
303 meta[0]=1408; CHECK(omega_moe_maps32_wide(&result,meta,8,maps,MAPS,mapwire,MAPW)==0 && result==0);
304 /* segs32_wide refusals with twins: directory (3), segment rows (2), capacities (1). */
305 dense(128,10,512,1); fill_dirs();
306#define SEGS_REFUSED(rc) do { memset(segwire,90,sizeof segwire); \
307 CHECK(omega_moe_segs32_wide(&result,seg,SEG,meta,8,dirs,DIRS,segwire,SEGW)==0 && result==(rc)); \
308 for (unsigned j=0;j<sizeof segwire;j++) { CHECK(segwire[j]==90); } refused++; } while (0)
309#define SEGS_OK() do { CHECK(omega_moe_segs32_wide(&result,seg,SEG,meta,8,dirs,DIRS,segwire,SEGW)==0 && result==0); } while (0)
310 dirs[1024]=0; SEGS_REFUSED(3); dirs[1024]=512*256+4096; SEGS_OK();
311 dirs[1023]=4100; SEGS_REFUSED(3); dirs[1023]=511*256+4096+128; SEGS_OK();
312 seg[512*4]=513; SEGS_REFUSED(2); seg[512*4]=512; SEGS_OK();
313 seg[2]=UINT64_MAX; SEGS_REFUSED(2); seg[2]=3; SEGS_OK();
314 meta[1]=514; SEGS_REFUSED(1); meta[1]=513; SEGS_OK();
315 meta[0]=1409; SEGS_REFUSED(1); meta[0]=1408; SEGS_OK();
316 /* (f) GROUP words. The 35B profile through the wide unit equals the legacy unit. */
317 uint64_t q9[16]={4096,8192,12288,65536,131072,196608,262144,512,14,126,134,9,1,1,1,256};
318 memcpy(req,q9,sizeof q9); CHECK(group()==0); CHECK(lgroup()==0);
319 CHECK(!memcmp(words,lwords,sizeof words)); group_words(512,14);
320 uint32_t before[16]; memcpy(before,words,sizeof words);
321#define BAD9(field,value,rc) do { uint64_t save=req[field]; req[field]=value; memset(lwords,0,sizeof lwords); \
322 CHECK(group()==rc); CHECK(lgroup()==rc); CHECK(!memcmp(before,words,sizeof words)); req[field]=save; \
323 CHECK(group()==0); refused++; } while(0)
324 BAD9(8,15,1); BAD9(8,0,1); BAD9(11,8,1); BAD9(15,255,1);
325 BAD9(9,125,2); BAD9(10,133,2); BAD9(7,0,2); BAD9(7,513,3); BAD9(0,4097,3);
326 BAD9(12,0,4); BAD9(13,2,4); BAD9(14,0,4);
327 BAD9(3,4096,5); BAD9(4,65536,5); BAD9(5,65536,5); BAD9(6,196608,5);
328 BAD9(0,UINT64_MAX-7,5); BAD9(3,UINT64_MAX-7,5); BAD9(7,128,2); BAD9(8,UINT64_MAX,1);
329 BAD9(15,512,1); BAD9(11,11,1);
330 /* Flash-Next profile: 11 contributions per token, shared id 512, tokens <= 11. */
331 uint64_t q11[16]={4096,8192,12288,65536,131072,196608,262144,512,11,121,129,11,1,1,1,512};
332 memcpy(req,q11,sizeof q11); CHECK(group()==0); group_words(512,11);
333 CHECK(lgroup()==1); /* the legacy unit refuses the 11-contribution profile */
334 memcpy(before,words,sizeof words);
335#define BAD11(field,value,rc,good) do { req[field]=value; CHECK(group()==rc); \
336 CHECK(!memcmp(before,words,sizeof words)); req[field]=good; CHECK(group()==0); \
337 memcpy(before,words,sizeof words); refused++; } while(0)
338 BAD11(8,12,1,11); BAD11(8,0,1,11); BAD11(8,UINT64_MAX,1,11);
339 BAD11(11,9,1,11); BAD11(11,10,1,11); BAD11(15,256,1,512); BAD11(15,511,1,512);
340 BAD11(9,120,2,121); BAD11(10,128,2,129);
341 BAD11(7,0,2,512); BAD11(7,160,2,176); BAD11(7,513,3,512); BAD11(0,4097,3,4096);
342 BAD11(12,0,4,1); BAD11(13,2,4,1); BAD11(14,0,4,1);
343 BAD11(3,4096,5,65536); BAD11(4,65536,5,131072); BAD11(5,65536,5,196608); BAD11(6,196608,5,262144);
344 BAD11(0,UINT64_MAX-7,5,4096); BAD11(3,UINT64_MAX-7,5,65536);
345 /* 11-contribution extents exactly: ids (10*512+40), table (10*512+176), segments 11*11*32, maps (121+8)*4.
346 * The map extent 516 is not a multiple of the 8-byte alignment, so its twin is the next aligned base 520. */
347#define REGROUP() do { CHECK(group()==0); memcpy(before,words,sizeof words); } while (0)
348 req[0]=300000; REGROUP(); BAD11(3,300000+5152,5,300000+5160); req[3]=65536; REGROUP();
349 req[1]=400000; REGROUP(); BAD11(3,400000+5280,5,400000+5296); req[3]=65536; REGROUP();
350 req[1]=8192; req[0]=4096; REGROUP();
351 BAD11(4,65536+3864,5,65536+3872); req[4]=131072; REGROUP();
352 BAD11(6,196608+512,5,196608+520); req[6]=262144; REGROUP();
353 group_words(512,11);
354 req[12]=0; req[13]=0; CHECK(group()==0); req[12]=1; req[13]=1;
355 /* (g) Windows: both profiles, then refusals with twins (window_refusal runs the twin first). */
356 windows(2048,512,9,14);
357 windows(2560,640,11,11);
358 uint64_t w11[8]={1,128,2560,640,11,512,12,0}; memcpy(wcfg,w11,sizeof w11);
359 window_refusal(6,11,2);
360 window_refusal(4,9,1); window_refusal(4,10,1); window_refusal(2,2048,1); window_refusal(3,512,1);
361 window_refusal(0,2,1); window_refusal(1,129,1); window_refusal(5,8,1); window_refusal(5,0,1);
362 window_refusal(6,129,1); window_refusal(7,1,1);
363 uint64_t w9[8]={1,128,2048,512,9,512,10,0}; memcpy(wcfg,w9,sizeof w9);
364 window_refusal(6,9,2); window_refusal(4,11,1); window_refusal(2,2560,1); window_refusal(3,640,1);
365 printf("MOE-WIDE HOST PASS: %u plans (35B shape == legacy units; Flash-Next 512x10+shared == C reference and rank verifier), %u refusals with passing twins, GROUP 9/11, windows 14/11\n", planned, refused);
366 return 0;
367}