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

986 lines · 62514 bytes Raw Blame History
1/* gpu_gemm_ex_test.c -- the general matrix multiply omega_gpu_gemm_ex (src/omega_gpu_gemm_ex.h): any M, N, K >= 1, op N/T,
2 * leading dimensions, alpha/beta, strided batches, f32/bf16/f16/e4m3/e5m2 inputs, f32/bf16/f16 output, fp8 scales.
3 *
4 * --host-only no device: the refusals, then every route through a host model of the core (the chip-exact tensor-core
5 * models of tests/bw_hmma_model.h / bw_qmma_model.h, sequential fmaf for the FFMA reference) on a seeded
6 * case list plus the edge cases; the four planted mutants of the general path must go red; the fp8 block-rows
7 * fallback (omega_gpu_gemm_ex_fp8_block_rows) on the shapes the fp8 kernels refuse, mutant 5 red
8 * --sim the same with the real C3 kernels (bf16, f16, e4m3 HMMA/QMMA and the FFMA reference) in the warp
9 * simulator on small cases, bit for bit against the host-model core; the FFMA and f16 kernel mutants red
10 * (no flag) the GB10: the predeclared sweep (--list prints it; --quick: the first 40; --part I/N: every N-th case
11 * from I), the subnormal probes, the edges, the mutants, the fp8 fallback through the real fp8 GEMV and
12 * grouped GEMM APIs (refused-before shapes answer, served shapes stay on the kernels), chip core == model
13 * core bit for bit where the model is cheap enough
14 *
15 * The check of every output element (predeclared, printed by --list): the double reference
16 * ref = alpha s * sum_k a b f + beta c_old (s: tensor scales, f: block scales)
17 * and |got - ref| <= |alpha s| (((1 + c)^(S + 4) - 1) sum|a b f| + 2^-124 sum (|a| + |b|) |f| + k 2^-140 + F sum|a b f|)
18 * + 2^-50 |beta c_old| + u_c |ref| + t_c
19 * c the per-step constant of step_c(): HMMA (17 2^-25 + 2^-23)(1 + 2^-20), QMMA (33 2^-25 + 2^-23)(1 + 2^-20);
20 * with S the fp32 accumulation steps (ceil(k / 16), ceil(k / 32) for e4m3, 9 ceil(k / 16) for bf16x3, k for FFMA, plus the
21 * number of k parts), F = 2^-7 for FAST_BF16 (both operands rounded to bf16) else 0, u_c / t_c = 2^-24 / 2^-149 (f32),
22 * 2^-8 / 2^-133 (bf16), 2^-11 / 2^-24 (f16). A non-finite ref must come back as the same class (NaN, or inf of the same
23 * sign); an inf result is accepted for a finite ref only when |ref| + bound reaches the largest finite output.
24 * Untouched C (padding between ldc and n, gaps between batches) must keep its bytes. With beta = 0 C is preset to NaN.
25 * Last line: "gemm_ex <tier>: PASS (<checks> checks)" or "... FAIL (<failed> of <checks>)". */
26#include "omega_gpu_gemm_ex.h"
27#include "omega_gpu_gemm.h"
28#include "omega_gpu_session.h"
29#include "omega_gpu_fp8_api.h"
30#include "omega_gpu_fp8gemm_api.h"
31#include "bw_warp_sim_tc.h"
32
33#include <inttypes.h>
34#include <math.h>
35#include <time.h>
36
37static int g_checks, g_failed;
38static void check(int ok, const char *what) {
39 g_checks++;
40 if (!ok) { g_failed++; printf("FAIL: %s\n", what); }
41}
42
43static const HmmaModel HM = { 16, 2, 0, 2, 0 }; /* chip-exact, evidence OWN30B-T3-HMMA-MODEL */
44static const QmmaModel QM = { 32, 26, 0, 2, 0 }; /* chip-exact, evidence OWN30B-L1G-QMMA-PFGEMM-c742aa13 */
45
46static uint64_t g_rng = 1;
47static uint32_t rnd(void) { g_rng = g_rng * 6364136223846793005ull + 1442695040888963407ull; return (uint32_t)(g_rng >> 33); }
48static uint32_t t_f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; }
49static float t_u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; }
50
51static const char *DTN[5] = { "f32", "bf16", "f16", "e4m3", "e5m2" };
52static const char *RTN[6] = { "none", "native", "widen", "bf16x3", "fast_bf16", "ffma" };
53static uint32_t dsz(unsigned t) { return t == OMEGA_GEMM_DT_F32 ? 4u : t <= OMEGA_GEMM_DT_F16 ? 2u : 1u; }
54
55/* an element of a stored array as a float (decoders independent of src/omega_gpu_gemm_ex.c) */
56static float tval(unsigned t, const void *p, uint64_t i) {
57 switch (t) {
58 case OMEGA_GEMM_DT_F32: { float f; memcpy(&f, (const uint8_t *)p + i * 4u, 4); return f; }
59 case OMEGA_GEMM_DT_BF16: return t_u2f((uint32_t)((const uint16_t *)p)[i] << 16);
60 case OMEGA_GEMM_DT_F16: return ws_h2f(((const uint16_t *)p)[i]);
61 case OMEGA_GEMM_DT_E4M3: { uint8_t c = ((const uint8_t *)p)[i]; return (c & 0x7fu) == 0x7fu ? NAN : qm_e4m3(c); }
62 default: return ws_h2f((uint16_t)(((const uint8_t *)p)[i] << 8));
63 }
64}
65static void tput(unsigned t, void *p, uint64_t i, uint32_t bits) {
66 if (t == OMEGA_GEMM_DT_F32) memcpy((uint8_t *)p + i * 4u, &bits, 4);
67 else if (t <= OMEGA_GEMM_DT_F16) ((uint16_t *)p)[i] = (uint16_t)bits;
68 else ((uint8_t *)p)[i] = (uint8_t)bits;
69}
70/* random finite values: magnitudes 2^-8 .. 2^9 (e4m3: every finite code), 1 in 61 zero */
71static uint32_t rnd_bits(unsigned t) {
72 uint32_t r = rnd(), s = (r >> 8) & 1u, e = (r >> 9) % 17u;
73 if (t != OMEGA_GEMM_DT_E4M3 && r % 61u == 0) return 0;
74 switch (t) {
75 case OMEGA_GEMM_DT_F32: return s << 31 | (119u + e) << 23 | (rnd() & 0x7fffffu);
76 case OMEGA_GEMM_DT_BF16: return s << 15 | (119u + e) << 7 | (rnd() & 0x7fu);
77 case OMEGA_GEMM_DT_F16: return s << 15 | (7u + e) << 10 | (rnd() & 0x3ffu);
78 case OMEGA_GEMM_DT_E4M3: { uint32_t c = rnd() & 0xffu; return (c & 0x7fu) == 0x7fu ? c ^ 1u : c; }
79 default: return s << 7 | (7u + e) << 2 | (rnd() & 3u);
80 }
81}
82static uint32_t nan_bits(unsigned t) { return t == OMEGA_GEMM_DT_F32 ? 0x7fc00000u : t == OMEGA_GEMM_DT_BF16 ? 0x7fc0u : 0x7e00u; }
83static uint32_t inf_bits(unsigned t, int neg) {
84 return t == OMEGA_GEMM_DT_F32 ? (neg ? 0xff800000u : 0x7f800000u) : t == OMEGA_GEMM_DT_BF16 ? (neg ? 0xff80u : 0x7f80u) : (neg ? 0xfc00u : 0x7c00u);
85}
86
87/* ------------------------------------------------------------------ the cores */
88
89/* the host model of the C3 core: the tensor-core steps of tests/gpu_gemm_test.c, f16 with the bf16 model (same HMMA
90 * datapath, offline oracle only), the FFMA reference as one fmaf per k from +0 */
91static int model_core(void *ctx, OmegaGpuGemmType t, unsigned lay, uint32_t M, uint32_t N, uint32_t K, const void *A, uint64_t lda,
92 const void *B, uint64_t ldb, float *C) {
93 (void)ctx;
94 const int acol = (lay & OMEGA_GPU_GEMM_A_COL) != 0, bcol = (lay & OMEGA_GPU_GEMM_B_COL) != 0;
95 const uint64_t ea = lda ? lda : acol ? M : K, eb = ldb ? ldb : bcol ? K : N;
96 const unsigned dt = t == OMEGA_GPU_GEMM_BF16 ? OMEGA_GEMM_DT_BF16 : t == OMEGA_GPU_GEMM_F16 ? OMEGA_GEMM_DT_F16
97 : t == OMEGA_GPU_GEMM_E4M3 ? OMEGA_GEMM_DT_E4M3 : OMEGA_GEMM_DT_F32;
98 const uint32_t ks = t == OMEGA_GPU_GEMM_E4M3 ? 32u : 16u;
99 for (uint32_t m = 0; m < M; m++)
100 for (uint32_t n = 0; n < N; n++) {
101 float acc = 0.0f;
102 if (t == OMEGA_GPU_GEMM_F32_FFMA) {
103 for (uint32_t k = 0; k < K; k++) acc = fmaf(tval(dt, A, (uint64_t)m * ea + k), tval(dt, B, (uint64_t)k * eb + n), acc);
104 } else
105 for (uint32_t k0 = 0; k0 < K; k0 += ks) {
106 float av[32], bv[32];
107 for (uint32_t k = 0; k < ks; k++) {
108 uint64_t kk = k0 + k;
109 if (kk >= K) { av[k] = bv[k] = 0.0f; continue; }
110 av[k] = tval(dt, A, acol ? kk * ea + m : (uint64_t)m * ea + kk);
111 bv[k] = tval(dt, B, bcol ? (uint64_t)n * eb + kk : kk * eb + n);
112 }
113 acc = ks == 16u ? hm_dot16(&HM, av, bv, acc) : qm_dot32(&QM, av, bv, acc);
114 }
115 C[(uint64_t)m * N + n] = acc;
116 }
117 return OMEGA_GPU_GEMM_OK;
118}
119
120/* the real kernels in the warp simulator */
121typedef struct {
122 BlackwellIRProgram *prog[4][OMEGA_GEMM_K_COUNT];
123 int mutant[4];
124 WsRange ranges[5];
125 int nr;
126 char err[300];
127 uint64_t calls;
128} SimCtx;
129
130static 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) {
131 SimCtx *c = ctx;
132 static WsSim s;
133 memset(&s, 0, sizeof s);
134 s.p = c->prog[c->nr >> 8][k]; s.threads = threads;
135 for (unsigned i = 0; i < OMEGA_GPU_GEMM_ARGS_WORDS; i++) s.cbank[0x380 / 4 + i] = a[i];
136 memcpy(s.ranges, c->ranges, sizeof c->ranges); s.nranges = c->nr & 0xff;
137 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; }
138 return 0;
139}
140static void sim_free(SimCtx *c) {
141 for (int t = 0; t < 4; t++) for (int k = 0; k < OMEGA_GEMM_K_COUNT; k++) { free(c->prog[t][k]); c->prog[t][k] = NULL; }
142}
143static int sim_core(void *ctx, OmegaGpuGemmType t, unsigned lay, uint32_t M, uint32_t N, uint32_t K, const void *A, uint64_t lda,
144 const void *B, uint64_t ldb, float *C) {
145 SimCtx *c = ctx;
146 OmegaGemmCfg cfg;
147 omega_gpu_gemm_default_cfg(t, &cfg); cfg.mutant = c->mutant[t];
148 const int ffma = t == OMEGA_GPU_GEMM_F32_FFMA;
149 if (!c->prog[t][OMEGA_GEMM_K_MAIN])
150 for (int k = ffma ? OMEGA_GEMM_K_MAIN : 0; k < OMEGA_GEMM_K_COUNT; k++) {
151 c->prog[t][k] = calloc(1, sizeof *c->prog[t][k]);
152 if (!c->prog[t][k] || omega_gpu_gemm_codegen(&cfg, (OmegaGemmKind)k, c->prog[t][k]) != 0) return OMEGA_GPU_GEMM_CODEGEN_FAIL;
153 }
154 OmegaGemmPlan p;
155 uint64_t sa = 0, sb = 0;
156 if (omega_gpu_gemm_plan(&cfg, M, N, K, &p) != 0 || omega_gpu_gemm_span(lay, M, N, K, lda, ldb, &sa, &sb) != 0) return OMEGA_GPU_GEMM_BAD_ARGS;
157 const uint32_t es = t == OMEGA_GPU_GEMM_E4M3 ? 1u : ffma ? 4u : 2u;
158 uint8_t *ap = malloc(p.ap_bytes + 1), *bp = malloc(p.bp_bytes + 1);
159 memset(ap, 0xa5, p.ap_bytes); memset(bp, 0x5a, p.bp_bytes);
160 for (uint64_t i = 0; i < (uint64_t)M * N; i++) C[i] = t_u2f(0x7fa00badu);
161 c->ranges[0] = (WsRange){ (uintptr_t)A, (uintptr_t)A + sa * es + 1 };
162 c->ranges[1] = (WsRange){ (uintptr_t)B, (uintptr_t)B + sb * es + 1 };
163 c->ranges[2] = (WsRange){ (uintptr_t)C, (uintptr_t)(C + (uint64_t)M * N) };
164 c->ranges[3] = (WsRange){ (uintptr_t)ap, (uintptr_t)ap + p.ap_bytes };
165 c->ranges[4] = (WsRange){ (uintptr_t)bp, (uintptr_t)bp + p.bp_bytes };
166 c->nr = (int)t << 8 | (ffma ? 3 : 5);
167 int rc = omega_gpu_gemm_drive_ld(&cfg, lay, M, N, K, (uintptr_t)A, lda, (uintptr_t)B, ldb, (uintptr_t)ap, (uintptr_t)bp, (uintptr_t)C, sim_launch, c);
168 for (uint64_t i = 0; rc == 0 && i < (uint64_t)M * N; i++) if (t_f2u(C[i]) == 0x7fa00badu) rc = OMEGA_GPU_GEMM_CHIP_FAIL;
169 if (rc) printf("sim: %s %s\n", omega_gpu_gemm_rc_name(rc), c->err);
170 c->calls++;
171 free(ap); free(bp);
172 return rc;
173}
174
175/* ------------------------------------------------------------------ cases */
176
177typedef struct {
178 unsigned at, bt, ct, opa, opb, compute, scale;
179 uint32_t m, n, k, batch, bm, bn, bk;
180 uint32_t pa, pb, pc; /* leading-dimension padding */
181 int bcast_a, bcast_b; /* stride 0 (with batch > 1) */
182 float alpha, beta;
183 uint64_t seed;
184 int edge; /* 0 random; 1 subnormal 2^-126 / 2^-110; 2 inf/NaN; 3 near FLT_MAX; 4 e4m3/e5m2 NaN, inf */
185} Case;
186
187typedef struct {
188 Case cs;
189 OmegaGemmExArgs x;
190 void *A, *B, *C, *C0;
191 float *sa, *sb;
192 uint64_t ea, eb, ec, nsa, nsb; /* elements */
193} Run;
194
195static uint64_t span(uint64_t rows, uint64_t cols, uint64_t ld) { return (rows - 1u) * ld + cols; }
196
197static void run_free(Run *r) { free(r->A); free(r->B); free(r->C); free(r->C0); free(r->sa); free(r->sb); memset(r, 0, sizeof *r); }
198
199/* builds the operands and the arguments of a case */
200static int run_make(Run *r, const Case *cs) {
201 memset(r, 0, sizeof *r);
202 r->cs = *cs;
203 OmegaGemmExArgs *x = &r->x;
204 x->op_a = cs->opa; x->op_b = cs->opb; x->m = cs->m; x->n = cs->n; x->k = cs->k; x->batch = cs->batch;
205 x->a_type = cs->at; x->b_type = cs->bt; x->c_type = cs->ct; x->alpha = cs->alpha; x->beta = cs->beta;
206 x->compute = cs->compute; x->scale = cs->scale; x->block_m = cs->bm; x->block_n = cs->bn; x->block_k = cs->bk;
207 const uint64_t ar = cs->opa ? cs->k : cs->m, ac = cs->opa ? cs->m : cs->k, br = cs->opb ? cs->n : cs->k, bc = cs->opb ? cs->k : cs->n;
208 x->lda = ac + cs->pa; x->ldb = bc + cs->pb; x->ldc = cs->n + cs->pc;
209 const uint64_t sa = span(ar, ac, x->lda), sb = span(br, bc, x->ldb), sc = span(cs->m, cs->n, x->ldc);
210 g_rng = cs->seed * 0x9e3779b97f4a7c15ull + 1;
211 x->stride_a = cs->batch > 1 && cs->bcast_a ? 0 : sa + rnd() % 5u;
212 x->stride_b = cs->batch > 1 && cs->bcast_b ? 0 : sb + rnd() % 5u;
213 x->stride_c = sc + rnd() % 5u;
214 r->ea = (cs->batch - 1u) * x->stride_a + sa; r->eb = (cs->batch - 1u) * x->stride_b + sb; r->ec = (cs->batch - 1u) * x->stride_c + sc;
215 r->A = malloc(r->ea * dsz(cs->at) + 8); r->B = malloc(r->eb * dsz(cs->bt) + 8);
216 r->C = malloc(r->ec * dsz(cs->ct) + 8); r->C0 = malloc(r->ec * dsz(cs->ct) + 8);
217 if (!r->A || !r->B || !r->C || !r->C0) return -1;
218 for (uint64_t i = 0; i < r->ea; i++) tput(cs->at, r->A, i, rnd_bits(cs->at));
219 for (uint64_t i = 0; i < r->eb; i++) tput(cs->bt, r->B, i, rnd_bits(cs->bt));
220 for (uint64_t i = 0; i < r->ec; i++) {
221 uint32_t v = cs->beta != 0.0f ? rnd_bits(cs->ct == OMEGA_GEMM_DT_F32 ? OMEGA_GEMM_DT_F32 : cs->ct) : nan_bits(cs->ct);
222 tput(cs->ct, r->C, i, v);
223 }
224 /* the edges */
225 if (cs->edge == 1) { /* values near 2^-126 and 2^-110 in A, ordinary B */
226 for (uint64_t i = 0; i < r->ea; i++) {
227 uint32_t e = rnd() & 1u ? 1u : 17u; /* biased exponent 1 (2^-126) or 17 (2^-110) */
228 tput(OMEGA_GEMM_DT_F32, r->A, i, (rnd() & 1u) << 31 | e << 23 | (rnd() & 0x7fffffu));
229 }
230 } else if (cs->edge == 2) { /* a few inf and NaN in A and B */
231 for (int q = 0; q < 3; q++) {
232 tput(cs->at, r->A, rnd() % r->ea, q == 2 ? nan_bits(cs->at) | 1u : inf_bits(cs->at, q == 1));
233 tput(cs->bt, r->B, rnd() % r->eb, inf_bits(cs->bt, 0));
234 }
235 } else if (cs->edge == 3) { /* A near FLT_MAX / k, B in [0.5, 1) */
236 for (uint64_t i = 0; i < r->ea; i++) tput(OMEGA_GEMM_DT_F32, r->A, i, (rnd() & 1u) << 31 | (254u - 5u) << 23 | (rnd() & 0x7fffffu));
237 for (uint64_t i = 0; i < r->eb; i++) tput(OMEGA_GEMM_DT_F32, r->B, i, (rnd() & 1u) << 31 | 126u << 23 | (rnd() & 0x7fffffu));
238 } else if (cs->edge == 4) { /* e4m3 NaN codes, e5m2 inf and NaN */
239 tput(cs->at, r->A, rnd() % r->ea, cs->at == OMEGA_GEMM_DT_E4M3 ? 0x7fu : 0x7cu);
240 tput(cs->bt, r->B, rnd() % r->eb, cs->bt == OMEGA_GEMM_DT_E4M3 ? 0xffu : 0xfdu);
241 }
242 memcpy(r->C0, r->C, r->ec * dsz(cs->ct));
243 if (cs->scale == OMEGA_GEMM_SCALE_TENSOR) { r->nsa = r->nsb = 1; }
244 else if (cs->scale == OMEGA_GEMM_SCALE_BLOCK) {
245 const uint64_t kb = (cs->k + cs->bk - 1u) / cs->bk;
246 r->nsa = (cs->m + cs->bm - 1u) / cs->bm * kb; r->nsb = kb * ((cs->n + cs->bn - 1u) / cs->bn);
247 }
248 if (r->nsa) {
249 x->scale_stride_a = cs->batch > 1 && !cs->bcast_a ? r->nsa : 0; x->scale_stride_b = cs->batch > 1 && !cs->bcast_b ? r->nsb : 0;
250 uint64_t na = r->nsa * (x->scale_stride_a ? cs->batch : 1u), nb = r->nsb * (x->scale_stride_b ? cs->batch : 1u);
251 r->sa = malloc(na * 4); r->sb = malloc(nb * 4);
252 /* scales of both signs under inf/NaN: a recomputed non-finite output keeps the scale product's sign */
253 for (uint64_t i = 0; i < na; i++) r->sa[i] = ldexpf(1.0f + (float)(rnd() % 8u) / 8.0f, (int)(rnd() % 9u) - 4) * (cs->edge == 2 && (rnd() & 1u) ? -1.0f : 1.0f);
254 for (uint64_t i = 0; i < nb; i++) r->sb[i] = ldexpf(1.0f + (float)(rnd() % 8u) / 8.0f, (int)(rnd() % 9u) - 4) * (cs->edge == 2 && (rnd() & 1u) ? -1.0f : 1.0f);
255 x->scale_a = r->sa; x->scale_b = r->sb;
256 }
257 x->a = r->A; x->b = r->B; x->c = r->C;
258 return 0;
259}
260
261static double f_of(const Run *r, uint32_t b, uint32_t i, uint32_t kk, uint32_t j) {
262 const Case *cs = &r->cs;
263 if (cs->scale != OMEGA_GEMM_SCALE_BLOCK) return 1.0;
264 const uint64_t kb = (cs->k + cs->bk - 1u) / cs->bk, nb = (cs->n + cs->bn - 1u) / cs->bn;
265 const float *sa = r->sa + b * r->x.scale_stride_a, *sb = r->sb + b * r->x.scale_stride_b;
266 return (double)sa[(i / cs->bm) * kb + kk / cs->bk] * (double)sb[(kk / cs->bk) * nb + j / cs->bn];
267}
268
269static uint64_t steps(const Case *cs) {
270 const uint64_t k = cs->k, parts = (k + 1807u) / 1808u + (k + 16383u) / 16384u + (cs->scale == OMEGA_GEMM_SCALE_BLOCK ? (k + cs->bk - 1u) / cs->bk : 0u);
271 if (cs->compute == OMEGA_GEMM_EX_F32_FFMA_REF) return k + parts;
272 const int f32ish = cs->at == OMEGA_GEMM_DT_F32 || cs->bt == OMEGA_GEMM_DT_F32 ||
273 (cs->at == OMEGA_GEMM_DT_BF16 && cs->bt == OMEGA_GEMM_DT_F16) || (cs->at == OMEGA_GEMM_DT_F16 && cs->bt == OMEGA_GEMM_DT_BF16);
274 if (f32ish && cs->compute != OMEGA_GEMM_EX_F32_FAST_BF16) return 9u * ((k + 15u) / 16u + parts) + parts;
275 if (cs->at == OMEGA_GEMM_DT_E4M3 && cs->bt == OMEGA_GEMM_DT_E4M3) return (k + 31u) / 32u + parts;
276 return (k + 15u) / 16u + parts;
277}
278/* per fp32 accumulation step: a tensor-core group of g products plus the accumulator (g + 1 terms) is aligned and
279 * truncated to 2^(emax - 26) with the sum >= 2^(emax - 1): (g + 1) 2^-25 relative, then the round-toward-zero conversion,
280 * 2^-23; times (1 + 2^-20) for the double evaluation. HMMA g = 16, QMMA g = 32; FFMA (2^-24) is under the HMMA constant.
281 * The steps compound: (1 + c)^S - 1. (2026-10-27: the earlier flat 2^-22 per step was below the chip-exact model's worst
282 * case; adversarial K = 1808 input from DnnGpu, err 5.343e-5 vs 2.843e-5.) */
283#define STEP_C_HMMA ((17.0 * 0x1p-25 + 0x1p-23) * (1.0 + 0x1p-20))
284#define STEP_C_QMMA ((33.0 * 0x1p-25 + 0x1p-23) * (1.0 + 0x1p-20))
285static double step_growth(uint64_t s, double c) { return expm1((double)s * log1p(c)); }
286static double step_c(const Case *cs) {
287 return cs->at == OMEGA_GEMM_DT_E4M3 && cs->bt == OMEGA_GEMM_DT_E4M3 && cs->compute != OMEGA_GEMM_EX_F32_FFMA_REF ? STEP_C_QMMA : STEP_C_HMMA;
288}
289static int is_fast(const Case *cs) {
290 return cs->compute == OMEGA_GEMM_EX_F32_FAST_BF16 &&
291 (cs->at == OMEGA_GEMM_DT_F32 || cs->bt == OMEGA_GEMM_DT_F32 || (cs->at == OMEGA_GEMM_DT_BF16 && cs->bt == OMEGA_GEMM_DT_F16) ||
292 (cs->at == OMEGA_GEMM_DT_F16 && cs->bt == OMEGA_GEMM_DT_BF16));
293}
294
295typedef struct { uint64_t checked, bad, untouched_bad; double max_rel; char first[300]; } Verdict;
296
297static void one_elem(const Run *r, uint32_t b, uint32_t i, uint32_t j, Verdict *v) {
298 const Case *cs = &r->cs;
299 const OmegaGemmExArgs *x = &r->x;
300 const uint8_t *A = (const uint8_t *)r->A + b * x->stride_a * dsz(cs->at), *B = (const uint8_t *)r->B + b * x->stride_b * dsz(cs->bt);
301 double s = 0, sab = 0, sapb = 0;
302 for (uint32_t kk = 0; kk < cs->k; kk++) {
303 double a = tval(cs->at, A, cs->opa ? (uint64_t)kk * x->lda + i : (uint64_t)i * x->lda + kk);
304 double bb = tval(cs->bt, B, cs->opb ? (uint64_t)j * x->ldb + kk : (uint64_t)kk * x->ldb + j);
305 double f = f_of(r, b, i, kk, j);
306 s += a * bb * f; sab += fabs(a * bb * f); sapb += (fabs(a) + fabs(bb)) * fabs(f);
307 }
308 double al = cs->alpha;
309 if (cs->scale == OMEGA_GEMM_SCALE_TENSOR) al *= (double)r->sa[b * x->scale_stride_a] * (double)r->sb[b * x->scale_stride_b];
310 const uint64_t ci = b * x->stride_c + (uint64_t)i * x->ldc + j;
311 const double old = cs->beta != 0.0f ? tval(cs->ct, r->C0, ci) : 0.0;
312 const double ref = (cs->alpha != 0.0f ? al * s : 0.0) + (double)cs->beta * old;
313 const double got = tval(cs->ct, r->C, ci);
314 const double uc = cs->ct == OMEGA_GEMM_DT_F32 ? 0x1p-24 : cs->ct == OMEGA_GEMM_DT_BF16 ? 0x1p-8 : 0x1p-11;
315 const double tc = cs->ct == OMEGA_GEMM_DT_F32 ? 0x1p-149 : cs->ct == OMEGA_GEMM_DT_BF16 ? 0x1p-133 : 0x1p-24;
316 const double maxc = cs->ct == OMEGA_GEMM_DT_F32 ? 3.4028234663852886e38 : cs->ct == OMEGA_GEMM_DT_BF16 ? 3.3895313892515355e38 : 65504.0;
317 double bound = (cs->alpha != 0.0f ? fabs(al) * (step_growth(steps(cs) + 4u, step_c(cs)) * sab + 0x1p-124 * sapb + cs->k * 0x1p-140 +
318 (is_fast(cs) ? 0x1p-7 * sab : 0.0)) : 0.0) + 0x1p-50 * fabs((double)cs->beta * old) + uc * fabs(ref) + tc;
319 int ok;
320 if (isnan(ref)) ok = isnan(got);
321 else if (isinf(ref)) ok = got == ref;
322 else if (isinf(got)) ok = fabs(ref) + bound >= maxc && (got > 0) == (ref > 0);
323 else ok = !isnan(got) && fabs(got - ref) <= bound;
324 /* the summary figure: the share of the element's own bound used (|got - ref| / bound). Dividing by |alpha s| sum|a b f|
325 * alone (before 2026-10-27) blew up wherever beta C, |ref| or the tiny-value terms carry the bound. */
326 if (ok && isfinite(ref) && isfinite(got) && bound > 0) {
327 double rel = fabs(got - ref) / bound;
328 if (rel > v->max_rel) v->max_rel = rel;
329 }
330 v->checked++;
331 if (!ok) {
332 if (!v->bad) snprintf(v->first, sizeof v->first, "batch %u C[%u][%u] = %.9g, ref %.9g, bound %.3g", b, i, j, got, ref, bound);
333 v->bad++;
334 }
335}
336
337/* every element when m n k batch <= 2^22, else 512 samples per batch plus the four corners */
338static Verdict verify(const Run *r) {
339 Verdict v; memset(&v, 0, sizeof v);
340 const Case *cs = &r->cs;
341 const OmegaGemmExArgs *x = &r->x;
342 const uint32_t es = dsz(cs->ct);
343 /* untouched C: everything outside the m x n windows */
344 uint8_t *mark = calloc(r->ec, 1);
345 for (uint32_t b = 0; b < cs->batch; b++)
346 for (uint32_t i = 0; i < cs->m; i++) memset(mark + b * x->stride_c + (uint64_t)i * x->ldc, 1, cs->n);
347 for (uint64_t e = 0; e < r->ec; e++)
348 if (!mark[e] && memcmp((uint8_t *)r->C + e * es, (uint8_t *)r->C0 + e * es, es)) v.untouched_bad++;
349 free(mark);
350 const int full = (uint64_t)cs->m * cs->n * cs->k * cs->batch <= (1ull << 22);
351 uint64_t saved = g_rng;
352 g_rng = cs->seed + 99;
353 for (uint32_t b = 0; b < cs->batch; b++) {
354 if (full) { for (uint32_t i = 0; i < cs->m; i++) for (uint32_t j = 0; j < cs->n; j++) one_elem(r, b, i, j, &v); continue; }
355 for (int q = 0; q < 512; q++) one_elem(r, b, rnd() % cs->m, rnd() % cs->n, &v);
356 one_elem(r, b, 0, 0, &v); one_elem(r, b, cs->m - 1, 0, &v); one_elem(r, b, 0, cs->n - 1, &v); one_elem(r, b, cs->m - 1, cs->n - 1, &v);
357 }
358 g_rng = saved;
359 return v;
360}
361
362static void case_name(const Case *c, char *w, size_t n) {
363 snprintf(w, n, "%s%s x %s%s -> %s %ux%ux%u batch %u%s%s pad %u/%u/%u alpha %g beta %g%s%s%s edge %d",
364 DTN[c->at], c->opa ? "^T" : "", DTN[c->bt], c->opb ? "^T" : "", DTN[c->ct], c->m, c->n, c->k, c->batch,
365 c->bcast_a ? " A-bcast" : "", c->bcast_b ? " B-bcast" : "", c->pa, c->pb, c->pc, (double)c->alpha, (double)c->beta,
366 c->compute == 1 ? " FAST_BF16" : c->compute == 2 ? " FFMA_REF" : "", c->scale == 1 ? " tensor-scale" : "",
367 c->scale == 2 ? " block-scale" : "", c->edge);
368 if (c->scale == 2) { size_t l = strlen(w); snprintf(w + l, n - l, " blocks %u/%u/%u", c->bm, c->bn, c->bk); }
369}
370
371/* runs one case through gemm_ex and checks it; returns the wrong elements (or 1 on a refusal); *route_out the route */
372static uint64_t run_case(const Case *cs, int quiet, unsigned *route_out, double *rel_out, uint64_t *calls_out) {
373 char w[1400], nm[400];
374 Run r;
375 case_name(cs, nm, sizeof nm);
376 if (run_make(&r, cs) != 0) { snprintf(w, sizeof w, "%s: host allocation", nm); check(0, w); run_free(&r); return 1; }
377 OmegaGemmExInfo info; memset(&info, 0, sizeof info);
378 r.x.info = &info;
379 int rc = omega_gpu_gemm_ex(&r.x);
380 Verdict v; memset(&v, 0, sizeof v);
381 if (rc == 0) v = verify(&r);
382 snprintf(w, sizeof w, "%s: %s route %s core calls %" PRIu64 " host-fixed %" PRIu64 ", %" PRIu64 " checked, %" PRIu64 " wrong, %" PRIu64
383 " untouched changed, max |err| / bound %.4f%s%s%s%s",
384 nm, omega_gpu_gemm_rc_name(rc), RTN[info.route < 6 ? info.route : 0], info.core_calls, info.host_fixed, v.checked, v.bad,
385 v.untouched_bad, v.max_rel, v.bad ? "; first " : "", v.bad ? v.first : "", rc ? ": " : "", rc ? omega_gpu_session_last_error() : "");
386 if (!quiet) { check(rc == 0 && v.bad == 0 && v.untouched_bad == 0, w); if (rc == 0 && !v.bad && !v.untouched_bad) printf("ok: %s\n", w); }
387 else printf("%s\n", w);
388 if (route_out) *route_out = info.route;
389 if (rel_out) *rel_out = v.max_rel;
390 if (calls_out) *calls_out = info.core_calls;
391 run_free(&r);
392 return rc ? 1 : v.bad + v.untouched_bad;
393}
394
395/* the same case through two cores (the chip or the simulator, then the host model): bit for bit */
396static void same_as_model(const Case *cs, OmegaGemmExCoreFn fn, void *ctx, const char *who) {
397 char w[900], nm[400];
398 Run r1, r2;
399 case_name(cs, nm, sizeof nm);
400 if (run_make(&r1, cs) || run_make(&r2, cs)) { check(0, "host allocation"); run_free(&r1); run_free(&r2); return; }
401 omega_gpu_gemm_ex_set_backend(fn, ctx);
402 int rc1 = omega_gpu_gemm_ex(&r1.x);
403 omega_gpu_gemm_ex_set_backend(model_core, NULL);
404 int rc2 = omega_gpu_gemm_ex(&r2.x);
405 omega_gpu_gemm_ex_set_backend(fn, ctx);
406 uint64_t diff = 0, first = UINT64_MAX;
407 const uint32_t es = dsz(cs->ct);
408 for (uint64_t e = 0; e < r1.ec; e++) {
409 uint32_t u1 = 0, u2 = 0;
410 memcpy(&u1, (uint8_t *)r1.C + e * es, es); memcpy(&u2, (uint8_t *)r2.C + e * es, es);
411 if (u1 != u2 && !(isnan(tval(cs->ct, r1.C, e)) && isnan(tval(cs->ct, r2.C, e)))) { if (!diff) first = e; diff++; }
412 }
413 snprintf(w, sizeof w, "%s core == host-model core, bit for bit: %s: %s / %s, %" PRIu64 " differ (first element %" PRIu64 ")", who, nm,
414 omega_gpu_gemm_rc_name(rc1), omega_gpu_gemm_rc_name(rc2), diff, first == UINT64_MAX ? 0 : first);
415 check(rc1 == 0 && rc2 == 0 && diff == 0, w);
416 if (rc1 == 0 && rc2 == 0 && diff == 0) printf("ok: %s\n", w);
417 run_free(&r1); run_free(&r2);
418}
419
420/* ------------------------------------------------------------------ the predeclared sweep */
421
422static const uint32_t DIMS[] = { 1, 2, 3, 5, 7, 8, 13, 15, 16, 17, 31, 32, 33, 63, 64, 65, 97, 127, 128, 129, 251, 255, 256, 257, 509, 511, 513, 1000, 1021, 1023, 1025 };
423#define NDIMS (sizeof DIMS / sizeof DIMS[0])
424#define SWEEP_SEED 20261027u
425#define SWEEP_N 600u
426
427static uint32_t pick_dim(uint32_t cap) { uint32_t d; do d = DIMS[rnd() % NDIMS]; while (d > cap); return d; }
428
429/* case i of the sweep: 13 route classes in turn, the dimensions, ops, padding, batch, alpha/beta, output from the seed */
430static Case sweep_case(uint32_t i) {
431 Case c; memset(&c, 0, sizeof c);
432 g_rng = (uint64_t)SWEEP_SEED * 1000003u + i;
433 rnd();
434 static const unsigned cls[13][4] = { /* a, b, compute, scale */
435 { 1, 1, 0, 0 }, { 2, 2, 0, 0 }, { 3, 3, 0, 0 }, { 4, 4, 0, 0 }, { 1, 3, 0, 0 }, { 2, 4, 0, 0 }, { 0, 0, 0, 0 },
436 { 0, 0, 1, 0 }, { 0, 1, 0, 0 }, { 1, 2, 0, 0 }, { 0, 0, 2, 0 }, { 3, 3, 0, 2 }, { 3, 4, 0, 1 } };
437 const unsigned k3 = i % 13u;
438 c.at = cls[k3][0]; c.bt = cls[k3][1]; c.compute = cls[k3][2]; c.scale = cls[k3][3];
439 const int x3 = k3 >= 6 && k3 <= 9; /* bf16x3 / fast: the host splits every element */
440 c.m = pick_dim(1025); c.n = pick_dim(1025); c.k = pick_dim(1025);
441 const uint32_t sp = rnd() % 20u;
442 if (sp == 0) { c.k = x3 ? (uint32_t[]){ 1807, 1808, 1809, 3617 }[rnd() % 4u] : (uint32_t[]){ 16383, 16384, 16385, 20000 }[rnd() % 4u]; c.m = pick_dim(65); c.n = pick_dim(65); }
443 else if (sp == 1) { if (rnd() & 1u) c.m = (uint32_t[]){ 4095, 4096, 4097, 5000 }[rnd() % 4u]; else c.n = (uint32_t[]){ 4095, 4096, 4097, 5000 }[rnd() % 4u];
444 if (c.m > 1025) c.n = pick_dim(33); else c.m = pick_dim(33); c.k = pick_dim(257); }
445 else if (sp == 2) { c.m = c.n = c.k = 1; }
446 c.opa = rnd() & 1u; c.opb = rnd() & 1u;
447 c.pa = rnd() % 3u ? 0 : 1 + rnd() % 7u; c.pb = rnd() % 3u ? 0 : 1 + rnd() % 7u; c.pc = rnd() % 3u ? 0 : 1 + rnd() % 7u;
448 c.batch = rnd() % 4u ? 1u : 2u + rnd() % 2u;
449 if ((uint64_t)c.m * c.n * c.k * c.batch > (1ull << 30)) c.batch = 1;
450 c.bcast_a = c.batch > 1 && rnd() % 4u == 0; c.bcast_b = c.batch > 1 && rnd() % 4u == 0;
451 static const float AL[4] = { 1.0f, -0.5f, 2.75f, 1.0f }, BE[4] = { 0.0f, 1.0f, -1.5f, 0.0f };
452 c.alpha = AL[rnd() % 4u]; c.beta = BE[rnd() % 4u];
453 const uint32_t co = rnd() % 5u;
454 c.ct = co < 3u ? OMEGA_GEMM_DT_F32 : co == 3u ? OMEGA_GEMM_DT_BF16 : OMEGA_GEMM_DT_F16;
455 if (c.ct == OMEGA_GEMM_DT_F16) c.alpha = ldexpf(c.alpha, -18 - (int)ceilf(log2f((float)c.k)));
456 if (c.scale == OMEGA_GEMM_SCALE_BLOCK) {
457 static const uint32_t BS[5] = { 1, 16, 100, 128, 7 };
458 c.bm = BS[rnd() % 5u]; c.bn = BS[rnd() % 5u]; c.bk = (uint32_t[]){ 16, 32, 100, 128 }[rnd() % 4u];
459 if (c.ct == OMEGA_GEMM_DT_F16) c.alpha = ldexpf(c.alpha, -8);
460 }
461 c.seed = (uint64_t)SWEEP_SEED * 7919u + i;
462 return c;
463}
464
465static void list_sweep(void) {
466 char nm[400];
467 printf("gemm_ex predeclared sweep: seed %u, %u cases; check per element as stated in tests/gpu_gemm_ex_test.c lines 14-21\n", SWEEP_SEED, SWEEP_N);
468 for (uint32_t i = 0; i < SWEEP_N; i++) { Case c = sweep_case(i); case_name(&c, nm, sizeof nm); printf("%u %s\n", i, nm); }
469}
470
471/* the edge cases and fixed cases of the host tiers */
472static Case mk(unsigned at, unsigned bt, unsigned ct, uint32_t m, uint32_t n, uint32_t k) {
473 Case c; memset(&c, 0, sizeof c);
474 c.at = at; c.bt = bt; c.ct = ct; c.m = m; c.n = n; c.k = k; c.batch = 1; c.alpha = 1.0f; c.seed = (uint64_t)m * 131 + n * 17 + k * 3 + at * 5 + bt;
475 return c;
476}
477static void edge_cases(Case *e, int *ne) {
478 int n = 0;
479 Case c;
480 c = mk(0, 0, 0, 33, 17, 70); c.edge = 1; e[n++] = c; /* bf16x3: A at 2^-126 and 2^-110 */
481 c = mk(0, 0, 0, 33, 17, 70); c.edge = 1; c.compute = 2; e[n++] = c; /* the same through the FFMA reference */
482 c = mk(0, 0, 0, 20, 19, 40); c.edge = 2; c.beta = 1.0f; e[n++] = c; /* inf and NaN propagate */
483 c = mk(1, 1, 0, 20, 19, 40); c.edge = 2; c.opa = 1; e[n++] = c; /* the same, bf16 native */
484 c = mk(2, 2, 2, 20, 19, 40); c.edge = 2; c.alpha = 0x1p-12f; e[n++] = c; /* f16 native */
485 c = mk(0, 0, 0, 16, 16, 24); c.edge = 3; e[n++] = c; /* near FLT_MAX: bf16x3 */
486 c = mk(0, 0, 0, 16, 16, 24); c.edge = 3; c.compute = 1; e[n++] = c; /* and FAST_BF16 */
487 c = mk(3, 3, 0, 18, 21, 70); c.edge = 4; e[n++] = c; /* e4m3 NaN codes */
488 c = mk(4, 4, 0, 18, 21, 70); c.edge = 4; e[n++] = c; /* e5m2 inf and NaN */
489 c = mk(1, 1, 0, 9, 9, 9); c.alpha = 0.0f; c.beta = 2.0f; e[n++] = c; /* alpha 0: C = beta C */
490 c = mk(1, 1, 1, 9, 9, 9); c.beta = 0.0f; e[n++] = c; /* beta 0 with NaN in C: never read */
491 c = mk(1, 1, 0, 7, 5, 3); c.batch = 3; c.bcast_a = 1; c.pc = 2; e[n++] = c; /* broadcast A over a batch */
492 c = mk(3, 3, 0, 40, 30, 100); c.scale = 2; c.bm = 7; c.bn = 16; c.bk = 32; e[n++] = c;
493 c = mk(1, 1, 0, 20, 19, 40); c.edge = 2; c.scale = 2; c.bm = 7; c.bn = 8; c.bk = 16; e[n++] = c; /* inf/NaN, signed block scales */
494 c = mk(3, 4, 1, 40, 30, 100); c.scale = 1; c.batch = 2; e[n++] = c;
495 c = mk(1, 1, 0, 3, 5, 16385); e[n++] = c; /* k past one core call */
496 c = mk(0, 0, 0, 3, 5, 1809); e[n++] = c; /* bf16x3 k past one part */
497 c = mk(2, 2, 0, 33, 31, 47); c.opa = 1; c.opb = 1; c.pa = 3; c.pb = 5; c.pc = 1; e[n++] = c;
498 *ne = n;
499}
500
501/* the four planted mutants of the general path; each must make a case go red */
502static void ex_mutants(const char *tier) {
503 char w[300];
504 Case cs[5];
505 cs[1] = mk(1, 1, 0, 24, 24, 24); /* 1: the last k element dropped */
506 cs[2] = mk(1, 1, 0, 24, 24, 24); cs[2].opb = 1; /* 2: op(B) read as N */
507 cs[3] = mk(3, 3, 0, 24, 24, 96); cs[3].scale = 2; cs[3].bm = 8; cs[3].bn = 8; cs[3].bk = 32; /* 3: the next k block's scale */
508 cs[4] = mk(1, 1, 0, 24, 24, 24); cs[4].beta = 1.0f; /* 4: beta ignored */
509 for (int mu = 1; mu <= 4; mu++) {
510 uint64_t clean = run_case(&cs[mu], 1, NULL, NULL, NULL);
511 omega_gpu_gemm_ex_test_set_mutant(mu);
512 uint64_t bad = run_case(&cs[mu], 1, NULL, NULL, NULL);
513 omega_gpu_gemm_ex_test_set_mutant(0);
514 snprintf(w, sizeof w, "%s red check: gemm_ex mutant %d is caught (%" PRIu64 " wrong; %" PRIu64 " without the mutant)", tier, mu, bad, clean);
515 check(bad > 0 && clean == 0, w);
516 printf("%s\n", w);
517 }
518}
519
520/* ------------------------------------------------------------------ the fp8 grouped/MoE fallback */
521
522/* Block-scaled fp8 weights (n x k e4m3, NaN codes excluded) with bf16-valued scales (exact in the dequant), x f32 or bf16.
523 * Check of y[r][j] (predeclared): the double reference ref = sum_c x e4m3(w) s, and
524 * |got - ref| <= ((1 + c_HMMA)^(9 ceil(k / 16) + 8) - 1) sum|x e4m3(w) s| + 2^-24 |ref| + k 2^-140
525 * (the bf16x3 bound of the header with the k parts folded into the + 8). */
526typedef struct { uint32_t n, k, block, m; } Fp8Shape;
527static uint8_t fp8_code(void) { uint8_t b; do b = (uint8_t)rnd(); while (omega_fp8_e4m3_is_nan(b)); return b; }
528static float fp8_scale(void) { return ldexpf(1.0f + (float)(rnd() % 8u) / 8.0f, -(int)(rnd() % 4u)); } /* 4 significant bits */
529static uint16_t bf16_bits(float f) { return (uint16_t)(t_f2u(f) >> 16); } /* exact for the values above */
530
531/* wrong elements of y (m x n) against the reference; x row r = xrow[r] of x (rows of k) */
532static uint64_t fp8_verify(const uint8_t *w, const float *s, double s_mul, uint32_t n, uint32_t k, uint32_t block, const float *x32,
533 const uint16_t *x16, const uint32_t *xrow, const uint32_t *wexp, uint32_t m, const float *y, double *worst) {
534 const uint32_t kb = (k + block - 1u) / block, nb = (n + block - 1u) / block;
535 uint64_t bad = 0;
536 for (uint32_t r = 0; r < m; r++) {
537 const uint32_t xr = xrow ? xrow[r] : r, e = wexp ? wexp[r] : 0u;
538 for (uint32_t j = 0; j < n; j++) {
539 double ref = 0, mag = 0;
540 for (uint32_t c = 0; c < k; c++) {
541 const double xv = x32 ? (double)x32[(size_t)xr * k + c] : (double)t_u2f((uint32_t)x16[(size_t)xr * k + c] << 16);
542 const double t = xv * (double)omega_fp8_e4m3_ref(w[((size_t)e * n + j) * k + c]) *
543 (double)s[(size_t)e * nb * kb + (size_t)(j / block) * kb + c / block] * s_mul;
544 ref += t; mag += fabs(t);
545 }
546 const double got = (double)y[(size_t)r * n + j];
547 const double bound = step_growth(9u * ((k + 15u) / 16u) + 8u, STEP_C_HMMA) * mag + 0x1p-24 * fabs(ref) + k * 0x1p-140;
548 const double err = fabs(got - ref);
549 if (!(err <= bound)) bad++;
550 if (mag > 0 && worst && err / (mag * 0x1p-24) > *worst) *worst = err / (mag * 0x1p-24);
551 }
552 }
553 return bad;
554}
555
556static void fp8_fill(uint8_t *w, float *s, uint16_t *s16, size_t nw, size_t ns, float *x32, uint16_t *x16, size_t nx) {
557 for (size_t i = 0; i < nw; i++) w[i] = fp8_code();
558 for (size_t i = 0; i < ns; i++) { s[i] = fp8_scale(); if (s16) s16[i] = bf16_bits(s[i]); }
559 for (size_t i = 0; i < nx; i++) {
560 const float v = (float)((int)(rnd() % 2001u) - 1000) / 256.0f;
561 if (x32) x32[i] = v; else x16[i] = bf16_bits(v);
562 }
563}
564
565/* Regression (DnnGpu, 2026-10-27, omega-dnn tests/test_gpu.py:472-489; GB10 receipt omega-dnn evidence/GPU-b28372d1/gemm.log):
566 * bf16 native 1 x 1 x 1808, a = [1, t x 1807] with t = 0x1.fcp-26, b = 1. Every 16-product group of t is truncated away
567 * against the accumulator 1, so the chip and the HMMA model both return exactly 0x1p+0 while ref = 1 + 1807 t. The error
568 * exceeds the old flat 2^-22-per-step bound and must stay inside the compounding bound of step_c(). */
569static void adv_k1808(const char *tier) {
570 enum { K = 1808 };
571 static uint16_t a[K], b[K];
572 const float t = 0x1.fcp-26f;
573 double ref = 0;
574 for (unsigned i = 0; i < K; i++) { a[i] = bf16_bits(i ? t : 1.0f); b[i] = bf16_bits(1.0f); ref += i ? (double)t : 1.0; }
575 float c = -1.0f;
576 OmegaGemmExInfo info;
577 OmegaGemmExArgs x;
578 memset(&x, 0, sizeof x);
579 x.op_a = OMEGA_GEMM_OP_N; x.op_b = OMEGA_GEMM_OP_N; x.m = 1; x.n = 1; x.k = K; x.batch = 1;
580 x.a_type = OMEGA_GEMM_DT_BF16; x.b_type = OMEGA_GEMM_DT_BF16; x.c_type = OMEGA_GEMM_DT_F32;
581 x.a = a; x.lda = K; x.b = b; x.ldb = 1; x.c = &c; x.ldc = 1; x.alpha = 1.0f; x.beta = 0.0f; x.info = &info;
582 const int rc = omega_gpu_gemm_ex(&x);
583 const uint64_t steps = (K + 15u) / 16u + 2u; /* + one 1808 part + one 16384 part */
584 const double err = fabs((double)c - ref), old_bound = ((double)steps + 4.0) * 0x1p-22 * ref;
585 const double bound = step_growth(steps + 4u, STEP_C_HMMA) * ref + 0x1p-24 * ref;
586 char w[300];
587 snprintf(w, sizeof w, "%s adversarial bf16 K=1808 (DnnGpu): rc %s, got %a (want 0x1p+0), err %.4g, old flat bound %.4g (must be exceeded), bound %.4g",
588 tier, omega_gpu_gemm_rc_name(rc), (double)c, err, old_bound, bound);
589 check(rc == 0 && c == 1.0f && err > old_bound && err <= bound, w);
590 printf("%s\n", w);
591}
592
593/* the raw entry omega_gpu_gemm_ex_fp8_block_rows on the installed core: refused-before shapes, row independence, mutant 5 */
594static void fp8_rows_tier(const char *tier) {
595 static const Fp8Shape sh[] = {
596 { 130, 200, 128, 3 }, { 128, 128, 128, 1 }, { 64, 384, 128, 2 }, { 33, 640, 128, 2 }, { 17, 1280, 128, 2 },
597 { 9, 1792, 128, 1 }, { 8, 2304, 128, 2 }, { 5, 2560, 128, 1 }, { 4, 3584, 128, 2 }, { 2, 16512, 128, 1 },
598 { 3, 3, 1, 2 }, { 20, 50, 7, 3 }, { 1, 1, 128, 1 } };
599 char w[300];
600 for (size_t i = 0; i < sizeof sh / sizeof sh[0]; i++) {
601 const Fp8Shape c = sh[i];
602 const int use16 = (int)(i & 1u);
603 g_rng = 0x5eed0000u + i;
604 const size_t nb = (c.n + c.block - 1u) / c.block, kb = (c.k + c.block - 1u) / c.block;
605 uint8_t *wt = malloc((size_t)c.n * c.k); float *s = malloc(nb * kb * 4u);
606 float *x32 = use16 ? NULL : malloc((size_t)c.m * c.k * 4u); uint16_t *x16 = use16 ? malloc((size_t)c.m * c.k * 2u) : NULL;
607 float *y = malloc((size_t)c.m * c.n * 4u), *y1 = malloc((size_t)c.n * 4u);
608 fp8_fill(wt, s, NULL, (size_t)c.n * c.k, nb * kb, x32, x16, (size_t)c.m * c.k);
609 const double s_mul = 0x1p-3;
610 OmegaGemmExInfo info;
611 int rc = omega_gpu_gemm_ex_fp8_block_rows(wt, s, s_mul, c.n, c.k, c.block, x32, x16, c.m, y, 0, &info);
612 double worst = 0;
613 uint64_t bad = rc ? 1u : fp8_verify(wt, s, s_mul, c.n, c.k, c.block, x32, x16, NULL, NULL, c.m, y, &worst);
614 uint32_t same = 0;
615 for (uint32_t r = 0; r < c.m && rc == 0; r++) {
616 int r1 = omega_gpu_gemm_ex_fp8_block_rows(wt, s, s_mul, c.n, c.k, c.block, x32 ? x32 + (size_t)r * c.k : NULL,
617 x16 ? x16 + (size_t)r * c.k : NULL, 1, y1, 0, NULL);
618 if (r1 == 0 && !memcmp(y1, y + (size_t)r * c.n, (size_t)c.n * 4u)) same++;
619 }
620 snprintf(w, sizeof w, "%s fp8 block rows n=%u k=%u block=%u m=%u x %s: rc %s, %" PRIu64 " wrong, worst %.2f x 2^-24 sum|t|, %u/%u rows equal to the m=1 call, route %s",
621 tier, c.n, c.k, c.block, c.m, use16 ? "bf16" : "f32", omega_gpu_gemm_rc_name(rc), bad, worst, same, c.m, RTN[info.route < 6 ? info.route : 0]);
622 check(rc == 0 && bad == 0 && same == c.m && info.route == OMEGA_GEMM_ROUTE_BF16X3, w);
623 printf("%s\n", w);
624 if (i == 0) { /* mutant 5: the next k block's scale; k = 200 has 2 blocks */
625 omega_gpu_gemm_ex_test_set_mutant(5);
626 rc = omega_gpu_gemm_ex_fp8_block_rows(wt, s, s_mul, c.n, c.k, c.block, x32, x16, c.m, y, 0, NULL);
627 omega_gpu_gemm_ex_test_set_mutant(0);
628 bad = rc ? 1u : fp8_verify(wt, s, s_mul, c.n, c.k, c.block, x32, x16, NULL, NULL, c.m, y, NULL);
629 snprintf(w, sizeof w, "%s red check: gemm_ex mutant 5 (fp8 block rows, next k block's scale) is caught (%" PRIu64 " wrong)", tier, bad);
630 check(bad > 0, w);
631 printf("%s\n", w);
632 }
633 free(wt); free(s); free(x32); free(x16); free(y); free(y1);
634 }
635 float yy[4]; uint8_t w1 = 0x38; float s1 = 1.0f, x1 = 1.0f;
636 check(omega_gpu_gemm_ex_fp8_block_rows(&w1, &s1, 1.0, 1, 1, 128, &x1, (const uint16_t *)&x1, 1, yy, 0, NULL) == OMEGA_GPU_GEMM_BAD_ARGS, "refusal: fp8 block rows with both x32 and x16");
637 check(omega_gpu_gemm_ex_fp8_block_rows(&w1, &s1, 1.0, 1, 1, 0, &x1, NULL, 1, yy, 0, NULL) == OMEGA_GPU_GEMM_BAD_ARGS, "refusal: fp8 block rows with block 0");
638 check(omega_gpu_gemm_ex_fp8_block_rows(&w1, &s1, 1.0, 2, 1, 128, &x1, NULL, 1, yy, 1, NULL) == OMEGA_GPU_GEMM_BAD_ARGS, "refusal: fp8 block rows with ldy 1 < n 2");
639}
640
641/* the real fp8 APIs on the chip: shapes the kernels refuse now answer through the fallback (info.general 1) within the
642 * bound; a served-domain shape stays on the kernel (info.general 0, grouped GEMM == GEMV bit for bit as before) */
643static void fp8_api_chip(void) {
644 char w[300];
645 static const struct { uint32_t experts, n, k; } T[] = { { 3, 256, 128 }, { 2, 128, 384 }, { 2, 256, 2048 } };
646 for (size_t ti = 0; ti < sizeof T / sizeof T[0]; ti++) {
647 const uint32_t E = T[ti].experts, n = T[ti].n, k = T[ti].k, nb = n / 128u, kb = k / 128u;
648 const int served = k == 2048u;
649 g_rng = 0xf8a90000u + ti;
650 const uint32_t G = served ? 8u : 65u, M = served ? 8u : 1030u, XR = 16u;
651 uint8_t *wt = malloc((size_t)E * n * k); float *s = malloc((size_t)E * nb * kb * 4u); uint16_t *s16 = malloc((size_t)E * nb * kb * 2u);
652 float *x = malloc((size_t)XR * k * 4u), *yg = malloc((size_t)M * n * 4u), *yv = malloc((size_t)n * 4u);
653 uint32_t *ids = malloc(M * 4u), *trow = malloc(M * 4u);
654 fp8_fill(wt, s, s16, (size_t)E * n * k, (size_t)E * nb * kb, x, NULL, (size_t)XR * k);
655 OmegaGpuFp8Tensor *t = NULL;
656 int rc = omega_gpu_fp8_tensor_upload(E, n, k, wt, s16, &t);
657 snprintf(w, sizeof w, "chip fp8 upload experts=%u n=%u k=%u: %s", E, n, k, omega_gpu_fp8_rc_name(rc));
658 check(rc == OMEGA_GPU_FP8_OK, w);
659 if (rc != OMEGA_GPU_FP8_OK) { free(wt); free(s); free(s16); free(x); free(yg); free(yv); free(ids); free(trow); continue; }
660 /* GEMV, one expert */
661 OmegaGpuFp8Info vi;
662 rc = omega_gpu_fp8_gemv_f32(t, E - 1u, x, yg, &vi);
663 uint32_t one = E - 1u;
664 uint64_t bad = rc ? 1u : fp8_verify(wt, s, 1.0, n, k, 128, x, NULL, NULL, &one, 1, yg, NULL);
665 snprintf(w, sizeof w, "chip fp8 gemv n=%u k=%u: rc %s, general %u (want %u), %" PRIu64 " wrong", n, k, omega_gpu_fp8_rc_name(rc), vi.general, served ? 0u : 1u, bad);
666 check(rc == 0 && bad == 0 && vi.general == (served ? 0u : 1u), w);
667 printf("%s\n", w);
668 /* grouped GEMV: 65 groups (past the table of 64) or 8, one x row per group */
669 for (uint32_t gi = 0; gi < G; gi++) { ids[gi] = rnd() % E; trow[gi] = gi % XR; }
670 float *xg = malloc((size_t)G * k * 4u);
671 for (uint32_t gi = 0; gi < G; gi++) memcpy(xg + (size_t)gi * k, x + (size_t)trow[gi] * k, (size_t)k * 4u);
672 rc = omega_gpu_fp8_gemv_grouped_f32(t, G, ids, xg, G, yg, &vi);
673 bad = rc ? 1u : fp8_verify(wt, s, 1.0, n, k, 128, xg, NULL, NULL, ids, G, yg, NULL);
674 snprintf(w, sizeof w, "chip fp8 grouped gemv groups=%u n=%u k=%u: rc %s, general %u (want %u), %" PRIu64 " wrong", G, n, k, omega_gpu_fp8_rc_name(rc), vi.general, served ? 0u : 1u, bad);
675 check(rc == 0 && bad == 0 && vi.general == (served ? 0u : 1u), w);
676 printf("%s\n", w);
677 free(xg);
678 /* grouped GEMM: 1030 rows (past 1024) or 8, ids sorted */
679 for (uint32_t i = 0; i < M; i++) { ids[i] = (uint32_t)((uint64_t)i * E / M); trow[i] = rnd() % XR; }
680 OmegaGpuFp8GemmInfo gi;
681 rc = omega_gpu_fp8gemm_f32(t, M, ids, trow, x, XR, yg, &gi);
682 bad = rc ? 1u : fp8_verify(wt, s, 1.0, n, k, 128, x, NULL, trow, ids, M, yg, NULL);
683 uint32_t same = 0;
684 if (rc == 0 && served) /* the kernel's promise: bit-identical to the GEMV once per row */
685 for (uint32_t i = 0; i < M; i++)
686 if (omega_gpu_fp8_gemv_f32(t, ids[i], x + (size_t)trow[i] * k, yv, NULL) == 0 && !memcmp(yv, yg + (size_t)i * n, (size_t)n * 4u)) same++;
687 snprintf(w, sizeof w, "chip fp8 grouped gemm M=%u n=%u k=%u: rc %s, general %u (want %u), %" PRIu64 " wrong%s", M, n, k, omega_gpu_fp8gemm_rc_name(rc), gi.general, served ? 0u : 1u, bad,
688 served ? (same == M ? ", every row == the GEMV" : ", rows differ from the GEMV") : "");
689 check(rc == 0 && bad == 0 && gi.general == (served ? 0u : 1u) && (!served || same == M), w);
690 printf("%s\n", w);
691 if (!served && ti == 1) { /* mutant 5 through the API path (k = 384: three k blocks) */
692 omega_gpu_gemm_ex_test_set_mutant(5);
693 rc = omega_gpu_fp8gemm_f32(t, M, ids, trow, x, XR, yg, &gi);
694 omega_gpu_gemm_ex_test_set_mutant(0);
695 bad = rc ? 0u : fp8_verify(wt, s, 1.0, n, k, 128, x, NULL, trow, ids, M, yg, NULL);
696 snprintf(w, sizeof w, "chip red check: gemm_ex mutant 5 through omega_gpu_fp8gemm_f32 (k=%u) is caught (rc %s, %" PRIu64 " wrong)", k, omega_gpu_fp8gemm_rc_name(rc), bad);
697 check(rc == 0 && bad > 0, w);
698 printf("%s\n", w);
699 }
700 omega_gpu_fp8_tensor_free(t);
701 free(wt); free(s); free(s16); free(x); free(yg); free(yv); free(ids); free(trow);
702 }
703}
704
705/* ------------------------------------------------------------------ tiers */
706
707static int refuses(OmegaGemmExArgs x, int want, const char *what) {
708 char w[200];
709 int rc = omega_gpu_gemm_ex(&x);
710 snprintf(w, sizeof w, "refusal: %s -> %s (want %s)", what, omega_gpu_gemm_rc_name(rc), omega_gpu_gemm_rc_name(want));
711 check(rc == want, w);
712 return rc == want;
713}
714
715static void refusals(void) {
716 static float a[64], b[64], c[64];
717 static float nanv = NAN;
718 OmegaGemmExArgs x; memset(&x, 0, sizeof x);
719 x.m = x.n = x.k = 4; x.a = a; x.b = b; x.c = c; x.alpha = 1.0f;
720 x.a_type = x.b_type = x.c_type = OMEGA_GEMM_DT_BF16;
721 check(omega_gpu_gemm_ex(NULL) == OMEGA_GPU_GEMM_BAD_ARGS, "refusal: NULL args");
722 OmegaGemmExArgs y;
723 y = x; y.c = NULL; refuses(y, OMEGA_GPU_GEMM_BAD_ARGS, "C NULL");
724 y = x; y.a = NULL; refuses(y, OMEGA_GPU_GEMM_BAD_ARGS, "A NULL with alpha 1");
725 y = x; y.m = 0; refuses(y, OMEGA_GPU_GEMM_BAD_ARGS, "m 0");
726 y = x; y.k = 0; refuses(y, OMEGA_GPU_GEMM_BAD_ARGS, "k 0");
727 y = x; y.op_a = 2; refuses(y, OMEGA_GPU_GEMM_BAD_ARGS, "op_a 2");
728 y = x; y.a_type = 5; refuses(y, OMEGA_GPU_GEMM_BAD_ARGS, "a_type 5");
729 y = x; y.c_type = 5; refuses(y, OMEGA_GPU_GEMM_BAD_ARGS, "c_type 5");
730 y = x; y.compute = 3; refuses(y, OMEGA_GPU_GEMM_BAD_ARGS, "compute 3");
731 y = x; y.scale = 3; refuses(y, OMEGA_GPU_GEMM_BAD_ARGS, "scale 3");
732 y = x; y.scale = 3; y.scale_a = b; y.scale_b = b; y.block_m = y.block_n = y.block_k = 1;
733 refuses(y, OMEGA_GPU_GEMM_BAD_ARGS, "scale 3 with finite scales and blocks");
734 y = x; y.lda = 3; refuses(y, OMEGA_GPU_GEMM_BAD_ARGS, "lda 3 < k 4");
735 y = x; y.op_b = 1; y.ldb = 3; refuses(y, OMEGA_GPU_GEMM_BAD_ARGS, "op_b T, ldb 3 < k 4");
736 y = x; y.ldc = 2; refuses(y, OMEGA_GPU_GEMM_BAD_ARGS, "ldc 2 < n 4");
737 y = x; y.batch = 2; y.stride_a = y.stride_b = 0; y.stride_c = 15; refuses(y, OMEGA_GPU_GEMM_BAD_ARGS, "C batches overlap (stride_c 15 < 16)");
738 y = x; y.c = a; refuses(y, OMEGA_GPU_GEMM_BAD_ARGS, "C is A");
739 y = x; y.c = (uint8_t *)b + 6; refuses(y, OMEGA_GPU_GEMM_BAD_ARGS, "C overlaps B");
740 y = x; y.lda = 1ull << 60; refuses(y, OMEGA_GPU_GEMM_BAD_ARGS, "lda 2^60");
741 y = x; y.batch = 3; y.stride_c = 1ull << 62; refuses(y, OMEGA_GPU_GEMM_BAD_ARGS, "stride_c 2^62");
742 y = x; y.a_type = y.b_type = OMEGA_GEMM_DT_E4M3; y.scale = OMEGA_GEMM_SCALE_TENSOR; refuses(y, OMEGA_GPU_GEMM_BAD_ARGS, "tensor scale without scales");
743 y = x; y.scale = OMEGA_GEMM_SCALE_TENSOR; y.scale_a = &nanv; y.scale_b = b; refuses(y, OMEGA_GPU_GEMM_BAD_ARGS, "NaN tensor scale");
744 y = x; y.scale = OMEGA_GEMM_SCALE_BLOCK; y.scale_a = b; y.scale_b = b; y.block_m = y.block_n = 1; refuses(y, OMEGA_GPU_GEMM_BAD_ARGS, "block_k 0");
745 y = x; y.m = OMEGA_GPU_GEMM_EX_MAX_DIM + 1u; refuses(y, OMEGA_GPU_GEMM_TOO_LARGE, "m 2^31");
746 y = x; y.batch = OMEGA_GPU_GEMM_EX_MAX_BATCH + 1u; refuses(y, OMEGA_GPU_GEMM_TOO_LARGE, "batch 2^31");
747 y = x; y.c_type = OMEGA_GEMM_DT_E4M3; refuses(y, OMEGA_GPU_GEMM_UNSUPPORTED, "e4m3 output");
748 y = x; y.c_type = OMEGA_GEMM_DT_E5M2; refuses(y, OMEGA_GPU_GEMM_UNSUPPORTED, "e5m2 output");
749 /* alpha 0: A and B may be NULL (not read) */
750 y = x; y.alpha = 0.0f; y.a = y.b = NULL; y.beta = 0.0f;
751 check(omega_gpu_gemm_ex(&y) == OMEGA_GPU_GEMM_OK, "alpha 0 with A, B NULL: accepted (A and B not read)");
752}
753
754static void route_table(void) {
755 /* the route of every input pair (contract table of omega_gpu_gemm_ex.h) through the model core, 3x3x3 */
756 static const unsigned want[5][5] = { /* compute DEFAULT */
757 { OMEGA_GEMM_ROUTE_BF16X3, OMEGA_GEMM_ROUTE_BF16X3, OMEGA_GEMM_ROUTE_BF16X3, OMEGA_GEMM_ROUTE_BF16X3, OMEGA_GEMM_ROUTE_BF16X3 },
758 { OMEGA_GEMM_ROUTE_BF16X3, OMEGA_GEMM_ROUTE_NATIVE, OMEGA_GEMM_ROUTE_BF16X3, OMEGA_GEMM_ROUTE_WIDEN, OMEGA_GEMM_ROUTE_WIDEN },
759 { OMEGA_GEMM_ROUTE_BF16X3, OMEGA_GEMM_ROUTE_BF16X3, OMEGA_GEMM_ROUTE_NATIVE, OMEGA_GEMM_ROUTE_WIDEN, OMEGA_GEMM_ROUTE_WIDEN },
760 { OMEGA_GEMM_ROUTE_BF16X3, OMEGA_GEMM_ROUTE_WIDEN, OMEGA_GEMM_ROUTE_WIDEN, OMEGA_GEMM_ROUTE_NATIVE, OMEGA_GEMM_ROUTE_WIDEN },
761 { OMEGA_GEMM_ROUTE_BF16X3, OMEGA_GEMM_ROUTE_WIDEN, OMEGA_GEMM_ROUTE_WIDEN, OMEGA_GEMM_ROUTE_WIDEN, OMEGA_GEMM_ROUTE_WIDEN } };
762 char w[200];
763 for (unsigned a = 0; a < 5; a++)
764 for (unsigned b = 0; b < 5; b++) {
765 Case c = mk(a, b, 0, 3, 3, 3);
766 unsigned route = 0;
767 uint64_t bad = run_case(&c, 1, &route, NULL, NULL);
768 snprintf(w, sizeof w, "route %s x %s: %s (want %s), %" PRIu64 " wrong", DTN[a], DTN[b], RTN[route], RTN[want[a][b]], bad);
769 check(route == want[a][b] && bad == 0, w);
770 }
771}
772
773static void host_tier(void) {
774 char w[200];
775 omega_gpu_gemm_ex_set_backend(model_core, NULL);
776 refusals();
777 route_table();
778 Case e[32]; int ne = 0;
779 edge_cases(e, &ne);
780 for (int i = 0; i < ne; i++) run_case(&e[i], 0, NULL, NULL, NULL);
781 /* every 12th sweep case with small dimensions through the model core */
782 int ran = 0;
783 for (uint32_t i = 0; i < SWEEP_N; i++) {
784 Case c = sweep_case(i);
785 if ((uint64_t)c.m * c.n * c.k * c.batch > (1ull << 21)) continue;
786 if (i % 3u) continue;
787 run_case(&c, 0, NULL, NULL, NULL); ran++;
788 }
789 snprintf(w, sizeof w, "host model: %d small sweep cases", ran);
790 check(ran >= 40, w);
791 printf("%s\n", w);
792 ex_mutants("host");
793 fp8_rows_tier("host");
794 adv_k1808("host");
795}
796
797/* --bench: the host-path cost of omega_gpu_gemm_ex (validation, plan, conversions, k-part sums, recompute, epilogue)
798 * without the core. A null core writes zeros, so the time is the library's own host work, the same core cost on both
799 * paths removed. Every edge case and every sweep case, then the fp8 block-rows shapes; each call timed BENCH_REPS
800 * times on prepared operands (C restored from C0 between repetitions, outside the timed region). One line per case:
801 * "bench <id> <rc> <median ns> <min ns> <max ns> <name>"; `--bench ID` times only that case. Compare two builds with
802 * tools/gemm_ex_bench.sh. */
803#define BENCH_REPS 5
804static int null_core(void *ctx, OmegaGpuGemmType t, unsigned lay, uint32_t M, uint32_t N, uint32_t K, const void *A, uint64_t lda,
805 const void *B, uint64_t ldb, float *C) {
806 (void)ctx; (void)t; (void)lay; (void)K; (void)A; (void)lda; (void)B; (void)ldb;
807 memset(C, 0, (size_t)M * N * sizeof *C);
808 return OMEGA_GPU_GEMM_OK;
809}
810static uint64_t now_ns(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (uint64_t)ts.tv_sec * 1000000000u + (uint64_t)ts.tv_nsec; }
811static 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; }
812static void bench_line(const char *id, int rc, uint64_t *t, const char *nm) {
813 qsort(t, BENCH_REPS, sizeof *t, cmp_u64);
814 printf("bench %s %d %" PRIu64 " %" PRIu64 " %" PRIu64 " %s\n", id, rc, t[BENCH_REPS / 2], t[0], t[BENCH_REPS - 1], nm);
815}
816static const char *g_bench_only;
817static void bench_case(const char *id, const Case *cs) {
818 char nm[400];
819 uint64_t t[BENCH_REPS];
820 Run r;
821 int rc = 0;
822 if (g_bench_only && strcmp(g_bench_only, id)) return;
823 case_name(cs, nm, sizeof nm);
824 if (run_make(&r, cs) != 0) { run_free(&r); printf("bench %s alloc %s\n", id, nm); return; }
825 for (int k = 0; k < BENCH_REPS; k++) {
826 memcpy(r.C, r.C0, r.ec * dsz(cs->ct));
827 const uint64_t t0 = now_ns();
828 rc = omega_gpu_gemm_ex(&r.x);
829 t[k] = now_ns() - t0;
830 }
831 bench_line(id, rc, t, nm);
832 run_free(&r);
833}
834static void bench_tier(const char *only) {
835 char id[32], nm[200];
836 g_bench_only = only;
837 omega_gpu_gemm_ex_set_backend(null_core, NULL);
838 Case e[32]; int ne = 0;
839 edge_cases(e, &ne);
840 for (int i = 0; i < ne; i++) { snprintf(id, sizeof id, "e%d", i); bench_case(id, &e[i]); }
841 for (uint32_t i = 0; i < SWEEP_N; i++) { Case c = sweep_case(i); snprintf(id, sizeof id, "s%u", i); bench_case(id, &c); }
842 static const Fp8Shape sh[] = { { 130, 200, 128, 3 }, { 64, 384, 128, 2 }, { 17, 1280, 128, 2 }, { 2, 16512, 128, 1 }, { 20, 50, 7, 3 } };
843 for (size_t i = 0; i < sizeof sh / sizeof sh[0]; i++) {
844 const Fp8Shape c = sh[i];
845 snprintf(id, sizeof id, "f%zu", i);
846 if (only && strcmp(only, id)) continue;
847 g_rng = 0x5eed0000u + i;
848 const size_t nb = (c.n + c.block - 1u) / c.block, kb = (c.k + c.block - 1u) / c.block;
849 uint8_t *wt = malloc((size_t)c.n * c.k); float *s = malloc(nb * kb * 4u);
850 float *x32 = malloc((size_t)c.m * c.k * 4u), *y = malloc((size_t)c.m * c.n * 4u);
851 fp8_fill(wt, s, NULL, (size_t)c.n * c.k, nb * kb, x32, NULL, (size_t)c.m * c.k);
852 uint64_t t[BENCH_REPS];
853 int rc = 0;
854 for (int k = 0; k < BENCH_REPS; k++) {
855 const uint64_t t0 = now_ns();
856 rc = omega_gpu_gemm_ex_fp8_block_rows(wt, s, 0x1p-3, c.n, c.k, c.block, x32, NULL, c.m, y, 0, NULL);
857 t[k] = now_ns() - t0;
858 }
859 snprintf(nm, sizeof nm, "fp8 block rows n=%u k=%u block=%u m=%u x f32", c.n, c.k, c.block, c.m);
860 bench_line(id, rc, t, nm);
861 free(wt); free(s); free(x32); free(y);
862 }
863}
864
865static void sim_tier(void) {
866 char w[300];
867 SimCtx sc; memset(&sc, 0, sizeof sc);
868 static const Case base[] = {
869 { 1, 1, 0, 0, 0, 0, 0, 17, 9, 33, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1.0f, 0.0f, 11, 0 },
870 { 1, 1, 0, 1, 1, 0, 0, 13, 20, 40, 2, 0, 0, 0, 3, 2, 1, 0, 0, 1.0f, 1.0f, 12, 0 },
871 { 2, 2, 0, 0, 1, 0, 0, 19, 11, 50, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1.0f, 0.0f, 13, 0 },
872 { 2, 2, 2, 1, 0, 0, 0, 9, 24, 17, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0x1p-14f, 0.0f, 14, 0 },
873 { 3, 3, 0, 0, 0, 0, 0, 17, 9, 70, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1.0f, 0.0f, 15, 0 },
874 { 3, 4, 1, 1, 0, 0, 1, 10, 12, 45, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1.0f, 0.0f, 16, 0 },
875 { 4, 4, 0, 0, 0, 0, 0, 7, 13, 21, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1.0f, 0.0f, 17, 0 },
876 { 0, 0, 0, 0, 1, 0, 0, 5, 6, 23, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1.0f, 0.0f, 18, 0 },
877 { 0, 0, 0, 0, 0, 1, 0, 6, 5, 33, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1.0f, 0.0f, 19, 0 },
878 { 0, 0, 0, 1, 1, 2, 0, 9, 130, 37, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1.0f, -1.5f, 20, 0 },
879 { 0, 0, 0, 0, 0, 2, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1.0f, 0.0f, 21, 0 },
880 { 3, 3, 0, 0, 0, 0, 2, 12, 12, 64, 1, 5, 16, 32, 0, 0, 0, 0, 0, 1.0f, 0.0f, 22, 0 } };
881 omega_gpu_gemm_ex_set_backend(sim_core, &sc);
882 for (size_t i = 0; i < sizeof base / sizeof base[0]; i++) { run_case(&base[i], 0, NULL, NULL, NULL); same_as_model(&base[i], sim_core, &sc, "simulator"); }
883 /* the kernel mutants: FFMA 1, f16 1-3 */
884 static const struct { unsigned t; int mu; Case c; } km[] = {
885 { OMEGA_GPU_GEMM_F32_FFMA, 1, { 0, 0, 0, 0, 0, 2, 0, 5, 7, 9, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1.0f, 0.0f, 31, 0 } },
886 { OMEGA_GPU_GEMM_F16, 1, { 2, 2, 0, 0, 0, 0, 0, 17, 9, 33, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1.0f, 0.0f, 32, 0 } },
887 { OMEGA_GPU_GEMM_F16, 2, { 2, 2, 0, 0, 0, 0, 0, 17, 9, 33, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1.0f, 0.0f, 33, 0 } },
888 { OMEGA_GPU_GEMM_F16, 3, { 2, 2, 0, 0, 0, 0, 0, 17, 9, 33, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1.0f, 0.0f, 34, 0 } } };
889 for (size_t i = 0; i < sizeof km / sizeof km[0]; i++) {
890 sim_free(&sc); sc.mutant[km[i].t] = km[i].mu;
891 uint64_t bad = run_case(&km[i].c, 1, NULL, NULL, NULL);
892 sim_free(&sc); sc.mutant[km[i].t] = 0;
893 snprintf(w, sizeof w, "sim red check: kernel %s mutant %d is caught (%" PRIu64 " wrong)", km[i].t == OMEGA_GPU_GEMM_F16 ? "f16" : "ffma", km[i].mu, bad);
894 check(bad > 0, w);
895 printf("%s\n", w);
896 }
897 sim_free(&sc);
898 omega_gpu_gemm_ex_set_backend(NULL, NULL);
899}
900
901/* one 1x1x1 product through the native core: does the tensor core keep a subnormal input? */
902static void subnormal_probe(OmegaGemmExCoreFn fn, void *ctx, unsigned dt, uint16_t a, uint16_t b, const char *what) {
903 char w[300];
904 uint16_t A = a, B = b; float C1 = 0, C2 = 0;
905 OmegaGemmExArgs x; memset(&x, 0, sizeof x);
906 x.m = x.n = x.k = 1; x.a = &A; x.b = &B; x.a_type = x.b_type = dt; x.c_type = OMEGA_GEMM_DT_F32; x.alpha = 1.0f;
907 x.c = &C1; omega_gpu_gemm_ex_set_backend(fn, ctx); int rc1 = omega_gpu_gemm_ex(&x);
908 x.c = &C2; omega_gpu_gemm_ex_set_backend(model_core, NULL); int rc2 = omega_gpu_gemm_ex(&x);
909 omega_gpu_gemm_ex_set_backend(fn, ctx);
910 const double exact = (double)tval(dt, &A, 0) * (double)tval(dt, &B, 0);
911 snprintf(w, sizeof w, "subnormal probe %s: core %a (%s), model %a, exact %a: the tensor core %s the subnormal input", what, (double)C1,
912 omega_gpu_gemm_rc_name(rc1), (double)C2, exact, C1 == 0.0f ? "FLUSHES" : (double)C1 == exact ? "KEEPS" : "changes");
913 check(rc1 == 0 && rc2 == 0 && t_f2u(C1) == t_f2u(C2), w);
914 printf("%s\n", w);
915}
916
917static void chip_tier(int quick, uint32_t part, uint32_t parts) {
918 char w[300];
919 omega_gpu_gemm_ex_set_backend(NULL, NULL);
920 if (part == 0) {
921 subnormal_probe(NULL, NULL, OMEGA_GEMM_DT_BF16, 0x0001u, 0x7180u, "bf16 2^-133 x 2^100");
922 subnormal_probe(NULL, NULL, OMEGA_GEMM_DT_F16, 0x0001u, 0x6400u, "f16 2^-24 x 2^10");
923 Case e[32]; int ne = 0;
924 edge_cases(e, &ne);
925 for (int i = 0; i < ne; i++) run_case(&e[i], 0, NULL, NULL, NULL);
926 ex_mutants("chip");
927 fp8_rows_tier("chip");
928 adv_k1808("chip");
929 fp8_api_chip();
930 /* the kernel mutants on the chip */
931 static const struct { unsigned t; int mu; Case c; } km[] = {
932 { OMEGA_GPU_GEMM_F32_FFMA, 1, { 0, 0, 0, 0, 0, 2, 0, 65, 130, 33, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1.0f, 0.0f, 41, 0 } },
933 { OMEGA_GPU_GEMM_F16, 1, { 2, 2, 0, 0, 0, 0, 0, 65, 63, 100, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1.0f, 0.0f, 42, 0 } },
934 { OMEGA_GPU_GEMM_F16, 2, { 2, 2, 0, 0, 0, 0, 0, 65, 63, 100, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1.0f, 0.0f, 43, 0 } },
935 { OMEGA_GPU_GEMM_F16, 3, { 2, 2, 0, 0, 0, 0, 0, 65, 63, 100, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1.0f, 0.0f, 44, 0 } } };
936 for (size_t i = 0; i < sizeof km / sizeof km[0]; i++) {
937 OmegaGemmCfg cfg; omega_gpu_gemm_default_cfg((OmegaGpuGemmType)km[i].t, &cfg); cfg.mutant = km[i].mu;
938 omega_gpu_gemm_test_set_cfg((OmegaGpuGemmType)km[i].t, &cfg);
939 uint64_t bad = run_case(&km[i].c, 1, NULL, NULL, NULL);
940 omega_gpu_gemm_test_set_cfg((OmegaGpuGemmType)km[i].t, NULL);
941 snprintf(w, sizeof w, "chip red check: kernel %s mutant %d is caught (%" PRIu64 " wrong)", km[i].t == OMEGA_GPU_GEMM_F16 ? "f16" : "ffma", km[i].mu, bad);
942 check(bad > 0, w);
943 printf("%s\n", w);
944 }
945 }
946 /* the sweep */
947 const uint32_t n = quick ? 40u : SWEEP_N;
948 uint32_t ran = 0, exact = 0;
949 double worst[6] = { 0 };
950 int before = g_failed;
951 for (uint32_t i = part; i < n; i += parts) {
952 Case c = sweep_case(i);
953 unsigned route = 0; double rel = 0; uint64_t calls = 0;
954 run_case(&c, 0, &route, &rel, &calls);
955 if (route < 6 && rel > worst[route]) worst[route] = rel;
956 ran++;
957 /* chip core == model core bit for bit where the model is cheap (the model computes every k of every element) */
958 uint64_t kcore = route == OMEGA_GEMM_ROUTE_BF16X3 ? 9u * (uint64_t)c.k : c.k;
959 if ((uint64_t)c.m * c.n * kcore * c.batch <= (1ull << 24) && i % 2u == 0) { same_as_model(&c, NULL, NULL, "chip"); exact++; }
960 }
961 snprintf(w, sizeof w, "sweep part %u/%u: %u cases (%u also bit for bit against the model core): %s", part, parts, ran, exact, g_failed == before ? "PASS" : "FAIL");
962 printf("%s\n", w);
963 for (int r = 1; r < 6; r++) printf("route %s: largest |got - ref| / bound = %.4f (passing elements)\n", RTN[r], worst[r]);
964 omega_gpu_session_close();
965}
966
967int main(int argc, char **argv) {
968 const char *tier = "chip";
969 int quick = 0;
970 uint32_t part = 0, parts = 1;
971 for (int i = 1; i < argc; i++) {
972 if (!strcmp(argv[i], "--host-only")) tier = "host";
973 else if (!strcmp(argv[i], "--sim")) tier = "sim";
974 else if (!strcmp(argv[i], "--list")) { list_sweep(); return 0; }
975 else if (!strcmp(argv[i], "--bench")) { bench_tier(i + 1 < argc ? argv[i + 1] : NULL); return 0; }
976 else if (!strcmp(argv[i], "--quick")) quick = 1;
977 else if (!strcmp(argv[i], "--part") && i + 1 < argc && sscanf(argv[i + 1], "%u/%u", &part, &parts) == 2 && parts && part < parts) i++;
978 else { fprintf(stderr, "usage: %s [--host-only | --sim | --list | --bench [ID] | --quick | --part I/N]\n", argv[0]); return 2; }
979 }
980 if (!strcmp(tier, "host")) host_tier();
981 else if (!strcmp(tier, "sim")) { host_tier(); sim_tier(); }
982 else chip_tier(quick, part, parts);
983 if (g_failed) printf("gemm_ex %s: FAIL (%d of %d)\n", tier, g_failed, g_checks);
984 else printf("gemm_ex %s: PASS (%d checks)\n", tier, g_checks);
985 return g_failed ? 1 : 0;
986}