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_abort.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_abort.c` 440 lines · 22592 bytes Raw Blame History 1 /* test_omega_gpu_decode_graph_abort: lock and token-state recovery of the decode graph's split API (omega #518) on a 2 * FAKE OmegaGpuPlatform. Host program, links libomega_gpu.a with the fake installed first; opens no device, touches 3 * no GPU. 4 * 5 * The fake's session lock REFUSES a second acquisition while held (counted in fk.double_locks) instead of blocking, 6 * so a lock left held by a failed call shows up as a count, not a hung test. The fake device never runs a kernel: a 7 * submitted list's markers never land (the cmdlist times out on the fake clock), and the graph's token slot is written 8 * by the "device" at the gpu->cpu sync only when a case arms fk.dev_token (a token the device produced). 9 * 10 * Contract under test (omega_gpu_decode_graph.h): a failed begin_token returns with the session lock released and the 11 * graph out of the token (no end_token owed); abort_token drops a begun token without submitting anything; end_token 12 * after a failed record submits nothing and releases the lock; an uncertain completion latches the process and every 13 * later begin fails without a retry or a submission. After each failure: set_final_norm, read, free and the next 14 * valid token neither deadlock nor see a stale token state. 15 * 16 * Each case runs in its own child process (a latch lasts for the life of the process). 17 */ 18 #include "omega_gpu_decode_graph.h" 19 #include "omega_gpu_session.h" 20 #include "omega_gpu_platform.h" 21 #include "omega_blackwell_codegen_dec.h" 22 23 #include 24 #include 25 #include 26 #include 27 #include 28 29 static int case_ok; 30 #define EXPECT(c) do { if (!(c)) { case_ok = 0; printf(" expect failed at line %d: %s\n", __LINE__, #c); } } while (0) 31 32 /* ---- the fake platform ---- */ 33 #define NB OMEGA_GPU_HANDLE_SLOTS 34 #define VA_BASE 0x100000000ull 35 #define VA_STRIDE 0x1000000ull 36 static struct { 37 OmegaGpuHandleTable t; 38 void *mem[NB]; 39 OmegaGpuBufferDesc desc[NB]; 40 OmegaGpuDeviceHandle dev; 41 OmegaGpuQueueHandle q; 42 OmegaGpuBufferHandle ring; 43 uint64_t ns; 44 unsigned locks_held, double_locks, bad_unlocks; 45 unsigned submits, dev_opens; 46 int fail_open, fail_c2g; /* refuse device open / every cpu->gpu visibility sync */ 47 uint64_t dev_token_va; /* nonzero: the device wrote dev_token there (seen at the gpu->cpu sync) */ 48 uint32_t dev_token; 49 } fk; 50 51 static uint64_t f_now_ns(void *c) { (void)c; fk.ns += 1000u; return fk.ns; } 52 static uint64_t f_now_ms(void *c) { return f_now_ns(c) / 1000000u; } 53 static int f_wait(void *c, uint64_t ms) { (void)c; fk.ns += ms * 1000000u; return 0; } 54 static int f_lock(void *c, uint32_t id) { 55 (void)c; 56 if (id != OMEGA_GPU_SESSION_LOCK_ID) return OMEGA_GPU_PLAT_REFUSED_ARGS; 57 if (fk.locks_held) { fk.double_locks++; return OMEGA_GPU_PLAT_REFUSED_BUSY; } /* a real lock would block here forever */ 58 fk.locks_held = 1; 59 return 0; 60 } 61 static int f_unlock(void *c, uint32_t id) { 62 (void)c; 63 if (id != OMEGA_GPU_SESSION_LOCK_ID || !fk.locks_held) { fk.bad_unlocks++; return OMEGA_GPU_PLAT_REFUSED_ARGS; } 64 fk.locks_held = 0; 65 return 0; 66 } 67 static const char *f_diag(void *c) { (void)c; return fk.fail_open ? "fake device: open refused" : ""; } 68 69 static int f_dev_open(void *c, uint32_t ord, OmegaGpuDeviceHandle *out) { 70 (void)c; 71 if (fk.fail_open) return OMEGA_GPU_PLAT_REFUSED_DEVICE; 72 if (ord != 0 || fk.dev.generation) return OMEGA_GPU_PLAT_REFUSED_LIMIT; 73 int rc = omega_gpu_handles_open(&fk.t, OMEGA_GPU_HANDLE_DEVICE, 0, 1, 0, &out->index, &out->generation); 74 if (rc == 0) { fk.dev = *out; fk.dev_opens++; } 75 return rc; 76 } 77 static int f_dev_close(void *c, OmegaGpuDeviceHandle d) { 78 (void)c; 79 int rc = omega_gpu_handles_check(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); 80 if (rc) return rc; 81 /* every handle of the device goes stale; the host memory stays mapped (leaked) so a graph that outlives the device 82 * writes into its own stale buffer, not freed memory: this test checks the lock, not use after close */ 83 for (uint32_t i = 0; i < NB; i++) 84 if (fk.t.live[i] && i != d.index) { (void)omega_gpu_handles_close(&fk.t, i, fk.t.generation[i], fk.t.kind[i]); fk.mem[i] = NULL; } 85 (void)omega_gpu_handles_close(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); 86 memset(&fk.dev, 0, sizeof fk.dev); memset(&fk.q, 0, sizeof fk.q); 87 return 0; 88 } 89 90 static OmegaGpuPlatformCaps g_caps = { 91 .abi_major = OMEGA_GPU_PLATFORM_ABI_MAJOR, .abi_minor = OMEGA_GPU_PLATFORM_ABI_MINOR, 92 .services = OMEGA_GPU_SVC_REQUIRED_SESSION, .max_devices = 1, .max_buffers = 1000, .max_queues = 1, .max_inflight = 1, 93 .max_buffer_bytes = 1ull << 30, .max_total_bytes = 1ull << 32, 94 }; 95 96 static int f_alloc(void *c, OmegaGpuDeviceHandle d, const OmegaGpuBufferDesc *desc, OmegaGpuBufferHandle *o, 97 OmegaCpuView *cpu, OmegaGpuVa *g, OmegaGpuBusAddr *b) { 98 (void)c; 99 int rc = omega_gpu_handles_check(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); 100 if (!rc) rc = omega_gpu_platform_check_buffer(desc, &g_caps); 101 if (!rc) rc = omega_gpu_handles_open(&fk.t, desc->kind, desc->bytes, 1000, 1ull << 32, &o->index, &o->generation); 102 if (rc) return rc; 103 void *m = calloc(1, desc->bytes); 104 if (!m) { (void)omega_gpu_handles_close(&fk.t, o->index, o->generation, desc->kind); return OMEGA_GPU_PLAT_REFUSED_LIMIT; } 105 fk.mem[o->index] = m; fk.desc[o->index] = *desc; 106 cpu->ptr = m; cpu->len = desc->bytes; g->va = VA_BASE + (uint64_t)o->index * VA_STRIDE; b->iova = 0; 107 return 0; 108 } 109 static int f_free(void *c, OmegaGpuBufferHandle h) { 110 (void)c; 111 if (h.index >= NB) return OMEGA_GPU_PLAT_REFUSED_BAD_HANDLE; 112 int rc = omega_gpu_handles_close(&fk.t, h.index, h.generation, fk.t.kind[h.index]); 113 if (rc) return rc; 114 free(fk.mem[h.index]); fk.mem[h.index] = NULL; 115 return 0; 116 } 117 static int buf_live(OmegaGpuBufferHandle h) { return h.index < NB && fk.t.live[h.index] && fk.t.generation[h.index] == h.generation; } 118 static void *va_to_cpu(uint64_t va) { 119 for (uint32_t i = 0; i < NB; i++) { 120 if (!fk.mem[i] || !fk.t.live[i]) continue; 121 const uint64_t base = VA_BASE + (uint64_t)i * VA_STRIDE; 122 if (va >= base && va < base + fk.desc[i].bytes) return (uint8_t *)fk.mem[i] + (va - base); 123 } 124 return NULL; 125 } 126 static int sync_check(OmegaGpuBufferHandle h, uint64_t off, uint64_t len) { 127 if (!buf_live(h)) return OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE; 128 if (len == 0 || off > fk.desc[h.index].bytes || len > fk.desc[h.index].bytes - off) return OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH; 129 return 0; 130 } 131 static int f_c2g(void *c, OmegaGpuBufferHandle h, uint64_t off, uint64_t len) { 132 (void)c; 133 if (fk.fail_c2g) return OMEGA_GPU_PLAT_REFUSED_DEVICE; 134 return sync_check(h, off, len); 135 } 136 static int f_g2c(void *c, OmegaGpuBufferHandle h, uint64_t off, uint64_t len) { 137 (void)c; 138 int rc = sync_check(h, off, len); 139 if (rc == 0 && fk.dev_token_va) { uint32_t *t = va_to_cpu(fk.dev_token_va); if (t) t[0] = fk.dev_token; } 140 return rc; 141 } 142 static int f_poll(void *c, OmegaGpuBufferHandle h, uint64_t off, uint32_t want, uint64_t ms, uint32_t *seen) { 143 (void)c; 144 if (!buf_live(h)) return OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE; 145 if (!(fk.desc[h.index].rights & OMEGA_GPU_RIGHT_HOST_POLLED)) return OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS; 146 const volatile uint32_t *w = (const volatile uint32_t *)((uint8_t *)fk.mem[h.index] + off); 147 *seen = *w; 148 if ((int32_t)(*w - want) >= 0) return 0; 149 fk.ns += ms * 1000000u; 150 return OMEGA_GPU_POLL_TIMED_OUT; 151 } 152 static int f_qopen(void *c, OmegaGpuDeviceHandle d, OmegaGpuBufferHandle ring, OmegaGpuQueueHandle *o) { 153 (void)c; 154 int rc = omega_gpu_handles_check(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); 155 if (!rc && (!buf_live(ring) || fk.t.kind[ring.index] != OMEGA_GPU_HANDLE_BUF_QUEUE)) rc = OMEGA_GPU_PLAT_REFUSED_BAD_KIND; 156 if (!rc) rc = omega_gpu_handles_open(&fk.t, OMEGA_GPU_HANDLE_QUEUE, 0, 1, 0, &o->index, &o->generation); 157 if (!rc) { fk.q = *o; fk.ring = ring; } 158 return rc; 159 } 160 static int f_qclose(void *c, OmegaGpuQueueHandle q) { 161 (void)c; 162 int rc = omega_gpu_handles_close(&fk.t, q.index, q.generation, OMEGA_GPU_HANDLE_QUEUE); 163 if (!rc) memset(&fk.q, 0, sizeof fk.q); 164 return rc; 165 } 166 /* The fake device accepts the words and runs nothing: no marker, semaphore or output ever lands. */ 167 static int f_submit(void *c, OmegaGpuQueueHandle q, const uint32_t *w, uint32_t n) { 168 (void)c; (void)w; 169 int rc = omega_gpu_handles_check(&fk.t, q.index, q.generation, OMEGA_GPU_HANDLE_QUEUE); 170 if (rc) return rc; 171 if ((uint64_t)n * 4u > fk.desc[fk.ring.index].bytes) return OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH; 172 fk.submits++; 173 return 0; 174 } 175 static int f_retire(void *c, OmegaGpuQueueHandle q) { (void)c; (void)q; return 0; } 176 static void *f_halloc(void *c, size_t b) { (void)c; return malloc(b); } 177 static void f_hfree(void *c, void *p) { (void)c; free(p); } 178 179 static OmegaGpuPlatform g_plat = { 180 .ctx = &fk, .time_now_ms = f_now_ms, .wait_ms = f_wait, .lock = f_lock, .unlock = f_unlock, 181 .dma_alloc = f_alloc, .dma_free = f_free, .sync_cpu_to_gpu = f_c2g, .sync_gpu_to_cpu = f_g2c, .poll_word = f_poll, 182 .device_open = f_dev_open, .device_close = f_dev_close, .diag_text = f_diag, 183 .queue_open = f_qopen, .queue_close = f_qclose, .queue_submit = f_submit, .queue_retire = f_retire, 184 .time_now_ns = f_now_ns, .host_alloc = f_halloc, .host_free = f_hfree, 185 }; 186 187 /* ---- a one-layer graph on the fake ---- */ 188 #define LAYERS 1u 189 #define VOCAB 256u 190 #define MAX_POS 256u 191 typedef struct { 192 OmegaGpuDecodeAttnCache *attn; 193 OmegaGpuRouter *router; 194 OmegaGpuBf16Tensor *lm; 195 OmegaGpuDecodeGraph *g; 196 } World; 197 198 static int world_build(World *w) { 199 memset(w, 0, sizeof *w); 200 static uint16_t lm_w[VOCAB * OMEGA_GPU_DGRAPH_HIDDEN]; /* zeros: finite bf16 */ 201 if (omega_gpu_decode_attn_cache_alloc(LAYERS, MAX_POS, 1e6, &w->attn) != 0) return -1; 202 if (omega_gpu_router_alloc(LAYERS, &w->router) != 0) return -1; 203 if (omega_gpu_bf16_tensor_upload(VOCAB, OMEGA_GPU_DGRAPH_HIDDEN, lm_w, &w->lm) != 0) return -1; 204 OmegaGpuDecodeGraphConfig cfg; memset(&cfg, 0, sizeof cfg); 205 cfg.layers = LAYERS; cfg.vocab = VOCAB; cfg.eps = 1e-6f; cfg.norm_topk = 1; 206 cfg.router = w->router; cfg.attn = w->attn; cfg.lm_head = w->lm; 207 return omega_gpu_decode_graph_create(&cfg, &w->g) == OMEGA_GPU_DGRAPH_OK ? 0 : -1; 208 } 209 static void world_free(World *w) { 210 omega_gpu_decode_graph_free(w->g); 211 omega_gpu_bf16_tensor_free(w->lm); 212 omega_gpu_router_free(w->router); 213 omega_gpu_decode_attn_cache_free(w->attn); 214 memset(w, 0, sizeof *w); 215 } 216 217 static float g_x[OMEGA_GPU_DGRAPH_HIDDEN], g_norm[OMEGA_GPU_DGRAPH_HIDDEN]; 218 219 /* The postcondition of every failed or finished call: lock free, never taken twice, never released twice. */ 220 #define EXPECT_LOCK_FREE() do { EXPECT(fk.locks_held == 0); EXPECT(fk.double_locks == 0); EXPECT(fk.bad_unlocks == 0); } while (0) 221 222 /* Not in a token any more: the between-token calls run (BAD_ARGS here would mean in_token stuck). */ 223 static void expect_between_tokens(World *w) { 224 EXPECT(omega_gpu_decode_graph_set_final_norm(w->g, g_norm) == OMEGA_GPU_DGRAPH_OK); 225 EXPECT_LOCK_FREE(); 226 float x[OMEGA_GPU_DGRAPH_HIDDEN]; 227 EXPECT(omega_gpu_decode_graph_read(w->g, x, NULL, NULL) == OMEGA_GPU_DGRAPH_OK); 228 EXPECT_LOCK_FREE(); 229 } 230 231 /* A valid split-API token: begin, nothing recorded, end; the fake device "wrote" the token. */ 232 static void expect_valid_token(World *w, uint32_t want) { 233 OmegaGpuDecodeGraphVas v; omega_gpu_decode_graph_vas(w->g, &v); 234 fk.dev_token_va = v.token; fk.dev_token = want; 235 const unsigned submits = fk.submits; 236 EXPECT(omega_gpu_decode_graph_begin_token(w->g, g_x) == OMEGA_GPU_DGRAPH_OK); 237 EXPECT(fk.locks_held == 1); 238 uint32_t tok = 0xdeadu; 239 OmegaGpuDecodeGraphStats st; memset(&st, 0xff, sizeof st); 240 EXPECT(omega_gpu_decode_graph_end_token(w->g, &tok, &st) == OMEGA_GPU_DGRAPH_OK); 241 EXPECT(tok == want && st.launches == 0 && st.submits == 0); 242 EXPECT(fk.submits == submits); /* an empty list submits nothing */ 243 EXPECT_LOCK_FREE(); 244 fk.dev_token_va = 0; 245 } 246 247 static int install(void) { 248 memset(&fk, 0, sizeof fk); omega_gpu_handles_init(&fk.t); 249 for (unsigned i = 0; i < OMEGA_GPU_DGRAPH_HIDDEN; i++) { g_x[i] = (float)(i % 7) - 3.0f; g_norm[i] = 1.0f; } 250 return omega_gpu_session_set_platform(&g_plat, &g_caps); 251 } 252 253 /* Visibility sync (cpu->gpu of x and the poisoned token) refused: split and one-shot paths, repeated, then recovery. */ 254 static void c_sync_fails(void) { 255 World w; 256 if (install() != OMEGA_GPU_PLAT_OK) { EXPECT(!"fake platform installs"); return; } /* never fall back to the real driver */ 257 if (world_build(&w) != 0) { EXPECT(!"world builds on the fake"); return; } 258 EXPECT_LOCK_FREE(); 259 const unsigned submits = fk.submits; 260 fk.fail_c2g = 1; 261 for (int rep = 0; rep < 3; rep++) { /* same outcome every time */ 262 EXPECT(omega_gpu_decode_graph_begin_token(w.g, g_x) == OMEGA_GPU_DGRAPH_CHIP_FAIL); 263 EXPECT_LOCK_FREE(); 264 } 265 /* nothing owed after a failed begin: end and abort see no token */ 266 uint32_t tok = 7; 267 EXPECT(omega_gpu_decode_graph_end_token(w.g, &tok, NULL) == OMEGA_GPU_DGRAPH_BAD_ARGS && tok == 7); 268 EXPECT(omega_gpu_decode_graph_abort_token(w.g) == OMEGA_GPU_DGRAPH_BAD_ARGS); 269 EXPECT(omega_gpu_decode_graph_record_final(w.g) == OMEGA_GPU_DGRAPH_BAD_ARGS); 270 EXPECT_LOCK_FREE(); 271 /* one-shot: the begin failure comes back as CHIP_FAIL with zeroed stats, lock free */ 272 OmegaGpuDecodeGraphStats st; memset(&st, 0xff, sizeof st); 273 tok = 7; 274 EXPECT(omega_gpu_decode_graph_token(w.g, g_x, 0, &tok, &st) == OMEGA_GPU_DGRAPH_CHIP_FAIL); 275 EXPECT(tok == 7 && st.launches == 0 && st.submits == 0 && st.total_ns == 0); 276 EXPECT_LOCK_FREE(); 277 EXPECT(fk.submits == submits); /* nothing reached the device, nothing retried */ 278 EXPECT(!omega_gpu_session_is_blocked()); 279 fk.fail_c2g = 0; 280 expect_between_tokens(&w); 281 expect_valid_token(&w, 42u); 282 expect_valid_token(&w, 43u); /* and again */ 283 world_free(&w); 284 EXPECT_LOCK_FREE(); 285 } 286 287 /* Device open refused: the session was closed (every handle stale), the reopen in begin_token fails. */ 288 static void c_open_fails(void) { 289 World w; 290 if (install() != OMEGA_GPU_PLAT_OK) { EXPECT(!"fake platform installs"); return; } /* never fall back to the real driver */ 291 if (world_build(&w) != 0) { EXPECT(!"world builds on the fake"); return; } 292 omega_gpu_session_close(); 293 EXPECT_LOCK_FREE(); 294 fk.fail_open = 1; 295 for (int rep = 0; rep < 3; rep++) { 296 EXPECT(omega_gpu_decode_graph_begin_token(w.g, g_x) == OMEGA_GPU_DGRAPH_CHIP_FAIL); 297 EXPECT_LOCK_FREE(); 298 } 299 EXPECT(omega_gpu_decode_graph_token(w.g, g_x, 0, NULL, NULL) == OMEGA_GPU_DGRAPH_CHIP_FAIL); 300 EXPECT_LOCK_FREE(); 301 EXPECT(omega_gpu_decode_graph_end_token(w.g, NULL, NULL) == OMEGA_GPU_DGRAPH_BAD_ARGS); 302 /* the graph's buffers belong to the closed device: set_final_norm and read reach the stale sync and refuse (not 303 * BAD_ARGS: the graph is out of the token), nothing hangs */ 304 EXPECT(omega_gpu_decode_graph_set_final_norm(w.g, g_norm) == OMEGA_GPU_DGRAPH_CHIP_FAIL); 305 EXPECT(omega_gpu_decode_graph_read(w.g, NULL, NULL, NULL) == OMEGA_GPU_DGRAPH_CHIP_FAIL); 306 EXPECT_LOCK_FREE(); 307 fk.fail_open = 0; 308 /* reopen succeeds; the old graph's buffer is stale, so its begin fails at the visibility sync, lock free */ 309 EXPECT(omega_gpu_decode_graph_begin_token(w.g, g_x) == OMEGA_GPU_DGRAPH_CHIP_FAIL); 310 EXPECT_LOCK_FREE(); 311 world_free(&w); 312 EXPECT_LOCK_FREE(); 313 /* a fresh graph on the reopened device runs a valid token */ 314 if (world_build(&w) != 0) { EXPECT(!"world rebuilds after the reopen"); return; } 315 expect_valid_token(&w, 5u); 316 world_free(&w); 317 EXPECT_LOCK_FREE(); 318 } 319 320 /* The caller abandons a begun token (cancellation): nothing submitted, lock released, the next token runs. */ 321 static void c_abort(void) { 322 World w; 323 if (install() != OMEGA_GPU_PLAT_OK) { EXPECT(!"fake platform installs"); return; } /* never fall back to the real driver */ 324 if (world_build(&w) != 0) { EXPECT(!"world builds on the fake"); return; } 325 OmegaGpuDecodeGraphVas v; omega_gpu_decode_graph_vas(w.g, &v); 326 const unsigned submits = fk.submits; 327 /* abort after a recorded launch */ 328 EXPECT(omega_gpu_decode_graph_begin_token(w.g, g_x) == OMEGA_GPU_DGRAPH_OK); 329 EXPECT(omega_gpu_decode_graph_record_res(w.g, OMEGA_DEC_RES_NORM, v.x, 0, 0, v.x, v.xq, 1e-6f, 1.0f) == OMEGA_GPU_DGRAPH_OK); 330 EXPECT(omega_gpu_decode_graph_abort_token(w.g) == OMEGA_GPU_DGRAPH_OK); 331 EXPECT_LOCK_FREE(); 332 EXPECT(fk.submits == submits); 333 EXPECT(omega_gpu_decode_graph_abort_token(w.g) == OMEGA_GPU_DGRAPH_BAD_ARGS); /* only once */ 334 EXPECT(omega_gpu_decode_graph_end_token(w.g, NULL, NULL) == OMEGA_GPU_DGRAPH_BAD_ARGS); 335 EXPECT_LOCK_FREE(); 336 /* abort after a failed record */ 337 EXPECT(omega_gpu_decode_graph_begin_token(w.g, g_x) == OMEGA_GPU_DGRAPH_OK); 338 EXPECT(omega_gpu_decode_graph_record_res(w.g, 0xffu, v.x, 0, 0, 0, v.xq, 1e-6f, 1.0f) == OMEGA_GPU_DGRAPH_BAD_ARGS); 339 EXPECT(omega_gpu_decode_graph_abort_token(w.g) == OMEGA_GPU_DGRAPH_OK); 340 EXPECT_LOCK_FREE(); 341 EXPECT(fk.submits == submits); 342 /* abort with nothing begun */ 343 EXPECT(omega_gpu_decode_graph_abort_token(NULL) == OMEGA_GPU_DGRAPH_BAD_ARGS); 344 expect_between_tokens(&w); 345 expect_valid_token(&w, 9u); 346 world_free(&w); 347 EXPECT_LOCK_FREE(); 348 } 349 350 /* end_token after a failed record: no submit, the failure returned, lock released, the next token runs. */ 351 static void c_end_after_record_fail(void) { 352 World w; 353 if (install() != OMEGA_GPU_PLAT_OK) { EXPECT(!"fake platform installs"); return; } /* never fall back to the real driver */ 354 if (world_build(&w) != 0) { EXPECT(!"world builds on the fake"); return; } 355 OmegaGpuDecodeGraphVas v; omega_gpu_decode_graph_vas(w.g, &v); 356 const unsigned submits = fk.submits; 357 EXPECT(omega_gpu_decode_graph_begin_token(w.g, g_x) == OMEGA_GPU_DGRAPH_OK); 358 EXPECT(omega_gpu_decode_graph_record_res(w.g, OMEGA_DEC_RES_NORM, v.x, 0, 0, v.x, v.xq, 1e-6f, 1.0f) == OMEGA_GPU_DGRAPH_OK); 359 EXPECT(omega_gpu_decode_graph_record_layer(w.g, 0, 0) == OMEGA_GPU_DGRAPH_BAD_ARGS); /* layer 0 never set */ 360 EXPECT(omega_gpu_decode_graph_record_final(w.g) == OMEGA_GPU_DGRAPH_CHIP_FAIL); /* failed sticks */ 361 uint32_t tok = 7; 362 EXPECT(omega_gpu_decode_graph_end_token(w.g, &tok, NULL) == OMEGA_GPU_DGRAPH_CHIP_FAIL && tok == 7); 363 EXPECT(fk.submits == submits); 364 EXPECT_LOCK_FREE(); 365 /* one-shot with an unset layer: the same, deterministically */ 366 for (int rep = 0; rep < 2; rep++) { 367 EXPECT(omega_gpu_decode_graph_token(w.g, g_x, 0, &tok, NULL) == OMEGA_GPU_DGRAPH_CHIP_FAIL && tok == 7); 368 EXPECT(fk.submits == submits); 369 EXPECT_LOCK_FREE(); 370 } 371 expect_between_tokens(&w); 372 expect_valid_token(&w, 11u); 373 world_free(&w); 374 EXPECT_LOCK_FREE(); 375 } 376 377 /* An uncertain completion: the submitted list never completes, the process latches. end_token reports the failure (never 378 * a token); every later begin fails at once with the lock released, submits nothing and retries nothing. */ 379 static void c_latched(void) { 380 World w; 381 if (install() != OMEGA_GPU_PLAT_OK) { EXPECT(!"fake platform installs"); return; } /* never fall back to the real driver */ 382 if (world_build(&w) != 0) { EXPECT(!"world builds on the fake"); return; } 383 OmegaGpuDecodeGraphVas v; omega_gpu_decode_graph_vas(w.g, &v); 384 fk.dev_token_va = v.token; fk.dev_token = 3u; /* even a token in the slot must not be claimed after a timeout */ 385 EXPECT(omega_gpu_decode_graph_begin_token(w.g, g_x) == OMEGA_GPU_DGRAPH_OK); 386 EXPECT(omega_gpu_decode_graph_record_res(w.g, OMEGA_DEC_RES_NORM, v.x, 0, 0, v.x, v.xq, 1e-6f, 1.0f) == OMEGA_GPU_DGRAPH_OK); 387 uint32_t tok = 7; 388 EXPECT(omega_gpu_decode_graph_end_token(w.g, &tok, NULL) == OMEGA_GPU_DGRAPH_CHIP_FAIL && tok == 7); 389 EXPECT(fk.submits >= 1); 390 EXPECT(omega_gpu_session_is_blocked()); 391 EXPECT_LOCK_FREE(); 392 const unsigned submits = fk.submits, opens = fk.dev_opens; 393 for (int rep = 0; rep < 3; rep++) { 394 EXPECT(omega_gpu_decode_graph_begin_token(w.g, g_x) == OMEGA_GPU_DGRAPH_CHIP_FAIL); 395 EXPECT_LOCK_FREE(); 396 EXPECT(omega_gpu_decode_graph_token(w.g, g_x, 0, &tok, NULL) == OMEGA_GPU_DGRAPH_CHIP_FAIL && tok == 7); 397 EXPECT_LOCK_FREE(); 398 } 399 EXPECT(fk.submits == submits && fk.dev_opens == opens); /* no retry, no reopen */ 400 EXPECT(omega_gpu_decode_graph_end_token(w.g, NULL, NULL) == OMEGA_GPU_DGRAPH_BAD_ARGS); 401 EXPECT(omega_gpu_decode_graph_abort_token(w.g) == OMEGA_GPU_DGRAPH_BAD_ARGS); 402 EXPECT(omega_gpu_decode_graph_set_final_norm(w.g, g_norm) == OMEGA_GPU_DGRAPH_OK); 403 (void)omega_gpu_decode_graph_read(w.g, NULL, NULL, NULL); /* latched: refused or parked, but it returns */ 404 EXPECT_LOCK_FREE(); 405 world_free(&w); 406 EXPECT_LOCK_FREE(); 407 EXPECT(omega_gpu_session_is_blocked()); 408 } 409 410 typedef struct { const char *name; void (*fn)(void); } Case; 411 static const Case CASES[] = { 412 { "visibility sync refused at begin: split + one-shot recover", c_sync_fails }, 413 { "device open refused at begin: lock free, reopen recovers", c_open_fails }, 414 { "abort_token: cancel a begun token, nothing submitted", c_abort }, 415 { "end_token after a failed record: no submit, lock free", c_end_after_record_fail }, 416 { "latched session: begin fails at once, no retry", c_latched }, 417 }; 418 419 int main(int argc, char **argv) { 420 const unsigned n = sizeof CASES / sizeof CASES[0]; 421 if (argc == 2) { /* one case in this process (debugging): its index */ 422 const unsigned i = (unsigned)atoi(argv[1]); 423 if (i >= n) return 2; 424 case_ok = 1; CASES[i].fn(); 425 printf("%s %s\n", case_ok ? "ok " : "FAIL", CASES[i].name); 426 return case_ok ? 0 : 1; 427 } 428 unsigned failed = 0; 429 for (unsigned i = 0; i < n; i++) { 430 fflush(stdout); 431 pid_t pid = fork(); 432 if (pid == 0) { case_ok = 1; CASES[i].fn(); fflush(stdout); _exit(case_ok ? 0 : 1); } 433 int status = 0; 434 int ok = pid > 0 && waitpid(pid, &status, 0) == pid && WIFEXITED(status) && WEXITSTATUS(status) == 0; 435 printf("%s %s\n", ok ? "ok " : "FAIL", CASES[i].name); 436 if (!ok) failed++; 437 } 438 printf("test_omega_gpu_decode_graph_abort: %u of %u cases passed: %s\n", n - failed, n, failed ? "FAIL" : "PASS"); 439 return failed ? 1 : 0; 440 }