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-TUNE-20261027 / gemm_ab.c
| 1 | /* gemm_ab.c -- GemmTune A/B timing driver for omega_gpu_gemm (CUDApp C3 general GEMM). |
| 2 | * Measurement harness only, not product code: it is compiled against each arm's own tests/gpu_gemm_test.c (fill, compare, |
| 3 | * the host model) and build/libomega_gpu.a, so arm A and arm B run the same inputs through the same checker. |
| 4 | * One process = one pass of one arm over the shape set, in a seeded order. Every timed call is compared against the host |
| 5 | * model (compare(): full for shapes up to 2^27 multiply-adds, else 4096 sampled elements plus the last row and column) and |
| 6 | * records the live server's in_flight from 127.0.0.1:18091/health before and after. |
| 7 | * gemm_ab --arm A|B --round R --seed S [--set ab|explore] [--cfg bf16|e4m3 rt,ct,wm,wn,stall[,band]]... |
| 8 | * (",band" only when built with -DGEMM_AB_BAND against a tree whose OmegaGemmCfg has the band field) |
| 9 | * Why C: it must link the C library under test and reuse the C checker verbatim; no .om binding of omega_gpu_gemm exists. |
| 10 | * It lives in the evidence folder as part of the receipt. */ |
| 11 | #define main gemm_test_main |
| 12 | #include "gpu_gemm_test.c" |
| 13 | #undef main |
| 14 | |
| 15 | static int in_flight(void) { |
| 16 | FILE *f = popen("curl -s -m 2 127.0.0.1:18091/health", "r"); |
| 17 | if (!f) return -1; |
| 18 | char buf[1024]; size_t n = fread(buf, 1, sizeof buf - 1, f); buf[n] = 0; |
| 19 | pclose(f); |
| 20 | const char *p = strstr(buf, "\"in_flight\""); |
| 21 | if (!p || !(p = strchr(p, ':'))) return -1; |
| 22 | return atoi(p + 1); |
| 23 | } |
| 24 | |
| 25 | typedef struct { uint32_t M, N, K; unsigned lay; const char *tag; } Shape; |
| 26 | static const Shape AB[] = { |
| 27 | /* serving shapes of the 35B (hidden 2048), A = activations row-major, B = weights [out][in] = column-major */ |
| 28 | { 1, 8192, 2048, 2, "q+gate decode" }, { 16, 12288, 2048, 2, "gdn in_proj b16" }, { 512, 8192, 2048, 2, "q+gate p512" }, |
| 29 | { 512, 2048, 4096, 2, "o_proj p512" }, { 2048, 12288, 2048, 2, "gdn in_proj p2048" }, { 2048, 2048, 4096, 2, "out_proj p2048" }, |
| 30 | { 64, 1024, 2048, 2, "expert gate+up" }, { 64, 2048, 512, 2, "expert down" }, { 2048, 16384, 2048, 2, "lm_head slice" }, |
| 31 | /* the #112 ladder, A row B row as in evidence/C3-GEMM */ |
| 32 | { 1024, 1024, 1024, 0, "ladder" }, { 2048, 2048, 2048, 0, "ladder" }, { 4096, 4096, 4096, 0, "ladder" }, { 8192, 8192, 8192, 0, "ladder" }, |
| 33 | { 3000, 5000, 700, 0, "ladder" }, { 64, 8192, 8192, 0, "ladder" }, { 8192, 64, 8192, 0, "ladder" }, { 1000, 999, 1001, 0, "ladder" }, |
| 34 | }; |
| 35 | static const Shape EXPLORE[] = { |
| 36 | { 512, 8192, 2048, 2, "q+gate p512" }, { 2048, 12288, 2048, 2, "gdn in_proj p2048" }, { 2048, 2048, 2048, 0, "ladder" }, |
| 37 | { 4096, 4096, 4096, 0, "ladder" }, { 64, 8192, 8192, 0, "ladder" }, |
| 38 | }; |
| 39 | |
| 40 | int main(int argc, char **argv) { |
| 41 | const char *arm = "?"; unsigned round = 0; uint64_t seed = 20261027; int explore = 0; |
| 42 | for (int i = 1; i < argc; i++) { |
| 43 | if (!strcmp(argv[i], "--arm") && i + 1 < argc) arm = argv[++i]; |
| 44 | else if (!strcmp(argv[i], "--round") && i + 1 < argc) round = (unsigned)atoi(argv[++i]); |
| 45 | else if (!strcmp(argv[i], "--seed") && i + 1 < argc) seed = strtoull(argv[++i], NULL, 10); |
| 46 | else if (!strcmp(argv[i], "--set") && i + 1 < argc) { i++; explore = !strcmp(argv[i], "explore"); } |
| 47 | else if (!strcmp(argv[i], "--spin-us") && i + 1 < argc) { |
| 48 | /* AB-PLAN amendment 3: spin on the marker word before the 50 us sleep-poll, so sub-ms kernels are not |
| 49 | * timed by a delayed wake-up. 0 (the default) keeps the library default. */ |
| 50 | unsigned us = (unsigned)strtoul(argv[++i], NULL, 10); |
| 51 | if (omega_gpu_session_set_spin_us(us) != 0) { fprintf(stderr, "bad --spin-us %u\n", us); return 2; } |
| 52 | printf("# spin_us %u\n", us); |
| 53 | } |
| 54 | else if (!strcmp(argv[i], "--cfg") && i + 2 < argc) { |
| 55 | OmegaGemmCfg c; memset(&c, 0, sizeof c); |
| 56 | c.type = !strcmp(argv[i + 1], "bf16") ? OMEGA_GPU_GEMM_BF16 : OMEGA_GPU_GEMM_E4M3; |
| 57 | unsigned band = 0; |
| 58 | int nf = sscanf(argv[i + 2], "%u,%u,%u,%u,%u,%u", &c.rt, &c.ct, &c.wm, &c.wn, &c.stall, &band); |
| 59 | #ifdef GEMM_AB_BAND |
| 60 | c.band = band; |
| 61 | #else |
| 62 | if (nf == 6) nf = -1; |
| 63 | #endif |
| 64 | if ((nf != 5 && nf != 6) || omega_gpu_gemm_test_set_cfg(c.type, &c) != 0) { |
| 65 | fprintf(stderr, "bad --cfg %s %s\n", argv[i + 1], argv[i + 2]); return 2; |
| 66 | } |
| 67 | i += 2; |
| 68 | } else { fprintf(stderr, "usage: %s --arm A|B --round R [--seed S] [--set ab|explore] [--spin-us US] [--cfg bf16|e4m3 rt,ct,wm,wn,stall]\n", argv[0]); return 2; } |
| 69 | } |
| 70 | const Shape *sh = explore ? EXPLORE : AB; |
| 71 | const size_t ns = explore ? sizeof EXPLORE / sizeof EXPLORE[0] : sizeof AB / sizeof AB[0]; |
| 72 | const size_t big = (size_t)8192 * 8192 * 2; |
| 73 | void *A = malloc(big), *B = malloc(big); float *C = malloc((size_t)8192 * 16384 * 4); |
| 74 | if (!A || !B || !C) { printf("NOT_RUN: host allocation\n"); return 2; } |
| 75 | /* order: every (type, shape) once, Fisher-Yates with a generator seeded by (seed, round, arm) -- same order for A and B in a round */ |
| 76 | size_t n = 2 * ns, ord[64]; |
| 77 | for (size_t i = 0; i < n; i++) ord[i] = i; |
| 78 | uint64_t r = seed * 1000003u + round; |
| 79 | 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; } |
| 80 | /* warm-up per type (kernel build + first launch), reported and excluded */ |
| 81 | for (int t = 0; t < 2; t++) { |
| 82 | fill((OmegaGpuGemmType)t, A, 65 * 100, 77); fill((OmegaGpuGemmType)t, B, 100 * 63, 78); |
| 83 | OmegaGpuGemmInfo info; int rc = omega_gpu_gemm((OmegaGpuGemmType)t, 0, 65, 63, 100, A, B, C, &info); |
| 84 | printf("warm arm=%s type=%s rc=%s gpr_main=%u\n", arm, tname((OmegaGpuGemmType)t), omega_gpu_gemm_rc_name(rc), info.gpr[OMEGA_GEMM_K_MAIN]); |
| 85 | } |
| 86 | int bad_total = 0; |
| 87 | for (size_t q = 0; q < n; q++) { |
| 88 | const OmegaGpuGemmType t = (OmegaGpuGemmType)(ord[q] / ns); |
| 89 | const Shape *s = &sh[ord[q] % ns]; |
| 90 | fill(t, A, (uint64_t)s->M * s->K, (uint64_t)s->M * 3 + s->N * 5 + s->K * 7 + s->lay); |
| 91 | fill(t, B, (uint64_t)s->K * s->N, (uint64_t)s->M * 11 + s->N * 13 + s->K * 17 + s->lay); |
| 92 | const int if0 = in_flight(); |
| 93 | OmegaGpuGemmInfo info; memset(&info, 0, sizeof info); |
| 94 | int rc = omega_gpu_gemm(t, s->lay, s->M, s->N, s->K, A, B, C, &info); |
| 95 | const int if1 = in_flight(); |
| 96 | char first[200] = ""; uint64_t checked = 0, bad = rc == 0 ? compare(t, s->lay, s->M, s->N, s->K, A, B, C, &checked, first, sizeof first) : 1; |
| 97 | bad_total += rc != 0 || bad != 0; |
| 98 | printf("sample arm=%s round=%u seq=%zu type=%s lay=%u M=%u N=%u K=%u tag=\"%s\" rc=%s checked=%" PRIu64 " wrong=%" PRIu64 " main_ns=%" PRIu64 |
| 99 | " pack_ns=%" PRIu64 " call_ns=%" PRIu64 " gpr_main=%u if_before=%d if_after=%d%s%s\n", |
| 100 | arm, round, q, tname(t), s->lay, s->M, s->N, s->K, s->tag, omega_gpu_gemm_rc_name(rc), checked, bad, info.main_ns, info.pack_ns, |
| 101 | info.call_ns, info.gpr[OMEGA_GEMM_K_MAIN], if0, if1, bad ? " first=" : "", bad ? first : ""); |
| 102 | fflush(stdout); |
| 103 | } |
| 104 | printf("pass arm=%s round=%u: %s (%d wrong or failed of %zu)\n", arm, round, bad_total ? "FAIL" : "PASS", bad_total, n); |
| 105 | free(A); free(B); free(C); |
| 106 | return bad_total ? 1 : 0; |
| 107 | } |