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

336 lines · 24169 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* gpu_pfgemm_test.c (OWN-30B T3b, 2026-10-13): tensor-core grouped FP8 GEMM v2, device resident (omega_blackwell_codegen_tc2.h,
3 * omega_gpu_pfgemm.c).
4 * --host-only register budgets of the production specs; the SWIGLU layout transform against a double oracle on the
5 * original (unpermuted) weights; the C emulation of the folded scale against a double oracle
6 * --sim the kernel in the warp simulator (both fragment orders) bit-identical to the C emulation (F32 and SWIGLU,
7 * indexed and direct rows); mutants 1-4 caught (a simulator error is a FAIL, never "caught")
8 * --chip (under flock /tmp/aien-gb10.lock) through the device API with GPU-cached buffers: sampled rows vs the
9 * emulation (F32 bit-identical; SWIGLU within 1 bf16 ulp: MUFU.EX2/RCP are approximate on the chip), second
10 * launch identical, mutants caught, medians of --reps at --tokens for every production shape
11 * Verdict line last: "GPU_PFGEMM: PASS|FAIL". */
12#include "omega_blackwell_codegen_tc2.h"
13#include "omega_blackwell_codegen_tc.h"
14#include "omega_gpu_fp8_api.h"
15#include "omega_gpu_prefill.h"
16#include "bw_warp_sim_tc.h"
17#include <math.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22static const HmmaModel GB10 = { 16, 2, 0, 2, 0 };
23static uint64_t rng = 0x243f6a8885a308d3ull;
24static uint32_t rnd(void) { rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; return (uint32_t)(rng >> 16); }
25static float frnd(void) { return ((float)(rnd() & 0xffffff) / 16777216.0f) * 2.0f - 1.0f; }
26static float u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; }
27static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; }
28static uint16_t bf16_rn(float f) { uint32_t u = f2u(f); if ((u & 0x7fffffffu) > 0x7f800000u) return 0x7fc0u; return (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >> 16); }
29static uint32_t stored_ix(uint32_t p) { static const uint32_t inv[4] = { 0, 2, 1, 3 }; return (p & ~3u) | inv[p & 3u]; }
30
31typedef struct {
32 uint32_t E, n, k, M, xr, layout, direct;
33 uint8_t *w, *pw; /* original and physical weights */
34 uint16_t *sbf; /* scale_inv bf16 */
35 float *ratio; /* physical ratio table */
36 float *x; /* xr * k true values */
37 uint16_t *xp; /* (xr + pad) * k stored bf16 */
38 uint32_t *eid, *src, *counts;
39} Prob;
40
41static uint8_t rand_e4m3(void) { for (;;) { uint8_t c = (uint8_t)rnd(); if ((c & 0x7f) != 0x7f) return c; } }
42static void prob_make(Prob *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) {
43 memset(p, 0, sizeof *p);
44 p->E = E; p->n = n; p->k = k; p->M = M; p->xr = direct ? M : xr; p->layout = layout; p->direct = direct;
45 const size_t wn = (size_t)E * n * k, sn = (size_t)E * (n / 128) * (k / 128), rn = (size_t)E * (n / 8) * (k / 128 + 1);
46 p->w = malloc(wn); p->pw = malloc(wn); p->sbf = malloc(sn * 2); p->ratio = malloc(rn * 4);
47 p->x = malloc((size_t)p->xr * k * 4); p->xp = calloc((size_t)(p->xr + pad) * k, 2);
48 p->eid = malloc(M * 4); p->src = malloc(M * 4); p->counts = calloc(E, 4);
49 for (size_t i = 0; i < wn; i++) p->w[i] = rand_e4m3();
50 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); }
51 if (omega_gpu_pf_weights_host_form(E, n, k, p->w, p->sbf, layout, p->pw, p->ratio) != 0) { printf("host form refused\n"); exit(2); }
52 for (size_t i = 0; i < (size_t)p->xr * k; i++) p->x[i] = frnd() * (rnd() % 16 == 0 ? 8.0f : 1.0f);
53 for (uint32_t r = 0; r < p->xr; r++) omega_tcgemm_permute_x(p->x + (size_t)r * k, k, p->xp + (size_t)r * k);
54 /* sorted expert ids with random segment lengths (some experts empty, some multi-tile) */
55 uint32_t i = 0;
56 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; }
57 for (uint32_t q = 0; q < M; q++) p->src[q] = direct ? q : rnd() % p->xr;
58}
59static void prob_free(Prob *p) { free(p->w); free(p->pw); free(p->sbf); free(p->ratio); free(p->x); free(p->xp); free(p->eid); free(p->src); free(p->counts); }
60
61/* emulation of one physical output (row i, physical col c): the kernel's arithmetic */
62static float emu_phys(const Prob *p, uint32_t i, uint32_t c) {
63 const uint32_t n = p->n, k = p->k, KB = k / 128;
64 const uint16_t *xs = p->xp + (size_t)p->src[i] * k;
65 const uint8_t *wr = p->pw + ((size_t)p->eid[i] * n + c) * k;
66 const float *R0 = p->ratio + ((size_t)p->eid[i] * (n / 16) + c / 16) * (KB + 1) * 2 + (c / 8) % 2;
67#define R(q) R0[2 * (q)]
68 float acc = 0.0f;
69 for (uint32_t kb = 0; kb < KB; kb++) {
70 acc = acc * R(kb);
71 for (uint32_t ch = 0; ch < 2; ch++) for (uint32_t s = 0; s < 4; s++) {
72 float a[16], b[16]; int m = 0;
73 for (uint32_t t = 0; t < 4; t++) for (uint32_t j = 0; j < 4; j++) {
74 uint32_t e = kb * 128 + ch * 64 + 16 * t + 4 * s + j;
75 a[m] = u2f((uint32_t)xs[stored_ix(e)] << 16); b[m] = u2f((uint32_t)omega_tcgemm_w_bf16(wr[e]) << 16); m++;
76 }
77 acc = hm_dot16(&GB10, a, b, acc);
78 }
79 }
80 return acc * R(KB);
81#undef R
82}
83/* the kernel's epilogue arithmetic: MUFU.EX2, FADD, MUFU.RCP, FMUL x 3, F2FP. The two MUFU ops go through ws_mufu_ex2 / ws_mufu_rcp
84 * (bw_mufu_ftz.h, omega #462): the chip's MUFU flushes a subnormal result to a sign-preserving zero, so for t = -g log2 e in (126, 128)
85 * (g in (-88.72, -87.34)) the reciprocal is +-0 and the activation is +-0, not the 2^-56-ish product an IEEE 1/d would give. */
86static uint16_t swiglu_bits(float g, float u) {
87 float t = g * u2f(0xbfb8aa3bu), ex = ws_mufu_ex2(t), d = ex + 1.0f, r = ws_mufu_rcp(d), o = g * r;
88 o = o * u; o = o * u2f(0x5f800000u);
89 return bf16_rn(o);
90}
91/* emulated output row i: F32 -> n floats; SWIGLU -> n/2 bf16 (as u16 in the low half of each u32 slot of out) */
92static void emu_row(const Prob *p, uint32_t i, float *yf, uint16_t *ya) {
93 if (p->layout == OMEGA_GPU_PF_PLAIN) { for (uint32_t c = 0; c < p->n; c++) yf[c] = emu_phys(p, i, c); return; }
94 for (uint32_t a = 0; a < p->n / 2; a++) { uint32_t G = a / 8, m = a % 8; ya[a] = swiglu_bits(emu_phys(p, i, 16 * G + m), emu_phys(p, i, 16 * G + 8 + m)); }
95}
96
97typedef struct { uint64_t w_va, r_va; uint32_t row0, cnt; uint64_t pad; } Tile;
98static uint32_t make_tiles(const Prob *p, uint32_t bm, Tile *t) {
99 uint32_t nt = 0, row = 0; const uint32_t KB = p->k / 128;
100 for (uint32_t e = 0; e < p->E; e++) { for (uint32_t r = 0; r < p->counts[e]; r += bm) {
101 t[nt].w_va = (uintptr_t)(p->pw + (size_t)e * p->n * p->k); t[nt].r_va = (uintptr_t)(p->ratio + (size_t)e * (p->n / 8) * (KB + 1));
102 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]; }
103 return nt;
104}
105
106static int host_only(void) {
107 int fail = 0;
108 static const OmegaTc2Spec specs[] = {
109 { 2048, 8, 2, 16, 1, OMEGA_TC2_EPI_F32, 0, 5120, 0 }, { 2048, 8, 2, 16, 1, OMEGA_TC2_EPI_SWIGLU, 0, 1536, 0 }, { 768, 8, 2, 16, 1, OMEGA_TC2_EPI_F32, 1, 2048, 0 },
110 { 4096, 8, 2, 16, 1, OMEGA_TC2_EPI_F32, 0, 2048, 0 }, { 2048, 8, 2, 16, 2, OMEGA_TC2_EPI_F32, 0, 5120, 0 }, { 2048, 8, 2, 16, 2, OMEGA_TC2_EPI_SWIGLU, 0, 1536, 0 },
111 { 768, 8, 2, 16, 2, OMEGA_TC2_EPI_F32, 1, 2048, 0 }, { 2048, 4, 2, 8, 2, OMEGA_TC2_EPI_F32, 0, 2048, 0 },
112 };
113 static BlackwellIRProgram prog;
114 for (size_t i = 0; i < sizeof specs / sizeof specs[0]; i++) {
115 int rc = omega_blackwell_codegen_tc2_prog(&specs[i], &prog);
116 uint32_t peak = rc == 0 ? prog.regalloc.peak_gpr_usage : 0, decl = omega_bw_gpr_declared(peak);
117 int ok = rc == 0 && decl <= 128u && 32u * specs[i].warps * decl <= 65536u;
118 printf("spec k %u rt %u ct %u warps %u aqn %u epi %u direct %u: %s, %zu instructions, peak %u, declared %u -> %s\n", specs[i].k, specs[i].rt, specs[i].ct, specs[i].warps, specs[i].aqn,
119 specs[i].epi, specs[i].direct, rc ? "REFUSED" : "built", rc ? 0 : prog.count, peak, decl, ok ? "fits" : "DOES NOT FIT");
120 if (!ok && specs[i].aqn == 1) fail++; /* aqn 2 is optional */
121 }
122 /* layout + emulation vs double oracle on the ORIGINAL weights: SWIGLU act[a] ~ silu(x.Wg[i]) * x.Wu[i] * 2^64, i = perm(a) */
123 Prob p; prob_make(&p, 2, 256, 256, 24, 8, OMEGA_GPU_PF_SWIGLU, 0, 32);
124 double worst = 0; const uint32_t KB = p.k / 128, NB = p.n / 128, I = p.n / 2;
125 for (uint32_t i = 0; i < p.M; i++) {
126 uint16_t act[128]; emu_row(&p, i, NULL, act);
127 for (uint32_t a = 0; a < I; a++) {
128 uint32_t li = stored_ix(a); double d[2];
129 for (int h = 0; h < 2; h++) { uint32_t row = h ? I + li : li; double s = 0;
130 for (uint32_t kk = 0; kk < p.k; kk++) {
131 double sc = (double)u2f((uint32_t)p.sbf[((size_t)p.eid[i] * NB + row / 128) * KB + kk / 128] << 16);
132 double wv = ldexp((double)u2f((uint32_t)omega_tcgemm_w_bf16(p.w[((size_t)p.eid[i] * p.n + row) * p.k + kk]) << 16), 120);
133 double xv = (double)u2f((uint32_t)p.xp[(size_t)p.src[i] * p.k + stored_ix(kk)] << 16) / 18446744073709551616.0;
134 s += sc * wv * xv; }
135 d[h] = s; }
136 double want = d[0] / (1.0 + exp(-d[0])) * d[1], got = (double)u2f((uint32_t)act[a] << 16) / 18446744073709551616.0;
137 double err = fabs(got - want) / (fabs(want) + 1e-3 * 1e-0);
138 if (err > worst) worst = err;
139 }
140 }
141 printf("SWIGLU layout + emulation vs double oracle on the original weights: worst relative error %.3g (bf16 output: < 1e-2 expected)\n", worst);
142 if (!(worst < 1e-2)) fail++;
143 prob_free(&p);
144 /* the reference against the chip's MUFU.RCP flush (GB10, evidence OWN30B-L1G-PFGEMM-SWIGLU-FTZ-df9bd9c4, 2026-10-15): at 3600
145 * tokens, 48 sampled rows, all 101 of 101 elements that differed from the IEEE reference had t = -g log2 e in (126.03, 127.99)
146 * and came back +-0 from the chip. g and u are the emulated f32 gate / up values (9 significant digits round-trip exactly),
147 * act the chip's bf16; the first and last entries are the smallest and largest t observed. */
148 static const struct { float g, u; uint16_t act; } chip[] = {
149 { -87.3551483f, 274.485016f, 0x8000 }, { -87.4824142f, 122.849442f, 0x8000 }, { -87.8418655f, 45.4098854f, 0x8000 },
150 { -88.1839066f, -28.427494f, 0x0000 }, { -88.3344727f, -3.59300494f, 0x0000 }, { -88.7186356f, 180.687149f, 0x8000 },
151 };
152 size_t ok = 0;
153 for (size_t i = 0; i < sizeof chip / sizeof chip[0]; i++) ok += swiglu_bits(chip[i].g, chip[i].u) == chip[i].act;
154 /* negative control: g -87 (t 125.5) has a normal reciprocal and must stay a nonzero finite negative activation (the flush must not
155 * reach outside the window); g -89 (t 128.4) overflows ex2 to +inf and is -0 in both arithmetics (not a flush) */
156 const uint16_t nc_lo = swiglu_bits(-87.0f, 100.0f), nc_hi = swiglu_bits(-89.0f, 100.0f);
157 const int nc_ok = (nc_lo & 0x7fffu) != 0 && (nc_lo & 0x7f80u) != 0x7f80u && (nc_lo & 0x8000u) && nc_hi == 0x8000u;
158 printf("SWIGLU reference vs chip-observed MUFU.RCP flush window: %zu/%zu match; control g -87 -> %04x (nonzero), g -89 -> %04x (-0): %s\n",
159 ok, sizeof chip / sizeof chip[0], nc_lo, nc_hi, ok == sizeof chip / sizeof chip[0] && nc_ok ? "ok" : "FAIL");
160 if (ok != sizeof chip / sizeof chip[0] || !nc_ok) fail++;
161 return fail;
162}
163
164static int sim_run(const Prob *p, const OmegaTc2Spec *sp, int order, void *y, char *msg, size_t msgn) {
165 static BlackwellIRProgram prog;
166 if (omega_blackwell_codegen_tc2_prog(sp, &prog) != 0) { snprintf(msg, msgn, "codegen refused (spec_ok %d)", omega_tc2_spec_ok(sp)); return -1; }
167 const uint32_t bm = 16u * sp->rt, span = omega_tc2_span(sp);
168 Tile *tiles = calloc(p->M + 1, sizeof *tiles);
169 uint32_t nt = make_tiles(p, bm, tiles);
170 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;
171 const size_t yb = (size_t)(p->M + bm) * p->n * (p->layout == OMEGA_GPU_PF_SWIGLU ? 1 : 4);
172 memset(y, 0xee, yb);
173 uint8_t *smem = malloc(omega_tc2_shared_bytes(sp));
174 static WsSim s; memset(&s, 0, sizeof s);
175 s.p = &prog; s.threads = 32u * sp->warps; s.smem = smem; s.smem_bytes = omega_tc2_shared_bytes(sp); s.order = order;
176 uint64_t a[4] = { (uintptr_t)tiles, sp->direct ? 0 : (uintptr_t)ridx, (uintptr_t)p->xp, (uintptr_t)y };
177 for (int i = 0; i < 4; i++) { s.cbank[(0x380 + 8 * i) / 4] = (uint32_t)a[i]; s.cbank[(0x380 + 8 * i) / 4 + 1] = (uint32_t)(a[i] >> 32); }
178 s.cbank[0x3a0 / 4] = p->n;
179 const uint32_t KB = p->k / 128;
180 s.ranges[0].lo = (uintptr_t)tiles; s.ranges[0].hi = (uintptr_t)(tiles + nt);
181 s.ranges[1].lo = (uintptr_t)ridx; s.ranges[1].hi = (uintptr_t)(ridx + p->M + bm);
182 s.ranges[2].lo = (uintptr_t)p->xp; s.ranges[2].hi = (uintptr_t)(p->xp + (size_t)(p->xr + (p->direct ? bm : 0)) * p->k);
183 s.ranges[3].lo = (uintptr_t)y; s.ranges[3].hi = (uintptr_t)y + (size_t)p->M * p->n * (p->layout == OMEGA_GPU_PF_SWIGLU ? 1 : 4);
184 s.ranges[4].lo = (uintptr_t)p->pw; s.ranges[4].hi = (uintptr_t)(p->pw + (size_t)p->E * p->n * p->k);
185 s.ranges[5].lo = (uintptr_t)p->ratio; s.ranges[5].hi = (uintptr_t)(p->ratio + (size_t)p->E * (p->n / 8) * (KB + 1));
186 s.nranges = 6;
187 int err = ws_run(&s, p->n / span, nt);
188 if (err) snprintf(msg, msgn, "simulator %s", s.msg);
189 free(tiles); free(ridx); free(smem);
190 return err ? -1 : 0;
191}
192static size_t compare(const Prob *p, const void *y, const uint32_t *rows, uint32_t nrows, int tol_ulp, size_t *ulp1) {
193 size_t bad = 0; if (ulp1) *ulp1 = 0;
194 float *yf = malloc(p->n * 4); uint16_t *ya = malloc(p->n);
195 for (uint32_t q = 0; q < nrows; q++) {
196 uint32_t i = rows[q]; emu_row(p, i, yf, ya);
197 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++; } }
198 else { const uint16_t *g = (const uint16_t *)y + (size_t)i * (p->n / 2);
199 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)++; }
200 else { if (getenv("PFDBG") && bad < 128) { const uint32_t G = c / 8, mm = c % 8; const float gg = emu_phys(p, i, 16 * G + mm), uu = emu_phys(p, i, 16 * G + 8 + mm);
201 printf(" row %u act %u got %04x want %04x g %.9g u %.9g t %.6f\n", i, c, g[c], ya[c], gg, uu, gg * u2f(0xbfb8aa3bu)); } bad++; } } }
202 }
203 free(yf); free(ya);
204 return bad;
205}
206
207static int sim_tests(void) {
208 int fail = 0; char msg[256];
209 static const struct { uint32_t E, n, k, M, xr, layout, direct, rt, warps, aqn; } cs[] = {
210 { 3, 256, 256, 70, 20, OMEGA_GPU_PF_PLAIN, 0, 8, 16, 1 }, { 3, 256, 384, 70, 20, OMEGA_GPU_PF_SWIGLU, 0, 8, 16, 1 },
211 { 3, 256, 256, 150, 0, OMEGA_GPU_PF_PLAIN, 1, 8, 16, 1 }, { 2, 256, 256, 40, 9, OMEGA_GPU_PF_PLAIN, 0, 4, 8, 2 },
212 { 2, 512, 128, 300, 30, OMEGA_GPU_PF_SWIGLU, 0, 8, 16, 2 },
213 { 3, 512, 256, 150, 40, OMEGA_GPU_PF_SWIGLU, 0, 4, 16, 1 }, { 3, 256, 384, 130, 0, OMEGA_GPU_PF_PLAIN, 1, 4, 16, 1 }, /* the chip shape (QMD shared cap) */
214 };
215 for (size_t c = 0; c < sizeof cs / sizeof cs[0]; c++) {
216 Prob p; prob_make(&p, cs[c].E, cs[c].n, cs[c].k, cs[c].M, cs[c].xr, cs[c].layout, cs[c].direct, 16 * cs[c].rt);
217 OmegaTc2Spec sp = { cs[c].k, cs[c].rt, 2, cs[c].warps, cs[c].aqn, cs[c].layout == OMEGA_GPU_PF_SWIGLU ? OMEGA_TC2_EPI_SWIGLU : OMEGA_TC2_EPI_F32, cs[c].direct, cs[c].n, 0 };
218 void *y = malloc((size_t)(p.M + 128) * p.n * 4);
219 uint32_t *rows = malloc(p.M * 4); for (uint32_t i = 0; i < p.M; i++) rows[i] = i;
220 for (int order = 0; order < 2; order++) {
221 int rc = sim_run(&p, &sp, order, y, msg, sizeof msg);
222 size_t bad = rc ? (size_t)-1 : compare(&p, y, rows, p.M, 0, NULL);
223 printf("sim case %zu (E %u n %u k %u M %u %s%s rt %u warps %u aqn %u) order %d: %s, %zu differ\n", c, p.E, p.n, p.k, p.M, p.layout ? "SWIGLU" : "F32", p.direct ? " direct" : "", sp.rt, sp.warps, sp.aqn, order, rc ? msg : "ran", rc ? 0 : bad);
224 if (rc || bad) fail++;
225 }
226 if (c == 0 || c == 1) for (int m = 1; m <= 4; m++) {
227 OmegaTc2Spec mm = sp; mm.mutant = m;
228 int rc = sim_run(&p, &mm, 0, y, msg, sizeof msg);
229 size_t bad = rc ? 0 : compare(&p, y, rows, p.M, 0, NULL);
230 printf("sim case %zu mutant %d: %s\n", c, m, rc ? msg : bad ? "caught" : "NOT CAUGHT");
231 if (rc || !bad) fail++;
232 }
233 free(y); free(rows); prob_free(&p);
234 }
235 return fail;
236}
237
238static 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; }
239
240static int chip_shape(const char *name, uint32_t tokens, int reps, int check_rows, int mutants) {
241 uint32_t E, n, k, M, xr, layout, direct;
242 if (!strcmp(name, "gateup")) { E = 128; n = 1536; k = 2048; M = tokens * 8; xr = tokens; layout = OMEGA_GPU_PF_SWIGLU; direct = 0; }
243 else if (!strcmp(name, "down")) { E = 128; n = 2048; k = 768; M = tokens * 8; xr = 0; layout = OMEGA_GPU_PF_PLAIN; direct = 1; }
244 else if (!strcmp(name, "qkv")) { E = 1; n = 5120; k = 2048; M = tokens; xr = tokens; layout = OMEGA_GPU_PF_PLAIN; direct = 0; }
245 else if (!strcmp(name, "o")) { E = 1; n = 2048; k = 4096; M = tokens; xr = tokens; layout = OMEGA_GPU_PF_PLAIN; direct = 0; }
246 else { printf("unknown shape %s\n", name); return 1; }
247 const uint32_t bm = omega_gpu_pf_gemm_bm();
248 Prob p; prob_make(&p, E, n, k, M, xr, layout, direct, bm);
249 int fail = 0, rc;
250 OmegaPfWeights wt; OmegaGpuMem xd = { 0 }, yd = { 0 }, tab = { 0 }, ridx = { 0 };
251 rc = omega_gpu_pf_weights_upload(E, n, k, p.w, p.sbf, layout, &wt);
252 const size_t xb = ((size_t)(p.xr + bm) * k * 2 + 15) & ~(size_t)15, yb = (size_t)(M + bm) * n * (layout == OMEGA_GPU_PF_SWIGLU ? 1 : 4);
253 if (!rc) rc = omega_gpu_pf_alloc_dev(xb, &xd);
254 if (!rc) rc = omega_gpu_pf_alloc_dev(yb, &yd);
255 if (!rc) rc = omega_gpu_pf_upload(&xd, 0, p.xp, xb);
256 const uint32_t maxt = M / bm + E + 1;
257 if (!rc) rc = omega_gpu_pf_alloc_host((size_t)maxt * 32, &tab);
258 if (!rc) rc = omega_gpu_pf_alloc_host((size_t)(M + bm) * 4, &ridx);
259 if (rc) { printf("%s setup: %s (%s)\n", name, omega_gpu_pf_rc_name(rc), omega_gpu_session_last_error()); return 1; }
260 uint32_t nt = omega_gpu_pf_gemm_tiles(&wt, p.counts, tab.cpu, maxt);
261 { uint32_t *ri = ridx.cpu; for (uint32_t i = 0; i < M + bm; i++) ri[i] = i < M ? p.src[i] : 0; }
262 omega_gpu_session_lock(); (void)omega_gpu_session_sync_to_gpu(&tab); (void)omega_gpu_session_sync_to_gpu(&ridx); omega_gpu_session_unlock();
263 const uint64_t rva = direct ? 0 : ridx.va;
264 uint8_t *y1 = malloc(yb), *y2 = malloc(yb);
265 uint64_t ns[32] = { 0 };
266 if (reps > 32) reps = 32;
267 for (int r = 0; r < reps && !rc; r++) rc = omega_gpu_pf_gemm(&wt, tab.va, nt, rva, xd.va, yd.va, &ns[r]);
268 if (!rc) rc = omega_gpu_pf_download(&yd, 0, y1, yb & ~(size_t)15);
269 memset(y2, 0, yb);
270 { uint8_t z[4096]; memset(z, 0, sizeof z); /* zero the output on the device, run once more: the second result must be identical */
271 uint8_t *zb = calloc(1, yb); if (!rc) rc = omega_gpu_pf_upload(&yd, 0, zb, yb & ~(size_t)15); free(zb); (void)z; }
272 uint64_t ns2 = 0;
273 if (!rc) rc = omega_gpu_pf_gemm(&wt, tab.va, nt, rva, xd.va, yd.va, &ns2);
274 if (!rc) rc = omega_gpu_pf_download(&yd, 0, y2, yb & ~(size_t)15);
275 if (rc) { printf("%s run: %s (%s)\n", name, omega_gpu_pf_rc_name(rc), omega_gpu_session_last_error()); fail++; }
276 else {
277 const size_t outb = (size_t)M * n * (layout == OMEGA_GPU_PF_SWIGLU ? 1 : 4);
278 printf("%s second launch identical: %s\n", name, memcmp(y1, y2, outb) ? "NO" : "yes");
279 if (memcmp(y1, y2, outb)) fail++;
280 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;
281 size_t ulp1 = 0, bad = compare(&p, y1, rows, check_rows, layout == OMEGA_GPU_PF_SWIGLU ? 1 : 0, &ulp1);
282 printf("%s bit-identity on %d sampled rows: %zu differ%s", name, check_rows, bad, layout == OMEGA_GPU_PF_SWIGLU ? "" : "\n");
283 if (layout == OMEGA_GPU_PF_SWIGLU) printf(" (beyond 1 bf16 ulp; %zu within 1 ulp of %zu)\n", ulp1, (size_t)check_rows * n / 2);
284 if (bad) fail++;
285 qsort(ns, reps, 8, cmp_u64);
286 const double fl = 2.0 * M * n * k, med = (double)ns[reps / 2];
287 printf("TCGEMM2 %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", name, tokens, M, n, k, nt,
288 med / 1e6, ns[0] / 1e6, fl / med / 1e3, (double)E * n * k / 1e6, (double)E * n * k / med);
289 if (mutants) for (int m = 1; m <= 4; m++) {
290 omega_gpu_pf_gemm_test_set_mutant(m);
291 int mrc = omega_gpu_pf_gemm(&wt, tab.va, nt, rva, xd.va, yd.va, NULL);
292 if (!mrc) mrc = omega_gpu_pf_download(&yd, 0, y2, yb & ~(size_t)15);
293 omega_gpu_pf_gemm_test_set_mutant(0);
294 size_t mb = mrc ? 0 : compare(&p, y2, rows, check_rows > 8 ? 8 : check_rows, layout == OMEGA_GPU_PF_SWIGLU ? 1 : 0, NULL);
295 printf("%s mutant %d: %s\n", name, m, mrc ? "launch failed" : mb ? "caught" : "NOT CAUGHT");
296 if (mrc || !mb) fail++;
297 }
298 free(rows);
299 }
300 free(y1); free(y2);
301 omega_gpu_pf_weights_free(&wt); omega_gpu_pf_free(&xd); omega_gpu_pf_free(&yd); omega_gpu_pf_free(&tab); omega_gpu_pf_free(&ridx);
302 prob_free(&p);
303 return fail;
304}
305
306int main(int argc, char **argv) {
307 int host = 1, sim = 1, chip = 0, reps = 7, check = 48, mutants = 1; uint32_t tokens = 3600; const char *shape = NULL;
308 for (int i = 1; i < argc; i++) {
309 if (!strcmp(argv[i], "--host-only")) sim = 0;
310 else if (!strcmp(argv[i], "--sim")) host = 0;
311 else if (!strcmp(argv[i], "--chip")) { chip = 1; host = 0; sim = 0; }
312 else if (!strcmp(argv[i], "--shape") && i + 1 < argc) shape = argv[++i];
313 else if (!strcmp(argv[i], "--tokens") && i + 1 < argc) tokens = (uint32_t)atoi(argv[++i]);
314 else if (!strcmp(argv[i], "--reps") && i + 1 < argc) reps = atoi(argv[++i]);
315 else if (!strcmp(argv[i], "--check-rows") && i + 1 < argc) check = atoi(argv[++i]);
316 else if (!strcmp(argv[i], "--no-mutants")) mutants = 0;
317 else if (!strcmp(argv[i], "--shape-knob") && i + 3 < argc) { uint32_t a = (uint32_t)atoi(argv[i + 1]), b = (uint32_t)atoi(argv[i + 2]), c = (uint32_t)atoi(argv[i + 3]); omega_gpu_pf_gemm_set_shape(a, b, c); i += 3; }
318 else { fprintf(stderr, "GPU_PFGEMM: INVALID argument %s\n", argv[i]); return 2; }
319 }
320 static const char *all[] = { "gateup", "down", "qkv", "o" };
321 int selected = 0;
322 for (int s = 0; s < 4; s++) if (!shape || !strcmp(shape, all[s])) selected++;
323 if (!selected) { fprintf(stderr, "GPU_PFGEMM: INVALID shape '%s'\n", shape); return 2; }
324 if (!host && !sim && !chip) { fprintf(stderr, "GPU_PFGEMM: NOT_RUN (no mode selected)\n"); return 2; }
325 int fail = 0, cases = 0;
326 if (host) { fail += host_only(); cases++; }
327 if (sim) { fail += sim_tests(); cases++; }
328 if (chip) {
329 for (int s = 0; s < 4; s++) if (!shape || !strcmp(shape, all[s])) {
330 fail += chip_shape(all[s], tokens, reps, check, mutants); cases++;
331 }
332 }
333 if (!cases) { fprintf(stderr, "GPU_PFGEMM: NOT_RUN (no cases executed)\n"); return 2; }
334 printf("GPU_PFGEMM: %s\n", fail ? "FAIL" : "PASS");
335 return fail ? 1 : 0;
336}