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 / evidence / OWN30B-REQUAL-MAIN-710d438c-20261015 / gates / cut4b / FB1-CUT4B-710d438c / gpu_matmul_api_test.c
| 1 | /* gpu_matmul_api_test: FB-1 cut 1 / 1b gate for omega_gpu_matmul_api. |
| 2 | * |
| 3 | * ./gpu_matmul_api_test --host-only argument and limit refusals, codegen only, no chip |
| 4 | * ./gpu_matmul_api_test --out receipt.json chip sweep over shapes, JSON receipt |
| 5 | * ./gpu_matmul_api_test --timing --out r.json resident GEMV timing (median of 100 calls) + parity |
| 6 | * ./gpu_matmul_api_test --mutant --out r.json wrong kernel (last K step dropped) must FAIL parity; |
| 7 | * exit 0 when every shape caught it ("mutant killed") |
| 8 | * ./gpu_matmul_api_test --cta-budget N ... run with a different CTA budget (I42 experiment) |
| 9 | * ./gpu_matmul_api_test --spin-us N ... marker-wait spin window for every launch (omega#328) |
| 10 | * ./gpu_matmul_api_test --unroll N ... force the fragment kernel unroll (omega #328 experiment) |
| 11 | * |
| 12 | * Shapes come from OMEGA_GPU_MATMUL_SHAPES ("m,k,n;m,k,n;..."), default below. |
| 13 | * Pass rule per shape: rc OK, parity verified by the API oracle, end-to-end error < 1e-5 relative to |
| 14 | * the accumulation scale (bf16 inputs against a double reference; only accumulation order and the |
| 15 | * chip's f32 accumulator differ), second call hits the kernel cache and agrees bit for bit. |
| 16 | */ |
| 17 | #include "omega_gpu_matmul_api.h" |
| 18 | #include "omega_gpu_session.h" |
| 19 | #include "omega_blackwell_codegen.h" |
| 20 | #include "omega_blackwell_matmul.h" |
| 21 | #include <inttypes.h> |
| 22 | #include <math.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <time.h> |
| 27 | |
| 28 | /* round a float to bf16 and back, as the API does on the way in */ |
| 29 | static float bf16r(float f) { uint16_t h = omega_fp32_to_bf16(f); uint32_t u = (uint32_t)h << 16; float o; memcpy(&o, &u, 4); return o; } |
| 30 | |
| 31 | static int g_checks, g_failed; |
| 32 | #define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) |
| 33 | |
| 34 | /* cut-1 list unchanged, then the cut-1b Llama shapes (hidden 2048, ffn 8192, vocab 128256) */ |
| 35 | static const char *DEFAULT_SHAPES = |
| 36 | "16,16,16;32,32,32;64,64,64;128,128,128;256,256,256;512,512,512;" |
| 37 | "1,1024,1024;1,256,1024;16,1024,256;8,1024,8;" |
| 38 | "1024,16,128;192,16,192;512,16,64;256,16,256;128,16,512;" |
| 39 | "1,2048,8192;1,8192,2048;1,2048,128256"; |
| 40 | static const char *MUTANT_SHAPES = "16,32,16;1,2048,8192;64,64,64;1,1024,1024"; |
| 41 | static const char *TIMING_SHAPES = "1,2048,8192;1,8192,2048"; |
| 42 | |
| 43 | static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } |
| 44 | static double now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; } |
| 45 | |
| 46 | static void fill(float *a, size_t n, uint32_t *seed) { for (size_t i = 0; i < n; i++) a[i] = ((float)(lcg(seed) % 2001) - 1000.0f) / 1000.0f; } |
| 47 | |
| 48 | /* end-to-end: full-K product on the host from the same bf16-rounded inputs, in double; error relative |
| 49 | * to the accumulation scale (sum of |terms|), not to a result that may cancel to near zero */ |
| 50 | static double e2e_max_rel(uint32_t m, uint32_t k, uint32_t n, const float *a, const float *b, const float *c, size_t *bad) { |
| 51 | double worst = 0.0; size_t nbad = 0; |
| 52 | for (size_t i = 0; i < m; i++) for (size_t j = 0; j < n; j++) { |
| 53 | double acc = 0.0, scale = 0.0; |
| 54 | for (size_t t = 0; t < k; t++) { double p = (double)bf16r(a[i * k + t]) * (double)bf16r(b[t * n + j]); acc += p; scale += fabs(p); } |
| 55 | double rel = fabs(acc - (double)c[i * n + j]) / (scale > 1e-6 ? scale : 1e-6); |
| 56 | if (rel > worst) worst = rel; |
| 57 | if (rel >= 1e-5) nbad++; |
| 58 | } |
| 59 | if (bad) *bad = nbad; |
| 60 | return worst; |
| 61 | } |
| 62 | |
| 63 | /* omega #328 fragment-major kernel, host only. The unpack below is written from the PTX ISA |
| 64 | * table for mma.m16n8k16 (.bf16 A: a0,a1 row g cols 2t,2t+1; a2,a3 row g+8; a4,a5 row g cols |
| 65 | * 2t+8,2t+9; a6,a7 row g+8. B: b0,b1 rows 2t,2t+1 col g; b2,b3 rows 2t+8,2t+9; g = lane>>2, |
| 66 | * t = lane&3), independently of the packers: pack then unpack must give the matrix back. */ |
| 67 | /* Result sentinels: one poison word per 16 x 8 fragment tile that holds a real output word, inside the |
| 68 | * tile's real rows and columns; the scan counts exactly the survivors. */ |
| 69 | static void sentinel_host_checks(void) { |
| 70 | static const uint32_t shapes[][2] = { { 1u, 24576u }, { 1u, 4096u }, { 16u, 151936u }, { 128u, 24576u }, { 116u, 4096u }, { 3u, 20u }, { 17u, 9u } }; |
| 71 | const float poison = -999.0f; |
| 72 | for (size_t s = 0; s < sizeof shapes / sizeof shapes[0]; s++) { |
| 73 | const uint32_t m = shapes[s][0], n = shapes[s][1], mp = (m + 15u) / 16u * 16u, np = (n + 7u) / 8u * 8u; |
| 74 | const uint32_t mt = (m + 15u) / 16u, nt = (n + 7u) / 8u; |
| 75 | float *cd = calloc((size_t)mp * np, sizeof *cd); |
| 76 | const size_t set = omega_matmul_result_sentinels(cd, m, n, np, poison, 1); |
| 77 | CHECK(set == (size_t)mt * nt, "sentinel count m=%u n=%u: %zu != %u", m, n, set, mt * nt); |
| 78 | /* every fragment tile with a real word holds exactly one sentinel, inside its real rows and columns */ |
| 79 | size_t tiles_ok = 0, outside = 0; |
| 80 | for (uint32_t ti = 0; ti < mt; ti++) |
| 81 | for (uint32_t tj = 0; tj < nt; tj++) { |
| 82 | size_t in_tile = 0; |
| 83 | for (uint32_t r = ti * 16u; r < ti * 16u + 16u; r++) |
| 84 | for (uint32_t c = tj * 8u; c < tj * 8u + 8u; c++) |
| 85 | if (cd[(size_t)r * np + c] == poison) { if (r < m && c < n) in_tile++; else outside++; } |
| 86 | if (in_tile == 1) tiles_ok++; |
| 87 | } |
| 88 | CHECK(tiles_ok == (size_t)mt * nt && outside == 0, "sentinel placement m=%u n=%u: %zu tiles ok, %zu outside", m, n, tiles_ok, outside); |
| 89 | CHECK(omega_matmul_result_sentinels(cd, m, n, np, poison, 0) == set, "sentinel scan m=%u n=%u: all survive", m, n); |
| 90 | /* clearing one tile's sentinel removes exactly one survivor; a fully written result has none */ |
| 91 | cd[(size_t)(m - 1u) * np + (n - 1u)] = 0.0f; |
| 92 | CHECK(omega_matmul_result_sentinels(cd, m, n, np, poison, 0) == set - 1u, "sentinel scan m=%u n=%u: one cleared", m, n); |
| 93 | for (size_t i = 0; i < (size_t)mp * np; i++) cd[i] = 1.5f; |
| 94 | CHECK(omega_matmul_result_sentinels(cd, m, n, np, poison, 0) == 0, "sentinel scan m=%u n=%u: none survive", m, n); |
| 95 | free(cd); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | static void frag_host_checks(void) { |
| 100 | sentinel_host_checks(); |
| 101 | /* row-0 pack (one-row decode call): a full one-row pack of A2 equals a full pack of A1 followed by the |
| 102 | * row-0 pack of A2, bf16 and f32 inputs, including a width with padding columns */ |
| 103 | { |
| 104 | const uint32_t ks[] = { 4096u, 40u }; |
| 105 | for (size_t i = 0; i < 2; i++) { |
| 106 | const uint32_t k = ks[i], kp = (k + 15u) / 16u * 16u; |
| 107 | float *a1 = malloc(k * sizeof *a1), *a2 = malloc(k * sizeof *a2); |
| 108 | uint16_t *b1 = malloc(k * 2), *b2 = malloc(k * 2), *full = malloc((size_t)16 * kp * 2), *inc = malloc((size_t)16 * kp * 2); |
| 109 | uint32_t seed = 77u + k; |
| 110 | fill(a1, k, &seed); fill(a2, k, &seed); |
| 111 | for (uint32_t j = 0; j < k; j++) { b1[j] = omega_fp32_to_bf16(a1[j]); b2[j] = omega_fp32_to_bf16(a2[j]); } |
| 112 | omega_matmul_frag_pack_a(b2, 1, k, 16, kp, full); |
| 113 | omega_matmul_frag_pack_a(b1, 1, k, 16, kp, inc); |
| 114 | omega_matmul_frag_pack_a_row0(b2, k, kp, inc); |
| 115 | CHECK(memcmp(full, inc, (size_t)16 * kp * 2) == 0, "row-0 pack bf16 k=%u equals the full one-row pack", k); |
| 116 | omega_matmul_frag_pack_a_f32(a1, 1, k, 16, kp, inc); |
| 117 | omega_matmul_frag_pack_a_row0_f32(a2, k, kp, inc); |
| 118 | CHECK(memcmp(full, inc, (size_t)16 * kp * 2) == 0, "row-0 pack f32 k=%u equals the full one-row pack", k); |
| 119 | free(a1); free(a2); free(b1); free(b2); free(full); free(inc); |
| 120 | } |
| 121 | } |
| 122 | CHECK(omega_blackwell_verify_codegen_fixtures_fragmm() == 0, "fragment matmul golden words (rc %d)", omega_blackwell_verify_codegen_fixtures_fragmm()); |
| 123 | struct { uint32_t m, k, n; } sh[] = { {1, 16, 8}, {1, 40, 13}, {17, 48, 24}, {33, 2560, 72}, {5, 160, 1000} }; |
| 124 | for (size_t s = 0; s < sizeof sh / sizeof sh[0]; s++) { |
| 125 | const uint32_t m = sh[s].m, k = sh[s].k, n = sh[s].n; |
| 126 | const uint32_t mp = (m + 15) / 16 * 16, kp = (k + 15) / 16 * 16, np = (n + 7) / 8 * 8, KS = kp / 16, NT = np / 8; |
| 127 | uint16_t *a = malloc((size_t)m * k * 2), *b = malloc((size_t)k * n * 2); |
| 128 | uint16_t *pa = malloc((size_t)mp * kp * 2), *pb = malloc((size_t)kp * np * 2); |
| 129 | uint32_t seed = 7 + (uint32_t)s; |
| 130 | for (size_t i = 0; i < (size_t)m * k; i++) a[i] = (uint16_t)(1 + lcg(&seed) % 60000); |
| 131 | for (size_t i = 0; i < (size_t)k * n; i++) b[i] = (uint16_t)(1 + lcg(&seed) % 60000); |
| 132 | omega_matmul_frag_pack_a(a, m, k, mp, kp, pa); |
| 133 | /* B in two uneven chunks, as the upload does in FRAG_PACK_TILES steps */ |
| 134 | uint32_t split = NT / 2; |
| 135 | omega_matmul_frag_pack_b(b, k, n, kp, 0, split, pb); |
| 136 | omega_matmul_frag_pack_b(b, k, n, kp, split, NT, pb + (size_t)split * kp * 8); |
| 137 | size_t bad_a = 0, bad_b = 0; |
| 138 | for (uint32_t mt = 0; mt < mp / 16; mt++) |
| 139 | for (uint32_t ks = 0; ks < KS; ks++) |
| 140 | for (uint32_t lane = 0; lane < 32; lane++) { |
| 141 | const uint32_t gg = lane >> 2, t = lane & 3; |
| 142 | const uint32_t rows[8] = { gg, gg, gg + 8, gg + 8, gg, gg, gg + 8, gg + 8 }; |
| 143 | const uint32_t cols[8] = { 2 * t, 2 * t + 1, 2 * t, 2 * t + 1, 2 * t + 8, 2 * t + 9, 2 * t + 8, 2 * t + 9 }; |
| 144 | for (int e = 0; e < 8; e++) { |
| 145 | uint32_t r = mt * 16 + rows[e], c = ks * 16 + cols[e]; |
| 146 | uint16_t want = (r < m && c < k) ? a[(size_t)r * k + c] : 0; |
| 147 | if (pa[(((size_t)mt * KS + ks) * 32 + lane) * 8 + e] != want) bad_a++; |
| 148 | } |
| 149 | } |
| 150 | for (uint32_t nt = 0; nt < NT; nt++) |
| 151 | for (uint32_t ks = 0; ks < KS; ks++) |
| 152 | for (uint32_t lane = 0; lane < 32; lane++) { |
| 153 | const uint32_t gg = lane >> 2, t = lane & 3; |
| 154 | const uint32_t rows[4] = { 2 * t, 2 * t + 1, 2 * t + 8, 2 * t + 9 }; |
| 155 | for (int e = 0; e < 4; e++) { |
| 156 | uint32_t r = ks * 16 + rows[e], c = nt * 8 + gg; |
| 157 | uint16_t want = (r < k && c < n) ? b[(size_t)r * n + c] : 0; |
| 158 | if (pb[(((size_t)nt * KS + ks) * 32 + lane) * 4 + e] != want) bad_b++; |
| 159 | } |
| 160 | } |
| 161 | CHECK(bad_a == 0 && bad_b == 0, "fragment pack round trip %ux%ux%u (A bad %zu, B bad %zu)", m, k, n, bad_a, bad_b); |
| 162 | /* the fused f32 packer equals convert-then-pack bit for bit (ties, denormals, inf, NaN included) */ |
| 163 | float *af = malloc((size_t)m * k * sizeof *af); |
| 164 | uint16_t *pf = malloc((size_t)mp * kp * 2); |
| 165 | for (size_t i = 0; i < (size_t)m * k; i++) { |
| 166 | uint32_t bits = lcg(&seed) * 2654435761u; |
| 167 | switch (i % 7) { |
| 168 | case 0: bits = (bits & 0x807fffffu) | 0x3f800000u; break; /* in [1,2) with every mantissa bit live (rounding ties) */ |
| 169 | case 1: bits = 0x7f800000u | ((bits >> 9) & 0x7fffffu); break; /* inf (0 payload) or NaN payloads */ |
| 170 | case 2: bits &= 0x807fffffu; break; /* zero and denormals */ |
| 171 | default: break; |
| 172 | } |
| 173 | memcpy(&af[i], &bits, 4); |
| 174 | a[i] = omega_fp32_to_bf16(af[i]); |
| 175 | } |
| 176 | omega_matmul_frag_pack_a(a, m, k, mp, kp, pa); |
| 177 | omega_matmul_frag_pack_a_f32(af, m, k, mp, kp, pf); |
| 178 | CHECK(memcmp(pa, pf, (size_t)mp * kp * 2) == 0, "fused f32 pack equals convert-then-pack %ux%u", m, k); |
| 179 | free(af); free(pf); |
| 180 | free(a); free(b); free(pa); free(pb); |
| 181 | } |
| 182 | /* the kernel generates for every padded Qwen3-4B and Llama-3.2-1B decode shape at the API's unroll */ |
| 183 | struct { uint32_t k, n, gx; } ok[] = { {16, 8, 1}, {2560, 4096, 256}, {2560, 1024, 128}, {4096, 2560, 256}, {2560, 9728, 256}, |
| 184 | {9728, 2560, 256}, {2560, 151936, 256}, {2048, 8192, 64}, {8192, 2048, 64}, |
| 185 | {2048, 128256, 64}, {16384, 262144, 64}, {48, 16, 2} }; |
| 186 | for (size_t i = 0; i < sizeof ok / sizeof ok[0]; i++) { |
| 187 | OmegaMatMulSpec spec; OmegaBlackwellKernel kernel; memset(&kernel, 0, sizeof kernel); |
| 188 | const uint32_t u = omega_matmul_frag_unroll(ok[i].k); |
| 189 | int rc = omega_matmul_spec_init(&spec, 16, ok[i].k, ok[i].n, OMEGA_MATMUL_PRECISION_BF16) == 0 && |
| 190 | omega_blackwell_codegen_matmul_frag(&spec, ok[i].gx, u, 0, &kernel) == 0 && kernel.code_size > 0; |
| 191 | CHECK(rc, "fragment kernel K=%u N=%u gx=%u unroll=%u generated", ok[i].k, ok[i].n, ok[i].gx, u); |
| 192 | CHECK(kernel.gpr_count <= 64, "fragment kernel K=%u N=%u unroll=%u fits 64 registers (got %u)", ok[i].k, ok[i].n, u, kernel.gpr_count); |
| 193 | if (i == 1) printf("fragment kernel K=2560 N=4096 unroll %u: %zu instructions, %u registers\n", u, kernel.insn_count, kernel.gpr_count); |
| 194 | OmegaBlackwellKernel mut; memset(&mut, 0, sizeof mut); |
| 195 | if (omega_blackwell_codegen_matmul_frag(&spec, ok[i].gx, u, 1, &mut) == 0) { |
| 196 | CHECK(memcmp(mut.code_digest, kernel.code_digest, 32) != 0, "fragment mutant differs K=%u N=%u", ok[i].k, ok[i].n); |
| 197 | omega_blackwell_kernel_free(&mut); |
| 198 | } else CHECK(ok[i].k / 16 / u < 2, "fragment mutant refused only with one pass (K=%u)", ok[i].k); |
| 199 | omega_blackwell_kernel_free(&kernel); |
| 200 | } |
| 201 | CHECK(omega_matmul_frag_unroll(2560) == 4 && omega_matmul_frag_unroll(48) == 1 && omega_matmul_frag_unroll(64) == 4 && |
| 202 | omega_matmul_frag_unroll(32) == 2 && omega_matmul_frag_unroll(128) == 4, "unroll choice (capped at 4)"); |
| 203 | { /* unroll 8 still generates (experiments) */ |
| 204 | OmegaMatMulSpec s8; OmegaBlackwellKernel k8; memset(&k8, 0, sizeof k8); |
| 205 | CHECK(omega_matmul_spec_init(&s8, 16, 2560, 4096, OMEGA_MATMUL_PRECISION_BF16) == 0 && |
| 206 | omega_blackwell_codegen_matmul_frag(&s8, 256, 8, 0, &k8) == 0 && k8.code_size > 0, "unroll 8 generates"); |
| 207 | omega_blackwell_kernel_free(&k8); |
| 208 | } |
| 209 | OmegaMatMulSpec spec; OmegaBlackwellKernel kernel; memset(&kernel, 0, sizeof kernel); |
| 210 | CHECK(omega_matmul_spec_init(&spec, 16, 48, 16, OMEGA_MATMUL_PRECISION_BF16) == 0 && |
| 211 | omega_blackwell_codegen_matmul_frag(&spec, 1, 2, 0, &kernel) != 0, "unroll not dividing K/16 refused"); |
| 212 | CHECK(omega_blackwell_codegen_matmul_frag(&spec, 1, 3, 0, &kernel) != 0, "unroll 3 refused"); |
| 213 | CHECK(omega_blackwell_codegen_matmul_frag(&spec, 3, 1, 0, &kernel) != 0, "fragment grid_x beyond the column tiles refused"); |
| 214 | /* 2026-10-12: the blocked kernel (rt row tiles x ct column tiles per warp) generates for the Qwen3-8B |
| 215 | * prefill shapes at m = 128 (rt 4, ct 4, grid_x 512 at the daemon's 1024-CTA budget) and the small |
| 216 | * parity shapes; it fits the allocator's 128 registers; its mutant differs; the envelope is enforced */ |
| 217 | struct { uint32_t k, n, gx, rt, ct; } bk[] = { {4096, 24576, 512, 4, 4}, {12288, 4096, 128, 4, 4}, {4096, 6144, 192, 4, 4}, |
| 218 | {128, 128, 4, 4, 4}, {32, 32, 1, 2, 4}, {16, 8, 1, 2, 1}, {1024, 8, 1, 4, 1}, |
| 219 | {64, 64, 2, 4, 2}, {48, 16, 1, 2, 2} }; |
| 220 | for (size_t i = 0; i < sizeof bk / sizeof bk[0]; i++) { |
| 221 | OmegaMatMulSpec spec; OmegaBlackwellKernel kernel; memset(&kernel, 0, sizeof kernel); |
| 222 | const uint32_t u = omega_matmul_frag_block_unroll(bk[i].k, bk[i].rt, bk[i].ct); |
| 223 | int rc = omega_matmul_spec_init(&spec, 16, bk[i].k, bk[i].n, OMEGA_MATMUL_PRECISION_BF16) == 0 && |
| 224 | omega_blackwell_codegen_matmul_frag_block(&spec, bk[i].gx, u, bk[i].rt, bk[i].ct, 0, &kernel) == 0 && kernel.code_size > 0; |
| 225 | CHECK(rc, "blocked kernel K=%u N=%u gx=%u rt=%u ct=%u unroll=%u generated", bk[i].k, bk[i].n, bk[i].gx, bk[i].rt, bk[i].ct, u); |
| 226 | CHECK(u * (bk[i].rt + bk[i].ct) <= 8, "blocked kernel K=%u rt=%u ct=%u issues at most 8 loads per pass (unroll %u)", bk[i].k, bk[i].rt, bk[i].ct, u); |
| 227 | if (i < 3) printf("blocked kernel K=%u N=%u rt=%u ct=%u unroll %u: %zu instructions, %u registers\n", bk[i].k, bk[i].n, bk[i].rt, bk[i].ct, u, kernel.insn_count, kernel.gpr_count); |
| 228 | OmegaBlackwellKernel mut; memset(&mut, 0, sizeof mut); |
| 229 | if (omega_blackwell_codegen_matmul_frag_block(&spec, bk[i].gx, u, bk[i].rt, bk[i].ct, 1, &mut) == 0) { |
| 230 | CHECK(memcmp(mut.code_digest, kernel.code_digest, 32) != 0, "blocked mutant differs K=%u N=%u", bk[i].k, bk[i].n); |
| 231 | omega_blackwell_kernel_free(&mut); |
| 232 | } else CHECK(bk[i].k / 16 / u < 2, "blocked mutant refused only with one pass (K=%u)", bk[i].k); |
| 233 | omega_blackwell_kernel_free(&kernel); |
| 234 | } |
| 235 | { |
| 236 | uint32_t rt, ct; |
| 237 | omega_matmul_frag_block_shape(8, 3072, &rt, &ct); CHECK(rt == 4 && ct == 4, "block shape m=128 n=24576 is 4x4"); |
| 238 | omega_matmul_frag_block_shape(1, 3072, &rt, &ct); CHECK(rt == 1 && ct == 1, "block shape m=1 is the single-tile kernel"); |
| 239 | omega_matmul_frag_block_shape(7, 3072, &rt, &ct); CHECK(rt == 1 && ct == 1, "block shape m=112 (7 row tiles) is the single-tile kernel"); |
| 240 | omega_matmul_frag_block_shape(6, 6, &rt, &ct); CHECK(rt == 2 && ct == 2, "block shape 6 row tiles, 6 column tiles is 2x2"); |
| 241 | omega_matmul_frag_block_shape(4, 1, &rt, &ct); CHECK(rt == 4 && ct == 1, "block shape 4 row tiles, 1 column tile is 4x1"); |
| 242 | CHECK(omega_matmul_frag_block_unroll(4096, 4, 4) == 1 && omega_matmul_frag_block_unroll(4096, 2, 2) == 2 && |
| 243 | omega_matmul_frag_block_unroll(48, 2, 2) == 1 && omega_matmul_frag_block_unroll(4096, 4, 2) == 1, "blocked unroll rule (loads per pass <= 8)"); |
| 244 | OmegaMatMulSpec s; OmegaBlackwellKernel kk; memset(&kk, 0, sizeof kk); |
| 245 | CHECK(omega_matmul_spec_init(&s, 16, 4096, 6144, OMEGA_MATMUL_PRECISION_BF16) == 0, "spec for refusals"); |
| 246 | CHECK(omega_blackwell_codegen_matmul_frag_block(&s, 1, 1, 1, 1, 0, &kk) != 0, "blocked 1x1 refused (single-tile kernel)"); |
| 247 | CHECK(omega_blackwell_codegen_matmul_frag_block(&s, 1, 1, 3, 2, 0, &kk) != 0, "blocked rt 3 refused"); |
| 248 | CHECK(omega_blackwell_codegen_matmul_frag_block(&s, 1, 1, 8, 4, 0, &kk) != 0, "blocked 8x4 (32 accumulators) refused"); |
| 249 | CHECK(omega_blackwell_codegen_matmul_frag_block(&s, 1, 1, 8, 1, 0, &kk) != 0, "blocked 8x1 (9 loads per pass) refused"); |
| 250 | CHECK(omega_blackwell_codegen_matmul_frag_block(&s, 1, 2, 4, 4, 0, &kk) != 0, "blocked unroll 2 at 4x4 (16 loads per pass) refused"); |
| 251 | CHECK(omega_blackwell_codegen_matmul_frag_block(&s, 1, 4, 2, 2, 0, &kk) != 0, "blocked unroll 4 refused"); |
| 252 | CHECK(omega_blackwell_codegen_matmul_frag_block(&s, 193, 1, 4, 4, 0, &kk) != 0, "blocked grid_x beyond the column blocks refused"); |
| 253 | OmegaMatMulSpec s3; CHECK(omega_matmul_spec_init(&s3, 16, 64, 24, OMEGA_MATMUL_PRECISION_BF16) == 0, "spec n=24"); |
| 254 | CHECK(omega_blackwell_codegen_matmul_frag_block(&s3, 1, 1, 2, 4, 0, &kk) != 0, "blocked ct not dividing the column tiles refused"); |
| 255 | CHECK(omega_blackwell_codegen_matmul_frag_block(&s3, 1, 1, 2, 2, 0, &kk) != 0, "blocked ct 2 on 3 column tiles refused"); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | static void host_only(void) { |
| 260 | float a[16 * 16] = {0}, b[16 * 16] = {0}, c[16 * 16] = {0}; |
| 261 | OmegaGpuMatmulInfo info; |
| 262 | CHECK(omega_gpu_matmul_f32(0, 16, 16, a, b, c, &info) == OMEGA_GPU_MATMUL_BAD_ARGS, "m=0 refused"); |
| 263 | CHECK(omega_gpu_matmul_f32(16, 16, 16, NULL, b, c, &info) == OMEGA_GPU_MATMUL_BAD_ARGS, "NULL a refused"); |
| 264 | CHECK(omega_gpu_matmul_f32(OMEGA_BW_MATMUL_MAX_M + 1, 16, 16, a, b, c, &info) == OMEGA_GPU_MATMUL_TOO_LARGE, "m over limit refused"); |
| 265 | CHECK(omega_gpu_matmul_bf16(16, OMEGA_BW_MATMUL_MAX_K + 1, 16, (uint16_t *)a, (uint16_t *)b, c, NULL) == OMEGA_GPU_MATMUL_TOO_LARGE, "k over limit refused (NULL info ok)"); |
| 266 | /* session spin window (omega#328): in-range value kept, out-of-range refused unchanged, 0 restores */ |
| 267 | const uint32_t spin0 = omega_gpu_session_spin_us(); |
| 268 | CHECK(omega_gpu_session_set_spin_us(2000) == 0 && omega_gpu_session_spin_us() == 2000, "spin window 2000 us set"); |
| 269 | CHECK(omega_gpu_session_set_spin_us(OMEGA_GPU_SESSION_MAX_SPIN_US + 1) == -1 && omega_gpu_session_spin_us() == 2000, |
| 270 | "spin window over the maximum refused, setting unchanged"); |
| 271 | CHECK(omega_gpu_session_set_spin_us(0) == 0 && omega_gpu_session_spin_us() == 0, "spin window 0 restores the sleep-poll"); |
| 272 | omega_gpu_session_set_spin_us(spin0); |
| 273 | CHECK(omega_gpu_matmul_bf16(16, 16, OMEGA_BW_MATMUL_MAX_N + 1, (uint16_t *)a, (uint16_t *)b, c, NULL) == OMEGA_GPU_MATMUL_TOO_LARGE, "n over limit refused"); |
| 274 | CHECK(OMEGA_BW_MATMUL_MAX_N >= 128256 && OMEGA_BW_MATMUL_MAX_K >= 8192 && OMEGA_BW_MATMUL_MAX_M >= 2048, "limits cover Llama shapes (vocab 128256, ffn 8192)"); |
| 275 | OmegaGpuTensor *t = NULL; |
| 276 | CHECK(omega_gpu_tensor_upload_f32(0, 16, a, &t) == OMEGA_GPU_MATMUL_BAD_ARGS && t == NULL, "tensor k=0 refused"); |
| 277 | CHECK(omega_gpu_tensor_upload_f32(16, 16, NULL, &t) == OMEGA_GPU_MATMUL_BAD_ARGS, "tensor NULL refused"); |
| 278 | CHECK(omega_gpu_matmul_resident_f32(16, a, NULL, c, &info) == OMEGA_GPU_MATMUL_BAD_ARGS, "resident NULL tensor refused"); |
| 279 | CHECK(strcmp(omega_gpu_matmul_rc_name(OMEGA_GPU_MATMUL_PARITY_FAIL), "PARITY_FAIL") == 0, "rc name"); |
| 280 | CHECK(omega_gpu_matmul_cta_budget() == OMEGA_GPU_MATMUL_MAX_CTAS, "default CTA budget"); |
| 281 | CHECK(omega_gpu_matmul_is_blocked() == 0, "not blocked at start"); |
| 282 | |
| 283 | /* codegen only: the looped kernel must accept every padded Llama shape without touching the chip */ |
| 284 | struct { uint32_t k, n, gx; } ok[] = { {16, 8, 1}, {2048, 8192, 64}, {8192, 2048, 64}, {2048, 128256, 64}, {1024, 1024, 1}, {16384, 262144, 64} }; |
| 285 | for (size_t i = 0; i < sizeof ok / sizeof ok[0]; i++) { |
| 286 | OmegaMatMulSpec spec; OmegaBlackwellKernel kernel; memset(&kernel, 0, sizeof kernel); |
| 287 | int rc = omega_matmul_spec_init(&spec, 16, ok[i].k, ok[i].n, OMEGA_MATMUL_PRECISION_BF16) == 0 && |
| 288 | omega_blackwell_codegen_matmul_tensor_loop(&spec, ok[i].gx, 0, &kernel) == 0 && kernel.code_size > 0; |
| 289 | CHECK(rc, "loop kernel K=%u N=%u gx=%u generated", ok[i].k, ok[i].n, ok[i].gx); |
| 290 | CHECK(kernel.gpr_count <= 64, "loop kernel K=%u N=%u fits 64 registers (got %u)", ok[i].k, ok[i].n, kernel.gpr_count); |
| 291 | /* the mutant kernel is a different program */ |
| 292 | OmegaBlackwellKernel mut; memset(&mut, 0, sizeof mut); |
| 293 | if (omega_blackwell_codegen_matmul_tensor_loop(&spec, ok[i].gx, 1, &mut) == 0) { |
| 294 | CHECK(memcmp(mut.code_digest, kernel.code_digest, 32) != 0, "mutant kernel differs K=%u N=%u", ok[i].k, ok[i].n); |
| 295 | omega_blackwell_kernel_free(&mut); |
| 296 | } else CHECK(ok[i].k == 16, "mutant refused only for K=16 (K=%u)", ok[i].k); |
| 297 | omega_blackwell_kernel_free(&kernel); |
| 298 | } |
| 299 | OmegaMatMulSpec spec; OmegaBlackwellKernel kernel; memset(&kernel, 0, sizeof kernel); |
| 300 | CHECK(omega_matmul_spec_init(&spec, 16, 24, 16, OMEGA_MATMUL_PRECISION_BF16) == 0 && omega_blackwell_codegen_matmul_tensor_loop(&spec, 1, 0, &kernel) != 0, "K not multiple of 16 refused by codegen (the API pads)"); |
| 301 | CHECK(omega_matmul_spec_init(&spec, 16, 16, 16, OMEGA_MATMUL_PRECISION_BF16) == 0 && omega_blackwell_codegen_matmul_tensor_loop(&spec, 3, 0, &kernel) != 0, "grid_x beyond the column tiles refused"); |
| 302 | /* the single-tile kernel of cut 1 still generates (other gates use it) */ |
| 303 | memset(&kernel, 0, sizeof kernel); |
| 304 | CHECK(omega_matmul_spec_init(&spec, 16, 16, 8, OMEGA_MATMUL_PRECISION_BF16) == 0 && omega_blackwell_codegen_matmul(&spec, &kernel) == 0 && kernel.code_size > 0, "16x16x8 single-tile kernel generated"); |
| 305 | omega_blackwell_kernel_free(&kernel); |
| 306 | frag_host_checks(); |
| 307 | omega_gpu_matmul_cache_clear(); |
| 308 | } |
| 309 | |
| 310 | /* One-row calls reuse the zero padding rows of the activation buffer: a second one-row call of the same |
| 311 | * width writes row 0 only (hook counts it) and still matches the oracle with a different A; a wider |
| 312 | * call in between, or another width, forces a full pack again. */ |
| 313 | static void one_row_repack_checks(void) { |
| 314 | const uint32_t k = 4096, n = 256; |
| 315 | float *a = malloc((size_t)32 * k * sizeof *a), *b = malloc((size_t)k * n * sizeof *b), *c = malloc((size_t)32 * n * sizeof *c); |
| 316 | uint32_t seed = 0x10c0u; |
| 317 | fill(a, (size_t)32 * k, &seed); fill(b, (size_t)k * n, &seed); |
| 318 | OmegaGpuMatmulInfo info; |
| 319 | const uint64_t h0 = omega_gpu_matmul_test_a_row0_hits(); |
| 320 | int rc1 = omega_gpu_matmul_f32(1, k, n, a, b, c, &info); |
| 321 | CHECK(rc1 == OMEGA_GPU_MATMUL_OK && info.parity_verified, "one-row call 1 (full pack) rc=%s parity=%d", omega_gpu_matmul_rc_name(rc1), info.parity_verified); |
| 322 | const uint64_t h1 = omega_gpu_matmul_test_a_row0_hits(); |
| 323 | int rc2 = omega_gpu_matmul_f32(1, k, n, a + k, b, c, &info); |
| 324 | const uint64_t h2 = omega_gpu_matmul_test_a_row0_hits(); |
| 325 | CHECK(rc2 == OMEGA_GPU_MATMUL_OK && info.parity_verified, "one-row call 2 (row 0 only, new A) rc=%s parity=%d", omega_gpu_matmul_rc_name(rc2), info.parity_verified); |
| 326 | CHECK(h1 == h0 && h2 == h1 + 1, "row-0 path taken on the second one-row call only (hits %" PRIu64 " %" PRIu64 " %" PRIu64 ")", h0, h1, h2); |
| 327 | int rc3 = omega_gpu_matmul_f32(32, k, n, a, b, c, &info); |
| 328 | CHECK(rc3 == OMEGA_GPU_MATMUL_OK && info.parity_verified, "32-row call between rc=%s parity=%d", omega_gpu_matmul_rc_name(rc3), info.parity_verified); |
| 329 | int rc4 = omega_gpu_matmul_f32(1, k, n, a + 2 * (size_t)k, b, c, &info); |
| 330 | const uint64_t h4 = omega_gpu_matmul_test_a_row0_hits(); |
| 331 | CHECK(rc4 == OMEGA_GPU_MATMUL_OK && info.parity_verified && h4 == h2, "one-row call after a wider call packs in full (hits %" PRIu64 ")", h4); |
| 332 | int rc5 = omega_gpu_matmul_f32(1, k, n, a + 3 * (size_t)k, b, c, &info); |
| 333 | const uint64_t h5 = omega_gpu_matmul_test_a_row0_hits(); |
| 334 | CHECK(rc5 == OMEGA_GPU_MATMUL_OK && info.parity_verified && h5 == h4 + 1, "one-row call after a one-row call writes row 0 only (hits %" PRIu64 ")", h5); |
| 335 | int rc6 = omega_gpu_matmul_f32(1, 64, n, a, b, c, &info); |
| 336 | const uint64_t h6 = omega_gpu_matmul_test_a_row0_hits(); |
| 337 | CHECK(rc6 == OMEGA_GPU_MATMUL_OK && info.parity_verified && h6 == h5, "one-row call of another width packs in full (hits %" PRIu64 ")", h6); |
| 338 | free(a); free(b); free(c); |
| 339 | } |
| 340 | |
| 341 | static int chip_sweep(const char *shapes, const char *out_path, int mutant) { |
| 342 | FILE *out = out_path ? fopen(out_path, "w") : NULL; |
| 343 | if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } |
| 344 | if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_MATMUL_API_SWEEP_V2\",\"mutant\":%s,\"cta_budget\":%u,\"shapes\":[", mutant ? "true" : "false", omega_gpu_matmul_cta_budget()); |
| 345 | char *copy = strdup(shapes); |
| 346 | char *save = NULL; |
| 347 | int first = 1; |
| 348 | for (char *tok = strtok_r(copy, ";", &save); tok; tok = strtok_r(NULL, ";", &save)) { |
| 349 | unsigned m, k, n; |
| 350 | if (sscanf(tok, "%u,%u,%u", &m, &k, &n) != 3) { printf("bad shape %s\n", tok); continue; } |
| 351 | size_t na = (size_t)m * k, nb = (size_t)k * n, nc = (size_t)m * n; |
| 352 | float *a = malloc(na * sizeof *a), *b = malloc(nb * sizeof *b), *c = malloc(nc * sizeof *c), *c2 = malloc(nc * sizeof *c2); |
| 353 | if (!a || !b || !c || !c2) { printf("out of host memory for %s\n", tok); free(a); free(b); free(c); free(c2); continue; } |
| 354 | uint32_t seed = 0x5eed0000u ^ (m << 20) ^ (k << 10) ^ n; |
| 355 | fill(a, na, &seed); fill(b, nb, &seed); |
| 356 | for (size_t i = 0; i < nc; i++) c[i] = -12345.0f; |
| 357 | OmegaGpuMatmulInfo info, info2; |
| 358 | double t0 = now_ms(); |
| 359 | int rc = omega_gpu_matmul_f32(m, k, n, a, b, c, &info); |
| 360 | double wall_ms = now_ms() - t0; |
| 361 | /* second call on the same shape must hit the kernel cache and agree */ |
| 362 | int rc2 = omega_gpu_matmul_f32(m, k, n, a, b, c2, &info2); |
| 363 | size_t repeat_mismatch = 0; |
| 364 | for (size_t i = 0; i < nc; i++) if (c[i] != c2[i]) repeat_mismatch++; |
| 365 | size_t e2e_bad = 0; |
| 366 | double e2e = (rc == OMEGA_GPU_MATMUL_OK || rc == OMEGA_GPU_MATMUL_PARITY_FAIL) ? e2e_max_rel(m, k, n, a, b, c, &e2e_bad) : 0.0; |
| 367 | int pass = rc == OMEGA_GPU_MATMUL_OK && info.parity_verified && e2e < 1e-5 |
| 368 | && rc2 == OMEGA_GPU_MATMUL_OK && info2.kernel_cache_hit && repeat_mismatch == 0; |
| 369 | if (mutant) { |
| 370 | /* the wrong kernel must be caught: PARITY_FAIL from the API oracle and a visible e2e error */ |
| 371 | int caught = rc == OMEGA_GPU_MATMUL_PARITY_FAIL && !info.parity_verified && e2e >= 1e-5; |
| 372 | CHECK(caught, "mutant shape %ux%ux%u NOT caught: rc=%s parity=%d e2e_rel=%g", m, k, n, omega_gpu_matmul_rc_name(rc), info.parity_verified, e2e); |
| 373 | pass = caught; |
| 374 | } else { |
| 375 | CHECK(pass, "shape %ux%ux%u rc=%s parity=%d max_rel=%g e2e_rel=%g rc2=%s hit=%d repeat_mismatch=%zu", |
| 376 | m, k, n, omega_gpu_matmul_rc_name(rc), info.parity_verified, info.max_rel_err, e2e, |
| 377 | omega_gpu_matmul_rc_name(rc2), info2.kernel_cache_hit, repeat_mismatch); |
| 378 | } |
| 379 | printf("%s %ux%ux%u rc=%s chip_ns=%" PRIu64 " call_ms=%.3f wall_ms=%.1f max_abs=%g max_rel=%g mismatches=%zu e2e_rel=%g e2e_bad=%zu grid=%ux%u calls=%u rows_per_call=%u cache_hit2=%d err=\"%s\"\n", |
| 380 | pass ? (mutant ? "KILLED" : "PASS") : (mutant ? "SURVIVED" : "FAIL"), m, k, n, omega_gpu_matmul_rc_name(rc), info.elapsed_ns, info.call_ns / 1e6, wall_ms, |
| 381 | info.max_abs_err, info.max_rel_err, info.mismatch_count, e2e, e2e_bad, info.grid_x, info.grid_y, info.chip_calls, info.rows_per_call, info2.kernel_cache_hit, rc == OMEGA_GPU_MATMUL_CHIP_FAIL ? omega_gpu_matmul_last_error() : ""); |
| 382 | if (out) { |
| 383 | fprintf(out, "%s{\"m\":%u,\"k\":%u,\"n\":%u,\"rc\":\"%s\",\"pass\":%s,\"chip_elapsed_ns\":%" PRIu64 |
| 384 | ",\"call_ms\":%.3f,\"wall_ms\":%.3f,\"max_abs_err\":%g,\"max_rel_err\":%g,\"mismatch_count\":%zu," |
| 385 | "\"parity_verified\":%s,\"completion_marker\":%u,\"repeat_rc\":\"%s\",\"repeat_cache_hit\":%s," |
| 386 | "\"repeat_mismatch\":%zu,\"e2e_max_rel\":%g,\"e2e_bad\":%zu,\"grid_x\":%u,\"grid_y\":%u,\"chip_calls\":%u,\"rows_per_call\":%u,\"padded_m\":%u,\"padded_k\":%u,\"padded_n\":%u,\"target_chip\":\"%s\",\"sm_architecture\":%u}", |
| 387 | first ? "" : ",", m, k, n, omega_gpu_matmul_rc_name(rc), pass ? "true" : "false", |
| 388 | info.elapsed_ns, info.call_ns / 1e6, wall_ms, info.max_abs_err, info.max_rel_err, info.mismatch_count, |
| 389 | info.parity_verified ? "true" : "false", info.completion_marker, |
| 390 | omega_gpu_matmul_rc_name(rc2), info2.kernel_cache_hit ? "true" : "false", |
| 391 | repeat_mismatch, e2e, e2e_bad, info.grid_x, info.grid_y, info.chip_calls, info.rows_per_call, info.padded_m, info.padded_k, info.padded_n, info.target_chip, info.sm_architecture); |
| 392 | first = 0; |
| 393 | } |
| 394 | free(a); free(b); free(c); free(c2); |
| 395 | if (omega_gpu_matmul_is_blocked()) { printf("device latched after an uncertain completion; stopping the sweep\n"); break; } |
| 396 | } |
| 397 | free(copy); |
| 398 | if (out) { |
| 399 | fprintf(out, "],\"blocked\":%s,\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", omega_gpu_matmul_is_blocked() ? "true" : "false", g_checks, g_failed, |
| 400 | g_failed == 0 ? (mutant ? "OMEGA_GPU_MATMUL_MUTANT_KILLED" : "OMEGA_GPU_MATMUL_API_PASS") : (mutant ? "OMEGA_GPU_MATMUL_MUTANT_SURVIVED" : "OMEGA_GPU_MATMUL_API_FAIL")); |
| 401 | fclose(out); |
| 402 | } |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | static int cmp_double(const void *x, const void *y) { double a = *(const double *)x, b = *(const double *)y; return a < b ? -1 : a > b; } |
| 407 | |
| 408 | /* Resident path: upload B once, then 100 calls; median wall time per call must be under 5 ms. |
| 409 | * Parity: first and last result against the double reference (0 elements at or beyond 1e-5). */ |
| 410 | static int timing(const char *shapes, const char *out_path) { |
| 411 | FILE *out = out_path ? fopen(out_path, "w") : NULL; |
| 412 | if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } |
| 413 | if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_MATMUL_API_TIMING_V1\",\"cta_budget\":%u,\"spin_us\":%u,\"calls\":100,\"shapes\":[", omega_gpu_matmul_cta_budget(), omega_gpu_session_spin_us()); |
| 414 | char *copy = strdup(shapes); char *save = NULL; int first = 1; |
| 415 | for (char *tok = strtok_r(copy, ";", &save); tok; tok = strtok_r(NULL, ";", &save)) { |
| 416 | unsigned m, k, n; |
| 417 | if (sscanf(tok, "%u,%u,%u", &m, &k, &n) != 3) { printf("bad shape %s\n", tok); continue; } |
| 418 | size_t na = (size_t)m * k, nb = (size_t)k * n, nc = (size_t)m * n; |
| 419 | float *a = malloc(na * sizeof *a), *b = malloc(nb * sizeof *b), *c = malloc(nc * sizeof *c), *c_first = malloc(nc * sizeof *c_first); |
| 420 | uint16_t *ah = malloc(na * sizeof *ah); |
| 421 | if (!a || !b || !c || !c_first || !ah) { printf("out of host memory\n"); return 2; } |
| 422 | uint32_t seed = 0x7111e000u ^ (m << 20) ^ (k << 10) ^ n; |
| 423 | fill(a, na, &seed); fill(b, nb, &seed); |
| 424 | for (size_t i = 0; i < na; i++) ah[i] = omega_fp32_to_bf16(a[i]); |
| 425 | OmegaGpuTensor *t = NULL; |
| 426 | double tu0 = now_ms(); |
| 427 | int urc = omega_gpu_tensor_upload_f32(k, n, b, &t); |
| 428 | double upload_ms = now_ms() - tu0; |
| 429 | CHECK(urc == OMEGA_GPU_MATMUL_OK, "upload %ux%u rc=%s", k, n, omega_gpu_matmul_rc_name(urc)); |
| 430 | double samples[100]; int ok_calls = 0; OmegaGpuMatmulInfo info; memset(&info, 0, sizeof info); |
| 431 | uint64_t chip_ns_sum = 0; size_t repeat_mismatch = 0; |
| 432 | if (urc == OMEGA_GPU_MATMUL_OK) { |
| 433 | for (int i = 0; i < 100; i++) { |
| 434 | for (size_t j = 0; j < nc; j++) c[j] = -12345.0f; |
| 435 | double t0 = now_ms(); |
| 436 | int rc = omega_gpu_matmul_resident_bf16(m, ah, t, c, &info); |
| 437 | samples[i] = now_ms() - t0; |
| 438 | /* OMEGA_GPU_MATMUL_TIMING_NO_ORACLE=1: the per-call host oracle (m x n x k double MACs) only on the first |
| 439 | * call; at m = 128 it is minutes per shape and the timing is the point (2026-10-12). */ |
| 440 | if (i == 0 && getenv("OMEGA_GPU_MATMUL_TIMING_NO_ORACLE")) omega_gpu_matmul_set_oracle(0); |
| 441 | if (rc == OMEGA_GPU_MATMUL_OK) ok_calls++; |
| 442 | chip_ns_sum += info.elapsed_ns; |
| 443 | if (i == 0) memcpy(c_first, c, nc * sizeof *c); |
| 444 | else for (size_t j = 0; j < nc; j++) if (c[j] != c_first[j]) repeat_mismatch++; |
| 445 | if (omega_gpu_matmul_is_blocked()) break; |
| 446 | } |
| 447 | } |
| 448 | qsort(samples, 100, sizeof samples[0], cmp_double); |
| 449 | double median = samples[50], p90 = samples[90], mn = samples[0], mx = samples[99]; |
| 450 | size_t bad_first = 0, bad_last = 0; |
| 451 | double e2e_first = e2e_max_rel(m, k, n, a, b, c_first, &bad_first); |
| 452 | double e2e_last = e2e_max_rel(m, k, n, a, b, c, &bad_last); |
| 453 | int pass = ok_calls == 100 && bad_first == 0 && bad_last == 0 && repeat_mismatch == 0 && (m > 1 || median < 5.0); /* the 5 ms gate is the GEMV (m = 1) gate */ |
| 454 | CHECK(pass, "timing %ux%ux%u ok_calls=%d median_ms=%.3f e2e_first=%g e2e_last=%g bad=%zu/%zu repeat_mismatch=%zu", |
| 455 | m, k, n, ok_calls, median, e2e_first, e2e_last, bad_first, bad_last, repeat_mismatch); |
| 456 | printf("%s timing %ux%ux%u upload_ms=%.1f ok_calls=%d median_ms=%.3f p90_ms=%.3f min_ms=%.3f max_ms=%.3f chip_ns_mean=%" PRIu64 " grid=%ux%u e2e_first=%g e2e_last=%g repeat_mismatch=%zu\n", |
| 457 | pass ? "PASS" : "FAIL", m, k, n, upload_ms, ok_calls, median, p90, mn, mx, ok_calls ? chip_ns_sum / (uint64_t)ok_calls : 0, info.grid_x, info.grid_y, e2e_first, e2e_last, repeat_mismatch); |
| 458 | if (out) { |
| 459 | fprintf(out, "%s{\"m\":%u,\"k\":%u,\"n\":%u,\"pass\":%s,\"upload_ms\":%.3f,\"ok_calls\":%d,\"median_ms\":%.4f,\"p90_ms\":%.4f,\"min_ms\":%.4f,\"max_ms\":%.4f," |
| 460 | "\"chip_ns_mean\":%" PRIu64 ",\"grid_x\":%u,\"grid_y\":%u,\"e2e_first\":%g,\"e2e_last\":%g,\"e2e_bad_first\":%zu,\"e2e_bad_last\":%zu,\"repeat_mismatch\":%zu}", |
| 461 | first ? "" : ",", m, k, n, pass ? "true" : "false", upload_ms, ok_calls, median, p90, mn, mx, |
| 462 | ok_calls ? chip_ns_sum / (uint64_t)ok_calls : 0, info.grid_x, info.grid_y, e2e_first, e2e_last, bad_first, bad_last, repeat_mismatch); |
| 463 | first = 0; |
| 464 | } |
| 465 | omega_gpu_matmul_set_oracle(1); |
| 466 | omega_gpu_tensor_free(t); |
| 467 | free(a); free(b); free(c); free(c_first); free(ah); |
| 468 | if (omega_gpu_matmul_is_blocked()) break; |
| 469 | } |
| 470 | free(copy); |
| 471 | if (out) { |
| 472 | fprintf(out, "],\"blocked\":%s,\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", omega_gpu_matmul_is_blocked() ? "true" : "false", g_checks, g_failed, |
| 473 | g_failed == 0 ? "OMEGA_GPU_MATMUL_TIMING_PASS" : "OMEGA_GPU_MATMUL_TIMING_FAIL"); |
| 474 | fclose(out); |
| 475 | } |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | int main(int argc, char **argv) { |
| 480 | const char *out_path = NULL; |
| 481 | int host = 0, mutant = 0, do_timing = 0; |
| 482 | for (int i = 1; i < argc; i++) { |
| 483 | if (strcmp(argv[i], "--host-only") == 0) host = 1; |
| 484 | else if (strcmp(argv[i], "--mutant") == 0) mutant = 1; |
| 485 | else if (strcmp(argv[i], "--timing") == 0) do_timing = 1; |
| 486 | else if (strcmp(argv[i], "--cta-budget") == 0 && i + 1 < argc) omega_gpu_matmul_set_cta_budget((uint32_t)strtoul(argv[++i], NULL, 10)); |
| 487 | else if (strcmp(argv[i], "--spin-us") == 0 && i + 1 < argc) { |
| 488 | if (omega_gpu_session_set_spin_us((uint32_t)strtoul(argv[++i], NULL, 10)) != 0) { fprintf(stderr, "--spin-us out of range\n"); return 2; } |
| 489 | } |
| 490 | else if (strcmp(argv[i], "--unroll") == 0 && i + 1 < argc) omega_gpu_matmul_test_set_unroll((uint32_t)strtoul(argv[++i], NULL, 10)); |
| 491 | else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i]; |
| 492 | } |
| 493 | const char *shapes = getenv("OMEGA_GPU_MATMUL_SHAPES"); |
| 494 | if (shapes && !*shapes) shapes = NULL; |
| 495 | int rc = 0; |
| 496 | if (host) host_only(); |
| 497 | else if (do_timing) rc = timing(shapes ? shapes : TIMING_SHAPES, out_path); |
| 498 | else if (mutant) { omega_gpu_matmul_test_set_mutant(1); rc = chip_sweep(shapes ? shapes : MUTANT_SHAPES, out_path, 1); omega_gpu_matmul_test_set_mutant(0); } |
| 499 | else rc = chip_sweep(shapes ? shapes : DEFAULT_SHAPES, out_path, 0); |
| 500 | if (!host && !do_timing && !mutant && !omega_gpu_matmul_is_blocked()) one_row_repack_checks(); |
| 501 | if (!omega_gpu_matmul_is_blocked()) omega_gpu_device_close(); |
| 502 | if (rc) return 2; |
| 503 | printf("%s: %d checks, %d failed%s\n", g_failed ? "FAIL" : "PASS", g_checks, g_failed, mutant ? (g_failed ? " (mutant SURVIVED)" : " (mutant killed)") : ""); |
| 504 | return g_failed ? 1 : 0; |
| 505 | } |