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

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