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 / codegen_dec_group_sim_test.c

183 lines · 11158 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/*
3 * GROUP (OMEGA_DEC_GROUP, om/codegen/cg_dec.om) on the host warp simulator, for the Qwen3.6-35B shape (S = 9 slots per head: 8 routed
4 * + shared, ids below 256, shared id 256) and the Qwen3.8-Flash-Next shape (S = 11: 10 routed + shared, 512 routed experts, shared id 512).
5 * The kernel is shape-generic (S 1..16 slots, ids are u32 sort keys); nothing in it knew about 512 experts until this test ran it.
6 * Inputs: router id rows per head and per-head directory tables ({w_va, s_va} per slot) for gate|up and down. The host reference sorts
7 * rows t = h * S + j by (id, t) and checks every output bit: XMAP, YMAP, both segment tables (distinct ids ascending, first row of
8 * each id, row0 = rank of its first row, cnt), zero segments past the distinct count, and the eight zero padding map entries.
9 * Functional simulation only (NOT scoreboard timing, NOT the chip). Every spec.mutant that changes results must be caught.
10 */
11#include "omega_blackwell_codegen_dec.h"
12#include "bw_warp_sim_k.h"
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16
17static BlackwellIRProgram prog;
18static int g_checks, g_failed;
19#define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0)
20
21enum { MAXR = 128, STRIDE = 256 };
22typedef struct { uint32_t S, N, R; uint32_t ids[MAXR]; } Case;
23
24static uint32_t rng_state = 0x2545F491u;
25static uint32_t rng(void) { rng_state = rng_state * 1664525u + 1013904223u; return rng_state >> 8; }
26
27static void put64(WsSim *s, uint32_t off, const void *p) {
28 const uint64_t a = (uint64_t)(uintptr_t)p;
29 s->cbank[off / 4] = (uint32_t)a; s->cbank[off / 4 + 1] = (uint32_t)(a >> 32);
30}
31
32/* device-side buffers: N heads of STRIDE bytes each */
33static uint8_t idbuf[16 * STRIDE], tgu[16 * STRIDE], tdn[16 * STRIDE];
34static uint8_t seg_gu[MAXR * OMEGA_DEC_GR_SEG_BYTES + 64], seg_dn[MAXR * OMEGA_DEC_GR_SEG_BYTES + 64];
35static uint32_t xmap[MAXR + OMEGA_DEC_GR_PAD + 8], ymap[MAXR + OMEGA_DEC_GR_PAD + 8];
36
37static uint64_t wva(uint32_t id) { return 0x100000000ull + (uint64_t)id * 0x40000ull; }
38static uint64_t sva(uint32_t id) { return 0x200000000ull + (uint64_t)id * 0x1000ull; }
39static uint64_t dwva(uint32_t id) { return 0x300000000ull + (uint64_t)id * 0x20000ull; }
40static uint64_t dsva(uint32_t id) { return 0x400000000ull + (uint64_t)id * 0x800ull; }
41
42static int run_case(const Case *c, int mutant, uint32_t order, uint32_t warp_sched) {
43 memset(idbuf, 0, sizeof idbuf); memset(tgu, 0, sizeof tgu); memset(tdn, 0, sizeof tdn);
44 memset(seg_gu, 0x5a, sizeof seg_gu); memset(seg_dn, 0x5a, sizeof seg_dn);
45 memset(xmap, 0x5a, sizeof xmap); memset(ymap, 0x5a, sizeof ymap);
46 for (uint32_t h = 0; h < c->N; h++)
47 for (uint32_t j = 0; j < c->S; j++) {
48 const uint32_t id = c->ids[h * c->S + j];
49 memcpy(idbuf + h * STRIDE + j * 4, &id, 4);
50 const uint64_t g[2] = { wva(id), sva(id) }, d[2] = { dwva(id), dsva(id) };
51 memcpy(tgu + h * STRIDE + j * 16, g, 16); memcpy(tdn + h * STRIDE + j * 16, d, 16);
52 }
53 OmegaDecSpec sp; memset(&sp, 0, sizeof sp);
54 sp.kind = OMEGA_DEC_GROUP; sp.groups = c->S; sp.mutant = mutant;
55 if (omega_blackwell_codegen_dec_prog(&sp, &prog) != 0) return -2;
56 static uint8_t smem[OMEGA_DEC_GR_SHARED_BYTES];
57 WsSim s; memset(&s, 0, sizeof s);
58 s.p = &prog; s.threads = OMEGA_DEC_THREADS; s.smem = smem; s.smem_bytes = sizeof smem; s.order = (int)order; s.warp_sched = warp_sched;
59 put64(&s, OMEGA_DEC_GR_IDS, idbuf); put64(&s, OMEGA_DEC_GR_TGU, tgu); put64(&s, OMEGA_DEC_GR_TDN, tdn);
60 put64(&s, OMEGA_DEC_GR_SGU, seg_gu); put64(&s, OMEGA_DEC_GR_SDN, seg_dn); put64(&s, OMEGA_DEC_GR_XMAP, xmap); put64(&s, OMEGA_DEC_GR_YMAP, ymap);
61 s.cbank[OMEGA_DEC_GR_STRIDE / 4] = STRIDE; s.cbank[OMEGA_DEC_GR_HEADS / 4] = c->N;
62 return ws_run(&s, 1, 1) ? -1 : 0;
63}
64
65/* host reference; returns the number of mismatching output items (0 = every bit equal) */
66static unsigned compare(const Case *c, char *why, size_t wl) {
67 const uint32_t R = c->R;
68 uint32_t order[MAXR];
69 for (uint32_t t = 0; t < R; t++) order[t] = t;
70 for (uint32_t a = 1; a < R; a++) { /* insertion sort by (id, t): stable */
71 const uint32_t v = order[a]; uint32_t b = a;
72 while (b > 0 && c->ids[order[b - 1]] > c->ids[v]) { order[b] = order[b - 1]; b--; }
73 order[b] = v;
74 }
75 unsigned bad = 0; why[0] = 0;
76 for (uint32_t p = 0; p < R; p++) {
77 const uint32_t t = order[p];
78 if (xmap[p] != t / c->S || ymap[p] != t) { if (!bad++) snprintf(why, wl, "rank %u: xmap %u ymap %u, want %u %u", p, xmap[p], ymap[p], t / c->S, t); }
79 }
80 for (uint32_t p = R; p < R + OMEGA_DEC_GR_PAD; p++)
81 if (xmap[p] != 0 || ymap[p] != 0) { if (!bad++) snprintf(why, wl, "padding entry %u not zero", p); }
82 uint32_t seg = 0, p = 0;
83 while (p < R) {
84 const uint32_t t0 = order[p], id = c->ids[t0];
85 uint32_t cnt = 0; while (p + cnt < R && c->ids[order[p + cnt]] == id) cnt++;
86 uint8_t want[32]; memset(want, 0, sizeof want);
87 const uint64_t g[2] = { wva(id), sva(id) }, d[2] = { dwva(id), dsva(id) };
88 const uint32_t tail[2] = { p, cnt };
89 memcpy(want, g, 16); memcpy(want + 16, tail, 8);
90 if (memcmp(seg_gu + seg * 32, want, 32)) { if (!bad++) snprintf(why, wl, "gate|up segment %u (id %u) differs", seg, id); }
91 memcpy(want, d, 16);
92 if (memcmp(seg_dn + seg * 32, want, 32)) { if (!bad++) snprintf(why, wl, "down segment %u (id %u) differs", seg, id); }
93 seg++; p += cnt;
94 }
95 for (uint32_t z = seg; z < R; z++) {
96 uint8_t zero[32] = { 0 };
97 if (memcmp(seg_gu + z * 32, zero, 32) || memcmp(seg_dn + z * 32, zero, 32)) { if (!bad++) snprintf(why, wl, "segment %u past the %u distinct ids is not zero", z, seg); }
98 }
99 return bad;
100}
101
102static void make_case(Case *c, uint32_t S, uint32_t N, uint32_t id_range, uint32_t shared_id, unsigned flavour) {
103 c->S = S; c->N = N; c->R = S * N;
104 for (uint32_t h = 0; h < N; h++)
105 for (uint32_t j = 0; j < S; j++) {
106 uint32_t id;
107 switch (flavour) {
108 case 0: id = rng() % id_range; break; /* random with duplicates */
109 case 1: id = 7; break; /* every row the same expert */
110 case 2: id = (h * S + j) % id_range; break; /* as distinct as the range allows */
111 case 3: id = id_range - 1 - (h * S + j) % id_range; break; /* descending */
112 default: id = (j % 3) * (id_range / 3); break; /* few ids, heavy duplication */
113 }
114 c->ids[h * S + j] = j == S - 1 ? shared_id : id; /* the last slot is the shared expert */
115 }
116}
117
118int main(void) {
119 static const struct { uint32_t S, range, shared; const char *name; } shapes[] = {
120 { 9, 256, 256, "Qwen3.6-35B S=9 ids<256 shared 256" },
121 { 11, 512, 512, "Flash-Next S=11 ids<512 shared 512" },
122 };
123 unsigned cases = 0, runs = 0;
124 for (unsigned sh = 0; sh < 2; sh++) {
125 const uint32_t S = shapes[sh].S, maxn = MAXR / S;
126 const uint32_t heads[] = { 1, 2, 3, maxn / 2, maxn - 1, maxn };
127 for (unsigned hi = 0; hi < sizeof heads / sizeof heads[0]; hi++)
128 for (unsigned flavour = 0; flavour < 5; flavour++)
129 for (unsigned rep = 0; rep < (flavour == 0 ? 6u : 1u); rep++) {
130 Case c; make_case(&c, S, heads[hi], shapes[sh].range, shapes[sh].shared, flavour);
131 if (c.R > MAXR) continue;
132 for (uint32_t order = 0; order < 2; order++)
133 for (uint32_t ws = 0; ws < 3; ws++) {
134 const int rc = run_case(&c, 0, order, ws == 2 ? 0x9e3779b9u : ws);
135 char why[160];
136 const unsigned bad = rc == 0 ? compare(&c, why, sizeof why) : 1;
137 CHECK(rc == 0 && bad == 0, "%s heads=%u flavour=%u rep=%u order=%u warp=%u: rc=%d %s", shapes[sh].name, c.N, flavour, rep, order, ws, rc, rc == 0 ? why : "");
138 runs++;
139 }
140 cases++;
141 }
142 printf("%s: all cases bit-exact so far (%u cases, %u simulator runs)\n", shapes[sh].name, cases, runs);
143 }
144
145 /* extremes of the Flash-Next shape: id 0, id 511 and shared 512 in one row set; the largest sort key id * 128 + row is 512 * 128 + 127 */
146 {
147 Case c; memset(&c, 0, sizeof c); c.S = 11; c.N = 11; c.R = 121;
148 for (uint32_t t = 0; t < c.R; t++) c.ids[t] = (t % 11 == 10) ? 512 : (t % 2 ? 511 : 0);
149 const int rc = run_case(&c, 0, 0, 0); char why[160];
150 const unsigned bad = rc == 0 ? compare(&c, why, sizeof why) : 1;
151 CHECK(rc == 0 && bad == 0, "ids 0 / 511 / shared 512 at 11 heads x 11 slots: rc=%d %s", rc, rc == 0 ? why : "");
152 /* the same ids on the S = 9 builder: a spec built for another S must NOT reproduce the 11-slot table (the control that S is read) */
153 c.S = 9; c.N = 13; c.R = 117; /* reinterpret the first 117 ids as 13 heads x 9 slots */
154 const int r9 = run_case(&c, 0, 0, 0); const unsigned b9 = r9 == 0 ? compare(&c, why, sizeof why) : 1;
155 CHECK(r9 == 0 && b9 == 0, "S=9 builder on 13 heads x 9 slots: rc=%d %s", r9, why);
156 }
157 /* S = 17 and S = 0 outside 1..16 are refused (the 11-slot builder is inside the range) */
158 {
159 OmegaDecSpec sp; memset(&sp, 0, sizeof sp); sp.kind = OMEGA_DEC_GROUP;
160 sp.groups = 16; CHECK(omega_blackwell_codegen_dec_prog(&sp, &prog) == 0, "twin: GROUP S=16 builds");
161 sp.groups = 17; CHECK(omega_blackwell_codegen_dec_prog(&sp, &prog) != 0, "GROUP S=17 is refused");
162 }
163 /* negative controls: each mutant must give a wrong table on at least one case of the Flash-Next shape, and none may pass for free */
164 {
165 Case c; make_case(&c, 11, 11, 512, 512, 0);
166 char why[160];
167 CHECK(run_case(&c, 0, 0, 0) == 0 && compare(&c, why, sizeof why) == 0, "mutant twin: mutant 0 matches the reference (%s)", why);
168 for (int m = 1; m <= 3; m++) {
169 unsigned caught = 0, tried = 0;
170 for (unsigned rep = 0; rep < 8; rep++) {
171 make_case(&c, 11, 11 - rep % 3, 512, 512, rep % 5 == 1 ? 4 : 0);
172 if (rep == 0) make_case(&c, 11, 11, 1, 512, 4); /* heavy duplication: the tie-break and the counts matter */
173 const int rc = run_case(&c, m, 0, 0);
174 tried++;
175 if (rc != 0 || compare(&c, why, sizeof why) != 0) caught++;
176 }
177 CHECK(caught > 0, "GROUP mutant %d NOT caught on any of %u Flash-Next cases", m, tried);
178 printf("GROUP mutant %d on S=11: caught on %u of %u cases\n", m, caught, tried);
179 }
180 }
181 printf("%s: %d checks, %d failed, %u simulator runs\n", g_failed ? "CODEGEN DEC GROUP SIM FAIL" : "CODEGEN DEC GROUP SIM PASS", g_checks, g_failed, runs);
182 return g_failed ? 1 : 0;
183}