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

491 lines · 38801 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/*
18 * gpu_fp8gemm_test: the prefill FP8 grouped GEMM (omega_gpu_fp8gemm_api.h), OWN-30B builder F item 26.
19 * --host-only emulation vs the FP8 double oracle, permutation, codegen budgets and refusals (no device)
20 * --sim warp-simulator parity (build with -DWS_MAX_THREADS=512): bit-identical to the host emulation of the
21 * GEMV's summation order in both fragment orders, multi-segment, multi-pass, row clamping, mutants 1-3 caught
22 * (default) the chip. Gate|up 128 x 1536 x 2048 and down 128 x 2048 x 768 over 64 tokens x top-8 routed rows, dense
23 * qkv 5120 x 2048 and o 2048 x 4096 over 64 rows: every output row bit-identical to omega_gpu_fp8_gemv_f32
24 * on the same row, a few rows against the host emulation and the double oracle, mutants caught,
25 * timing medians (host caches evicted) vs the per-token grouped-GEMV path.
26 * knobs: --reps N --mt N --warps N --ctas N --no-evict --tokens N --spin-us N --shape gateup|down|qkv|o|all --out receipt.json
27 * Verdict line last: "gpu_fp8gemm_test: N checks, F failed: PASS|FAIL".
28 */
29#include "omega_gpu_fp8gemm_api.h"
30#include "omega_gpu_fp8_api.h"
31#include "omega_gpu_session.h"
32#include "bw_warp_sim_k.h"
33#include <fcntl.h>
34#include <inttypes.h>
35#include <math.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <time.h>
40#include <unistd.h>
41
42static int g_checks, g_failed;
43#define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0)
44static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; }
45static float frand(uint32_t *s, float lo, float hi) { return lo + (hi - lo) * ((float)(lcg(s) >> 8) / 16777216.0f); }
46static double now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; }
47static 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; }
48static uint16_t bf16_rne(float f) { uint32_t x; memcpy(&x, &f, 4); uint32_t r = x + 0x7fffu + ((x >> 16) & 1u); return (uint16_t)(r >> 16); }
49static float bf16_f32(uint16_t b) { uint32_t x = (uint32_t)b << 16; float f; memcpy(&f, &x, 4); return f; }
50static uint8_t rand_e4m3(uint32_t *s) { uint8_t b = (uint8_t)(lcg(s) >> 24); if (omega_fp8_e4m3_is_nan(b)) b = (uint8_t)(b - 1); return b; }
51static float f32_of_bits(uint32_t b) { float f; memcpy(&f, &b, 4); return f; }
52
53typedef struct { uint32_t experts, n, k; uint8_t *w; uint16_t *s; } Weights;
54static void weights_make(Weights *W, uint32_t experts, uint32_t n, uint32_t k, uint32_t seed) {
55 W->experts = experts; W->n = n; W->k = k;
56 size_t wb = (size_t)experts * n * k, sc = (size_t)experts * ((n + 127) / 128) * (k / 128);
57 W->w = malloc(wb); W->s = malloc(sc * 2);
58 uint32_t st = seed;
59 for (size_t i = 0; i < wb; i++) W->w[i] = rand_e4m3(&st);
60 for (size_t i = 0; i < sc; i++) W->s[i] = bf16_rne(frand(&st, 0.001f, 0.1f) * ((lcg(&st) & 1) ? 1.0f : -1.0f));
61}
62static void weights_free(Weights *W) { free(W->w); free(W->s); }
63
64/* The kernel's exact f32 result for one (expert row, activation row): per lane, per K step a dot chain over the lane's LB
65 * bytes (word, byte), acc = fma(dot, S', acc), then the SHFL.DOWN tree 16..1. w: the row's k bytes, s: its k/128 scale words
66 * (bf16 bits of one 128-row block), x: natural-order activation row. */
67static float emu_row(const uint8_t *w, const uint16_t *s, const float *x, uint32_t k, uint32_t lb) {
68 /* static, not 64 KB of stack (omega#456); this test runs one thread. k is bounded by OMEGA_FP8_MAX_K (16384). */
69 static float xp[16384];
70 if (k > sizeof xp / sizeof xp[0]) { fprintf(stderr, "emu_row: k=%u exceeds %zu\n", k, sizeof xp / sizeof xp[0]); exit(2); }
71 omega_fp8_pack_x_f32(x, k, xp);
72 float a[32]; const uint32_t steps = k / (32u * lb);
73 for (unsigned lane = 0; lane < 32; lane++) {
74 float acc = 0.0f;
75 for (uint32_t st = 0; st < steps; st++) {
76 float dot = 0.0f; const uint32_t c0 = st * 32u * lb + lane * lb;
77 for (uint32_t b = 0; b < lb; b++) dot = fmaf(f32_of_bits(omega_fp8_e4m3_kernel_bits(w[c0 + b])), xp[c0 + b], dot);
78 float sp = bf16_f32(s[c0 / 128u]) * 72057594037927936.0f; /* * 2^56 */
79 acc = fmaf(dot, sp, acc);
80 }
81 a[lane] = acc;
82 }
83 for (unsigned o = 16; o >= 1; o /= 2) { float b[32]; for (unsigned l = 0; l < 32; l++) b[l] = a[l + o < 32 ? l + o : l]; for (unsigned l = 0; l < 32; l++) a[l] += b[l]; }
84 return a[0];
85}
86/* full expected output of a row table against Weights W (emulation) */
87static void expect_rows(const Weights *W, uint32_t M, const uint32_t *ids, const uint32_t *trow, const float *x, float *y, uint32_t lb) {
88 const uint32_t n = W->n, k = W->k;
89 for (uint32_t i = 0; i < M; i++) for (uint32_t r = 0; r < n; r++)
90 y[(size_t)i * n + r] = emu_row(W->w + ((size_t)ids[i] * n + r) * k, W->s + ((size_t)ids[i] * ((n + 127) / 128) + r / 128) * (k / 128), x + (size_t)trow[i] * k, k, lb);
91}
92static float *make_x(uint32_t rows, uint32_t k, uint32_t seed) {
93 float *x = malloc((size_t)rows * k * 4); uint32_t s = seed;
94 for (size_t i = 0; i < (size_t)rows * k; i++) x[i] = frand(&s, -2.0f, 2.0f);
95 return x;
96}
97/* a routed row table: `tokens` tokens x top-k distinct experts, sorted by expert (ties by token) */
98static uint32_t make_table(uint32_t tokens, uint32_t topk, uint32_t experts, uint32_t seed, uint32_t *ids, uint32_t *trow, uint32_t *picks) {
99 uint32_t M = tokens * topk, st = seed;
100 uint32_t *cnt = calloc(experts, 4), (*pick)[16] = malloc((size_t)tokens * sizeof *pick);
101 for (uint32_t t = 0; t < tokens; t++) for (uint32_t j = 0; j < topk; j++) {
102 uint32_t e; int dup;
103 do { e = (lcg(&st) >> 8) % experts; dup = 0; for (uint32_t q = 0; q < j; q++) if (pick[t][q] == e) dup = 1; } while (dup);
104 pick[t][j] = e; cnt[e]++;
105 }
106 uint32_t *pos = malloc(experts * 4), c = 0;
107 for (uint32_t e = 0; e < experts; e++) { pos[e] = c; c += cnt[e]; }
108 for (uint32_t t = 0; t < tokens; t++) for (uint32_t j = 0; j < topk; j++) { uint32_t e = pick[t][j], p = pos[e]++; ids[p] = e; trow[p] = t; }
109 for (uint32_t t = 0; t < tokens; t++) for (uint32_t j = 0; j < topk; j++) picks[t * topk + j] = pick[t][j];
110 free(cnt); free(pick); free(pos);
111 return M;
112}
113
114static void host_only(void) {
115 printf("host-only: emulation, permutation, budgets\n");
116 { Weights W; weights_make(&W, 1, 128, 2048, 5); float *x = make_x(1, 2048, 6);
117 double yo[128], ys[128]; omega_fp8_gemv_oracle(W.w, W.s, 128, 2048, x, yo, ys);
118 double worst = 0; for (uint32_t r = 0; r < 128; r++) { float e = emu_row(W.w + (size_t)r * 2048, W.s, x, 2048, 16); (void)e; }
119 for (uint32_t r = 0; r < 128; r++) { float e = emu_row(W.w + (size_t)r * 2048, W.s + (r / 128) * 16, x, 2048, 16); double d = fabs((double)e - yo[r]) / (ys[r] + 1e-30); if (d > worst) worst = d; }
120 CHECK(worst < 1e-5, "emulation vs the FP8 double oracle: worst rel %.2e", worst);
121 printf("emulation vs oracle: worst relative error %.2e of the term scale\n", worst);
122 weights_free(&W); free(x); }
123 { /* permutation is a bijection of each row, scaled by 2^64 */
124 float x[2048], p[2048]; for (unsigned i = 0; i < 2048; i++) x[i] = (float)(i + 1);
125 omega_fp8gemm_permute_x(x, 2048, 16, p);
126 char *seen = calloc(2048, 1); int ok = 1;
127 for (unsigned i = 0; i < 2048; i++) { double v = (double)p[i] / 18446744073709551616.0; if (v < 1 || v > 2048 || seen[(int)v - 1]++) ok = 0; }
128 CHECK(ok, "permutation of a 2048-row is a bijection"); free(seen);
129 float p8[768]; float x8[768]; for (unsigned i = 0; i < 768; i++) x8[i] = (float)(i + 1);
130 omega_fp8gemm_permute_x(x8, 768, 8, p8); ok = 1; seen = calloc(768, 1);
131 for (unsigned i = 0; i < 768; i++) { double v = (double)p8[i] / 18446744073709551616.0; if (v < 1 || v > 768 || seen[(int)v - 1]++) ok = 0; }
132 CHECK(ok, "permutation of a 768-row (lane 8) is a bijection"); free(seen);
133 /* column c = lane L, word w, byte e of step 0 lands at (w*32 + L)*4 + e: c = 5 * 16 + 6 = 86 -> L 5, j 6 -> w 1, e 2 */
134 CHECK(p[(1 * 32 + 5) * 4 + 2] / 18446744073709551616.0f == x[86], "column 86 sits at lane 5 word 1 element 2"); }
135 struct { uint32_t k, lb, mt, w; } shp[] = { { 2048, 16, 8, 16 }, { 4096, 16, 8, 16 }, { 1024, 16, 8, 16 }, { 768, 8, 8, 16 }, { 768, 8, 8, 8 }, { 2048, 8, 8, 16 }, { 2048, 16, 4, 16 }, { 2048, 16, 8, 32 }, { 2048, 16, 8, 8 }, { 2048, 16, 4, 8 }, { 4096, 16, 4, 8 } };
136 for (unsigned i = 0; i < sizeof shp / sizeof shp[0]; i++) for (int m = 0; m <= 3; m++) {
137 OmegaFp8GemmSpec s = { shp[i].k, shp[i].lb, shp[i].mt, shp[i].w, m, 0 }; uint32_t n = 0, gp = 0;
138 int rc = omega_fp8gemm_prog_stats(&s, &n, &gp);
139 uint32_t decl = ((gp + 1 + 7) / 8) * 8;
140 CHECK(rc == 0 && n <= 1024, "codegen k %u lb %u mt %u warps %u mutant %d: rc %d insns %u gprs %u", s.k, s.lane_bytes, s.mt, s.warps, m, rc, n, gp);
141 if (m == 0 && rc == 0) printf("codegen k %4u lane %2u mt %2u warps %2u: %u instructions, %u registers (declared %u, register file %u of 65536)%s, shared %u B\n", s.k, s.lane_bytes, s.mt, s.warps, n, gp, decl, 32u * s.warps * decl, 32u * s.warps * decl <= 65536u ? "" : " DOES NOT FIT", omega_fp8gemm_shared_bytes(&s));
142 }
143 { OmegaFp8GemmSpec s = { 2048, 16, 16, 16, 0, 0 }; CHECK(omega_fp8gemm_prog_stats(&s, NULL, NULL) != 0, "mt 16 at lane 16 refused (32 KB of shared memory)"); }
144 { OmegaFp8GemmSpec s = { 2048, 16, 6, 16, 0, 0 }; CHECK(omega_fp8gemm_prog_stats(&s, NULL, NULL) != 0, "mt 6 refused"); }
145 { OmegaFp8GemmSpec s = { 2048, 16, 8, 16, 5, 0 }; CHECK(omega_fp8gemm_prog_stats(&s, NULL, NULL) != 0, "mutant 5 refused"); }
146 { OmegaFp8GemmSpec s = { 2048, 16, 8, 16, 0, 2 }; CHECK(omega_fp8gemm_prog_stats(&s, NULL, NULL) != 0, "rowmap 2 refused"); }
147 { OmegaFp8GemmSpec s = { 2048, 16, 8, 16, 0, 1 }; CHECK(omega_fp8gemm_prog_stats(&s, NULL, NULL) != 0, "rowmap at mt 8 refused (registers)"); }
148 { uint32_t ks[] = { 2048, 4096, 512, 768 }; for (unsigned i = 0; i < 4; i++) {
149 OmegaFp8GemmSpec s = { ks[i], ks[i] % 512 ? 8u : 16u, 4, 16, 0, 1 }; uint32_t n = 0, gp = 0;
150 int rc = omega_fp8gemm_prog_stats(&s, &n, &gp);
151 uint32_t decl = ((gp + 1 + 7) / 8) * 8;
152 CHECK(rc == 0 && n <= 1024 && 32u * s.warps * decl <= 65536u, "codegen k %u mt 4 rowmap: rc %d insns %u gprs %u", ks[i], rc, n, gp);
153 printf("codegen k %4u lane %2u mt 4 warps 16 rowmap: %u instructions, %u registers (declared %u, register file %u of 65536)\n", s.k, s.lane_bytes, n, gp, decl, 32u * s.warps * decl);
154 } }
155 CHECK(OMEGA_GEMM_ARGS_WORDS <= OMEGA_GPU_SESSION_MAX_ARGS, "argument words fit the constant bank window");
156 uint32_t ids[3] = { 2, 1, 1 }, tr[3] = { 0, 0, 0 }; float xr[2048] = { 0 }, yo[4];
157 CHECK(omega_gpu_fp8gemm_f32(NULL, 3, ids, tr, xr, 1, yo, NULL) == OMEGA_GPU_FP8GEMM_BAD_ARGS, "a NULL tensor is refused");
158}
159
160/* ------------------------------------------------------------- simulator */
161static uint64_t g_sim_issues;
162typedef struct { uint32_t k, n, lb, mt, w, grid; uint32_t counts[4]; uint32_t nseg; } SimPt;
163static int sim_point(const SimPt *p, int mutant, uint32_t seed, char *msg, size_t msgn, size_t *bad) {
164 static BlackwellIRProgram prog;
165 OmegaFp8GemmSpec spec = { p->k, p->lb, p->mt, p->w, mutant, 0 };
166 if (omega_blackwell_codegen_fp8gemm_prog(&spec, &prog) != 0) { snprintf(msg, msgn, "codegen refused"); return -1; }
167 uint32_t M = 0; for (uint32_t g = 0; g < p->nseg; g++) M += p->counts[g];
168 Weights W; weights_make(&W, p->nseg, p->n, p->k, seed);
169 uint32_t ids[64], trow[64]; { uint32_t i = 0; for (uint32_t g = 0; g < p->nseg; g++) for (uint32_t c = 0; c < p->counts[g]; c++, i++) { ids[i] = g; trow[i] = (i * 5 + 1) % 16; } }
170 float *x = make_x(16, p->k, seed + 1);
171 float *yexp = malloc((size_t)M * p->n * 4);
172 expect_rows(&W, M, ids, trow, x, yexp, p->lb);
173 size_t wbytes = (size_t)p->nseg * p->n * p->k, sc = (size_t)p->nseg * ((p->n + 127) / 128) * (p->k / 128);
174 uint8_t *wd = aligned_alloc(16, (wbytes + 15) & ~15ull); memcpy(wd, W.w, wbytes);
175 float *sd = aligned_alloc(16, ((sc * 4) + 15) & ~15ull); omega_fp8_pack_scales(W.s, sc, sd, 0);
176 float *xp = aligned_alloc(16, ((size_t)(M + p->mt) * p->k * 4 + 15) & ~15ull); memset(xp, 0, (size_t)(M + p->mt) * p->k * 4);
177 float *tmp = malloc((size_t)p->k * 4);
178 for (uint32_t i = 0; i < M; i++) { memcpy(tmp, x + (size_t)trow[i] * p->k, (size_t)p->k * 4); omega_fp8gemm_permute_x(tmp, p->k, p->lb, xp + (size_t)i * p->k); }
179 typedef struct { uint64_t w, s; uint32_t row0, cnt, pad[2]; } Seg;
180 Seg *seg = aligned_alloc(16, 64 * sizeof(Seg)); memset(seg, 0, 64 * sizeof(Seg));
181 { uint32_t r0 = 0; for (uint32_t g = 0; g < p->nseg; g++) { seg[g].w = (uintptr_t)(wd + (size_t)g * p->n * p->k); seg[g].s = (uintptr_t)(sd + (size_t)g * ((p->n + 127) / 128) * (p->k / 128)); seg[g].row0 = r0; seg[g].cnt = p->counts[g]; r0 += p->counts[g]; } }
182 float *y = aligned_alloc(16, ((size_t)M * p->n * 4 + 15) & ~15ull);
183 uint32_t shared = omega_fp8gemm_shared_bytes(&spec);
184 uint8_t *smem = aligned_alloc(16, shared);
185 float *ycopy = NULL; int err = 0;
186 for (int order = 0; order < 2 && !err; order++) {
187 WsSim s; memset(&s, 0, sizeof s);
188 s.p = &prog; s.threads = 32u * p->w; s.order = order; s.smem = smem; s.smem_bytes = shared;
189 for (size_t i = 0; i < (size_t)M * p->n; i++) y[i] = -999.0f;
190 uint64_t A[3] = { (uintptr_t)seg, (uintptr_t)xp, (uintptr_t)y };
191 for (unsigned i = 0; i < 3; i++) { s.cbank[(OMEGA_GEMM_ARG_SEG + 8 * i) / 4] = (uint32_t)A[i]; s.cbank[(OMEGA_GEMM_ARG_SEG + 8 * i) / 4 + 1] = (uint32_t)(A[i] >> 32); }
192 s.cbank[OMEGA_GEMM_ARG_N / 4] = p->n; s.cbank[OMEGA_GEMM_ARG_STRIDE / 4] = p->grid * p->w;
193 s.nranges = 5;
194 s.ranges[0].lo = (uintptr_t)seg; s.ranges[0].hi = (uintptr_t)seg + 64 * sizeof(Seg);
195 s.ranges[1].lo = (uintptr_t)wd; s.ranges[1].hi = (uintptr_t)wd + wbytes;
196 s.ranges[2].lo = (uintptr_t)sd; s.ranges[2].hi = (uintptr_t)sd + sc * 4;
197 s.ranges[3].lo = (uintptr_t)xp; s.ranges[3].hi = (uintptr_t)xp + (size_t)(M + p->mt) * p->k * 4;
198 s.ranges[4].lo = (uintptr_t)y; s.ranges[4].hi = (uintptr_t)y + (size_t)M * p->n * 4;
199 err = ws_run(&s, p->grid, p->nseg);
200 if (err) { snprintf(msg, msgn, "order %d: %s (pc %d)", order, s.msg, s.err_pc); break; }
201 g_sim_issues += s.warp_issues;
202 size_t unwritten = 0; for (size_t i = 0; i < (size_t)M * p->n; i++) if (y[i] == -999.0f) unwritten++;
203 if (unwritten) { err = -1; snprintf(msg, msgn, "%zu output words never written", unwritten); break; }
204 if (order == 0) { ycopy = malloc((size_t)M * p->n * 4); memcpy(ycopy, y, (size_t)M * p->n * 4); }
205 else if (memcmp(ycopy, y, (size_t)M * p->n * 4) != 0) { err = -1; snprintf(msg, msgn, "the fragment order changed the result"); }
206 }
207 if (!err) { size_t b = 0; for (size_t i = 0; i < (size_t)M * p->n; i++) if (memcmp(&yexp[i], &y[i], 4) != 0) b++; *bad = b; snprintf(msg, msgn, "%zu of %zu outputs differ from the emulation%s", b, (size_t)M * p->n, b ? "" : " (bit-identical)"); }
208 free(ycopy); free(wd); free(sd); free(xp); free(tmp); free(seg); free(y); free(smem); free(yexp); free(x); weights_free(&W);
209 return err;
210}
211/* The stroke's forms of the kernel (L312, 2026-10-21): an empty segment (cnt 0) in the table, and rowmap (x' rows through XMAP,
212 * y rows through YMAP). empty_at >= 0 inserts a cnt 0 entry at that table index (grid_y = nseg + 1). With rowmap the x' buffer
213 * holds the 16 permuted SOURCE rows and XMAP[i] = trow[i]; YMAP is a random permutation of the M sorted rows, so the mapped
214 * result must equal the emulation moved through YMAP and differ from the unmoved one. budget: the simulator's step budget. */
215static int sim_point2(const SimPt *p, int mutant, uint32_t seed, int rowmap, int empty_at, uint64_t budget, char *msg, size_t msgn, size_t *bad, int *moved) {
216 static BlackwellIRProgram prog;
217 OmegaFp8GemmSpec spec = { p->k, p->lb, p->mt, p->w, mutant, (uint32_t)rowmap };
218 if (omega_blackwell_codegen_fp8gemm_prog(&spec, &prog) != 0) { snprintf(msg, msgn, "codegen refused"); return -1; }
219 uint32_t M = 0; for (uint32_t g = 0; g < p->nseg; g++) M += p->counts[g];
220 Weights W; weights_make(&W, p->nseg, p->n, p->k, seed);
221 uint32_t ids[64], trow[64]; { uint32_t i = 0; for (uint32_t g = 0; g < p->nseg; g++) for (uint32_t c = 0; c < p->counts[g]; c++, i++) { ids[i] = g; trow[i] = (i * 5 + 1) % 16; } }
222 float *x = make_x(16, p->k, seed + 1);
223 float *yexp = malloc((size_t)M * p->n * 4);
224 expect_rows(&W, M, ids, trow, x, yexp, p->lb);
225 size_t wbytes = (size_t)p->nseg * p->n * p->k, sc = (size_t)p->nseg * ((p->n + 127) / 128) * (p->k / 128);
226 uint8_t *wd = aligned_alloc(16, (wbytes + 15) & ~15ull); memcpy(wd, W.w, wbytes);
227 float *sd = aligned_alloc(16, ((sc * 4) + 15) & ~15ull); omega_fp8_pack_scales(W.s, sc, sd, 0);
228 const uint32_t xrows = rowmap ? 16u : M + p->mt;
229 float *xp = aligned_alloc(16, ((size_t)xrows * p->k * 4 + 15) & ~15ull); memset(xp, 0, (size_t)xrows * p->k * 4);
230 float *tmp = malloc((size_t)p->k * 4);
231 if (rowmap) for (uint32_t r = 0; r < 16; r++) { memcpy(tmp, x + (size_t)r * p->k, (size_t)p->k * 4); omega_fp8gemm_permute_x(tmp, p->k, p->lb, xp + (size_t)r * p->k); }
232 else for (uint32_t i = 0; i < M; i++) { memcpy(tmp, x + (size_t)trow[i] * p->k, (size_t)p->k * 4); omega_fp8gemm_permute_x(tmp, p->k, p->lb, xp + (size_t)i * p->k); }
233 uint32_t *xmap = aligned_alloc(16, 256 * 4), *ymap = aligned_alloc(16, 256 * 4);
234 memset(xmap, 0, 256 * 4); memset(ymap, 0, 256 * 4);
235 { uint32_t s = seed * 7u + 3u; for (uint32_t i = 0; i < M; i++) { xmap[i] = trow[i]; ymap[i] = i; }
236 for (uint32_t i = M; i > 1; i--) { uint32_t j = lcg(&s) % i, t = ymap[i - 1]; ymap[i - 1] = ymap[j]; ymap[j] = t; } }
237 float *yexp2 = malloc((size_t)M * p->n * 4);
238 for (uint32_t i = 0; i < M; i++) memcpy(yexp2 + (size_t)(rowmap ? ymap[i] : i) * p->n, yexp + (size_t)i * p->n, (size_t)p->n * 4);
239 typedef struct { uint64_t w, s; uint32_t row0, cnt, pad[2]; } Seg;
240 Seg *seg = aligned_alloc(16, 64 * sizeof(Seg)); memset(seg, 0, 64 * sizeof(Seg));
241 uint32_t segs = 0;
242 { uint32_t r0 = 0;
243 for (uint32_t g = 0; g < p->nseg; g++) {
244 if ((int)segs == empty_at) { seg[segs].w = (uintptr_t)wd; seg[segs].s = (uintptr_t)sd; seg[segs].row0 = r0; seg[segs].cnt = 0; segs++; }
245 seg[segs].w = (uintptr_t)(wd + (size_t)g * p->n * p->k); seg[segs].s = (uintptr_t)(sd + (size_t)g * ((p->n + 127) / 128) * (p->k / 128));
246 seg[segs].row0 = r0; seg[segs].cnt = p->counts[g]; r0 += p->counts[g]; segs++;
247 }
248 if ((int)segs == empty_at) { seg[segs].w = (uintptr_t)wd; seg[segs].s = (uintptr_t)sd; seg[segs].row0 = r0; seg[segs].cnt = 0; segs++; } }
249 float *y = aligned_alloc(16, ((size_t)M * p->n * 4 + 15) & ~15ull);
250 uint32_t shared = omega_fp8gemm_shared_bytes(&spec);
251 uint8_t *smem = aligned_alloc(16, shared);
252 float *ycopy = NULL; int err = 0;
253 for (int order = 0; order < 2 && !err; order++) {
254 WsSim s; memset(&s, 0, sizeof s);
255 s.p = &prog; s.threads = 32u * p->w; s.order = order; s.smem = smem; s.smem_bytes = shared; s.step_budget = budget;
256 for (size_t i = 0; i < (size_t)M * p->n; i++) y[i] = -999.0f;
257 uint64_t A[3] = { (uintptr_t)seg, (uintptr_t)xp, (uintptr_t)y };
258 for (unsigned i = 0; i < 3; i++) { s.cbank[(OMEGA_GEMM_ARG_SEG + 8 * i) / 4] = (uint32_t)A[i]; s.cbank[(OMEGA_GEMM_ARG_SEG + 8 * i) / 4 + 1] = (uint32_t)(A[i] >> 32); }
259 s.cbank[OMEGA_GEMM_ARG_N / 4] = p->n; s.cbank[OMEGA_GEMM_ARG_STRIDE / 4] = p->grid * p->w;
260 s.cbank[OMEGA_GEMM_ARG_XMAP / 4] = (uint32_t)(uintptr_t)xmap; s.cbank[OMEGA_GEMM_ARG_XMAP / 4 + 1] = (uint32_t)((uintptr_t)xmap >> 32);
261 s.cbank[OMEGA_GEMM_ARG_YMAP / 4] = (uint32_t)(uintptr_t)ymap; s.cbank[OMEGA_GEMM_ARG_YMAP / 4 + 1] = (uint32_t)((uintptr_t)ymap >> 32);
262 s.nranges = 7;
263 s.ranges[0].lo = (uintptr_t)seg; s.ranges[0].hi = (uintptr_t)seg + 64 * sizeof(Seg);
264 s.ranges[1].lo = (uintptr_t)wd; s.ranges[1].hi = (uintptr_t)wd + wbytes;
265 s.ranges[2].lo = (uintptr_t)sd; s.ranges[2].hi = (uintptr_t)sd + sc * 4;
266 s.ranges[3].lo = (uintptr_t)xp; s.ranges[3].hi = (uintptr_t)xp + (size_t)xrows * p->k * 4;
267 s.ranges[4].lo = (uintptr_t)y; s.ranges[4].hi = (uintptr_t)y + (size_t)M * p->n * 4;
268 s.ranges[5].lo = (uintptr_t)xmap; s.ranges[5].hi = (uintptr_t)xmap + (M + p->mt) * 4;
269 s.ranges[6].lo = (uintptr_t)ymap; s.ranges[6].hi = (uintptr_t)ymap + (M + p->mt) * 4;
270 err = ws_run(&s, p->grid, segs);
271 if (err) { snprintf(msg, msgn, "order %d: %s (pc %d)", order, s.msg, s.err_pc); break; }
272 g_sim_issues += s.warp_issues;
273 size_t unwritten = 0; for (size_t i = 0; i < (size_t)M * p->n; i++) if (y[i] == -999.0f) unwritten++;
274 if (unwritten) { err = -1; snprintf(msg, msgn, "%zu output words never written", unwritten); break; }
275 if (order == 0) { ycopy = malloc((size_t)M * p->n * 4); memcpy(ycopy, y, (size_t)M * p->n * 4); }
276 else if (memcmp(ycopy, y, (size_t)M * p->n * 4) != 0) { err = -1; snprintf(msg, msgn, "the fragment order changed the result"); }
277 }
278 if (!err) {
279 size_t b = 0; for (size_t i = 0; i < (size_t)M * p->n; i++) if (memcmp(&yexp2[i], &y[i], 4) != 0) b++;
280 *bad = b; if (moved) *moved = memcmp(yexp, y, (size_t)M * p->n * 4) != 0;
281 snprintf(msg, msgn, "%zu of %zu outputs differ from the emulation%s%s", b, (size_t)M * p->n, b ? "" : " (bit-identical)", rowmap ? (memcmp(yexp, y, (size_t)M * p->n * 4) ? ", rows moved by YMAP" : ", YMAP moved nothing") : "");
282 }
283 free(ycopy); free(wd); free(sd); free(xp); free(tmp); free(seg); free(y); free(smem); free(yexp); free(yexp2); free(x); free(xmap); free(ymap); weights_free(&W);
284 return err;
285}
286
287static void sim_battery(void) {
288 SimPt pts[] = {
289 { 2048, 40, 16, 8, 16, 2, { 1, 5, 9 }, 3 }, /* lane 16, KS 4 (2 iterations), 2 passes in segment 3, row clamping (n 40 < 2 x 32 x 1) */
290 { 4096, 20, 16, 8, 16, 1, { 3, 8 }, 2 }, /* KS 8: 4 iterations */
291 { 1024, 33, 16, 8, 16, 2, { 2, 1 }, 2 }, /* KS 2: one iteration, no tail prefetch */
292 { 768, 130, 8, 8, 16, 2, { 4, 17 }, 2 }, /* lane 8, KS 3 (odd, unrolled), 2 row blocks of scales, 2 passes */
293 { 768, 24, 8, 8, 8, 1, { 1, 8, 2 }, 3 }, /* lane 8 at 8 warps */
294 { 2048, 16, 16, 4, 16, 1, { 6, 4 }, 2 }, /* mt 4 */
295 };
296 for (size_t i = 0; i < sizeof pts / sizeof pts[0]; i++) {
297 char msg[300]; size_t bad = 1;
298 int err = sim_point(&pts[i], 0, 31 + (uint32_t)i, msg, sizeof msg, &bad);
299 CHECK(!err && bad == 0, "sim k %u n %u lane %u mt %u warps %u grid %u: %s", pts[i].k, pts[i].n, pts[i].lb, pts[i].mt, pts[i].w, pts[i].grid, msg);
300 printf("sim k %u n %u lane %u mt %u warps %u grid %u: %s %s\n", pts[i].k, pts[i].n, pts[i].lb, pts[i].mt, pts[i].w, pts[i].grid, err ? "ERROR" : bad ? "MISMATCH" : "ok", msg);
301 }
302 for (int m = 1; m <= 3; m++) {
303 char msg[300]; size_t bad = 0;
304 int err = sim_point(&pts[0], m, 77, msg, sizeof msg, &bad);
305 CHECK(!err && bad != 0, "sim mutant %d must be caught: %s", m, msg);
306 printf("sim mutant %d: %s %s\n", m, err ? "ERROR" : bad ? "caught" : "NOT CAUGHT", msg);
307 }
308 /* the stroke's forms (L312): empty segments and row maps */
309 {
310 char msg[300]; size_t bad = 1; int moved = 0;
311 /* RED first: mutant 4 (the guard dropped) on an empty segment must run out of steps */
312 int err = sim_point2(&pts[5], 4, 99, 0, 1, 2000000ull, msg, sizeof msg, &bad, NULL);
313 /* the runaway pass loop is caught either way: the step budget, or a read past the x' rows (the simulator checks every address) */
314 CHECK(err != 0 && (strstr(msg, "step budget") || strstr(msg, "outside the declared buffers")), "sim mutant 4 (no empty-segment guard) must be caught: %s", msg);
315 printf("sim empty segment, mutant 4 (guard dropped): %s %s\n", err ? "caught" : "NOT CAUGHT", msg);
316 /* GREEN: the guard; an empty segment first, in the middle, last */
317 for (int at = 0; at <= 2; at++) {
318 err = sim_point2(&pts[0], 0, 41 + (uint32_t)at, 0, at + (at == 2 ? 1 : 0), 0, msg, sizeof msg, &bad, NULL);
319 CHECK(!err && bad == 0, "sim empty segment at %d: %s", at, msg);
320 printf("sim k %u empty segment at index %d: %s %s\n", pts[0].k, at + (at == 2 ? 1 : 0), err ? "ERROR" : bad ? "MISMATCH" : "ok", msg);
321 }
322 /* rowmap: x' rows through XMAP (16 source rows), y rows through YMAP (a random permutation); with and without an empty segment */
323 const SimPt rmpts[] = {
324 { 2048, 40, 16, 4, 16, 2, { 1, 5, 9 }, 3 }, /* 3 passes in segment 3 (mt 4), row clamping */
325 { 4096, 20, 16, 4, 16, 1, { 3, 8 }, 2 }, /* KS 8, + an empty segment */
326 { 512, 40, 16, 4, 16, 2, { 1, 5, 9 }, 3 }, /* the down projection: KS 1 (unrolled) */
327 { 768, 24, 8, 4, 8, 1, { 1, 8, 2 }, 3 }, /* lane 8 at 8 warps */
328 { 2048, 16, 16, 4, 16, 1, { 6, 4 }, 2 },
329 };
330 for (size_t i = 0; i < sizeof rmpts / sizeof rmpts[0]; i++) {
331 const SimPt *p = &rmpts[i];
332 err = sim_point2(p, 0, 61 + (uint32_t)i, 1, i == 1 ? 1 : -1, 0, msg, sizeof msg, &bad, &moved);
333 CHECK(!err && bad == 0 && moved, "sim rowmap k %u mt %u%s: %s", p->k, p->mt, i == 1 ? " + empty segment" : "", msg);
334 printf("sim rowmap k %u n %u mt %u warps %u%s: %s %s\n", p->k, p->n, p->mt, p->w, i == 1 ? " + empty segment" : "", err ? "ERROR" : bad ? "MISMATCH" : "ok", msg);
335 }
336 for (int m = 1; m <= 3; m++) {
337 err = sim_point2(&rmpts[0], m, 77, 1, -1, 0, msg, sizeof msg, &bad, NULL);
338 CHECK(!err && bad != 0, "sim rowmap mutant %d must be caught: %s", m, msg);
339 printf("sim rowmap mutant %d: %s %s\n", m, err ? "ERROR" : bad ? "caught" : "NOT CAUGHT", msg);
340 }
341 }
342 printf("sim: %" PRIu64 " warp issues in total\n", g_sim_issues);
343}
344
345/* ------------------------------------------------------------- chip */
346static int g_evict = 1;
347static void evict_host_caches(void) {
348 static uint8_t *buf; const size_t sz = 512u << 20; static uint8_t pass;
349 if (!buf) buf = malloc(sz);
350 if (!buf) return;
351 memset(buf, ++pass, sz);
352 volatile uint64_t sum = 0; for (size_t i = 0; i < sz; i += 64) sum += buf[i];
353}
354static double med(uint64_t *v, uint32_t n) { qsort(v, n, 8, cmp_u64); return (double)v[n / 2]; }
355
356typedef struct { const char *name; uint32_t experts, n, k, tokens, topk; } Shape;
357static FILE *g_out; static int g_first = 1;
358
359static void run_shape(const Shape *S, uint32_t reps, uint32_t seed) {
360 printf("== %s: %u experts, n %u, k %u, %u tokens x top-%u\n", S->name, S->experts, S->n, S->k, S->tokens, S->topk);
361 Weights W; weights_make(&W, S->experts, S->n, S->k, seed);
362 OmegaGpuFp8Tensor *t = NULL;
363 double t0 = now_ms();
364 int rc = omega_gpu_fp8_tensor_upload(S->experts, S->n, S->k, W.w, W.s, &t);
365 printf("upload %.0f MB: rc=%s %.0f ms\n", (double)S->experts * S->n * S->k / 1e6, omega_gpu_fp8_rc_name(rc), now_ms() - t0);
366 CHECK(rc == 0, "%s upload", S->name);
367 if (rc) { weights_free(&W); return; }
368 const uint32_t M = S->tokens * S->topk;
369 uint32_t *ids = malloc(M * 4), *trow = malloc(M * 4);
370 uint32_t *picks = malloc((size_t)M * 4);
371 make_table(S->tokens, S->topk, S->experts, seed + 7, ids, trow, picks);
372 uint32_t xrows = S->experts > 1 ? (S->k == 768 ? M : S->tokens) : S->tokens;
373 if (S->experts > 1 && S->k == 768) for (uint32_t i = 0; i < M; i++) trow[i] = i; /* down: one activation row per routed row */
374 float *x = make_x(xrows, S->k, seed + 3);
375 float *y = malloc((size_t)M * S->n * 4), *yref = malloc((size_t)M * S->n * 4), *y2 = malloc((size_t)M * S->n * 4);
376 OmegaGpuFp8GemmInfo info;
377 rc = omega_gpu_fp8gemm_f32(t, M, ids, trow, x, xrows, y, &info);
378 CHECK(rc == 0, "%s gemm: %s %s", S->name, omega_gpu_fp8gemm_rc_name(rc), rc ? omega_gpu_fp8gemm_last_error() : "");
379 if (rc == 0) {
380 /* reference: the existing GEMV, one call per row */
381 double tr = now_ms(); size_t bad = 0, first = (size_t)-1;
382 for (uint32_t i = 0; i < M; i++) {
383 int r2 = omega_gpu_fp8_gemv_f32(t, ids[i], x + (size_t)trow[i] * S->k, yref + (size_t)i * S->n, NULL);
384 if (r2) { printf("gemv reference failed at row %u: %s\n", i, omega_gpu_fp8_last_error()); bad = (size_t)-1; break; }
385 }
386 if (bad == 0) for (size_t i = 0; i < (size_t)M * S->n; i++) if (memcmp(&y[i], &yref[i], 4) != 0) { if (!bad) first = i; bad++; }
387 CHECK(bad == 0, "%s: %zu of %zu outputs differ from the per-row GEMV (first %zd)", S->name, bad, (size_t)M * S->n, (ssize_t)first);
388 printf("parity vs per-row GEMV (%u calls, %.0f ms): %zu of %zu outputs differ%s\n", M, now_ms() - tr, bad, (size_t)M * S->n, bad ? "" : " (bit-identical)");
389 /* host emulation + oracle on a few rows */
390 size_t eb = 0; double worst = 0; uint32_t lb = info.lane_bytes;
391 for (uint32_t q = 0; q < 4; q++) {
392 uint32_t i = (q * 977u + 3u) % M, r = (q * 331u + 17u) % S->n;
393 float e = emu_row(W.w + ((size_t)ids[i] * S->n + r) * S->k, W.s + ((size_t)ids[i] * ((S->n + 127) / 128) + r / 128) * (S->k / 128), x + (size_t)trow[i] * S->k, S->k, lb);
394 if (memcmp(&e, &y[(size_t)i * S->n + r], 4) != 0) eb++;
395 double yo[128], ys[128]; /* oracle over the 128-row block holding r */
396 uint32_t b0 = (r / 128) * 128;
397 omega_fp8_gemv_oracle(W.w + ((size_t)ids[i] * S->n + b0) * S->k, W.s + ((size_t)ids[i] * ((S->n + 127) / 128) + r / 128) * (S->k / 128), 128, S->k, x + (size_t)trow[i] * S->k, yo, ys);
398 double d = fabs((double)y[(size_t)i * S->n + r] - yo[r - b0]) / (ys[r - b0] + 1e-30); if (d > worst) worst = d;
399 }
400 CHECK(eb == 0 && worst < 1e-5, "%s: host emulation mismatches %zu, oracle worst %.2e", S->name, eb, worst);
401 printf("4 sampled outputs: %zu differ from the host emulation, worst relative error vs the double oracle %.2e\n", eb, worst);
402 rc = omega_gpu_fp8gemm_f32(t, M, ids, trow, x, xrows, y2, NULL);
403 CHECK(rc == 0 && memcmp(y, y2, (size_t)M * S->n * 4) == 0, "%s: second call bit-identical", S->name);
404 printf("second call: %s\n", rc == 0 && memcmp(y, y2, (size_t)M * S->n * 4) == 0 ? "bit-identical" : "DIFFERS");
405 /* mutants */
406 for (int m = 1; m <= 3; m++) {
407 omega_gpu_fp8gemm_test_set_mutant(m);
408 rc = omega_gpu_fp8gemm_f32(t, M, ids, trow, x, xrows, y2, NULL);
409 size_t b = 0; if (rc == 0) for (size_t i = 0; i < (size_t)M * S->n; i++) if (memcmp(&y[i], &y2[i], 4) != 0) b++;
410 if (rc != 0) printf("chip mutant %d: call returned %s: %s\n", m, omega_gpu_fp8gemm_rc_name(rc), omega_gpu_fp8gemm_last_error());
411 int caught = rc == 0 && b != 0;
412 if (m == 2 && info.k / (32u * info.lane_bytes) < 2) caught = 1; /* single-step k: mutant 2 has no effect by construction */
413 CHECK(caught, "%s: chip mutant %d must be caught", S->name, m);
414 printf("chip mutant %d: %s (%zu of %zu outputs differ)\n", m, caught ? "caught" : "NOT CAUGHT", b, (size_t)M * S->n);
415 if (g_out) fprintf(g_out, "%s{\"point\":\"%s mutant %d\",\"caught\":%d}", g_first ? "" : ",", S->name, m, caught), g_first = 0;
416 }
417 omega_gpu_fp8gemm_test_set_mutant(0);
418 /* timing: the new path vs the per-token path (grouped GEMV, 8 experts per token for the MoE shapes; one GEMV per row for dense) */
419 uint64_t *ce = malloc(reps * 8), *cc = malloc(reps * 8); uint32_t done = 0;
420 for (uint32_t i = 0; i < reps; i++) {
421 if (g_evict) evict_host_caches();
422 { int r3 = omega_gpu_fp8gemm_f32(t, M, ids, trow, x, xrows, y2, &info); if (r3 != 0) { printf("timing call %u: %s: %s\n", i, omega_gpu_fp8gemm_rc_name(r3), omega_gpu_fp8gemm_last_error()); break; } }
423 ce[done] = info.elapsed_ns; cc[done] = info.call_ns; done++;
424 }
425 CHECK(done == reps, "%s timing loop completed %u of %u", S->name, done, reps);
426 double mc = done ? med(ce, done) : 0, ma = done ? med(cc, done) : 0;
427 printf("GEMM (medians of %u, evict %d): mt %u warps %u ctas %u x %u segments, %u rows, up to %u passes: chip %.1f us, call %.1f us; streams %.0f MB (distinct experts %.0f MB) = %.1f GB/s on streamed, %.1f GB/s on distinct\n",
428 done, g_evict, info.mt, info.warps_per_cta, info.grid_x, info.segments, M, info.passes_max, mc / 1e3, ma / 1e3, info.streamed_bytes / 1e6, info.weight_bytes / 1e6, info.streamed_bytes / mc, info.weight_bytes / mc);
429 /* per-token baseline: for the MoE shapes one grouped GEMV (topk experts) per token; the sorted table is rebuilt per token */
430 uint64_t *bc = malloc(reps * 8), *bw = malloc(reps * 8); uint32_t bd = 0;
431 for (uint32_t i = 0; i < reps; i++) {
432 if (g_evict) evict_host_caches();
433 uint64_t sum_e = 0, sum_c = 0; int ok = 1;
434 for (uint32_t tk = 0; tk < S->tokens && ok; tk++) {
435 OmegaGpuFp8Info fi; int r2;
436 if (S->experts == 1) r2 = omega_gpu_fp8_gemv_f32(t, 0, x + (size_t)tk * S->k, yref, &fi);
437 else if (S->k == 768) r2 = omega_gpu_fp8_gemv_grouped_f32(t, S->topk, picks + (size_t)tk * S->topk, x + (size_t)tk * S->topk * S->k, S->topk, yref, &fi);
438 else r2 = omega_gpu_fp8_gemv_grouped_f32(t, S->topk, picks + (size_t)tk * S->topk, x + (size_t)tk * S->k, 1, yref, &fi);
439 if (r2) ok = 0; else { sum_e += fi.elapsed_ns; sum_c += fi.call_ns; }
440 }
441 if (!ok) break;
442 bc[bd] = sum_e; bw[bd] = sum_c; bd++;
443 }
444 double mb = bd ? med(bc, bd) : 0, mbw = bd ? med(bw, bd) : 0;
445 if (bd) printf("per-token grouped GEMV baseline (%u tokens, medians of %u): chip %.1f us, call %.1f us => GEMM is %.2fx faster (chip), %.2fx (call)\n", S->tokens, bd, mb / 1e3, mbw / 1e3, mb / mc, mbw / ma);
446 printf("per token: GEMM chip %.1f us/token, call %.1f us/token\n", mc / 1e3 / S->tokens, ma / 1e3 / S->tokens);
447 if (g_out) fprintf(g_out, "%s{\"point\":\"%s timing\",\"rows\":%u,\"segments\":%u,\"mt\":%u,\"warps\":%u,\"ctas\":%u,\"passes_max\":%u,\"reps\":%u,\"chip_median_ns\":%.0f,\"call_median_ns\":%.0f,\"streamed_bytes\":%" PRIu64 ",\"distinct_bytes\":%" PRIu64 ",\"gbps_streamed\":%.2f,\"baseline_chip_median_ns\":%.0f,\"baseline_call_median_ns\":%.0f,\"parity_bad\":%zu}",
448 g_first ? "" : ",", S->name, M, info.segments, info.mt, info.warps_per_cta, info.grid_x, info.passes_max, done, mc, ma, info.streamed_bytes, info.weight_bytes, info.streamed_bytes / mc, mb, mbw, bad), g_first = 0;
449 free(ce); free(cc); free(bc); free(bw);
450 }
451 omega_gpu_fp8_tensor_free(t);
452 free(ids); free(trow); free(picks); free(x); free(y); free(yref); free(y2); weights_free(&W);
453}
454
455int main(int argc, char **argv) {
456 int host = 0, sim = 0; const char *out_path = NULL, *shape = "all"; uint32_t reps = 0, mt = 0, warps = 0, ctas = 0, spin = 2000, tokens = 64;
457 for (int i = 1; i < argc; i++) {
458 if (strcmp(argv[i], "--host-only") == 0) host = 1;
459 else if (strcmp(argv[i], "--sim") == 0) sim = 1;
460 else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i];
461 else if (strcmp(argv[i], "--shape") == 0 && i + 1 < argc) shape = argv[++i];
462 else if (strcmp(argv[i], "--reps") == 0 && i + 1 < argc) reps = (uint32_t)atoi(argv[++i]);
463 else if (strcmp(argv[i], "--mt") == 0 && i + 1 < argc) mt = (uint32_t)atoi(argv[++i]);
464 else if (strcmp(argv[i], "--warps") == 0 && i + 1 < argc) warps = (uint32_t)atoi(argv[++i]);
465 else if (strcmp(argv[i], "--ctas") == 0 && i + 1 < argc) ctas = (uint32_t)atoi(argv[++i]);
466 else if (strcmp(argv[i], "--tokens") == 0 && i + 1 < argc) tokens = (uint32_t)atoi(argv[++i]);
467 else if (strcmp(argv[i], "--spin-us") == 0 && i + 1 < argc) spin = (uint32_t)atoi(argv[++i]);
468 else if (strcmp(argv[i], "--no-evict") == 0) g_evict = 0;
469 else { printf("unknown argument %s\n", argv[i]); return 2; }
470 }
471 if (host) host_only();
472 if (sim) sim_battery();
473 if (!host && !sim) {
474 if (mt || warps) omega_gpu_fp8gemm_set_shape(mt, warps);
475 if (ctas) omega_gpu_fp8gemm_set_cta_budget(ctas);
476 (void)omega_gpu_session_set_spin_us(spin);
477 if (out_path) {
478 /* 0644, not fopen's 0666 & ~umask: a receipt is never world-writable */
479 int fd = open(out_path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
480 g_out = fd >= 0 ? fdopen(fd, "w") : NULL;
481 if (fd >= 0 && !g_out) close(fd);
482 if (g_out) fprintf(g_out, "{\"receipt\":\"OMEGA_GPU_FP8GEMM_V1\",\"reps\":%u,\"evict\":%d,\"tokens\":%u,\"spin_us\":%u,\"points\":[", reps ? reps : 1, g_evict, tokens, omega_gpu_session_spin_us());
483 }
484 Shape shapes[] = { { "gateup", 128, 1536, 2048, tokens, 8 }, { "down", 128, 2048, 768, tokens, 8 }, { "qkv", 1, 5120, 2048, tokens, 1 }, { "o", 1, 2048, 4096, tokens, 1 }, { "d768", 1, 2048, 768, tokens, 1 } };
485 for (unsigned i = 0; i < 5; i++) if ((!strcmp(shape, "all") && i < 4) || !strcmp(shape, shapes[i].name)) run_shape(&shapes[i], reps ? reps : 1, 1000 + i);
486 if (g_out) { fprintf(g_out, "],\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS"); fclose(g_out); }
487 omega_gpu_session_close();
488 }
489 printf("gpu_fp8gemm_test: %d checks, %d failed: %s\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS");
490 return g_failed ? 1 : 0;
491}