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
CUDApp / tests / gpu_serving_alloc_count_test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* |
| 3 | * gpu_serving_alloc_count_test: MEASUREMENT of driver (RM) allocations and frees made by the real |
| 4 | * native GPU stack (session + matmul API + attention API) while it "serves" a Qwen3-4B-shaped |
| 5 | * sequence, on the simulated driver of tests/fake_m16_native.c. Host only, opens no device. |
| 6 | * |
| 7 | * Why: sovereign-core design note docs/design/gb10-weight-ownership.md (sc#277, sc PR #292) argues |
| 8 | * that omega still asks the driver for memory while serving, and that one refused request faults |
| 9 | * the GPU session for the rest of the process. This test counts those requests. |
| 10 | * |
| 11 | * What it asserts: only bookkeeping that is true today (the session opens once, the fake saw every |
| 12 | * request, no double free, nothing latched, the call mix is what the report says it is). It does |
| 13 | * NOT assert the allocation counts: they are printed as MEASURE lines. A later change that reserves |
| 14 | * serving memory up front would lower them and this test would still pass; the numbers in the PR |
| 15 | * that made it are the record of today's behaviour. |
| 16 | * |
| 17 | * What it does not model: the kernels. The fake runs in FK_KERNEL_SKIP mode, so launches are |
| 18 | * counted and complete, but no matmul or attention result is computed. The matmul API therefore |
| 19 | * reports its own poison check (CHIP_FAIL) and the attention API reports UNWRITTEN; those return |
| 20 | * codes are expected here and are not errors of the allocation path. Elementwise kernels (rmsnorm, |
| 21 | * rope, swiglu) are not exercised; the design note's audit says they stop allocating after the |
| 22 | * first full-size call. |
| 23 | * |
| 24 | * Shapes (Qwen3-4B): hidden 2560, 32 q heads, 8 kv heads, head_dim 128, intermediate 9728, |
| 25 | * vocab 151936, 36 layers. One resident tensor per distinct weight shape stands in for the 36 |
| 26 | * layers (resident-weight allocations are a load-time cost, counted in phase LOAD only). |
| 27 | * Call pattern per layer follows crates/aien-inference-abi/src/transformer_backend.rs: |
| 28 | * decode (m = 1): q, k, v, attention(seq), o, gate, up, down ; after the layers lm_head (m = 1) |
| 29 | * prefill chunk: the same with m = chunk rows (<= 128), attention once per token at seq = |
| 30 | * offset + t + 1, lm_head with m = 1 after the chunk |
| 31 | * CTA budget 256 (the daemon value in the audit). |
| 32 | */ |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | |
| 37 | #include "fake_m16_native.h" |
| 38 | #include "nvrm.h" /* the session reaches the fake driver through the Linux reference platform (P07 cut C) */ |
| 39 | #include "omega_blackwell_codegen.h" /* omega_matmul_frag_block_shape: the kernel cache key */ |
| 40 | #include "omega_blackwell_matmul.h" /* omega_fp32_to_bf16 (update mode) */ |
| 41 | #include "omega_gpu_attention_api.h" |
| 42 | #include "omega_gpu_matmul_api.h" |
| 43 | #include "omega_gpu_serving.h" |
| 44 | #include "omega_gpu_session.h" |
| 45 | |
| 46 | /* The two driver symbols the session needs that the fake (written for the vector engine) lacks. */ |
| 47 | int nvrm_open(Nvrm *rm) { (void)rm; fk.device_open = 1; return 0; } |
| 48 | int nvrm_close(Nvrm *rm) { (void)rm; return 0; } |
| 49 | void nvrm_retire(Nvrm *rm, uint32_t upto) { (void)rm; (void)upto; } |
| 50 | |
| 51 | #define HID 2560u |
| 52 | #define QDIM 4096u |
| 53 | #define KVDIM 1024u |
| 54 | #define INTER 9728u |
| 55 | #define VOCAB 151936u |
| 56 | #define LAYERS 36u |
| 57 | #define QH 32u |
| 58 | #define KVH 8u |
| 59 | #define HD 128u |
| 60 | #define CHUNK 128u |
| 61 | |
| 62 | static int g_fail; |
| 63 | #define CHECK(c, ...) do { if (!(c)) { g_fail++; printf("FAIL %s:%d: ", __FILE__, __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0) |
| 64 | |
| 65 | /* ---- the weights: one tensor per distinct shape ---- */ |
| 66 | static OmegaGpuTensor *W_Q, *W_K, *W_O, *W_G, *W_D, *W_LM; |
| 67 | |
| 68 | static OmegaGpuTensor *upload(uint32_t k, uint32_t n) { |
| 69 | uint16_t *z = calloc((size_t)k * n, sizeof *z); |
| 70 | OmegaGpuTensor *t = NULL; |
| 71 | CHECK(z != NULL, "calloc %ux%u", k, n); |
| 72 | int rc = z ? omega_gpu_tensor_upload_bf16(k, n, z, &t) : -1; |
| 73 | CHECK(rc == OMEGA_GPU_MATMUL_OK, "upload %ux%u rc=%d %s", k, n, rc, omega_gpu_matmul_last_error()); |
| 74 | free(z); |
| 75 | return t; |
| 76 | } |
| 77 | |
| 78 | /* ---- per-phase counters ---- */ |
| 79 | typedef struct { |
| 80 | const char *name; |
| 81 | int allocs, frees; |
| 82 | uint64_t alloc_bytes, free_bytes; |
| 83 | int mm_calls, mm_miss, mm_remiss, mm_other_allocs; /* code-cache misses; misses on an already-seen shape; scratch growth */ |
| 84 | int at_calls, at_allocs; |
| 85 | int a0, f0; /* log positions at phase start */ |
| 86 | } Phase; |
| 87 | |
| 88 | /* The matmul API's kernel cache key: (kp, np, grid_x, rt, ct). rt and ct follow from the call's padded shape |
| 89 | * (omega_matmul_frag_block_shape, the rule the API's launch_shape uses); since the blocked prefill kernel (#407) |
| 90 | * two calls with the same grid_x can need different kernels. */ |
| 91 | typedef struct { uint32_t kp, np, gx, rt, ct; } Key; |
| 92 | static Key g_seen[128]; |
| 93 | static int g_nseen; |
| 94 | static int seen(const OmegaGpuMatmulInfo *in) { |
| 95 | Key k = { in->padded_k, in->padded_n, in->grid_x, 0, 0 }; |
| 96 | omega_matmul_frag_block_shape(in->padded_m / 16u, in->padded_n / 8u, &k.rt, &k.ct); |
| 97 | for (int i = 0; i < g_nseen; i++) |
| 98 | if (g_seen[i].kp == k.kp && g_seen[i].np == k.np && g_seen[i].gx == k.gx && g_seen[i].rt == k.rt && g_seen[i].ct == k.ct) return 1; |
| 99 | if (g_nseen < 128) g_seen[g_nseen++] = k; |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static Phase *P; /* current phase */ |
| 104 | static int g_rc_mm_ok, g_rc_mm_chipfail, g_rc_at_ok, g_rc_at_unwritten, g_rc_bad; |
| 105 | |
| 106 | static float *g_x, *g_y; /* activation in / result out, sized for CHUNK x INTER and CHUNK x VOCAB-free */ |
| 107 | static float *g_logits; /* 1 x VOCAB */ |
| 108 | static float *g_k, *g_v; /* host KV for the attention call, 4096 x 1024 f32 each */ |
| 109 | static float g_q[QDIM], g_attn[QDIM]; |
| 110 | |
| 111 | static void mm(uint32_t m, const OmegaGpuTensor *w, float *out) { |
| 112 | uint32_t k, n; |
| 113 | omega_gpu_tensor_shape(w, &k, &n); |
| 114 | int a_before = fk.alloc_calls; |
| 115 | OmegaGpuMatmulInfo info; |
| 116 | int rc = omega_gpu_matmul_resident_f32(m, g_x, w, out, &info); |
| 117 | int made = fk.alloc_calls - a_before; |
| 118 | P->mm_calls++; |
| 119 | if (rc == OMEGA_GPU_MATMUL_OK) g_rc_mm_ok++; |
| 120 | else if (rc == OMEGA_GPU_MATMUL_CHIP_FAIL) g_rc_mm_chipfail++; |
| 121 | else { g_rc_bad++; printf("unexpected matmul rc=%d (%s)\n", rc, omega_gpu_matmul_rc_name(rc)); } |
| 122 | int miss = info.kernel_cache_hit ? 0 : 1; /* at most one miss per launch; m <= 128 rows is one launch here */ |
| 123 | if (miss) { |
| 124 | P->mm_miss++; |
| 125 | if (seen(&info)) P->mm_remiss++; |
| 126 | } else { |
| 127 | (void)seen(&info); |
| 128 | } |
| 129 | P->mm_other_allocs += made - miss; |
| 130 | } |
| 131 | |
| 132 | static void attn(uint32_t seq) { |
| 133 | int a_before = fk.alloc_calls; |
| 134 | OmegaGpuAttnInfo info; |
| 135 | int rc = omega_gpu_gqa_attention_f32(g_q, g_k, g_v, seq, QH, KVH, HD, g_attn, &info); |
| 136 | P->at_calls++; |
| 137 | P->at_allocs += fk.alloc_calls - a_before; |
| 138 | if (rc == OMEGA_GPU_ATTN_OK) g_rc_at_ok++; |
| 139 | else if (rc == OMEGA_GPU_ATTN_UNWRITTEN) g_rc_at_unwritten++; |
| 140 | else { g_rc_bad++; printf("unexpected attention rc=%d (%s)\n", rc, omega_gpu_attention_rc_name(rc)); } |
| 141 | } |
| 142 | |
| 143 | /* One transformer step over `rows` rows of a sequence that already holds `offset` tokens. */ |
| 144 | static void step(uint32_t rows, uint32_t offset) { |
| 145 | for (uint32_t l = 0; l < LAYERS; l++) { |
| 146 | mm(rows, W_Q, g_y); mm(rows, W_K, g_y); mm(rows, W_K, g_y); |
| 147 | for (uint32_t t = 0; t < rows; t++) attn(offset + t + 1); |
| 148 | mm(rows, W_O, g_y); mm(rows, W_G, g_y); mm(rows, W_G, g_y); mm(rows, W_D, g_y); |
| 149 | } |
| 150 | mm(1, W_LM, g_logits); |
| 151 | } |
| 152 | |
| 153 | static void begin(Phase *p, const char *name) { |
| 154 | memset(p, 0, sizeof *p); |
| 155 | p->name = name; |
| 156 | p->a0 = fk.alloc_size_n; |
| 157 | p->f0 = fk.free_size_n; |
| 158 | p->allocs = fk.alloc_calls; /* start values, turned into deltas by end() */ |
| 159 | p->frees = fk.free_calls; |
| 160 | P = p; |
| 161 | } |
| 162 | |
| 163 | static void sizes(const char *kind, const uint64_t *log, int from, int to) { |
| 164 | /* distinct sizes with counts, in order of first appearance, at most 6 shown */ |
| 165 | uint64_t seen_sz[256]; int cnt[256]; int ns = 0; |
| 166 | for (int i = from; i < to && i < FK_SIZE_LOG; i++) { |
| 167 | int j; |
| 168 | for (j = 0; j < ns; j++) if (seen_sz[j] == log[i]) break; |
| 169 | if (j == ns && ns < 256) { seen_sz[ns] = log[i]; cnt[ns] = 0; ns++; } |
| 170 | if (j < 256) cnt[j]++; |
| 171 | } |
| 172 | printf("MEASURE %s sizes (%d distinct):", kind, ns); |
| 173 | for (int j = 0; j < ns && j < 6; j++) printf(" %llux%d", (unsigned long long)seen_sz[j], cnt[j]); |
| 174 | if (ns > 6) printf(" ... last %llux%d", (unsigned long long)seen_sz[ns - 1], cnt[ns - 1]); |
| 175 | printf("\n"); |
| 176 | } |
| 177 | |
| 178 | static void end(Phase *p, int sized) { |
| 179 | int a1 = fk.alloc_size_n, f1 = fk.free_size_n; |
| 180 | p->allocs = fk.alloc_calls - p->allocs; |
| 181 | p->frees = fk.free_calls - p->frees; |
| 182 | for (int i = p->a0; i < a1 && i < FK_SIZE_LOG; i++) p->alloc_bytes += fk.alloc_size_log[i]; |
| 183 | for (int i = p->f0; i < f1 && i < FK_SIZE_LOG; i++) p->free_bytes += fk.free_size_log[i]; |
| 184 | printf("MEASURE phase %-22s allocs %4d frees %4d alloc_bytes %12llu matmul_calls %5d (code-cache misses %d, of which on a shape seen before %d, other scratch growth %d) attention_calls %5d (allocs %d)\n", |
| 185 | p->name, p->allocs, p->frees, (unsigned long long)p->alloc_bytes, p->mm_calls, p->mm_miss, p->mm_remiss, p->mm_other_allocs, |
| 186 | p->at_calls, p->at_allocs); |
| 187 | if (sized && p->allocs) { sizes("alloc", fk.alloc_size_log, p->a0, a1); sizes("free ", fk.free_size_log, p->f0, f1); } |
| 188 | CHECK(a1 <= FK_SIZE_LOG && f1 <= FK_SIZE_LOG, "size log overflow (%d allocs, %d frees)", a1, f1); |
| 189 | } |
| 190 | |
| 191 | static const OmegaGpuServingBounds QWEN3_4B_BOUNDS = { |
| 192 | .max_context = 4096, .max_seqs = 1, .num_q_heads = QH, .num_kv_heads = KVH, .head_dim = HD, .kv_block_size = 16, |
| 193 | .max_rows = CHUNK, .max_k = INTER, .max_n = INTER, .max_n_one_row = VOCAB, .kernel_slots = 32, |
| 194 | }; |
| 195 | |
| 196 | /* Bounds mode: a tiny reservation, then calls inside it (no driver traffic) and past it (refused loudly). */ |
| 197 | static int paged_call(uint32_t bs, uint32_t ctx); |
| 198 | static int attn_ran(int rc); |
| 199 | |
| 200 | static int bounds_mode(void) { |
| 201 | OmegaGpuServingBounds b = QWEN3_4B_BOUNDS; |
| 202 | b.max_context = 256; b.max_rows = 32; b.max_k = HID; b.max_n = QDIM; b.max_n_one_row = 0; |
| 203 | Phase ph; begin(&ph, "BOUNDS"); /* sets P for mm()/attn() */ |
| 204 | W_Q = upload(HID, QDIM); |
| 205 | attn(1); mm(1, W_Q, g_y); /* warm kernels at small size */ |
| 206 | CHECK(omega_gpu_reserve_serving(&b) == 0, "reserve failed: %s", omega_gpu_matmul_last_error()); |
| 207 | mm(1, W_Q, g_y); mm(17, W_Q, g_y); mm(32, W_Q, g_y); attn(256); /* warm each shape once (reserving a deeper kernel cache restarts it empty) */ |
| 208 | const int a0 = fk.alloc_calls, f0 = fk.free_calls; |
| 209 | mm(1, W_Q, g_y); mm(17, W_Q, g_y); mm(32, W_Q, g_y); attn(1); attn(100); attn(256); |
| 210 | CHECK(fk.alloc_calls == a0 && fk.free_calls == f0, "calls inside the bounds touched the driver (%d allocs, %d frees)", fk.alloc_calls - a0, fk.free_calls - f0); |
| 211 | OmegaGpuMatmulInfo info; |
| 212 | int rc = omega_gpu_matmul_resident_f32(33, g_x, W_Q, g_y, &info); /* 33 rows -> 48 padded: past 32 */ |
| 213 | CHECK(rc == OMEGA_GPU_MATMUL_TOO_LARGE, "33 rows past a 32-row reservation: rc=%d (%s)", rc, omega_gpu_matmul_rc_name(rc)); |
| 214 | CHECK(strstr(omega_gpu_matmul_last_error(), "serving reservation exceeded") != NULL, "matmul error text: \"%s\"", omega_gpu_matmul_last_error()); |
| 215 | rc = omega_gpu_gqa_attention_f32(g_q, g_k, g_v, 257, QH, KVH, HD, g_attn, NULL); |
| 216 | CHECK(rc == OMEGA_GPU_ATTN_TOO_LARGE, "context 257 past a 256 reservation: rc=%d (%s)", rc, omega_gpu_attention_rc_name(rc)); |
| 217 | CHECK(strstr(omega_gpu_attention_last_error(), "serving reservation exceeded") != NULL, "attention error text: \"%s\"", omega_gpu_attention_last_error()); |
| 218 | CHECK(fk.alloc_calls == a0 && fk.free_calls == f0, "a refused call touched the driver (%d allocs, %d frees)", fk.alloc_calls - a0, fk.free_calls - f0); |
| 219 | CHECK(!omega_gpu_matmul_is_blocked() && !omega_gpu_session_is_blocked(), "a refusal latched the session"); |
| 220 | mm(32, W_Q, g_y); attn(256); /* still serves after a refusal */ |
| 221 | CHECK(fk.alloc_calls == a0, "serving after a refusal allocated"); |
| 222 | /* paged bf16 attention (the sovereign-core path): 512 tokens = 32 blocks of 16 = exactly the reserved bytes */ |
| 223 | /* build each paged kernel the checked calls use: the split count is part of the kernel (cut F/J, own_log2split), |
| 224 | * 16 tokens run split 0 and 512 tokens split 2, and the attention kernel cache is outside the reservation |
| 225 | * (omega_gpu_serving.h, "Not covered") */ |
| 226 | (void)paged_call(16, 16); (void)paged_call(16, 512); |
| 227 | const int p0 = fk.alloc_calls, pf0 = fk.free_calls; |
| 228 | rc = paged_call(16, 16); CHECK(attn_ran(rc), "paged 16 tokens in bounds: rc=%d (%s)", rc, omega_gpu_attention_last_error()); |
| 229 | rc = paged_call(16, 512); CHECK(attn_ran(rc), "paged 512 tokens (exactly the bound) refused: rc=%d (%s)", rc, omega_gpu_attention_last_error()); |
| 230 | CHECK(fk.alloc_calls == p0 && fk.free_calls == pf0, "in-bounds paged calls touched the driver (%d allocs, %d frees)", fk.alloc_calls - p0, fk.free_calls - pf0); |
| 231 | rc = paged_call(16, 513); CHECK(rc == OMEGA_GPU_ATTN_TOO_LARGE, "paged 513 tokens (bound+1): rc=%d", rc); |
| 232 | CHECK(strstr(omega_gpu_attention_last_error(), "serving reservation exceeded") != NULL, "paged error text: \"%s\"", omega_gpu_attention_last_error()); |
| 233 | CHECK(fk.alloc_calls == p0 && fk.free_calls == pf0, "a refused paged call touched the driver"); |
| 234 | omega_gpu_serving_release(); /* lifted: growth is allowed again */ |
| 235 | rc = omega_gpu_gqa_attention_f32(g_q, g_k, g_v, 257, QH, KVH, HD, g_attn, NULL); |
| 236 | CHECK(rc == OMEGA_GPU_ATTN_OK || rc == OMEGA_GPU_ATTN_UNWRITTEN, "after release, context 257 rc=%d", rc); |
| 237 | CHECK(fk.alloc_calls > a0, "after release, growth did not allocate"); |
| 238 | printf("MEASURE bounds: refused calls made 0 driver allocations; 33 rows -> TOO_LARGE, ctx 257 -> TOO_LARGE; error \"%s\"\n", "serving reservation exceeded: ..."); |
| 239 | omega_gpu_tensor_free(W_Q); |
| 240 | omega_gpu_device_close(); |
| 241 | CHECK(fk.live_at_close == 0, "leak: %d", fk.live_at_close); |
| 242 | printf("gpu_serving_alloc_count_test bounds: %s (%d failed checks)\n", g_fail ? "FAIL" : "PASS", g_fail); |
| 243 | return g_fail ? 1 : 0; |
| 244 | } |
| 245 | |
| 246 | /* Paged bf16 attention call over `ctx` tokens of a one-layer pool of `bs`-token blocks (the path sovereign-core uses). */ |
| 247 | static int paged_call(uint32_t bs, uint32_t ctx) { |
| 248 | const uint32_t nb = (ctx + bs - 1) / bs; |
| 249 | OmegaGpuKvLayout ly; |
| 250 | ly.head_stride_bytes = (uint64_t)HD * 2; ly.token_stride_bytes = ly.head_stride_bytes * KVH; |
| 251 | ly.kv_plane_stride_bytes = ly.token_stride_bytes * bs; ly.layer_stride_bytes = 2 * ly.kv_plane_stride_bytes; |
| 252 | ly.block_stride_bytes = ly.layer_stride_bytes; ly.pool_bytes = ly.block_stride_bytes * nb; |
| 253 | ly.num_blocks = nb; ly.num_layers = 1; ly.block_size = bs; |
| 254 | uint8_t *pool = calloc(1, (size_t)ly.pool_bytes); |
| 255 | uint32_t *ids = calloc(nb, sizeof *ids); |
| 256 | CHECK(pool && ids, "paged host buffers"); |
| 257 | int rc = OMEGA_GPU_ATTN_BAD_ARGS; |
| 258 | if (pool && ids) { |
| 259 | for (uint32_t i = 0; i < nb; i++) ids[i] = i; |
| 260 | rc = omega_gpu_paged_attention_bf16(g_q, pool, &ly, ids, nb, ctx, 0, QH, KVH, HD, g_attn, NULL); |
| 261 | } |
| 262 | free(pool); free(ids); |
| 263 | return rc; |
| 264 | } |
| 265 | static int attn_ran(int rc) { return rc == OMEGA_GPU_ATTN_OK || rc == OMEGA_GPU_ATTN_UNWRITTEN; } |
| 266 | |
| 267 | /* Under half a block of context: the paged path stages a whole block, more than the f32 formula (review item 1). */ |
| 268 | static int bounds_block_mode(void) { |
| 269 | OmegaGpuServingBounds b = QWEN3_4B_BOUNDS; |
| 270 | b.max_context = 16; b.kv_block_size = 64; b.max_rows = 16; b.max_k = HID; b.max_n = QDIM; b.max_n_one_row = 0; |
| 271 | Phase ph; begin(&ph, "BOUNDS-BLOCK"); |
| 272 | W_Q = upload(HID, QDIM); |
| 273 | CHECK(omega_gpu_reserve_serving(&b) == 0, "reserve failed: %s", omega_gpu_matmul_last_error()); |
| 274 | int rc = paged_call(64, 16); /* first paged call builds the kernel (one code buffer); no pre-reserve call, it would raise the pool high-water mark */ |
| 275 | CHECK(attn_ran(rc), "paged call of 16 tokens in one 64-token block refused: rc=%d (%s)", rc, omega_gpu_attention_last_error()); |
| 276 | const int a0 = fk.alloc_calls, f0 = fk.free_calls; |
| 277 | rc = paged_call(64, 64); |
| 278 | CHECK(attn_ran(rc), "paged call of exactly one block (the byte bound) refused: rc=%d (%s)", rc, omega_gpu_attention_last_error()); |
| 279 | CHECK(fk.alloc_calls == a0 && fk.free_calls == f0, "in-bounds paged calls touched the driver"); |
| 280 | rc = paged_call(64, 65); |
| 281 | CHECK(rc == OMEGA_GPU_ATTN_TOO_LARGE, "two blocks past a one-block reservation: rc=%d", rc); |
| 282 | CHECK(fk.alloc_calls == a0 && fk.free_calls == f0, "refused paged call touched the driver"); |
| 283 | printf("MEASURE bounds-block: 16 and 64 tokens (one 64-token block) served with 0 driver traffic; 65 tokens -> TOO_LARGE\n"); |
| 284 | omega_gpu_tensor_free(W_Q); |
| 285 | omega_gpu_device_close(); |
| 286 | CHECK(fk.live_at_close == 0, "leak: %d", fk.live_at_close); |
| 287 | printf("gpu_serving_alloc_count_test bounds-block: %s (%d failed checks)\n", g_fail ? "FAIL" : "PASS", g_fail); |
| 288 | return g_fail ? 1 : 0; |
| 289 | } |
| 290 | |
| 291 | /* A reserve that fails part way must leave nothing flagged (review item 2). Sweep the failing driver request over every |
| 292 | * allocation the reserve makes (pool, q, out, table, activation, result), on a freshly opened device each time; the |
| 293 | * control (no injected failure) must leave the reservation active. */ |
| 294 | static int rollback_mode(void) { |
| 295 | OmegaGpuServingBounds b = QWEN3_4B_BOUNDS; |
| 296 | b.max_context = 256; b.kv_block_size = 16; b.max_rows = 32; b.max_k = HID; b.max_n = QDIM; b.max_n_one_row = 0; b.kernel_slots = 0; |
| 297 | Phase ph; begin(&ph, "ROLLBACK"); |
| 298 | int failed_reserves = 0; |
| 299 | /* n = 100 first: the un-injected control, before the probes of the failing runs raise the buffers' high-water marks */ |
| 300 | const int sweep[] = { 100, 1, 2, 3, 4, 5, 6 }; |
| 301 | for (unsigned si = 0; si < sizeof sweep / sizeof sweep[0]; si++) { |
| 302 | const int n = sweep[si]; |
| 303 | W_Q = upload(HID, QDIM); /* opens the session (its own allocations come first) */ |
| 304 | fk.alloc_mode = FK_ALLOC_FAIL; |
| 305 | fk.alloc_fail_nth = fk.alloc_calls + n; |
| 306 | int rc = omega_gpu_reserve_serving(&b); |
| 307 | fk.alloc_fail_nth = 0; |
| 308 | /* probes that need more than the reservation: refused only if something is still flagged */ |
| 309 | /* each probe is larger than every earlier one, so a buffer that is still flagged is refused even though the high-water mark carries over */ |
| 310 | int ra = omega_gpu_gqa_attention_f32(g_q, g_k, g_v, si ? 300 + 500 * si : 257, QH, KVH, HD, g_attn, NULL); |
| 311 | int refused_attn = ra == OMEGA_GPU_ATTN_TOO_LARGE && strstr(omega_gpu_attention_last_error(), "serving reservation exceeded") != NULL; |
| 312 | OmegaGpuMatmulInfo info; |
| 313 | int rm = omega_gpu_matmul_resident_f32(si ? 32 + 16 * si : 48, g_x, W_Q, g_y, &info); |
| 314 | int refused_mm = rm == OMEGA_GPU_MATMUL_TOO_LARGE && strstr(omega_gpu_matmul_last_error(), "serving reservation exceeded") != NULL; |
| 315 | if (rc != 0) { |
| 316 | failed_reserves++; |
| 317 | CHECK(!refused_attn && !refused_mm, "failed reserve (injected fail on its request %d, rc=%d) left a reservation flagged: attention %d matmul %d", n, rc, refused_attn, refused_mm); |
| 318 | } else { |
| 319 | CHECK(refused_attn && refused_mm, "control (request %d never failed, reserve ok): the reservation should be active, attention %d matmul %d", n, refused_attn, refused_mm); |
| 320 | } |
| 321 | omega_gpu_tensor_free(W_Q); |
| 322 | omega_gpu_device_close(); |
| 323 | } |
| 324 | CHECK(failed_reserves >= 6, "only %d of the sweep's reserves failed (expected the 6 buffer requests)", failed_reserves); |
| 325 | CHECK(fk.live_at_close == 0, "leak: %d", fk.live_at_close); |
| 326 | printf("MEASURE rollback: %d injected failures during reserve, none left anything flagged; the un-injected control stayed reserved\n", failed_reserves); |
| 327 | printf("gpu_serving_alloc_count_test rollback: %s (%d failed checks)\n", g_fail ? "FAIL" : "PASS", g_fail); |
| 328 | return g_fail ? 1 : 0; |
| 329 | } |
| 330 | |
| 331 | /* mode: "plain" (default) = today's measurement, no reservation; "reserve" = reserve after warm-up and |
| 332 | * ASSERT zero driver traffic while serving; "reserve-skip" = the same asserts without the call (must FAIL: the red run); |
| 333 | * "bounds" = refusal behaviour. */ |
| 334 | /* ---- sweep: the sovereign-core daemon's bounds (sc#311, sc#277). Every row count 1..256 over every weight |
| 335 | * shape is a distinct (kp, np, grid_x) for some rows, because grid_x is baked into the kernel and depends on |
| 336 | * the row tiles. prepare = 1: prepare each (rows, shape) and seal, then serving must make no driver call and an |
| 337 | * unprepared shape is refused. prepare = 0 (control, kernel_slots 32 as before): the cache wraps and serving |
| 338 | * allocates, which this mode must report as a failure. */ |
| 339 | #define SWEEP_ROWS 256u |
| 340 | static int sweep_mode(int prepare) { |
| 341 | W_Q = upload(HID, QDIM); W_K = upload(HID, KVDIM); W_O = upload(QDIM, HID); |
| 342 | W_G = upload(HID, INTER); W_D = upload(INTER, HID); W_LM = upload(HID, VOCAB); |
| 343 | OmegaGpuTensor *odd = upload(HID, 512); /* a shape the server never prepares */ |
| 344 | OmegaGpuTensor *w[6] = {W_Q, W_K, W_O, W_G, W_D, W_LM}; |
| 345 | float *x = calloc((size_t)SWEEP_ROWS * INTER, sizeof *x), *y = calloc((size_t)SWEEP_ROWS * VOCAB, sizeof *y); |
| 346 | CHECK(x && y, "sweep buffers"); |
| 347 | if (g_fail) return 1; |
| 348 | OmegaGpuServingBounds b = QWEN3_4B_BOUNDS; |
| 349 | b.max_rows = SWEEP_ROWS; b.max_n = VOCAB; b.max_n_one_row = VOCAB; b.kernel_slots = prepare ? 128 : 32; |
| 350 | CHECK(omega_gpu_reserve_serving(&b) == 0, "reserve failed: %s", omega_gpu_matmul_last_error()); |
| 351 | int prepared = -1; /* code buffers the prepare pass made: one per distinct kernel */ |
| 352 | if (prepare) { |
| 353 | int a0 = fk.alloc_calls; |
| 354 | for (unsigned i = 0; i < 6; i++) { |
| 355 | uint32_t k, n; |
| 356 | omega_gpu_tensor_shape(w[i], &k, &n); |
| 357 | for (uint32_t m = 1; m <= SWEEP_ROWS; m++) { |
| 358 | int rc = omega_gpu_matmul_prepare(m, k, n); |
| 359 | CHECK(rc == OMEGA_GPU_MATMUL_OK, "prepare m=%u %ux%u rc=%d %s", m, k, n, rc, omega_gpu_matmul_last_error()); |
| 360 | } |
| 361 | } |
| 362 | prepared = fk.alloc_calls - a0; |
| 363 | printf("MEASURE sweep prepare: %d driver allocations (one code buffer per distinct kernel)\n", prepared); |
| 364 | omega_gpu_serving_seal(); |
| 365 | } |
| 366 | g_nseen = 0; |
| 367 | OmegaGpuAllocStats st0, st1; |
| 368 | omega_gpu_session_alloc_stats(&st0); |
| 369 | int a0 = fk.alloc_calls, too_large = 0, other = 0; |
| 370 | for (unsigned pass = 0; pass < 2; pass++) |
| 371 | for (uint32_t m = 1; m <= SWEEP_ROWS; m++) |
| 372 | for (unsigned i = 0; i < 6; i++) { |
| 373 | OmegaGpuMatmulInfo info; |
| 374 | int rc = omega_gpu_matmul_resident_f32(m, x, w[i], y, &info); |
| 375 | if (rc == OMEGA_GPU_MATMUL_TOO_LARGE) too_large++; |
| 376 | else if (rc != OMEGA_GPU_MATMUL_OK && rc != OMEGA_GPU_MATMUL_CHIP_FAIL) other++; /* CHIP_FAIL = fake kernel skips writes */ |
| 377 | if (rc != OMEGA_GPU_MATMUL_TOO_LARGE) (void)seen(&info); |
| 378 | } |
| 379 | int made = fk.alloc_calls - a0; |
| 380 | omega_gpu_session_alloc_stats(&st1); |
| 381 | CHECK((int)(st1.allocs - st0.allocs) == made, "session alloc stats %llu != driver %d", |
| 382 | (unsigned long long)(st1.allocs - st0.allocs), made); |
| 383 | printf("MEASURE sweep serving (rows 1..%u x 6 shapes, twice): %d driver allocations, %d distinct kernels, %d refused, %d other\n", |
| 384 | SWEEP_ROWS, made, g_nseen, too_large, other); |
| 385 | CHECK(made == 0, "serving made %d driver allocations", made); |
| 386 | CHECK(too_large == 0 && other == 0, "serving refused %d, other rc %d", too_large, other); |
| 387 | /* measured: 95 before the blocked prefill kernel (#407), 83 with it (keyed with rt, ct; lm_head stays |
| 388 | * on the single-tile kernel, its rt > 1 store offset does not fit the 24-bit immediate) */ |
| 389 | CHECK(g_nseen == 83, "distinct kernels %d, expected 83 for Qwen3-4B at CTA budget 256", g_nseen); |
| 390 | CHECK(prepared < 0 || prepared == g_nseen, "prepare built %d kernels, serving used %d distinct", prepared, g_nseen); |
| 391 | if (prepare) { |
| 392 | int a1 = fk.alloc_calls; |
| 393 | OmegaGpuMatmulInfo info; |
| 394 | int rc = omega_gpu_matmul_resident_f32(1, x, odd, y, &info); |
| 395 | CHECK(rc == OMEGA_GPU_MATMUL_TOO_LARGE && strstr(omega_gpu_matmul_last_error(), "not prepared"), |
| 396 | "unprepared shape rc=%d %s", rc, omega_gpu_matmul_last_error()); |
| 397 | CHECK(fk.alloc_calls == a1, "unprepared shape asked the driver for memory"); |
| 398 | omega_gpu_serving_release(); |
| 399 | rc = omega_gpu_matmul_resident_f32(1, x, odd, y, &info); |
| 400 | CHECK(rc != OMEGA_GPU_MATMUL_TOO_LARGE, "release did not lift the seal: %s", omega_gpu_matmul_last_error()); |
| 401 | } |
| 402 | free(x); free(y); |
| 403 | printf("gpu_serving_alloc_count_test sweep%s: %s (%d failed checks)\n", prepare ? "" : "-skip", g_fail ? "FAIL" : "PASS", g_fail); |
| 404 | return g_fail ? 1 : 0; |
| 405 | } |
| 406 | |
| 407 | /* ---- pin: a full cache of prepared kernels is never evicted. 8 slots, 8 prepared kernels (W_Q at 16..144 rows: |
| 408 | * 9 row counts; since the blocked prefill kernel (#407) 64 and 128 rows share one kernel, rt 4 x ct 4 at grid_x 128), |
| 409 | * then an unprepared call BEFORE any seal must be refused with no driver call, and every prepared kernel must still hit. */ |
| 410 | static int pin_mode(void) { |
| 411 | W_Q = upload(HID, QDIM); W_K = upload(HID, KVDIM); |
| 412 | float *x = calloc((size_t)144 * HID, sizeof *x), *y = calloc((size_t)144 * QDIM, sizeof *y); |
| 413 | CHECK(x && y, "pin buffers"); |
| 414 | if (g_fail) return 1; |
| 415 | OmegaGpuServingBounds b = QWEN3_4B_BOUNDS; |
| 416 | b.max_rows = 144; b.kernel_slots = 8; |
| 417 | CHECK(omega_gpu_reserve_serving(&b) == 0, "reserve failed: %s", omega_gpu_matmul_last_error()); |
| 418 | const int ap = fk.alloc_calls; |
| 419 | for (uint32_t m = 16; m <= 144; m += 16) { |
| 420 | int rc = omega_gpu_matmul_prepare(m, HID, QDIM); |
| 421 | CHECK(rc == OMEGA_GPU_MATMUL_OK, "prepare m=%u rc=%d %s", m, rc, omega_gpu_matmul_last_error()); |
| 422 | } |
| 423 | CHECK(fk.alloc_calls - ap == 8, "premise: prepare built %d kernels, the cache holds 8", fk.alloc_calls - ap); |
| 424 | int a0 = fk.alloc_calls; |
| 425 | OmegaGpuMatmulInfo info; |
| 426 | int rc = omega_gpu_matmul_resident_f32(1, x, W_K, y, &info); |
| 427 | CHECK(rc == OMEGA_GPU_MATMUL_TOO_LARGE && strstr(omega_gpu_matmul_last_error(), "prepared kernel"), |
| 428 | "unprepared call with a full pinned cache: rc=%d %s", rc, omega_gpu_matmul_last_error()); |
| 429 | CHECK(fk.alloc_calls == a0, "refused call asked the driver for memory"); |
| 430 | for (uint32_t m = 16; m <= 144; m += 16) { |
| 431 | rc = omega_gpu_matmul_resident_f32(m, x, W_Q, y, &info); |
| 432 | CHECK(rc != OMEGA_GPU_MATMUL_TOO_LARGE && info.kernel_cache_hit, "prepared m=%u evicted (rc=%d hit=%d)", m, rc, info.kernel_cache_hit); |
| 433 | } |
| 434 | CHECK(fk.alloc_calls == a0, "prepared kernels rebuilt: %d driver allocations", fk.alloc_calls - a0); |
| 435 | free(x); free(y); |
| 436 | printf("gpu_serving_alloc_count_test pin: %s (%d failed checks)\n", g_fail ? "FAIL" : "PASS", g_fail); |
| 437 | return g_fail ? 1 : 0; |
| 438 | } |
| 439 | |
| 440 | |
| 441 | /* Sealed without a serving reservation: the scratch buffers could still grow, so every matmul call |
| 442 | * must be refused by name before any driver allocation (review of omega#338). */ |
| 443 | static int seal_bare_mode(void) { |
| 444 | W_Q = upload(HID, QDIM); |
| 445 | float *x = calloc((size_t)144 * HID, sizeof *x), *y = calloc((size_t)144 * QDIM, sizeof *y); |
| 446 | CHECK(x && y, "seal-bare buffers"); |
| 447 | if (g_fail) return 1; |
| 448 | int rc = omega_gpu_matmul_prepare(16, HID, QDIM); |
| 449 | CHECK(rc == OMEGA_GPU_MATMUL_OK, "prepare m=16 rc=%d %s", rc, omega_gpu_matmul_last_error()); |
| 450 | omega_gpu_matmul_seal(1); |
| 451 | int a0 = fk.alloc_calls; |
| 452 | OmegaGpuMatmulInfo info; |
| 453 | rc = omega_gpu_matmul_resident_f32(144, x, W_Q, y, &info); |
| 454 | CHECK(rc == OMEGA_GPU_MATMUL_TOO_LARGE && strstr(omega_gpu_matmul_last_error(), "without a scratch reservation"), |
| 455 | "sealed, unreserved, larger call: rc=%d %s", rc, omega_gpu_matmul_last_error()); |
| 456 | CHECK(fk.alloc_calls == a0, "sealed call without a reservation asked the driver for memory (%d)", fk.alloc_calls - a0); |
| 457 | omega_gpu_matmul_seal(0); |
| 458 | rc = omega_gpu_matmul_resident_f32(16, x, W_Q, y, &info); |
| 459 | CHECK(rc != OMEGA_GPU_MATMUL_TOO_LARGE, "unsealed call still refused: rc=%d %s", rc, omega_gpu_matmul_last_error()); |
| 460 | free(x); free(y); |
| 461 | printf("gpu_serving_alloc_count_test seal-bare: %s (%d failed checks)\n", g_fail ? "FAIL" : "PASS", g_fail); |
| 462 | return g_fail ? 1 : 0; |
| 463 | } |
| 464 | /* update: omega_gpu_tensor_update_* re-packs new values into the tensor's own device buffer. The fake's device |
| 465 | * memory is host memory with GPU address = CPU address (fk.alloc_va), so the buffer is compared word for word |
| 466 | * with omega_matmul_frag_pack_b of the new values. Two shapes: 72 x 1100 (k pads to 80; n is not a multiple of |
| 467 | * 8 and spans three 64-tile chunks) and 4096 x 200 (the f32 staging holds 8 tiles at this k: four chunks, the |
| 468 | * last ragged). Per shape: the upload's one driver allocation is the GPU-uncached one, update_f32, update_bf16 |
| 469 | * back, update while sealed, no driver allocation or free, shape-mismatch refusals write nothing. Then NULL |
| 470 | * refusals and stale handles (closed device, then a reopened one). */ |
| 471 | static int upd_same(const uint16_t *d, const uint16_t *want, size_t cnt, const char *what) { |
| 472 | size_t diff = 0; |
| 473 | for (size_t i = 0; i < cnt; i++) diff += d[i] != want[i]; |
| 474 | CHECK(diff == 0, "%s: %zu of %zu packed words differ from omega_matmul_frag_pack_b", what, diff, cnt); |
| 475 | return diff == 0; |
| 476 | } |
| 477 | static OmegaGpuTensor *update_shape(uint32_t K, uint32_t N, uint16_t **b1_out, float **b2_out) { |
| 478 | const uint32_t KP = (K + 15u) / 16u * 16u, NT = (N + 7u) / 8u; |
| 479 | const size_t cnt = (size_t)K * N, packed = (size_t)KP * NT * 8u; |
| 480 | uint16_t *b1 = malloc(cnt * sizeof *b1), *b2h = malloc(cnt * sizeof *b2h), *want = malloc(packed * sizeof *want); |
| 481 | float *b2 = malloc(cnt * sizeof *b2); |
| 482 | CHECK(b1 && b2 && b2h && want, "update buffers %ux%u", K, N); |
| 483 | if (!b1 || !b2 || !b2h || !want) return NULL; |
| 484 | uint32_t s = 0x5eedu ^ K ^ (N << 16); |
| 485 | for (size_t i = 0; i < cnt; i++) { |
| 486 | s = s * 1664525u + 1013904223u; |
| 487 | b1[i] = omega_fp32_to_bf16(((float)(s >> 8) / 16777216.0f) - 0.5f); |
| 488 | b2[i] = ((float)(s % 2000003u) / 1000001.0f) - 1.0f + 1e-7f * (float)(i % 7u); /* low bits that rounding must drop */ |
| 489 | } |
| 490 | /* edge values for the update path's rounding: NaNs with payloads and sign, infinities, zeros, subnormals, |
| 491 | * exact ties (odd and even), the largest finite value (rounds to infinity) */ |
| 492 | static const uint32_t EDGE[] = {0x7fc00001u, 0xff800001u, 0x7fbfffffu, 0x7f800000u, 0xff800000u, 0x00000000u, 0x80000000u, |
| 493 | 0x00000001u, 0x807fffffu, 0x00018000u, 0x3f808000u, 0x3f818000u, 0xbf808000u, 0x7f7fffffu, 0xff7fffffu}; |
| 494 | for (size_t e = 0; e < sizeof EDGE / sizeof EDGE[0]; e++) for (size_t r = 0; r < 3; r++) { |
| 495 | const size_t i = r * (K / 3u) * N + (e * 73u + r) % N; /* rows 0, k/3, 2k/3; columns across the chunks */ |
| 496 | memcpy(&b2[i], &EDGE[e], 4); |
| 497 | } |
| 498 | for (size_t i = 0; i < cnt; i++) b2h[i] = omega_fp32_to_bf16(b2[i]); |
| 499 | |
| 500 | OmegaGpuTensor *t = NULL; |
| 501 | const int a0 = fk.alloc_calls, u0 = fk.uncached_calls; |
| 502 | int rc = omega_gpu_tensor_upload_bf16(K, N, b1, &t); |
| 503 | CHECK(rc == OMEGA_GPU_MATMUL_OK && fk.alloc_calls == a0 + 1 && a0 < 16, "premise %ux%u: upload rc=%d made %d allocations (index %d)", K, N, rc, fk.alloc_calls - a0, a0); |
| 504 | CHECK(fk.uncached_calls == u0 + 1, "%ux%u: the tensor's driver allocation was not the GPU-uncached one", K, N); |
| 505 | if (rc != OMEGA_GPU_MATMUL_OK || fk.alloc_calls != a0 + 1 || a0 >= 16) return NULL; |
| 506 | const uint16_t *d = (const uint16_t *)(uintptr_t)fk.alloc_va[a0]; |
| 507 | omega_matmul_frag_pack_b(b1, K, N, KP, 0, NT, want); |
| 508 | upd_same(d, want, packed, "premise: upload"); |
| 509 | |
| 510 | const int a1 = fk.alloc_calls, f1 = fk.free_calls; |
| 511 | CHECK(omega_gpu_tensor_update_f32(t, K + 1u, N, b2) == OMEGA_GPU_MATMUL_BAD_ARGS, "update_f32 with k + 1 not refused"); |
| 512 | CHECK(omega_gpu_tensor_update_bf16(t, K, N - 1u, b1) == OMEGA_GPU_MATMUL_BAD_ARGS, "update_bf16 with n - 1 not refused"); |
| 513 | CHECK(omega_gpu_tensor_update_f32(t, N, K, b2) == OMEGA_GPU_MATMUL_BAD_ARGS, "update_f32 with k and n swapped not refused"); |
| 514 | upd_same(d, want, packed, "shape-mismatch refusals left the buffer"); |
| 515 | rc = omega_gpu_tensor_update_f32(t, K, N, b2); |
| 516 | CHECK(rc == OMEGA_GPU_MATMUL_OK, "update_f32 %ux%u rc=%d %s", K, N, rc, omega_gpu_matmul_last_error()); |
| 517 | omega_matmul_frag_pack_b(b2h, K, N, KP, 0, NT, want); |
| 518 | upd_same(d, want, packed, "update_f32"); |
| 519 | rc = omega_gpu_tensor_update_bf16(t, K, N, b1); |
| 520 | CHECK(rc == OMEGA_GPU_MATMUL_OK, "update_bf16 %ux%u rc=%d %s", K, N, rc, omega_gpu_matmul_last_error()); |
| 521 | omega_matmul_frag_pack_b(b1, K, N, KP, 0, NT, want); |
| 522 | upd_same(d, want, packed, "update_bf16 back"); |
| 523 | omega_gpu_matmul_seal(1); |
| 524 | rc = omega_gpu_tensor_update_f32(t, K, N, b2); |
| 525 | omega_gpu_matmul_seal(0); |
| 526 | CHECK(rc == OMEGA_GPU_MATMUL_OK, "update while sealed rc=%d %s", rc, omega_gpu_matmul_last_error()); |
| 527 | omega_matmul_frag_pack_b(b2h, K, N, KP, 0, NT, want); |
| 528 | upd_same(d, want, packed, "update_f32 while sealed"); |
| 529 | CHECK(fk.alloc_calls == a1 && fk.free_calls == f1, "updates touched the driver (%d allocs, %d frees)", fk.alloc_calls - a1, fk.free_calls - f1); |
| 530 | uint32_t k2 = 0, n2 = 0; |
| 531 | omega_gpu_tensor_shape(t, &k2, &n2); |
| 532 | CHECK(k2 == K && n2 == N, "shape changed by update: %ux%u", k2, n2); |
| 533 | free(b2h); free(want); |
| 534 | *b1_out = b1; *b2_out = b2; |
| 535 | return t; |
| 536 | } |
| 537 | static int update_mode(void) { |
| 538 | OmegaGpuTensor *warm = upload(16, 8); /* opens the session, so each next upload is exactly one driver allocation */ |
| 539 | uint16_t *b1 = NULL, *b1b = NULL; float *b2 = NULL, *b2b = NULL; |
| 540 | OmegaGpuTensor *t = update_shape(72, 1100, &b1, &b2); |
| 541 | OmegaGpuTensor *t2 = update_shape(4096, 200, &b1b, &b2b); |
| 542 | if (!t || !t2) return 1; |
| 543 | |
| 544 | CHECK(omega_gpu_tensor_update_f32(NULL, 72, 1100, b2) == OMEGA_GPU_MATMUL_BAD_ARGS, "update_f32 NULL tensor not refused"); |
| 545 | CHECK(omega_gpu_tensor_update_bf16(NULL, 72, 1100, b1) == OMEGA_GPU_MATMUL_BAD_ARGS, "update_bf16 NULL tensor not refused"); |
| 546 | CHECK(omega_gpu_tensor_update_f32(t, 72, 1100, NULL) == OMEGA_GPU_MATMUL_BAD_ARGS, "update_f32 NULL values not refused"); |
| 547 | CHECK(omega_gpu_tensor_update_bf16(t, 72, 1100, NULL) == OMEGA_GPU_MATMUL_BAD_ARGS, "update_bf16 NULL values not refused"); |
| 548 | |
| 549 | /* a handle whose device was closed is stale: refused before anything is written (its memory is gone) */ |
| 550 | omega_gpu_device_close(); |
| 551 | const int a2 = fk.alloc_calls; |
| 552 | int rc = omega_gpu_tensor_update_f32(t, 72, 1100, b2); |
| 553 | CHECK(rc == OMEGA_GPU_MATMUL_CHIP_FAIL, "update_f32 of a stale handle rc=%d (want CHIP_FAIL)", rc); |
| 554 | rc = omega_gpu_tensor_update_bf16(t, 72, 1100, b1); |
| 555 | CHECK(rc == OMEGA_GPU_MATMUL_CHIP_FAIL, "update_bf16 of a stale handle rc=%d (want CHIP_FAIL)", rc); |
| 556 | CHECK(fk.alloc_calls == a2, "stale update asked the driver for memory"); |
| 557 | OmegaGpuTensor *reopen = upload(16, 8); /* a new device: the old handle stays stale */ |
| 558 | rc = omega_gpu_tensor_update_f32(t, 72, 1100, b2); |
| 559 | CHECK(rc == OMEGA_GPU_MATMUL_CHIP_FAIL, "update_f32 of a handle from a previous device rc=%d (want CHIP_FAIL)", rc); |
| 560 | omega_gpu_tensor_free(reopen); |
| 561 | omega_gpu_tensor_free(t); |
| 562 | omega_gpu_tensor_free(t2); |
| 563 | omega_gpu_tensor_free(warm); |
| 564 | omega_gpu_device_close(); |
| 565 | free(b1); free(b2); free(b1b); free(b2b); |
| 566 | printf("gpu_serving_alloc_count_test update: %s (%d failed checks)\n", g_fail ? "FAIL" : "PASS", g_fail); |
| 567 | return g_fail ? 1 : 0; |
| 568 | } |
| 569 | |
| 570 | int main(int argc, char **argv) { |
| 571 | const char *mode = argc > 1 ? argv[1] : "plain"; |
| 572 | const int do_reserve = strcmp(mode, "reserve") == 0, assert_zero = do_reserve || strcmp(mode, "reserve-skip") == 0; |
| 573 | fake_reset(); |
| 574 | fk.kernel_mode = FK_KERNEL_SKIP; |
| 575 | fk.alloc_max = 1ull << 31; |
| 576 | omega_gpu_matmul_set_cta_budget(256); |
| 577 | |
| 578 | g_x = calloc((size_t)CHUNK * INTER, sizeof *g_x); |
| 579 | g_y = calloc((size_t)CHUNK * INTER, sizeof *g_y); |
| 580 | g_logits = calloc(VOCAB, sizeof *g_logits); |
| 581 | g_k = calloc((size_t)4096 * KVDIM, sizeof *g_k); |
| 582 | g_v = calloc((size_t)4096 * KVDIM, sizeof *g_v); |
| 583 | CHECK(g_x && g_y && g_logits && g_k && g_v, "host buffers"); |
| 584 | if (g_fail) return 1; |
| 585 | |
| 586 | Phase ph[9]; |
| 587 | int np = 0; |
| 588 | if (strcmp(mode, "bounds") == 0) return bounds_mode(); |
| 589 | if (strcmp(mode, "bounds-block") == 0) return bounds_block_mode(); |
| 590 | if (strcmp(mode, "rollback") == 0) return rollback_mode(); |
| 591 | if (strcmp(mode, "sweep") == 0) return sweep_mode(1); |
| 592 | if (strcmp(mode, "pin") == 0) return pin_mode(); |
| 593 | if (strcmp(mode, "seal-bare") == 0) return seal_bare_mode(); |
| 594 | if (strcmp(mode, "update") == 0) return update_mode(); |
| 595 | if (strcmp(mode, "sweep-skip") == 0) return sweep_mode(0); |
| 596 | |
| 597 | begin(&ph[np], "LOAD (session+6 weights)"); |
| 598 | W_Q = upload(HID, QDIM); W_K = upload(HID, KVDIM); W_O = upload(QDIM, HID); |
| 599 | W_G = upload(HID, INTER); W_D = upload(INTER, HID); W_LM = upload(HID, VOCAB); |
| 600 | end(&ph[np], 1); np++; |
| 601 | CHECK(omega_gpu_session_open_count() == 1, "session opened %u times", omega_gpu_session_open_count()); |
| 602 | |
| 603 | /* WARM-UP: one decode step at context 1. Everything after this phase is "serving". */ |
| 604 | begin(&ph[np], "WARMUP decode ctx=1"); |
| 605 | step(1, 0); |
| 606 | end(&ph[np], 1); np++; |
| 607 | if (assert_zero) { |
| 608 | /* a daemon's startup: reserve once after loading and a first request, then cover each row-count class |
| 609 | * (1 decode row, a full chunk, the 100 and 72 row remainders) so every matmul shape has been built once */ |
| 610 | if (do_reserve) CHECK(omega_gpu_reserve_serving(&QWEN3_4B_BOUNDS) == 0, "reserve failed: %s", omega_gpu_matmul_last_error()); |
| 611 | begin(&ph[np], "WARMUP row classes"); |
| 612 | step(128, 0); step(100, 0); step(72, 0); step(1, 0); |
| 613 | end(&ph[np], 0); np++; |
| 614 | } |
| 615 | const int after_warm_allocs = fk.alloc_calls, after_warm_frees = fk.free_calls; |
| 616 | |
| 617 | uint32_t ctx = 0; |
| 618 | const uint32_t N1 = 100, D1 = 64, N2 = 200, D2 = 64; |
| 619 | |
| 620 | begin(&ph[np], "PREFILL-A 100 tokens"); |
| 621 | for (uint32_t off = 0; off < N1; off += CHUNK) { uint32_t r = N1 - off < CHUNK ? N1 - off : CHUNK; step(r, ctx + off); } |
| 622 | ctx += N1; |
| 623 | end(&ph[np], 1); np++; |
| 624 | |
| 625 | begin(&ph[np], "DECODE-A 64 tokens"); |
| 626 | for (uint32_t i = 0; i < D1; i++) { step(1, ctx); ctx++; } |
| 627 | end(&ph[np], 1); np++; |
| 628 | |
| 629 | begin(&ph[np], "PREFILL-B 200 tokens"); |
| 630 | for (uint32_t off = 0; off < N2; off += CHUNK) { uint32_t r = N2 - off < CHUNK ? N2 - off : CHUNK; step(r, ctx + off); } |
| 631 | ctx += N2; |
| 632 | end(&ph[np], 1); np++; |
| 633 | |
| 634 | begin(&ph[np], "DECODE-B 64 tokens"); |
| 635 | for (uint32_t i = 0; i < D2; i++) { step(1, ctx); ctx++; } |
| 636 | end(&ph[np], 1); np++; |
| 637 | |
| 638 | const int serve_allocs = fk.alloc_calls - after_warm_allocs, serve_frees = fk.free_calls - after_warm_frees; |
| 639 | printf("MEASURE AFTER WARM-UP: %d driver allocations, %d driver frees over %u tokens of context (final ctx %u)\n", |
| 640 | serve_allocs, serve_frees, ctx, ctx); |
| 641 | printf("MEASURE distinct matmul kernel shapes seen (kp,np,grid_x): %d (cache slots: %d)\n", g_nseen, do_reserve ? 32 : 8); |
| 642 | printf("MEASURE live driver buffers at end: %d ; session opens: %u ; launches reaching the fake: %d\n", |
| 643 | fk.live, omega_gpu_session_open_count(), fk.kernel_ran); |
| 644 | printf("MEASURE return codes: matmul ok %d, matmul poison-check CHIP_FAIL %d, attention ok %d, attention UNWRITTEN %d, other %d\n", |
| 645 | g_rc_mm_ok, g_rc_mm_chipfail, g_rc_at_ok, g_rc_at_unwritten, g_rc_bad); |
| 646 | |
| 647 | /* ---- what is true today and stays true ---- */ |
| 648 | CHECK(g_rc_bad == 0, "%d calls returned a code other than the expected poison-check ones", g_rc_bad); |
| 649 | CHECK(!omega_gpu_matmul_is_blocked() && !omega_gpu_session_is_blocked(), "session latched"); |
| 650 | CHECK(fk.double_free == 0, "double free %d", fk.double_free); |
| 651 | CHECK(fk.pb_parse_error == 0, "pushbuffer parse error"); |
| 652 | int calls_made = 0; |
| 653 | for (int i = 1; i < np; i++) calls_made += ph[i].mm_calls + ph[i].at_calls; |
| 654 | CHECK(fk.kernel_ran == calls_made, "launches %d != calls made %d", fk.kernel_ran, calls_made); |
| 655 | if (assert_zero) { |
| 656 | /* the point of the reservation: nothing reached the driver while serving inside the bounds */ |
| 657 | CHECK(serve_allocs == 0 && serve_frees == 0, "serving made %d driver allocations and %d frees after warm-up; the reservation promises none", serve_allocs, serve_frees); |
| 658 | for (int i = np - 4; i < np; i++) CHECK(ph[i].allocs == 0 && ph[i].frees == 0, "phase %s: %d allocs, %d frees", ph[i].name, ph[i].allocs, ph[i].frees); |
| 659 | } |
| 660 | CHECK(fk.live < FK_LIVE_MAX, "fake live table full (%d)", fk.live); |
| 661 | /* every real allocation is either live or was freed */ |
| 662 | CHECK(fk.alloc_calls - fk.free_calls >= fk.live, "alloc/free/live inconsistent: %d - %d < %d", fk.alloc_calls, fk.free_calls, fk.live); |
| 663 | |
| 664 | /* teardown: tensors first, then the device (the on-close hooks free the API scratch) */ |
| 665 | omega_gpu_tensor_free(W_Q); omega_gpu_tensor_free(W_K); omega_gpu_tensor_free(W_O); |
| 666 | omega_gpu_tensor_free(W_G); omega_gpu_tensor_free(W_D); omega_gpu_tensor_free(W_LM); |
| 667 | omega_gpu_device_close(); |
| 668 | CHECK(fk.live_at_close == 0, "leak: %d driver buffers live at close", fk.live_at_close); |
| 669 | printf("MEASURE leaked at close: %d\n", fk.live_at_close); |
| 670 | |
| 671 | printf("gpu_serving_alloc_count_test %s: %s (%d failed checks)\n", mode, g_fail ? "FAIL" : "PASS", g_fail); |
| 672 | return g_fail ? 1 : 0; |
| 673 | } |