interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / tests / gpu_l2_reuse_test.c

122 lines · 9673 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* gpu_l2_reuse_test: does an FP8 GEMV that re-reads weights another launch just read run faster? (OWN-30B W-L2d, 2026-10-14)
3 *
4 * Question behind it: a decode token has ~120 us per layer where DRAM is idle (attention at pos 300 ~73 us latency-bound,
5 * router ~25 us, glue ~18 us; W-L2 timeline). If the GB10 L2 keeps a GEMV's weights between two launches, an independent
6 * prefetch launch overlapped with those latency-bound launches (no barrier between them: no data dependency) could pull the
7 * NEXT GEMV's weights into L2, and that GEMV would then run from L2. This test measures only the precondition: the same
8 * tensor read twice in one list, [GEMV(T) -> y][WAIT_FOR_IDLE][GEMV(T) -> y], GPU timestamps around each launch.
9 * One variable per pass: the weight memory policy (GPU-uncached = every session buffer today, vs GPU-cached,
10 * omega_gpu_fp8_set_weight_cache). Sizes n x 2048 for n = 1024 .. 16384 (2 .. 33.5 MB) bracket the unknown L2 size.
11 * Barrier WFI_ONLY (no shader-cache invalidate; the chip study r1 showed the form does not change the time).
12 * Every rep: y poisoned first, y bit-identical to the one-launch-at-a-time reference.
13 * Negative control: the uncached pass (no reuse is possible through an uncached mapping, nvos.h:1117) must show warm ~= cold.
14 * Chip only: caller holds the GB10 lock. Output "L2 cached=.. n=.. MB=.. cold_us=.. warm_us=.. ratio=..", --out receipt JSON. */
15#include "omega_gpu_fp8_api.h"
16#include "omega_gpu_cmdlist.h"
17#include "omega_gpu_session.h"
18#include <inttypes.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include "test_out_file.h"
23
24static int g_checks, g_failed;
25#define EV_N 32768u
26#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)
27static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; }
28static 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; }
29static uint64_t median(uint64_t *v, uint32_t n) { qsort(v, n, sizeof *v, cmp_u64); return v[n / 2]; }
30
31int main(int argc, char **argv) {
32 uint32_t reps = 10, k = 2048; const char *out_path = NULL;
33 for (int i = 1; i < argc; i++) {
34 if (strcmp(argv[i], "--reps") == 0 && i + 1 < argc) reps = (uint32_t)atoi(argv[++i]);
35 else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i];
36 else { printf("usage: %s [--reps R] [--out receipt.json]\n", argv[0]); return 2; }
37 }
38 if (reps == 0 || reps > 64) reps = 10;
39 static const uint32_t ns[] = { 1024, 2048, 4096, 8192, 12288, 16384 };
40 const uint32_t nmax = 16384;
41 omega_gpu_fp8_set_depth(1, 0);
42 (void)omega_gpu_session_set_spin_us(2000);
43 uint8_t *w = malloc((size_t)nmax * k); uint16_t *sc = malloc((size_t)(nmax / 128) * (k / 128) * 2); float *x = malloc((size_t)k * 4), *ref = malloc((size_t)nmax * 4);
44 uint32_t st = 2026;
45 for (size_t i = 0; i < (size_t)nmax * k; i++) { uint8_t b = (uint8_t)(lcg(&st) >> 24); w[i] = (b & 0x7F) == 0x7F ? 0 : b; }
46 for (size_t i = 0; i < (size_t)(nmax / 128) * (k / 128); i++) sc[i] = 0x3F80;
47 for (uint32_t i = 0; i < k; i++) x[i] = (float)(lcg(&st) >> 8) / 16777216.0f * 2.0f - 1.0f;
48
49 OmegaGpuCmdList *cl = NULL;
50 if (omega_gpu_cmdlist_create(8, &cl) != 0) { printf("cmdlist create: %s\n", omega_gpu_session_last_error()); return 1; }
51 OmegaGpuMem tab, xd, yd, ye;
52 omega_gpu_session_lock();
53 int arc = omega_gpu_session_alloc(32u, &tab) | omega_gpu_session_alloc((size_t)k * 4u, &xd) | omega_gpu_session_alloc((size_t)nmax * 4u, &yd) | omega_gpu_session_alloc((size_t)EV_N * 4u, &ye);
54 omega_gpu_session_unlock();
55 if (arc != 0) { printf("alloc: %s\n", omega_gpu_session_last_error()); return 1; }
56 omega_fp8_pack_x_f32(x, k, xd.cpu);
57 if (omega_gpu_session_sync_to_gpu(&xd) != 0) return 1;
58
59 FILE *out = out_path ? test_out_open(out_path) : NULL;
60 if (out) fprintf(out, "{\"test\":\"OMEGA_GPU_L2_REUSE\",\"k\":%u,\"reps\":%u,\"points\":[", k, reps);
61 int first_pt = 1; double ratio_unc_max = 0, ratio_cached_min = 1e9;
62 /* evictor: a GPU-cached EV_N x k tensor (67 MB) read first in every list, so "cold" is cold even if L2 keeps lines across submits */
63 OmegaGpuFp8Tensor *ev = NULL;
64 { uint8_t *we = malloc((size_t)EV_N * k); uint16_t *se = malloc((size_t)(EV_N / 128) * (k / 128) * 2);
65 for (size_t i = 0; i < (size_t)EV_N * k; i++) { uint8_t b = (uint8_t)(lcg(&st) >> 24); we[i] = (b & 0x7F) == 0x7F ? 0 : b; }
66 for (size_t i = 0; i < (size_t)(EV_N / 128) * (k / 128); i++) se[i] = 0x3F80;
67 omega_gpu_fp8_set_weight_cache(1);
68 int erc = omega_gpu_fp8_tensor_upload(1, EV_N, k, we, se, &ev);
69 free(we); free(se);
70 float *yev = malloc((size_t)EV_N * 4); if (erc == OMEGA_GPU_FP8_OK) erc = omega_gpu_fp8_gemv_f32(ev, 0, x, yev, NULL); free(yev); /* kernel built + warm */
71 uint64_t *tw = tab.cpu;
72 if (erc != OMEGA_GPU_FP8_OK || omega_gpu_fp8_tensor_entry(ev, 0, &tw[2], &tw[3]) != 0) { printf("evictor upload: %s\n", omega_gpu_fp8_last_error()); return 1; } }
73 for (int pass = 0; pass < 3; pass++) { const int cached = pass > 0, evict = pass != 1; /* (uncached, evict) (cached, no evict) (cached, evict) */
74 omega_gpu_fp8_set_weight_cache(cached);
75 for (size_t ni = 0; ni < sizeof ns / sizeof ns[0]; ni++) {
76 const uint32_t n = ns[ni]; const uint64_t bytes = (uint64_t)n * k + (uint64_t)(n / 128) * (k / 128) * 4u;
77 OmegaGpuFp8Tensor *t = NULL;
78 int rc = omega_gpu_fp8_tensor_upload(1, n, k, w, sc, &t);
79 if (rc == OMEGA_GPU_FP8_OK) rc = omega_gpu_fp8_gemv_f32(t, 0, x, ref, NULL);
80 if (rc != OMEGA_GPU_FP8_OK) { printf("upload/reference n=%u cached=%d: %s (%s)\n", n, cached, omega_gpu_fp8_rc_name(rc), omega_gpu_fp8_last_error()); g_failed++; omega_gpu_fp8_tensor_free(t); break; }
81 uint64_t *tabw = tab.cpu;
82 if (omega_gpu_fp8_tensor_entry(t, 0, &tabw[0], &tabw[1]) != 0 || omega_gpu_session_sync_to_gpu(&tab) != 0) { g_failed++; omega_gpu_fp8_tensor_free(t); break; }
83 uint64_t colds[64], warms[64]; int identical = 1, ok = 1;
84 for (uint32_t r = 0; r < reps && ok; r++) {
85 float *y = yd.cpu; for (uint32_t i = 0; i < n; i++) y[i] = -999.0f;
86 if (omega_gpu_session_sync_to_gpu(&yd) != 0) { ok = 0; break; }
87 omega_gpu_session_lock();
88 omega_gpu_cmdlist_reset(cl);
89 if (evict && omega_gpu_fp8_gemv_grouped_record(cl, ev, 1, tab.va + 16u, xd.va, 1, ye.va) != 0) { printf("record evictor: %s\n", omega_gpu_fp8_last_error()); ok = 0; }
90 for (int j = 0; j < 2 && ok; j++)
91 if (omega_gpu_fp8_gemv_grouped_record(cl, t, 1, tab.va, xd.va, 1, yd.va) != 0) { printf("record: %s\n", omega_gpu_fp8_last_error()); ok = 0; }
92 OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); o.mode = OMEGA_GPU_CMDLIST_PCAS; o.barrier = OMEGA_GPU_CMDLIST_BARRIER_WFI_ONLY; o.stamps = 1;
93 OmegaGpuCmdListStats s; memset(&s, 0, sizeof s);
94 uint64_t sts[4]; int cnt = -1; const int want = evict ? 4 : 3;
95 if (ok && omega_gpu_cmdlist_submit(cl, &o, &s) != 0) { printf("submit: %s\n", omega_gpu_session_last_error()); ok = 0; }
96 if (ok) cnt = omega_gpu_cmdlist_stamps(cl, sts, 4);
97 omega_gpu_session_unlock();
98 if (!ok || cnt != want) { ok = 0; break; }
99 colds[r] = sts[want - 2] - sts[want - 3]; warms[r] = sts[want - 1] - sts[want - 2];
100 if (omega_gpu_session_sync_to_cpu(&yd) != 0) { ok = 0; break; }
101 if (memcmp(y, ref, (size_t)n * 4) != 0) identical = 0;
102 }
103 CHECK(ok && identical, "cached=%d evict=%d n=%u: %u reps ran, y bit-identical to the reference on every rep", cached, evict, n, reps);
104 if (ok) {
105 const double c = median(colds, reps) / 1e3, wm = median(warms, reps) / 1e3, ratio = wm / c;
106 printf("L2 cached=%d evict=%d n=%-5u MB=%.1f cold_us=%.1f warm_us=%.1f ratio=%.3f cold_GB/s=%.1f warm_GB/s=%.1f\n", cached, evict, n, bytes / 1e6, c, wm, ratio, bytes / (c * 1e3), bytes / (wm * 1e3));
107 if (cached && evict) { if (ratio < ratio_cached_min) ratio_cached_min = ratio; } else if (!cached && ratio > ratio_unc_max) ratio_unc_max = ratio;
108 if (out) { fprintf(out, "%s{\"cached\":%d,\"evict\":%d,\"n\":%u,\"bytes\":%" PRIu64 ",\"cold_ns\":%" PRIu64 ",\"warm_ns\":%" PRIu64 ",\"identical\":%d}", first_pt ? "" : ",", cached, evict, n, bytes, median(colds, reps), median(warms, reps), identical); first_pt = 0; }
109 }
110 omega_gpu_fp8_tensor_free(t);
111 if (omega_gpu_session_is_blocked()) { printf("session latched: stopping\n"); g_failed++; break; }
112 }
113 }
114 omega_gpu_fp8_set_weight_cache(0);
115 printf("SUMMARY uncached max warm/cold %.3f, cached min warm/cold %.3f\n", ratio_unc_max, ratio_cached_min);
116 if (out) { fprintf(out, "],\"uncached_ratio_max\":%.4f,\"cached_ratio_min\":%.4f,\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", ratio_unc_max, ratio_cached_min, g_checks, g_failed, g_failed ? "FAIL" : "PASS"); fclose(out); }
117 printf("gpu_l2_reuse_test: %d checks, %d failed: %s\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS");
118 omega_gpu_fp8_tensor_free(ev);
119 omega_gpu_cmdlist_free(cl);
120 free(w); free(sc); free(x); free(ref);
121 return g_failed ? 1 : 0;
122}