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