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