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
GitHub aien-dev/omega-mv#333 (code only)
Apply it
git checkout -b patch-6b1118a938b89682 7894abb7ab56ac48ea738126186995864003110e curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/6b1118a938b89682.patch | git am
Provenance
github-import 1
aien-dev/omega-mv
333
1ee5715668d6ad7f406fb9b4cff5d87e0986287f
{"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":333,"url":"https://github.com/aien-dev/omega-mv/pull/333","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:37:23Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/.crumb b/.crumb index 21230e5d5..cc8f0c9b0 100644 --- a/.crumb +++ b/.crumb @@ -63,8 +63,8 @@ "purpose": "Directory doctrine. Purpose not yet described by a human." }, { - "digest": "39433fa0e7399d9b252e4eba35651c36d385fdbe8936064fb5fb31fbd63e7dc1", - "last_commit": "78d719d", + "digest": "b0f9da3c4a7b14bd483ce7cb63016880c21d47e393aa997e01f1c674c4b9753c", + "last_commit": "884360a", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, @@ -87,14 +87,14 @@ "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "0f1131a82d19f6923dd6136590b9a406245f967e1d3787dcd0d873d4906c40e6", - "last_commit": "01f6a74", + "digest": "d33d643a490c998fc92cc0df349b8df0f53fe4b76a175d10ed5d776d5f176f06", + "last_commit": "884360a", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "240bd58ed4724aab6e803da398cfc13b84108ce1b041ddb67dd0ff07f9657e55", - "last_commit": "78d719d", + "digest": "daa6072bbb4f876d9e0e0a8cc88c56e1fe98aad01a4b9b18bfc8cea84845f7f0", + "last_commit": "884360a", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, @@ -105,15 +105,15 @@ "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:a17d2cd9e7c7f11f617bbf1747090b2ee2306e3d3ccec3a3f598e9b23741765e", - "compiled_at": "2026-10-07T21:46:01Z", + "children_root": "sha256:33e7177270f4557debebe8ed5495b23c84805eac64e5ec2c704b40735848124e", + "compiled_at": "2026-10-07T22:12:35Z", "compiler": "crumb-compile/1", - "digest": "sha256:f8144dfd45bfa815261e79c9dc10299fd8d325cc8842b34be1ad309a9d9fe1d8", + "digest": "sha256:f9ca61a6a80d0d8a3bce9b416b9a55a11655d7c860e548c6d7362ba9d139e0c1", "evidence_files": 4818, "evidence_root": "sha256:07642731585ad396c5d9b566a4b8df7807018f494d322dffce41bd04fb51e553", - "files": 6659, - "generated_at_commit": "78d719dda6b72ff28e3f03f9256f4796cbe8cd1f", - "source_tree": "sha256:6dfa3c87745f8ccc48962e16509340bd894280dfb8610d487aed2ed6d58fb735" + "files": 6661, + "generated_at_commit": "884360a67256afc14fa6e6035292ad609fab337f", + "source_tree": "sha256:4f3546871ec7c86545d542e722a94d4ccde5adf26b61bf2bfda95397d7d1a2d0" }, "owner_lane": "", "provenance": { diff --git a/mk/.crumb b/mk/.crumb index 06a7bf9c2..21075475e 100644 --- a/mk/.crumb +++ b/mk/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-07T21:28:08Z", + "compiled_at": "2026-10-07T22:12:35Z", "compiler": "crumb-compile/1", - "digest": "sha256:39433fa0e7399d9b252e4eba35651c36d385fdbe8936064fb5fb31fbd63e7dc1", + "digest": "sha256:b0f9da3c4a7b14bd483ce7cb63016880c21d47e393aa997e01f1c674c4b9753c", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 60, - "generated_at_commit": "01f6a74636b8383b010cdb95597839582c415c27", - "source_tree": "sha256:39973ddb9cb9f8eda311b0a3b156ffb8fc6808eca9bd59ec88b1a302c27588e6" + "generated_at_commit": "884360a67256afc14fa6e6035292ad609fab337f", + "source_tree": "sha256:d703768e49a40d47819f67b982d42207c3b70a7b8b3faa8e2df0796f5235d396" }, "owner_lane": "", "provenance": { diff --git a/mk/omega-gpu-engine.mk b/mk/omega-gpu-engine.mk index f9401f495..62d4fa826 100644 --- a/mk/omega-gpu-engine.mk +++ b/mk/omega-gpu-engine.mk @@ -74,7 +74,7 @@ test-gpu-engine-gb10-compile: build/test_omega_gpu_engine_gb10.o # Measurement (sc#277): driver allocations and frees made by the REAL session + matmul + attention # APIs while serving a Qwen3-4B-shaped sequence, against the simulated driver. Host only, no device. # Prints MEASURE lines; asserts only bookkeeping. Links no physics C sources, only its headers. -GPU_SERVE_ALLOC_SRCS = src/omega_gpu_session.c src/omega_gpu_matmul_api.c src/omega_gpu_attention_api.c \ +GPU_SERVE_ALLOC_SRCS = src/omega_gpu_session.c src/omega_gpu_matmul_api.c src/omega_gpu_attention_api.c src/omega_gpu_serving.c \ src/omega_gpu_wait.c src/omega_blackwell_codegen.c src/omega_blackwell_encoder.c src/omega_blackwell_qmd.c \ src/omega_blackwell_matmul.c src/sha256.c build/gpu_serving_alloc_count_test: tests/gpu_serving_alloc_count_test.c tests/fake_m16_native.c tests/fake_m16_native.h $(GPU_SERVE_ALLOC_SRCS) @@ -83,4 +83,11 @@ build/gpu_serving_alloc_count_test: tests/gpu_serving_alloc_count_test.c tests/f -o $@ tests/gpu_serving_alloc_count_test.c tests/fake_m16_native.c $(GPU_SERVE_ALLOC_SRCS) -lm .PHONY: test-gpu-serving-alloc test-gpu-serving-alloc: build/gpu_serving_alloc_count_test - ./build/gpu_serving_alloc_count_test + ./build/gpu_serving_alloc_count_test plain + ./build/gpu_serving_alloc_count_test reserve + ./build/gpu_serving_alloc_count_test bounds + ./build/gpu_serving_alloc_count_test bounds-block + ./build/gpu_serving_alloc_count_test rollback + @if ./build/gpu_serving_alloc_count_test reserve-skip >build/gpu_serving_alloc_count_skip.log 2>&1; then echo "RED RUN PASSED: the zero-allocation assertion does not need the reservation call"; exit 1; fi + @if [ "$$(grep -c "^FAIL" build/gpu_serving_alloc_count_skip.log)" -lt 1 ]; then echo "reserve-skip exited nonzero without a FAIL check (crash, not the red assertion)"; exit 1; fi + @echo "reserve-skip (no reservation call): fails as required ($$(grep -c "^FAIL" build/gpu_serving_alloc_count_skip.log) failing checks)" diff --git a/src/.crumb b/src/.crumb index 436a49d6f..49e958ab8 100644 --- a/src/.crumb +++ b/src/.crumb @@ -134,7 +134,7 @@ }, { "digest": "98acfce1a1131f63cb89cce389b153d85952ccdcf5f3e0462c080a159b45ec44", - "last_commit": "957b5a3", + "last_commit": "01f6a74", "name": "runtime", "purpose": "Directory src/runtime. Purpose not yet described by a human." }, @@ -170,14 +170,14 @@ } ], "children_root": "sha256:00102626b51610209ee39290ee9df5d653441f43b09872a1b21388f881cc5500", - "compiled_at": "2026-10-07T16:27:12Z", + "compiled_at": "2026-10-07T22:12:35Z", "compiler": "crumb-compile/1", - "digest": "sha256:0f1131a82d19f6923dd6136590b9a406245f967e1d3787dcd0d873d4906c40e6", + "digest": "sha256:d33d643a490c998fc92cc0df349b8df0f53fe4b76a175d10ed5d776d5f176f06", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 412, - "generated_at_commit": "957b5a387ad5e8ff373696fff56b95946b402618", - "source_tree": "sha256:83b1ba27d582aae9c229ce50b78e77c81cf3e44f00c9d544c4835ee701c8d667" + "files": 414, + "generated_at_commit": "884360a67256afc14fa6e6035292ad609fab337f", + "source_tree": "sha256:6e4bd60b8c7c1f395c9438c3a4e44fce1efae27b1ca35d2f443f4e5fbe3b4882" }, "owner_lane": "", "provenance": { diff --git a/src/omega_gpu_attention_api.c b/src/omega_gpu_attention_api.c index dc2818315..28dc99bf2 100644 --- a/src/omega_gpu_attention_api.c +++ b/src/omega_gpu_attention_api.c @@ -594,10 +594,12 @@ static int dev_open_locked(void) { /* Where a call stages the KV slice it is about to hand the kernel: straight into the * device staging buffer on the chip path (one copy), a host buffer under the simulator. * Caller holds the lock; the result is valid until the next staging call. */ -static void *stage_target(size_t bytes, void **host_tmp) { +static void *stage_target(size_t bytes, void **host_tmp, int *fail_rc) { *host_tmp = NULL; + *fail_rc = OMEGA_GPU_ATTN_CHIP_FAIL; if (g_sim) { *host_tmp = malloc(bytes ? bytes : 1); return *host_tmp; } - if (omega_gpu_session_scratch(&g_pool, bytes) != 0) return NULL; + int r = omega_gpu_session_scratch(&g_pool, bytes); + if (r != 0) { if (r == OMEGA_GPU_SESSION_SCRATCH_RESERVED) *fail_rc = OMEGA_GPU_ATTN_TOO_LARGE; return NULL; } /* reserved bound exceeded: loud, no allocation */ return g_pool.mem.cpu; } @@ -605,9 +607,11 @@ static void *stage_target(size_t bytes, void **host_tmp) { * invalidate, constant bank, QMD0/QMD1, first marker, L2_FLUSH_DIRTY, second marker), * then readback and the poison scan. L->pool may already be the pool staging buffer. */ static int run_launch(const Slot *ks, const Launch *L, OmegaGpuAttnInfo *info) { - if (omega_gpu_session_scratch(&g_q, L->q_bytes) || omega_gpu_session_scratch(&g_tab, L->tab_bytes) || - omega_gpu_session_scratch(&g_out, L->out_bytes) || (L->pool != g_pool.mem.cpu && omega_gpu_session_scratch(&g_pool, L->pool_bytes))) - return OMEGA_GPU_ATTN_CHIP_FAIL; + int sr = omega_gpu_session_scratch(&g_q, L->q_bytes); + if (!sr) sr = omega_gpu_session_scratch(&g_tab, L->tab_bytes); + if (!sr) sr = omega_gpu_session_scratch(&g_out, L->out_bytes); + if (!sr && L->pool != g_pool.mem.cpu) sr = omega_gpu_session_scratch(&g_pool, L->pool_bytes); + if (sr) return sr == OMEGA_GPU_SESSION_SCRATCH_RESERVED ? OMEGA_GPU_ATTN_TOO_LARGE : OMEGA_GPU_ATTN_CHIP_FAIL; memcpy(g_q.mem.cpu, L->q, L->q_bytes); if (L->pool != g_pool.mem.cpu) memcpy(g_pool.mem.cpu, L->pool, L->pool_bytes); memcpy(g_tab.mem.cpu, L->tab, L->tab_bytes); @@ -652,6 +656,50 @@ static int launch(const Slot *ks, const Launch *L, OmegaGpuAttnInfo *info) { return run_launch(ks, L, info); } +/* ---- opt-in serving reservation (see the header) ---- */ + +static int check_heads(uint32_t num_q_heads, uint32_t num_kv_heads, uint32_t head_dim, uint32_t *log2gqa); +static int log2_exact(uint32_t v, uint32_t *out); + +int omega_gpu_attention_reserve(uint32_t max_context, uint32_t max_seqs, uint32_t num_q_heads, uint32_t num_kv_heads, uint32_t head_dim, uint32_t kv_block_size) { + uint32_t log2gqa, log2bs; + if (max_context == 0 || max_seqs == 0) { omega_gpu_session_set_error("attention reserve: zero bound"); return OMEGA_GPU_ATTN_BAD_ARGS; } + if (kv_block_size && log2_exact(kv_block_size, &log2bs) != 0) { omega_gpu_session_set_error("attention reserve: block size not a power of two"); return OMEGA_GPU_ATTN_BAD_ARGS; } + int rc = check_heads(num_q_heads, num_kv_heads, head_dim, &log2gqa); + if (rc != OMEGA_GPU_ATTN_OK) { omega_gpu_session_set_error("attention reserve: bad head shape"); return rc; } + /* KV staging, worst case over the two paths, per sequence: contiguous f32 holds both planes for max_context tokens + * (2 * ctx * kvh * hd * 4); paged bf16 stages whole blocks of both planes (round_up(ctx, block) * kvh * hd * 4), which + * is the larger one when max_context is under half a block. */ + size_t per_seq = 2u * max_context * (size_t)num_kv_heads * head_dim * 4u; + if (kv_block_size) { + size_t paged = ((size_t)max_context + kv_block_size - 1) / kv_block_size * kv_block_size * num_kv_heads * head_dim * 4u; + if (paged > per_seq) per_seq = paged; + } + const size_t pool = per_seq * max_seqs; + const size_t qo = (size_t)max_seqs * num_q_heads * head_dim * 4u; + const size_t tab = ((size_t)max_seqs * max_context + max_seqs) * 4u; + LOCK(); + rc = OMEGA_GPU_ATTN_OK; + if (g_sim) rc = OMEGA_GPU_ATTN_OK; /* the simulator stages in host memory: nothing to reserve */ + else if (dev_open_locked() != OMEGA_GPU_ATTN_OK) rc = OMEGA_GPU_ATTN_CHIP_FAIL; + else if (omega_gpu_session_scratch_reserve(&g_pool, pool) || omega_gpu_session_scratch_reserve(&g_q, qo) || + omega_gpu_session_scratch_reserve(&g_out, qo) || omega_gpu_session_scratch_reserve(&g_tab, tab < 8 ? 8 : tab)) { + /* roll back this API's own buffers: a failed reserve leaves nothing flagged (growth allowed as before) */ + omega_gpu_session_scratch_unreserve(&g_pool); omega_gpu_session_scratch_unreserve(&g_q); + omega_gpu_session_scratch_unreserve(&g_out); omega_gpu_session_scratch_unreserve(&g_tab); + rc = OMEGA_GPU_ATTN_CHIP_FAIL; + } + UNLOCK(); + return rc; +} + +void omega_gpu_attention_unreserve(void) { + LOCK(); + omega_gpu_session_scratch_unreserve(&g_pool); omega_gpu_session_scratch_unreserve(&g_q); + omega_gpu_session_scratch_unreserve(&g_out); omega_gpu_session_scratch_unreserve(&g_tab); + UNLOCK(); +} + /* ------------------------------------------------------- public entries */ static void info_begin(OmegaGpuAttnInfo *info, const OmegaBlackwellKernel *k, bool hit) { @@ -710,8 +758,9 @@ int omega_gpu_gqa_attention_f32(const float *q, const float *k_cache, const floa if (rc != OMEGA_GPU_ATTN_OK) return rc; size_t plane = (size_t)seq_len * num_kv_heads * HD * 4; void *host_tmp = NULL; - uint8_t *pool = stage_target(2 * plane, &host_tmp); - if (!pool) { UNLOCK(); return OMEGA_GPU_ATTN_CHIP_FAIL; } + int stage_rc; + uint8_t *pool = stage_target(2 * plane, &host_tmp, &stage_rc); + if (!pool) { UNLOCK(); return stage_rc; } memcpy(pool, k_cache, plane); memcpy(pool + plane, v_cache, plane); if (info) { info->kv_bytes_staged += 2 * plane; info->kv_bytes_naive += 2 * plane; info->kv_blocks_logical += 1; info->kv_blocks_unique += 1; } /* one contiguous pseudo-block */ @@ -801,8 +850,9 @@ static int paged_core(const float *q, const uint8_t *pool, const OmegaGpuKvLayou uint64_t staged_bytes = (uint64_t)(g ? g : 1) * ly->layer_stride_bytes; if (staged_bytes > 0xffffffffull) { free(tab); rc = OMEGA_GPU_ATTN_TOO_LARGE; break; } void *host_tmp = NULL; - uint8_t *staged = stage_target((size_t)staged_bytes, &host_tmp); - if (!staged) { free(tab); rc = OMEGA_GPU_ATTN_CHIP_FAIL; break; } + int stage_rc; + uint8_t *staged = stage_target((size_t)staged_bytes, &host_tmp, &stage_rc); + if (!staged) { free(tab); rc = stage_rc; break; } /* pass 2: copy each staged slot's (physical block, layer) slice exactly once */ if (g_dedupe) { for (uint32_t u = 0; u < g; u++) { diff --git a/src/omega_gpu_attention_api.h b/src/omega_gpu_attention_api.h index 45833a020..999e8630b 100644 --- a/src/omega_gpu_attention_api.h +++ b/src/omega_gpu_attention_api.h @@ -141,6 +141,22 @@ int omega_gpu_paged_attention_batch_bf16(const float *q, const void *pool, const const char *omega_gpu_attention_rc_name(int rc); void omega_gpu_attention_cache_clear(void); +/* OPT-IN serving reservation (omega_gpu_reserve_serving in omega_gpu_serving.h calls this). Allocates the KV staging + * pool (2 planes f32, 8 KiB per token at 8 kv heads x 128), q, output and table buffers once for contexts up to + * max_context and up to max_seqs sequences per call. A later call that fits never allocates; one that does not + * returns OMEGA_GPU_ATTN_TOO_LARGE ("serving reservation exceeded" in omega_gpu_attention_last_error()) and asks the + * driver for nothing. Never called = behaviour unchanged. The f32 contiguous path still refuses contexts past + * OMEGA_GPU_ATTN_MAX_GQA_CTX on its own. kv_block_size (power of two, 0 = no paged use) sizes the pool for the paged bf16 + * path too: it stages whole blocks, so under half a block of context it needs more than the f32 formula. The pool is + * sized in BYTES for the larger of the two paths: the paged path therefore admits up to about 2 x max_context tokens + * (whole blocks of the same bytes); what it refuses is a call whose staging exceeds the reserved bytes. max_seqs is + * checked per launch chunk (the batched call splits sequences into chunks of 64 / num_q_heads), so a call with more + * sequences than max_seqs succeeds when every chunk fits. A paged or batched call refused on a LATER chunk has already + * written the outputs of the earlier chunks: treat TOO_LARGE as "output incomplete". If the reserve fails part way it + * rolls back: none of this API's buffers stays flagged. 0 on success, else an OMEGA_GPU_ATTN_* code. */ +int omega_gpu_attention_reserve(uint32_t max_context, uint32_t max_seqs, uint32_t num_q_heads, uint32_t num_kv_heads, uint32_t head_dim, uint32_t kv_block_size); +void omega_gpu_attention_unreserve(void); + /* Stage name of the most recent CHIP_FAIL ("" if none), shared with the other GPU APIs * (omega_gpu_session_last_error); an open failure carries the driver text. */ const char *omega_gpu_attention_last_error(void); diff --git a/src/omega_gpu_matmul_api.c b/src/omega_gpu_matmul_api.c index 73edb287d..6b1e6f09f 100644 --- a/src/omega_gpu_matmul_api.c +++ b/src/omega_gpu_matmul_api.c @@ -25,7 +25,8 @@ #include <string.h> #include <time.h> -#define CACHE_SLOTS 8 +#define CACHE_SLOTS 8 /* default kernel cache slots; raised only by omega_gpu_matmul_reserve */ +#define CACHE_SLOTS_MAX 32 /* storage for the opt-in larger cache (a code buffer is a few KB) */ #define FRAG_PACK_TILES 64u /* column tiles per host packing chunk at upload */ #define MARKER_TIMEOUT_MS 20000ull /* a 1x2048x128256 call is ~0.1 s; 20 s is a stall, not a slow run */ #define POISON_F32 (-999.0f) @@ -51,9 +52,10 @@ static struct { int oracle, mutant; uint32_t unroll; /* test override of the fragment kernel unroll; 0 = omega_matmul_frag_unroll */ bool hooked; - CacheSlot cache[CACHE_SLOTS]; + CacheSlot cache[CACHE_SLOTS_MAX]; unsigned cache_next; -} g = { .cta_budget = OMEGA_GPU_MATMUL_MAX_CTAS, .oracle = 1 }; + unsigned slots; /* active kernel cache slots, <= CACHE_SLOTS_MAX */ +} g = { .cta_budget = OMEGA_GPU_MATMUL_MAX_CTAS, .oracle = 1, .slots = CACHE_SLOTS }; #define FAILAT(s) (omega_gpu_session_set_error(s), 1) const char *omega_gpu_matmul_last_error(void) { return omega_gpu_session_last_error(); } @@ -83,7 +85,7 @@ int omega_gpu_matmul_is_blocked(void) { return omega_gpu_session_is_blocked(); } /* ---- device-side state (caller holds the session lock) ---- */ static void cache_clear_locked(void) { - for (unsigned i = 0; i < CACHE_SLOTS; i++) { + for (unsigned i = 0; i < CACHE_SLOTS_MAX; i++) { CacheSlot *s = &g.cache[i]; if (!s->valid) continue; omega_blackwell_kernel_free(&s->kernel); @@ -98,6 +100,7 @@ static void on_session_close(void) { cache_clear_locked(); omega_gpu_session_scratch_free(&g.a_buf); omega_gpu_session_scratch_free(&g.c_buf); + g.slots = CACHE_SLOTS; /* a serving reservation ends with the device */ } static OmegaGpuSession *dev_open_locked(void) { @@ -122,6 +125,44 @@ void omega_gpu_matmul_set_cta_budget(uint32_t ctas) { uint32_t omega_gpu_matmul_cta_budget(void) { return g.cta_budget; } +/* ---- opt-in serving reservation (see the header) ---- */ + +int omega_gpu_matmul_reserve(uint32_t max_rows, uint32_t max_k, uint32_t max_n, uint32_t max_n_one_row, uint32_t kernel_slots) { + if (max_rows == 0 || max_k == 0 || max_n == 0) { omega_gpu_session_set_error("matmul reserve: zero bound"); return OMEGA_GPU_MATMUL_BAD_ARGS; } + if (max_rows > OMEGA_BW_MATMUL_MAX_M || max_k > OMEGA_BW_MATMUL_MAX_K || max_n > OMEGA_BW_MATMUL_MAX_N || max_n_one_row > OMEGA_BW_MATMUL_MAX_N || + kernel_slots > CACHE_SLOTS_MAX) { + omega_gpu_session_set_error("matmul reserve: bound past the matmul envelope"); + return OMEGA_GPU_MATMUL_TOO_LARGE; + } + const size_t mp = round_up(max_rows, OMEGA_GPU_MATMUL_TILE_M), kp = round_up(max_k, OMEGA_GPU_MATMUL_TILE_K); + size_t c_bytes = mp * round_up(max_n, OMEGA_GPU_MATMUL_TILE_N) * 4u; + if (max_n_one_row) { /* a call of up to 16 rows (one row tile) may be wider than the batch width: lm_head */ + size_t one = (size_t)OMEGA_GPU_MATMUL_TILE_M * round_up(max_n_one_row, OMEGA_GPU_MATMUL_TILE_N) * 4u; + if (one > c_bytes) c_bytes = one; + } + omega_gpu_session_lock(); + int rc = OMEGA_GPU_MATMUL_OK; + if (!dev_open_locked()) rc = OMEGA_GPU_MATMUL_CHIP_FAIL; + else if (omega_gpu_session_scratch_reserve(&g.a_buf, mp * kp * 2u) != 0 || omega_gpu_session_scratch_reserve(&g.c_buf, c_bytes) != 0) { + /* roll back this API's own buffers: a failed reserve leaves nothing flagged */ + omega_gpu_session_scratch_unreserve(&g.a_buf); omega_gpu_session_scratch_unreserve(&g.c_buf); + rc = OMEGA_GPU_MATMUL_CHIP_FAIL; + } + else if (kernel_slots > g.slots) { /* grow only; the cache restarts empty */ + cache_clear_locked(); + g.slots = kernel_slots; + } + omega_gpu_session_unlock(); + return rc; +} + +void omega_gpu_matmul_unreserve(void) { + omega_gpu_session_lock(); + omega_gpu_session_scratch_unreserve(&g.a_buf); + omega_gpu_session_scratch_unreserve(&g.c_buf); + omega_gpu_session_unlock(); +} + void omega_gpu_matmul_set_oracle(int on) { g.oracle = on ? 1 : 0; } void omega_gpu_matmul_test_set_mutant(int on) { @@ -141,7 +182,7 @@ void omega_gpu_matmul_test_set_unroll(uint32_t unroll) { /* ---- kernel cache (caller holds the lock, device open) ---- */ static int kernel_for(uint32_t kp, uint32_t np, uint32_t grid_x, CacheSlot **out, bool *hit) { - for (unsigned i = 0; i < CACHE_SLOTS; i++) { + for (unsigned i = 0; i < g.slots; i++) { CacheSlot *s = &g.cache[i]; if (s->valid && s->kp == kp && s->np == np && s->grid_x == grid_x && s->mutant == g.mutant) { *out = s; *hit = true; @@ -163,7 +204,7 @@ static int kernel_for(uint32_t kp, uint32_t np, uint32_t grid_x, CacheSlot **out if (omega_gpu_session_alloc_code(kernel.code_size, &code) != 0) { omega_blackwell_kernel_free(&kernel); return OMEGA_GPU_MATMUL_CHIP_FAIL; } memcpy(code.cpu, kernel.code, kernel.code_size); CacheSlot *s = &g.cache[g.cache_next]; - g.cache_next = (g.cache_next + 1) % CACHE_SLOTS; + g.cache_next = (g.cache_next + 1) % g.slots; if (s->valid) { omega_blackwell_kernel_free(&s->kernel); omega_gpu_session_free(&s->code); } s->valid = true; s->kp = kp; s->np = np; s->grid_x = grid_x; s->mutant = g.mutant; s->spec = spec; s->kernel = kernel; s->code = code; @@ -263,8 +304,12 @@ static int matmul_core(uint32_t m, const uint16_t *a, const OmegaGpuTensor *b, f strncpy(info->target_chip, "NVIDIA DGX Spark (Grace Blackwell GB10)", sizeof info->target_chip - 1); info->sm_architecture = 121; } - if (omega_gpu_session_scratch(&g.a_buf, (size_t)mp * kp * 2u) != 0 || omega_gpu_session_scratch(&g.c_buf, (size_t)mp * np * 4u) != 0) { - omega_gpu_session_unlock(); return OMEGA_GPU_MATMUL_CHIP_FAIL; + int src = omega_gpu_session_scratch(&g.a_buf, (size_t)mp * kp * 2u); + if (src == 0) src = omega_gpu_session_scratch(&g.c_buf, (size_t)mp * np * 4u); + if (src != 0) { + omega_gpu_session_unlock(); + /* a serving reservation refuses growth loudly (TOO_LARGE, error text names the sizes); anything else is a driver failure */ + return src == OMEGA_GPU_SESSION_SCRATCH_RESERVED ? OMEGA_GPU_MATMUL_TOO_LARGE : OMEGA_GPU_MATMUL_CHIP_FAIL; } /* activation rows in, fragment order (omega_matmul_frag_pack_a), zero padded, packed on the * host and copied in order into the uncached buffer; result rows poisoned */ diff --git a/src/omega_gpu_matmul_api.h b/src/omega_gpu_matmul_api.h index ccd5203a2..6466a4198 100644 --- a/src/omega_gpu_matmul_api.h +++ b/src/omega_gpu_matmul_api.h @@ -132,6 +132,18 @@ const char *omega_gpu_matmul_last_error(void); void omega_gpu_matmul_set_cta_budget(uint32_t ctas); uint32_t omega_gpu_matmul_cta_budget(void); +/* OPT-IN serving reservation (omega_gpu_reserve_serving in omega_gpu_serving.h calls this). Allocates the + * activation and result staging buffers once for calls of up to max_rows rows over weights up to max_k x max_n + * (max_n_one_row, 0 = none: a wider n allowed for calls of at most 16 rows, e.g. lm_head), and makes the kernel + * cache kernel_slots deep (0 or <= 8 = unchanged; at most 32). From then on a call that fits never allocates + * staging; a call that does not fit returns OMEGA_GPU_MATMUL_TOO_LARGE with omega_gpu_matmul_last_error() saying + * "serving reservation exceeded", and asks the driver for nothing. Never called = behaviour unchanged. The kernel + * cache still builds each NEW shape once (one small code buffer); a slot count at least the number of distinct + * shapes keeps them for good. 0 on success, else an OMEGA_GPU_MATMUL_* code. */ +int omega_gpu_matmul_reserve(uint32_t max_rows, uint32_t max_k, uint32_t max_n, uint32_t max_n_one_row, uint32_t kernel_slots); +/* Drop the reservation flag (buffers stay allocated, growth is allowed again). */ +void omega_gpu_matmul_unreserve(void); + /* Host oracle on the non-resident calls (default on). */ void omega_gpu_matmul_set_oracle(int on); diff --git a/src/omega_gpu_serving.c b/src/omega_gpu_serving.c new file mode 100644 index 000000000..c35338c06 --- /dev/null +++ b/src/omega_gpu_serving.c @@ -0,0 +1,18 @@ +/* omega_gpu_serving.c: see omega_gpu_serving.h. Thin wrapper over the per-API reserve calls. */ +#include "omega_gpu_serving.h" +#include "omega_gpu_attention_api.h" +#include "omega_gpu_matmul_api.h" + +int omega_gpu_reserve_serving(const OmegaGpuServingBounds *b) { + if (!b) return OMEGA_GPU_MATMUL_BAD_ARGS; + int rc = omega_gpu_attention_reserve(b->max_context, b->max_seqs ? b->max_seqs : 1, b->num_q_heads, b->num_kv_heads, b->head_dim, b->kv_block_size); + if (rc != 0) return rc; + rc = omega_gpu_matmul_reserve(b->max_rows, b->max_k, b->max_n, b->max_n_one_row ? b->max_n_one_row : b->max_n, b->kernel_slots); + if (rc != 0) omega_gpu_attention_unreserve(); /* the matmul half failed: do not leave half a reservation active */ + return rc; +} + +void omega_gpu_serving_release(void) { + omega_gpu_attention_unreserve(); + omega_gpu_matmul_unreserve(); +} diff --git a/src/omega_gpu_serving.h b/src/omega_gpu_serving.h new file mode 100644 index 000000000..adf463a17 --- /dev/null +++ b/src/omega_gpu_serving.h @@ -0,0 +1,45 @@ +/* + * omega_gpu_serving.h: OPT-IN up-front reservation of the GB10 serving buffers (sovereign-core design + * note docs/design/gb10-weight-ownership.md, sc#277). + * + * Without this call nothing changes: the attention staging pool, the matmul activation and result + * buffers and the matmul kernel cache grow on demand, which on the GB10 means one driver allocation + * per new context length and per row-count growth (omega PR #332 measured 458 after warm-up over 428 + * tokens). With it, a daemon calls omega_gpu_reserve_serving once after loading weights, and every + * later call inside the declared bounds reuses the same buffers. A call outside the bounds is refused + * with TOO_LARGE and a clear omega_gpu_*_last_error() text ("serving reservation exceeded: need N + * bytes, reserved M"); it never asks the driver for more memory, so it cannot fault the session. + * + * Not covered (documented follow-ups): the elementwise API's scratch (it stops growing after the + * first full-size call), the attention kernel cache (8 slots, one kernel per model shape), and the + * first build of each distinct matmul shape (one small code buffer per shape, kept for good when + * kernel_slots covers the shapes of the model). + */ +#ifndef OMEGA_GPU_SERVING_H +#define OMEGA_GPU_SERVING_H + +#include <stdint.h> + +typedef struct { + uint32_t max_context; /* longest sequence (tokens) one attention call may cover */ + uint32_t max_seqs; /* sequences per attention call; 0 = 1 */ + uint32_t num_q_heads, num_kv_heads, head_dim; /* the model's attention shape */ + uint32_t kv_block_size; /* paged KV block size in tokens (power of two); 0 = the paged bf16 path is not used */ + uint32_t max_rows; /* most rows one matmul call may carry (prefill chunk) */ + uint32_t max_k; /* widest weight input dimension */ + uint32_t max_n; /* widest weight output dimension for calls of up to max_rows rows */ + uint32_t max_n_one_row; /* widest output dimension for calls of at most 16 rows (lm_head); 0 = max_n */ + uint32_t kernel_slots; /* matmul kernel cache slots; 0 = leave at 8; at most 32. Distinct shapes of one model: ~21 for Qwen3-4B */ +} OmegaGpuServingBounds; + +/* Opens the device if needed. 0 on success, else the first failing OMEGA_GPU_MATMUL_* / ATTN_* code + * (omega_gpu_matmul_last_error() has the text). All-or-nothing: each API rolls back its own buffers when one of its + * allocations fails, and this call rolls back the attention half when the matmul half fails, so a failed reserve + * leaves nothing flagged. Notes: a refused paged/batched attention call may follow earlier launch chunks that already + * wrote their outputs (TOO_LARGE = output incomplete); max_seqs is checked per launch chunk, not per call. */ +int omega_gpu_reserve_serving(const OmegaGpuServingBounds *b); + +/* Lift the reservation: buffers stay, growth is allowed again. (Closing the device also ends it.) */ +void omega_gpu_serving_release(void); + +#endif diff --git a/src/omega_gpu_session.c b/src/omega_gpu_session.c index 6d3fa421d..a8349c655 100644 --- a/src/omega_gpu_session.c +++ b/src/omega_gpu_session.c @@ -95,6 +95,12 @@ void omega_gpu_session_free(NvrmMem *m) { } int omega_gpu_session_scratch(OmegaGpuScratch *s, size_t bytes) { + if (s->reserved && (bytes > s->cap || !s->mem.cpu)) { + char msg[128]; + snprintf(msg, sizeof msg, "serving reservation exceeded: need %zu bytes, reserved %zu", bytes, s->cap); + omega_gpu_session_set_error(msg); + return OMEGA_GPU_SESSION_SCRATCH_RESERVED; + } if (bytes > s->high_water) s->high_water = bytes; if (s->cap >= bytes && s->mem.cpu) return 0; omega_gpu_session_free(&s->mem); @@ -107,8 +113,18 @@ int omega_gpu_session_scratch(OmegaGpuScratch *s, size_t bytes) { void omega_gpu_session_scratch_free(OmegaGpuScratch *s) { omega_gpu_session_free(&s->mem); s->cap = 0; + s->reserved = false; } +int omega_gpu_session_scratch_reserve(OmegaGpuScratch *s, size_t bytes) { + s->reserved = false; /* grow normally first */ + if (omega_gpu_session_scratch(s, bytes) != 0) return -1; + s->reserved = true; + return 0; +} + +void omega_gpu_session_scratch_unreserve(OmegaGpuScratch *s) { s->reserved = false; } + static void free_session_buffers(void) { omega_gpu_session_free(&g_s.qmd); omega_gpu_session_free(&g_s.marker); omega_gpu_session_free(&g_s.cbank); omega_gpu_session_free(&g_s.pb); diff --git a/src/omega_gpu_session.h b/src/omega_gpu_session.h index e7953b757..d77b33f0e 100644 --- a/src/omega_gpu_session.h +++ b/src/omega_gpu_session.h @@ -58,6 +58,7 @@ typedef struct { NvrmMem mem; size_t cap; /* usable bytes */ size_t high_water; /* largest request so far */ + bool reserved; /* serving reservation active: a request past cap is refused, never allocated (omega_gpu_session_scratch_reserve) */ } OmegaGpuScratch; /* One launch: a kernel already resident at code_va, the argument words for @@ -107,6 +108,13 @@ void omega_gpu_session_free(NvrmMem *m); /* no-op when latched or closed; zeroes /* Caller holds the lock, device open. Makes s->mem at least `bytes` long * (reallocated when it grows; contents are not kept). */ int omega_gpu_session_scratch(OmegaGpuScratch *s, size_t bytes); +/* Caller holds the lock, device open. Allocates s->mem for `bytes` now and marks it reserved: from then on + * omega_gpu_session_scratch(s, n) with n <= the reserved capacity never touches the driver, and with n past it + * returns OMEGA_GPU_SESSION_SCRATCH_RESERVED (-2) with a clear error and no allocation. 0 on success, -1 on + * allocation failure (not reserved then). Cleared by omega_gpu_session_scratch_free (device close) or _unreserve. */ +#define OMEGA_GPU_SESSION_SCRATCH_RESERVED (-2) +int omega_gpu_session_scratch_reserve(OmegaGpuScratch *s, size_t bytes); +void omega_gpu_session_scratch_unreserve(OmegaGpuScratch *s); void omega_gpu_session_scratch_free(OmegaGpuScratch *s); /* Caller holds the lock, device open, not latched. Submits one launch and waits diff --git a/tests/.crumb b/tests/.crumb index ea213c878..65c798564 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -284,14 +284,14 @@ } ], "children_root": "sha256:e4b130835499158755721627147fd30866f0679d7cdbc125bec754e9c0a94396", - "compiled_at": "2026-10-07T21:46:01Z", + "compiled_at": "2026-10-07T22:12:35Z", "compiler": "crumb-compile/1", - "digest": "sha256:240bd58ed4724aab6e803da398cfc13b84108ce1b041ddb67dd0ff07f9657e55", + "digest": "sha256:daa6072bbb4f876d9e0e0a8cc88c56e1fe98aad01a4b9b18bfc8cea84845f7f0", "evidence_files": 11, "evidence_root": "sha256:d5c1bceb4d57ef0eb42797c58801b5926509711614c3e38bb7105b6efa3942f2", "files": 819, - "generated_at_commit": "78d719dda6b72ff28e3f03f9256f4796cbe8cd1f", - "source_tree": "sha256:aec1271c353ef0760180402bc39cec4c3f00209aeb9e408aee856360a48ca83e" + "generated_at_commit": "884360a67256afc14fa6e6035292ad609fab337f", + "source_tree": "sha256:468b08b466d9d007c04db67cdc70329c1dc6b17fc06b5e8dd1291c570ad7f21f" }, "owner_lane": "", "provenance": { diff --git a/tests/gpu_serving_alloc_count_test.c b/tests/gpu_serving_alloc_count_test.c index 7755cbb47..b5dbbe285 100644 --- a/tests/gpu_serving_alloc_count_test.c +++ b/tests/gpu_serving_alloc_count_test.c @@ -36,6 +36,7 @@ #include "fake_m16_native.h" #include "omega_gpu_attention_api.h" #include "omega_gpu_matmul_api.h" +#include "omega_gpu_serving.h" #include "omega_gpu_session.h" /* The two driver symbols the session needs that the fake (written for the vector engine) lacks. */ @@ -177,7 +178,149 @@ static void end(Phase *p, int sized) { CHECK(a1 <= FK_SIZE_LOG && f1 <= FK_SIZE_LOG, "size log overflow (%d allocs, %d frees)", a1, f1); } -int main(void) { +static const OmegaGpuServingBounds QWEN3_4B_BOUNDS = { + .max_context = 4096, .max_seqs = 1, .num_q_heads = QH, .num_kv_heads = KVH, .head_dim = HD, .kv_block_size = 16, + .max_rows = CHUNK, .max_k = INTER, .max_n = INTER, .max_n_one_row = VOCAB, .kernel_slots = 32, +}; + +/* Bounds mode: a tiny reservation, then calls inside it (no driver traffic) and past it (refused loudly). */ +static int paged_call(uint32_t bs, uint32_t ctx); +static int attn_ran(int rc); + +static int bounds_mode(void) { + OmegaGpuServingBounds b = QWEN3_4B_BOUNDS; + b.max_context = 256; b.max_rows = 32; b.max_k = HID; b.max_n = QDIM; b.max_n_one_row = 0; + Phase ph; begin(&ph, "BOUNDS"); /* sets P for mm()/attn() */ + W_Q = upload(HID, QDIM); + attn(1); mm(1, W_Q, g_y); /* warm kernels at small size */ + CHECK(omega_gpu_reserve_serving(&b) == 0, "reserve failed: %s", omega_gpu_matmul_last_error()); + 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) */ + const int a0 = fk.alloc_calls, f0 = fk.free_calls; + mm(1, W_Q, g_y); mm(17, W_Q, g_y); mm(32, W_Q, g_y); attn(1); attn(100); attn(256); + 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); + OmegaGpuMatmulInfo info; + int rc = omega_gpu_matmul_resident_f32(33, g_x, W_Q, g_y, &info); /* 33 rows -> 48 padded: past 32 */ + CHECK(rc == OMEGA_GPU_MATMUL_TOO_LARGE, "33 rows past a 32-row reservation: rc=%d (%s)", rc, omega_gpu_matmul_rc_name(rc)); + CHECK(strstr(omega_gpu_matmul_last_error(), "serving reservation exceeded") != NULL, "matmul error text: \"%s\"", omega_gpu_matmul_last_error()); + rc = omega_gpu_gqa_attention_f32(g_q, g_k, g_v, 257, QH, KVH, HD, g_attn, NULL); + CHECK(rc == OMEGA_GPU_ATTN_TOO_LARGE, "context 257 past a 256 reservation: rc=%d (%s)", rc, omega_gpu_attention_rc_name(rc)); + CHECK(strstr(omega_gpu_attention_last_error(), "serving reservation exceeded") != NULL, "attention error text: \"%s\"", omega_gpu_attention_last_error()); + 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); + CHECK(!omega_gpu_matmul_is_blocked() && !omega_gpu_session_is_blocked(), "a refusal latched the session"); + mm(32, W_Q, g_y); attn(256); /* still serves after a refusal */ + CHECK(fk.alloc_calls == a0, "serving after a refusal allocated"); + /* paged bf16 attention (the sovereign-core path): 512 tokens = 32 blocks of 16 = exactly the reserved bytes */ + (void)paged_call(16, 16); /* build the paged kernel once */ + const int p0 = fk.alloc_calls, pf0 = fk.free_calls; + rc = paged_call(16, 16); CHECK(attn_ran(rc), "paged 16 tokens in bounds: rc=%d (%s)", rc, omega_gpu_attention_last_error()); + 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()); + 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); + rc = paged_call(16, 513); CHECK(rc == OMEGA_GPU_ATTN_TOO_LARGE, "paged 513 tokens (bound+1): rc=%d", rc); + CHECK(strstr(omega_gpu_attention_last_error(), "serving reservation exceeded") != NULL, "paged error text: \"%s\"", omega_gpu_attention_last_error()); + CHECK(fk.alloc_calls == p0 && fk.free_calls == pf0, "a refused paged call touched the driver"); + omega_gpu_serving_release(); /* lifted: growth is allowed again */ + rc = omega_gpu_gqa_attention_f32(g_q, g_k, g_v, 257, QH, KVH, HD, g_attn, NULL); + CHECK(rc == OMEGA_GPU_ATTN_OK || rc == OMEGA_GPU_ATTN_UNWRITTEN, "after release, context 257 rc=%d", rc); + CHECK(fk.alloc_calls > a0, "after release, growth did not allocate"); + printf("MEASURE bounds: refused calls made 0 driver allocations; 33 rows -> TOO_LARGE, ctx 257 -> TOO_LARGE; error \"%s\"\n", "serving reservation exceeded: ..."); + omega_gpu_tensor_free(W_Q); + omega_gpu_device_close(); + CHECK(fk.live_at_close == 0, "leak: %d", fk.live_at_close); + printf("gpu_serving_alloc_count_test bounds: %s (%d failed checks)\n", g_fail ? "FAIL" : "PASS", g_fail); + return g_fail ? 1 : 0; +} + +/* Paged bf16 attention call over `ctx` tokens of a one-layer pool of `bs`-token blocks (the path sovereign-core uses). */ +static int paged_call(uint32_t bs, uint32_t ctx) { + const uint32_t nb = (ctx + bs - 1) / bs; + OmegaGpuKvLayout ly; + ly.head_stride_bytes = (uint64_t)HD * 2; ly.token_stride_bytes = ly.head_stride_bytes * KVH; + ly.kv_plane_stride_bytes = ly.token_stride_bytes * bs; ly.layer_stride_bytes = 2 * ly.kv_plane_stride_bytes; + ly.block_stride_bytes = ly.layer_stride_bytes; ly.pool_bytes = ly.block_stride_bytes * nb; + ly.num_blocks = nb; ly.num_layers = 1; ly.block_size = bs; + uint8_t *pool = calloc(1, (size_t)ly.pool_bytes); + uint32_t *ids = calloc(nb, sizeof *ids); + CHECK(pool && ids, "paged host buffers"); + int rc = OMEGA_GPU_ATTN_BAD_ARGS; + if (pool && ids) { + for (uint32_t i = 0; i < nb; i++) ids[i] = i; + rc = omega_gpu_paged_attention_bf16(g_q, pool, &ly, ids, nb, ctx, 0, QH, KVH, HD, g_attn, NULL); + } + free(pool); free(ids); + return rc; +} +static int attn_ran(int rc) { return rc == OMEGA_GPU_ATTN_OK || rc == OMEGA_GPU_ATTN_UNWRITTEN; } + +/* Under half a block of context: the paged path stages a whole block, more than the f32 formula (review item 1). */ +static int bounds_block_mode(void) { + OmegaGpuServingBounds b = QWEN3_4B_BOUNDS; + 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; + Phase ph; begin(&ph, "BOUNDS-BLOCK"); + W_Q = upload(HID, QDIM); + CHECK(omega_gpu_reserve_serving(&b) == 0, "reserve failed: %s", omega_gpu_matmul_last_error()); + 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 */ + CHECK(attn_ran(rc), "paged call of 16 tokens in one 64-token block refused: rc=%d (%s)", rc, omega_gpu_attention_last_error()); + const int a0 = fk.alloc_calls, f0 = fk.free_calls; + rc = paged_call(64, 64); + CHECK(attn_ran(rc), "paged call of exactly one block (the byte bound) refused: rc=%d (%s)", rc, omega_gpu_attention_last_error()); + CHECK(fk.alloc_calls == a0 && fk.free_calls == f0, "in-bounds paged calls touched the driver"); + rc = paged_call(64, 65); + CHECK(rc == OMEGA_GPU_ATTN_TOO_LARGE, "two blocks past a one-block reservation: rc=%d", rc); + CHECK(fk.alloc_calls == a0 && fk.free_calls == f0, "refused paged call touched the driver"); + printf("MEASURE bounds-block: 16 and 64 tokens (one 64-token block) served with 0 driver traffic; 65 tokens -> TOO_LARGE\n"); + omega_gpu_tensor_free(W_Q); + omega_gpu_device_close(); + CHECK(fk.live_at_close == 0, "leak: %d", fk.live_at_close); + printf("gpu_serving_alloc_count_test bounds-block: %s (%d failed checks)\n", g_fail ? "FAIL" : "PASS", g_fail); + return g_fail ? 1 : 0; +} + +/* A reserve that fails part way must leave nothing flagged (review item 2). Sweep the failing driver request over every + * allocation the reserve makes (pool, q, out, table, activation, result), on a freshly opened device each time; the + * control (no injected failure) must leave the reservation active. */ +static int rollback_mode(void) { + OmegaGpuServingBounds b = QWEN3_4B_BOUNDS; + 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; + Phase ph; begin(&ph, "ROLLBACK"); + int failed_reserves = 0; + /* n = 100 first: the un-injected control, before the probes of the failing runs raise the buffers' high-water marks */ + const int sweep[] = { 100, 1, 2, 3, 4, 5, 6 }; + for (unsigned si = 0; si < sizeof sweep / sizeof sweep[0]; si++) { + const int n = sweep[si]; + W_Q = upload(HID, QDIM); /* opens the session (its own allocations come first) */ + fk.alloc_mode = FK_ALLOC_FAIL; + fk.alloc_fail_nth = fk.alloc_calls + n; + int rc = omega_gpu_reserve_serving(&b); + fk.alloc_fail_nth = 0; + /* probes that need more than the reservation: refused only if something is still flagged */ + /* 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 */ + int ra = omega_gpu_gqa_attention_f32(g_q, g_k, g_v, si ? 300 + 500 * si : 257, QH, KVH, HD, g_attn, NULL); + int refused_attn = ra == OMEGA_GPU_ATTN_TOO_LARGE && strstr(omega_gpu_attention_last_error(), "serving reservation exceeded") != NULL; + OmegaGpuMatmulInfo info; + int rm = omega_gpu_matmul_resident_f32(si ? 32 + 16 * si : 48, g_x, W_Q, g_y, &info); + int refused_mm = rm == OMEGA_GPU_MATMUL_TOO_LARGE && strstr(omega_gpu_matmul_last_error(), "serving reservation exceeded") != NULL; + if (rc != 0) { + failed_reserves++; + 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); + } else { + 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); + } + omega_gpu_tensor_free(W_Q); + omega_gpu_device_close(); + } + CHECK(failed_reserves >= 6, "only %d of the sweep's reserves failed (expected the 6 buffer requests)", failed_reserves); + CHECK(fk.live_at_close == 0, "leak: %d", fk.live_at_close); + printf("MEASURE rollback: %d injected failures during reserve, none left anything flagged; the un-injected control stayed reserved\n", failed_reserves); + printf("gpu_serving_alloc_count_test rollback: %s (%d failed checks)\n", g_fail ? "FAIL" : "PASS", g_fail); + return g_fail ? 1 : 0; +} + +/* mode: "plain" (default) = today's measurement, no reservation; "reserve" = reserve after warm-up and + * ASSERT zero driver traffic while serving; "reserve-skip" = the same asserts without the call (must FAIL: the red run); + * "bounds" = refusal behaviour. */ +int main(int argc, char **argv) { + const char *mode = argc > 1 ? argv[1] : "plain"; + const int do_reserve = strcmp(mode, "reserve") == 0, assert_zero = do_reserve || strcmp(mode, "reserve-skip") == 0; fake_reset(); fk.kernel_mode = FK_KERNEL_SKIP; fk.alloc_max = 1ull << 31; @@ -191,8 +334,11 @@ int main(void) { CHECK(g_x && g_y && g_logits && g_k && g_v, "host buffers"); if (g_fail) return 1; - Phase ph[8]; + Phase ph[9]; int np = 0; + if (strcmp(mode, "bounds") == 0) return bounds_mode(); + if (strcmp(mode, "bounds-block") == 0) return bounds_block_mode(); + if (strcmp(mode, "rollback") == 0) return rollback_mode(); begin(&ph[np], "LOAD (session+6 weights)"); W_Q = upload(HID, QDIM); W_K = upload(HID, KVDIM); W_O = upload(QDIM, HID); @@ -204,6 +350,14 @@ int main(void) { begin(&ph[np], "WARMUP decode ctx=1"); step(1, 0); end(&ph[np], 1); np++; + if (assert_zero) { + /* a daemon's startup: reserve once after loading and a first request, then cover each row-count class + * (1 decode row, a full chunk, the 100 and 72 row remainders) so every matmul shape has been built once */ + if (do_reserve) CHECK(omega_gpu_reserve_serving(&QWEN3_4B_BOUNDS) == 0, "reserve failed: %s", omega_gpu_matmul_last_error()); + begin(&ph[np], "WARMUP row classes"); + step(128, 0); step(100, 0); step(72, 0); step(1, 0); + end(&ph[np], 0); np++; + } const int after_warm_allocs = fk.alloc_calls, after_warm_frees = fk.free_calls; uint32_t ctx = 0; @@ -230,7 +384,7 @@ int main(void) { const int serve_allocs = fk.alloc_calls - after_warm_allocs, serve_frees = fk.free_calls - after_warm_frees; printf("MEASURE AFTER WARM-UP: %d driver allocations, %d driver frees over %u tokens of context (final ctx %u)\n", serve_allocs, serve_frees, ctx, ctx); - printf("MEASURE distinct matmul kernel shapes seen (kp,np,grid_x): %d (cache slots: 8)\n", g_nseen); + printf("MEASURE distinct matmul kernel shapes seen (kp,np,grid_x): %d (cache slots: %d)\n", g_nseen, do_reserve ? 32 : 8); printf("MEASURE live driver buffers at end: %d ; session opens: %u ; launches reaching the fake: %d\n", fk.live, omega_gpu_session_open_count(), fk.kernel_ran); printf("MEASURE return codes: matmul ok %d, matmul poison-check CHIP_FAIL %d, attention ok %d, attention UNWRITTEN %d, other %d\n", @@ -241,9 +395,14 @@ int main(void) { CHECK(!omega_gpu_matmul_is_blocked() && !omega_gpu_session_is_blocked(), "session latched"); CHECK(fk.double_free == 0, "double free %d", fk.double_free); CHECK(fk.pb_parse_error == 0, "pushbuffer parse error"); - CHECK(fk.kernel_ran == ph[1].mm_calls + ph[1].at_calls + ph[2].mm_calls + ph[2].at_calls + ph[3].mm_calls + ph[3].at_calls + - ph[4].mm_calls + ph[4].at_calls + ph[5].mm_calls + ph[5].at_calls, - "launches %d != calls made", fk.kernel_ran); + int calls_made = 0; + for (int i = 1; i < np; i++) calls_made += ph[i].mm_calls + ph[i].at_calls; + CHECK(fk.kernel_ran == calls_made, "launches %d != calls made %d", fk.kernel_ran, calls_made); + if (assert_zero) { + /* the point of the reservation: nothing reached the driver while serving inside the bounds */ + 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); + 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); + } CHECK(fk.live < FK_LIVE_MAX, "fake live table full (%d)", fk.live); /* every real allocation is either live or was freed */ CHECK(fk.alloc_calls - fk.free_calls >= fk.live, "alloc/free/live inconsistent: %d - %d < %d", fk.alloc_calls, fk.free_calls, fk.live); @@ -255,6 +414,6 @@ int main(void) { CHECK(fk.live_at_close == 0, "leak: %d driver buffers live at close", fk.live_at_close); printf("MEASURE leaked at close: %d\n", fk.live_at_close); - printf("gpu_serving_alloc_count_test: %s (%d failed checks)\n", g_fail ? "FAIL" : "PASS", g_fail); + printf("gpu_serving_alloc_count_test %s: %s (%d failed checks)\n", mode, g_fail ? "FAIL" : "PASS", g_fail); return g_fail ? 1 : 0; }