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_qgemm_test.c

494 lines · 33821 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_qgemm_test.c (OWN-30B L1 GEMM, W-L1G, 2026-10-14): the native FP8 prefill path (omega_blackwell_codegen_qg.h).
18 * --host-only reference self-checks
19 * --sim kernels in the warp simulator bit-identical to the C reference; mutants caught (a simulator error is a FAIL)
20 * --chip (under flock /tmp/aien-gb10.lock) through the device API: bit-identical to the reference, mutants caught,
21 * medians at --tokens
22 * Verdict line last: "GPU_QGEMM: PASS|FAIL". */
23#include "omega_blackwell_codegen_qg.h"
24#include "omega_gpu_prefill.h"
25#include "omega_gpu_fp8_api.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 = 0x243f6a8885a308d3ull;
33static uint32_t rnd(void) { rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; return (uint32_t)(rng >> 16); }
34static float u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; }
35static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; }
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; }
37
38/* ---------------- quantiser reference (header semantics) ---------------- */
39static void pq_ref(const float *x, uint32_t k, uint8_t *q, float *s, int mutant) {
40 for (uint32_t b = 0; b < k / 128; b++) {
41 float m = ldexpf(1.0f, -100);
42 if (mutant == 1) { /* lane-local amax: lane l sees elements 4l..4l+3 only (scale per lane; the reference keeps lane 31's) */
43 for (uint32_t l = 0; l < 32; l++) { float ml = ldexpf(1.0f, -100); for (int j = 0; j < 4; j++) ml = fmaxf(ml, fabsf(x[128 * b + 4 * l + j])); if (l == 31) m = ml; }
44 } else for (uint32_t j = 0; j < 128; j++) m = fmaxf(m, fabsf(x[128 * b + j]));
45 uint32_t mb = f2u(m), E = mb >> 23, M = mb & 0x7fffff, c = mutant == 2 ? 0 : (M + 0x1fffff) >> 23;
46 float scale = u2f((E + c - 8) << 23), inv = u2f((262 - E - c) << 23);
47 s[b] = scale;
48 for (uint32_t j = 0; j < 128; j++) q[128 * b + j] = qm_e4m3_rne_sat(x[128 * b + j] * inv);
49 }
50}
51static float rand_act(void) {
52 float v = ((float)(rnd() & 0xffffff) / 16777216.0f) * 2.0f - 1.0f;
53 int r = rnd() % 32; if (r == 0) v *= 300.0f; else if (r == 1) v *= 1e-3f; else if (r == 2) v = 0.0f;
54 return v;
55}
56static uint16_t bf16_rn(float f) { uint32_t u = f2u(f); return (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >> 16); }
57/* rows x k activations; row 3 all zero, row 5 one huge outlier; bf16 copy when in_type is BF16 */
58static void make_x(uint32_t rows, uint32_t k, uint32_t in_type, float *x, uint16_t *xb) {
59 for (size_t i = 0; i < (size_t)rows * k; i++) x[i] = rand_act();
60 if (rows > 3) memset(x + 3 * (size_t)k, 0, k * 4);
61 if (rows > 5) x[5 * (size_t)k + 77] = 3.0e4f;
62 if (in_type == OMEGA_QG_IN_BF16) for (size_t i = 0; i < (size_t)rows * k; i++) { xb[i] = bf16_rn(x[i]); x[i] = u2f((uint32_t)xb[i] << 16); }
63}
64static size_t pq_compare(const float *x, uint32_t rows, uint32_t k, const uint8_t *q, const float *s, int mutant) {
65 size_t bad = 0; uint8_t *rq = malloc(k); float *rs = malloc(k / 128 * 4);
66 for (uint32_t r = 0; r < rows; r++) {
67 pq_ref(x + (size_t)r * k, k, rq, rs, mutant);
68 for (uint32_t j = 0; j < k; j++) if (rq[j] != q[(size_t)r * k + j]) bad++;
69 for (uint32_t b = 0; b < k / 128; b++) if (f2u(rs[b]) != f2u(s[(size_t)r * (k / 128) + b])) bad++;
70 }
71 free(rq); free(rs); return bad;
72}
73
74static int pq_host(void) {
75 int fail = 0;
76 /* scale choice: amax / scale in (224, 448] for normal amax */
77 for (int i = 0; i < 100000; i++) {
78 float x[128]; for (int j = 0; j < 128; j++) x[j] = rand_act() * ldexpf(1.0f, (int)(rnd() % 40) - 20);
79 uint8_t q[128]; float s[1]; pq_ref(x, 128, q, s, 0);
80 float m = 0; for (int j = 0; j < 128; j++) m = fmaxf(m, fabsf(x[j]));
81 if (m > 0 && !(m / s[0] <= 448.0f && m / s[0] > 224.0f)) { if (fail < 3) printf("scale out of range: amax %g scale %g\n", m, s[0]); fail++; }
82 double err = 0; for (int j = 0; j < 128; j++) { double d = fabs((double)qm_e4m3(q[j]) * s[0] - x[j]); if (d > err) err = d; }
83 if (m > 0 && err > m * (1.0 / 16 + 1e-6)) { if (fail < 3) printf("quantisation error %g vs amax %g\n", err, m); fail++; }
84 }
85 printf("quantiser reference: %s\n", fail ? "FAIL" : "PASS (amax/scale in (224, 448], error within half an e4m3 ulp)");
86 return fail;
87}
88
89static int pq_sim_one(uint32_t k, uint32_t in_type, uint32_t groups, uint32_t rows, int mutant, size_t *bad_out, char *msg, size_t msgn) {
90 OmegaPqSpec sp = { k, in_type, mutant, groups };
91 static BlackwellIRProgram prog;
92 if (omega_blackwell_codegen_pq_prog(&sp, &prog) != 0) { snprintf(msg, msgn, "codegen refused"); return -1; }
93 float *x = malloc((size_t)rows * k * 4); uint16_t *xb = malloc((size_t)rows * k * 2);
94 uint8_t *q = malloc((size_t)rows * k); float *s = malloc((size_t)rows * (k / 128) * 4);
95 make_x(rows, k, in_type, x, xb);
96 memset(q, 0xee, (size_t)rows * k); memset(s, 0xee, (size_t)rows * (k / 128) * 4);
97 static WsSim ws; memset(&ws, 0, sizeof ws);
98 ws.p = &prog; ws.threads = 32u * (groups ? groups : k / 128);
99 const void *xin = in_type == OMEGA_QG_IN_F32 ? (const void *)x : (const void *)xb;
100 uint64_t a[3] = { (uintptr_t)xin, (uintptr_t)q, (uintptr_t)s };
101 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); }
102 ws.ranges[0].lo = (uintptr_t)xin; ws.ranges[0].hi = (uintptr_t)xin + (size_t)rows * k * (in_type == OMEGA_QG_IN_F32 ? 4 : 2);
103 ws.ranges[1].lo = (uintptr_t)q; ws.ranges[1].hi = (uintptr_t)q + (size_t)rows * k;
104 ws.ranges[2].lo = (uintptr_t)s; ws.ranges[2].hi = (uintptr_t)s + (size_t)rows * (k / 128) * 4;
105 ws.nranges = 3;
106 int err = ws_run(&ws, rows, groups ? k / 128 / groups : 1);
107 if (err) snprintf(msg, msgn, "simulator %.150s", ws.msg);
108 *bad_out = err ? 0 : pq_compare(x, rows, k, q, s, 0);
109 free(x); free(xb); free(q); free(s);
110 return err ? -1 : 0;
111}
112static int pq_sim(void) {
113 int fail = 0; char msg[200];
114 /* groups > 0: one row split over k / (128 groups) CTAs (the Qwen3.8 widths 5120, 6144, 17408) */
115 static const struct { uint32_t k, in_type, groups; } cs[] = { { 2048, OMEGA_QG_IN_F32, 0 }, { 768, OMEGA_QG_IN_BF16, 0 }, { 4096, OMEGA_QG_IN_F32, 0 }, { 128, OMEGA_QG_IN_BF16, 0 },
116 { 5120, OMEGA_QG_IN_F32, 20 }, { 6144, OMEGA_QG_IN_BF16, 24 }, { 17408, OMEGA_QG_IN_BF16, 17 }, { 2048, OMEGA_QG_IN_F32, 4 } };
117 for (size_t c = 0; c < sizeof cs / sizeof cs[0]; c++) {
118 size_t bad = 0; int rc = pq_sim_one(cs[c].k, cs[c].in_type, cs[c].groups, 9, 0, &bad, msg, sizeof msg);
119 printf("sim quantiser k %u %s groups %u: %s, %zu differ\n", cs[c].k, cs[c].in_type ? "bf16" : "f32", cs[c].groups, rc ? msg : "ran", bad);
120 if (rc || bad) fail++;
121 }
122 for (int m = 1; m <= 3; m++) {
123 size_t bad = 0; int rc = pq_sim_one(m == 3 ? 5120 : 2048, OMEGA_QG_IN_F32, m == 3 ? 20 : 0, 9, m, &bad, msg, sizeof msg);
124 printf("sim quantiser mutant %d: %s\n", m, rc ? msg : bad ? "caught" : "NOT CAUGHT");
125 if (rc || !bad) fail++;
126 }
127 return fail;
128}
129
130static int pq_chip(uint32_t tokens, int reps) {
131 int fail = 0;
132 /* k > 4096 runs the split-row form (omega_gpu_pf_quant picks groups); its mutant drops the ctaid.y offset */
133 static const struct { uint32_t k, in_type, rows_mul; } cs[] = { { 2048, OMEGA_QG_IN_F32, 1 }, { 768, OMEGA_QG_IN_BF16, 8 }, { 5120, OMEGA_QG_IN_F32, 1 },
134 { 6144, OMEGA_QG_IN_BF16, 1 }, { 17408, OMEGA_QG_IN_BF16, 1 } };
135 for (size_t c = 0; c < sizeof cs / sizeof cs[0]; c++) {
136 const uint32_t k = cs[c].k, rows = tokens * cs[c].rows_mul, eb = cs[c].in_type == OMEGA_QG_IN_F32 ? 4 : 2;
137 float *x = malloc((size_t)rows * k * 4); uint16_t *xb = malloc((size_t)rows * k * 2);
138 uint8_t *q = malloc((size_t)rows * k); float *s = malloc((size_t)rows * (k / 128) * 4);
139 make_x(rows, k, cs[c].in_type, x, xb);
140 OmegaGpuMem xd = { 0 }, qd = { 0 }, sd = { 0 };
141 const size_t xbytes = (size_t)rows * k * eb, qbytes = (size_t)rows * k, sbytes = ((size_t)rows * (k / 128) * 4 + 15) & ~(size_t)15;
142 int rc = omega_gpu_pf_alloc_dev(xbytes, &xd);
143 if (!rc) rc = omega_gpu_pf_alloc_dev(qbytes, &qd);
144 if (!rc) rc = omega_gpu_pf_alloc_dev(sbytes, &sd);
145 if (!rc) rc = omega_gpu_pf_upload(&xd, 0, cs[c].in_type == OMEGA_QG_IN_F32 ? (const void *)x : (const void *)xb, xbytes);
146 uint64_t ns[32] = { 0 }; if (reps > 32) reps = 32;
147 for (int r = 0; r < reps && !rc; r++) rc = omega_gpu_pf_quant(k, cs[c].in_type, rows, xd.va, qd.va, sd.va, &ns[r]);
148 if (!rc) rc = omega_gpu_pf_download(&qd, 0, q, qbytes);
149 if (!rc) { float *st = malloc(sbytes); rc = omega_gpu_pf_download(&sd, 0, st, sbytes); memcpy(s, st, (size_t)rows * (k / 128) * 4); free(st); }
150 if (rc) { printf("quantiser chip k %u: %s (%s)\n", k, omega_gpu_pf_rc_name(rc), omega_gpu_session_last_error()); fail++; }
151 else {
152 size_t bad = pq_compare(x, rows, k, q, s, 0);
153 qsort(ns, reps, 8, cmp_u64);
154 printf("QUANT chip k %u %s rows %u: %zu differ from the reference; median %.3f ms (%.0f GB/s moved)\n", k, cs[c].in_type ? "bf16" : "f32", rows, bad,
155 ns[reps / 2] / 1e6, (double)(xbytes + qbytes + sbytes) / ns[reps / 2]);
156 if (bad) fail++;
157 const int mut = k > 4096 ? 3 : 1;
158 /* junk-fill the output first: mutant 3 rewrites groups 0 .. G-1 with right values and leaves the rest alone,
159 so stale results of the good run would hide it */
160 memset(q, 0xee, qbytes);
161 int mrc = omega_gpu_pf_upload(&qd, 0, q, qbytes);
162 omega_gpu_pf_quant_test_set_mutant(mut);
163 if (!mrc) mrc = omega_gpu_pf_quant(k, cs[c].in_type, rows, xd.va, qd.va, sd.va, NULL);
164 omega_gpu_pf_quant_test_set_mutant(0);
165 if (!mrc) mrc = omega_gpu_pf_download(&qd, 0, q, qbytes);
166 size_t mb = mrc ? 0 : pq_compare(x, rows > 64 ? 64 : rows, k, q, s, 0);
167 printf("QUANT chip k %u mutant %d: %s\n", k, mut, mrc ? "launch failed" : mb ? "caught" : "NOT CAUGHT");
168 if (mrc || !mb) fail++;
169 }
170 omega_gpu_pf_free(&xd); omega_gpu_pf_free(&qd); omega_gpu_pf_free(&sd);
171 free(x); free(xb); free(q); free(s);
172 }
173 return fail;
174}
175
176/* ---------------- grouped GEMM on QMMA ---------------- */
177static const QmmaModel GB10Q = { 32, 26, 0, 2, 0 }; /* chip-exact, evidence OWN30B-L1G-QMMA-PFGEMM-c742aa13 */
178typedef struct {
179 uint32_t E, n, k, M, xr, layout, direct, pad;
180 uint8_t *w, *xq; /* weights [E][n][k] (checkpoint order, read in place), quantised activations (xr + pad rows) */
181 uint16_t *sbf; /* scale_inv bf16 */
182 float *swf, *xs, *x; /* GEMV-tensor scale table [E][n/128][KB] f32 * 2^56, activation scales (xr + pad rows), true activations */
183 uint32_t *eid, *src, *counts;
184} QProb;
185static uint8_t rand_e4m3(void) { for (;;) { uint8_t c = (uint8_t)rnd(); if ((c & 0x7f) != 0x7f) return c; } }
186static void qprob_make(QProb *p, uint32_t E, uint32_t n, uint32_t k, uint32_t M, uint32_t xr, uint32_t layout, uint32_t direct, uint32_t pad) {
187 memset(p, 0, sizeof *p);
188 p->E = E; p->n = n; p->k = k; p->M = M; p->xr = direct ? M : xr; p->layout = layout; p->direct = direct; p->pad = pad;
189 const uint32_t KB = k / 128, XR = p->xr + pad;
190 const size_t wn = (size_t)E * n * k, sn = (size_t)E * (n / 128) * KB;
191 p->w = malloc(wn); p->sbf = malloc(sn * 2); p->swf = malloc(sn * 4);
192 p->x = calloc((size_t)XR * k, 4); p->xq = calloc((size_t)XR * k, 1); p->xs = malloc((size_t)XR * KB * 4);
193 p->eid = malloc(M * 4); p->src = malloc(M * 4); p->counts = calloc(E, 4);
194 for (size_t i = 0; i < wn; i++) p->w[i] = rand_e4m3();
195 for (size_t i = 0; i < sn; i++) { float s = ldexpf(1.0f + (float)(rnd() & 127) / 128.0f, -(int)(rnd() % 12) - 4); p->sbf[i] = (uint16_t)(f2u(s) >> 16); }
196 if (omega_fp8_pack_scales(p->sbf, sn, p->swf, 0) != 0) { printf("scale pack refused\n"); exit(2); }
197 for (size_t i = 0; i < (size_t)p->xr * k; i++) p->x[i] = rand_act();
198 if (p->xr > 3) memset(p->x + 3 * (size_t)k, 0, k * 4);
199 for (uint32_t r = 0; r < XR; r++) pq_ref(p->x + (size_t)r * k, k, p->xq + (size_t)r * k, p->xs + (size_t)r * KB, 0);
200 uint32_t i = 0;
201 for (uint32_t e = 0; e < E && i < M; e++) { uint32_t c = e + 1 == E ? M - i : (uint32_t)((uint64_t)(rnd() % 2000) * 2 * M / (2000ull * E)); if (c > M - i) c = M - i; for (uint32_t q = 0; q < c; q++) p->eid[i + q] = e; p->counts[e] = c; i += c; }
202 for (uint32_t q = 0; q < M; q++) p->src[q] = direct ? q : rnd() % p->xr;
203}
204static void qprob_free(QProb *p) { free(p->w); free(p->xq); free(p->sbf); free(p->swf); free(p->xs); free(p->x); free(p->eid); free(p->src); free(p->counts); }
205
206/* the kernel's arithmetic for output row i, physical column c */
207static float qemu_phys(const QProb *p, uint32_t i, uint32_t c) {
208 const uint32_t k = p->k, KB = k / 128, e = p->eid[i], s = p->src[i];
209 /* physical column c -> weight row: F32 c; SWIGLU gate 8G + h % 8 (h < 8) or up I + 8G + h % 8, G = c / 16, h = c % 16 */
210 const uint32_t wrow = p->layout == OMEGA_GPU_PF_PLAIN ? c : 8 * (c / 16) + c % 8 + (c % 16 >= 8 ? p->n / 2 : 0);
211 const uint8_t *xr = p->xq + (size_t)s * k, *wr = p->w + ((size_t)e * p->n + wrow) * k;
212 const float *swt = p->swf + ((size_t)e * (p->n / 128) + wrow / 128) * KB;
213 float acc = 0.0f;
214 for (uint32_t kb = 0; kb < KB; kb++) {
215 const float sc = p->xs[(size_t)s * KB + kb] * (swt[kb] * 0x1p-56f);
216 for (uint32_t h = 0; h < 2; h++) {
217 float T = 0.0f;
218 for (uint32_t v = 0; v < 2; v++) {
219 float av[32], bv[32];
220 for (uint32_t t = 0; t < 4; t++) for (uint32_t j = 0; j < 4; j++) {
221 const uint32_t o = 128 * kb + 64 * h + 16 * t + 8 * v + j;
222 av[4 * t + j] = qm_e4m3(xr[o]); bv[4 * t + j] = qm_e4m3(wr[o]);
223 av[16 + 4 * t + j] = qm_e4m3(xr[o + 4]); bv[16 + 4 * t + j] = qm_e4m3(wr[o + 4]);
224 }
225 T = qm_dot32(&GB10Q, av, bv, T);
226 }
227 acc = fmaf(T, sc, acc);
228 }
229 }
230 return acc;
231}
232/* the kernel's SwiGLU epilogue (omega_blackwell_codegen_qg.c: FMNMX t-cap, MUFU.EX2, MUFU.RCP). Both MUFU results go
233 * through the chip's flush rule (tests/bw_mufu_ftz.h, omega #462). Not observable with the t-cap: t <= 125 keeps
234 * 1 / (2^t + 1) >= 2^-125 normal, and an EX2 result below 2^-126 (t < -126) rounds to the same d = 1.0 after the +1 as
235 * its flushed zero; qg_host checks this bit for bit. Removing the cap (issue #462) is a separate chip-gated change. */
236static uint16_t qswiglu_bits(float g, float u) {
237 float t = fminf(g * u2f(0xbfb8aa3bu), 125.0f), ex = ws_mufu_ex2(t), d = ex + 1.0f, r = ws_mufu_rcp(d), o = g * r;
238 o = o * u;
239 return bf16_rn(o);
240}
241static void qemu_row(const QProb *p, uint32_t i, float *yf, uint16_t *ya) {
242 if (p->layout == OMEGA_GPU_PF_PLAIN) { for (uint32_t c = 0; c < p->n; c++) yf[c] = qemu_phys(p, i, c); return; }
243 for (uint32_t a = 0; a < p->n / 2; a++) { uint32_t G = a / 8, m = a % 8; ya[a] = qswiglu_bits(qemu_phys(p, i, 16 * G + m), qemu_phys(p, i, 16 * G + 8 + m)); }
244}
245/* double oracle on the ORIGINAL weights and the dequantised activations: logical output col (PLAIN) or gate/up of index a */
246static double qoracle(const QProb *p, uint32_t i, uint32_t col, double *mag) {
247 const uint32_t k = p->k, KB = k / 128, e = p->eid[i], s = p->src[i];
248 double acc = 0, m = 0;
249 for (uint32_t j = 0; j < k; j++) {
250 const double sw = u2f((uint32_t)p->sbf[((size_t)e * (p->n / 128) + col / 128) * KB + j / 128] << 16);
251 const double t = (double)qm_e4m3(p->w[((size_t)e * p->n + col) * k + j]) * sw * qm_e4m3(p->xq[(size_t)s * k + j]) * p->xs[(size_t)s * KB + j / 128];
252 acc += t; m += fabs(t);
253 }
254 *mag = m; return acc;
255}
256
257typedef struct { uint64_t w_va, s_va; uint32_t row0, cnt; uint64_t pad; } QTile;
258static uint32_t qmake_tiles(const QProb *p, uint32_t bm, QTile *t) {
259 uint32_t nt = 0, row = 0; const uint32_t KB = p->k / 128;
260 for (uint32_t e = 0; e < p->E; e++) { for (uint32_t r = 0; r < p->counts[e]; r += bm) {
261 t[nt].w_va = (uintptr_t)(p->w + (size_t)e * p->n * p->k); t[nt].s_va = (uintptr_t)(p->swf + (size_t)e * (p->n / 128) * KB);
262 t[nt].row0 = row + r; t[nt].cnt = p->counts[e] - r < bm ? p->counts[e] - r : bm; t[nt].pad = 0; nt++; } row += p->counts[e]; }
263 return nt;
264}
265static size_t qcompare(const QProb *p, const void *y, const uint32_t *rows, uint32_t nrows, int tol_ulp, size_t *ulp1) {
266 size_t bad = 0; if (ulp1) *ulp1 = 0;
267 float *yf = malloc(p->n * 4); uint16_t *ya = malloc(p->n);
268 for (uint32_t q = 0; q < nrows; q++) {
269 const uint32_t i = rows[q]; qemu_row(p, i, yf, ya);
270 if (p->layout == OMEGA_GPU_PF_PLAIN) { const uint32_t *g = (const uint32_t *)y + (size_t)i * p->n; for (uint32_t c = 0; c < p->n; c++) if (g[c] != f2u(yf[c])) { if (getenv("PFDBG") && bad < 6) printf(" row %u col %u got %.9g want %.9g\n", i, c, u2f(g[c]), yf[c]); bad++; } }
271 else { const uint16_t *g = (const uint16_t *)y + (size_t)i * (p->n / 2);
272 for (uint32_t c = 0; c < p->n / 2; c++) if (g[c] != ya[c]) { int d = abs((int)g[c] - (int)ya[c]); if (d <= tol_ulp && ((g[c] ^ ya[c]) & 0x8000) == 0) { if (ulp1) (*ulp1)++; } else { if (getenv("PFDBG") && bad < 12) { const uint32_t G = c / 8, mm = c % 8; printf(" row %u act %u got %04x want %04x (g %.9g u %.9g)\n", i, c, g[c], ya[c], qemu_phys(p, i, 16 * G + mm), qemu_phys(p, i, 16 * G + 8 + mm)); } bad++; } } }
273 }
274 free(yf); free(ya);
275 return bad;
276}
277
278/* omega #462: with the t-cap the MUFU flush never changes a SwiGLU output bit. Compare qswiglu_bits against the IEEE
279 * form over every g in [80, 110] and [-110, -80] (the EX2-subnormal window t in (-150, -126] and the cap t = 125, both
280 * signs) plus a stride-65537 sample of all f32. Not a red test: there is no input for which the two differ. */
281static int qg_mufu_ftz_unobservable(void) {
282 uint64_t n = 0, diff = 0;
283 const float us[2] = { 1.0f, -3.0f };
284 for (int pass = 0; pass < 3; pass++) {
285 uint64_t lo = pass == 0 ? f2u(80.0f) : pass == 1 ? f2u(-80.0f) : 0, hi = pass == 0 ? f2u(110.0f) : pass == 1 ? f2u(-110.0f) : 0xffffffffull, step = pass == 2 ? 65537 : 1;
286 for (uint64_t b = lo; b <= hi; b += step) {
287 float g = u2f((uint32_t)b);
288 if (isnan(g)) continue;
289 float t = fminf(g * u2f(0xbfb8aa3bu), 125.0f), r = 1.0f / (exp2f(t) + 1.0f);
290 for (int k = 0; k < 2; k++) { n++; if (bf16_rn((g * r) * us[k]) != qswiglu_bits(g, us[k])) diff++; }
291 }
292 }
293 printf("%s qgemm SwiGLU: MUFU flush vs IEEE, %llu inputs, %llu differ (t-cap keeps it unobservable, #462)\n", diff ? "FAIL" : "PASS", (unsigned long long)n, (unsigned long long)diff);
294 return diff ? 1 : 0;
295}
296static int qg_host(void) {
297 int fail = qg_mufu_ftz_unobservable();
298 for (uint32_t layout = 0; layout < 3; layout += 2) {
299 QProb p; qprob_make(&p, 3, 256, 384, 40, 17, layout, 0, 0);
300 double worst = 0;
301 for (uint32_t i = 0; i < p.M; i += 3) {
302 if (layout == OMEGA_GPU_PF_PLAIN) for (uint32_t c = 0; c < p.n; c++) { double m, o = qoracle(&p, i, c, &m); double d = fabs(qemu_phys(&p, i, c) - o) / (m + 1e-30); if (d > worst) worst = d; }
303 else for (uint32_t a = 0; a < p.n / 2; a++) { uint32_t G = a / 8, mm = a % 8; double m1, m2;
304 double og = qoracle(&p, i, a, &m1), ou = qoracle(&p, i, p.n / 2 + a, &m2);
305 double dg = fabs(qemu_phys(&p, i, 16 * G + mm) - og) / (m1 + 1e-30), du = fabs(qemu_phys(&p, i, 16 * G + 8 + mm) - ou) / (m2 + 1e-30);
306 if (dg > worst) worst = dg;
307 if (du > worst) worst = du; }
308 }
309 const int ok = worst < 1e-6;
310 printf("qgemm emulation vs double oracle (%s): worst error %.3g of the sum of |terms| %s\n", layout ? "swiglu_nat" : "plain", worst, ok ? "PASS" : "FAIL");
311 if (!ok) fail++;
312 qprob_free(&p);
313 }
314 return fail;
315}
316
317static int qg_sim_run(const QProb *p, const OmegaQgSpec *sp, int order, void *y, char *msg, size_t msgn) {
318 static BlackwellIRProgram prog;
319 if (omega_blackwell_codegen_qgemm_prog(sp, &prog) != 0) { snprintf(msg, msgn, "codegen refused (spec_ok %d, insns %zu)", omega_qg_spec_ok(sp), prog.count); return -1; }
320 const uint32_t bm = 16u * sp->rt, span = omega_qg_span(sp), KB = p->k / 128;
321 QTile *tiles = calloc(p->M + 1, sizeof *tiles);
322 const uint32_t nt = qmake_tiles(p, bm, tiles);
323 uint32_t *ridx = malloc((p->M + bm) * 4); for (uint32_t i = 0; i < p->M + bm; i++) ridx[i] = i < p->M ? p->src[i] : 0;
324 const size_t ob = p->layout == OMEGA_GPU_PF_PLAIN ? 4 : 1, yb = (size_t)(p->M + bm) * p->n * ob;
325 memset(y, 0xee, yb);
326 uint8_t *smem = malloc(omega_qg_shared_bytes(sp));
327 static WsSim s; memset(&s, 0, sizeof s);
328 s.p = &prog; s.threads = 32u * sp->warps; s.smem = smem; s.smem_bytes = omega_qg_shared_bytes(sp); s.order = order;
329 const uint64_t a[5] = { (uintptr_t)tiles, sp->direct ? 0 : (uintptr_t)ridx, (uintptr_t)p->xq, (uintptr_t)y, (uintptr_t)p->xs };
330 for (int i = 0; i < 5; i++) { s.cbank[(0x380 + 8 * i) / 4] = (uint32_t)a[i]; s.cbank[(0x380 + 8 * i) / 4 + 1] = (uint32_t)(a[i] >> 32); }
331 s.cbank[0x3a8 / 4] = p->n;
332 const size_t XR = p->xr + p->pad;
333 s.ranges[0].lo = (uintptr_t)tiles; s.ranges[0].hi = (uintptr_t)(tiles + nt);
334 s.ranges[1].lo = (uintptr_t)ridx; s.ranges[1].hi = (uintptr_t)(ridx + p->M + bm);
335 s.ranges[2].lo = (uintptr_t)p->xq; s.ranges[2].hi = (uintptr_t)(p->xq + XR * p->k);
336 s.ranges[3].lo = (uintptr_t)y; s.ranges[3].hi = (uintptr_t)y + (size_t)p->M * p->n * ob;
337 s.ranges[4].lo = (uintptr_t)p->w; s.ranges[4].hi = (uintptr_t)(p->w + (size_t)p->E * p->n * p->k);
338 s.ranges[5].lo = (uintptr_t)p->swf; s.ranges[5].hi = (uintptr_t)(p->swf + (size_t)p->E * (p->n / 128) * KB);
339 s.ranges[6].lo = (uintptr_t)p->xs; s.ranges[6].hi = (uintptr_t)(p->xs + XR * KB);
340 s.nranges = 7;
341 const int err = ws_run(&s, p->n / span, nt);
342 if (err) snprintf(msg, msgn, "simulator %.150s", s.msg);
343 free(tiles); free(ridx); free(smem);
344 return err ? -1 : 0;
345}
346static int qg_sim(void) {
347 int fail = 0; char msg[200];
348 static const struct { uint32_t E, n, k, M, xr, layout, direct, rt, warps; } cs[] = {
349 { 3, 256, 384, 300, 120, OMEGA_GPU_PF_SWIGLU_NAT, 0, 4, 8 },
350 { 3, 256, 256, 200, 0, OMEGA_GPU_PF_PLAIN, 1, 4, 8 },
351 { 2, 256, 128, 150, 64, OMEGA_GPU_PF_PLAIN, 0, 4, 8 },
352 { 2, 512, 1024, 140, 0, OMEGA_GPU_PF_SWIGLU_NAT, 1, 4, 8 },
353 };
354 for (size_t c = 0; c < sizeof cs / sizeof cs[0]; c++) {
355 const uint32_t bm = 16 * cs[c].rt;
356 QProb p; qprob_make(&p, cs[c].E, cs[c].n, cs[c].k, cs[c].M, cs[c].xr, cs[c].layout, cs[c].direct, bm);
357 OmegaQgSpec sp = { cs[c].k, cs[c].rt, cs[c].warps, cs[c].layout == OMEGA_GPU_PF_PLAIN ? OMEGA_QG_EPI_F32 : OMEGA_QG_EPI_SWIGLU, cs[c].direct, cs[c].n, 45, 0 };
358 uint32_t *rows = malloc(p.M * 4); for (uint32_t i = 0; i < p.M; i++) rows[i] = i;
359 void *y = malloc((size_t)(p.M + bm) * p.n * 4);
360 for (int order = 0; order < 2; order++) {
361 const int rc = qg_sim_run(&p, &sp, order, y, msg, sizeof msg);
362 size_t ulp1 = 0, bad = rc ? 0 : qcompare(&p, y, rows, p.M, 0, &ulp1);
363 printf("sim qgemm case %zu (n %u k %u M %u %s %s rt %u warps %u) order %d: %s, %zu differ\n", c, p.n, p.k, p.M, cs[c].layout ? "swiglu" : "f32",
364 cs[c].direct ? "direct" : "indexed", cs[c].rt, cs[c].warps, order, rc ? msg : "ran", bad);
365 if (rc || bad) fail++;
366 }
367 if (c == 0 || c == 1) for (int m = 1; m <= 4; m++) {
368 OmegaQgSpec ms = sp; ms.mutant = m;
369 const int rc = qg_sim_run(&p, &ms, 0, y, msg, sizeof msg);
370 const size_t bad = rc ? 0 : qcompare(&p, y, rows, p.M, 0, NULL);
371 printf("sim qgemm case %zu mutant %d: %s\n", c, m, rc ? msg : bad ? "caught" : "NOT CAUGHT");
372 if (rc || !bad) fail++;
373 }
374 free(rows); free(y); qprob_free(&p);
375 }
376 return fail;
377}
378
379static int g_cached; /* 1: weights in a GPU-cached device copy (GEMV layout), 0: read in place from the GEMV tensor */
380static int qg_chip_shape(const char *name, uint32_t tokens, int reps, int check_rows, int mutants) {
381 uint32_t E, n, k, M, xr, layout, direct;
382 if (!strcmp(name, "gateup")) { E = 128; n = 1536; k = 2048; M = tokens * 8; xr = tokens; layout = OMEGA_GPU_PF_SWIGLU_NAT; direct = 0; }
383 else if (!strcmp(name, "down")) { E = 128; n = 2048; k = 768; M = tokens * 8; xr = 0; layout = OMEGA_GPU_PF_PLAIN; direct = 1; }
384 else if (!strcmp(name, "qkv")) { E = 1; n = 5120; k = 2048; M = tokens; xr = tokens; layout = OMEGA_GPU_PF_PLAIN; direct = 0; }
385 else if (!strcmp(name, "o")) { E = 1; n = 2048; k = 4096; M = tokens; xr = tokens; layout = OMEGA_GPU_PF_PLAIN; direct = 0; }
386 /* Qwen3.8-27B dense prefill (direct rows): fused gate|up, down, the GDN qkv|z mix, o */
387 else if (!strcmp(name, "q38gu")) { E = 1; n = 34816; k = 5120; M = tokens; xr = 0; layout = OMEGA_GPU_PF_SWIGLU_NAT; direct = 1; }
388 else if (!strcmp(name, "q38down")) { E = 1; n = 5120; k = 17408; M = tokens; xr = 0; layout = OMEGA_GPU_PF_PLAIN; direct = 1; }
389 else if (!strcmp(name, "q38mix")) { E = 1; n = 16384; k = 5120; M = tokens; xr = 0; layout = OMEGA_GPU_PF_PLAIN; direct = 1; }
390 else if (!strcmp(name, "q38o")) { E = 1; n = 5120; k = 6144; M = tokens; xr = 0; layout = OMEGA_GPU_PF_PLAIN; direct = 1; }
391 else { printf("unknown shape %s\n", name); return 1; }
392 const uint32_t bm = omega_gpu_pf_qgemm_bm(), KB = k / 128;
393 QProb p; qprob_make(&p, E, n, k, M, xr, layout, direct, bm);
394 int fail = 0, rc;
395 OmegaGpuFp8Tensor *wt = NULL; OmegaGpuMem xd = { 0 }, sd = { 0 }, yd = { 0 }, tab = { 0 }, ridx = { 0 }, wc = { 0 }, sc = { 0 };
396 const size_t XR = p.xr + bm, ob = layout == OMEGA_GPU_PF_PLAIN ? 4 : 1;
397 rc = omega_gpu_fp8_tensor_upload(E, n, k, p.w, p.sbf, &wt) == 0 ? 0 : OMEGA_GPU_PF_CHIP_FAIL; /* the GEMV tensor, read in place */
398 const size_t xb = (XR * k + 15) & ~(size_t)15, sb = (XR * KB * 4 + 15) & ~(size_t)15, yb = (size_t)(M + bm) * n * ob;
399 if (!rc) rc = omega_gpu_pf_alloc_dev(xb, &xd);
400 if (!rc) rc = omega_gpu_pf_alloc_dev(sb, &sd);
401 if (!rc) rc = omega_gpu_pf_alloc_dev(yb, &yd);
402 if (!rc) rc = omega_gpu_pf_upload(&xd, 0, p.xq, XR * k & ~(size_t)15);
403 if (!rc) rc = omega_gpu_pf_upload(&sd, 0, p.xs, XR * KB * 4 & ~(size_t)15);
404 const uint32_t maxt = M / bm + E + 1;
405 if (!rc) rc = omega_gpu_pf_alloc_host((size_t)maxt * 32, &tab);
406 if (!rc) rc = omega_gpu_pf_alloc_host((size_t)(M + bm) * 4, &ridx);
407 if (rc) { printf("%s setup: %s (%s)\n", name, omega_gpu_pf_rc_name(rc), omega_gpu_session_last_error()); qprob_free(&p); return 1; }
408 uint32_t nt = 0;
409 if (!g_cached) nt = omega_gpu_pf_qgemm_tiles(wt, p.counts, tab.cpu, maxt);
410 else {
411 const size_t wbytes = (size_t)E * n * k, sbytes = ((size_t)E * (n / 128) * KB * 4 + 15) & ~(size_t)15;
412 float *sst = calloc(1, sbytes); memcpy(sst, p.swf, (size_t)E * (n / 128) * KB * 4);
413 if (omega_gpu_pf_alloc_dev(wbytes, &wc) || omega_gpu_pf_alloc_dev(sbytes, &sc) || omega_gpu_pf_upload(&wc, 0, p.w, wbytes) || omega_gpu_pf_upload(&sc, 0, sst, sbytes)) { printf("%s cached copy failed\n", name); free(sst); return 1; }
414 free(sst);
415 uint64_t *wv = malloc(E * 8), *sv = malloc(E * 8);
416 for (uint32_t e = 0; e < E; e++) { wv[e] = wc.va + (uint64_t)e * n * k; sv[e] = sc.va + (uint64_t)e * (n / 128) * KB * 4; }
417 nt = omega_gpu_pf_qgemm_tiles_va(E, wv, sv, p.counts, tab.cpu, maxt); free(wv); free(sv);
418 }
419 if (!nt) { printf("%s tile table failed\n", name); return 1; }
420 { uint32_t *ri = ridx.cpu; for (uint32_t i = 0; i < M + bm; i++) ri[i] = i < M ? p.src[i] : 0; }
421 omega_gpu_session_lock(); (void)omega_gpu_session_sync_to_gpu(&tab); (void)omega_gpu_session_sync_to_gpu(&ridx); omega_gpu_session_unlock();
422 const uint64_t rva = direct ? 0 : ridx.va;
423 uint8_t *y1 = malloc(yb), *y2 = calloc(1, yb);
424 uint64_t ns[32] = { 0 };
425 if (reps > 32) reps = 32;
426 for (int r = 0; r < reps && !rc; r++) rc = omega_gpu_pf_qgemm(n, k, layout, tab.va, nt, rva, xd.va, sd.va, yd.va, &ns[r]);
427 if (!rc) rc = omega_gpu_pf_download(&yd, 0, y1, yb & ~(size_t)15);
428 if (!rc) rc = omega_gpu_pf_upload(&yd, 0, y2, yb & ~(size_t)15); /* zero the output, run once more: identical result required */
429 uint64_t ns2 = 0;
430 if (!rc) rc = omega_gpu_pf_qgemm(n, k, layout, tab.va, nt, rva, xd.va, sd.va, yd.va, &ns2);
431 if (!rc) rc = omega_gpu_pf_download(&yd, 0, y2, yb & ~(size_t)15);
432 if (rc) { printf("%s run: %s (%s)\n", name, omega_gpu_pf_rc_name(rc), omega_gpu_session_last_error()); fail++; }
433 else {
434 const size_t outb = (size_t)M * n * ob;
435 printf("%s second launch identical: %s\n", name, memcmp(y1, y2, outb) ? "NO" : "yes");
436 if (memcmp(y1, y2, outb)) fail++;
437 uint32_t *rows = malloc(check_rows * 4); for (int q = 0; q < check_rows; q++) rows[q] = q == 0 ? 0 : q == 1 ? M - 1 : rnd() % M;
438 size_t ulp1 = 0; const size_t bad = qcompare(&p, y1, rows, check_rows, layout == OMEGA_GPU_PF_PLAIN ? 0 : 1, &ulp1);
439 printf("%s bit-identity on %d sampled rows: %zu differ", name, check_rows, bad);
440 if (layout != OMEGA_GPU_PF_PLAIN) printf(" (beyond 1 bf16 ulp; %zu within 1 ulp of %zu)", ulp1, (size_t)check_rows * n / 2);
441 printf("\n");
442 if (bad) fail++;
443 qsort(ns, reps, 8, cmp_u64);
444 const double fl = 2.0 * M * n * k, med = (double)ns[reps / 2];
445 printf("QGEMM%s %s tokens %u M %u n %u k %u tiles %u: median %.3f ms (min %.3f), %.1f TFLOPS, weights %.0f MB -> %.0f GB/s of distinct weights\n", g_cached ? "-cached" : "-inplace", name, tokens, M, n, k, nt,
446 med / 1e6, ns[0] / 1e6, fl / med / 1e3, (double)E * n * k / 1e6, (double)E * n * k / med);
447 if (mutants) for (int m = 1; m <= 4; m++) {
448 omega_gpu_pf_qgemm_test_set_mutant(m);
449 int mrc = omega_gpu_pf_qgemm(n, k, layout, tab.va, nt, rva, xd.va, sd.va, yd.va, NULL);
450 omega_gpu_pf_qgemm_test_set_mutant(0);
451 if (!mrc) mrc = omega_gpu_pf_download(&yd, 0, y2, yb & ~(size_t)15);
452 const size_t mb = mrc ? 0 : qcompare(&p, y2, rows, check_rows > 8 ? 8 : check_rows, layout == OMEGA_GPU_PF_PLAIN ? 0 : 1, NULL);
453 printf("%s mutant %d: %s\n", name, m, mrc ? "launch failed" : mb ? "caught" : "NOT CAUGHT");
454 if (mrc || !mb) fail++;
455 }
456 free(rows);
457 }
458 free(y1); free(y2);
459 omega_gpu_fp8_tensor_free(wt); omega_gpu_pf_free(&wc); omega_gpu_pf_free(&sc); omega_gpu_pf_free(&xd); omega_gpu_pf_free(&sd); omega_gpu_pf_free(&yd); omega_gpu_pf_free(&tab); omega_gpu_pf_free(&ridx);
460 qprob_free(&p);
461 return fail;
462}
463
464int main(int argc, char **argv) {
465 int host = 1, sim = 1, chip = 0, reps = 7, rows = 64; uint32_t tokens = 3600, rt = 0, warps = 0, wait = 0, waitset = 0; const char *only = NULL;
466 for (int i = 1; i < argc; i++) {
467 if (!strcmp(argv[i], "--host-only")) sim = 0;
468 else if (!strcmp(argv[i], "--sim")) host = 0;
469 else if (!strcmp(argv[i], "--chip")) { chip = 1; host = 0; sim = 0; }
470 else if (!strcmp(argv[i], "--tokens") && i + 1 < argc) tokens = (uint32_t)atoi(argv[++i]);
471 else if (!strcmp(argv[i], "--reps") && i + 1 < argc) reps = atoi(argv[++i]);
472 else if (!strcmp(argv[i], "--rows") && i + 1 < argc) rows = atoi(argv[++i]);
473 else if (!strcmp(argv[i], "--shape") && i + 1 < argc) only = argv[++i];
474 else if (!strcmp(argv[i], "--cached")) g_cached = 1;
475 else if (!strcmp(argv[i], "--rt") && i + 1 < argc) rt = (uint32_t)atoi(argv[++i]);
476 else if (!strcmp(argv[i], "--warps") && i + 1 < argc) warps = (uint32_t)atoi(argv[++i]);
477 else if (!strcmp(argv[i], "--wait") && i + 1 < argc) { wait = (uint32_t)atoi(argv[++i]); waitset = 1; }
478 }
479 int fail = 0;
480 if (host) { fail += pq_host(); fail += qg_host(); }
481 if (sim) { fail += pq_sim(); fail += qg_sim(); }
482 if (chip) {
483 if (rt || warps || waitset) omega_gpu_pf_qgemm_set_shape(rt ? rt : 4, warps ? warps : 8, waitset ? wait : 45);
484 const int q38 = only && !strcmp(only, "q38"); /* --shape q38: the quantiser plus the four Qwen3.8 shapes */
485 if (!only || q38) fail += pq_chip(tokens, reps);
486 static const char *shapes[] = { "gateup", "down", "qkv", "o", "q38gu", "q38down", "q38mix", "q38o" };
487 for (int s = 0; s < 8; s++) {
488 const int run = q38 ? s >= 4 : only ? !strcmp(only, shapes[s]) : s < 4;
489 if (run) fail += qg_chip_shape(shapes[s], tokens, reps, rows, q38 ? s < 6 : !only || s < 2);
490 }
491 }
492 printf("GPU_QGEMM: %s\n", fail ? "FAIL" : "PASS");
493 return fail ? 1 : 0;
494}