Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tests/gpu_matmul_api_test.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tests / gpu_matmul_api_test.c` 708 lines ยท 53527 bytes Raw Blame History 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_matmul_api_test: FB-1 cut 1 / 1b gate for omega_gpu_matmul_api. 18 * 19 * ./gpu_matmul_api_test --host-only argument and limit refusals, codegen only, no chip 20 * ./gpu_matmul_api_test --out receipt.json chip sweep over shapes, JSON receipt 21 * ./gpu_matmul_api_test --timing --out r.json resident GEMV timing (median of 100 calls) + parity 22 * ./gpu_matmul_api_test --update --out r.json tensor_update_* in place vs a fresh upload, and its timing (R3) 23 * ./gpu_matmul_api_test --mutant --out r.json wrong kernel (last K step dropped) must FAIL parity; 24 * exit 0 when every shape caught it ("mutant killed") 25 * ./gpu_matmul_api_test --cta-budget N ... run with a different CTA budget (I42 experiment) 26 * ./gpu_matmul_api_test --spin-us N ... marker-wait spin window for every launch (omega#328) 27 * ./gpu_matmul_api_test --unroll N ... force the fragment kernel unroll (omega #328 experiment) 28 * ./gpu_matmul_api_test --stages --out r.json omega #473 stage profile (resident, bf16 and f32 rows, median of 7) 29 * 30 * Shapes come from OMEGA_GPU_MATMUL_SHAPES ("m,k,n;m,k,n;..."), default below. 31 * Pass rule per shape: rc OK, parity verified by the API oracle, end-to-end error < 1e-5 relative to 32 * the accumulation scale (bf16 inputs against a double reference; only accumulation order and the 33 * chip's f32 accumulator differ), second call hits the kernel cache and agrees bit for bit. 34 */ 35 #include "omega_gpu_matmul_api.h" 36 #include "omega_gpu_session.h" 37 #include "omega_blackwell_codegen.h" 38 #include "omega_blackwell_matmul.h" 39 #include 40 #include 41 #include 42 #include 43 #include 44 #include 45 46 /* round a float to bf16 and back, as the API does on the way in */ 47 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; } 48 49 static int g_checks, g_failed; 50 #define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) 51 52 /* cut-1 list unchanged, then the cut-1b Llama shapes (hidden 2048, ffn 8192, vocab 128256) */ 53 static const char *DEFAULT_SHAPES = 54 "16,16,16;32,32,32;64,64,64;128,128,128;256,256,256;512,512,512;" 55 "1,1024,1024;1,256,1024;16,1024,256;8,1024,8;" 56 "1024,16,128;192,16,192;512,16,64;256,16,256;128,16,512;" 57 "1,2048,8192;1,8192,2048;1,2048,128256"; 58 static const char *MUTANT_SHAPES = "16,32,16;1,2048,8192;64,64,64;1,1024,1024"; 59 static const char *TIMING_SHAPES = "1,2048,8192;1,8192,2048"; 60 /* omega #473 training shapes */ 61 static const char *STAGE_SHAPES = "8192,768,2304;8192,768,768;8192,768,3072;8192,3072,768;1024,768,50304"; 62 63 static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } 64 static double now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; } 65 66 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; } 67 68 /* end-to-end: full-K product on the host from the same bf16-rounded inputs, in double; error relative 69 * to the accumulation scale (sum of |terms|), not to a result that may cancel to near zero */ 70 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) { 71 double worst = 0.0; size_t nbad = 0; 72 for (size_t i = 0; i < m; i++) for (size_t j = 0; j < n; j++) { 73 double acc = 0.0, scale = 0.0; 74 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); } 75 double rel = fabs(acc - (double)c[i * n + j]) / (scale > 1e-6 ? scale : 1e-6); 76 if (rel > worst) worst = rel; 77 if (rel >= 1e-5) nbad++; 78 } 79 if (bad) *bad = nbad; 80 return worst; 81 } 82 83 /* omega #328 fragment-major kernel, host only. The unpack below is written from the PTX ISA 84 * table for mma.m16n8k16 (.bf16 A: a0,a1 row g cols 2t,2t+1; a2,a3 row g+8; a4,a5 row g cols 85 * 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, 86 * t = lane&3), independently of the packers: pack then unpack must give the matrix back. */ 87 /* Result sentinels: one poison word per 16 x 8 fragment tile that holds a real output word, inside the 88 * tile's real rows and columns; the scan counts exactly the survivors. */ 89 static void sentinel_host_checks(void) { 90 static const uint32_t shapes[][2] = { { 1u, 24576u }, { 1u, 4096u }, { 16u, 151936u }, { 128u, 24576u }, { 116u, 4096u }, { 3u, 20u }, { 17u, 9u } }; 91 const float poison = -999.0f; 92 for (size_t s = 0; s < sizeof shapes / sizeof shapes[0]; s++) { 93 const uint32_t m = shapes[s][0], n = shapes[s][1], mp = (m + 15u) / 16u * 16u, np = (n + 7u) / 8u * 8u; 94 const uint32_t mt = (m + 15u) / 16u, nt = (n + 7u) / 8u; 95 float *cd = calloc((size_t)mp * np, sizeof *cd); 96 const size_t set = omega_matmul_result_sentinels(cd, m, n, np, poison, 1); 97 CHECK(set == (size_t)mt * nt, "sentinel count m=%u n=%u: %zu != %u", m, n, set, mt * nt); 98 /* every fragment tile with a real word holds exactly one sentinel, inside its real rows and columns */ 99 size_t tiles_ok = 0, outside = 0; 100 for (uint32_t ti = 0; ti < mt; ti++) 101 for (uint32_t tj = 0; tj < nt; tj++) { 102 size_t in_tile = 0; 103 for (uint32_t r = ti * 16u; r < ti * 16u + 16u; r++) 104 for (uint32_t c = tj * 8u; c < tj * 8u + 8u; c++) 105 if (cd[(size_t)r * np + c] == poison) { if (r < m && c < n) in_tile++; else outside++; } 106 if (in_tile == 1) tiles_ok++; 107 } 108 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); 109 CHECK(omega_matmul_result_sentinels(cd, m, n, np, poison, 0) == set, "sentinel scan m=%u n=%u: all survive", m, n); 110 /* clearing one tile's sentinel removes exactly one survivor; a fully written result has none */ 111 cd[(size_t)(m - 1u) * np + (n - 1u)] = 0.0f; 112 CHECK(omega_matmul_result_sentinels(cd, m, n, np, poison, 0) == set - 1u, "sentinel scan m=%u n=%u: one cleared", m, n); 113 for (size_t i = 0; i < (size_t)mp * np; i++) cd[i] = 1.5f; 114 CHECK(omega_matmul_result_sentinels(cd, m, n, np, poison, 0) == 0, "sentinel scan m=%u n=%u: none survive", m, n); 115 free(cd); 116 } 117 } 118 119 static void frag_host_checks(void) { 120 sentinel_host_checks(); 121 /* row-0 pack (one-row decode call): a full one-row pack of A2 equals a full pack of A1 followed by the 122 * row-0 pack of A2, bf16 and f32 inputs, including a width with padding columns */ 123 { 124 const uint32_t ks[] = { 4096u, 40u }; 125 for (size_t i = 0; i < 2; i++) { 126 const uint32_t k = ks[i], kp = (k + 15u) / 16u * 16u; 127 float *a1 = malloc(k * sizeof *a1), *a2 = malloc(k * sizeof *a2); 128 uint16_t *b1 = malloc(k * 2), *b2 = malloc(k * 2), *full = malloc((size_t)16 * kp * 2), *inc = malloc((size_t)16 * kp * 2); 129 uint32_t seed = 77u + k; 130 fill(a1, k, &seed); fill(a2, k, &seed); 131 for (uint32_t j = 0; j < k; j++) { b1[j] = omega_fp32_to_bf16(a1[j]); b2[j] = omega_fp32_to_bf16(a2[j]); } 132 omega_matmul_frag_pack_a(b2, 1, k, 16, kp, full); 133 omega_matmul_frag_pack_a(b1, 1, k, 16, kp, inc); 134 omega_matmul_frag_pack_a_row0(b2, k, kp, inc); 135 CHECK(memcmp(full, inc, (size_t)16 * kp * 2) == 0, "row-0 pack bf16 k=%u equals the full one-row pack", k); 136 omega_matmul_frag_pack_a_f32(a1, 1, k, 16, kp, inc); 137 omega_matmul_frag_pack_a_row0_f32(a2, k, kp, inc); 138 CHECK(memcmp(full, inc, (size_t)16 * kp * 2) == 0, "row-0 pack f32 k=%u equals the full one-row pack", k); 139 free(a1); free(a2); free(b1); free(b2); free(full); free(inc); 140 } 141 } 142 CHECK(omega_blackwell_verify_codegen_fixtures_fragmm() == 0, "fragment matmul golden words (rc %d)", omega_blackwell_verify_codegen_fixtures_fragmm()); 143 struct { uint32_t m, k, n; } sh[] = { {1, 16, 8}, {1, 40, 13}, {17, 48, 24}, {33, 2560, 72}, {5, 160, 1000} }; 144 for (size_t s = 0; s < sizeof sh / sizeof sh[0]; s++) { 145 const uint32_t m = sh[s].m, k = sh[s].k, n = sh[s].n; 146 const uint32_t mp = (m + 15) / 16 * 16, kp = (k + 15) / 16 * 16, np = (n + 7) / 8 * 8, KS = kp / 16, NT = np / 8; 147 uint16_t *a = malloc((size_t)m * k * 2), *b = malloc((size_t)k * n * 2); 148 uint16_t *pa = malloc((size_t)mp * kp * 2), *pb = malloc((size_t)kp * np * 2); 149 uint32_t seed = 7 + (uint32_t)s; 150 for (size_t i = 0; i < (size_t)m * k; i++) a[i] = (uint16_t)(1 + lcg(&seed) % 60000); 151 for (size_t i = 0; i < (size_t)k * n; i++) b[i] = (uint16_t)(1 + lcg(&seed) % 60000); 152 omega_matmul_frag_pack_a(a, m, k, mp, kp, pa); 153 /* B in two uneven chunks, as the upload does in FRAG_PACK_TILES steps */ 154 uint32_t split = NT / 2; 155 omega_matmul_frag_pack_b(b, k, n, kp, 0, split, pb); 156 omega_matmul_frag_pack_b(b, k, n, kp, split, NT, pb + (size_t)split * kp * 8); 157 size_t bad_a = 0, bad_b = 0; 158 for (uint32_t mt = 0; mt < mp / 16; mt++) 159 for (uint32_t ks = 0; ks < KS; ks++) 160 for (uint32_t lane = 0; lane < 32; lane++) { 161 const uint32_t gg = lane >> 2, t = lane & 3; 162 const uint32_t rows[8] = { gg, gg, gg + 8, gg + 8, gg, gg, gg + 8, gg + 8 }; 163 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 }; 164 for (int e = 0; e < 8; e++) { 165 uint32_t r = mt * 16 + rows[e], c = ks * 16 + cols[e]; 166 uint16_t want = (r < m && c < k) ? a[(size_t)r * k + c] : 0; 167 if (pa[(((size_t)mt * KS + ks) * 32 + lane) * 8 + e] != want) bad_a++; 168 } 169 } 170 for (uint32_t nt = 0; nt < NT; nt++) 171 for (uint32_t ks = 0; ks < KS; ks++) 172 for (uint32_t lane = 0; lane < 32; lane++) { 173 const uint32_t gg = lane >> 2, t = lane & 3; 174 const uint32_t rows[4] = { 2 * t, 2 * t + 1, 2 * t + 8, 2 * t + 9 }; 175 for (int e = 0; e < 4; e++) { 176 uint32_t r = ks * 16 + rows[e], c = nt * 8 + gg; 177 uint16_t want = (r < k && c < n) ? b[(size_t)r * n + c] : 0; 178 if (pb[(((size_t)nt * KS + ks) * 32 + lane) * 4 + e] != want) bad_b++; 179 } 180 } 181 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); 182 /* the fused f32 packer equals convert-then-pack bit for bit (ties, denormals, inf, NaN included) */ 183 float *af = malloc((size_t)m * k * sizeof *af); 184 uint16_t *pf = malloc((size_t)mp * kp * 2); 185 for (size_t i = 0; i < (size_t)m * k; i++) { 186 uint32_t bits = lcg(&seed) * 2654435761u; 187 switch (i % 7) { 188 case 0: bits = (bits & 0x807fffffu) | 0x3f800000u; break; /* in [1,2) with every mantissa bit live (rounding ties) */ 189 case 1: bits = 0x7f800000u | ((bits >> 9) & 0x7fffffu); break; /* inf (0 payload) or NaN payloads */ 190 case 2: bits &= 0x807fffffu; break; /* zero and denormals */ 191 default: break; 192 } 193 memcpy(&af[i], &bits, 4); 194 a[i] = omega_fp32_to_bf16(af[i]); 195 } 196 omega_matmul_frag_pack_a(a, m, k, mp, kp, pa); 197 omega_matmul_frag_pack_a_f32(af, m, k, mp, kp, pf); 198 CHECK(memcmp(pa, pf, (size_t)mp * kp * 2) == 0, "fused f32 pack equals convert-then-pack %ux%u", m, k); 199 free(af); free(pf); 200 free(a); free(b); free(pa); free(pb); 201 } 202 /* the kernel generates for every padded Qwen3-4B and Llama-3.2-1B decode shape at the API's unroll */ 203 struct { uint32_t k, n, gx; } ok[] = { {16, 8, 1}, {2560, 4096, 256}, {2560, 1024, 128}, {4096, 2560, 256}, {2560, 9728, 256}, 204 {9728, 2560, 256}, {2560, 151936, 256}, {2048, 8192, 64}, {8192, 2048, 64}, 205 {2048, 128256, 64}, {16384, 262144, 64}, {48, 16, 2} }; 206 for (size_t i = 0; i < sizeof ok / sizeof ok[0]; i++) { 207 OmegaMatMulSpec spec; OmegaBlackwellKernel kernel; memset(&kernel, 0, sizeof kernel); 208 const uint32_t u = omega_matmul_frag_unroll(ok[i].k); 209 int rc = omega_matmul_spec_init(&spec, 16, ok[i].k, ok[i].n, OMEGA_MATMUL_PRECISION_BF16) == 0 && 210 omega_blackwell_codegen_matmul_frag(&spec, ok[i].gx, u, 0, &kernel) == 0 && kernel.code_size > 0; 211 CHECK(rc, "fragment kernel K=%u N=%u gx=%u unroll=%u generated", ok[i].k, ok[i].n, ok[i].gx, u); 212 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); 213 if (i == 1) printf("fragment kernel K=2560 N=4096 unroll %u: %zu instructions, %u registers\n", u, kernel.insn_count, kernel.gpr_count); 214 OmegaBlackwellKernel mut; memset(&mut, 0, sizeof mut); 215 if (omega_blackwell_codegen_matmul_frag(&spec, ok[i].gx, u, 1, &mut) == 0) { 216 CHECK(memcmp(mut.code_digest, kernel.code_digest, 32) != 0, "fragment mutant differs K=%u N=%u", ok[i].k, ok[i].n); 217 omega_blackwell_kernel_free(&mut); 218 } else CHECK(ok[i].k / 16 / u < 2, "fragment mutant refused only with one pass (K=%u)", ok[i].k); 219 omega_blackwell_kernel_free(&kernel); 220 } 221 CHECK(omega_matmul_frag_unroll(2560) == 4 && omega_matmul_frag_unroll(48) == 1 && omega_matmul_frag_unroll(64) == 4 && 222 omega_matmul_frag_unroll(32) == 2 && omega_matmul_frag_unroll(128) == 4, "unroll choice (capped at 4)"); 223 { /* unroll 8 still generates (experiments) */ 224 OmegaMatMulSpec s8; OmegaBlackwellKernel k8; memset(&k8, 0, sizeof k8); 225 CHECK(omega_matmul_spec_init(&s8, 16, 2560, 4096, OMEGA_MATMUL_PRECISION_BF16) == 0 && 226 omega_blackwell_codegen_matmul_frag(&s8, 256, 8, 0, &k8) == 0 && k8.code_size > 0, "unroll 8 generates"); 227 omega_blackwell_kernel_free(&k8); 228 } 229 OmegaMatMulSpec spec; OmegaBlackwellKernel kernel; memset(&kernel, 0, sizeof kernel); 230 CHECK(omega_matmul_spec_init(&spec, 16, 48, 16, OMEGA_MATMUL_PRECISION_BF16) == 0 && 231 omega_blackwell_codegen_matmul_frag(&spec, 1, 2, 0, &kernel) != 0, "unroll not dividing K/16 refused"); 232 CHECK(omega_blackwell_codegen_matmul_frag(&spec, 1, 3, 0, &kernel) != 0, "unroll 3 refused"); 233 CHECK(omega_blackwell_codegen_matmul_frag(&spec, 3, 1, 0, &kernel) != 0, "fragment grid_x beyond the column tiles refused"); 234 /* 2026-10-12: the blocked kernel (rt row tiles x ct column tiles per warp) generates for the Qwen3-8B 235 * prefill shapes at m = 128 (rt 4, ct 4, grid_x 512 at the daemon's 1024-CTA budget) and the small 236 * parity shapes; it fits the allocator's 128 registers; its mutant differs; the envelope is enforced */ 237 struct { uint32_t k, n, gx, rt, ct; } bk[] = { {4096, 24576, 512, 4, 4}, {12288, 4096, 128, 4, 4}, {4096, 6144, 192, 4, 4}, 238 {128, 128, 4, 4, 4}, {32, 32, 1, 2, 4}, {16, 8, 1, 2, 1}, {1024, 8, 1, 4, 1}, 239 {64, 64, 2, 4, 2}, {48, 16, 1, 2, 2} }; 240 for (size_t i = 0; i < sizeof bk / sizeof bk[0]; i++) { 241 OmegaMatMulSpec spec; OmegaBlackwellKernel kernel; memset(&kernel, 0, sizeof kernel); 242 const uint32_t u = omega_matmul_frag_block_unroll(bk[i].k, bk[i].rt, bk[i].ct); 243 int rc = omega_matmul_spec_init(&spec, 16, bk[i].k, bk[i].n, OMEGA_MATMUL_PRECISION_BF16) == 0 && 244 omega_blackwell_codegen_matmul_frag_block(&spec, bk[i].gx, u, bk[i].rt, bk[i].ct, 0, &kernel) == 0 && kernel.code_size > 0; 245 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); 246 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); 247 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); 248 OmegaBlackwellKernel mut; memset(&mut, 0, sizeof mut); 249 if (omega_blackwell_codegen_matmul_frag_block(&spec, bk[i].gx, u, bk[i].rt, bk[i].ct, 1, &mut) == 0) { 250 CHECK(memcmp(mut.code_digest, kernel.code_digest, 32) != 0, "blocked mutant differs K=%u N=%u", bk[i].k, bk[i].n); 251 omega_blackwell_kernel_free(&mut); 252 } else CHECK(bk[i].k / 16 / u < 2, "blocked mutant refused only with one pass (K=%u)", bk[i].k); 253 omega_blackwell_kernel_free(&kernel); 254 } 255 { 256 uint32_t rt, ct; 257 omega_matmul_frag_block_shape(8, 3072, &rt, &ct); CHECK(rt == 4 && ct == 4, "block shape m=128 n=24576 is 4x4"); 258 omega_matmul_frag_block_shape(1, 3072, &rt, &ct); CHECK(rt == 1 && ct == 1, "block shape m=1 is the single-tile kernel"); 259 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"); 260 omega_matmul_frag_block_shape(6, 6, &rt, &ct); CHECK(rt == 2 && ct == 2, "block shape 6 row tiles, 6 column tiles is 2x2"); 261 omega_matmul_frag_block_shape(4, 1, &rt, &ct); CHECK(rt == 4 && ct == 1, "block shape 4 row tiles, 1 column tile is 4x1"); 262 /* the C store offset of the last row tile is a signed 24-bit immediate: wide N steps rt down (lm_head: to 1) */ 263 omega_matmul_frag_block_shape(2, 18992, &rt, &ct); CHECK(rt == 1 && ct == 1, "block shape lm_head (N 151936) at 2 row tiles is the single-tile kernel: %ux%u", rt, ct); 264 omega_matmul_frag_block_shape(8, 18992, &rt, &ct); CHECK(rt == 1 && ct == 1, "block shape lm_head (N 151936) at 8 row tiles is the single-tile kernel: %ux%u", rt, ct); 265 omega_matmul_frag_block_shape(8, 8192, &rt, &ct); CHECK(rt == 2 && ct == 4, "block shape N 65536 at 8 row tiles steps rt 4 -> 2: %ux%u", rt, ct); 266 CHECK(omega_matmul_frag_block_unroll(4096, 4, 4) == 1 && omega_matmul_frag_block_unroll(4096, 2, 2) == 2 && 267 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)"); 268 OmegaMatMulSpec s; OmegaBlackwellKernel kk; memset(&kk, 0, sizeof kk); 269 CHECK(omega_matmul_spec_init(&s, 16, 4096, 6144, OMEGA_MATMUL_PRECISION_BF16) == 0, "spec for refusals"); 270 CHECK(omega_blackwell_codegen_matmul_frag_block(&s, 1, 1, 1, 1, 0, &kk) != 0, "blocked 1x1 refused (single-tile kernel)"); 271 CHECK(omega_blackwell_codegen_matmul_frag_block(&s, 1, 1, 3, 2, 0, &kk) != 0, "blocked rt 3 refused"); 272 CHECK(omega_blackwell_codegen_matmul_frag_block(&s, 1, 1, 8, 4, 0, &kk) != 0, "blocked 8x4 (32 accumulators) refused"); 273 CHECK(omega_blackwell_codegen_matmul_frag_block(&s, 1, 1, 8, 1, 0, &kk) != 0, "blocked 8x1 (9 loads per pass) refused"); 274 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"); 275 CHECK(omega_blackwell_codegen_matmul_frag_block(&s, 1, 4, 2, 2, 0, &kk) != 0, "blocked unroll 4 refused"); 276 CHECK(omega_blackwell_codegen_matmul_frag_block(&s, 193, 1, 4, 4, 0, &kk) != 0, "blocked grid_x beyond the column blocks refused"); 277 OmegaMatMulSpec s3; CHECK(omega_matmul_spec_init(&s3, 16, 64, 24, OMEGA_MATMUL_PRECISION_BF16) == 0, "spec n=24"); 278 CHECK(omega_blackwell_codegen_matmul_frag_block(&s3, 1, 1, 2, 4, 0, &kk) != 0, "blocked ct not dividing the column tiles refused"); 279 CHECK(omega_blackwell_codegen_matmul_frag_block(&s3, 1, 1, 2, 2, 0, &kk) != 0, "blocked ct 2 on 3 column tiles refused"); 280 } 281 } 282 283 static void host_only(void) { 284 float a[16 * 16] = {0}, b[16 * 16] = {0}, c[16 * 16] = {0}; 285 OmegaGpuMatmulInfo info; 286 CHECK(omega_gpu_matmul_f32(0, 16, 16, a, b, c, &info) == OMEGA_GPU_MATMUL_BAD_ARGS, "m=0 refused"); 287 CHECK(omega_gpu_matmul_f32(16, 16, 16, NULL, b, c, &info) == OMEGA_GPU_MATMUL_BAD_ARGS, "NULL a refused"); 288 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"); 289 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)"); 290 /* session spin window (omega#328): in-range value kept, out-of-range refused unchanged, 0 restores */ 291 const uint32_t spin0 = omega_gpu_session_spin_us(); 292 CHECK(omega_gpu_session_set_spin_us(2000) == 0 && omega_gpu_session_spin_us() == 2000, "spin window 2000 us set"); 293 CHECK(omega_gpu_session_set_spin_us(OMEGA_GPU_SESSION_MAX_SPIN_US + 1) == -1 && omega_gpu_session_spin_us() == 2000, 294 "spin window over the maximum refused, setting unchanged"); 295 CHECK(omega_gpu_session_set_spin_us(0) == 0 && omega_gpu_session_spin_us() == 0, "spin window 0 restores the sleep-poll"); 296 omega_gpu_session_set_spin_us(spin0); 297 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"); 298 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)"); 299 OmegaGpuTensor *t = NULL; 300 CHECK(omega_gpu_tensor_upload_f32(0, 16, a, &t) == OMEGA_GPU_MATMUL_BAD_ARGS && t == NULL, "tensor k=0 refused"); 301 CHECK(omega_gpu_tensor_upload_f32(16, 16, NULL, &t) == OMEGA_GPU_MATMUL_BAD_ARGS, "tensor NULL refused"); 302 CHECK(omega_gpu_matmul_resident_f32(16, a, NULL, c, &info) == OMEGA_GPU_MATMUL_BAD_ARGS, "resident NULL tensor refused"); 303 CHECK(omega_gpu_tensor_update_f32(NULL, 16, 16, a) == OMEGA_GPU_MATMUL_BAD_ARGS, "update_f32 NULL tensor refused"); 304 CHECK(omega_gpu_tensor_update_bf16(NULL, 16, 16, (const uint16_t *)a) == OMEGA_GPU_MATMUL_BAD_ARGS, "update_bf16 NULL tensor refused"); 305 CHECK(strcmp(omega_gpu_matmul_rc_name(OMEGA_GPU_MATMUL_PARITY_FAIL), "PARITY_FAIL") == 0, "rc name"); 306 CHECK(omega_gpu_matmul_cta_budget() == OMEGA_GPU_MATMUL_MAX_CTAS, "default CTA budget"); 307 { 308 OmegaGpuMatmulStages s0; 309 memset(&s0, 0xff, sizeof s0); 310 omega_gpu_matmul_last_stages(&s0); 311 omega_gpu_matmul_last_stages(NULL); 312 CHECK(s0.core_ns == 0 && s0.launch_ns == 0 && s0.pack_ns == 0, "stages all zero before the first matmul"); 313 } 314 /* omega #473 R4: the default is the best budget of the sealed receipt evidence/R4-MATMUL-CTA-`ffc3176a` (every element of 315 * every call at the five training shapes and two single-tile shapes, budgets 64..1024, all sealed; 1024 fastest) */ 316 CHECK(OMEGA_GPU_MATMUL_MAX_CTAS == 1024u, "default CTA budget is the R4 sealed budget 1024 (got %u)", (unsigned)OMEGA_GPU_MATMUL_MAX_CTAS); 317 CHECK(omega_gpu_matmul_is_blocked() == 0, "not blocked at start"); 318 319 /* codegen only: the looped kernel must accept every padded Llama shape without touching the chip */ 320 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} }; 321 for (size_t i = 0; i < sizeof ok / sizeof ok[0]; i++) { 322 OmegaMatMulSpec spec; OmegaBlackwellKernel kernel; memset(&kernel, 0, sizeof kernel); 323 int rc = omega_matmul_spec_init(&spec, 16, ok[i].k, ok[i].n, OMEGA_MATMUL_PRECISION_BF16) == 0 && 324 omega_blackwell_codegen_matmul_tensor_loop(&spec, ok[i].gx, 0, &kernel) == 0 && kernel.code_size > 0; 325 CHECK(rc, "loop kernel K=%u N=%u gx=%u generated", ok[i].k, ok[i].n, ok[i].gx); 326 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); 327 /* the mutant kernel is a different program */ 328 OmegaBlackwellKernel mut; memset(&mut, 0, sizeof mut); 329 if (omega_blackwell_codegen_matmul_tensor_loop(&spec, ok[i].gx, 1, &mut) == 0) { 330 CHECK(memcmp(mut.code_digest, kernel.code_digest, 32) != 0, "mutant kernel differs K=%u N=%u", ok[i].k, ok[i].n); 331 omega_blackwell_kernel_free(&mut); 332 } else CHECK(ok[i].k == 16, "mutant refused only for K=16 (K=%u)", ok[i].k); 333 omega_blackwell_kernel_free(&kernel); 334 } 335 OmegaMatMulSpec spec; OmegaBlackwellKernel kernel; memset(&kernel, 0, sizeof kernel); 336 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)"); 337 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"); 338 /* the single-tile kernel of cut 1 still generates (other gates use it) */ 339 memset(&kernel, 0, sizeof kernel); 340 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"); 341 omega_blackwell_kernel_free(&kernel); 342 frag_host_checks(); 343 omega_gpu_matmul_cache_clear(); 344 } 345 346 /* One-row calls reuse the zero padding rows of the activation buffer: a second one-row call of the same 347 * width writes row 0 only (hook counts it) and still matches the oracle with a different A; a wider 348 * call in between, or another width, forces a full pack again. */ 349 static void one_row_repack_checks(void) { 350 const uint32_t k = 4096, n = 256; 351 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); 352 uint32_t seed = 0x10c0u; 353 fill(a, (size_t)32 * k, &seed); fill(b, (size_t)k * n, &seed); 354 OmegaGpuMatmulInfo info; 355 const uint64_t h0 = omega_gpu_matmul_test_a_row0_hits(); 356 int rc1 = omega_gpu_matmul_f32(1, k, n, a, b, c, &info); 357 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); 358 const uint64_t h1 = omega_gpu_matmul_test_a_row0_hits(); 359 int rc2 = omega_gpu_matmul_f32(1, k, n, a + k, b, c, &info); 360 const uint64_t h2 = omega_gpu_matmul_test_a_row0_hits(); 361 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); 362 CHECK(h1 == h0 && h2 == h1 + 1, "row-0 path taken on the second one-row call only (hits %" PRIu64 " %" PRIu64 " %" PRIu64 ")", h0, h1, h2); 363 int rc3 = omega_gpu_matmul_f32(32, k, n, a, b, c, &info); 364 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); 365 int rc4 = omega_gpu_matmul_f32(1, k, n, a + 2 * (size_t)k, b, c, &info); 366 const uint64_t h4 = omega_gpu_matmul_test_a_row0_hits(); 367 CHECK(rc4 == OMEGA_GPU_MATMUL_OK && info.parity_verified && h4 == h2, "one-row call after a wider call packs in full (hits %" PRIu64 ")", h4); 368 int rc5 = omega_gpu_matmul_f32(1, k, n, a + 3 * (size_t)k, b, c, &info); 369 const uint64_t h5 = omega_gpu_matmul_test_a_row0_hits(); 370 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); 371 int rc6 = omega_gpu_matmul_f32(1, 64, n, a, b, c, &info); 372 const uint64_t h6 = omega_gpu_matmul_test_a_row0_hits(); 373 CHECK(rc6 == OMEGA_GPU_MATMUL_OK && info.parity_verified && h6 == h5, "one-row call of another width packs in full (hits %" PRIu64 ")", h6); 374 free(a); free(b); free(c); 375 } 376 377 /* omega #473: the stage timer must explain the call. The six stages tile matmul_core in order, the chip 378 * time sits inside the launch stage, and the core span sits inside the whole call. */ 379 static uint64_t stage_sum(const OmegaGpuMatmulStages *s) { 380 return s->setup_ns + s->pack_ns + s->upload_ns + s->launch_ns + s->sync_ns + s->copy_out_ns; 381 } 382 static void stage_checks(const char *tag, const OmegaGpuMatmulInfo *info) { 383 OmegaGpuMatmulStages s; 384 omega_gpu_matmul_last_stages(&s); 385 CHECK(s.chip_ns == info->elapsed_ns && s.chip_ns > 0, "%s stages: chip_ns %" PRIu64 " equals elapsed_ns %" PRIu64, tag, s.chip_ns, info->elapsed_ns); 386 CHECK(s.launch_ns >= s.chip_ns, "%s stages: launch_ns %" PRIu64 " holds chip_ns %" PRIu64, tag, s.launch_ns, s.chip_ns); 387 CHECK(s.pack_ns > 0 && s.copy_out_ns > 0, "%s stages: pack %" PRIu64 " and copy-out %" PRIu64 " measured", tag, s.pack_ns, s.copy_out_ns); 388 CHECK(stage_sum(&s) <= s.core_ns && s.core_ns <= info->call_ns && s.core_ns > 0, 389 "%s stages: sum %" PRIu64 " <= core %" PRIu64 " <= call %" PRIu64, tag, stage_sum(&s), s.core_ns, info->call_ns); 390 } 391 392 /* omega #473 profile: resident calls at the given shapes, 3 warmups then 7 timed calls per input type 393 * (bf16 rows, f32 rows); median of each stage. Correctness is the R4 receipt's job; here the two input 394 * types must agree bit for bit and every call must pass stage_checks. */ 395 static uint64_t med_u64(uint64_t *v, int n) { 396 for (int i = 1; i < n; i++) for (int j = i; j > 0 && v[j - 1] > v[j]; j--) { uint64_t t = v[j]; v[j] = v[j - 1]; v[j - 1] = t; } 397 return v[n / 2]; 398 } 399 static int stages_profile(const char *shapes, const char *out_path) { 400 FILE *out = out_path ? fopen(out_path, "w") : NULL; 401 if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } 402 if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_MATMUL_STAGES_V1\",\"cta_budget\":%u,\"warmups\":3,\"timed\":7,\"rows\":[", omega_gpu_matmul_cta_budget()); 403 char *copy = strdup(shapes), *save = NULL; 404 int first = 1; 405 for (char *tok = strtok_r(copy, ";", &save); tok; tok = strtok_r(NULL, ";", &save)) { 406 unsigned m, k, n; 407 if (sscanf(tok, "%u,%u,%u", &m, &k, &n) != 3) { printf("bad shape %s\n", tok); continue; } 408 const size_t na = (size_t)m * k, nb = (size_t)k * n, nc = (size_t)m * n; 409 float *a = malloc(na * 4), *b = malloc(nb * 4), *c16 = malloc(nc * 4), *c32 = malloc(nc * 4); 410 uint16_t *ah = malloc(na * 2); 411 if (!a || !b || !c16 || !c32 || !ah) { CHECK(0, "host memory for %s", tok); break; } 412 uint32_t seed = 0x5eed0000u ^ (m << 20) ^ (k << 10) ^ n; 413 fill(a, na, &seed); fill(b, nb, &seed); 414 for (size_t i = 0; i < na; i++) ah[i] = omega_fp32_to_bf16(a[i]); 415 OmegaGpuTensor *t = NULL; 416 int urc = omega_gpu_tensor_upload_f32(k, n, b, &t); 417 CHECK(urc == OMEGA_GPU_MATMUL_OK, "upload %s rc=%s", tok, omega_gpu_matmul_rc_name(urc)); 418 if (urc != OMEGA_GPU_MATMUL_OK) break; 419 for (int in32 = 0; in32 < 2; in32++) { 420 uint64_t v[9][7]; /* setup pack upload launch chip sync copy_out core call */ 421 int ok = 1; 422 for (int it = 0; it < 10 && ok; it++) { 423 OmegaGpuMatmulInfo info; 424 int rc = in32 ? omega_gpu_matmul_resident_f32(m, a, t, c32, &info) : omega_gpu_matmul_resident_bf16(m, ah, t, c16, &info); 425 if (rc != OMEGA_GPU_MATMUL_OK) { CHECK(0, "%s %s rc=%s (%s)", tok, in32 ? "f32" : "bf16", omega_gpu_matmul_rc_name(rc), omega_gpu_matmul_last_error()); ok = 0; break; } 426 stage_checks(tok, &info); 427 if (it < 3) continue; 428 OmegaGpuMatmulStages s; omega_gpu_matmul_last_stages(&s); 429 const uint64_t row[9] = { s.setup_ns, s.pack_ns, s.upload_ns, s.launch_ns, s.chip_ns, s.sync_ns, s.copy_out_ns, s.core_ns, info.call_ns }; 430 for (int q = 0; q < 9; q++) v[q][it - 3] = row[q]; 431 } 432 if (!ok) continue; 433 uint64_t md[9]; 434 for (int q = 0; q < 9; q++) md[q] = med_u64(v[q], 7); 435 const double flop = 2.0 * m * k * n; 436 printf("STAGES %s in=%s budget=%u call_ms=%.3f core_ms=%.3f setup=%.3f pack=%.3f upload=%.3f launch=%.3f chip=%.3f launch_minus_chip=%.3f sync=%.3f copy_out=%.3f " 437 "tflops_chip=%.3f tflops_call=%.3f chip_share=%.1f%%\n", tok, in32 ? "f32" : "bf16", omega_gpu_matmul_cta_budget(), md[8] / 1e6, md[7] / 1e6, 438 md[0] / 1e6, md[1] / 1e6, md[2] / 1e6, md[3] / 1e6, md[4] / 1e6, (md[3] > md[4] ? md[3] - md[4] : 0) / 1e6, md[5] / 1e6, md[6] / 1e6, 439 flop / (double)md[4] / 1e3, flop / (double)md[8] / 1e3, 100.0 * (double)md[4] / (double)md[8]); 440 if (out) { 441 fprintf(out, "%s{\"m\":%u,\"k\":%u,\"n\":%u,\"input\":\"%s\",\"median_ns\":{\"setup\":%" PRIu64 ",\"pack\":%" PRIu64 ",\"upload\":%" PRIu64 ",\"launch\":%" PRIu64 442 ",\"chip\":%" PRIu64 ",\"sync\":%" PRIu64 ",\"copy_out\":%" PRIu64 ",\"core\":%" PRIu64 ",\"call\":%" PRIu64 "},\"tflops_chip\":%.4f,\"tflops_call\":%.4f}", 443 first ? "" : ",", m, k, n, in32 ? "f32" : "bf16", md[0], md[1], md[2], md[3], md[4], md[5], md[6], md[7], md[8], flop / (double)md[4] / 1e3, flop / (double)md[8] / 1e3); 444 first = 0; 445 } 446 } 447 size_t diff = 0; 448 for (size_t i = 0; i < nc; i++) diff += memcmp(&c16[i], &c32[i], 4) != 0; 449 CHECK(diff == 0, "%s bf16-in and f32-in outputs agree bit for bit (%zu differ)", tok, diff); 450 omega_gpu_tensor_free(t); 451 free(a); free(b); free(c16); free(c32); free(ah); 452 if (omega_gpu_matmul_is_blocked()) break; 453 } 454 free(copy); 455 if (out) { fprintf(out, "],\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS"); fclose(out); } 456 return 0; 457 } 458 459 static int chip_sweep(const char *shapes, const char *out_path, int mutant) { 460 FILE *out = out_path ? fopen(out_path, "w") : NULL; 461 if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } 462 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()); 463 char *copy = strdup(shapes); 464 char *save = NULL; 465 int first = 1; 466 for (char *tok = strtok_r(copy, ";", &save); tok; tok = strtok_r(NULL, ";", &save)) { 467 unsigned m, k, n; 468 if (sscanf(tok, "%u,%u,%u", &m, &k, &n) != 3) { printf("bad shape %s\n", tok); continue; } 469 size_t na = (size_t)m * k, nb = (size_t)k * n, nc = (size_t)m * n; 470 float *a = malloc(na * sizeof *a), *b = malloc(nb * sizeof *b), *c = malloc(nc * sizeof *c), *c2 = malloc(nc * sizeof *c2); 471 if (!a || !b || !c || !c2) { printf("out of host memory for %s\n", tok); free(a); free(b); free(c); free(c2); continue; } 472 uint32_t seed = 0x5eed0000u ^ (m << 20) ^ (k << 10) ^ n; 473 fill(a, na, &seed); fill(b, nb, &seed); 474 for (size_t i = 0; i < nc; i++) c[i] = -12345.0f; 475 OmegaGpuMatmulInfo info, info2; 476 double t0 = now_ms(); 477 int rc = omega_gpu_matmul_f32(m, k, n, a, b, c, &info); 478 double wall_ms = now_ms() - t0; 479 if (!mutant && rc == OMEGA_GPU_MATMUL_OK) stage_checks(tok, &info); 480 /* second call on the same shape must hit the kernel cache and agree */ 481 int rc2 = omega_gpu_matmul_f32(m, k, n, a, b, c2, &info2); 482 size_t repeat_mismatch = 0; 483 for (size_t i = 0; i < nc; i++) if (c[i] != c2[i]) repeat_mismatch++; 484 size_t e2e_bad = 0; 485 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; 486 int pass = rc == OMEGA_GPU_MATMUL_OK && info.parity_verified && e2e < 1e-5 487 && rc2 == OMEGA_GPU_MATMUL_OK && info2.kernel_cache_hit && repeat_mismatch == 0; 488 if (mutant) { 489 /* the wrong kernel must be caught: PARITY_FAIL from the API oracle and a visible e2e error */ 490 int caught = rc == OMEGA_GPU_MATMUL_PARITY_FAIL && !info.parity_verified && e2e >= 1e-5; 491 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); 492 pass = caught; 493 } else { 494 CHECK(pass, "shape %ux%ux%u rc=%s parity=%d max_rel=%g e2e_rel=%g rc2=%s hit=%d repeat_mismatch=%zu", 495 m, k, n, omega_gpu_matmul_rc_name(rc), info.parity_verified, info.max_rel_err, e2e, 496 omega_gpu_matmul_rc_name(rc2), info2.kernel_cache_hit, repeat_mismatch); 497 } 498 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", 499 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, 500 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() : ""); 501 if (out) { 502 fprintf(out, "%s{\"m\":%u,\"k\":%u,\"n\":%u,\"rc\":\"%s\",\"pass\":%s,\"chip_elapsed_ns\":%" PRIu64 503 ",\"call_ms\":%.3f,\"wall_ms\":%.3f,\"max_abs_err\":%g,\"max_rel_err\":%g,\"mismatch_count\":%zu," 504 "\"parity_verified\":%s,\"completion_marker\":%u,\"repeat_rc\":\"%s\",\"repeat_cache_hit\":%s," 505 "\"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}", 506 first ? "" : ",", m, k, n, omega_gpu_matmul_rc_name(rc), pass ? "true" : "false", 507 info.elapsed_ns, info.call_ns / 1e6, wall_ms, info.max_abs_err, info.max_rel_err, info.mismatch_count, 508 info.parity_verified ? "true" : "false", info.completion_marker, 509 omega_gpu_matmul_rc_name(rc2), info2.kernel_cache_hit ? "true" : "false", 510 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); 511 first = 0; 512 } 513 free(a); free(b); free(c); free(c2); 514 if (omega_gpu_matmul_is_blocked()) { printf("device latched after an uncertain completion; stopping the sweep\n"); break; } 515 } 516 free(copy); 517 if (out) { 518 fprintf(out, "],\"blocked\":%s,\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", omega_gpu_matmul_is_blocked() ? "true" : "false", g_checks, g_failed, 519 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")); 520 fclose(out); 521 } 522 return 0; 523 } 524 525 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; } 526 527 /* Resident path: upload B once, then 100 calls; median wall time per call must be under 5 ms. 528 * Parity: first and last result against the double reference (0 elements at or beyond 1e-5). */ 529 static int timing(const char *shapes, const char *out_path) { 530 FILE *out = out_path ? fopen(out_path, "w") : NULL; 531 if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } 532 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()); 533 char *copy = strdup(shapes); char *save = NULL; int first = 1; 534 for (char *tok = strtok_r(copy, ";", &save); tok; tok = strtok_r(NULL, ";", &save)) { 535 unsigned m, k, n; 536 if (sscanf(tok, "%u,%u,%u", &m, &k, &n) != 3) { printf("bad shape %s\n", tok); continue; } 537 size_t na = (size_t)m * k, nb = (size_t)k * n, nc = (size_t)m * n; 538 float *a = malloc(na * sizeof *a), *b = malloc(nb * sizeof *b), *c = malloc(nc * sizeof *c), *c_first = malloc(nc * sizeof *c_first); 539 uint16_t *ah = malloc(na * sizeof *ah); 540 if (!a || !b || !c || !c_first || !ah) { printf("out of host memory\n"); return 2; } 541 uint32_t seed = 0x7111e000u ^ (m << 20) ^ (k << 10) ^ n; 542 fill(a, na, &seed); fill(b, nb, &seed); 543 for (size_t i = 0; i < na; i++) ah[i] = omega_fp32_to_bf16(a[i]); 544 OmegaGpuTensor *t = NULL; 545 double tu0 = now_ms(); 546 int urc = omega_gpu_tensor_upload_f32(k, n, b, &t); 547 double upload_ms = now_ms() - tu0; 548 CHECK(urc == OMEGA_GPU_MATMUL_OK, "upload %ux%u rc=%s", k, n, omega_gpu_matmul_rc_name(urc)); 549 double samples[100]; int ok_calls = 0; OmegaGpuMatmulInfo info; memset(&info, 0, sizeof info); 550 uint64_t chip_ns_sum = 0; size_t repeat_mismatch = 0; 551 if (urc == OMEGA_GPU_MATMUL_OK) { 552 for (int i = 0; i < 100; i++) { 553 for (size_t j = 0; j < nc; j++) c[j] = -12345.0f; 554 double t0 = now_ms(); 555 int rc = omega_gpu_matmul_resident_bf16(m, ah, t, c, &info); 556 samples[i] = now_ms() - t0; 557 /* OMEGA_GPU_MATMUL_TIMING_NO_ORACLE=1: the per-call host oracle (m x n x k double MACs) only on the first 558 * call; at m = 128 it is minutes per shape and the timing is the point (2026-10-12). */ 559 if (i == 0 && getenv("OMEGA_GPU_MATMUL_TIMING_NO_ORACLE")) omega_gpu_matmul_set_oracle(0); 560 if (rc == OMEGA_GPU_MATMUL_OK) ok_calls++; 561 chip_ns_sum += info.elapsed_ns; 562 if (i == 0) memcpy(c_first, c, nc * sizeof *c); 563 else for (size_t j = 0; j < nc; j++) if (c[j] != c_first[j]) repeat_mismatch++; 564 if (omega_gpu_matmul_is_blocked()) break; 565 } 566 } 567 qsort(samples, 100, sizeof samples[0], cmp_double); 568 double median = samples[50], p90 = samples[90], mn = samples[0], mx = samples[99]; 569 size_t bad_first = 0, bad_last = 0; 570 double e2e_first = e2e_max_rel(m, k, n, a, b, c_first, &bad_first); 571 double e2e_last = e2e_max_rel(m, k, n, a, b, c, &bad_last); 572 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 */ 573 CHECK(pass, "timing %ux%ux%u ok_calls=%d median_ms=%.3f e2e_first=%g e2e_last=%g bad=%zu/%zu repeat_mismatch=%zu", 574 m, k, n, ok_calls, median, e2e_first, e2e_last, bad_first, bad_last, repeat_mismatch); 575 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", 576 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); 577 if (out) { 578 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," 579 "\"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}", 580 first ? "" : ",", m, k, n, pass ? "true" : "false", upload_ms, ok_calls, median, p90, mn, mx, 581 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); 582 first = 0; 583 } 584 omega_gpu_matmul_set_oracle(1); 585 omega_gpu_tensor_free(t); 586 free(a); free(b); free(c); free(c_first); free(ah); 587 if (omega_gpu_matmul_is_blocked()) break; 588 } 589 free(copy); 590 if (out) { 591 fprintf(out, "],\"blocked\":%s,\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", omega_gpu_matmul_is_blocked() ? "true" : "false", g_checks, g_failed, 592 g_failed == 0 ? "OMEGA_GPU_MATMUL_TIMING_PASS" : "OMEGA_GPU_MATMUL_TIMING_FAIL"); 593 fclose(out); 594 } 595 return 0; 596 } 597 598 /* --update (R3, omega #473): omega_gpu_tensor_update_* on the chip. Per shape (m rows of A, k x n weights): 599 * upload B1, matmul -> c1; update_f32(B2), matmul -> c2; fresh upload of B2, matmul -> c2f. c2 must equal c2f 600 * bit for bit and the double reference of A x B2 (0 elements at or beyond 1e-5); update_bf16(bf16(B1)) then 601 * matmul must give c1 back bit for bit. Timing, median of 5 each: update_f32, update_bf16, and the 602 * upload_f32 + free that an update replaces. */ 603 static const char *UPDATE_SHAPES = "16,768,2304;16,768,50304"; 604 static double median5(double *s) { qsort(s, 5, sizeof *s, cmp_double); return s[2]; } 605 static int update_mode(const char *shapes, const char *out_path) { 606 FILE *out = out_path ? fopen(out_path, "w") : NULL; 607 if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } 608 if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_TENSOR_UPDATE_V1\",\"cta_budget\":%u,\"timed_calls\":5,\"shapes\":[", omega_gpu_matmul_cta_budget()); 609 char *copy = strdup(shapes); char *save = NULL; int first = 1; 610 for (char *tok = strtok_r(copy, ";", &save); tok; tok = strtok_r(NULL, ";", &save)) { 611 unsigned m, k, n; 612 if (sscanf(tok, "%u,%u,%u", &m, &k, &n) != 3) { printf("bad shape %s\n", tok); continue; } 613 size_t na = (size_t)m * k, nb = (size_t)k * n, nc = (size_t)m * n; 614 float *a = malloc(na * sizeof *a), *b1 = malloc(nb * sizeof *b1), *b2 = malloc(nb * sizeof *b2); 615 float *c1 = malloc(nc * sizeof *c1), *c2 = malloc(nc * sizeof *c2), *c2f = malloc(nc * sizeof *c2f), *c3 = malloc(nc * sizeof *c3); 616 uint16_t *b1h = malloc(nb * sizeof *b1h); 617 if (!a || !b1 || !b2 || !c1 || !c2 || !c2f || !c3 || !b1h) { printf("out of host memory\n"); return 2; } 618 uint32_t seed = 0x0bda7e00u ^ (m << 20) ^ (k << 10) ^ n; 619 fill(a, na, &seed); fill(b1, nb, &seed); fill(b2, nb, &seed); 620 for (size_t i = 0; i < nb; i++) b1h[i] = omega_fp32_to_bf16(b1[i]); 621 OmegaGpuTensor *t = NULL, *tf = NULL; 622 int rc = omega_gpu_tensor_upload_f32(k, n, b1, &t); 623 CHECK(rc == OMEGA_GPU_MATMUL_OK, "update %ux%ux%u: upload B1 rc=%s", m, k, n, omega_gpu_matmul_rc_name(rc)); 624 if (rc != OMEGA_GPU_MATMUL_OK) return 2; 625 rc = omega_gpu_matmul_resident_f32(m, a, t, c1, NULL); 626 CHECK(rc == OMEGA_GPU_MATMUL_OK, "matmul B1 rc=%s", omega_gpu_matmul_rc_name(rc)); 627 rc = omega_gpu_tensor_update_f32(t, k, n, b2); 628 CHECK(rc == OMEGA_GPU_MATMUL_OK, "update_f32 rc=%s %s", omega_gpu_matmul_rc_name(rc), omega_gpu_matmul_last_error()); 629 rc = omega_gpu_matmul_resident_f32(m, a, t, c2, NULL); 630 CHECK(rc == OMEGA_GPU_MATMUL_OK, "matmul after update rc=%s", omega_gpu_matmul_rc_name(rc)); 631 rc = omega_gpu_tensor_upload_f32(k, n, b2, &tf); 632 CHECK(rc == OMEGA_GPU_MATMUL_OK, "fresh upload B2 rc=%s", omega_gpu_matmul_rc_name(rc)); 633 if (rc == OMEGA_GPU_MATMUL_OK) rc = omega_gpu_matmul_resident_f32(m, a, tf, c2f, NULL); 634 CHECK(rc == OMEGA_GPU_MATMUL_OK, "matmul fresh B2 rc=%s", omega_gpu_matmul_rc_name(rc)); 635 omega_gpu_tensor_free(tf); 636 size_t vs_fresh = 0, back = 0, bad = 0; 637 for (size_t j = 0; j < nc; j++) vs_fresh += memcmp(&c2[j], &c2f[j], 4) != 0; 638 double e2e = e2e_max_rel(m, k, n, a, b2, c2, &bad); 639 rc = omega_gpu_tensor_update_bf16(t, k, n, b1h); 640 CHECK(rc == OMEGA_GPU_MATMUL_OK, "update_bf16 rc=%s %s", omega_gpu_matmul_rc_name(rc), omega_gpu_matmul_last_error()); 641 rc = omega_gpu_matmul_resident_f32(m, a, t, c3, NULL); 642 CHECK(rc == OMEGA_GPU_MATMUL_OK, "matmul after update back rc=%s", omega_gpu_matmul_rc_name(rc)); 643 for (size_t j = 0; j < nc; j++) back += memcmp(&c3[j], &c1[j], 4) != 0; 644 CHECK(vs_fresh == 0, "update %ux%ux%u: %zu of %zu outputs differ from a fresh upload of the same values", m, k, n, vs_fresh, nc); 645 CHECK(bad == 0, "update %ux%ux%u: %zu outputs off the double reference (worst %g)", m, k, n, bad, e2e); 646 CHECK(back == 0, "update %ux%ux%u: %zu of %zu outputs differ after updating back to B1", m, k, n, back, nc); 647 double s_f32[5], s_bf16[5], s_up[5]; 648 int timed_ok = 1; 649 for (int i = 0; i < 5; i++) { 650 double t0 = now_ms(); timed_ok &= omega_gpu_tensor_update_f32(t, k, n, b2) == OMEGA_GPU_MATMUL_OK; s_f32[i] = now_ms() - t0; 651 t0 = now_ms(); timed_ok &= omega_gpu_tensor_update_bf16(t, k, n, b1h) == OMEGA_GPU_MATMUL_OK; s_bf16[i] = now_ms() - t0; 652 t0 = now_ms(); timed_ok &= omega_gpu_tensor_upload_f32(k, n, b2, &tf) == OMEGA_GPU_MATMUL_OK; omega_gpu_tensor_free(tf); s_up[i] = now_ms() - t0; 653 } 654 CHECK(timed_ok, "update %ux%ux%u: a timed call failed", m, k, n); 655 double mf = median5(s_f32), mb = median5(s_bf16), mu = median5(s_up); 656 int pass = vs_fresh == 0 && bad == 0 && back == 0 && timed_ok; 657 printf("%s update %ux%ux%u vs_fresh=%zu e2e=%g bad=%zu back=%zu update_f32_ms=%.2f update_bf16_ms=%.2f upload_f32_free_ms=%.2f\n", 658 pass ? "PASS" : "FAIL", m, k, n, vs_fresh, e2e, bad, back, mf, mb, mu); 659 if (out) { 660 fprintf(out, "%s{\"m\":%u,\"k\":%u,\"n\":%u,\"pass\":%s,\"vs_fresh_diff\":%zu,\"e2e_max_rel\":%g,\"e2e_bad\":%zu,\"back_diff\":%zu," 661 "\"update_f32_ms\":%.3f,\"update_bf16_ms\":%.3f,\"upload_f32_free_ms\":%.3f}", 662 first ? "" : ",", m, k, n, pass ? "true" : "false", vs_fresh, e2e, bad, back, mf, mb, mu); 663 first = 0; 664 } 665 omega_gpu_tensor_free(t); 666 free(a); free(b1); free(b2); free(c1); free(c2); free(c2f); free(c3); free(b1h); 667 if (omega_gpu_matmul_is_blocked()) break; 668 } 669 free(copy); 670 if (out) { 671 fprintf(out, "],\"blocked\":%s,\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", omega_gpu_matmul_is_blocked() ? "true" : "false", g_checks, g_failed, 672 g_failed == 0 ? "OMEGA_GPU_TENSOR_UPDATE_PASS" : "OMEGA_GPU_TENSOR_UPDATE_FAIL"); 673 fclose(out); 674 } 675 return 0; 676 } 677 678 int main(int argc, char **argv) { 679 const char *out_path = NULL; 680 int host = 0, mutant = 0, do_timing = 0, do_update = 0, do_stages = 0; 681 for (int i = 1; i < argc; i++) { 682 if (strcmp(argv[i], "--host-only") == 0) host = 1; 683 else if (strcmp(argv[i], "--mutant") == 0) mutant = 1; 684 else if (strcmp(argv[i], "--timing") == 0) do_timing = 1; 685 else if (strcmp(argv[i], "--update") == 0) do_update = 1; 686 else if (strcmp(argv[i], "--stages") == 0) do_stages = 1; 687 else if (strcmp(argv[i], "--cta-budget") == 0 && i + 1 < argc) omega_gpu_matmul_set_cta_budget((uint32_t)strtoul(argv[++i], NULL, 10)); 688 else if (strcmp(argv[i], "--spin-us") == 0 && i + 1 < argc) { 689 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; } 690 } 691 else if (strcmp(argv[i], "--unroll") == 0 && i + 1 < argc) omega_gpu_matmul_test_set_unroll((uint32_t)strtoul(argv[++i], NULL, 10)); 692 else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i]; 693 } 694 const char *shapes = getenv("OMEGA_GPU_MATMUL_SHAPES"); 695 if (shapes && !*shapes) shapes = NULL; 696 int rc = 0; 697 if (host) host_only(); 698 else if (do_timing) rc = timing(shapes ? shapes : TIMING_SHAPES, out_path); 699 else if (do_update) rc = update_mode(shapes ? shapes : UPDATE_SHAPES, out_path); 700 else if (do_stages) rc = stages_profile(shapes ? shapes : STAGE_SHAPES, out_path); 701 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); } 702 else rc = chip_sweep(shapes ? shapes : DEFAULT_SHAPES, out_path, 0); 703 if (!host && !do_timing && !do_update && !do_stages && !mutant && !omega_gpu_matmul_is_blocked()) one_row_repack_checks(); 704 if (!omega_gpu_matmul_is_blocked()) omega_gpu_device_close(); 705 if (rc) return 2; 706 printf("%s: %d checks, %d failed%s\n", g_failed ? "FAIL" : "PASS", g_checks, g_failed, mutant ? (g_failed ? " (mutant SURVIVED)" : " (mutant killed)") : ""); 707 return g_failed ? 1 : 0; 708 }