omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tests / gpu_moe_window_test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* gpu_moe_window_test: OWN-30B cut 2 gate for omega_gpu_moe_api (column windows over a resident tensor). |
| 18 | * |
| 19 | * ./gpu_moe_window_test --host-only argument refusals (alignment rule fail-closed) and the packed |
| 20 | * window byte rule (a window's bytes in the packed tensor equal |
| 21 | * the pack of the sub-matrix; one tile of skew differs); no chip |
| 22 | * ./gpu_moe_window_test --out receipt.json chip gate: for every case the window result equals the |
| 23 | * whole-tensor (omega_gpu_matmul_resident_*) columns bit for bit |
| 24 | * and the double oracle; repeat call is a cache hit and identical; |
| 25 | * the multi-window call equals the single windows; negative |
| 26 | * controls (wrong window by one tile, wrong kernel) are caught; |
| 27 | * timing of 8 expert windows per call (the Stage A decode shape) |
| 28 | * ./gpu_moe_window_test --cta-budget N --spin-us N --calls N --small (knobs; --small skips the 128-expert tensors) |
| 29 | */ |
| 30 | #include "omega_gpu_moe_api.h" |
| 31 | #include "omega_gpu_session.h" |
| 32 | #include "omega_blackwell_codegen.h" |
| 33 | #include "omega_blackwell_matmul.h" |
| 34 | #include <inttypes.h> |
| 35 | #include <math.h> |
| 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | #include <time.h> |
| 40 | |
| 41 | static int g_checks, g_failed; |
| 42 | #define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) |
| 43 | |
| 44 | static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } |
| 45 | static double now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; } |
| 46 | static void fill(float *a, size_t n, uint32_t *seed) { for (size_t i = 0; i < n; i++) a[i] = ((float)(lcg(seed) % 2001) - 1000.0f) / 1000.0f; } |
| 47 | static float bf2f(uint16_t b) { uint32_t u = (uint32_t)b << 16; float f; memcpy(&f, &u, 4); return f; } |
| 48 | 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; } |
| 49 | |
| 50 | /* double oracle of the window: error relative to the accumulation scale, count at or beyond 1e-5 */ |
| 51 | static double window_oracle(uint32_t m, uint32_t k, uint32_t n, const uint16_t *a, const uint16_t *b, uint32_t col0, uint32_t cols, const float *c, size_t *bad) { |
| 52 | double worst = 0.0; size_t nbad = 0; |
| 53 | for (size_t i = 0; i < m; i++) for (size_t j = 0; j < cols; j++) { |
| 54 | double acc = 0.0, scale = 0.0; |
| 55 | for (size_t t = 0; t < k; t++) { double p = (double)bf2f(a[i * k + t]) * (double)bf2f(b[t * n + col0 + j]); acc += p; scale += fabs(p); } |
| 56 | double rel = fabs(acc - (double)c[i * cols + j]) / (scale > 1e-6 ? scale : 1e-6); |
| 57 | if (rel > worst) worst = rel; |
| 58 | if (rel >= 1e-5) nbad++; |
| 59 | } |
| 60 | if (bad) *bad = nbad; |
| 61 | return worst; |
| 62 | } |
| 63 | |
| 64 | /* ------------------------------------------------------------------ host only */ |
| 65 | static void host_only(void) { |
| 66 | /* the packed window byte rule: for a tile-aligned col0, the bytes of the packed tensor from |
| 67 | * omega_gpu_moe_window_byte_offset(kp, col0) are the pack of B[:, col0 .. col0 + wnp); moving one |
| 68 | * tile (the wrong-window control) changes them */ |
| 69 | struct { uint32_t k, n, col0, cols; } sh[] = { { 48, 1000, 8, 13 }, { 48, 1000, 792, 200 }, { 2048, 6144, 1536, 1536 }, { 768, 8192, 6144, 2048 }, { 40, 24, 16, 8 } }; |
| 70 | for (size_t s = 0; s < sizeof sh / sizeof sh[0]; s++) { |
| 71 | const uint32_t k = sh[s].k, n = sh[s].n, col0 = sh[s].col0, cols = sh[s].cols; |
| 72 | const uint32_t kp = (k + 15) / 16 * 16, np = (n + 7) / 8 * 8, NT = np / 8, wnp = (cols + 7) / 8 * 8, WT = wnp / 8; |
| 73 | uint16_t *b = malloc((size_t)k * n * 2), *full = malloc((size_t)kp * np * 2), *win = malloc((size_t)kp * wnp * 2), *sub = malloc((size_t)k * wnp * 2); |
| 74 | uint32_t seed = 11 + (uint32_t)s; |
| 75 | for (size_t i = 0; i < (size_t)k * n; i++) b[i] = (uint16_t)(1 + lcg(&seed) % 60000); |
| 76 | omega_matmul_frag_pack_b(b, k, n, kp, 0, NT, full); |
| 77 | /* the sub-matrix B[:, col0 .. col0 + real) where real = min(wnp, n - col0): past the real columns the |
| 78 | * tensor's padding is zero, which the sub-matrix pack reproduces by having only `real` columns */ |
| 79 | const uint32_t real = n - col0 < wnp ? n - col0 : wnp; |
| 80 | for (uint32_t r = 0; r < k; r++) for (uint32_t j = 0; j < real; j++) sub[(size_t)r * real + j] = b[(size_t)r * n + col0 + j]; |
| 81 | omega_matmul_frag_pack_b(sub, k, real, kp, 0, WT, win); |
| 82 | const uint64_t off = omega_gpu_moe_window_byte_offset(kp, col0); |
| 83 | CHECK(off == (uint64_t)(col0 / 8) * kp * 8 * 2, "offset arithmetic k=%u col0=%u", k, col0); |
| 84 | CHECK(memcmp((const uint8_t *)full + off, win, (size_t)kp * wnp * 2) == 0, "window bytes k=%u n=%u col0=%u cols=%u equal the sub-matrix pack", k, n, col0, cols); |
| 85 | /* wrong window: one tile of skew (either way) must differ */ |
| 86 | int differs = 1; |
| 87 | if (col0 / 8 + 1 + WT <= NT) differs = memcmp((const uint8_t *)full + off + (size_t)kp * 8 * 2, win, (size_t)kp * wnp * 2) != 0; |
| 88 | else differs = memcmp((const uint8_t *)full + off - (size_t)kp * 8 * 2, win, (size_t)kp * wnp * 2) != 0; |
| 89 | CHECK(differs, "window bytes k=%u col0=%u: one tile of skew differs (wrong-window control)", k, col0); |
| 90 | free(b); free(full); free(win); free(sub); |
| 91 | } |
| 92 | /* refusals: nothing below opens the device (the alignment rule is checked before the lock) */ |
| 93 | float a[64] = { 0 }, c[64]; |
| 94 | uint32_t col0s[2] = { 0, 8 }; |
| 95 | CHECK(omega_gpu_matmul_resident_window_f32(1, NULL, NULL, 0, 8, c, NULL) == OMEGA_GPU_MATMUL_BAD_ARGS, "NULL a / tensor refused"); |
| 96 | CHECK(omega_gpu_matmul_resident_window_f32(0, a, NULL, 0, 8, c, NULL) == OMEGA_GPU_MATMUL_BAD_ARGS, "m = 0 refused"); |
| 97 | CHECK(omega_gpu_matmul_resident_windows_f32(1, a, NULL, 0, col0s, 8, c, NULL) == OMEGA_GPU_MATMUL_BAD_ARGS, "nwin = 0 refused"); |
| 98 | CHECK(omega_gpu_moe_tensor_upload_bf16(0, 8, (const uint16_t *)a, NULL) == OMEGA_GPU_MATMUL_BAD_ARGS, "upload k = 0 / NULL out refused"); |
| 99 | OmegaGpuMoeTensor *t = NULL; |
| 100 | CHECK(omega_gpu_moe_tensor_upload_bf16(16, OMEGA_BW_MATMUL_MAX_N + 8, (const uint16_t *)a, &t) == OMEGA_GPU_MATMUL_TOO_LARGE && t == NULL, "upload n past the envelope refused"); |
| 101 | CHECK(omega_gpu_moe_reserve(0, 16, 16, 0) == OMEGA_GPU_MATMUL_BAD_ARGS, "reserve zero bound refused"); |
| 102 | CHECK(omega_gpu_moe_prepare(1, 16, OMEGA_BW_MATMUL_MAX_N + 8) == OMEGA_GPU_MATMUL_TOO_LARGE, "prepare past the envelope refused"); |
| 103 | uint32_t kk, nn; omega_gpu_moe_tensor_shape(NULL, &kk, &nn); |
| 104 | CHECK(kk == 0 && nn == 0, "shape of NULL is 0 x 0"); |
| 105 | omega_gpu_moe_tensor_free(NULL); |
| 106 | printf("host-only: alignment refusals that need a tensor (col0 %% 8, window past n) run in the chip gate\n"); |
| 107 | } |
| 108 | |
| 109 | /* ------------------------------------------------------------------ chip */ |
| 110 | typedef struct { uint32_t k, n, m, cols, nwin; const char *name; int big; } Case; |
| 111 | |
| 112 | static int chip(const char *out_path, int calls, int small) { |
| 113 | FILE *out = out_path ? fopen(out_path, "w") : NULL; |
| 114 | if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } |
| 115 | if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_MOE_WINDOW_V1\",\"cta_budget\":%u,\"spin_us\":%u,\"calls\":%d,\"cases\":[", |
| 116 | omega_gpu_moe_cta_budget(), omega_gpu_session_spin_us(), calls); |
| 117 | const Case cases[] = { |
| 118 | { 48, 1000, 1, 13, 3, "small_m1_unaligned_cols", 0 }, |
| 119 | { 48, 1000, 5, 200, 2, "small_m5", 0 }, |
| 120 | { 2048, 1536 * 16, 1, 1536, 8, "gate_up_16_experts", 0 }, |
| 121 | { 2048, 1536 * 128, 1, 1536, 8, "gate_up_128_experts", 1 }, |
| 122 | { 768, 2048 * 128, 1, 2048, 8, "down_128_experts", 1 }, |
| 123 | { 2048, 1536 * 16, 16, 1536, 8, "gate_up_16_experts_m16", 0 }, |
| 124 | /* one variable: the same 1536-wide window on a standalone one-expert tensor (one spare tile so the |
| 125 | * skew control stays inside the allocation); compares against the window inside the 16/128-expert tensors */ |
| 126 | { 2048, 1536 + 8, 1, 1536, 1, "gate_up_1_expert_standalone", 0 }, |
| 127 | { 768, 2048 + 8, 1, 2048, 1, "down_1_expert_standalone", 0 }, |
| 128 | }; |
| 129 | int first = 1; |
| 130 | for (size_t ci = 0; ci < sizeof cases / sizeof cases[0]; ci++) { |
| 131 | const Case *cs = &cases[ci]; |
| 132 | if (small && cs->big) continue; |
| 133 | const uint32_t k = cs->k, n = cs->n, m = cs->m, cols = cs->cols, nwin = cs->nwin; |
| 134 | const uint32_t kp = (k + 15) / 16 * 16, wnp = (cols + 7) / 8 * 8; |
| 135 | printf("== %s: k=%u n=%u m=%u cols=%u windows=%u\n", cs->name, k, n, m, cols, nwin); |
| 136 | size_t na = (size_t)m * k, nb = (size_t)k * n, nc = (size_t)m * n, nw = (size_t)m * cols; |
| 137 | float *a = malloc(na * 4), *whole = malloc(nc * 4), *cw = malloc(nw * 4), *cw2 = malloc(nw * 4), *multi = malloc(nw * 4 * nwin); |
| 138 | uint16_t *ah = malloc(na * 2), *bh = malloc(nb * 2); |
| 139 | if (!a || !whole || !cw || !cw2 || !multi || !ah || !bh) { printf("out of host memory\n"); return 2; } |
| 140 | uint32_t seed = 0x30b0000u ^ (k << 8) ^ n ^ m; |
| 141 | fill(a, na, &seed); |
| 142 | for (size_t i = 0; i < na; i++) ah[i] = omega_fp32_to_bf16(a[i]); |
| 143 | { float *bt = malloc((size_t)1 << 20); size_t done = 0; /* bf16 B in 256K-element chunks, no k x n f32 copy */ |
| 144 | while (done < nb) { size_t c = nb - done < (1u << 18) ? nb - done : (1u << 18); fill(bt, c, &seed); for (size_t i = 0; i < c; i++) bh[done + i] = omega_fp32_to_bf16(bt[i]); done += c; } |
| 145 | free(bt); } |
| 146 | /* the 8 windows: distinct tile-aligned expert slots, never the last tile (the skew control moves one tile up) */ |
| 147 | uint32_t col0[8]; const uint32_t slots = n / cols; |
| 148 | for (uint32_t w = 0; w < nwin; w++) { uint32_t s; int dup; do { s = lcg(&seed) % (slots > 1 ? slots - 1 : 1); dup = 0; for (uint32_t v = 0; v < w; v++) if (col0[v] == s * cols) dup = 1; } while (dup); col0[w] = s * cols; } |
| 149 | if (cs->cols == 13) { col0[0] = 8; col0[1] = 16; col0[2] = 976; } /* 976 + 13 = 989 < 1000, inside the real columns */ |
| 150 | if (cs->cols == 200) { col0[0] = 792; col0[1] = 0; } /* 792 + 200 = 992 */ |
| 151 | |
| 152 | OmegaGpuTensor *tc = NULL; OmegaGpuMoeTensor *tm = NULL; |
| 153 | double t0 = now_ms(); |
| 154 | int rc1 = omega_gpu_tensor_upload_bf16(k, n, bh, &tc); |
| 155 | double up_classic = now_ms() - t0; t0 = now_ms(); |
| 156 | int rc2 = omega_gpu_moe_tensor_upload_bf16(k, n, bh, &tm); |
| 157 | double up_moe = now_ms() - t0; |
| 158 | CHECK(rc1 == OMEGA_GPU_MATMUL_OK && rc2 == OMEGA_GPU_MATMUL_OK, "%s: uploads (classic %s, moe %s)", cs->name, omega_gpu_matmul_rc_name(rc1), omega_gpu_matmul_rc_name(rc2)); |
| 159 | printf(" upload classic %.0f ms, moe %.0f ms (%zu MB bf16)\n", up_classic, up_moe, nb * 2 / (1u << 20)); |
| 160 | if (rc1 != OMEGA_GPU_MATMUL_OK || rc2 != OMEGA_GPU_MATMUL_OK) break; |
| 161 | |
| 162 | /* alignment rule, fail-closed, with the tensor present */ |
| 163 | OmegaGpuMatmulInfo info; |
| 164 | int rr = omega_gpu_matmul_resident_window_bf16(m, ah, tm, 4, 8, cw, &info); |
| 165 | CHECK(rr == OMEGA_GPU_MATMUL_BAD_ARGS && strstr(omega_gpu_matmul_last_error(), "not a multiple of 8"), "%s: col0 = 4 refused (%s: %s)", cs->name, omega_gpu_matmul_rc_name(rr), omega_gpu_matmul_last_error()); |
| 166 | rr = omega_gpu_matmul_resident_window_bf16(m, ah, tm, (n - 8) / 8 * 8, 16, cw, &info); |
| 167 | CHECK(rr == OMEGA_GPU_MATMUL_BAD_ARGS && strstr(omega_gpu_matmul_last_error(), "not inside"), "%s: window past n refused (%s: %s)", cs->name, omega_gpu_matmul_rc_name(rr), omega_gpu_matmul_last_error()); |
| 168 | rr = omega_gpu_matmul_resident_window_bf16(m, ah, tm, 0, 0, cw, &info); |
| 169 | CHECK(rr == OMEGA_GPU_MATMUL_BAD_ARGS, "%s: cols = 0 refused (%s)", cs->name, omega_gpu_matmul_rc_name(rr)); |
| 170 | |
| 171 | /* whole-tensor product through the existing API (the reference the window must match column for column) */ |
| 172 | OmegaGpuMatmulInfo wi; |
| 173 | int wrc = omega_gpu_matmul_resident_bf16(m, ah, tc, whole, &wi); |
| 174 | CHECK(wrc == OMEGA_GPU_MATMUL_OK, "%s: whole-tensor call rc=%s (%s)", cs->name, omega_gpu_matmul_rc_name(wrc), omega_gpu_matmul_last_error()); |
| 175 | if (wrc != OMEGA_GPU_MATMUL_OK) break; |
| 176 | printf(" whole: %.3f ms call, chip %.3f ms, grid %ux%u\n", wi.call_ns / 1e6, wi.elapsed_ns / 1e6, wi.grid_x, wi.grid_y); |
| 177 | |
| 178 | size_t mism_total = 0, oracle_bad_total = 0; double oracle_worst = 0.0; int all_hit = 1, f32_same = 1; double win_chip_ms = 0.0; |
| 179 | for (uint32_t w = 0; w < nwin; w++) { |
| 180 | OmegaGpuMatmulInfo i1, i2, i3; |
| 181 | int r1 = omega_gpu_matmul_resident_window_bf16(m, ah, tm, col0[w], cols, cw, &i1); |
| 182 | CHECK(r1 == OMEGA_GPU_MATMUL_OK && i1.n == cols && i1.padded_n == wnp && i1.resident, "%s: window %u (col0 %u) rc=%s (%s)", cs->name, w, col0[w], omega_gpu_matmul_rc_name(r1), omega_gpu_matmul_last_error()); |
| 183 | if (r1 != OMEGA_GPU_MATMUL_OK) break; |
| 184 | size_t mism = 0; |
| 185 | for (uint32_t r = 0; r < m; r++) for (uint32_t j = 0; j < cols; j++) if (cw[(size_t)r * cols + j] != whole[(size_t)r * n + col0[w] + j]) mism++; |
| 186 | mism_total += mism; |
| 187 | size_t bad = 0; double worst = window_oracle(m, k, n, ah, bh, col0[w], cols, cw, &bad); |
| 188 | oracle_bad_total += bad; if (worst > oracle_worst) oracle_worst = worst; |
| 189 | /* repeat: cache hit, bit-identical; f32 activation path: identical too (same bf16 rounding) */ |
| 190 | int r2 = omega_gpu_matmul_resident_window_bf16(m, ah, tm, col0[w], cols, cw2, &i2); |
| 191 | int r3 = omega_gpu_matmul_resident_window_f32(m, a, tm, col0[w], cols, multi, &i3); |
| 192 | if (!(r2 == OMEGA_GPU_MATMUL_OK && i2.kernel_cache_hit && memcmp(cw, cw2, nw * 4) == 0)) all_hit = 0; |
| 193 | if (!(r3 == OMEGA_GPU_MATMUL_OK && memcmp(cw, multi, nw * 4) == 0)) f32_same = 0; |
| 194 | win_chip_ms += i1.elapsed_ns / 1e6; |
| 195 | printf(" window %u col0=%u: mismatches vs whole %zu/%zu, oracle worst %.2e bad %zu, chip %.3f ms, grid %ux%u, hit=%d\n", |
| 196 | w, col0[w], mism, nw, worst, bad, i1.elapsed_ns / 1e6, i1.grid_x, i1.grid_y, (int)i2.kernel_cache_hit); |
| 197 | if (omega_gpu_matmul_is_blocked()) break; |
| 198 | } |
| 199 | CHECK(mism_total == 0, "%s: every window equals the whole-tensor columns bit for bit (%zu mismatches)", cs->name, mism_total); |
| 200 | CHECK(oracle_bad_total == 0, "%s: every window within 1e-5 of the double oracle (worst %g, %zu bad)", cs->name, oracle_worst, oracle_bad_total); |
| 201 | CHECK(all_hit, "%s: repeated window calls hit the kernel cache and are bit-identical", cs->name); |
| 202 | CHECK(f32_same, "%s: f32 activation path is bit-identical to the bf16 one", cs->name); |
| 203 | if (omega_gpu_matmul_is_blocked()) break; |
| 204 | |
| 205 | /* the multi-window call (one activation pack, nwin launches) equals the single windows */ |
| 206 | OmegaGpuMatmulInfo mi; |
| 207 | int mrc = omega_gpu_matmul_resident_windows_f32(m, a, tm, nwin, col0, cols, multi, &mi); |
| 208 | size_t mm = 0; |
| 209 | if (mrc == OMEGA_GPU_MATMUL_OK) for (uint32_t w = 0; w < nwin; w++) for (uint32_t r = 0; r < m; r++) for (uint32_t j = 0; j < cols; j++) |
| 210 | if (multi[(size_t)w * nw + (size_t)r * cols + j] != whole[(size_t)r * n + col0[w] + j]) mm++; |
| 211 | CHECK(mrc == OMEGA_GPU_MATMUL_OK && mi.chip_calls == nwin && mm == 0, "%s: multi-window call rc=%s launches=%u mismatches=%zu", cs->name, omega_gpu_matmul_rc_name(mrc), mi.chip_calls, mm); |
| 212 | |
| 213 | /* negative control 1: the wrong window (one tile up) must disagree with the whole-tensor columns */ |
| 214 | omega_gpu_moe_test_set_window_skew(1); |
| 215 | int src = omega_gpu_matmul_resident_window_bf16(m, ah, tm, col0[0], cols, cw, &info); |
| 216 | size_t skew_mism = 0; |
| 217 | for (uint32_t r = 0; r < m; r++) for (uint32_t j = 0; j < cols; j++) if (cw[(size_t)r * cols + j] != whole[(size_t)r * n + col0[0] + j]) skew_mism++; |
| 218 | omega_gpu_moe_test_set_window_skew(0); |
| 219 | CHECK(src == OMEGA_GPU_MATMUL_OK && skew_mism > nw / 2, "%s: wrong-window control caught (rc=%s, %zu/%zu differ)", cs->name, omega_gpu_matmul_rc_name(src), skew_mism, nw); |
| 220 | /* negative control 2: the wrong kernel (last K pass dropped) must fail the oracle */ |
| 221 | omega_gpu_moe_test_set_mutant(1); |
| 222 | int mut = omega_gpu_matmul_resident_window_bf16(m, ah, tm, col0[0], cols, cw, &info); |
| 223 | size_t mut_bad = 0; double mut_worst = mut == OMEGA_GPU_MATMUL_OK ? window_oracle(m, k, n, ah, bh, col0[0], cols, cw, &mut_bad) : 0.0; |
| 224 | omega_gpu_moe_test_set_mutant(0); |
| 225 | CHECK(mut == OMEGA_GPU_MATMUL_OK && mut_bad > 0, "%s: wrong-kernel control caught (rc=%s, worst %g, %zu bad)", cs->name, omega_gpu_matmul_rc_name(mut), mut_worst, mut_bad); |
| 226 | if (omega_gpu_matmul_is_blocked()) break; |
| 227 | |
| 228 | /* timing: `calls` multi-window calls (the per-token expert pattern) and `calls` single-window calls */ |
| 229 | double *sm = malloc((size_t)calls * sizeof *sm), *ss = malloc((size_t)calls * sizeof *ss); |
| 230 | uint64_t chip_multi = 0, chip_single = 0; int ok_m = 0, ok_s = 0; size_t rep_mism = 0; |
| 231 | for (int i = 0; i < calls; i++) { |
| 232 | double q0 = now_ms(); |
| 233 | int r = omega_gpu_matmul_resident_windows_f32(m, a, tm, nwin, col0, cols, multi, &mi); |
| 234 | sm[i] = now_ms() - q0; |
| 235 | if (r == OMEGA_GPU_MATMUL_OK) { ok_m++; chip_multi += mi.elapsed_ns; } |
| 236 | if (omega_gpu_matmul_is_blocked()) break; |
| 237 | } |
| 238 | for (uint32_t w = 0; w < nwin; w++) for (uint32_t r = 0; r < m; r++) for (uint32_t j = 0; j < cols; j++) |
| 239 | if (multi[(size_t)w * nw + (size_t)r * cols + j] != whole[(size_t)r * n + col0[w] + j]) rep_mism++; |
| 240 | for (int i = 0; i < calls && !omega_gpu_matmul_is_blocked(); i++) { |
| 241 | double q0 = now_ms(); |
| 242 | int r = omega_gpu_matmul_resident_window_f32(m, a, tm, col0[i % nwin], cols, cw, &info); |
| 243 | ss[i] = now_ms() - q0; |
| 244 | if (r == OMEGA_GPU_MATMUL_OK) { ok_s++; chip_single += info.elapsed_ns; } |
| 245 | } |
| 246 | qsort(sm, (size_t)calls, sizeof *sm, cmp_double); qsort(ss, (size_t)calls, sizeof *ss, cmp_double); |
| 247 | const double med_m = sm[calls / 2], med_s = ss[calls / 2]; |
| 248 | const double bytes_win = (double)kp * wnp * 2.0; |
| 249 | const double gbs_multi = bytes_win * nwin / (med_m * 1e-3) / 1e9, gbs_single = bytes_win / (med_s * 1e-3) / 1e9; |
| 250 | CHECK(ok_m == calls && ok_s == calls && rep_mism == 0, "%s: timing calls ok %d/%d multi, %d/%d single, last multi result mismatches %zu", cs->name, ok_m, calls, ok_s, calls, rep_mism); |
| 251 | printf(" timing: %u windows/call median %.3f ms (%.1f us/window, chip mean %.1f us/window, %.1f GB/s); single window median %.3f ms (%.1f GB/s, chip mean %.1f us); %.2f MB per window\n", |
| 252 | nwin, med_m, med_m * 1e3 / nwin, ok_m ? chip_multi / 1e3 / ok_m / nwin : 0.0, gbs_multi, med_s, gbs_single, ok_s ? chip_single / 1e3 / ok_s : 0.0, bytes_win / 1e6); |
| 253 | if (out) { |
| 254 | fprintf(out, "%s{\"case\":\"%s\",\"k\":%u,\"n\":%u,\"m\":%u,\"cols\":%u,\"padded_cols\":%u,\"windows\":%u,\"col0\":[", first ? "" : ",", cs->name, k, n, m, cols, wnp, nwin); |
| 255 | for (uint32_t w = 0; w < nwin; w++) fprintf(out, "%s%u", w ? "," : "", col0[w]); |
| 256 | fprintf(out, "],\"upload_classic_ms\":%.1f,\"upload_moe_ms\":%.1f,\"whole_call_ms\":%.4f,\"whole_chip_ms\":%.4f," |
| 257 | "\"window_mismatches_vs_whole\":%zu,\"oracle_worst_rel\":%g,\"oracle_bad\":%zu,\"repeat_cache_hit_identical\":%s,\"f32_path_identical\":%s," |
| 258 | "\"multi_rc\":\"%s\",\"multi_mismatches\":%zu,\"skew_control_differ\":%zu,\"skew_control_elems\":%zu,\"mutant_control_bad\":%zu,\"mutant_control_worst\":%g," |
| 259 | "\"calls\":%d,\"multi_median_ms\":%.4f,\"multi_p90_ms\":%.4f,\"multi_min_ms\":%.4f,\"multi_chip_us_per_window\":%.2f,\"multi_gbs\":%.1f," |
| 260 | "\"single_median_ms\":%.4f,\"single_p90_ms\":%.4f,\"single_min_ms\":%.4f,\"single_chip_us\":%.2f,\"single_gbs\":%.1f,\"bytes_per_window\":%.0f,\"grid_x\":%u,\"grid_y\":%u}", |
| 261 | up_classic, up_moe, wi.call_ns / 1e6, wi.elapsed_ns / 1e6, |
| 262 | mism_total, oracle_worst, oracle_bad_total, all_hit ? "true" : "false", f32_same ? "true" : "false", |
| 263 | omega_gpu_matmul_rc_name(mrc), mm, skew_mism, nw, mut_bad, mut_worst, |
| 264 | calls, med_m, sm[calls * 9 / 10], sm[0], ok_m ? chip_multi / 1e3 / ok_m / nwin : 0.0, gbs_multi, |
| 265 | med_s, ss[calls * 9 / 10], ss[0], ok_s ? chip_single / 1e3 / ok_s : 0.0, gbs_single, bytes_win, info.grid_x, info.grid_y); |
| 266 | first = 0; |
| 267 | } |
| 268 | free(sm); free(ss); |
| 269 | omega_gpu_tensor_free(tc); omega_gpu_moe_tensor_free(tm); |
| 270 | free(a); free(whole); free(cw); free(cw2); free(multi); free(ah); free(bh); |
| 271 | if (omega_gpu_matmul_is_blocked()) break; |
| 272 | } |
| 273 | if (out) { |
| 274 | fprintf(out, "],\"blocked\":%s,\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", omega_gpu_matmul_is_blocked() ? "true" : "false", g_checks, g_failed, |
| 275 | g_failed == 0 ? "OMEGA_GPU_MOE_WINDOW_PASS" : "OMEGA_GPU_MOE_WINDOW_FAIL"); |
| 276 | fclose(out); |
| 277 | } |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | int main(int argc, char **argv) { |
| 282 | const char *out_path = NULL; int host = 0, calls = 100, small = 0; |
| 283 | for (int i = 1; i < argc; i++) { |
| 284 | if (strcmp(argv[i], "--host-only") == 0) host = 1; |
| 285 | else if (strcmp(argv[i], "--small") == 0) small = 1; |
| 286 | else if (strcmp(argv[i], "--calls") == 0 && i + 1 < argc) calls = atoi(argv[++i]); |
| 287 | else if (strcmp(argv[i], "--cta-budget") == 0 && i + 1 < argc) { uint32_t b = (uint32_t)strtoul(argv[++i], NULL, 10); omega_gpu_moe_set_cta_budget(b); omega_gpu_matmul_set_cta_budget(b); } |
| 288 | else if (strcmp(argv[i], "--spin-us") == 0 && i + 1 < argc) { 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; } } |
| 289 | else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i]; |
| 290 | } |
| 291 | if (calls < 2) calls = 2; |
| 292 | int rc = 0; |
| 293 | if (host) host_only(); |
| 294 | else rc = chip(out_path, calls, small); |
| 295 | if (!omega_gpu_matmul_is_blocked()) omega_gpu_device_close(); |
| 296 | if (rc) return 2; |
| 297 | printf("%s: %d checks, %d failed\n", g_failed ? "FAIL" : "PASS", g_checks, g_failed); |
| 298 | return g_failed ? 1 : 0; |
| 299 | } |