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

393 lines · 24601 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_matmul_r4_receipt: the sealed CTA-budget receipt omega_gpu_matmul_api.h asks for before
18 * OMEGA_GPU_MATMUL_MAX_CTAS moves (omega #473, R4).
19 *
20 * ./gpu_matmul_r4_receipt --host-only reference and comparator self-tests, no chip
21 * ./gpu_matmul_r4_receipt --out receipt.json chip receipt (run under the quiet flag)
22 * [--budgets 64,128,256,512,1024] [--repeats 3] [--threads 12] [--shapes "m,k,n;..."]
23 *
24 * Per shape: B is uploaded once (resident, bf16). The reference is the full m x n product of the same
25 * bf16 inputs in double on the host (threaded), with the accumulation scale (sum of |terms|) per element.
26 * Per budget: the CTA budget is set (kernel cache cleared), then 2 x repeats resident calls alternate
27 * -A, +A, -A, +A, ... (A negated by its sign bit, so the exact answer of each call is the negation of the
28 * previous one). Every element of every call is checked:
29 * - rep 0 of each sign: every element within 1e-5 of the accumulation scale of the reference
30 * (the rule of gpu_matmul_api_test.c e2e_max_rel), and finite;
31 * - later repeats: bit-identical to rep 0 of the same sign;
32 * - I42 (cut 1: grids of 128 CTAs and above lost output with the single-tile kernel): an element still
33 * holding the previous call's bits (nonzero) is stale, i.e. not written by this call. Alternating the
34 * sign makes stale output visible; the budget sequence starts with -A because the previous budget
35 * ended with +A. The device-side sentinel check (OUTPUT_UNCHANGED) runs inside the API as well.
36 * Negative controls: the comparator flags a one-element perturbation and a planted stale element; the
37 * chip mutant kernel (last K step dropped) at the largest budget must be flagged by the full comparison.
38 * Timing: chip = OmegaGpuMatmulInfo.elapsed_ns, call = call_ns; median over all 2 x repeats calls.
39 * The best sealed budget is the one with every shape PASS and the least summed median chip time.
40 */
41#include "omega_gpu_matmul_api.h"
42#include "omega_blackwell_codegen.h"
43#include "omega_blackwell_matmul.h"
44#include "sha256.h"
45#include <inttypes.h>
46#include <pthread.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
50
51#define TOL 1e-5
52#define MAX_BUDGETS 16
53#define NAN_FILL 0x7fc0deadu
54
55static const char *DEFAULT_SHAPES =
56 /* the five training shapes (omega #473), then two I42 shapes on the single-tile kernel (rt = ct = 1):
57 * decode 1 x 2048 x 8192 (grid_x up to the budget) and 7 row tiles (112 rows) */
58 "8192,768,2304;8192,768,768;8192,768,3072;8192,3072,768;1024,768,50304;1,2048,8192;112,768,3072";
59static const uint32_t DEFAULT_BUDGETS[] = { 64, 128, 256, 512, 1024 };
60
61static int g_checks, g_failed;
62#define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0)
63
64static inline double absd(double x) { return x < 0.0 ? -x : x; }
65static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; }
66static uint32_t fbits(float f) { uint32_t u; memcpy(&u, &f, 4); return u; }
67static float bitsf(uint32_t u) { float f; memcpy(&f, &u, 4); return f; }
68static int is_finite(float f) { return (fbits(f) & 0x7f800000u) != 0x7f800000u; }
69
70/* inputs as gpu_matmul_api_test.c fill(): values in [-1, 1] at 1/1000 steps, rounded to bf16 */
71static void fill_bf16(uint16_t *x, size_t n, uint32_t *seed) {
72 for (size_t i = 0; i < n; i++) x[i] = omega_fp32_to_bf16(((float)(lcg(seed) % 2001) - 1000.0f) / 1000.0f);
73}
74
75/* ---- reference: acc[i*n+j] = sum_t a[i,t] b[t,j] in double, sc = max(sum |terms|, 1e-6) ---- */
76typedef struct {
77 uint32_t m, k, n, r0, r1;
78 const double *ad, *btd; /* A m x k, B transposed n x k, both exact bf16 values in double */
79 double *acc, *sc;
80} RefJob;
81
82static void *ref_rows(void *p) {
83 RefJob *J = p;
84 const uint32_t k = J->k, n = J->n;
85 for (uint32_t i = J->r0; i < J->r1; i++) {
86 const double *ar = J->ad + (size_t)i * k;
87 for (uint32_t j = 0; j < n; j++) {
88 const double *br = J->btd + (size_t)j * k;
89 double s0 = 0, s1 = 0, s2 = 0, s3 = 0, q0 = 0, q1 = 0, q2 = 0, q3 = 0;
90 uint32_t t = 0;
91 for (; t + 4 <= k; t += 4) {
92 double p0 = ar[t] * br[t], p1 = ar[t + 1] * br[t + 1], p2 = ar[t + 2] * br[t + 2], p3 = ar[t + 3] * br[t + 3];
93 s0 += p0; s1 += p1; s2 += p2; s3 += p3;
94 q0 += absd(p0); q1 += absd(p1); q2 += absd(p2); q3 += absd(p3);
95 }
96 for (; t < k; t++) { double p0 = ar[t] * br[t]; s0 += p0; q0 += absd(p0); }
97 const double q = (q0 + q1) + (q2 + q3);
98 J->acc[(size_t)i * n + j] = (s0 + s1) + (s2 + s3);
99 J->sc[(size_t)i * n + j] = q > 1e-6 ? q : 1e-6;
100 }
101 }
102 return NULL;
103}
104
105static int reference(uint32_t m, uint32_t k, uint32_t n, const uint16_t *a, const uint16_t *b, unsigned threads, double *acc, double *sc) {
106 double *ad = malloc((size_t)m * k * sizeof *ad), *btd = malloc((size_t)n * k * sizeof *btd);
107 if (!ad || !btd) { free(ad); free(btd); return -1; }
108 for (size_t i = 0; i < (size_t)m * k; i++) ad[i] = omega_bf16_to_fp32(a[i]);
109 for (uint32_t t = 0; t < k; t++)
110 for (uint32_t j = 0; j < n; j++) btd[(size_t)j * k + t] = omega_bf16_to_fp32(b[(size_t)t * n + j]);
111 if (threads < 1) threads = 1;
112 if (threads > 64) threads = 64;
113 if (threads > m) threads = m;
114 pthread_t th[64]; RefJob J[64];
115 int rc = 0;
116 for (unsigned w = 0; w < threads; w++) {
117 J[w] = (RefJob){ m, k, n, (uint32_t)((uint64_t)m * w / threads), (uint32_t)((uint64_t)m * (w + 1) / threads), ad, btd, acc, sc };
118 if (pthread_create(&th[w], NULL, ref_rows, &J[w]) != 0) { ref_rows(&J[w]); th[w] = 0; }
119 }
120 for (unsigned w = 0; w < threads; w++) if (th[w]) pthread_join(th[w], NULL);
121 free(ad); free(btd);
122 return rc;
123}
124
125/* ---- full comparison of one output against sign * reference ---- */
126typedef struct { size_t mismatches, nonfinite; double max_rel; } Cmp;
127
128static Cmp compare_full(size_t cnt, const float *c, const double *acc, const double *sc, double sign) {
129 Cmp r = { 0, 0, 0.0 };
130 for (size_t i = 0; i < cnt; i++) {
131 if (!is_finite(c[i])) { r.nonfinite++; r.mismatches++; continue; }
132 const double rel = absd((double)c[i] - sign * acc[i]) / sc[i];
133 if (rel > r.max_rel) r.max_rel = rel;
134 if (rel > TOL) r.mismatches++;
135 }
136 return r;
137}
138
139static size_t count_bit_diff(size_t cnt, const float *x, const float *y) {
140 size_t d = 0;
141 for (size_t i = 0; i < cnt; i++) d += fbits(x[i]) != fbits(y[i]);
142 return d;
143}
144
145/* elements still holding the previous call's bits (the exact answer flipped sign, so only an element
146 * whose answer is exactly zero may legitimately repeat; those are excluded) */
147static size_t count_stale(size_t cnt, const float *cur, const float *prev) {
148 size_t s = 0;
149 for (size_t i = 0; i < cnt; i++) s += fbits(cur[i]) == fbits(prev[i]) && (fbits(cur[i]) & 0x7fffffffu) != 0;
150 return s;
151}
152
153static size_t count_not_negation(size_t cnt, const float *pos, const float *neg) {
154 size_t d = 0;
155 for (size_t i = 0; i < cnt; i++) d += fbits(pos[i]) != (fbits(neg[i]) ^ 0x80000000u);
156 return d;
157}
158
159/* sha256 of the output bytes (first 16 hex digits), so separate runs can be compared bit for bit */
160static void digest16(const float *c, size_t cnt, char hex[17]) {
161 uint8_t d[SHA256_DIGEST_SIZE];
162 sha256_hash((const uint8_t *)c, cnt * sizeof *c, d);
163 for (int i = 0; i < 8; i++) snprintf(hex + 2 * i, 3, "%02x", d[i]);
164}
165
166static int cmp_u64(const void *x, const void *y) { uint64_t a = *(const uint64_t *)x, b = *(const uint64_t *)y; return a < b ? -1 : a > b; }
167static uint64_t median_u64(uint64_t *v, size_t n) { qsort(v, n, sizeof *v, cmp_u64); return n ? v[n / 2] : 0; }
168
169/* comparator self-test on data the caller has (exact reference rounded to f32 is the "chip" output) */
170static void comparator_controls(uint32_t m, uint32_t n, const double *acc, const double *sc, const char *tag, FILE *out) {
171 const size_t cnt = (size_t)m * n;
172 float *c = malloc(cnt * sizeof *c), *prev = malloc(cnt * sizeof *prev);
173 if (!c || !prev) { CHECK(0, "%s comparator controls: host memory", tag); free(c); free(prev); return; }
174 for (size_t i = 0; i < cnt; i++) { c[i] = (float)acc[i]; prev[i] = -(float)acc[i]; }
175 Cmp clean = compare_full(cnt, c, acc, sc, 1.0);
176 const size_t at = cnt / 2 + 3;
177 c[at] = (float)(acc[at] + 4.0 * TOL * sc[at]);
178 Cmp pert = compare_full(cnt, c, acc, sc, 1.0);
179 c[at] = (float)acc[at];
180 const size_t at2 = cnt - 1;
181 float keep = c[at2];
182 if (acc[at2] == 0.0) { c[at2] = 1.0f; prev[at2] = 1.0f; } else c[at2] = prev[at2];
183 const size_t stale = count_stale(cnt, c, prev);
184 c[at2] = keep;
185 c[0] = bitsf(NAN_FILL);
186 Cmp nan = compare_full(cnt, c, acc, sc, 1.0);
187 CHECK(clean.mismatches == 0, "%s control: exact reference must compare clean (%zu)", tag, clean.mismatches);
188 CHECK(pert.mismatches == 1, "%s control: one perturbed element must be flagged (%zu)", tag, pert.mismatches);
189 CHECK(stale == 1, "%s control: one planted stale element must be flagged (%zu)", tag, stale);
190 CHECK(nan.mismatches == 1 && nan.nonfinite == 1, "%s control: one unwritten (NaN) element must be flagged (%zu)", tag, nan.mismatches);
191 printf("%s comparator controls: clean=%zu perturbed=%zu stale=%zu unwritten=%zu\n", tag, clean.mismatches, pert.mismatches, stale, nan.mismatches);
192 if (out) fprintf(out, "\"comparator_control\":{\"shape\":\"%s\",\"clean_mismatches\":%zu,\"perturbed_flagged\":%zu,\"stale_flagged\":%zu,\"unwritten_flagged\":%zu},",
193 tag, clean.mismatches, pert.mismatches, stale, nan.mismatches);
194 free(c); free(prev);
195}
196
197static int host_only(unsigned threads) {
198 /* threaded reference against a naive single-thread double loop, and the comparator controls */
199 const uint32_t m = 37, k = 80, n = 52;
200 uint16_t *a = malloc((size_t)m * k * 2), *b = malloc((size_t)k * n * 2);
201 double *acc = malloc((size_t)m * n * 8), *sc = malloc((size_t)m * n * 8);
202 uint32_t seed = 4730u;
203 fill_bf16(a, (size_t)m * k, &seed); fill_bf16(b, (size_t)k * n, &seed);
204 CHECK(reference(m, k, n, a, b, threads, acc, sc) == 0, "reference ran");
205 double worst = 0.0;
206 for (uint32_t i = 0; i < m; i++)
207 for (uint32_t j = 0; j < n; j++) {
208 double s = 0, q = 0;
209 for (uint32_t t = 0; t < k; t++) { double p = (double)omega_bf16_to_fp32(a[i * k + t]) * omega_bf16_to_fp32(b[t * n + j]); s += p; q += absd(p); }
210 double d = absd(s - acc[i * n + j]) / (q > 1e-6 ? q : 1e-6);
211 if (d > worst) worst = d;
212 if (absd(q - sc[i * n + j]) > 1e-12 * (q > 1.0 ? q : 1.0)) worst = 1.0;
213 }
214 CHECK(worst < 1e-12, "threaded reference equals the naive loop (worst rel %g)", worst);
215 comparator_controls(m, n, acc, sc, "host 37x80x52", NULL);
216 /* sign-bit negation of bf16 A negates the reference exactly */
217 for (size_t i = 0; i < (size_t)m * k; i++) a[i] ^= 0x8000u;
218 double *acc2 = malloc((size_t)m * n * 8), *sc2 = malloc((size_t)m * n * 8);
219 reference(m, k, n, a, b, threads, acc2, sc2);
220 size_t notneg = 0;
221 for (size_t i = 0; i < (size_t)m * n; i++) notneg += acc2[i] != -acc[i] || sc2[i] != sc[i];
222 CHECK(notneg == 0, "negated A gives the exact negated reference (%zu differ)", notneg);
223 free(a); free(b); free(acc); free(sc); free(acc2); free(sc2);
224 printf("%s: %d checks, %d failed\n", g_failed ? "FAIL" : "PASS", g_checks, g_failed);
225 return g_failed ? 1 : 0;
226}
227
228typedef struct { uint64_t chip_med_ns; int pass; } Cell;
229
230int main(int argc, char **argv) {
231 const char *out_path = NULL, *shapes = DEFAULT_SHAPES;
232 uint32_t budgets[MAX_BUDGETS]; unsigned nb = 0;
233 unsigned repeats = 3, threads = 12;
234 int host = 0;
235 for (int i = 1; i < argc; i++) {
236 if (strcmp(argv[i], "--host-only") == 0) host = 1;
237 else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i];
238 else if (strcmp(argv[i], "--shapes") == 0 && i + 1 < argc) shapes = argv[++i];
239 else if (strcmp(argv[i], "--repeats") == 0 && i + 1 < argc) repeats = (unsigned)strtoul(argv[++i], NULL, 10);
240 else if (strcmp(argv[i], "--threads") == 0 && i + 1 < argc) threads = (unsigned)strtoul(argv[++i], NULL, 10);
241 else if (strcmp(argv[i], "--budgets") == 0 && i + 1 < argc) {
242 char *s = argv[++i];
243 while (*s && nb < MAX_BUDGETS) { budgets[nb++] = (uint32_t)strtoul(s, &s, 10); if (*s == ',') s++; else break; }
244 } else { fprintf(stderr, "unknown argument %s\n", argv[i]); return 2; }
245 }
246 if (host) return host_only(threads);
247 if (!nb) { nb = sizeof DEFAULT_BUDGETS / sizeof DEFAULT_BUDGETS[0]; memcpy(budgets, DEFAULT_BUDGETS, sizeof DEFAULT_BUDGETS); }
248 if (repeats < 1) repeats = 1;
249 if (repeats > 32) repeats = 32; /* 2 x repeats calls fit the per-budget timing arrays */
250 FILE *out = out_path ? fopen(out_path, "w") : NULL;
251 if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; }
252 if (out) {
253 fprintf(out, "{\"schema\":\"OMEGA_GPU_MATMUL_R4_RECEIPT_V1\",\"default_budget\":%u,\"repeats\":%u,\"tol_rel_scale\":%g,\"budgets\":[", OMEGA_GPU_MATMUL_MAX_CTAS, repeats, TOL);
254 for (unsigned b = 0; b < nb; b++) fprintf(out, "%s%u", b ? "," : "", budgets[b]);
255 fprintf(out, "],");
256 }
257 Cell cells[32][MAX_BUDGETS]; memset(cells, 0, sizeof cells);
258 char shape_names[32][40]; unsigned ns = 0;
259 int controls_done = 0, mutant_done = 0;
260 if (out) fprintf(out, "\"runs\":[");
261 int first_run = 1;
262 char *copy = strdup(shapes), *save = NULL;
263 for (char *tok = strtok_r(copy, ";", &save); tok && ns < 32 && !omega_gpu_matmul_is_blocked(); tok = strtok_r(NULL, ";", &save)) {
264 unsigned m, k, n;
265 if (sscanf(tok, "%u,%u,%u", &m, &k, &n) != 3) { CHECK(0, "bad shape %s", tok); continue; }
266 snprintf(shape_names[ns], sizeof shape_names[ns], "%ux%ux%u", m, k, n);
267 const size_t na = (size_t)m * k, nbb = (size_t)k * n, cnt = (size_t)m * n;
268 uint16_t *a = malloc(na * 2), *an = malloc(na * 2), *b = malloc(nbb * 2);
269 double *acc = malloc(cnt * 8), *sc = malloc(cnt * 8);
270 float *pos0 = malloc(cnt * 4), *neg0 = malloc(cnt * 4), *tmp[2] = { malloc(cnt * 4), malloc(cnt * 4) }, *base_pos = malloc(cnt * 4);
271 if (!a || !an || !b || !acc || !sc || !pos0 || !neg0 || !tmp[0] || !tmp[1] || !base_pos) { CHECK(0, "host memory for %s", tok); break; }
272 uint32_t seed = 0x5eed0000u ^ (m << 20) ^ (k << 10) ^ n;
273 fill_bf16(a, na, &seed); fill_bf16(b, nbb, &seed);
274 for (size_t i = 0; i < na; i++) an[i] = a[i] ^ 0x8000u;
275 uint32_t rt, ct; omega_matmul_frag_block_shape((m + 15u) / 16u, (n + 7u) / 8u, &rt, &ct);
276 const double flop = 2.0 * m * k * n;
277 if (reference(m, k, n, a, b, threads, acc, sc) != 0) { CHECK(0, "reference %s: host memory", tok); break; }
278 if (!controls_done && m == 8192 && n == 768 && k == 768) {
279 if (out) { fprintf(out, "%s{\"control\":true,", first_run ? "" : ","); first_run = 0; }
280 comparator_controls(m, n, acc, sc, shape_names[ns], out);
281 if (out) fprintf(out, "\"pass\":%s}", g_failed ? "false" : "true");
282 controls_done = 1;
283 }
284 OmegaGpuTensor *t = NULL;
285 int urc = omega_gpu_tensor_upload_bf16(k, n, b, &t);
286 CHECK(urc == OMEGA_GPU_MATMUL_OK, "upload %s rc=%s (%s)", tok, omega_gpu_matmul_rc_name(urc), omega_gpu_matmul_last_error());
287 if (urc != OMEGA_GPU_MATMUL_OK) break;
288 float *prev = NULL;
289 for (unsigned bi = 0; bi < nb; bi++) {
290 omega_gpu_matmul_set_cta_budget(budgets[bi]);
291 const unsigned calls = 2u * repeats;
292 uint64_t chip[64], call[64]; int rcs_ok = 1, hit_after_first = 1;
293 size_t rep_diff = 0, stale_total = 0, stale_max = 0;
294 Cmp cp = { 0, 0, 0 }, cn = { 0, 0, 0 };
295 OmegaGpuMatmulInfo info; memset(&info, 0, sizeof info);
296 for (unsigned ci = 0; ci < calls && ci < 64; ci++) {
297 const int neg = (ci % 2u) == 0, rep = (int)(ci / 2u);
298 float *dst = rep == 0 ? (neg ? neg0 : pos0) : tmp[ci % 2u];
299 for (size_t i = 0; i < cnt; i++) dst[i] = bitsf(NAN_FILL);
300 int rc = omega_gpu_matmul_resident_bf16(m, neg ? an : a, t, dst, &info);
301 chip[ci] = info.elapsed_ns; call[ci] = info.call_ns;
302 if (rc != OMEGA_GPU_MATMUL_OK) {
303 rcs_ok = 0;
304 CHECK(0, "%s budget %u call %u rc=%s (%s)", tok, budgets[bi], ci, omega_gpu_matmul_rc_name(rc), omega_gpu_matmul_last_error());
305 break;
306 }
307 if (ci > 0 && !info.kernel_cache_hit) hit_after_first = 0;
308 if (prev) { size_t s = count_stale(cnt, dst, prev); stale_total += s; if (s > stale_max) stale_max = s; }
309 if (rep == 0) {
310 if (neg) cn = compare_full(cnt, dst, acc, sc, -1.0); else cp = compare_full(cnt, dst, acc, sc, 1.0);
311 } else rep_diff += count_bit_diff(cnt, dst, neg ? neg0 : pos0);
312 prev = dst;
313 }
314 const unsigned done = rcs_ok ? calls : 0;
315 uint64_t chip_s[64], call_s[64];
316 memcpy(chip_s, chip, sizeof chip); memcpy(call_s, call, sizeof call);
317 const uint64_t chip_med = done ? median_u64(chip_s, done) : 0, call_med = done ? median_u64(call_s, done) : 0;
318 const size_t neg_exact = rcs_ok ? count_not_negation(cnt, pos0, neg0) : cnt;
319 size_t cross = 0;
320 if (rcs_ok) { if (bi == 0) memcpy(base_pos, pos0, cnt * 4); else cross = count_bit_diff(cnt, pos0, base_pos); }
321 char dig[17] = "-";
322 if (rcs_ok) digest16(pos0, cnt, dig);
323 const uint32_t ctas = info.grid_x * info.grid_y;
324 const int pass = rcs_ok && cp.mismatches == 0 && cn.mismatches == 0 && rep_diff == 0 && stale_total == 0;
325 CHECK(pass, "%s budget %u: rc_ok=%d pos_mism=%zu neg_mism=%zu repeat_diff=%zu stale=%zu", tok, budgets[bi], rcs_ok, cp.mismatches, cn.mismatches, rep_diff, stale_total);
326 cells[ns][bi].pass = pass; cells[ns][bi].chip_med_ns = chip_med;
327 const double tflops = chip_med ? flop / (double)chip_med / 1e3 : 0.0;
328 printf("%s %s budget=%u grid=%ux%u ctas=%u launches=%u rt=%u ct=%u i42_regime=%d chip_med_ms=%.3f call_med_ms=%.3f tflops_chip=%.3f "
329 "elements=%zu pos_mism=%zu neg_mism=%zu max_rel=%.3g repeat_bitdiff=%zu stale=%zu neg_exact_diff=%zu vs_first_budget_bitdiff=%zu sha=%s cache_hit=%d\n",
330 pass ? "PASS" : "FAIL", tok, budgets[bi], info.grid_x, info.grid_y, ctas, info.chip_calls, rt, ct, ctas >= 128,
331 chip_med / 1e6, call_med / 1e6, tflops, cnt, cp.mismatches, cn.mismatches, cp.max_rel > cn.max_rel ? cp.max_rel : cn.max_rel,
332 rep_diff, stale_total, neg_exact, cross, dig, hit_after_first);
333 if (out) {
334 fprintf(out, "%s{\"m\":%u,\"k\":%u,\"n\":%u,\"budget\":%u,\"pass\":%s,\"grid_x\":%u,\"grid_y\":%u,\"ctas_per_launch\":%u,\"launches\":%u,"
335 "\"rt\":%u,\"ct\":%u,\"single_tile_kernel\":%s,\"i42_regime\":%s,\"calls\":%u,\"elements_per_call\":%zu,"
336 "\"pos_mismatches\":%zu,\"neg_mismatches\":%zu,\"nonfinite\":%zu,\"max_rel\":%.4g,\"repeat_bitdiff\":%zu,"
337 "\"stale_elements\":%zu,\"stale_max_per_call\":%zu,\"neg_exact_negation_diff\":%zu,\"vs_first_budget_bitdiff\":%zu,"
338 "\"pos_output_sha256_16\":\"%s\",\"cache_hit_after_first\":%s,\"chip_median_ns\":%" PRIu64 ",\"call_median_ns\":%" PRIu64 ",\"tflops_chip\":%.4f,\"chip_ns\":[",
339 first_run ? "" : ",", m, k, n, budgets[bi], pass ? "true" : "false", info.grid_x, info.grid_y, ctas, info.chip_calls,
340 rt, ct, rt == 1 && ct == 1 ? "true" : "false", ctas >= 128 ? "true" : "false", done, cnt,
341 cp.mismatches, cn.mismatches, cp.nonfinite + cn.nonfinite, cp.max_rel > cn.max_rel ? cp.max_rel : cn.max_rel, rep_diff,
342 stale_total, stale_max, neg_exact, cross, dig, hit_after_first ? "true" : "false", chip_med, call_med, tflops);
343 for (unsigned ci = 0; ci < done; ci++) fprintf(out, "%s%" PRIu64, ci ? "," : "", chip[ci]);
344 fprintf(out, "],\"call_ns\":[");
345 for (unsigned ci = 0; ci < done; ci++) fprintf(out, "%s%" PRIu64, ci ? "," : "", call[ci]);
346 fprintf(out, "]}");
347 first_run = 0;
348 }
349 fflush(stdout);
350 if (omega_gpu_matmul_is_blocked()) break;
351 }
352 /* negative control on the chip: the mutant kernel at the largest budget must be flagged */
353 if (!mutant_done && m == 8192 && k == 768 && n == 768 && !omega_gpu_matmul_is_blocked()) {
354 omega_gpu_matmul_set_cta_budget(budgets[nb - 1]);
355 omega_gpu_matmul_test_set_mutant(1);
356 OmegaGpuMatmulInfo info;
357 int rc = omega_gpu_matmul_resident_bf16(m, a, t, pos0, &info);
358 Cmp mc = rc == OMEGA_GPU_MATMUL_OK ? compare_full(cnt, pos0, acc, sc, 1.0) : (Cmp){ 0, 0, 0 };
359 omega_gpu_matmul_test_set_mutant(0);
360 const int caught = rc != OMEGA_GPU_MATMUL_OK || mc.mismatches > 0;
361 CHECK(caught, "mutant at budget %u NOT caught by the full comparison", budgets[nb - 1]);
362 printf("%s mutant control %s budget=%u rc=%s flagged=%zu of %zu\n", caught ? "KILLED" : "SURVIVED", tok, budgets[nb - 1], omega_gpu_matmul_rc_name(rc), mc.mismatches, cnt);
363 if (out) { fprintf(out, ",{\"mutant_control\":true,\"m\":%u,\"k\":%u,\"n\":%u,\"budget\":%u,\"rc\":\"%s\",\"flagged\":%zu,\"elements\":%zu,\"killed\":%s}",
364 m, k, n, budgets[nb - 1], omega_gpu_matmul_rc_name(rc), mc.mismatches, cnt, caught ? "true" : "false"); }
365 mutant_done = 1;
366 }
367 omega_gpu_tensor_free(t);
368 free(a); free(an); free(b); free(acc); free(sc); free(pos0); free(neg0); free(tmp[0]); free(tmp[1]); free(base_pos);
369 ns++;
370 }
371 free(copy);
372 omega_gpu_matmul_set_cta_budget(0);
373 CHECK(controls_done && mutant_done, "negative controls ran (comparator %d, mutant %d)", controls_done, mutant_done);
374 /* best sealed budget: every shape PASS, least summed median chip time over every shape in the run (the default
375 * list is the five training shapes plus 1x2048x8192 and 112x768x3072) */
376 int best = -1; uint64_t best_sum = 0;
377 if (out) fprintf(out, "],\"budget_summary\":[");
378 for (unsigned bi = 0; bi < nb; bi++) {
379 int all = ns > 0; uint64_t sum = 0;
380 for (unsigned s = 0; s < ns; s++) { all &= cells[s][bi].pass; sum += cells[s][bi].chip_med_ns; }
381 printf("budget %u: %s, summed median chip %.3f ms over %u shapes\n", budgets[bi], all ? "SEALED" : "NOT SEALED", sum / 1e6, ns);
382 if (out) fprintf(out, "%s{\"budget\":%u,\"sealed\":%s,\"sum_chip_median_ns\":%" PRIu64 "}", bi ? "," : "", budgets[bi], all ? "true" : "false", sum);
383 if (all && (best < 0 || sum < best_sum)) { best = (int)bi; best_sum = sum; }
384 }
385 const int ok = g_failed == 0 && best >= 0 && !omega_gpu_matmul_is_blocked();
386 printf("best sealed budget: %d\n%s: %d checks, %d failed\n", best >= 0 ? (int)budgets[best] : -1, ok ? "PASS" : "FAIL", g_checks, g_failed);
387 if (out) {
388 fprintf(out, "],\"best_sealed_budget\":%d,\"blocked\":%s,\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", best >= 0 ? (int)budgets[best] : -1,
389 omega_gpu_matmul_is_blocked() ? "true" : "false", g_checks, g_failed, ok ? "OMEGA_GPU_MATMUL_R4_SEALED" : "OMEGA_GPU_MATMUL_R4_FAIL");
390 fclose(out);
391 }
392 return ok ? 0 : 1;
393}