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/test_omega_gpu_decode_graph_timeline.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 / test_omega_gpu_decode_graph_timeline.c` 303 lines · 15523 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* test_omega_gpu_decode_graph_timeline: the decode graph's per-token timeline never writes past stamp[] (omega #456). 3 * Host program on a FAKE OmegaGpuPlatform that completes every submission (markers, launch semaphores, stamp reports); 4 * links libomega_gpu.a with the fake installed first; opens no device, touches no GPU. 5 * 6 * The timeline holds OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1 stamps. Batched, a token's list never holds more launches than 7 * that. With flush_each (one submission per launch) the list is emptied after every launch, so nothing in the command 8 * list bounds the number of launches a token records: before #456 the 4097th stamped launch wrote stamp[4097], past the 9 * end of the graph. Contract under test: 4096 launches fill the timeline exactly (stamps() returns 4096); one more fails 10 * the token (CHIP_FAIL, timeline refused, lock released) and the next token runs normally. 11 * 12 * Each case runs in its own child process. 13 */ 14 #include "omega_gpu_decode_graph.h" 15 #include "omega_gpu_session.h" 16 #include "omega_gpu_platform.h" 17 #include "omega_gpu_cmdlist.h" 18 #include "omega_blackwell_submit.h" 19 20 #include 21 #include 22 #include 23 #include 24 #include 25 26 static int case_ok; 27 #define EXPECT(c) do { if (!(c)) { case_ok = 0; printf(" expect failed at line %d: %s\n", __LINE__, #c); } } while (0) 28 29 /* ---- the fake platform: every submitted word stream "runs" ---- */ 30 #define NB OMEGA_GPU_HANDLE_SLOTS 31 #define VA_BASE 0x100000000ull 32 #define VA_STRIDE 0x1000000ull 33 static struct { 34 OmegaGpuHandleTable t; 35 void *mem[NB]; 36 OmegaGpuBufferDesc desc[NB]; 37 OmegaGpuDeviceHandle dev; 38 OmegaGpuQueueHandle q; 39 OmegaGpuBufferHandle ring; 40 uint64_t ns; 41 unsigned locks_held, double_locks, bad_unlocks, submits; 42 uint64_t dev_token_va; /* nonzero: the device wrote dev_token there (seen at the gpu->cpu sync) */ 43 uint32_t dev_token; 44 } fk; 45 46 static uint64_t f_now_ns(void *c) { (void)c; fk.ns += 1000u; return fk.ns; } 47 static uint64_t f_now_ms(void *c) { return f_now_ns(c) / 1000000u; } 48 static int f_wait(void *c, uint64_t ms) { (void)c; fk.ns += ms * 1000000u; return 0; } 49 static int f_lock(void *c, uint32_t id) { 50 (void)c; 51 if (id != OMEGA_GPU_SESSION_LOCK_ID) return OMEGA_GPU_PLAT_REFUSED_ARGS; 52 if (fk.locks_held) { fk.double_locks++; return OMEGA_GPU_PLAT_REFUSED_BUSY; } /* a real lock would block forever */ 53 fk.locks_held = 1; 54 return 0; 55 } 56 static int f_unlock(void *c, uint32_t id) { 57 (void)c; 58 if (id != OMEGA_GPU_SESSION_LOCK_ID || !fk.locks_held) { fk.bad_unlocks++; return OMEGA_GPU_PLAT_REFUSED_ARGS; } 59 fk.locks_held = 0; 60 return 0; 61 } 62 static const char *f_diag(void *c) { (void)c; return ""; } 63 static int f_dev_open(void *c, uint32_t ord, OmegaGpuDeviceHandle *out) { 64 (void)c; 65 if (ord != 0 || fk.dev.generation) return OMEGA_GPU_PLAT_REFUSED_LIMIT; 66 int rc = omega_gpu_handles_open(&fk.t, OMEGA_GPU_HANDLE_DEVICE, 0, 1, 0, &out->index, &out->generation); 67 if (rc == 0) fk.dev = *out; 68 return rc; 69 } 70 static int f_dev_close(void *c, OmegaGpuDeviceHandle d) { 71 (void)c; 72 int rc = omega_gpu_handles_check(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); 73 if (rc) return rc; 74 (void)omega_gpu_handles_close(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); 75 memset(&fk.dev, 0, sizeof fk.dev); 76 return 0; 77 } 78 static OmegaGpuPlatformCaps g_caps = { 79 .abi_major = OMEGA_GPU_PLATFORM_ABI_MAJOR, .abi_minor = OMEGA_GPU_PLATFORM_ABI_MINOR, 80 .services = OMEGA_GPU_SVC_REQUIRED_SESSION, .max_devices = 1, .max_buffers = 1000, .max_queues = 1, .max_inflight = 1, 81 .max_buffer_bytes = 1ull << 30, .max_total_bytes = 1ull << 32, 82 }; 83 static int f_alloc(void *c, OmegaGpuDeviceHandle d, const OmegaGpuBufferDesc *desc, OmegaGpuBufferHandle *o, 84 OmegaCpuView *cpu, OmegaGpuVa *g, OmegaGpuBusAddr *b) { 85 (void)c; 86 int rc = omega_gpu_handles_check(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); 87 if (!rc) rc = omega_gpu_platform_check_buffer(desc, &g_caps); 88 if (!rc) rc = omega_gpu_handles_open(&fk.t, desc->kind, desc->bytes, 1000, 1ull << 32, &o->index, &o->generation); 89 if (rc) return rc; 90 void *m = calloc(1, desc->bytes); 91 if (!m) { (void)omega_gpu_handles_close(&fk.t, o->index, o->generation, desc->kind); return OMEGA_GPU_PLAT_REFUSED_LIMIT; } 92 fk.mem[o->index] = m; fk.desc[o->index] = *desc; 93 cpu->ptr = m; cpu->len = desc->bytes; g->va = VA_BASE + (uint64_t)o->index * VA_STRIDE; b->iova = 0; 94 return 0; 95 } 96 static int f_free(void *c, OmegaGpuBufferHandle h) { 97 (void)c; 98 if (h.index >= NB) return OMEGA_GPU_PLAT_REFUSED_BAD_HANDLE; 99 int rc = omega_gpu_handles_close(&fk.t, h.index, h.generation, fk.t.kind[h.index]); 100 if (rc) return rc; 101 free(fk.mem[h.index]); fk.mem[h.index] = NULL; 102 return 0; 103 } 104 static int buf_live(OmegaGpuBufferHandle h) { return h.index < NB && fk.t.live[h.index] && fk.t.generation[h.index] == h.generation; } 105 static void *va_to_cpu(uint64_t va) { 106 for (uint32_t i = 0; i < NB; i++) { 107 if (!fk.mem[i] || !fk.t.live[i]) continue; 108 const uint64_t base = VA_BASE + (uint64_t)i * VA_STRIDE; 109 if (va >= base && va < base + fk.desc[i].bytes) return (uint8_t *)fk.mem[i] + (va - base); 110 } 111 return NULL; 112 } 113 static int sync_check(OmegaGpuBufferHandle h, uint64_t off, uint64_t len) { 114 if (!buf_live(h)) return OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE; 115 if (len == 0 || off > fk.desc[h.index].bytes || len > fk.desc[h.index].bytes - off) return OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH; 116 return 0; 117 } 118 static int f_c2g(void *c, OmegaGpuBufferHandle h, uint64_t off, uint64_t len) { (void)c; return sync_check(h, off, len); } 119 /* gpu->cpu: a command list's launch semaphores (16-byte slots, every one still INIT) read DONE: the launches ran. 120 * Only a range whose every 16-byte slot holds INIT is taken for a semaphore array. */ 121 static int f_g2c(void *c, OmegaGpuBufferHandle h, uint64_t off, uint64_t len) { 122 (void)c; 123 int rc = sync_check(h, off, len); 124 if (rc) return rc; 125 if (len % 16u == 0) { 126 uint32_t *w = (uint32_t *)((uint8_t *)fk.mem[h.index] + off); 127 uint64_t i = 0, n = len / 16u; 128 while (i < n && w[4 * i] == OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT) i++; 129 if (i == n) for (i = 0; i < n; i++) w[4 * i] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE; 130 } 131 if (fk.dev_token_va) { uint32_t *t = va_to_cpu(fk.dev_token_va); if (t) t[0] = fk.dev_token; } 132 return 0; 133 } 134 static int f_poll(void *c, OmegaGpuBufferHandle h, uint64_t off, uint32_t want, uint64_t ms, uint32_t *seen) { 135 (void)c; 136 if (!buf_live(h)) return OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE; 137 if (!(fk.desc[h.index].rights & OMEGA_GPU_RIGHT_HOST_POLLED)) return OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS; 138 const volatile uint32_t *w = (const volatile uint32_t *)((uint8_t *)fk.mem[h.index] + off); 139 *seen = *w; 140 if ((int32_t)(*w - want) >= 0) return 0; 141 fk.ns += ms * 1000000u; 142 return OMEGA_GPU_POLL_TIMED_OUT; 143 } 144 static int f_qopen(void *c, OmegaGpuDeviceHandle d, OmegaGpuBufferHandle ring, OmegaGpuQueueHandle *o) { 145 (void)c; 146 int rc = omega_gpu_handles_check(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); 147 if (!rc && (!buf_live(ring) || fk.t.kind[ring.index] != OMEGA_GPU_HANDLE_BUF_QUEUE)) rc = OMEGA_GPU_PLAT_REFUSED_BAD_KIND; 148 if (!rc) rc = omega_gpu_handles_open(&fk.t, OMEGA_GPU_HANDLE_QUEUE, 0, 1, 0, &o->index, &o->generation); 149 if (!rc) { fk.q = *o; fk.ring = ring; } 150 return rc; 151 } 152 static int f_qclose(void *c, OmegaGpuQueueHandle q) { 153 (void)c; 154 int rc = omega_gpu_handles_close(&fk.t, q.index, q.generation, OMEGA_GPU_HANDLE_QUEUE); 155 if (!rc) memset(&fk.q, 0, sizeof fk.q); 156 return rc; 157 } 158 static uint32_t mthd(uint32_t subc, uint32_t m, uint32_t count) { return (2u << 28) | (count << 16) | (subc << 13) | (m >> 2); } 159 /* The fake device runs the stream: semaphore releases (0x005c: the completion markers) write their payload, compute 160 * report semaphores (0x0158, four-word: the stamps) write {payload, timestamp}. */ 161 static int f_submit(void *c, OmegaGpuQueueHandle q, const uint32_t *w, uint32_t n) { 162 (void)c; 163 int rc = omega_gpu_handles_check(&fk.t, q.index, q.generation, OMEGA_GPU_HANDLE_QUEUE); 164 if (rc) return rc; 165 if ((uint64_t)n * 4u > fk.desc[fk.ring.index].bytes) return OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH; 166 fk.submits++; 167 for (uint32_t i = 0; i + 5 < n; i++) { 168 if (w[i] == mthd(0, 0x005c, 5)) { 169 uint32_t *m = va_to_cpu((uint64_t)w[i + 1] | ((uint64_t)w[i + 2] << 32)); 170 if (m) *m = w[i + 3]; 171 i += 5; 172 } else if (w[i] == mthd(1, 0x0158, 5)) { 173 uint64_t *s = va_to_cpu((uint64_t)w[i + 3] | ((uint64_t)w[i + 4] << 32)); 174 if (s) { s[0] = (uint64_t)w[i + 1] | ((uint64_t)w[i + 2] << 32); s[1] = (fk.ns += 1000u); } 175 i += 5; 176 } 177 } 178 return 0; 179 } 180 static int f_retire(void *c, OmegaGpuQueueHandle q) { (void)c; (void)q; return 0; } 181 static void *f_halloc(void *c, size_t b) { (void)c; return malloc(b); } 182 static void f_hfree(void *c, void *p) { (void)c; free(p); } 183 184 static OmegaGpuPlatform g_plat = { 185 .ctx = &fk, .time_now_ms = f_now_ms, .wait_ms = f_wait, .lock = f_lock, .unlock = f_unlock, 186 .dma_alloc = f_alloc, .dma_free = f_free, .sync_cpu_to_gpu = f_c2g, .sync_gpu_to_cpu = f_g2c, .poll_word = f_poll, 187 .device_open = f_dev_open, .device_close = f_dev_close, .diag_text = f_diag, 188 .queue_open = f_qopen, .queue_close = f_qclose, .queue_submit = f_submit, .queue_retire = f_retire, 189 .time_now_ns = f_now_ns, .host_alloc = f_halloc, .host_free = f_hfree, 190 }; 191 192 /* ---- a one-layer graph on the fake ---- */ 193 #define LAYERS 1u 194 #define VOCAB 256u 195 #define MAX_POS 256u 196 typedef struct { 197 OmegaGpuDecodeAttnCache *attn; 198 OmegaGpuRouter *router; 199 OmegaGpuBf16Tensor *lm; 200 OmegaGpuDecodeGraph *g; 201 } World; 202 203 static int world_build(World *w) { 204 memset(w, 0, sizeof *w); 205 static uint16_t lm_w[VOCAB * OMEGA_GPU_DGRAPH_HIDDEN]; /* zeros: finite bf16 */ 206 if (omega_gpu_decode_attn_cache_alloc(LAYERS, MAX_POS, 1e6, &w->attn) != 0) return -1; 207 if (omega_gpu_router_alloc(LAYERS, &w->router) != 0) return -1; 208 if (omega_gpu_bf16_tensor_upload(VOCAB, OMEGA_GPU_DGRAPH_HIDDEN, lm_w, &w->lm) != 0) return -1; 209 OmegaGpuDecodeGraphConfig cfg; memset(&cfg, 0, sizeof cfg); 210 cfg.layers = LAYERS; cfg.vocab = VOCAB; cfg.eps = 1e-6f; cfg.norm_topk = 1; 211 cfg.router = w->router; cfg.attn = w->attn; cfg.lm_head = w->lm; 212 if (omega_gpu_decode_graph_create(&cfg, &w->g) != OMEGA_GPU_DGRAPH_OK) return -1; 213 omega_gpu_decode_graph_set_stamps(w->g, 1); 214 omega_gpu_decode_graph_set_flush_each(w->g, 1); /* one submission per launch: the list never bounds the token */ 215 return 0; 216 } 217 218 static float g_x[OMEGA_GPU_DGRAPH_HIDDEN]; 219 #define EXPECT_LOCK_FREE() do { EXPECT(fk.locks_held == 0); EXPECT(fk.double_locks == 0); EXPECT(fk.bad_unlocks == 0); } while (0) 220 221 static int install(void) { 222 memset(&fk, 0, sizeof fk); omega_gpu_handles_init(&fk.t); 223 for (unsigned i = 0; i < OMEGA_GPU_DGRAPH_HIDDEN; i++) g_x[i] = (float)(i % 7) - 3.0f; 224 return omega_gpu_session_set_platform(&g_plat, &g_caps); 225 } 226 227 /* One token of `launches` SWIGLU glue launches (each submitted on its own); returns end_token's code, fills *refused 228 * with the first record that did not return OK (launches when every record did). */ 229 static int token_of(World *w, uint32_t launches, uint32_t want, uint32_t *refused, uint32_t *tok) { 230 OmegaGpuDecodeGraphVas v; omega_gpu_decode_graph_vas(w->g, &v); 231 fk.dev_token_va = v.token; fk.dev_token = want; 232 *refused = launches; 233 if (omega_gpu_decode_graph_begin_token(w->g, g_x) != OMEGA_GPU_DGRAPH_OK) return -1000; 234 for (uint32_t i = 0; i < launches; i++) 235 if (omega_gpu_decode_graph_record_swiglu(w->g, v.gu, v.act, 1.0f) != OMEGA_GPU_DGRAPH_OK && *refused == launches) *refused = i; 236 const int rc = omega_gpu_decode_graph_end_token(w->g, tok, NULL); 237 fk.dev_token_va = 0; 238 return rc; 239 } 240 241 static uint64_t g_st[OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 2]; 242 static uint8_t g_kd[OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 2]; 243 244 /* MAX_LAUNCHES stamped launches: the timeline is exactly full and reads back whole. */ 245 static void c_full(void) { 246 World w; 247 if (install() != OMEGA_GPU_PLAT_OK) { EXPECT(!"fake platform installs"); return; } /* never fall back to the real driver */ 248 if (world_build(&w) != 0) { EXPECT(!"world builds on the fake"); return; } 249 uint32_t refused = 0, tok = 0; 250 EXPECT(token_of(&w, OMEGA_GPU_CMDLIST_MAX_LAUNCHES, 7, &refused, &tok) == OMEGA_GPU_DGRAPH_OK); 251 EXPECT(refused == OMEGA_GPU_CMDLIST_MAX_LAUNCHES && tok == 7); 252 EXPECT(fk.submits == OMEGA_GPU_CMDLIST_MAX_LAUNCHES); 253 EXPECT_LOCK_FREE(); 254 EXPECT(omega_gpu_decode_graph_stamps(w.g, g_st, g_kd, OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 2) == (int)OMEGA_GPU_CMDLIST_MAX_LAUNCHES); 255 for (uint32_t i = 0; i < OMEGA_GPU_CMDLIST_MAX_LAUNCHES; i++) if (!(g_st[i + 1] > g_st[i])) { EXPECT(!"stamps increase"); break; } 256 } 257 258 /* One launch more than the timeline holds: that record fails the token, nothing is written past stamp[], the lock is 259 * released at end_token, no timeline is offered, and the next (small) token runs and reads back normally. */ 260 static void c_overflow(void) { 261 World w; 262 if (install() != OMEGA_GPU_PLAT_OK) { EXPECT(!"fake platform installs"); return; } 263 if (world_build(&w) != 0) { EXPECT(!"world builds on the fake"); return; } 264 uint32_t refused = 0, tok = 0xdeadu; 265 EXPECT(token_of(&w, OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 1u, 7, &refused, &tok) == OMEGA_GPU_DGRAPH_CHIP_FAIL); 266 EXPECT(refused == OMEGA_GPU_CMDLIST_MAX_LAUNCHES); /* the 4097th launch, the first that has no stamp slot */ 267 EXPECT(tok == 0xdeadu); 268 EXPECT(strstr(omega_gpu_session_last_error(), "timeline full") != NULL); 269 EXPECT_LOCK_FREE(); 270 EXPECT(omega_gpu_decode_graph_stamps(w.g, g_st, g_kd, OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 2) == OMEGA_GPU_DGRAPH_BAD_ARGS); 271 EXPECT(token_of(&w, 3, 9, &refused, &tok) == OMEGA_GPU_DGRAPH_OK); 272 EXPECT(refused == 3 && tok == 9); 273 EXPECT_LOCK_FREE(); 274 EXPECT(omega_gpu_decode_graph_stamps(w.g, g_st, g_kd, OMEGA_GPU_CMDLIST_MAX_LAUNCHES + 2) == 3); 275 } 276 277 static const struct { const char *name; void (*fn)(void); } CASES[] = { 278 { "timeline exactly full: 4096 stamped launches read back", c_full }, 279 { "one launch past the timeline: token fails, no write past stamp[]", c_overflow }, 280 }; 281 282 int main(int argc, char **argv) { 283 const unsigned n = sizeof CASES / sizeof CASES[0]; 284 if (argc == 2) { /* one case in this process (debugging): its index */ 285 const unsigned i = (unsigned)atoi(argv[1]); 286 if (i >= n) return 2; 287 case_ok = 1; CASES[i].fn(); 288 printf("%s %s\n", case_ok ? "ok " : "FAIL", CASES[i].name); 289 return case_ok ? 0 : 1; 290 } 291 unsigned failed = 0; 292 for (unsigned i = 0; i < n; i++) { 293 fflush(stdout); 294 pid_t pid = fork(); 295 if (pid == 0) { case_ok = 1; CASES[i].fn(); fflush(stdout); _exit(case_ok ? 0 : 1); } 296 int status = 0; 297 int ok = pid > 0 && waitpid(pid, &status, 0) == pid && WIFEXITED(status) && WEXITSTATUS(status) == 0; 298 printf("%s %s\n", ok ? "ok " : "FAIL", CASES[i].name); 299 if (!ok) failed++; 300 } 301 printf("test_omega_gpu_decode_graph_timeline: %u of %u cases passed: %s\n", n - failed, n, failed ? "FAIL" : "PASS"); 302 return failed ? 1 : 0; 303 }