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_attn_maxpos.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_attn_maxpos.c` 375 lines · 20185 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* test_omega_gpu_decode_attn_maxpos: the decode attention position bound (omega #535: OMEGA_GPU_DATTN_MAX_POS 131072, 18 * the 128k context) on a FAKE OmegaGpuPlatform. Host program; opens no device, touches no GPU. The Makefile links a 19 * sanitized copy of src/omega_gpu_decode_attn_api.c ahead of libomega_gpu.a, so every cache write, RoPE fill and 20 * launch staging of the file under test is ASan/UBSan checked against the fake's exact-size buffers. 21 * 22 * The fake device completes every submission: completion-marker releases write their payload, and the launch "writes" 23 * the attention output buffer (the one DATA buffer of 32 * 128 f32), so the API's poison check passes. It computes 24 * nothing: this is a bound and sizing test, not a kernel result (the kernel's numbers are the chip battery's, 25 * tools/run_gpu_decode_attn_chip.sh). 26 * 27 * Contract under test, at the cap C = OMEGA_GPU_DATTN_MAX_POS = 131072: 28 * - a cache of C positions allocates; C + 256 (the next legal multiple) is refused before any device work; 29 * - every buffer the cache owns is sized from max_pos: K|V planes 2 * (4 * C * 256 + slack), RoPE C * 512, 30 * split partials 4 * (C / 256) * 1040 * 4; 31 * - KV rows at 65535, 65536, 131071 round-trip; 131072 and 131073 are refused, as is a count crossing the cap; 32 * - RoPE rows 0, 65535, 65536, 99999, 131071 equal cos/sin of pos * theta^(-2i/128) from libm; 33 * - a decode step at pos 131071 launches with 512 splits (the whole partial buffer); 131072, 131073 are refused 34 * before any submission; 35 * - a cache of the old cap 65536 refuses pos 65536: the bound is the cache's own max_pos. 36 * Each case runs in its own child process. 37 */ 38 #include "omega_gpu_decode_attn_api.h" 39 #include "omega_blackwell_codegen_attn.h" 40 #include "omega_gpu_session.h" 41 #include "omega_gpu_platform.h" 42 #include "omega_blackwell_submit.h" 43 44 #include 45 #include 46 #include 47 #include 48 #include 49 #include 50 51 static int case_ok; 52 #define EXPECT(c) do { if (!(c)) { case_ok = 0; printf(" expect failed at line %d: %s\n", __LINE__, #c); } } while (0) 53 54 #define CAP OMEGA_GPU_DATTN_MAX_POS 55 #define THETA `1000000`.0 56 57 /* ---- the fake platform ---- */ 58 #define NB OMEGA_GPU_HANDLE_SLOTS 59 #define VA_BASE 0x100000000ull 60 #define VA_STRIDE (1ull << 31) /* one slot per 2 GiB of VA: the largest buffer here (256 MiB + slack) never overlaps the next */ 61 #define OUT_BYTES ((uint64_t)OMEGA_GPU_DATTN_Q_HEADS * 128u * 4u) 62 static struct { 63 OmegaGpuHandleTable t; 64 void *mem[NB]; 65 OmegaGpuBufferDesc desc[NB]; 66 OmegaGpuDeviceHandle dev; 67 OmegaGpuQueueHandle q; 68 OmegaGpuBufferHandle ring; 69 uint64_t ns; 70 unsigned locks_held, double_locks, bad_unlocks, submits, allocs; 71 uint64_t data_bytes[64]; /* sizes of the DATA buffers, in allocation order */ 72 unsigned n_data; 73 } fk; 74 75 static uint64_t f_now_ns(void *c) { (void)c; fk.ns += 1000u; return fk.ns; } 76 static uint64_t f_now_ms(void *c) { return f_now_ns(c) / 1000000u; } 77 static int f_wait(void *c, uint64_t ms) { (void)c; fk.ns += ms * 1000000u; return 0; } 78 static int f_lock(void *c, uint32_t id) { 79 (void)c; 80 if (id != OMEGA_GPU_SESSION_LOCK_ID) return OMEGA_GPU_PLAT_REFUSED_ARGS; 81 if (fk.locks_held) { fk.double_locks++; return OMEGA_GPU_PLAT_REFUSED_BUSY; } 82 fk.locks_held = 1; 83 return 0; 84 } 85 static int f_unlock(void *c, uint32_t id) { 86 (void)c; 87 if (id != OMEGA_GPU_SESSION_LOCK_ID || !fk.locks_held) { fk.bad_unlocks++; return OMEGA_GPU_PLAT_REFUSED_ARGS; } 88 fk.locks_held = 0; 89 return 0; 90 } 91 static const char *f_diag(void *c) { (void)c; return ""; } 92 static int f_dev_open(void *c, uint32_t ord, OmegaGpuDeviceHandle *out) { 93 (void)c; 94 if (ord != 0 || fk.dev.generation) return OMEGA_GPU_PLAT_REFUSED_LIMIT; 95 int rc = omega_gpu_handles_open(&fk.t, OMEGA_GPU_HANDLE_DEVICE, 0, 1, 0, &out->index, &out->generation); 96 if (rc == 0) fk.dev = *out; 97 return rc; 98 } 99 static int f_dev_close(void *c, OmegaGpuDeviceHandle d) { 100 (void)c; 101 int rc = omega_gpu_handles_check(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); 102 if (rc) return rc; 103 (void)omega_gpu_handles_close(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); 104 memset(&fk.dev, 0, sizeof fk.dev); 105 return 0; 106 } 107 static OmegaGpuPlatformCaps g_caps = { 108 .abi_major = OMEGA_GPU_PLATFORM_ABI_MAJOR, .abi_minor = OMEGA_GPU_PLATFORM_ABI_MINOR, 109 .services = OMEGA_GPU_SVC_REQUIRED_SESSION, .max_devices = 1, .max_buffers = 1000, .max_queues = 1, .max_inflight = 1, 110 .max_buffer_bytes = 1ull << 30, .max_total_bytes = 1ull << 32, 111 }; 112 static int f_alloc(void *c, OmegaGpuDeviceHandle d, const OmegaGpuBufferDesc *desc, OmegaGpuBufferHandle *o, 113 OmegaCpuView *cpu, OmegaGpuVa *g, OmegaGpuBusAddr *b) { 114 (void)c; 115 int rc = omega_gpu_handles_check(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); 116 if (!rc) rc = omega_gpu_platform_check_buffer(desc, &g_caps); 117 if (!rc) rc = omega_gpu_handles_open(&fk.t, desc->kind, desc->bytes, 1000, 1ull << 32, &o->index, &o->generation); 118 if (rc) return rc; 119 void *m = calloc(1, desc->bytes); /* exact size: ASan sees any write past the end */ 120 if (!m) { (void)omega_gpu_handles_close(&fk.t, o->index, o->generation, desc->kind); return OMEGA_GPU_PLAT_REFUSED_LIMIT; } 121 fk.mem[o->index] = m; fk.desc[o->index] = *desc; 122 if (desc->kind == OMEGA_GPU_HANDLE_BUF_DATA && fk.n_data < 64) fk.data_bytes[fk.n_data++] = desc->bytes; 123 fk.allocs++; 124 cpu->ptr = m; cpu->len = desc->bytes; g->va = VA_BASE + (uint64_t)o->index * VA_STRIDE; b->iova = 0; 125 return 0; 126 } 127 static int f_free(void *c, OmegaGpuBufferHandle h) { 128 (void)c; 129 if (h.index >= NB) return OMEGA_GPU_PLAT_REFUSED_BAD_HANDLE; 130 int rc = omega_gpu_handles_close(&fk.t, h.index, h.generation, fk.t.kind[h.index]); 131 if (rc) return rc; 132 free(fk.mem[h.index]); fk.mem[h.index] = NULL; 133 return 0; 134 } 135 static int buf_live(OmegaGpuBufferHandle h) { return h.index < NB && fk.t.live[h.index] && fk.t.generation[h.index] == h.generation; } 136 /* the host view of [va, va + len): NULL unless the whole range lies inside one live buffer */ 137 static void *va_to_cpu(uint64_t va, uint64_t len) { 138 for (uint32_t i = 0; i < NB; i++) { 139 if (!fk.mem[i] || !fk.t.live[i]) continue; 140 const uint64_t base = VA_BASE + (uint64_t)i * VA_STRIDE; 141 if (va >= base && va - base <= fk.desc[i].bytes && len <= fk.desc[i].bytes - (va - base)) return (uint8_t *)fk.mem[i] + (va - base); 142 } 143 return NULL; 144 } 145 static int sync_check(OmegaGpuBufferHandle h, uint64_t off, uint64_t len) { 146 if (!buf_live(h)) return OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE; 147 if (len == 0 || off > fk.desc[h.index].bytes || len > fk.desc[h.index].bytes - off) return OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH; 148 return 0; 149 } 150 static int f_c2g(void *c, OmegaGpuBufferHandle h, uint64_t off, uint64_t len) { (void)c; return sync_check(h, off, len); } 151 /* A read-back of the split-counter / intermediate-semaphore words finds them DONE, as the device leaves them. */ 152 static int f_g2c(void *c, OmegaGpuBufferHandle h, uint64_t off, uint64_t len) { 153 (void)c; 154 int rc = sync_check(h, off, len); 155 if (rc) return rc; 156 uint32_t *w = (uint32_t *)((uint8_t *)fk.mem[h.index] + off); 157 if (len == 4u) { /* the launch's QMD release semaphore: the kernel finished */ 158 w[0] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE; 159 } else if (len % 16u == 0) { /* a row of four-word semaphores */ 160 uint64_t i = 0, n = len / 16u; 161 while (i < n && w[4 * i] == OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT) i++; 162 if (i == n) for (i = 0; i < n; i++) w[4 * i] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE; 163 } 164 return 0; 165 } 166 static int f_poll(void *c, OmegaGpuBufferHandle h, uint64_t off, uint32_t want, uint64_t ms, uint32_t *seen) { 167 (void)c; 168 if (!buf_live(h)) return OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE; 169 if (!(fk.desc[h.index].rights & OMEGA_GPU_RIGHT_HOST_POLLED)) return OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS; 170 const volatile uint32_t *w = (const volatile uint32_t *)((uint8_t *)fk.mem[h.index] + off); 171 *seen = *w; 172 if ((int32_t)(*w - want) >= 0) return 0; 173 fk.ns += ms * 1000000u; 174 return OMEGA_GPU_POLL_TIMED_OUT; 175 } 176 static int f_qopen(void *c, OmegaGpuDeviceHandle d, OmegaGpuBufferHandle ring, OmegaGpuQueueHandle *o) { 177 (void)c; 178 int rc = omega_gpu_handles_check(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); 179 if (!rc && (!buf_live(ring) || fk.t.kind[ring.index] != OMEGA_GPU_HANDLE_BUF_QUEUE)) rc = OMEGA_GPU_PLAT_REFUSED_BAD_KIND; 180 if (!rc) rc = omega_gpu_handles_open(&fk.t, OMEGA_GPU_HANDLE_QUEUE, 0, 1, 0, &o->index, &o->generation); 181 if (!rc) { fk.q = *o; fk.ring = ring; } 182 return rc; 183 } 184 static int f_qclose(void *c, OmegaGpuQueueHandle q) { 185 (void)c; 186 int rc = omega_gpu_handles_close(&fk.t, q.index, q.generation, OMEGA_GPU_HANDLE_QUEUE); 187 if (!rc) memset(&fk.q, 0, sizeof fk.q); 188 return rc; 189 } 190 static uint32_t mthd(uint32_t subc, uint32_t m, uint32_t count) { return (2u << 28) | (count << 16) | (subc << 13) | (m >> 2); } 191 /* The fake device runs the stream: completion-marker releases (0x005c) write their payload, and the attention output 192 * (the DATA buffer of 32 * 128 f32) is overwritten with zeros, the "kernel ran" the API's poison check looks for. */ 193 static int f_submit(void *c, OmegaGpuQueueHandle q, const uint32_t *w, uint32_t n) { 194 (void)c; 195 int rc = omega_gpu_handles_check(&fk.t, q.index, q.generation, OMEGA_GPU_HANDLE_QUEUE); 196 if (rc) return rc; 197 if ((uint64_t)n * 4u > fk.desc[fk.ring.index].bytes) return OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH; 198 fk.submits++; 199 for (uint32_t i = 0; i < NB; i++) 200 if (fk.mem[i] && fk.t.live[i] && fk.desc[i].kind == OMEGA_GPU_HANDLE_BUF_DATA && fk.desc[i].bytes == OUT_BYTES) memset(fk.mem[i], 0, OUT_BYTES); 201 for (uint32_t i = 0; i + 5 < n; i++) 202 if (w[i] == mthd(0, 0x005c, 5)) { 203 uint32_t *m = va_to_cpu((uint64_t)w[i + 1] | ((uint64_t)w[i + 2] << 32), 4); 204 if (m) *m = w[i + 3]; 205 i += 5; 206 } 207 return 0; 208 } 209 static int f_retire(void *c, OmegaGpuQueueHandle q) { (void)c; (void)q; return 0; } 210 static void *f_halloc(void *c, size_t b) { (void)c; return malloc(b); } 211 static void f_hfree(void *c, void *p) { (void)c; free(p); } 212 213 static OmegaGpuPlatform g_plat = { 214 .ctx = &fk, .time_now_ms = f_now_ms, .wait_ms = f_wait, .lock = f_lock, .unlock = f_unlock, 215 .dma_alloc = f_alloc, .dma_free = f_free, .sync_cpu_to_gpu = f_c2g, .sync_gpu_to_cpu = f_g2c, .poll_word = f_poll, 216 .device_open = f_dev_open, .device_close = f_dev_close, .diag_text = f_diag, 217 .queue_open = f_qopen, .queue_close = f_qclose, .queue_submit = f_submit, .queue_retire = f_retire, 218 .time_now_ns = f_now_ns, .host_alloc = f_halloc, .host_free = f_hfree, 219 }; 220 static int install(void) { 221 memset(&fk, 0, sizeof fk); omega_gpu_handles_init(&fk.t); 222 return omega_gpu_session_set_platform(&g_plat, &g_caps); 223 } 224 #define EXPECT_LOCK_FREE() do { EXPECT(fk.locks_held == 0); EXPECT(fk.double_locks == 0); EXPECT(fk.bad_unlocks == 0); } while (0) 225 226 /* the session rounds every buffer up to a 4 KiB page */ 227 static int has_data(uint64_t bytes) { 228 bytes = (bytes + 0xfffu) & ~0xfffull; 229 for (unsigned i = 0; i < fk.n_data; i++) if (fk.data_bytes[i] == bytes) return 1; 230 return 0; 231 } 232 static OmegaGpuDecodeAttnCache *alloc_at(uint32_t max_pos) { 233 OmegaGpuDecodeAttnCache *c = NULL; 234 if (install() != OMEGA_GPU_PLAT_OK) { EXPECT(!"fake platform installs"); return NULL; } /* never fall back to the real driver */ 235 int rc = omega_gpu_decode_attn_cache_alloc(1, max_pos, THETA, &c); 236 if (rc != OMEGA_GPU_DATTN_OK) printf(" cache_alloc(1, %u): %s (%s)\n", max_pos, omega_gpu_decode_attn_rc_name(rc), omega_gpu_decode_attn_last_error()); 237 EXPECT(rc == OMEGA_GPU_DATTN_OK && c != NULL); 238 return rc == OMEGA_GPU_DATTN_OK ? c : NULL; 239 } 240 241 /* ---- cases ---- */ 242 243 static void c_alloc_cap(void) { 244 EXPECT(CAP == 131072u); 245 OmegaGpuDecodeAttnCache *c = alloc_at(CAP); 246 if (!c) return; 247 const uint64_t plane = 4ull * CAP * OMEGA_DATTN_ROW_BYTES + OMEGA_DATTN_PLANE_SLACK; 248 EXPECT(has_data(2 * plane)); /* K | V planes */ 249 EXPECT(has_data((uint64_t)CAP * OMEGA_DATTN_ROPE_ROW_BYTES)); /* RoPE table */ 250 EXPECT(has_data(4ull * (CAP / OMEGA_DATTN_CHUNK) * OMEGA_DATTN_PARTIAL_WORDS * 4u)); /* split partials, 512 splits */ 251 OmegaGpuDecodeAttnCacheView v; 252 EXPECT(omega_gpu_decode_attn_cache_view(c, 0, &v) == OMEGA_GPU_DATTN_OK && v.max_pos == CAP && v.v_va - v.k_va == plane); 253 omega_gpu_decode_attn_cache_free(c); 254 EXPECT_LOCK_FREE(); 255 const unsigned allocs = fk.allocs; 256 c = (OmegaGpuDecodeAttnCache *)1; 257 EXPECT(omega_gpu_decode_attn_cache_alloc(1, CAP + OMEGA_DATTN_CHUNK, THETA, &c) == OMEGA_GPU_DATTN_BAD_ARGS && c == NULL); 258 EXPECT(fk.allocs == allocs); /* refused before any device work */ 259 EXPECT_LOCK_FREE(); 260 } 261 262 static void c_kv_rows(void) { 263 OmegaGpuDecodeAttnCache *c = alloc_at(CAP); 264 if (!c) return; 265 static const uint32_t ok_pos[] = { 0, 65535, 65536, 131071 }; 266 uint16_t k[128], v[128], kr[128], vr[128]; 267 for (unsigned n = 0; n < sizeof ok_pos / sizeof ok_pos[0]; n++) 268 for (uint32_t h = 0; h < OMEGA_GPU_DATTN_KV_HEADS; h++) { 269 for (unsigned i = 0; i < 128; i++) { k[i] = (uint16_t)(ok_pos[n] * 7u + h * 131u + i); v[i] = (uint16_t)~k[i]; } 270 EXPECT(omega_gpu_decode_attn_cache_write(c, 0, h, ok_pos[n], 1, k, v) == OMEGA_GPU_DATTN_OK); 271 EXPECT(omega_gpu_decode_attn_cache_read(c, 0, h, ok_pos[n], kr, vr) == OMEGA_GPU_DATTN_OK); 272 EXPECT(memcmp(k, kr, sizeof k) == 0 && memcmp(v, vr, sizeof v) == 0); 273 } 274 /* the rows really are where the view says: (kv_head * max_pos + pos) * 256 from the plane base */ 275 OmegaGpuDecodeAttnCacheView view; 276 EXPECT(omega_gpu_decode_attn_cache_view(c, 0, &view) == OMEGA_GPU_DATTN_OK); 277 const uint16_t *last = va_to_cpu(view.v_va + ((uint64_t)3 * CAP + 131071u) * 256u, 256); 278 EXPECT(last && omega_gpu_decode_attn_cache_read(c, 0, 3, 131071, kr, vr) == OMEGA_GPU_DATTN_OK && memcmp(last, vr, 256) == 0); 279 EXPECT(omega_gpu_decode_attn_cache_write(c, 0, 0, 131072, 1, k, v) == OMEGA_GPU_DATTN_BAD_ARGS); 280 EXPECT(omega_gpu_decode_attn_cache_write(c, 0, 0, 131073, 1, k, v) == OMEGA_GPU_DATTN_BAD_ARGS); 281 EXPECT(omega_gpu_decode_attn_cache_read(c, 0, 0, 131072, kr, vr) == OMEGA_GPU_DATTN_BAD_ARGS); 282 EXPECT(omega_gpu_decode_attn_cache_read(c, 0, 0, 131073, kr, vr) == OMEGA_GPU_DATTN_BAD_ARGS); 283 uint16_t k2[256] = {0}, v2[256] = {0}; 284 EXPECT(omega_gpu_decode_attn_cache_write(c, 0, 3, 131071, 2, k2, v2) == OMEGA_GPU_DATTN_BAD_ARGS); /* crosses the cap */ 285 omega_gpu_decode_attn_cache_free(c); 286 EXPECT_LOCK_FREE(); 287 } 288 289 static void c_rope_rows(void) { 290 OmegaGpuDecodeAttnCache *c = alloc_at(CAP); 291 if (!c) return; 292 OmegaGpuDecodeAttnCacheView view; 293 EXPECT(omega_gpu_decode_attn_cache_view(c, 0, &view) == OMEGA_GPU_DATTN_OK); 294 EXPECT(va_to_cpu(view.rope_va, (uint64_t)CAP * OMEGA_DATTN_ROPE_ROW_BYTES) != NULL); /* C rows ... */ 295 EXPECT(va_to_cpu(view.rope_va, (uint64_t)CAP * OMEGA_DATTN_ROPE_ROW_BYTES + 1u) == NULL); /* ... and no more */ 296 static const uint32_t rows[] = { 0, 65535, 65536, 99999, 131071 }; 297 double worst = 0; 298 for (unsigned n = 0; n < sizeof rows / sizeof rows[0]; n++) { 299 const float *r = va_to_cpu(view.rope_va + (uint64_t)rows[n] * OMEGA_DATTN_ROPE_ROW_BYTES, OMEGA_DATTN_ROPE_ROW_BYTES); 300 if (!r) { EXPECT(!"rope row inside the table"); continue; } 301 for (unsigned i = 0; i < 64; i++) { 302 const double a = (double)rows[n] * pow(THETA, -(double)(2 * i) / 128.0); 303 const double dc = fabs((double)r[i] - cos(a)), ds = fabs((double)r[64 + i] - sin(a)); 304 if (dc > worst) worst = dc; 305 if (ds > worst) worst = ds; 306 } 307 } 308 printf(" rope rows 0..131071: worst |table - libm| %.3g\n", worst); 309 EXPECT(worst <= 1e-6); /* f32 rounding of an exact value is <= 6e-8; 1e-6 leaves room for the pow ulps at pos 131071 */ 310 omega_gpu_decode_attn_cache_free(c); 311 EXPECT_LOCK_FREE(); 312 } 313 314 static float g_qkv[OMEGA_GPU_DATTN_QKV_WORDS], g_out[OMEGA_GPU_DATTN_Q_HEADS * 128]; 315 static void c_decode_at_cap(void) { 316 OmegaGpuDecodeAttnCache *c = alloc_at(CAP); 317 if (!c) return; 318 OmegaGpuDecodeAttnInfo info; 319 int rc = omega_gpu_decode_attn(c, 0, 131071, g_qkv, 1e-6f, 1.0f, g_out, &info); 320 if (rc) printf(" decode_attn(pos 131071): %s (%s)\n", omega_gpu_decode_attn_rc_name(rc), omega_gpu_decode_attn_last_error()); 321 EXPECT(rc == OMEGA_GPU_DATTN_OK); 322 EXPECT(info.splits == CAP / OMEGA_DATTN_CHUNK && info.grid_x == 512u && info.grid_y == 4u); 323 EXPECT(info.kv_bytes == 2ull * 4u * CAP * 256u); 324 EXPECT_LOCK_FREE(); 325 const unsigned submits = fk.submits; 326 EXPECT(omega_gpu_decode_attn(c, 0, 131072, g_qkv, 1e-6f, 1.0f, g_out, &info) == OMEGA_GPU_DATTN_BAD_ARGS); 327 EXPECT(omega_gpu_decode_attn(c, 0, 131073, g_qkv, 1e-6f, 1.0f, g_out, &info) == OMEGA_GPU_DATTN_BAD_ARGS); 328 EXPECT(fk.submits == submits); /* refused before any submission */ 329 EXPECT_LOCK_FREE(); 330 omega_gpu_decode_attn_cache_free(c); 331 } 332 333 static void c_old_cap_cache(void) { 334 OmegaGpuDecodeAttnCache *c = alloc_at(65536u); 335 if (!c) return; 336 uint16_t k[128] = {0}, v[128] = {0}; 337 EXPECT(omega_gpu_decode_attn_cache_write(c, 0, 3, 65535, 1, k, v) == OMEGA_GPU_DATTN_OK); 338 EXPECT(omega_gpu_decode_attn_cache_write(c, 0, 3, 65536, 1, k, v) == OMEGA_GPU_DATTN_BAD_ARGS); 339 OmegaGpuDecodeAttnInfo info; 340 EXPECT(omega_gpu_decode_attn(c, 0, 65535, g_qkv, 1e-6f, 1.0f, g_out, &info) == OMEGA_GPU_DATTN_OK && info.splits == 256u); 341 EXPECT(omega_gpu_decode_attn(c, 0, 65536, g_qkv, 1e-6f, 1.0f, g_out, &info) == OMEGA_GPU_DATTN_BAD_ARGS); 342 omega_gpu_decode_attn_cache_free(c); 343 EXPECT_LOCK_FREE(); 344 } 345 346 static const struct { const char *name; void (*fn)(void); } CASES[] = { 347 { "cache of 131072 positions allocates, buffers sized for it; 131328 refused", c_alloc_cap }, 348 { "KV rows 65535/65536/131071 round-trip; 131072/131073 refused", c_kv_rows }, 349 { "RoPE rows up to 131071 match libm; table holds exactly 131072 rows", c_rope_rows }, 350 { "decode step at pos 131071 launches 512 splits; 131072/131073 refused", c_decode_at_cap }, 351 { "a 65536-position cache refuses pos 65536", c_old_cap_cache }, 352 }; 353 354 int main(int argc, char **argv) { 355 const unsigned n = sizeof CASES / sizeof CASES[0]; 356 if (argc == 2) { /* one case in this process (debugging): its index */ 357 const unsigned i = (unsigned)atoi(argv[1]); 358 if (i >= n) return 2; 359 case_ok = 1; CASES[i].fn(); 360 printf("%s %s\n", case_ok ? "ok " : "FAIL", CASES[i].name); 361 return case_ok ? 0 : 1; 362 } 363 unsigned failed = 0; 364 for (unsigned i = 0; i < n; i++) { 365 fflush(stdout); 366 pid_t pid = fork(); 367 if (pid == 0) { case_ok = 1; CASES[i].fn(); fflush(stdout); _exit(case_ok ? 0 : 1); } 368 int status = 0; 369 int ok = pid > 0 && waitpid(pid, &status, 0) == pid && WIFEXITED(status) && WEXITSTATUS(status) == 0; 370 printf("%s %s\n", ok ? "ok " : "FAIL", CASES[i].name); 371 if (!ok) failed++; 372 } 373 printf("test_omega_gpu_decode_attn_maxpos: %u of %u cases passed: %s\n", n - failed, n, failed ? "FAIL" : "PASS"); 374 return failed ? 1 : 0; 375 }