Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/tests/moe/caller_wide.c # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers 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 9 #include 10 #include 11 #include 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) 14 enum { IDS = 1280, WORK = 12672, SEG = 2052, MAPS = 2832, YB = 1416, DIRS = 1026, MAPW = 11328, SEGW = 16416 }; 15 /* Wide planner state. */ 16 static uint64_t cfg[8], meta[8], ids[IDS], work[WORK], seg[SEG], maps[MAPS], dirs[DIRS]; 17 static uint8_t mapwire[MAPW], segwire[SEGW], idwire[IDS * 4]; 18 /* Legacy planner state (the oracle at the 35B shape). */ 19 static uint64_t lcfg[8], lmeta[8], lids[1024], lwork[10368], lseg[1028], lmaps[2320], ldirs[514]; 20 static uint8_t lmapwire[9280], lsegwire[8224]; 21 /* Independent reference. */ 22 static uint64_t rwork[WORK], rseg[SEG], rmaps[MAPS], rmeta[8]; 23 static uint32_t order[1408]; 24 static uint64_t req[16]; 25 static uint32_t words[16], lwords[16]; 26 static uint64_t wcfg[8], wout[1024], wmeta[4], lwout[1024], lwmeta[4]; 27 static unsigned planned, refused; 28 29 static 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 } 34 static 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 } 39 static 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. */ 46 static 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. */ 79 static 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 } 92 static 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. */ 96 static 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. */ 118 static 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 } 125 static 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 } 136 static 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>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. */ 192 static 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