CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / evidence / GEMM-GENERAL-AB-20261027 / gemm_ex_ab.c
| 1 | /* gemm_ex_ab.c -- GemmGeneral served-shape A/B timing driver (AB-PLAN.md in this directory). |
| 2 | * Measurement harness only, not product code. One process, one build (CUDApp gemm-general/gemm-ex at the plan's sha): |
| 3 | * arm A = omega_gpu_gemm_ld (src/omega_gpu_gemm.c, C3: the existing entry for these shapes) |
| 4 | * arm B = omega_gpu_gemm_ex (src/omega_gpu_gemm_ex.c: the general entry, same shapes, same inputs) |
| 5 | * Weights are stored [out][in] (A row-major M x K, B N rows of K): C3 layout OMEGA_GPU_GEMM_B_COL, gemm_ex op_a N, op_b T. |
| 6 | * Round 0 runs every cell through both arms and is labelled warm=1 (kernel builds, first touch); rounds 1..R are timed. |
| 7 | * In every round the cell order is a seeded shuffle and the arm order inside a cell a seeded coin. |
| 8 | * Per pair: wall_ns of each call (CLOCK_MONOTONIC around the call), rc, B's route and core_calls, the live server's |
| 9 | * in_flight before and after the pair, and the correctness gate: C_B == C_A bit for bit, plus 64 sampled elements of C_A |
| 10 | * within K * 2^-22 * sum|a b| of a double host reference. |
| 11 | * After the timed rounds, the negative control: gemm_ex mutant 1 (drops the last k) on one cell must fail both the bitwise |
| 12 | * check and the reference check; a control that stays green fails the run. |
| 13 | * gemm_ex_ab --rounds R [--seed S] [--spin-us US] |
| 14 | * Why C: it links the C library under test; no .om binding of either entry exists. Copied unchanged into the evidence. */ |
| 15 | #include "omega_gpu_gemm.h" |
| 16 | #include "omega_gpu_gemm_ex.h" |
| 17 | #include "omega_gpu_session.h" |
| 18 | #include <inttypes.h> |
| 19 | #include <math.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <time.h> |
| 24 | |
| 25 | static int in_flight(void) { |
| 26 | FILE *f = popen("curl -s -m 2 127.0.0.1:18091/health", "r"); |
| 27 | if (!f) return -1; |
| 28 | char buf[1024]; size_t n = fread(buf, 1, sizeof buf - 1, f); buf[n] = 0; |
| 29 | pclose(f); |
| 30 | const char *p = strstr(buf, "\"in_flight\""); |
| 31 | if (!p || !(p = strchr(p, ':'))) return -1; |
| 32 | return atoi(p + 1); |
| 33 | } |
| 34 | |
| 35 | static uint64_t g_rng; |
| 36 | static uint32_t rnd(void) { g_rng = g_rng * 6364136223846793005ull + 1442695040888963407ull; return (uint32_t)(g_rng >> 33); } |
| 37 | static uint64_t now_ns(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec; } |
| 38 | |
| 39 | static float bf2f(uint16_t h) { uint32_t u = (uint32_t)h << 16; float f; memcpy(&f, &u, 4); return f; } |
| 40 | static float e4m3f(uint8_t b) { |
| 41 | const int s = b >> 7, e = (b >> 3) & 15, m = b & 7; |
| 42 | const float v = e ? ldexpf(1.0f + (float)m / 8.0f, e - 7) : ldexpf((float)m / 8.0f, -6); |
| 43 | return s ? -v : v; |
| 44 | } |
| 45 | /* finite values: bf16 in [-2, 2) with 8 significant bits; e4m3 any code but the NaNs 0x7f / 0xff */ |
| 46 | static void fill(int fp8, void *p, uint64_t n, uint64_t seed) { |
| 47 | g_rng = seed * 2862933555777941757ull + 3037000493ull; |
| 48 | for (uint64_t i = 0; i < n; i++) { |
| 49 | if (fp8) { uint8_t b; do b = (uint8_t)rnd(); while ((b & 0x7fu) == 0x7fu); ((uint8_t *)p)[i] = b; } |
| 50 | else { float f = ldexpf((float)(int32_t)(rnd() % 512u) - 256.0f, -7); uint32_t u; memcpy(&u, &f, 4); ((uint16_t *)p)[i] = (uint16_t)(u >> 16); } |
| 51 | } |
| 52 | } |
| 53 | static float val(int fp8, const void *p, uint64_t i) { return fp8 ? e4m3f(((const uint8_t *)p)[i]) : bf2f(((const uint16_t *)p)[i]); } |
| 54 | |
| 55 | typedef struct { uint32_t M, N, K; const char *tag; } Shape; |
| 56 | /* serving shapes of the live 35B (hidden 2048), the same nine GemmTune measured (handoffs/GemmTune/AB-PLAN.md) */ |
| 57 | static const Shape SH[] = { |
| 58 | { 1, 8192, 2048, "q+gate decode" }, { 16, 12288, 2048, "gdn in_proj b16" }, { 512, 8192, 2048, "q+gate p512" }, |
| 59 | { 512, 2048, 4096, "o_proj p512" }, { 2048, 12288, 2048, "gdn in_proj p2048" }, { 2048, 2048, 4096, "out_proj p2048" }, |
| 60 | { 64, 1024, 2048, "expert gate+up" }, { 64, 2048, 512, "expert down" }, { 2048, 16384, 2048, "lm_head slice" }, |
| 61 | }; |
| 62 | #define NS (sizeof SH / sizeof SH[0]) |
| 63 | |
| 64 | static uint64_t ref_bad_of(int fp8, const Shape *s, const void *A, const void *B, const float *C, uint64_t seed) { |
| 65 | uint64_t bad = 0; |
| 66 | g_rng = seed; |
| 67 | for (int e = 0; e < 64; e++) { |
| 68 | const uint32_t i = e == 0 ? s->M - 1u : rnd() % s->M, j = e == 0 ? s->N - 1u : rnd() % s->N; |
| 69 | double d = 0.0, mag = 0.0; |
| 70 | for (uint32_t kk = 0; kk < s->K; kk++) { |
| 71 | const double p = (double)val(fp8, A, (uint64_t)i * s->K + kk) * (double)val(fp8, B, (uint64_t)j * s->K + kk); |
| 72 | d += p; mag += fabs(p); |
| 73 | } |
| 74 | const double got = (double)C[(uint64_t)i * s->N + j]; |
| 75 | if (!(fabs(got - d) <= (double)s->K * ldexp(1.0, -22) * mag + 1e-30)) bad++; |
| 76 | } |
| 77 | return bad; |
| 78 | } |
| 79 | |
| 80 | int main(int argc, char **argv) { |
| 81 | unsigned rounds = 0; uint64_t seed = 20261027; |
| 82 | for (int i = 1; i < argc; i++) { |
| 83 | if (!strcmp(argv[i], "--rounds") && i + 1 < argc) rounds = (unsigned)atoi(argv[++i]); |
| 84 | else if (!strcmp(argv[i], "--seed") && i + 1 < argc) seed = strtoull(argv[++i], NULL, 10); |
| 85 | else if (!strcmp(argv[i], "--spin-us") && i + 1 < argc) { |
| 86 | unsigned us = (unsigned)strtoul(argv[++i], NULL, 10); |
| 87 | if (omega_gpu_session_set_spin_us(us) != 0) { fprintf(stderr, "bad --spin-us %u\n", us); return 2; } |
| 88 | printf("# spin_us %u\n", us); |
| 89 | } else { fprintf(stderr, "usage: %s --rounds R [--seed S] [--spin-us US]\n", argv[0]); return 2; } |
| 90 | } |
| 91 | if (!rounds) { fprintf(stderr, "--rounds R (>= 1) required\n"); return 2; } |
| 92 | const size_t amax = (size_t)2048 * 4096 * 2, bmax = (size_t)16384 * 2048 * 2, cmax = (size_t)2048 * 16384; |
| 93 | void *A = malloc(amax), *B = malloc(bmax); float *CA = malloc(cmax * 4), *CB = malloc(cmax * 4); |
| 94 | if (!A || !B || !CA || !CB) { printf("NOT_RUN: host allocation\n"); return 2; } |
| 95 | memset(A, 0, amax); memset(B, 0, bmax); memset(CA, 0, cmax * 4); memset(CB, 0, cmax * 4); /* first touch outside timing */ |
| 96 | const size_t n = 2 * NS; |
| 97 | int bad_total = 0; |
| 98 | for (unsigned round = 0; round <= rounds; round++) { |
| 99 | size_t ord[2 * NS]; |
| 100 | for (size_t i = 0; i < n; i++) ord[i] = i; |
| 101 | uint64_t r = seed * 1000003u + round; |
| 102 | for (size_t i = n - 1; i > 0; i--) { r = r * 6364136223846793005ull + 1442695040888963407ull; size_t j = (size_t)(r >> 33) % (i + 1); size_t t = ord[i]; ord[i] = ord[j]; ord[j] = t; } |
| 103 | for (size_t q = 0; q < n; q++) { |
| 104 | const int fp8 = (int)(ord[q] / NS); |
| 105 | const Shape *s = &SH[ord[q] % NS]; |
| 106 | const OmegaGpuGemmType t = fp8 ? OMEGA_GPU_GEMM_E4M3 : OMEGA_GPU_GEMM_BF16; |
| 107 | fill(fp8, A, (uint64_t)s->M * s->K, (uint64_t)s->M * 3 + s->N * 5 + s->K * 7 + (uint64_t)fp8); |
| 108 | fill(fp8, B, (uint64_t)s->N * s->K, (uint64_t)s->M * 11 + s->N * 13 + s->K * 17 + (uint64_t)fp8); |
| 109 | const size_t cn = (size_t)s->M * s->N; |
| 110 | memset(CA, 0xff, cn * 4); memset(CB, 0xff, cn * 4); /* NaN poison: an unwritten element fails the gate */ |
| 111 | r = r * 6364136223846793005ull + 1442695040888963407ull; |
| 112 | const int a_first = (int)((r >> 40) & 1u); |
| 113 | OmegaGemmExInfo xi; memset(&xi, 0, sizeof xi); |
| 114 | OmegaGemmExArgs x; memset(&x, 0, sizeof x); |
| 115 | x.op_a = OMEGA_GEMM_OP_N; x.op_b = OMEGA_GEMM_OP_T; x.m = s->M; x.n = s->N; x.k = s->K; x.batch = 1; |
| 116 | x.a_type = x.b_type = fp8 ? OMEGA_GEMM_DT_E4M3 : OMEGA_GEMM_DT_BF16; x.c_type = OMEGA_GEMM_DT_F32; |
| 117 | x.a = A; x.lda = s->K; x.b = B; x.ldb = s->K; x.c = CB; x.ldc = s->N; x.alpha = 1.0f; x.beta = 0.0f; x.info = ξ |
| 118 | const int if0 = in_flight(); |
| 119 | int rca = -99, rcb = -99; uint64_t wa = 0, wb = 0; |
| 120 | for (int leg = 0; leg < 2; leg++) { |
| 121 | if ((leg == 0) == (a_first != 0)) { |
| 122 | const uint64_t t0 = now_ns(); |
| 123 | rca = omega_gpu_gemm_ld(t, OMEGA_GPU_GEMM_B_COL, s->M, s->N, s->K, A, s->K, B, s->K, CA, NULL); |
| 124 | wa = now_ns() - t0; |
| 125 | } else { |
| 126 | const uint64_t t0 = now_ns(); |
| 127 | rcb = omega_gpu_gemm_ex(&x); |
| 128 | wb = now_ns() - t0; |
| 129 | } |
| 130 | } |
| 131 | const int if1 = in_flight(); |
| 132 | /* correctness gate */ |
| 133 | const int same = rca == 0 && rcb == 0 && memcmp(CA, CB, cn * 4) == 0; |
| 134 | const uint64_t ref_bad = rca == 0 ? ref_bad_of(fp8, s, A, B, CA, seed ^ ((uint64_t)round << 32) ^ q) : 64u; |
| 135 | const int ok = same && ref_bad == 0 && xi.route == OMEGA_GEMM_ROUTE_NATIVE && xi.host_fixed == 0; |
| 136 | bad_total += !ok; |
| 137 | printf("pair round=%u warm=%s seq=%zu type=%s M=%u N=%u K=%u tag=\"%s\" first=%s rc_a=%s rc_b=%s wall_a_ns=%" PRIu64 |
| 138 | " wall_b_ns=%" PRIu64 " route_b=%u core_calls_b=%" PRIu64 " host_fixed_b=%" PRIu64 " bitwise=%d ref_bad=%" PRIu64 |
| 139 | " gate=%s if_before=%d if_after=%d\n", |
| 140 | round, round == 0 ? "True" : "False", q, fp8 ? "e4m3" : "bf16", s->M, s->N, s->K, s->tag, a_first ? "A" : "B", |
| 141 | omega_gpu_gemm_rc_name(rca), omega_gpu_gemm_rc_name(rcb), wa, wb, xi.route, xi.core_calls, xi.host_fixed, same, |
| 142 | ref_bad, ok ? "PASS" : "FAIL", if0, if1); |
| 143 | fflush(stdout); |
| 144 | } |
| 145 | } |
| 146 | { /* negative control: expert down, bf16, mutant 1 */ |
| 147 | const Shape *s = &SH[7]; |
| 148 | fill(0, A, (uint64_t)s->M * s->K, 1); fill(0, B, (uint64_t)s->N * s->K, 2); |
| 149 | const size_t cn = (size_t)s->M * s->N; |
| 150 | memset(CA, 0xff, cn * 4); memset(CB, 0xff, cn * 4); |
| 151 | const int rca = omega_gpu_gemm_ld(OMEGA_GPU_GEMM_BF16, OMEGA_GPU_GEMM_B_COL, s->M, s->N, s->K, A, s->K, B, s->K, CA, NULL); |
| 152 | OmegaGemmExInfo xi; memset(&xi, 0, sizeof xi); |
| 153 | OmegaGemmExArgs x; memset(&x, 0, sizeof x); |
| 154 | x.op_a = OMEGA_GEMM_OP_N; x.op_b = OMEGA_GEMM_OP_T; x.m = s->M; x.n = s->N; x.k = s->K; x.batch = 1; |
| 155 | x.a_type = x.b_type = OMEGA_GEMM_DT_BF16; x.c_type = OMEGA_GEMM_DT_F32; |
| 156 | x.a = A; x.lda = s->K; x.b = B; x.ldb = s->K; x.c = CB; x.ldc = s->N; x.alpha = 1.0f; x.beta = 0.0f; x.info = ξ |
| 157 | omega_gpu_gemm_ex_test_set_mutant(1); |
| 158 | const int rcb = omega_gpu_gemm_ex(&x); |
| 159 | omega_gpu_gemm_ex_test_set_mutant(0); |
| 160 | const int same = memcmp(CA, CB, cn * 4) == 0; |
| 161 | const uint64_t rb = rcb == 0 ? ref_bad_of(0, s, A, B, CB, 3) : 64u; |
| 162 | const uint64_t ra = rca == 0 ? ref_bad_of(0, s, A, B, CA, 3) : 64u; |
| 163 | const int red = rca == 0 && ra == 0 && rcb == 0 && !same && rb > 0; |
| 164 | printf("control mutant=1 cell=\"%s\" rc_a=%s rc_b=%s bitwise=%d ref_bad_a=%" PRIu64 " ref_bad_b=%" PRIu64 " verdict=%s\n", |
| 165 | s->tag, omega_gpu_gemm_rc_name(rca), omega_gpu_gemm_rc_name(rcb), same, ra, rb, red ? "RED (expected)" : "GREEN (control failed)"); |
| 166 | if (!red) bad_total++; |
| 167 | } |
| 168 | printf("run: %s (%d pairs failed the correctness gate of %zu)\n", bad_total ? "FAIL" : "PASS", bad_total, n * (rounds + 1u)); |
| 169 | free(A); free(B); free(CA); free(CB); |
| 170 | return bad_total ? 1 : 0; |
| 171 | } |