interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / tests / gpu_pfhost_test.c

294 lines · 17424 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* gpu_pfhost_test.c (OWN-30B W-L1G-D, 2026-10-14): router logits and MoE combine for the whole-prompt prefill
18 * (omega_blackwell_codegen_pfhost.h). Both kernels must equal the host loops of the Rust driver BIT FOR BIT.
19 * --host-only reference self-checks (packing, the -0.0 start, mutant references differ)
20 * --sim kernels in the warp simulator bit-identical to the reference, both scheduling orders; mutants caught
21 * --chip (under flock /tmp/aien-gb10.lock) through the device API at --tokens rows: bit-identical, mutants caught, medians
22 * Verdict line last: "GPU_PFHOST: PASS|FAIL". */
23#include "omega_blackwell_codegen_pfhost.h"
24#include "omega_gpu_pfhost.h"
25#include "omega_gpu_prefill.h"
26#include "bw_warp_sim_tc.h"
27#include <math.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31
32static uint64_t rng = 0x9e3779b97f4a7c15ull;
33static uint32_t rnd(void) { rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; return (uint32_t)(rng >> 16); }
34static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; }
35static float u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; }
36static int cmp_u64(const void *a, const void *b) { uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; return x < y ? -1 : x > y; }
37static float rand_unit(void) { return ((float)(rnd() & 0xffffff) / 16777216.0f) * 2.0f - 1.0f; }
38/* router weights as the checkpoint has them: bf16 values widened to f32 */
39static float rand_bf16w(void) { return u2f(f2u(rand_unit() * 0.08f) & 0xffff0000u); }
40/* activations: RMSNorm-like magnitudes with some outliers and exact zeros */
41static float rand_act(void) { float v = rand_unit() * 2.0f; int r = rnd() % 64; if (r == 0) v *= 40.0f; else if (r == 1) v = 0.0f; return v; }
42
43/* ---------------- references (compiled with -ffp-contract=off: a * b then +, as Rust) ---------------- */
44static void rl_ref(const float *x, const float *w, uint32_t rows, uint32_t k, uint32_t e, float *lg, int mutant) {
45 const uint32_t kk = mutant == 2 ? k - 128u : k;
46 for (uint32_t r = 0; r < rows; r++)
47 for (uint32_t ex = 0; ex < e; ex++) {
48 float acc = -0.0f;
49 for (uint32_t j = 0; j < kk; j++) acc = mutant == 1 ? fmaf(w[(size_t)ex * k + j], x[(size_t)r * k + j], acc) : acc + w[(size_t)ex * k + j] * x[(size_t)r * k + j];
50 lg[(size_t)r * e + ex] = acc;
51 }
52}
53static void mc_ref(const float *down, const uint32_t *inv, const float *wt, uint32_t rows, uint32_t h, uint32_t K, float *out, int mutant) {
54 const uint32_t used = mutant == 2 ? K - 1u : K;
55 for (uint32_t r = 0; r < rows; r++)
56 for (uint32_t c = 0; c < h; c++) {
57 float acc = 0.0f;
58 for (uint32_t s = 0; s < used; s++) {
59 const float d = down[(size_t)inv[(size_t)r * K + s] * h + c], ww = wt[(size_t)r * K + s];
60 acc = mutant == 1 ? fmaf(ww, d, acc) : acc + ww * d;
61 }
62 out[(size_t)r * h + c] = acc;
63 }
64}
65static size_t diff_bits(const float *a, const float *b, size_t n) { size_t bad = 0; for (size_t i = 0; i < n; i++) if (f2u(a[i]) != f2u(b[i])) bad++; return bad; }
66
67typedef struct { uint32_t rows, k, e, t, rp; float *x, *w, *wq, *ref; } RlProb;
68static void rl_make(RlProb *p, uint32_t rows, uint32_t k, uint32_t e, uint32_t t) {
69 p->rows = rows; p->k = k; p->e = e; p->t = t; p->rp = (rows + t - 1u) / t * t;
70 p->x = malloc((size_t)p->rp * k * 4); p->w = malloc((size_t)e * k * 4);
71 p->wq = malloc(omega_gpu_pf_router_packed_bytes(e, k)); p->ref = malloc((size_t)rows * e * 4);
72 for (size_t i = 0; i < (size_t)p->rp * k; i++) p->x[i] = i < (size_t)rows * k ? rand_act() : u2f(0x7fc00000u); /* pad rows NaN: must not leak */
73 for (size_t i = 0; i < (size_t)e * k; i++) p->w[i] = rand_bf16w();
74 if (rows > 2) memset(p->x + 2 * (size_t)k, 0, k * 4); /* an all-zero row: every logit -0.0 + 0 products */
75 omega_gpu_pf_router_pack(e, k, p->w, p->wq);
76 rl_ref(p->x, p->w, rows, k, e, p->ref, 0);
77}
78static void rl_free(RlProb *p) { free(p->x); free(p->w); free(p->wq); free(p->ref); }
79
80typedef struct { uint32_t rows, h, K; float *down, *wt, *ref; uint32_t *inv; } McProb;
81static void mc_make(McProb *p, uint32_t rows, uint32_t h, uint32_t K) {
82 p->rows = rows; p->h = h; p->K = K;
83 const size_t g = (size_t)rows * K;
84 p->down = malloc(g * h * 4); p->wt = malloc(g * 4); p->inv = malloc(g * 4); p->ref = malloc((size_t)rows * h * 4);
85 for (size_t i = 0; i < g * h; i++) p->down[i] = rand_act() * 0.5f;
86 for (size_t i = 0; i < g; i++) { p->wt[i] = (float)(rnd() & 0xffff) / 65536.0f * 0.4f; p->inv[i] = (uint32_t)i; }
87 for (size_t i = g; i > 1; i--) { size_t j = rnd() % i; uint32_t t = p->inv[i - 1]; p->inv[i - 1] = p->inv[j]; p->inv[j] = t; } /* a permutation, like the expert sort */
88 mc_ref(p->down, p->inv, p->wt, rows, h, K, p->ref, 0);
89}
90static void mc_free(McProb *p) { free(p->down); free(p->wt); free(p->inv); free(p->ref); }
91
92/* ---------------- host-only ---------------- */
93static int host_checks(void) {
94 int fail = 0;
95 /* packing: wq[g][ex][q] = w[ex][4g + q], pad group zero */
96 { const uint32_t e = 32, k = 256; float *w = malloc(e * k * 4), *wq = malloc(omega_gpu_pf_router_packed_bytes(e, k));
97 for (uint32_t i = 0; i < e * k; i++) w[i] = (float)i;
98 omega_gpu_pf_router_pack(e, k, w, wq);
99 size_t bad = 0;
100 for (uint32_t g = 0; g < k / 4; g++) for (uint32_t ex = 0; ex < e; ex++) for (uint32_t q = 0; q < 4; q++) if (wq[(g * e + ex) * 4 + q] != w[ex * k + 4 * g + q]) bad++;
101 for (uint32_t i = 0; i < e * 4; i++) if (f2u(wq[(k / 4) * e * 4 + i]) != 0) bad++;
102 printf("host pack R4: %zu wrong\n", bad); if (bad) fail++;
103 free(w); free(wq); }
104 /* the sum starts at -0.0 (Rust f32 Sum): an all-zero row gives -0.0 + (+0.0 products) = +0.0, a row of -0.0 products stays -0.0 */
105 { float z = -0.0f; z = z + 0.0f * -1.0f; printf("host -0.0 start: -0 + (-0) = %s\n", signbit(z) ? "-0 (ok)" : "+0 (WRONG)"); if (!signbit(z)) fail++; }
106 /* the mutant references really differ */
107 { RlProb p; rl_make(&p, 5, 256, 32, 4); float *m = malloc(5 * 32 * 4);
108 for (int mu = 1; mu <= 2; mu++) { rl_ref(p.x, p.w, 5, 256, 32, m, mu); size_t d = diff_bits(m, p.ref, 5 * 32); printf("host router mutant %d reference differs in %zu of 160\n", mu, d); if (!d) fail++; }
109 free(m); rl_free(&p); }
110 { McProb p; mc_make(&p, 3, 128, 8); float *m = malloc(3 * 128 * 4);
111 for (int mu = 1; mu <= 2; mu++) { mc_ref(p.down, p.inv, p.wt, 3, 128, 8, m, mu); size_t d = diff_bits(m, p.ref, 3 * 128); printf("host combine mutant %d reference differs in %zu of 384\n", mu, d); if (!d) fail++; }
112 free(m); mc_free(&p); }
113 return fail;
114}
115
116/* ---------------- simulator ---------------- */
117static int rl_sim_one(const RlProb *p, int mutant, int order, float *lg, char *msg, size_t msgn) {
118 OmegaRlSpec sp = { p->k, p->e, p->t, mutant };
119 static BlackwellIRProgram prog;
120 if (omega_blackwell_codegen_rl_prog(&sp, &prog) != 0) { snprintf(msg, msgn, "codegen refused"); return -1; }
121 const size_t lgb = (size_t)p->rp * p->e * 4, wqb = omega_gpu_pf_router_packed_bytes(p->e, p->k);
122 uint8_t *smem = malloc(omega_rl_shared_bytes(&sp));
123 static WsSim ws; memset(&ws, 0, sizeof ws);
124 ws.p = &prog; ws.threads = p->e; ws.smem = smem; ws.smem_bytes = omega_rl_shared_bytes(&sp); ws.order = order;
125 uint64_t a[3] = { (uintptr_t)p->x, (uintptr_t)p->wq, (uintptr_t)lg };
126 for (int i = 0; i < 3; i++) { ws.cbank[(0x380 + 8 * i) / 4] = (uint32_t)a[i]; ws.cbank[(0x380 + 8 * i) / 4 + 1] = (uint32_t)(a[i] >> 32); }
127 ws.ranges[0].lo = (uintptr_t)p->x; ws.ranges[0].hi = (uintptr_t)p->x + (size_t)p->rp * p->k * 4;
128 ws.ranges[1].lo = (uintptr_t)p->wq; ws.ranges[1].hi = (uintptr_t)p->wq + wqb;
129 ws.ranges[2].lo = (uintptr_t)lg; ws.ranges[2].hi = (uintptr_t)lg + lgb;
130 ws.nranges = 3;
131 memset(lg, 0xee, lgb);
132 int err = ws_run(&ws, p->rp / p->t, 1);
133 if (err) snprintf(msg, msgn, "simulator %.150s", ws.msg);
134 free(smem);
135 return err ? -1 : 0;
136}
137static int mc_sim_one(const McProb *p, int mutant, int order, float *out, char *msg, size_t msgn) {
138 OmegaMcSpec sp = { p->h, p->K, mutant };
139 static BlackwellIRProgram prog;
140 if (omega_blackwell_codegen_mc_prog(&sp, &prog) != 0) { snprintf(msg, msgn, "codegen refused"); return -1; }
141 const size_t g = (size_t)p->rows * p->K;
142 static WsSim ws; memset(&ws, 0, sizeof ws);
143 ws.p = &prog; ws.threads = p->h / 4; ws.order = order;
144 uint64_t a[4] = { (uintptr_t)p->down, (uintptr_t)p->inv, (uintptr_t)p->wt, (uintptr_t)out };
145 for (int i = 0; i < 4; i++) { ws.cbank[(0x380 + 8 * i) / 4] = (uint32_t)a[i]; ws.cbank[(0x380 + 8 * i) / 4 + 1] = (uint32_t)(a[i] >> 32); }
146 ws.ranges[0].lo = (uintptr_t)p->down; ws.ranges[0].hi = (uintptr_t)p->down + g * p->h * 4;
147 ws.ranges[1].lo = (uintptr_t)p->inv; ws.ranges[1].hi = (uintptr_t)p->inv + g * 4;
148 ws.ranges[2].lo = (uintptr_t)p->wt; ws.ranges[2].hi = (uintptr_t)p->wt + g * 4;
149 ws.ranges[3].lo = (uintptr_t)out; ws.ranges[3].hi = (uintptr_t)out + (size_t)p->rows * p->h * 4;
150 ws.nranges = 4;
151 memset(out, 0xee, (size_t)p->rows * p->h * 4);
152 int err = ws_run(&ws, p->rows, 1);
153 if (err) snprintf(msg, msgn, "simulator %.150s", ws.msg);
154 return err ? -1 : 0;
155}
156static int sim_checks(void) {
157 int fail = 0; char msg[200];
158 static const struct { uint32_t rows, k, e, t; } rc[] = { { 37, 2048, 128, 16 }, { 9, 256, 32, 4 }, { 21, 384, 64, 8 }, { 16, 2048, 128, 4 } };
159 for (size_t c = 0; c < sizeof rc / sizeof rc[0]; c++) {
160 RlProb p; rl_make(&p, rc[c].rows, rc[c].k, rc[c].e, rc[c].t);
161 float *lg = malloc((size_t)p.rp * p.e * 4);
162 for (int order = 0; order < 2; order++) {
163 int r = rl_sim_one(&p, 0, order, lg, msg, sizeof msg);
164 size_t bad = r ? 0 : diff_bits(lg, p.ref, (size_t)p.rows * p.e);
165 printf("sim router k %u e %u t %u rows %u order %d: %s, %zu of %zu differ\n", p.k, p.e, p.t, p.rows, order, r ? msg : "ran", bad, (size_t)p.rows * p.e);
166 if (r || bad) fail++;
167 }
168 if (c == 0) for (int mu = 1; mu <= 2; mu++) {
169 int r = rl_sim_one(&p, mu, 0, lg, msg, sizeof msg);
170 size_t bad = r ? 0 : diff_bits(lg, p.ref, (size_t)p.rows * p.e);
171 printf("sim router mutant %d: %s\n", mu, r ? msg : bad ? "caught" : "NOT CAUGHT");
172 if (r || !bad) fail++;
173 }
174 free(lg); rl_free(&p);
175 }
176 static const struct { uint32_t rows, h, K; } mc[] = { { 7, 2048, 8 }, { 5, 128, 4 }, { 3, 384, 16 } };
177 for (size_t c = 0; c < sizeof mc / sizeof mc[0]; c++) {
178 McProb p; mc_make(&p, mc[c].rows, mc[c].h, mc[c].K);
179 float *out = malloc((size_t)p.rows * p.h * 4);
180 for (int order = 0; order < 2; order++) {
181 int r = mc_sim_one(&p, 0, order, out, msg, sizeof msg);
182 size_t bad = r ? 0 : diff_bits(out, p.ref, (size_t)p.rows * p.h);
183 printf("sim combine h %u top_k %u rows %u order %d: %s, %zu of %zu differ\n", p.h, p.K, p.rows, order, r ? msg : "ran", bad, (size_t)p.rows * p.h);
184 if (r || bad) fail++;
185 }
186 if (c == 0) for (int mu = 1; mu <= 2; mu++) {
187 int r = mc_sim_one(&p, mu, 0, out, msg, sizeof msg);
188 size_t bad = r ? 0 : diff_bits(out, p.ref, (size_t)p.rows * p.h);
189 printf("sim combine mutant %d: %s\n", mu, r ? msg : bad ? "caught" : "NOT CAUGHT");
190 if (r || !bad) fail++;
191 }
192 free(out); mc_free(&p);
193 }
194 return fail;
195}
196
197/* ---------------- chip ---------------- */
198static int alloc_fill(int host, size_t bytes, const void *src, OmegaGpuMem *m) {
199 bytes = (bytes + 15u) & ~(size_t)15u;
200 int rc = host ? omega_gpu_pf_alloc_host(bytes, m) : omega_gpu_pf_alloc_dev(bytes, m);
201 if (rc) return rc;
202 if (!src) return 0;
203 if (host) { memcpy(m->cpu, src, bytes); return 0; }
204 return omega_gpu_pf_upload(m, 0, src, bytes);
205}
206static int rl_chip(uint32_t rows, int reps) {
207 int fail = 0;
208 const uint32_t k = 2048, e = 128, t = omega_gpu_pf_router_rows();
209 RlProb p; rl_make(&p, rows, k, e, t);
210 const size_t lgb = (size_t)p.rp * e * 4;
211 OmegaGpuMem xd = { 0 }, wd = { 0 }, ld = { 0 };
212 /* as the driver: x and logits in HOST (GPU-uncached) buffers, the packed weights GPU-cached */
213 int rc = alloc_fill(1, (size_t)p.rp * k * 4, p.x, &xd);
214 if (!rc) rc = alloc_fill(0, omega_gpu_pf_router_packed_bytes(e, k), p.wq, &wd);
215 if (!rc) rc = alloc_fill(1, lgb, NULL, &ld);
216 uint64_t ns[32] = { 0 }; if (reps > 32) reps = 32; if (reps < 1) reps = 1;
217 float *lg = malloc(lgb);
218 for (int r = 0; r < reps && !rc; r++) { memset(ld.cpu, 0xee, lgb); rc = omega_gpu_pf_router_logits(k, e, rows, xd.va, wd.va, ld.va, &ns[r]); }
219 if (rc) { printf("ROUTER chip rows %u: %s (%s)\n", rows, omega_gpu_pf_rc_name(rc), omega_gpu_session_last_error()); fail++; }
220 else {
221 memcpy(lg, ld.cpu, lgb);
222 size_t bad = diff_bits(lg, p.ref, (size_t)rows * e);
223 qsort(ns, (size_t)reps, 8, cmp_u64);
224 printf("ROUTER chip k %u e %u t %u rows %u: %zu of %zu differ from the host loop; median %.3f ms\n", k, e, t, rows, bad, (size_t)rows * e, ns[reps / 2] / 1e6);
225 if (bad) fail++;
226 for (int mu = 1; mu <= 2; mu++) {
227 omega_gpu_pf_router_test_set_mutant(mu);
228 memset(ld.cpu, 0xee, lgb);
229 int mrc = omega_gpu_pf_router_logits(k, e, rows, xd.va, wd.va, ld.va, NULL);
230 omega_gpu_pf_router_test_set_mutant(0);
231 memcpy(lg, ld.cpu, lgb);
232 size_t mb = mrc ? 0 : diff_bits(lg, p.ref, (size_t)rows * e);
233 printf("ROUTER chip mutant %d: %s\n", mu, mrc ? "launch failed" : mb ? "caught" : "NOT CAUGHT");
234 if (mrc || !mb) fail++;
235 }
236 }
237 free(lg); omega_gpu_pf_free(&xd); omega_gpu_pf_free(&wd); omega_gpu_pf_free(&ld); rl_free(&p);
238 return fail;
239}
240static int mc_chip(uint32_t rows, int reps) {
241 int fail = 0;
242 const uint32_t h = 2048, K = 8;
243 McProb p; mc_make(&p, rows, h, K);
244 const size_t g = (size_t)rows * K, ob = (size_t)rows * h * 4;
245 OmegaGpuMem dd = { 0 }, id = { 0 }, wd = { 0 }, od = { 0 };
246 /* as the driver: down rows GPU-cached (the down GEMM writes them), inv / weights / out in HOST buffers */
247 int rc = alloc_fill(0, g * h * 4, p.down, &dd);
248 if (!rc) rc = alloc_fill(1, g * 4, p.inv, &id);
249 if (!rc) rc = alloc_fill(1, g * 4, p.wt, &wd);
250 if (!rc) rc = alloc_fill(1, ob, NULL, &od);
251 uint64_t ns[32] = { 0 }; if (reps > 32) reps = 32; if (reps < 1) reps = 1;
252 float *out = malloc(ob);
253 for (int r = 0; r < reps && !rc; r++) { memset(od.cpu, 0xee, ob); rc = omega_gpu_pf_moe_combine(h, K, rows, dd.va, id.va, wd.va, od.va, &ns[r]); }
254 if (rc) { printf("COMBINE chip rows %u: %s (%s)\n", rows, omega_gpu_pf_rc_name(rc), omega_gpu_session_last_error()); fail++; }
255 else {
256 memcpy(out, od.cpu, ob);
257 size_t bad = diff_bits(out, p.ref, (size_t)rows * h);
258 qsort(ns, (size_t)reps, 8, cmp_u64);
259 printf("COMBINE chip h %u top_k %u rows %u: %zu of %zu differ from the host loop; median %.3f ms\n", h, K, rows, bad, (size_t)rows * h, ns[reps / 2] / 1e6);
260 if (bad) fail++;
261 for (int mu = 1; mu <= 2; mu++) {
262 omega_gpu_pf_combine_test_set_mutant(mu);
263 memset(od.cpu, 0xee, ob);
264 int mrc = omega_gpu_pf_moe_combine(h, K, rows, dd.va, id.va, wd.va, od.va, NULL);
265 omega_gpu_pf_combine_test_set_mutant(0);
266 memcpy(out, od.cpu, ob);
267 size_t mb = mrc ? 0 : diff_bits(out, p.ref, (size_t)rows * h);
268 printf("COMBINE chip mutant %d: %s\n", mu, mrc ? "launch failed" : mb ? "caught" : "NOT CAUGHT");
269 if (mrc || !mb) fail++;
270 }
271 }
272 free(out); omega_gpu_pf_free(&dd); omega_gpu_pf_free(&id); omega_gpu_pf_free(&wd); omega_gpu_pf_free(&od); mc_free(&p);
273 return fail;
274}
275
276int main(int argc, char **argv) {
277 int host = 1, sim = 1, chip = 0, reps = 7; uint32_t tokens = 2048;
278 for (int i = 1; i < argc; i++) {
279 if (!strcmp(argv[i], "--host-only")) sim = 0;
280 else if (!strcmp(argv[i], "--sim")) host = 0;
281 else if (!strcmp(argv[i], "--chip")) { chip = 1; host = 0; sim = 0; }
282 else if (!strcmp(argv[i], "--tokens") && i + 1 < argc) tokens = (uint32_t)atoi(argv[++i]);
283 else if (!strcmp(argv[i], "--reps") && i + 1 < argc) reps = atoi(argv[++i]);
284 }
285 int fail = 0;
286 if (host) fail += host_checks();
287 if (sim) fail += sim_checks();
288 if (chip) {
289 fail += rl_chip(tokens, reps); fail += rl_chip(37, 3);
290 fail += mc_chip(tokens, reps); fail += mc_chip(7, 3);
291 }
292 printf("GPU_PFHOST: %s\n", fail ? "FAIL" : "PASS");
293 return fail ? 1 : 0;
294}