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