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