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
omega-mv / tests / gpu_tcgemm_test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* gpu_tcgemm_test.c (OWN-30B T3, 2026-10-13): tensor-core grouped FP8 GEMM (omega_blackwell_codegen_tc.h). |
| 18 | * --host-only program statistics and register budgets for the production shapes; the C emulation (exact HMMA model, |
| 19 | * tests/bw_hmma_model.h) against a double oracle; permutation and decode checks |
| 20 | * --sim the generated kernel in the warp simulator (both fragment orders) bit-identical to the C emulation; |
| 21 | * mutants 1-4 caught (a simulator error counts as a FAIL, never as caught) |
| 22 | * --chip (under flock /tmp/aien-gb10.lock) through omega_gpu_tcgemm_api: bit-identity to the emulation, second call |
| 23 | * identical, mutants caught on the chip, timing medians (TFLOPS, GB/s) at large M |
| 24 | * Verdict line last: "GPU_TCGEMM: PASS|FAIL". */ |
| 25 | #include "omega_blackwell_codegen_tc.h" |
| 26 | #include "omega_gpu_fp8_api.h" |
| 27 | #include "omega_gpu_session.h" |
| 28 | #include "omega_gpu_tcgemm_api.h" |
| 29 | #include <inttypes.h> |
| 30 | #include "bw_warp_sim_tc.h" |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <time.h> |
| 35 | #include <math.h> |
| 36 | |
| 37 | static const HmmaModel GB10 = { 16, 2, 0, 2, 0 }; |
| 38 | static uint64_t rng = 0x243f6a8885a308d3ull; |
| 39 | static uint32_t rnd(void) { rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; return (uint32_t)(rng >> 16); } |
| 40 | static float frnd(void) { return ((float)(rnd() & 0xffffff) / 16777216.0f) * 2.0f - 1.0f; } |
| 41 | static float u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } |
| 42 | |
| 43 | /* a problem: E experts of n x k e4m3, M sorted rows (expert ids non-decreasing), X source rows */ |
| 44 | typedef struct { |
| 45 | uint32_t E, n, k, M, xr; |
| 46 | uint8_t *w; /* E * n * k */ |
| 47 | uint16_t *sbf; /* E * (n/128) * (k/128) bf16 scale_inv */ |
| 48 | float *sf; /* folded f32 * 2^56 */ |
| 49 | float *x; /* xr * k */ |
| 50 | uint16_t *xp; /* xr * k stored (permuted bf16 * 2^64) */ |
| 51 | uint32_t *eid, *src; /* M */ |
| 52 | } Prob; |
| 53 | |
| 54 | static uint8_t rand_e4m3(void) { for (;;) { uint8_t c = (uint8_t)rnd(); if ((c & 0x7f) != 0x7f) return c; } } |
| 55 | static uint16_t bf16_trunc(float f) { uint32_t u; memcpy(&u, &f, 4); return (uint16_t)(u >> 16); } |
| 56 | |
| 57 | static void prob_make(Prob *p, uint32_t E, uint32_t n, uint32_t k, uint32_t M, uint32_t xr, int skewed) { |
| 58 | memset(p, 0, sizeof *p); |
| 59 | p->E = E; p->n = n; p->k = k; p->M = M; p->xr = xr; |
| 60 | size_t wn = (size_t)E * n * k, sn = (size_t)E * (n / 128) * (k / 128); |
| 61 | p->w = malloc(wn); p->sbf = malloc(sn * 2); p->sf = malloc(sn * 4); p->x = malloc((size_t)xr * k * 4); p->xp = malloc((size_t)xr * k * 2); |
| 62 | p->eid = malloc(M * 4); p->src = malloc(M * 4); |
| 63 | for (size_t i = 0; i < wn; i++) p->w[i] = rand_e4m3(); |
| 64 | for (size_t i = 0; i < sn; i++) p->sbf[i] = bf16_trunc(ldexpf(1.0f + (float)(rnd() & 127) / 128.0f, -(int)(rnd() % 12) - 4)); |
| 65 | if (omega_fp8_pack_scales(p->sbf, sn, p->sf, 0) != 0) { printf("scale pack failed\n"); exit(2); } |
| 66 | for (size_t i = 0; i < (size_t)xr * k; i++) p->x[i] = frnd() * (rnd() % 16 == 0 ? 8.0f : 1.0f); |
| 67 | for (uint32_t r = 0; r < xr; r++) omega_tcgemm_permute_x(p->x + (size_t)r * k, k, p->xp + (size_t)r * k); |
| 68 | /* expert ids: sorted; skewed puts many rows on a few experts (multi-tile segments) */ |
| 69 | for (uint32_t i = 0; i < M; i++) { uint32_t e = skewed ? (uint32_t)((uint64_t)i * i / ((uint64_t)M * M / E + 1)) : (uint32_t)((uint64_t)i * E / M); if (e >= E) e = E - 1; p->eid[i] = e; p->src[i] = rnd() % xr; } |
| 70 | } |
| 71 | static void prob_free(Prob *p) { free(p->w); free(p->sbf); free(p->sf); free(p->x); free(p->xp); free(p->eid); free(p->src); } |
| 72 | |
| 73 | /* stored index of element p in a permuted row: group (0,2,1,3) */ |
| 74 | static uint32_t stored_ix(uint32_t p) { static const uint32_t inv[4] = { 0, 2, 1, 3 }; return (p & ~3u) | inv[p & 3u]; } |
| 75 | |
| 76 | /* C emulation: exactly the kernel's arithmetic (8 HMMA steps per 128 block from a zero temporary, then fma with S') */ |
| 77 | static void emulate(const Prob *p, float *y) { |
| 78 | const uint32_t n = p->n, k = p->k, KB = k / 128; |
| 79 | for (uint32_t i = 0; i < p->M; i++) { |
| 80 | const uint16_t *xs = p->xp + (size_t)p->src[i] * k; |
| 81 | for (uint32_t c = 0; c < n; c++) { |
| 82 | const uint8_t *wr = p->w + ((size_t)p->eid[i] * n + c) * k; |
| 83 | const float *sr = p->sf + ((size_t)p->eid[i] * (n / 128) + c / 128) * KB; |
| 84 | float acc = 0.0f; |
| 85 | for (uint32_t kb = 0; kb < KB; kb++) { |
| 86 | float tmp = 0.0f; |
| 87 | for (uint32_t ch = 0; ch < 2; ch++) for (uint32_t s = 0; s < 4; s++) { |
| 88 | float a[16], b[16]; int m = 0; |
| 89 | for (uint32_t t = 0; t < 4; t++) for (uint32_t j = 0; j < 4; j++) { |
| 90 | uint32_t e = kb * 128 + ch * 64 + 16 * t + 4 * s + j; |
| 91 | a[m] = u2f((uint32_t)xs[stored_ix(e)] << 16); b[m] = u2f((uint32_t)omega_tcgemm_w_bf16(wr[e]) << 16); m++; |
| 92 | } |
| 93 | tmp = hm_dot16(&GB10, a, b, tmp); |
| 94 | } |
| 95 | acc = fmaf(tmp, sr[kb], acc); |
| 96 | } |
| 97 | y[(size_t)i * n + c] = acc; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /* tiles: split every expert segment into runs of at most bm rows; rowidx padded by bm entries */ |
| 103 | typedef struct { uint64_t w_va, s_va; uint32_t row0, cnt, pad[2]; } Tile; |
| 104 | static uint32_t make_tiles(const Prob *p, uint32_t bm, Tile *t, const uint8_t *wbase, const float *sbase) { |
| 105 | uint32_t nt = 0; |
| 106 | for (uint32_t i = 0; i < p->M;) { |
| 107 | uint32_t j = i; while (j < p->M && p->eid[j] == p->eid[i]) j++; |
| 108 | for (uint32_t r = i; r < j; r += bm) { |
| 109 | memset(&t[nt], 0, sizeof t[nt]); |
| 110 | t[nt].w_va = (uint64_t)(uintptr_t)(wbase + (size_t)p->eid[i] * p->n * p->k); |
| 111 | t[nt].s_va = (uint64_t)(uintptr_t)(sbase + (size_t)p->eid[i] * (p->n / 128) * (p->k / 128)); |
| 112 | t[nt].row0 = r; t[nt].cnt = j - r < bm ? j - r : bm; nt++; |
| 113 | } |
| 114 | i = j; |
| 115 | } |
| 116 | return nt; |
| 117 | } |
| 118 | |
| 119 | static int host_only(void) { |
| 120 | int fail = 0; |
| 121 | /* decode: every non-NaN code, the bf16 bits are the value * 2^-120 exactly */ |
| 122 | for (unsigned c = 0; c < 256; c++) { |
| 123 | if ((c & 0x7f) == 0x7f) continue; |
| 124 | unsigned e = (c >> 3) & 15, m = c & 7; double v = e ? ldexp(1.0 + m / 8.0, (int)e - 7) : ldexp(m / 8.0, -6); if (c & 0x80) v = -v; |
| 125 | if ((double)u2f((uint32_t)omega_tcgemm_w_bf16((uint8_t)c) << 16) != ldexp(v, -120)) { printf("decode code %02x wrong\n", c); fail++; } |
| 126 | } |
| 127 | { int pr = omega_blackwell_verify_codegen_fixtures_bfly_f2fp(); printf("SHFL.BFLY / F2FP.BF16 golden words: %s (rc %d)\n", pr ? "FAIL" : "ok", pr); if (pr) fail++; } |
| 128 | /* production shapes: instructions and registers (declared = peak + 2 rounded to 8 <= 128; 32 * warps * declared <= 65536) */ |
| 129 | static const OmegaTcGemmSpec shapes[] = { |
| 130 | { 2048, 4, 2, 8, 0 }, { 768, 4, 2, 8, 0 }, { 4096, 4, 2, 8, 0 }, { 2048, 4, 1, 8, 0 }, { 2048, 2, 2, 4, 0 }, { 2048, 4, 2, 4, 0 }, { 768, 4, 2, 4, 0 }, |
| 131 | { 5120, 4, 2, 8, 0 }, { 17408, 4, 2, 8, 0 }, { 32768, 4, 2, 8, 0 }, /* Qwen3.8-27B widths and the new cap (omega#541) */ |
| 132 | }; |
| 133 | static BlackwellIRProgram prog; |
| 134 | for (size_t i = 0; i < sizeof shapes / sizeof shapes[0]; i++) { |
| 135 | int rc = omega_blackwell_codegen_tcgemm_prog(&shapes[i], &prog); |
| 136 | uint32_t peak = rc == 0 ? prog.regalloc.peak_gpr_usage : 0, decl = omega_bw_gpr_declared(peak); |
| 137 | int ok = rc == 0 && decl <= 128u && 32u * shapes[i].warps * decl <= 65536u; |
| 138 | printf("shape k %u rt %u ct %u warps %u: rc %d, %zu instructions, peak %u, declared %u, shared %u, span %u: %s\n", shapes[i].k, shapes[i].rt, shapes[i].ct, shapes[i].warps, |
| 139 | rc, rc == 0 ? prog.count : 0, peak, decl, omega_tcgemm_shared_bytes(&shapes[i]), omega_tcgemm_span(&shapes[i]), ok ? "ok" : "REFUSED"); |
| 140 | if (i < 2 && !ok) fail++; |
| 141 | } |
| 142 | /* emulation vs double oracle (x as stored bf16, true e4m3 and bf16 scale): relative to the sum of |terms| */ |
| 143 | Prob p; prob_make(&p, 3, 256, 512, 40, 17, 0); |
| 144 | float *y = malloc((size_t)p.M * p.n * 4); emulate(&p, y); |
| 145 | double worst = 0; |
| 146 | for (uint32_t i = 0; i < p.M; i++) for (uint32_t c = 0; c < p.n; c++) { |
| 147 | double s = 0, mag = 0; |
| 148 | for (uint32_t e = 0; e < p.k; e++) { |
| 149 | double wv = (double)u2f((uint32_t)omega_tcgemm_w_bf16(p.w[((size_t)p.eid[i] * p.n + c) * p.k + e]) << 16) * ldexp(1.0, 120); |
| 150 | double xv = (double)u2f((uint32_t)p.xp[(size_t)p.src[i] * p.k + stored_ix(e)] << 16) * ldexp(1.0, -64); |
| 151 | double sv = (double)u2f((uint32_t)p.sbf[((size_t)p.eid[i] * (p.n / 128) + c / 128) * (p.k / 128) + e / 128] << 16); |
| 152 | s += wv * xv * sv; mag += fabs(wv * xv * sv); |
| 153 | } |
| 154 | double rel = fabs(y[(size_t)i * p.n + c] - s) / (mag + 1e-30); if (rel > worst) worst = rel; |
| 155 | } |
| 156 | printf("emulation vs double oracle: worst |err| / sum|terms| = %.3g (%s, bound 1e-6)\n", worst, worst < 1e-6 ? "ok" : "FAIL"); |
| 157 | if (worst >= 1e-6) fail++; |
| 158 | free(y); prob_free(&p); |
| 159 | printf("host-only: %s\n", fail ? "FAIL" : "PASS"); |
| 160 | return fail; |
| 161 | } |
| 162 | |
| 163 | /* run the kernel in the simulator on p; returns 0 and fills y, or -1 with msg */ |
| 164 | static int sim_run(const Prob *p, const OmegaTcGemmSpec *sp, int order, float *y, char *msg, size_t msgn) { |
| 165 | static BlackwellIRProgram prog; |
| 166 | if (omega_blackwell_codegen_tcgemm_prog(sp, &prog) != 0) { snprintf(msg, msgn, "codegen refused"); return -1; } |
| 167 | const uint32_t bm = 16u * sp->rt, span = omega_tcgemm_span(sp); |
| 168 | if (p->n % span) { snprintf(msg, msgn, "n %% span"); return -1; } |
| 169 | Tile *tiles = calloc(p->M + 1, sizeof *tiles); |
| 170 | uint32_t nt = make_tiles(p, bm, tiles, p->w, p->sf); |
| 171 | uint32_t *ridx = malloc((p->M + bm) * 4); for (uint32_t i = 0; i < p->M + bm; i++) ridx[i] = i < p->M ? p->src[i] : 0; |
| 172 | for (size_t i = 0; i < (size_t)p->M * p->n; i++) y[i] = -999.0f; |
| 173 | uint8_t *smem = malloc(omega_tcgemm_shared_bytes(sp)); |
| 174 | static WsSim s; memset(&s, 0, sizeof s); |
| 175 | s.p = &prog; s.threads = 32u * sp->warps; s.smem = smem; s.smem_bytes = omega_tcgemm_shared_bytes(sp); s.order = order; |
| 176 | uint64_t a[5] = { (uintptr_t)tiles, (uintptr_t)ridx, (uintptr_t)p->xp, (uintptr_t)y, p->n }; |
| 177 | for (int i = 0; i < 4; i++) { s.cbank[(0x380 + 8 * i) / 4] = (uint32_t)a[i]; s.cbank[(0x380 + 8 * i) / 4 + 1] = (uint32_t)(a[i] >> 32); } |
| 178 | s.cbank[0x3a0 / 4] = p->n; |
| 179 | s.ranges[0].lo = (uintptr_t)tiles; s.ranges[0].hi = (uintptr_t)(tiles + nt); |
| 180 | s.ranges[1].lo = (uintptr_t)ridx; s.ranges[1].hi = (uintptr_t)(ridx + p->M + bm); |
| 181 | s.ranges[2].lo = (uintptr_t)p->xp; s.ranges[2].hi = (uintptr_t)(p->xp + (size_t)p->xr * p->k); |
| 182 | s.ranges[3].lo = (uintptr_t)y; s.ranges[3].hi = (uintptr_t)(y + (size_t)p->M * p->n); |
| 183 | s.ranges[4].lo = (uintptr_t)p->w; s.ranges[4].hi = (uintptr_t)(p->w + (size_t)p->E * p->n * p->k); |
| 184 | s.ranges[5].lo = (uintptr_t)p->sf; s.ranges[5].hi = (uintptr_t)(p->sf + (size_t)p->E * (p->n / 128) * (p->k / 128)); |
| 185 | s.nranges = 6; |
| 186 | int err = ws_run(&s, p->n / span, nt); |
| 187 | if (err) snprintf(msg, msgn, "simulator %s", s.msg); |
| 188 | free(tiles); free(ridx); free(smem); |
| 189 | return err ? -1 : 0; |
| 190 | } |
| 191 | |
| 192 | static int sim_tests(void) { |
| 193 | int fail = 0; |
| 194 | static const struct { uint32_t E, n, k, M, xr; int skew; OmegaTcGemmSpec sp; } pts[] = { |
| 195 | { 3, 256, 256, 40, 13, 0, { 256, 4, 2, 8, 0 } }, |
| 196 | { 2, 128, 384, 70, 9, 1, { 384, 4, 2, 8, 0 } }, /* three blocks, a multi-tile segment */ |
| 197 | { 4, 128, 128, 37, 11, 0, { 128, 2, 2, 4, 0 } }, |
| 198 | { 2, 256, 256, 20, 7, 0, { 256, 4, 1, 8, 0 } }, |
| 199 | { 2, 128, 256, 33, 5, 1, { 256, 1, 4, 4, 0 } }, |
| 200 | { 1, 512, 256, 64, 64, 0, { 256, 4, 2, 8, 0 } }, /* dense: one expert, four CTAs across n */ |
| 201 | { 1, 128, 17408, 64, 64, 0, { 17408, 4, 2, 8, 0 } }, /* Qwen3.8-27B down width (omega#541) */ |
| 202 | }; |
| 203 | for (size_t i = 0; i < sizeof pts / sizeof pts[0]; i++) { |
| 204 | Prob p; prob_make(&p, pts[i].E, pts[i].n, pts[i].k, pts[i].M, pts[i].xr, pts[i].skew); |
| 205 | float *ye = malloc((size_t)p.M * p.n * 4), *ys = malloc((size_t)p.M * p.n * 4), *y0 = malloc((size_t)p.M * p.n * 4); |
| 206 | emulate(&p, ye); |
| 207 | for (int order = 0; order < 2; order++) { |
| 208 | char msg[240] = ""; int rc = sim_run(&p, &pts[i].sp, order, ys, msg, sizeof msg); |
| 209 | size_t bad = 0, poison = 0; |
| 210 | if (rc == 0) for (size_t q = 0; q < (size_t)p.M * p.n; q++) { if (ys[q] == -999.0f) poison++; if (memcmp(&ys[q], &ye[q], 4)) bad++; } |
| 211 | if (order == 0 && rc == 0) memcpy(y0, ys, (size_t)p.M * p.n * 4); |
| 212 | int same = order == 0 || (rc == 0 && memcmp(y0, ys, (size_t)p.M * p.n * 4) == 0); |
| 213 | printf("sim E %u n %u k %u M %u rt %u ct %u warps %u order %d: %s %s%zu of %zu differ from the emulation, %zu unwritten%s\n", p.E, p.n, p.k, p.M, pts[i].sp.rt, pts[i].sp.ct, pts[i].sp.warps, order, |
| 214 | rc ? "ERROR" : (bad || poison || !same) ? "MISMATCH" : "ok", msg, bad, (size_t)p.M * p.n, poison, bad || poison ? "" : " (bit-identical)"); |
| 215 | if (rc || bad || poison || !same) fail++; |
| 216 | } |
| 217 | free(ye); free(ys); free(y0); prob_free(&p); |
| 218 | } |
| 219 | /* mutants: each must run cleanly in the simulator and differ from the emulation */ |
| 220 | for (int m = 1; m <= 4; m++) { |
| 221 | Prob p; prob_make(&p, 2, 128, 384, 50, 9, 1); |
| 222 | float *ye = malloc((size_t)p.M * p.n * 4), *ys = malloc((size_t)p.M * p.n * 4); |
| 223 | emulate(&p, ye); |
| 224 | OmegaTcGemmSpec sp = { 384, 4, 2, 8, m }; |
| 225 | char msg[240] = ""; int rc = sim_run(&p, &sp, 0, ys, msg, sizeof msg); |
| 226 | size_t bad = 0; if (rc == 0) for (size_t q = 0; q < (size_t)p.M * p.n; q++) if (memcmp(&ys[q], &ye[q], 4)) bad++; |
| 227 | printf("sim mutant %d: %s %s(%zu of %zu differ)\n", m, rc ? "ERROR (counts as FAIL)" : bad ? "caught" : "NOT CAUGHT", msg, bad, (size_t)p.M * p.n); |
| 228 | if (rc || !bad) fail++; |
| 229 | free(ye); free(ys); prob_free(&p); |
| 230 | } |
| 231 | printf("sim: %s\n", fail ? "FAIL" : "PASS"); |
| 232 | return fail; |
| 233 | } |
| 234 | |
| 235 | int chip_tests(int argc, char **argv); |
| 236 | |
| 237 | int main(int argc, char **argv) { |
| 238 | if (argc > 2 && strcmp(argv[1], "--dump") == 0) { /* raw code words for nvdisasm -b SM121 (offline check only): --dump file [k rt ct warps mutant] */ |
| 239 | static BlackwellIRProgram dp; static uint8_t buf[OMEGA_BW_CODE_CAP_MAX]; size_t len = 0; |
| 240 | OmegaTcGemmSpec sp = { argc > 3 ? (uint32_t)atoi(argv[3]) : 2048u, argc > 4 ? (uint32_t)atoi(argv[4]) : 4u, argc > 5 ? (uint32_t)atoi(argv[5]) : 2u, argc > 6 ? (uint32_t)atoi(argv[6]) : 8u, argc > 7 ? atoi(argv[7]) : 0 }; |
| 241 | if (omega_blackwell_codegen_tcgemm_prog(&sp, &dp) != 0 || omega_bw_encode_program(&dp, buf, sizeof buf, &len) != 0) return 1; |
| 242 | FILE *f = fopen(argv[2], "wb"); if (!f) return 1; fwrite(buf, 1, len, f); fclose(f); printf("peak %u, %zu bytes\n", dp.regalloc.peak_gpr_usage, len); return 0; |
| 243 | } |
| 244 | int host = 0, sim = 0, chip = 0; |
| 245 | for (int i = 1; i < argc; i++) { |
| 246 | if (!strcmp(argv[i], "--host-only")) host = 1; |
| 247 | else if (!strcmp(argv[i], "--sim")) sim = 1; |
| 248 | else if (!strcmp(argv[i], "--chip")) chip = 1; |
| 249 | } |
| 250 | if (!host && !sim && !chip) host = sim = 1; |
| 251 | int fail = 0; |
| 252 | if (host) fail += host_only(); |
| 253 | if (sim && !fail) fail += sim_tests(); |
| 254 | if (chip && !fail) fail += chip_tests(argc, argv); |
| 255 | printf("GPU_TCGEMM: %s\n", fail ? "FAIL" : "PASS"); |
| 256 | return fail ? 1 : 0; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | /* ------------------------------------------------------------- chip */ |
| 261 | /* route `tokens` tokens to top-8 of E experts (distinct, uniform), sorted by expert; dense (E == 1): one row per token */ |
| 262 | static void route(Prob *p, uint32_t tokens) { |
| 263 | if (p->E == 1) { for (uint32_t i = 0; i < p->M; i++) { p->eid[i] = 0; p->src[i] = i; } return; } |
| 264 | uint32_t *cnt = calloc(p->E, 4), *pe = malloc((size_t)tokens * 8 * 4); |
| 265 | for (uint32_t t = 0; t < tokens; t++) for (uint32_t j = 0; j < 8; j++) { |
| 266 | uint32_t e; int dup; do { e = rnd() % p->E; dup = 0; for (uint32_t q = 0; q < j; q++) if (pe[t * 8 + q] == e) dup = 1; } while (dup); |
| 267 | pe[t * 8 + j] = e; cnt[e]++; |
| 268 | } |
| 269 | uint32_t *off = calloc(p->E + 1, 4); for (uint32_t e = 0; e < p->E; e++) off[e + 1] = off[e] + cnt[e]; |
| 270 | for (uint32_t t = 0; t < tokens; t++) for (uint32_t j = 0; j < 8; j++) { uint32_t e = pe[t * 8 + j], at = off[e]++; p->eid[at] = e; p->src[at] = t; } |
| 271 | free(cnt); free(pe); free(off); |
| 272 | } |
| 273 | static void emulate_rows(const Prob *p, const uint32_t *rows, uint32_t nr, float *out) { /* emulate only the listed rows */ |
| 274 | Prob q = *p; q.M = 1; |
| 275 | for (uint32_t i = 0; i < nr; i++) { q.eid = p->eid + rows[i]; q.src = p->src + rows[i]; emulate(&q, out + (size_t)i * p->n); } |
| 276 | } |
| 277 | static 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; } |
| 278 | |
| 279 | int chip_tests(int argc, char **argv) { |
| 280 | const char *shape = "gateup", *outp = NULL; uint32_t tokens = 128, reps = 9, check = 384; int mut = 1; |
| 281 | for (int i = 1; i < argc; i++) { |
| 282 | if (!strcmp(argv[i], "--shape") && i + 1 < argc) shape = argv[++i]; |
| 283 | else if (!strcmp(argv[i], "--tokens") && i + 1 < argc) tokens = (uint32_t)atoi(argv[++i]); |
| 284 | else if (!strcmp(argv[i], "--reps") && i + 1 < argc) reps = (uint32_t)atoi(argv[++i]); |
| 285 | else if (!strcmp(argv[i], "--check-rows") && i + 1 < argc) check = (uint32_t)atoi(argv[++i]); |
| 286 | else if (!strcmp(argv[i], "--out") && i + 1 < argc) outp = argv[++i]; |
| 287 | else if (!strcmp(argv[i], "--no-mutants")) mut = 0; |
| 288 | } |
| 289 | uint32_t E, n, k; |
| 290 | if (!strcmp(shape, "gateup")) { E = 128; n = 1536; k = 2048; } else if (!strcmp(shape, "down")) { E = 128; n = 2048; k = 768; } |
| 291 | else if (!strcmp(shape, "qkv")) { E = 1; n = 5120; k = 2048; } else if (!strcmp(shape, "o")) { E = 1; n = 2048; k = 4096; } |
| 292 | /* Qwen3.8-27B dense shapes (omega#541): MLP gate or up, down (k 17408), DeltaNet in_proj_qkv, out_proj / o_proj */ |
| 293 | else if (!strcmp(shape, "q38gate")) { E = 1; n = 17408; k = 5120; } else if (!strcmp(shape, "q38down")) { E = 1; n = 5120; k = 17408; } |
| 294 | else if (!strcmp(shape, "q38qkv")) { E = 1; n = 10240; k = 5120; } else if (!strcmp(shape, "q38o")) { E = 1; n = 5120; k = 6144; } |
| 295 | else { printf("unknown shape %s\n", shape); return 1; } |
| 296 | const uint32_t M = E == 1 ? tokens : tokens * 8u; |
| 297 | if (reps > 64) reps = 64; |
| 298 | int fail = 0; |
| 299 | Prob p; prob_make(&p, E, n, k, M, tokens, 0); route(&p, tokens); |
| 300 | OmegaGpuFp8Tensor *t = NULL; |
| 301 | int rc = omega_gpu_fp8_tensor_upload(E, n, k, p.w, p.sbf, &t); |
| 302 | if (rc != 0) { printf("chip: upload failed %d: %s\n", rc, omega_gpu_tcgemm_last_error()); prob_free(&p); return 1; } |
| 303 | float *y = malloc((size_t)M * n * 4), *y2 = malloc((size_t)M * n * 4); |
| 304 | OmegaGpuTcGemmInfo info; |
| 305 | rc = omega_gpu_tcgemm_f32(t, M, p.eid, p.src, p.x, tokens, y, &info); |
| 306 | if (rc) { printf("chip: call failed %s: %s\n", omega_gpu_tcgemm_rc_name(rc), omega_gpu_tcgemm_last_error()); fail++; goto done; } |
| 307 | printf("chip %s: %u tokens, M %u rows, %u segments, %u tiles, grid %u x %u, rt %u ct %u warps %u, declared %u registers\n", shape, tokens, M, info.segments, info.tiles, info.grid_x, info.grid_y, info.rt, info.ct, info.warps, info.gpr_declared); |
| 308 | { /* bit-identity on sampled rows: the first and last rows of the table and a stride across it */ |
| 309 | uint32_t nr = check < M ? check : M, *rows = malloc(nr * 4); float *ye = malloc((size_t)nr * n * 4); size_t bad = 0; |
| 310 | for (uint32_t i = 0; i < nr; i++) rows[i] = nr == M ? i : i < 8 ? i : i >= nr - 8 ? M - (nr - i) : (uint32_t)((uint64_t)i * M / nr); |
| 311 | emulate_rows(&p, rows, nr, ye); |
| 312 | for (uint32_t i = 0; i < nr; i++) for (uint32_t c = 0; c < n; c++) if (memcmp(&y[(size_t)rows[i] * n + c], &ye[(size_t)i * n + c], 4)) bad++; |
| 313 | printf("chip bit-identity: %zu of %zu sampled outputs (%u rows) differ from the emulation%s\n", bad, (size_t)nr * n, nr, bad ? "" : " (bit-identical)"); |
| 314 | if (bad) fail++; |
| 315 | free(rows); free(ye); |
| 316 | } |
| 317 | rc = omega_gpu_tcgemm_f32(t, M, p.eid, p.src, p.x, tokens, y2, &info); |
| 318 | { int same = rc == 0 && memcmp(y, y2, (size_t)M * n * 4) == 0; printf("chip second call: %s\n", same ? "identical" : "DIFFERS"); if (!same) fail++; } |
| 319 | if (mut) for (int m = 1; m <= 4; m++) { |
| 320 | omega_gpu_tcgemm_test_set_mutant(m); |
| 321 | rc = omega_gpu_tcgemm_f32(t, M, p.eid, p.src, p.x, tokens, y2, NULL); |
| 322 | size_t b = 0; if (rc == 0) for (size_t q = 0; q < (size_t)M * n; q++) if (memcmp(&y[q], &y2[q], 4)) b++; |
| 323 | printf("chip mutant %d: %s (%zu of %zu differ)%s%s\n", m, rc ? "ERROR (counts as FAIL)" : b ? "caught" : "NOT CAUGHT", b, (size_t)M * n, rc ? " " : "", rc ? omega_gpu_tcgemm_last_error() : ""); |
| 324 | if (rc || !b) fail++; |
| 325 | } |
| 326 | omega_gpu_tcgemm_test_set_mutant(0); |
| 327 | { |
| 328 | uint64_t ce[64], cc[64]; uint32_t done = 0; |
| 329 | for (uint32_t r = 0; r < reps; r++) { if (omega_gpu_tcgemm_f32(t, M, p.eid, p.src, p.x, tokens, y2, &info) == 0) { ce[done] = info.elapsed_ns; cc[done] = info.call_ns; done++; } } |
| 330 | if (done != reps) { printf("chip timing: %u of %u calls failed\n", reps - done, reps); fail++; } |
| 331 | if (done) { |
| 332 | qsort(ce, done, 8, cmp_u64); qsort(cc, done, 8, cmp_u64); |
| 333 | double ns = (double)ce[done / 2], flops = 2.0 * M * n * k; |
| 334 | printf("TCGEMM %s (medians of %u): chip %.1f us, call %.1f us; %.2f TFLOPS; weights streamed %.0f MB (%.1f GB/s), activations gathered %.0f MB, output %.0f MB; total %.1f GB/s\n", shape, done, ns / 1e3, cc[done / 2] / 1e3, |
| 335 | flops / ns / 1e3, info.streamed_bytes / 1e6, info.streamed_bytes / ns, info.act_bytes / 1e6, (double)M * n * 4 / 1e6, (info.streamed_bytes + info.act_bytes + (double)M * n * 4) / ns); |
| 336 | if (outp) { FILE *f = fopen(outp, "w"); if (f) { fprintf(f, "{\"shape\":\"%s\",\"tokens\":%u,\"rows\":%u,\"tiles\":%u,\"reps\":%u,\"chip_median_ns\":%.0f,\"call_median_ns\":%" PRIu64 ",\"tflops\":%.3f,\"streamed_bytes\":%" PRIu64 ",\"act_bytes\":%" PRIu64 ",\"fail\":%d}\n", |
| 337 | shape, tokens, M, info.tiles, done, ns, cc[done / 2], flops / ns / 1e3, info.streamed_bytes, info.act_bytes, fail); fclose(f); } } |
| 338 | } |
| 339 | } |
| 340 | done: |
| 341 | free(y); free(y2); omega_gpu_fp8_tensor_free(t); prob_free(&p); |
| 342 | printf("chip: %s\n", fail ? "FAIL" : "PASS"); |
| 343 | return fail; |
| 344 | } |