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

337 lines · 20872 bytes Raw Blame History
1/* gpu_gemm_test.c -- row C3 of docs/OMEGA.md: matrix multiply of any shape, BF16 and FP8 (e4m3), both layouts of A and B,
2 * on the GB10 tensor cores, checked bit for bit against a host model of the same tensor-core steps (src/omega_gpu_gemm.h).
3 *
4 * --host-only no device: refusals, every kernel (both types, several shapes of tile, the red-check mutants) through our
5 * code generator and encoder, register counts
6 * --sim the real kernels in the warp simulator (tensor-core ops computed with the chip-exact models), small shapes
7 * over every edge case, all four layouts, three tile configurations; the three mutants must be caught
8 * (no flag) the GB10: at least 200 shapes with M, N, K from 1 to 8192, both types, all four layouts. Every output word
9 * must be written; shapes up to 2^27 multiply-adds are compared in full, larger ones on 4096 sampled
10 * elements plus the last row and column. Speed: 2 M N K / kernel time, as a fraction of the measured
11 * tensor-core ceiling (BF16 HMMA 115.36 TFLOPS, evidence OWN30B-T3-TC-PEAK-BW-5faa0f70-20261013T2236Z;
12 * FP8 QMMA 237.52 TFLOPS, L151 as quoted in evidence/L403-roof-20261022). The mutants must be caught.
13 * --quick: 24 shapes, for a first look.
14 * Last line: "C3 gemm <tier>: PASS (<checks> checks)" or "... FAIL (<failed> of <checks>)". */
15#include "omega_gpu_gemm.h"
16#include "omega_gpu_session.h"
17#include "bw_warp_sim_tc.h"
18
19#include <inttypes.h>
20
21static int g_checks, g_failed;
22static void check(int ok, const char *what) {
23 g_checks++;
24 if (!ok) { g_failed++; printf("FAIL: %s\n", what); }
25}
26
27static const HmmaModel HM = { 16, 2, 0, 2, 0 }; /* chip-exact, evidence OWN30B-T3-HMMA-MODEL */
28static const QmmaModel QM = { 32, 26, 0, 2, 0 }; /* chip-exact, evidence OWN30B-L1G-QMMA-PFGEMM-c742aa13 */
29static const char *tname(OmegaGpuGemmType t) { return t == OMEGA_GPU_GEMM_BF16 ? "bf16" : "e4m3"; }
30static const char *lname(unsigned l) { static const char *n[4] = { "A row B row", "A col B row", "A row B col", "A col B col" }; return n[l & 3u]; }
31
32static uint64_t g_rng = 1;
33static uint32_t rnd(void) { g_rng = g_rng * 6364136223846793005ull + 1442695040888963407ull; return (uint32_t)(g_rng >> 33); }
34
35/* inputs: bf16 values 0 or +-(1 + m/128) 2^e, e in -8..8; e4m3 any code except the two NaNs */
36static void fill(OmegaGpuGemmType t, void *p, uint64_t n, uint64_t seed) {
37 g_rng = seed * 0x2545f4914f6cdd1dull + 7;
38 if (t == OMEGA_GPU_GEMM_BF16) {
39 uint16_t *h = p;
40 for (uint64_t i = 0; i < n; i++) {
41 uint32_t r = rnd();
42 h[i] = r % 61u == 0 ? 0 : (uint16_t)(((r >> 8) & 1u) << 15 | (uint32_t)(127 + (int)((r >> 9) % 17u) - 8) << 7 | (r >> 16 & 0x7fu));
43 }
44 } else {
45 uint8_t *b = p;
46 for (uint64_t i = 0; i < n; i++) { uint8_t c = (uint8_t)rnd(); b[i] = (c & 0x7fu) == 0x7fu ? (uint8_t)(c ^ 1u) : c; }
47 }
48}
49
50static float val(OmegaGpuGemmType t, const void *p, uint64_t i) {
51 if (t == OMEGA_GPU_GEMM_BF16) { uint32_t u = (uint32_t)((const uint16_t *)p)[i] << 16; float f; memcpy(&f, &u, 4); return f; }
52 return qm_e4m3(((const uint8_t *)p)[i]);
53}
54
55/* the host model: one tensor-core step per 16 (bf16) or 32 (e4m3) values of k, in increasing k, from +0 */
56static float ref_elem(OmegaGpuGemmType t, unsigned lay, uint32_t M, uint32_t N, uint32_t K, const void *A, const void *B, uint32_t m, uint32_t n) {
57 const uint32_t ks = t == OMEGA_GPU_GEMM_BF16 ? 16u : 32u;
58 float acc = 0.0f;
59 for (uint32_t k0 = 0; k0 < K; k0 += ks) {
60 float av[32], bv[32];
61 for (uint32_t k = 0; k < ks; k++) {
62 uint32_t kk = k0 + k;
63 if (kk >= K) { av[k] = bv[k] = 0.0f; continue; }
64 av[k] = val(t, A, lay & OMEGA_GPU_GEMM_A_COL ? (uint64_t)kk * M + m : (uint64_t)m * K + kk);
65 bv[k] = val(t, B, lay & OMEGA_GPU_GEMM_B_COL ? (uint64_t)n * K + kk : (uint64_t)kk * N + n);
66 }
67 acc = t == OMEGA_GPU_GEMM_BF16 ? hm_dot16(&HM, av, bv, acc) : qm_dot32(&QM, av, bv, acc);
68 }
69 return acc;
70}
71
72static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; }
73
74/* compare: every element when M N K <= 2^27, else 4096 samples + the last row and column; returns the mismatches */
75static uint64_t compare(OmegaGpuGemmType t, unsigned lay, uint32_t M, uint32_t N, uint32_t K, const void *A, const void *B, const float *C,
76 uint64_t *checked, char *first, size_t firstn) {
77 uint64_t bad = 0; *checked = 0; first[0] = 0;
78 #define ONE(mm, nn) do { uint32_t m_ = (mm), n_ = (nn); float w_ = ref_elem(t, lay, M, N, K, A, B, m_, n_), g_ = C[(uint64_t)m_ * N + n_]; (*checked)++; \
79 if (f2u(w_) != f2u(g_)) { if (!bad) snprintf(first, firstn, "C[%u][%u] = %08x, model %08x", m_, n_, f2u(g_), f2u(w_)); bad++; } } while (0)
80 if ((uint64_t)M * N * K <= (1ull << 27)) {
81 for (uint32_t m = 0; m < M; m++) for (uint32_t n = 0; n < N; n++) ONE(m, n);
82 } else {
83 g_rng = (uint64_t)M * 31 + N * 7 + K;
84 for (int i = 0; i < 4096; i++) ONE(rnd() % M, rnd() % N);
85 for (uint32_t n = 0; n < N; n += N > 512 ? N / 512 : 1) ONE(M - 1, n);
86 for (uint32_t m = 0; m < M; m += M > 512 ? M / 512 : 1) ONE(m, N - 1);
87 ONE(M - 1, N - 1);
88 }
89 #undef ONE
90 return bad;
91}
92
93/* ------------------------------------------------------------------ host only */
94
95static void host_only(void) {
96 char w[200];
97 uint16_t a[4] = { 0 }, b[4] = { 0 }; float c[4];
98 check(omega_gpu_gemm(OMEGA_GPU_GEMM_BF16, 0, 0, 1, 1, a, b, c, NULL) == OMEGA_GPU_GEMM_BAD_ARGS, "M = 0 refused");
99 check(omega_gpu_gemm(OMEGA_GPU_GEMM_BF16, 0, 1, 1, 0, a, b, c, NULL) == OMEGA_GPU_GEMM_BAD_ARGS, "K = 0 refused");
100 check(omega_gpu_gemm(OMEGA_GPU_GEMM_BF16, 0, OMEGA_GPU_GEMM_MAX_DIM + 1, 1, 1, a, b, c, NULL) == OMEGA_GPU_GEMM_TOO_LARGE, "M past the maximum refused");
101 check(omega_gpu_gemm(OMEGA_GPU_GEMM_BF16, 4, 1, 1, 1, a, b, c, NULL) == OMEGA_GPU_GEMM_BAD_ARGS, "unknown layout bits refused");
102 check(omega_gpu_gemm((OmegaGpuGemmType)2, 0, 1, 1, 1, a, b, c, NULL) == OMEGA_GPU_GEMM_BAD_ARGS, "unknown type refused");
103 check(omega_gpu_gemm(OMEGA_GPU_GEMM_E4M3, 0, 1, 1, 1, NULL, b, c, NULL) == OMEGA_GPU_GEMM_BAD_ARGS, "NULL A refused");
104 OmegaGemmCfg bad; omega_gpu_gemm_default_cfg(OMEGA_GPU_GEMM_BF16, &bad); bad.rt = 3;
105 check(!omega_gpu_gemm_cfg_ok(&bad), "a tile of 3 row tiles refused");
106
107 /* the model itself is a multiply: within 2^-20 of the exact sum, relative to the sum of the magnitudes */
108 for (int t = 0; t < 2; t++) for (unsigned lay = 0; lay < 4; lay++) {
109 enum { MM = 23, NN = 19, KK = 300 };
110 static uint16_t A[MM * KK], B[KK * NN];
111 fill((OmegaGpuGemmType)t, A, MM * KK, 5 + lay); fill((OmegaGpuGemmType)t, B, KK * NN, 9 + lay);
112 double worst = 0;
113 for (uint32_t m = 0; m < MM; m++) for (uint32_t n = 0; n < NN; n++) {
114 double ex = 0, mag = 0;
115 for (uint32_t k = 0; k < KK; k++) {
116 double p = (double)val((OmegaGpuGemmType)t, A, lay & OMEGA_GPU_GEMM_A_COL ? (uint64_t)k * MM + m : (uint64_t)m * KK + k) *
117 (double)val((OmegaGpuGemmType)t, B, lay & OMEGA_GPU_GEMM_B_COL ? (uint64_t)n * KK + k : (uint64_t)k * NN + n);
118 ex += p; mag += fabs(p);
119 }
120 double d = fabs((double)ref_elem((OmegaGpuGemmType)t, lay, MM, NN, KK, A, B, m, n) - ex) / (mag > 0 ? mag : 1);
121 if (d > worst) worst = d;
122 }
123 snprintf(w, sizeof w, "the %s model is a multiply (%s): worst error %.3g of the magnitude sum", tname((OmegaGpuGemmType)t), lname(lay), worst);
124 check(worst < ldexp(1.0, -20), w);
125 printf("%s\n", w);
126 }
127
128 const OmegaGemmCfg shapes[] = { { 0, 2, 4, 2, 2, 4, 0 }, { 0, 1, 1, 1, 1, 4, 0 }, { 0, 4, 1, 2, 2, 2, 0 }, { 0, 1, 4, 4, 1, 2, 0 }, { 0, 2, 2, 1, 4, 8, 0 } };
129 OmegaGemmCfg big; omega_gpu_gemm_default_cfg(OMEGA_GPU_GEMM_BF16, &big); big.rt = 4; big.ct = 2;
130 check(!omega_gpu_gemm_cfg_ok(&big), "a tile past the register budget (rt 4 ct 2) refused");
131 for (int t = 0; t < 2; t++)
132 for (size_t s = 0; s < sizeof shapes / sizeof shapes[0]; s++)
133 for (int mut = 0; mut <= 3; mut++)
134 for (int k = 0; k < OMEGA_GEMM_K_COUNT; k++) {
135 OmegaGemmCfg c = shapes[s]; c.type = (OmegaGpuGemmType)t; c.mutant = mut;
136 BlackwellIRProgram *p = calloc(1, sizeof *p);
137 int rc = omega_gpu_gemm_codegen(&c, (OmegaGemmKind)k, p);
138 size_t bytes = rc == 0 ? omega_bw_program_code_bytes(p) : 0;
139 uint8_t *code = bytes ? malloc(bytes) : NULL;
140 OmegaBlackwellKernel kk; memset(&kk, 0, sizeof kk);
141 int enc = code ? omega_blackwell_kernel_encode_into(p, code, bytes, &kk) : -1;
142 int np = enc == 0 ? omega_qmma_patch_code(p, code, bytes) : -1;
143 snprintf(w, sizeof w, "%s rt %u ct %u wm %u wn %u kernel %d mutant %d: codegen %d, encode %d, qmma patches %d, %u gprs", tname(c.type),
144 c.rt, c.ct, c.wm, c.wn, k, mut, rc, enc, np, (unsigned)kk.gpr_count);
145 check(rc == 0 && enc == 0 && kk.code_size == bytes && np >= 0 && (t == 1 || np == 0) && omega_bw_gpr_declared(kk.gpr_count) <= 255u, w);
146 if (mut == 0) printf("%s\n", w);
147 free(code); free(p);
148 }
149}
150
151/* ------------------------------------------------------------------ the simulator */
152
153typedef struct {
154 BlackwellIRProgram *prog[OMEGA_GEMM_K_COUNT];
155 WsRange ranges[5];
156 int order;
157 char err[300];
158} SimCtx;
159
160static int sim_launch(void *ctx, OmegaGemmKind k, const uint32_t a[OMEGA_GPU_GEMM_ARGS_WORDS], uint32_t gx, uint32_t gy, uint32_t threads) {
161 SimCtx *c = ctx;
162 static WsSim s;
163 memset(&s, 0, sizeof s);
164 s.p = c->prog[k]; s.threads = threads; s.order = c->order;
165 for (unsigned i = 0; i < OMEGA_GPU_GEMM_ARGS_WORDS; i++) s.cbank[0x380 / 4 + i] = a[i];
166 memcpy(s.ranges, c->ranges, sizeof c->ranges); s.nranges = 5;
167 if (ws_run(&s, gx, gy) != 0) { snprintf(c->err, sizeof c->err, "kernel %d: %s at pc %d", (int)k, s.msg, s.err_pc); return -1; }
168 return 0;
169}
170
171static int sim_build(SimCtx *c, const OmegaGemmCfg *cfg) {
172 for (int k = 0; k < OMEGA_GEMM_K_COUNT; k++) {
173 c->prog[k] = calloc(1, sizeof *c->prog[k]);
174 if (!c->prog[k] || omega_gpu_gemm_codegen(cfg, (OmegaGemmKind)k, c->prog[k]) != 0) return -1;
175 }
176 return 0;
177}
178static void sim_free(SimCtx *c) { for (int k = 0; k < OMEGA_GEMM_K_COUNT; k++) { free(c->prog[k]); c->prog[k] = NULL; } }
179
180/* one multiply through the simulator; 0 when it ran and every output word was written */
181static int sim_gemm(SimCtx *c, const OmegaGemmCfg *cfg, unsigned lay, uint32_t M, uint32_t N, uint32_t K, const void *A, const void *B, float *C) {
182 OmegaGemmPlan p;
183 if (omega_gpu_gemm_plan(cfg, M, N, K, &p) != 0) return -2;
184 uint8_t *ap = malloc(p.ap_bytes), *bp = malloc(p.bp_bytes);
185 memset(ap, 0xa5, p.ap_bytes); memset(bp, 0x5a, p.bp_bytes);
186 for (uint64_t i = 0; i < (uint64_t)M * N; i++) { uint32_t u = 0x7fa00badu; memcpy(&C[i], &u, 4); }
187 c->ranges[0] = (WsRange){ (uintptr_t)A, (uintptr_t)A + p.a_bytes + 1 }; /* the e4m3 pack reads a u16 around the last byte */
188 c->ranges[1] = (WsRange){ (uintptr_t)B, (uintptr_t)B + p.b_bytes + 1 };
189 c->ranges[2] = (WsRange){ (uintptr_t)ap, (uintptr_t)ap + p.ap_bytes };
190 c->ranges[3] = (WsRange){ (uintptr_t)bp, (uintptr_t)bp + p.bp_bytes };
191 c->ranges[4] = (WsRange){ (uintptr_t)C, (uintptr_t)(C + (uint64_t)M * N) };
192 int rc = omega_gpu_gemm_drive(cfg, lay, M, N, K, (uintptr_t)A, (uintptr_t)B, (uintptr_t)ap, (uintptr_t)bp, (uintptr_t)C, sim_launch, c);
193 for (uint64_t i = 0; rc == 0 && i < (uint64_t)M * N; i++) if (f2u(C[i]) == 0x7fa00badu) rc = -9;
194 free(ap); free(bp);
195 return rc;
196}
197
198static void sim_tier(void) {
199 char w[600], first[200];
200 static const uint32_t sh[][3] = { { 1, 1, 1 }, { 1, 1, 17 }, { 16, 8, 16 }, { 17, 9, 33 }, { 64, 64, 64 }, { 65, 63, 100 },
201 { 3, 130, 70 }, { 100, 3, 257 }, { 129, 65, 31 }, { 31, 200, 129 } };
202 const size_t nsh = sizeof sh / sizeof sh[0];
203 const OmegaGemmCfg cfgs[] = { { 0, 2, 4, 2, 2, 4, 0 }, { 0, 1, 1, 1, 1, 4, 0 }, { 0, 4, 1, 1, 4, 2, 0 } };
204 void *A = malloc(300 * 300 * 2), *B = malloc(300 * 300 * 2);
205 float *C = malloc(300 * 300 * 4);
206 for (int t = 0; t < 2; t++)
207 for (size_t ci = 0; ci < sizeof cfgs / sizeof cfgs[0]; ci++) {
208 OmegaGemmCfg cfg = cfgs[ci]; cfg.type = (OmegaGpuGemmType)t;
209 SimCtx c; memset(&c, 0, sizeof c);
210 check(sim_build(&c, &cfg) == 0, "simulator: kernels built");
211 uint64_t runs = 0, wrong = 0;
212 for (size_t s = 0; s < nsh; s++)
213 for (unsigned lay = 0; lay < 4; lay++) {
214 if (ci > 0 && lay != (unsigned)(s % 4)) continue; /* the other tile shapes: one layout per shape */
215 const uint32_t M = sh[s][0], N = sh[s][1], K = sh[s][2];
216 fill(cfg.type, A, (uint64_t)M * K, s * 4 + lay); fill(cfg.type, B, (uint64_t)K * N, s * 4 + lay + 1000);
217 c.order = (int)(s & 1u);
218 int rc = sim_gemm(&c, &cfg, lay, M, N, K, A, B, C);
219 uint64_t checked = 0, bad = rc == 0 ? compare(cfg.type, lay, M, N, K, A, B, C, &checked, first, sizeof first) : 1;
220 runs++;
221 if (rc != 0 || bad) {
222 wrong++;
223 snprintf(w, sizeof w, "sim %s rt %u ct %u wm %u wn %u, %ux%ux%u %s: rc %d, %" PRIu64 " wrong of %" PRIu64 " %s %s", tname(cfg.type), cfg.rt,
224 cfg.ct, cfg.wm, cfg.wn, M, N, K, lname(lay), rc, bad, checked, first, c.err);
225 check(0, w);
226 }
227 }
228 snprintf(w, sizeof w, "simulator %s rt %u ct %u wm %u wn %u: %" PRIu64 " multiplies, %" PRIu64 " wrong", tname(cfg.type), cfg.rt, cfg.ct, cfg.wm, cfg.wn, runs, wrong);
229 check(wrong == 0, w);
230 printf("%s\n", w);
231 sim_free(&c);
232 }
233 /* red checks: each mutant must change a result the real kernels get right */
234 for (int t = 0; t < 2; t++)
235 for (int mut = 1; mut <= 3; mut++) {
236 OmegaGemmCfg cfg; omega_gpu_gemm_default_cfg((OmegaGpuGemmType)t, &cfg); cfg.mutant = mut;
237 SimCtx c; memset(&c, 0, sizeof c);
238 const uint32_t M = 65, N = 63, K = 100;
239 fill(cfg.type, A, M * K, 77); fill(cfg.type, B, K * N, 78);
240 int built = sim_build(&c, &cfg) == 0, rc = built ? sim_gemm(&c, &cfg, 0, M, N, K, A, B, C) : -1;
241 uint64_t checked = 0, bad = rc == 0 ? compare(cfg.type, 0, M, N, K, A, B, C, &checked, first, sizeof first) : 1;
242 snprintf(w, sizeof w, "simulator red check: %s mutant %d is caught (%" PRIu64 " wrong)", tname(cfg.type), mut, bad);
243 check(built && bad > 0, w);
244 printf("%s\n", w);
245 sim_free(&c);
246 }
247 free(A); free(B); free(C);
248}
249
250/* ------------------------------------------------------------------ the chip */
251
252static size_t mem_available(void) {
253 FILE *f = fopen("/proc/meminfo", "r");
254 char line[256]; size_t kb = 0;
255 while (f && fgets(line, sizeof line, f)) if (sscanf(line, "MemAvailable: %zu kB", &kb) == 1) break;
256 if (f) fclose(f);
257 return kb * 1024u;
258}
259
260static const double CEIL_TFLOPS[2] = { 115.36, 237.52 };
261
262static void chip_one(OmegaGpuGemmType t, unsigned lay, uint32_t M, uint32_t N, uint32_t K, void *A, void *B, float *C) {
263 char w[600], first[200];
264 fill(t, A, (uint64_t)M * K, (uint64_t)M * 3 + N * 5 + K * 7 + lay);
265 fill(t, B, (uint64_t)K * N, (uint64_t)M * 11 + N * 13 + K * 17 + lay);
266 OmegaGpuGemmInfo info;
267 int rc = omega_gpu_gemm(t, lay, M, N, K, A, B, C, &info);
268 uint64_t checked = 0, bad = rc == 0 ? compare(t, lay, M, N, K, A, B, C, &checked, first, sizeof first) : 1;
269 double flop = 2.0 * M * N * K, tf = info.main_ns ? flop / (double)info.main_ns / 1e3 : 0, tfp = info.main_ns ? flop / (double)(info.main_ns + info.pack_ns) / 1e3 : 0;
270 snprintf(w, sizeof w, "%s %ux%ux%u %s: %s, %" PRIu64 " checked, %" PRIu64 " wrong%s%s; kernel %.3f ms %.2f TFLOPS = %.4f of ceiling, with packs %.2f TFLOPS = %.4f%s%s",
271 tname(t), M, N, K, lname(lay), omega_gpu_gemm_rc_name(rc), checked, bad, bad && rc == 0 ? ", first " : "", bad && rc == 0 ? first : "",
272 info.main_ns / 1e6, tf, tf / CEIL_TFLOPS[t], tfp, tfp / CEIL_TFLOPS[t], rc ? ": " : "", rc ? omega_gpu_session_last_error() : "");
273 check(rc == 0 && bad == 0, w);
274 if (rc == 0 && bad == 0) printf("ok: %s\n", w);
275}
276
277static void chip_tier(int quick) {
278 char w[200];
279 const size_t need = (size_t)8192 * 8192 * 2 * 2 + (size_t)8192 * 8192 * 4 + ((size_t)8 << 30);
280 if (!quick && mem_available() < need * 2) { printf("NOT_RUN: MemAvailable %zu MiB < %zu MiB\n", mem_available() >> 20, need * 2 >> 20); exit(2); }
281 const uint32_t big = quick ? 1024u : 8192u;
282 void *A = malloc((size_t)big * big * 2), *B = malloc((size_t)big * big * 2);
283 float *C = malloc((size_t)big * big * 4);
284 if (!A || !B || !C) { printf("NOT_RUN: host allocation\n"); exit(2); }
285 /* the shapes: per type and layout, the same list (26 each, 208 in all): edges, odd sizes, skinny, square up to 8192 */
286 static const uint32_t sh[][3] = {
287 { 1, 1, 1 }, { 1, 8192, 1 }, { 8192, 1, 1 }, { 1, 1, 8192 }, { 7, 13, 29 }, { 16, 8, 16 }, { 33, 65, 127 }, { 100, 100, 100 },
288 { 128, 64, 64 }, { 255, 257, 511 }, { 1000, 999, 1001 }, { 1, 4096, 4096 }, { 4096, 1, 4096 }, { 4096, 4096, 1 },
289 { 17, 8192, 3000 }, { 8192, 17, 3000 }, { 1024, 1024, 1024 }, { 2048, 2048, 2048 }, { 4096, 4096, 4096 }, { 3000, 5000, 700 },
290 { 8192, 8192, 64 }, { 64, 8192, 8192 }, { 8192, 64, 8192 }, { 6000, 7000, 8000 }, { 8191, 8191, 8191 }, { 8192, 8192, 8192 } };
291 const size_t nsh = quick ? 12 : sizeof sh / sizeof sh[0];
292 int before = g_failed, shapes = 0;
293 for (int t = 0; t < 2; t++)
294 for (unsigned lay = 0; lay < 4; lay++)
295 for (size_t s = 0; s < nsh; s++) {
296 if (quick && lay != (unsigned)(s % 4)) continue;
297 uint32_t M = sh[s][0] > big ? big : sh[s][0], N = sh[s][1] > big ? big : sh[s][1], K = sh[s][2] > big ? big : sh[s][2];
298 chip_one((OmegaGpuGemmType)t, lay, M, N, K, A, B, C);
299 shapes++;
300 }
301 printf("%d shapes, both types, all four layouts: %s\n", shapes, g_failed == before ? "PASS" : "FAIL");
302 for (int t = 0; t < 2; t++)
303 for (int mut = 1; mut <= 3; mut++) {
304 OmegaGemmCfg cfg; omega_gpu_gemm_default_cfg((OmegaGpuGemmType)t, &cfg); cfg.mutant = mut;
305 omega_gpu_gemm_test_set_cfg((OmegaGpuGemmType)t, &cfg);
306 const uint32_t M = 65, N = 63, K = 100;
307 fill((OmegaGpuGemmType)t, A, M * K, 77); fill((OmegaGpuGemmType)t, B, K * N, 78);
308 int rc = omega_gpu_gemm((OmegaGpuGemmType)t, 0, M, N, K, A, B, C, NULL);
309 char first[200]; uint64_t checked = 0, bad = rc == 0 ? compare((OmegaGpuGemmType)t, 0, M, N, K, A, B, C, &checked, first, sizeof first) : 1;
310 snprintf(w, sizeof w, "chip red check: %s mutant %d is caught (%s, %" PRIu64 " wrong)", tname((OmegaGpuGemmType)t), mut, omega_gpu_gemm_rc_name(rc), bad);
311 check(bad > 0, w);
312 printf("%s\n", w);
313 omega_gpu_gemm_test_set_cfg((OmegaGpuGemmType)t, NULL);
314 }
315 for (int t = 0; t < 2; t++) chip_one((OmegaGpuGemmType)t, 3, 65, 63, 100, A, B, C); /* right again after the red checks */
316 omega_gpu_session_close(); /* the kernels go with the device (close hook); the next call builds them on a new one */
317 printf("after the device closed and opened again:\n");
318 for (int t = 0; t < 2; t++) chip_one((OmegaGpuGemmType)t, 0, 70, 33, 129, A, B, C);
319 free(A); free(B); free(C);
320}
321
322int main(int argc, char **argv) {
323 const char *tier = "chip";
324 int quick = 0;
325 for (int i = 1; i < argc; i++) {
326 if (!strcmp(argv[i], "--host-only")) tier = "host";
327 else if (!strcmp(argv[i], "--sim")) tier = "sim";
328 else if (!strcmp(argv[i], "--quick")) quick = 1;
329 else { fprintf(stderr, "usage: %s [--host-only | --sim | --quick]\n", argv[0]); return 2; }
330 }
331 if (!strcmp(tier, "host")) host_only();
332 else if (!strcmp(tier, "sim")) { host_only(); sim_tier(); }
333 else chip_tier(quick);
334 if (g_failed) printf("C3 gemm %s: FAIL (%d of %d)\n", tier, g_failed, g_checks);
335 else printf("C3 gemm %s: PASS (%d checks)\n", tier, g_checks);
336 return g_failed ? 1 : 0;
337}