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_launch_cost_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_launch_cost_test.c` 170 lines · 12920 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* gpu_launch_cost_test: what does one FP8 GEMV launch cost inside a command list beyond its bytes? (OWN-30B W-L2c, 2026-10-14) 3 * 4 * The same weight bytes (n x k e4m3, default 16384 x 2048 = 33.5 MB) are split into m = 1, 2, 4, 8, 16 launches (m tensors of 5 * n/m rows, independent: distinct rows, distinct y), recorded into ONE command list and timed with the GPU timestamps 6 * (omega_gpu_cmdlist stamps). T(m) - T(1) over (m - 1) is the per-launch fixed cost with the WAIT_FOR_IDLE barrier between 7 * launches. Variant (one variable): the same lists with OMEGA_GPU_CMDLIST_BARRIER_NONE (legal here: the launches share no 8 * data), which tells whether the barrier or the kernel's own ramp/tail is the cost. Every rep checks y bit-identical to the 9 * session's one-launch-at-a-time result for every tensor (poisoned first). Negative control: a dropped launch (skip_launch) 10 * must be named by submit. 11 * Chip only (no host mode): caller holds the GB10 lock. Output: lines "COST m=.. barrier=.. span_us=.. per_launch_us=.. GB/s=.." 12 * and the fixed-cost lines; --out writes the receipt JSON. */ 13 #include "omega_gpu_fp8_api.h" 14 #include "omega_gpu_cmdlist.h" 15 #include "omega_gpu_session.h" 16 #include 17 #include 18 #include 19 #include 20 #include "test_out_file.h" 21 22 static int g_checks, g_failed, g_program; 23 #define CHECK(c, ...) do { g_checks++; if (!(c)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } else { printf("ok: "); printf(__VA_ARGS__); printf("\n"); } } while (0) 24 static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } 25 static int cmp_u64(const void *a, const void *b) { uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; return x < y ? -1 : x > y; } 26 static uint64_t median(uint64_t *v, uint32_t n) { qsort(v, n, sizeof *v, cmp_u64); return v[n / 2]; } 27 28 #define MAX_M 16 29 typedef struct { OmegaGpuFp8Tensor *t[MAX_M]; float *ref; uint32_t m, rows, k; } Split; 30 31 static int make_split(Split *s, uint32_t m, uint32_t n, uint32_t k, const uint8_t *w, const uint16_t *sc, const float *x) { 32 memset(s, 0, sizeof *s); 33 s->m = m; s->rows = n / m; s->k = k; s->ref = malloc((size_t)n * 4); 34 for (uint32_t i = 0; i < m; i++) { 35 int rc = omega_gpu_fp8_tensor_upload(1, s->rows, k, w + (size_t)i * s->rows * k, sc + (size_t)i * (s->rows / 128) * (k / 128), &s->t[i]); 36 if (rc != OMEGA_GPU_FP8_OK) { printf("upload m=%u i=%u: %s (%s)\n", m, i, omega_gpu_fp8_rc_name(rc), omega_gpu_fp8_last_error()); return -1; } 37 rc = omega_gpu_fp8_gemv_f32(s->t[i], 0, x, s->ref + (size_t)i * s->rows, NULL); /* warm-up (kernel cached) + the reference */ 38 if (rc != OMEGA_GPU_FP8_OK) { printf("reference m=%u i=%u: %s (%s)\n", m, i, omega_gpu_fp8_rc_name(rc), omega_gpu_fp8_last_error()); return -1; } 39 } 40 return 0; 41 } 42 static void free_split(Split *s) { for (uint32_t i = 0; i < s->m; i++) omega_gpu_fp8_tensor_free(s->t[i]); free(s->ref); } 43 44 /* one list of m launches, submitted reps times; medians of the stamp span and of the host-seen elapsed; y checked every rep */ 45 static int time_split(Split *s, OmegaGpuCmdList *cl, OmegaGpuMem *tab, OmegaGpuMem *xd, OmegaGpuMem *yd, OmegaGpuCmdListBarrier bar, 46 uint32_t reps, uint64_t *span_med, uint64_t *elapsed_med, uint64_t *first_med, int *identical) { 47 const uint32_t m = s->m, n = s->rows * m; 48 uint64_t *spans = malloc(reps * 8), *els = malloc(reps * 8), *firsts = malloc(reps * 8); 49 uint64_t *tabw = tab->cpu; 50 for (uint32_t i = 0; i < m; i++) if (omega_gpu_fp8_tensor_entry(s->t[i], 0, &tabw[2 * i], &tabw[2 * i + 1]) != 0) return -1; 51 if (omega_gpu_session_sync_to_gpu(tab) != 0) return -1; 52 *identical = 1; 53 int rc = 0; 54 for (uint32_t r = 0; r < reps && rc == 0; r++) { 55 float *y = yd->cpu; for (uint32_t i = 0; i < n; i++) y[i] = -999.0f; 56 if (omega_gpu_session_sync_to_gpu(yd) != 0) { rc = -1; break; } 57 omega_gpu_session_lock(); 58 omega_gpu_cmdlist_reset(cl); 59 for (uint32_t i = 0; i < m && rc == 0; i++) { 60 const uint32_t begin = omega_gpu_cmdlist_count(cl); 61 if (omega_gpu_fp8_gemv_grouped_record(cl, s->t[i], 1, tab->va + 16u * i, xd->va, 1, yd->va + (uint64_t)i * s->rows * 4u) != 0) { printf("record m=%u i=%u: %s\n", m, i, omega_gpu_fp8_last_error()); rc = -1; } 62 if (g_program && rc == 0) { 63 OmegaGpuCmdListAccess effects[] = { 64 {tab->va + 16u*i, 16, OMEGA_GPU_CMDLIST_ACCESS_READ}, 65 {xd->va, (uint64_t)s->k*4, OMEGA_GPU_CMDLIST_ACCESS_READ}, 66 {tabw[2*i], (uint64_t)s->rows*s->k, OMEGA_GPU_CMDLIST_ACCESS_READ}, 67 {tabw[2*i+1], (uint64_t)(s->rows/128)*(s->k/128)*4, OMEGA_GPU_CMDLIST_ACCESS_READ}, 68 {yd->va + (uint64_t)i*s->rows*4, (uint64_t)s->rows*4, OMEGA_GPU_CMDLIST_ACCESS_WRITE} 69 }; 70 for (uint32_t j=begin; jref, (size_t)n * 4) != 0) *identical = 0; 85 } 86 if (rc == 0) { *span_med = median(spans, reps); *elapsed_med = median(els, reps); *first_med = median(firsts, reps); } 87 free(spans); free(els); free(firsts); 88 return rc; 89 } 90 91 int main(int argc, char **argv) { 92 uint32_t reps = 20, n = 16384, k = 2048; const char *out_path = NULL; 93 for (int i = 1; i < argc; i++) { 94 if (strcmp(argv[i], "--reps") == 0 && i + 1 < argc) reps = (uint32_t)atoi(argv[++i]); 95 else if (strcmp(argv[i], "--n") == 0 && i + 1 < argc) n = (uint32_t)atoi(argv[++i]); 96 else if (strcmp(argv[i], "--k") == 0 && i + 1 < argc) k = (uint32_t)atoi(argv[++i]); 97 else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i]; 98 else if (strcmp(argv[i], "--program") == 0) g_program=1; 99 else { printf("usage: %s [--reps N] [--n ROWS] [--k K] [--out receipt.json] [--program]\n", argv[0]); return 2; } 100 } 101 if (n % (128u * MAX_M) || k % 512u) { printf("n must be a multiple of %u and k of 512\n", 128u * MAX_M); return 2; } 102 omega_gpu_fp8_set_depth(1, 0); /* one kernel (the step-by-step production kernel) for every split: the row count must not change the kernel */ 103 (void)omega_gpu_session_set_spin_us(2000); 104 /* data: random e4m3 bytes without the NaN codes, scales 1.0, x in [-1, 1) */ 105 uint8_t *w = malloc((size_t)n * k); uint16_t *sc = malloc((size_t)(n / 128) * (k / 128) * 2); float *x = malloc((size_t)k * 4); 106 uint32_t st = 2026; 107 for (size_t i = 0; i < (size_t)n * k; i++) { uint8_t b = (uint8_t)(lcg(&st) >> 24); w[i] = (b & 0x7F) == 0x7F ? 0 : b; } 108 for (size_t i = 0; i < (size_t)(n / 128) * (k / 128); i++) sc[i] = 0x3F80; /* bf16 1.0 */ 109 for (uint32_t i = 0; i < k; i++) x[i] = (float)(lcg(&st) >> 8) / `16777216`.0f * 2.0f - 1.0f; 110 const uint64_t bytes = (uint64_t)n * k + (uint64_t)(n / 128) * (k / 128) * 4u; 111 112 OmegaGpuCmdList *cl = NULL; 113 if (omega_gpu_cmdlist_create(64, &cl) != 0) { printf("cmdlist create: %s\n", omega_gpu_session_last_error()); return 1; } 114 OmegaGpuMem tab, xd, yd; 115 omega_gpu_session_lock(); 116 int arc = omega_gpu_session_alloc(16u * MAX_M, &tab) | omega_gpu_session_alloc((size_t)k * 4u, &xd) | omega_gpu_session_alloc((size_t)n * 4u, &yd); 117 omega_gpu_session_unlock(); 118 if (arc != 0) { printf("alloc: %s\n", omega_gpu_session_last_error()); return 1; } 119 omega_fp8_pack_x_f32(x, k, xd.cpu); 120 if (omega_gpu_session_sync_to_gpu(&xd) != 0) return 1; 121 122 FILE *out = out_path ? test_out_open(out_path) : NULL; 123 if (out) fprintf(out, "{\"test\":\"OMEGA_GPU_LAUNCH_COST\",\"planner\":\"%s\",\"intermediate_stamps\":\"%s\",\"n\":%u,\"k\":%u,\"bytes\":%" PRIu64 ",\"reps\":%u,\"points\":[", g_program ? "whole-program" : "legacy", g_program ? "front-end-or-drain" : "drain", n, k, bytes, reps); 124 static const uint32_t ms[] = { 1, 2, 4, 8, 16 }; 125 double t1[8] = { 0 }; int first_pt = 1; 126 static const OmegaGpuCmdListBarrier bars[] = { OMEGA_GPU_CMDLIST_BARRIER_WFI, OMEGA_GPU_CMDLIST_BARRIER_WFI_ONLY, OMEGA_GPU_CMDLIST_BARRIER_WFI_DATA, OMEGA_GPU_CMDLIST_BARRIER_FLUSH_DATA, OMEGA_GPU_CMDLIST_BARRIER_NONE, OMEGA_GPU_CMDLIST_BARRIER_WFI }; 127 for (unsigned bi = 0; bi < (g_program ? 1u : sizeof bars / sizeof bars[0]); bi++) { const OmegaGpuCmdListBarrier bar = bars[bi]; const char *bn = g_program ? "program" : omega_gpu_cmdlist_barrier_name(bar); /* WFI first and last: drift control */ 128 for (size_t mi = 0; mi < sizeof ms / sizeof ms[0]; mi++) { 129 Split s; 130 if (make_split(&s, ms[mi], n, k, w, sc, x) != 0) { printf("split m=%u failed\n", ms[mi]); g_failed++; free_split(&s); break; } 131 uint64_t span = 0, el = 0, first = 0; int identical = 0; 132 int rc = time_split(&s, cl, &tab, &xd, &yd, bar, reps, &span, &el, &first, &identical); 133 CHECK(rc == 0 && identical, "m=%u barrier=%s: %u reps ran, y bit-identical to the one-launch-at-a-time reference on every rep", ms[mi], bn, reps); 134 if (rc == 0) { 135 double span_us = span / 1e3, el_us = el / 1e3, per = el_us / ms[mi], gbs = (double)bytes / (double)el; /* elapsed-based: comparable across barrier modes */ 136 if (ms[mi] == 1) t1[bi] = el_us; 137 double fixed = ms[mi] > 1 ? (el_us - t1[bi]) / (ms[mi] - 1) : 0.0; 138 printf("COST m=%-2u barrier=%-14s span_us=%.1f per_launch_us=%.1f first_launch_us=%.1f elapsed_us=%.1f GB/s=%.1f fixed_per_extra_launch_us=%.2f\n", 139 ms[mi], bn, span_us, per, first / 1e3, el / 1e3, gbs, fixed); 140 if (out) { fprintf(out, "%s{\"m\":%u,\"barrier\":\"%s\",\"span_ns\":%" PRIu64 ",\"first_launch_ns\":%" PRIu64 ",\"elapsed_ns\":%" PRIu64 ",\"gbps\":%.2f,\"fixed_per_extra_launch_us\":%.2f,\"identical\":%d}", 141 first_pt ? "" : ",", ms[mi], bn, span, first, el, gbs, fixed, identical); first_pt = 0; } 142 } 143 free_split(&s); 144 if (omega_gpu_session_is_blocked()) { printf("session latched: stopping\n"); break; } 145 } 146 } 147 /* negative control: a dropped launch is named */ 148 { 149 Split s; int rc = make_split(&s, 2, n, k, w, sc, x); 150 if (rc == 0) { 151 uint64_t *tabw = tab.cpu; for (uint32_t i = 0; i < 2; i++) omega_gpu_fp8_tensor_entry(s.t[i], 0, &tabw[2 * i], &tabw[2 * i + 1]); 152 omega_gpu_session_sync_to_gpu(&tab); 153 omega_gpu_session_lock(); 154 omega_gpu_cmdlist_reset(cl); 155 for (uint32_t i = 0; i < 2; i++) omega_gpu_fp8_gemv_grouped_record(cl, s.t[i], 1, tab.va + 16u * i, xd.va, 1, yd.va + (uint64_t)i * s.rows * 4u); 156 OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); o.mode = OMEGA_GPU_CMDLIST_PCAS; o.barrier = OMEGA_GPU_CMDLIST_BARRIER_WFI; o.skip_launch = 2; 157 OmegaGpuCmdListStats st2; memset(&st2, 0, sizeof st2); 158 int src = omega_gpu_cmdlist_submit(cl, &o, &st2); 159 omega_gpu_session_unlock(); 160 CHECK(src != 0 && st2.first_missing == 1, "negative control: dropped launch 1 named by submit (rc %d, first_missing %d)", src, st2.first_missing); 161 } else g_failed++; 162 free_split(&s); 163 } 164 if (out) { fprintf(out, "],\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS"); fclose(out); } 165 omega_gpu_session_lock(); omega_gpu_session_free(&tab); omega_gpu_session_free(&xd); omega_gpu_session_free(&yd); omega_gpu_session_unlock(); 166 omega_gpu_cmdlist_free(cl); 167 omega_gpu_session_close(); 168 printf("gpu_launch_cost_test: %d checks, %d failed: %s\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS"); 169 return g_failed ? 1 : 0; 170 }