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#338 (code only)
Apply it
git checkout -b patch-212b9c3eb80634ca 9e168fc8be6932ed2b79206fbb911de1722ff2c2 curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/212b9c3eb80634ca.patch | git am
Provenance
github-import 1
aien-dev/omega-mv
338
3b11eb0d656efcbfe6a30819314518422f990287
{"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":338,"url":"https://github.com/aien-dev/omega-mv/pull/338","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:37:24Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/.crumb b/.crumb index bf168df2b..19456ce73 100644 --- a/.crumb +++ b/.crumb @@ -63,8 +63,8 @@ "purpose": "Directory doctrine. Purpose not yet described by a human." }, { - "digest": "b0f9da3c4a7b14bd483ce7cb63016880c21d47e393aa997e01f1c674c4b9753c", - "last_commit": "b980783", + "digest": "4b41ef787de24514f7a08ef4f207378e5039013e5795f1fa729f370c57bb94e2", + "last_commit": "bc9fa71", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, @@ -82,19 +82,19 @@ }, { "digest": "830ce7faaea9ae86190aa304fd8d7797881fbaca9d14fc39a327d202712f73fb", - "last_commit": "97ee275", + "last_commit": "9e168fc", "name": "spec", "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "d33d643a490c998fc92cc0df349b8df0f53fe4b76a175d10ed5d776d5f176f06", - "last_commit": "b980783", + "digest": "35b23efce57f3889c81983c8e82a3645b13d3ac35146151b5c52d9a3bbdd4618", + "last_commit": "bc9fa71", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "daa6072bbb4f876d9e0e0a8cc88c56e1fe98aad01a4b9b18bfc8cea84845f7f0", - "last_commit": "b980783", + "digest": "8d75800f19cbf387d4f5cfb71bdff1801719421f07bc378c89be5dd5e3578bbb", + "last_commit": "bc9fa71", "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:90bc8bc96331de3968b9f6570a7b1ceef6c0d1c5c3c12a6c3ddc436f5929a682", - "compiled_at": "2026-10-08T00:23:24Z", + "children_root": "sha256:730c96a909d8874433ae79e2114eaac0b0de5eaa93c8fb4943e3e6a2096cc11b", + "compiled_at": "2026-10-08T02:13:11Z", "compiler": "crumb-compile/1", - "digest": "sha256:da2e64dbc4db3aa66ee22f0e4b9ff5ef42e894dea3efdc96d4538ee2932716b4", + "digest": "sha256:8210e26b9f4070bf5aa941d4d494b39eb0b3df5c75cfc3eb790233605475a886", "evidence_files": 4818, "evidence_root": "sha256:07642731585ad396c5d9b566a4b8df7807018f494d322dffce41bd04fb51e553", "files": 6661, - "generated_at_commit": "b980783a4674275e720592e4c3887679dd24ec8b", - "source_tree": "sha256:2de937d709d51ad0e11b31fb8402c72436c13019b49ec8a7efc1e51514bb24fc" + "generated_at_commit": "bc9fa7169bd871651675ec51eb78b629cab4057e", + "source_tree": "sha256:fc67770222232b538d4ee07c39df18b10fa1b2c9a2ca0e5d6d4799ce9fb58b69" }, "owner_lane": "", "provenance": { diff --git a/Makefile b/Makefile index 062269a0b..fa57a6c5b 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ SRCS = src/sha256.c src/omega_canonical.c src/omega_validate.c src/omega_core.c src/omega_library.c src/omega_discovery.c src/omega_machine.c src/omega_realize_synth.c \ src/omega_matvec.c src/omega_accelerator.c src/omega_accelerator_world.c \ src/omega_vector.c src/omega_blackwell_encoder.c src/omega_blackwell_qmd.c \ - src/omega_blackwell_realize.c src/omega_blackwell_submit.c src/omega_blackwell_engine.c src/omega_gpu_engine.c src/omega_blackwell_gates.c src/omega_blackwell_matmul.c src/omega_blackwell_codegen.c src/omega_gpu_session.c src/omega_gpu_matmul_api.c src/omega_gpu_elementwise_api.c src/omega_gpu_attention_api.c src/omega_world_gates.c src/omega_gpu_wait.c \ + src/omega_blackwell_realize.c src/omega_blackwell_submit.c src/omega_blackwell_engine.c src/omega_gpu_engine.c src/omega_blackwell_gates.c src/omega_blackwell_matmul.c src/omega_blackwell_codegen.c src/omega_gpu_session.c src/omega_gpu_matmul_api.c src/omega_gpu_elementwise_api.c src/omega_gpu_attention_api.c src/omega_gpu_serving.c src/omega_world_gates.c src/omega_gpu_wait.c \ src/omega_evidence.c \ $(PHYSICS_DIR)/m16/m16_native.c $(PHYSICS_DIR)/nvrm/nvrm.c \ tools/omegatool.c diff --git a/mk/.crumb b/mk/.crumb index 21075475e..72199631c 100644 --- a/mk/.crumb +++ b/mk/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-07T22:12:35Z", + "compiled_at": "2026-10-08T02:13:11Z", "compiler": "crumb-compile/1", - "digest": "sha256:b0f9da3c4a7b14bd483ce7cb63016880c21d47e393aa997e01f1c674c4b9753c", + "digest": "sha256:4b41ef787de24514f7a08ef4f207378e5039013e5795f1fa729f370c57bb94e2", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 60, - "generated_at_commit": "884360a67256afc14fa6e6035292ad609fab337f", - "source_tree": "sha256:d703768e49a40d47819f67b982d42207c3b70a7b8b3faa8e2df0796f5235d396" + "generated_at_commit": "bc9fa7169bd871651675ec51eb78b629cab4057e", + "source_tree": "sha256:82b49d6f0ebab8f76d9bd32474899094c18ebfd6aa83123be5983fc23d4e2168" }, "owner_lane": "", "provenance": { diff --git a/mk/omega-gpu-engine.mk b/mk/omega-gpu-engine.mk index 62d4fa826..9d9420c17 100644 --- a/mk/omega-gpu-engine.mk +++ b/mk/omega-gpu-engine.mk @@ -88,6 +88,12 @@ test-gpu-serving-alloc: build/gpu_serving_alloc_count_test ./build/gpu_serving_alloc_count_test bounds ./build/gpu_serving_alloc_count_test bounds-block ./build/gpu_serving_alloc_count_test rollback + ./build/gpu_serving_alloc_count_test sweep + ./build/gpu_serving_alloc_count_test pin + ./build/gpu_serving_alloc_count_test seal-bare + @if ./build/gpu_serving_alloc_count_test sweep-skip >build/gpu_serving_alloc_count_sweep_skip.log 2>&1; then echo "RED RUN PASSED: zero allocations over the row sweep do not need prepare + seal"; exit 1; fi + @if [ "$$(grep -c "^FAIL" build/gpu_serving_alloc_count_sweep_skip.log)" -lt 1 ]; then echo "sweep-skip exited nonzero without a FAIL check (crash, not the red assertion)"; exit 1; fi + @grep -E "^MEASURE sweep" build/gpu_serving_alloc_count_sweep_skip.log | sed "s/^/sweep-skip (control): /" @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 49e958ab8..77c04a3ac 100644 --- a/src/.crumb +++ b/src/.crumb @@ -62,73 +62,73 @@ "children": [ { "digest": "e999ad5d79b28e45cf3c87fca942087088f243bdfd0e55da7948eb5d6e619460", - "last_commit": "c0369e6", + "last_commit": "7350a61", "name": "algebra", "purpose": "Directory src/algebra. Purpose not yet described by a human." }, { "digest": "24b48531671e1ad40c520571c43aa6fa8dfe43ce7139a5724f31afa9f8411afd", - "last_commit": "c0369e6", + "last_commit": "834ee93", "name": "ans", "purpose": "ANS (Alignment Navigation System), Inertial Alignment v1: frozen reference atoms, drift filter o" }, { "digest": "5b110f4a6136ebd3d6703590962b7beed9f8cec034c2d12944b1590434e3c747", - "last_commit": "c0369e6", + "last_commit": "d48ab89", "name": "autodiff", "purpose": "M21 OMEGA_AUTODIFF: reverse-mode tape over M20 tensor CPU ops, caller-owned nodes and scratch, p" }, { "digest": "ec640bd4bee9e58c18ca807acae07111ce4e980306fbb6c34f18110eed830b3a", - "last_commit": "c0369e6", + "last_commit": "dce821c", "name": "compiler", "purpose": "Directory src/compiler. Purpose not yet described by a human." }, { "digest": "41faf68aa92b4f16b40243239e1866110a443ce284ff9f7ec3b125b289bcd845", - "last_commit": "c0369e6", + "last_commit": "3c82389", "name": "crumbline", "purpose": "Directory src/crumbline. Purpose not yet described by a human." }, { "digest": "945ae745a338467614bee287efb628f38f53ad83e4842c1a7fbf7d0b91a8da57", - "last_commit": "c0369e6", + "last_commit": "00f54b6", "name": "dual_experiment", "purpose": "DUAL applicability experiment library (ARCH-0031 offline evidence lane): deterministic discrete " }, { "digest": "62a0884e7f075eb3c92b22fc400234a06bd8c3020fc75e55a9b0c0f0f6a26d7e", - "last_commit": "c0369e6", + "last_commit": "1a147e4", "name": "estimation", "purpose": "Directory src/estimation. Purpose not yet described by a human." }, { "digest": "330c407ff51bfd684e062707c055f58f325f5af16fe9764889aec3a7ffeb31ac", - "last_commit": "c0369e6", + "last_commit": "3108fc2", "name": "fabric", "purpose": "Directory src/fabric. Purpose not yet described by a human." }, { "digest": "3bdcb211108a4fdc33550b2c54df3167dd15d4a5fe004eefbcef11b489d0e27a", - "last_commit": "c0369e6", + "last_commit": "71c6ff8", "name": "language", "purpose": "Directory src/language. Purpose not yet described by a human." }, { "digest": "8383106b5d1b3adbc44b8e837a5d1f5ff6699a6d54070068e36716b2a32390d5", - "last_commit": "c0369e6", + "last_commit": "d550b70", "name": "path", "purpose": "Directory src/path. Purpose not yet described by a human." }, { "digest": "6974fb2a6051d3a1b07153ced8cddc6a81d04a79d229b5c9a18e00f7d2c55d25", - "last_commit": "c0369e6", + "last_commit": "965edfb", "name": "physics0", "purpose": "PD-0 hidden-equation benchmark (Physics-0 Discovery Engine cut 1): world, oracle, scorer, ladder" }, { "digest": "c43b60db4a5183a3ca59c2e24a0978f463e8ac3aa7a2ed0c54947eb085c4eb9e", - "last_commit": "c0369e6", + "last_commit": "3ef4f69", "name": "polyglot", "purpose": "Directory src/polyglot. Purpose not yet described by a human." }, @@ -140,44 +140,44 @@ }, { "digest": "9f6d98a6d9b0020aa56df481a5ab3aa301533cb85173f96a851d1d779fca2d3a", - "last_commit": "c0369e6", + "last_commit": "5ae192a", "name": "searchtrace", "purpose": "Directory src/searchtrace. Purpose not yet described by a human." }, { "digest": "ae63793b7b13442e349e438229fccf6a859a5be55343cdd997e35f2966c1157c", - "last_commit": "c0369e6", + "last_commit": "f633e34", "name": "tensor", "purpose": "M20 OMEGA_TENSOR: immutable tensor semantic layer, views, generation-checked storage, CPU realiz" }, { "digest": "be7e624435df81eae403f51a381acf480e9fd2f2a04eb34e955547973645b95d", - "last_commit": "c0369e6", + "last_commit": "54826a3", "name": "train", "purpose": "Directory src/train. Purpose not yet described by a human." }, { "digest": "78c180a66607ebaf93e6ef6a6a56a3059ebf97db394d76af50c0e3f25d2be639", - "last_commit": "c0369e6", + "last_commit": "2564f57", "name": "turing", "purpose": "Directory src/turing. Purpose not yet described by a human." }, { "digest": "daf5e5e90c1ccb4c089220a6545844dbe4078a233f1777e8145b74f1bf5dd5fe", - "last_commit": "c0369e6", + "last_commit": "40d1ea3", "name": "visor", "purpose": "Directory src/visor. Purpose not yet described by a human." } ], "children_root": "sha256:00102626b51610209ee39290ee9df5d653441f43b09872a1b21388f881cc5500", - "compiled_at": "2026-10-07T22:12:35Z", + "compiled_at": "2026-10-08T02:13:11Z", "compiler": "crumb-compile/1", - "digest": "sha256:d33d643a490c998fc92cc0df349b8df0f53fe4b76a175d10ed5d776d5f176f06", + "digest": "sha256:35b23efce57f3889c81983c8e82a3645b13d3ac35146151b5c52d9a3bbdd4618", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 414, - "generated_at_commit": "884360a67256afc14fa6e6035292ad609fab337f", - "source_tree": "sha256:6e4bd60b8c7c1f395c9438c3a4e44fce1efae27b1ca35d2f443f4e5fbe3b4882" + "generated_at_commit": "bc9fa7169bd871651675ec51eb78b629cab4057e", + "source_tree": "sha256:01b756b614c7412be581f29dc582c2443a92416c45ca984a1619650995a3fd0a" }, "owner_lane": "", "provenance": { diff --git a/src/omega_gpu_matmul_api.c b/src/omega_gpu_matmul_api.c index 6b1e6f09f..e356e005e 100644 --- a/src/omega_gpu_matmul_api.c +++ b/src/omega_gpu_matmul_api.c @@ -22,11 +22,12 @@ #include "nvrm.h" #include <math.h> #include <stdlib.h> +#include <stdio.h> #include <string.h> #include <time.h> #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 CACHE_SLOTS_MAX 128 /* storage for the opt-in larger cache (a code buffer is a few KB); Qwen3-4B serving needs 95 */ #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) @@ -39,6 +40,7 @@ struct OmegaGpuTensor { typedef struct { bool valid; + bool pinned; /* built by omega_gpu_matmul_prepare: never evicted */ uint32_t kp, np, grid_x; int mutant; OmegaMatMulSpec spec; @@ -55,6 +57,7 @@ static struct { CacheSlot cache[CACHE_SLOTS_MAX]; unsigned cache_next; unsigned slots; /* active kernel cache slots, <= CACHE_SLOTS_MAX */ + int sealed; /* omega_gpu_matmul_seal: a kernel cache miss is refused, never built */ } g = { .cta_budget = OMEGA_GPU_MATMUL_MAX_CTAS, .oracle = 1, .slots = CACHE_SLOTS }; #define FAILAT(s) (omega_gpu_session_set_error(s), 1) @@ -101,6 +104,7 @@ static void on_session_close(void) { 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 */ + g.sealed = 0; } static OmegaGpuSession *dev_open_locked(void) { @@ -163,6 +167,39 @@ void omega_gpu_matmul_unreserve(void) { omega_gpu_session_unlock(); } +static int kernel_for(uint32_t kp, uint32_t np, uint32_t grid_x, CacheSlot **out, bool *hit); +static void launch_shape(uint32_t mp, uint32_t np, uint32_t *rows_per_launch, uint32_t *grid_x); + +int omega_gpu_matmul_prepare(uint32_t m, uint32_t k, uint32_t n) { + if (m == 0 || k == 0 || n == 0) { omega_gpu_session_set_error("matmul prepare: zero dimension"); return OMEGA_GPU_MATMUL_BAD_ARGS; } + if (m > OMEGA_BW_MATMUL_MAX_M || k > OMEGA_BW_MATMUL_MAX_K || n > OMEGA_BW_MATMUL_MAX_N) { + omega_gpu_session_set_error("matmul prepare: shape past the matmul envelope"); + return OMEGA_GPU_MATMUL_TOO_LARGE; + } + const uint32_t mp = round_up(m, OMEGA_GPU_MATMUL_TILE_M), kp = round_up(k, OMEGA_GPU_MATMUL_TILE_K), np = round_up(n, OMEGA_GPU_MATMUL_TILE_N); + uint32_t rows_per_launch, grid_x; + omega_gpu_session_lock(); + int rc = OMEGA_GPU_MATMUL_OK; + if (!dev_open_locked()) rc = OMEGA_GPU_MATMUL_CHIP_FAIL; + else { + launch_shape(mp, np, &rows_per_launch, &grid_x); + CacheSlot *s = NULL; bool hit = false; + rc = kernel_for(kp, np, grid_x, &s, &hit); + if (rc == OMEGA_GPU_MATMUL_OK) s->pinned = true; /* a prepared kernel is never evicted */ + } + omega_gpu_session_unlock(); + return rc; +} + +/* Anything that clears the kernel cache while sealed (cache_clear, set_cta_budget, the test knobs, a + * reserve that grows the slot count) also drops the pinned kernels; the seal stays, so every later + * call is refused by name ("was not prepared") until the server prepares again or releases. */ +void omega_gpu_matmul_seal(int on) { + omega_gpu_session_lock(); + g.sealed = on ? 1 : 0; + 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) { @@ -179,6 +216,18 @@ void omega_gpu_matmul_test_set_unroll(uint32_t unroll) { omega_gpu_session_unlock(); } +/* ---- one launch shape (the kernel cache key bakes in grid_x) ---- */ + +/* rows per launch: all of them unless the row tiles alone exceed the CTA budget; grid_x fills the rest of the budget */ +static void launch_shape(uint32_t mp, uint32_t np, uint32_t *rows_per_launch, uint32_t *grid_x) { + const uint32_t budget = g.cta_budget ? g.cta_budget : OMEGA_GPU_MATMUL_MAX_CTAS, mt = mp / 16u, nt = np / 8u; + *rows_per_launch = mt <= budget ? mp : budget * 16u; + uint32_t gx = budget / (*rows_per_launch / 16u); + if (gx == 0) gx = 1; + if (gx > nt) gx = nt; + *grid_x = gx; +} + /* ---- 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) { @@ -189,6 +238,22 @@ static int kernel_for(uint32_t kp, uint32_t np, uint32_t grid_x, CacheSlot **out return OMEGA_GPU_MATMUL_OK; } } + if (g.sealed) { + char e[160]; + snprintf(e, sizeof e, "serving reservation exceeded: matmul kernel kp=%u np=%u grid_x=%u was not prepared", kp, np, grid_x); + omega_gpu_session_set_error(e); + return OMEGA_GPU_MATMUL_TOO_LARGE; + } + /* choose the slot before any build or driver call: round-robin, skipping prepared (pinned) kernels; all pinned = refused, nothing evicted */ + CacheSlot *s = NULL; + for (unsigned i = 0; i < g.slots && !s; i++) { + CacheSlot *c = &g.cache[(g.cache_next + i) % g.slots]; + if (!c->valid || !c->pinned) { s = c; g.cache_next = (unsigned)((c - g.cache) + 1) % g.slots; } + } + if (!s) { + omega_gpu_session_set_error("matmul kernel cache: every slot holds a prepared kernel (raise kernel_slots)"); + return OMEGA_GPU_MATMUL_TOO_LARGE; + } OmegaMatMulSpec spec; /* the kernel ignores M; 16 keeps the spec inside the tile rule */ if (omega_matmul_spec_init(&spec, 16, kp, np, OMEGA_MATMUL_PRECISION_BF16) != 0) return OMEGA_GPU_MATMUL_TOO_LARGE; @@ -203,10 +268,8 @@ static int kernel_for(uint32_t kp, uint32_t np, uint32_t grid_x, CacheSlot **out NvrmMem code; 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) % 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->valid = true; s->pinned = false; s->kp = kp; s->np = np; s->grid_x = grid_x; s->mutant = g.mutant; s->spec = spec; s->kernel = kernel; s->code = code; *out = s; *hit = false; return OMEGA_GPU_MATMUL_OK; @@ -288,15 +351,13 @@ void omega_gpu_tensor_free(OmegaGpuTensor *t) { static int matmul_core(uint32_t m, const uint16_t *a, const OmegaGpuTensor *b, float *c, OmegaGpuMatmulInfo *info) { const uint32_t k = b->k, n = b->n, kp = b->kp, np = b->np; - const uint32_t mp = round_up(m, OMEGA_GPU_MATMUL_TILE_M), mt = mp / 16, nt = np / 8; + const uint32_t mp = round_up(m, OMEGA_GPU_MATMUL_TILE_M); omega_gpu_session_lock(); int rc = OMEGA_GPU_MATMUL_OK; if (!dev_open_locked()) { omega_gpu_session_unlock(); return OMEGA_GPU_MATMUL_CHIP_FAIL; } - const uint32_t budget = g.cta_budget ? g.cta_budget : OMEGA_GPU_MATMUL_MAX_CTAS; - /* rows per launch: all of them unless the row tiles alone exceed the budget */ - uint32_t rows_per_launch = mt <= budget ? mp : budget * 16u; + uint32_t rows_per_launch, grid_x; + launch_shape(mp, np, &rows_per_launch, &grid_x); uint32_t grid_y_full = rows_per_launch / 16u; - uint32_t grid_x = budget / grid_y_full; if (grid_x == 0) grid_x = 1; if (grid_x > nt) grid_x = nt; if (info) { info->m = m; info->k = k; info->n = n; info->padded_m = mp; info->padded_n = np; info->padded_k = kp; info->k_slices = 1; info->rows_per_call = rows_per_launch; info->grid_x = grid_x; info->grid_y = grid_y_full; @@ -304,6 +365,13 @@ 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 (g.sealed && !(g.a_buf.reserved && g.c_buf.reserved)) { + /* sealed means no driver allocation on the call path; without a scratch reservation the + * buffers below could still grow, so refuse before touching them */ + omega_gpu_session_set_error("serving reservation exceeded: matmul sealed without a scratch reservation (call omega_gpu_reserve_serving first)"); + omega_gpu_session_unlock(); + return OMEGA_GPU_MATMUL_TOO_LARGE; + } 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) { diff --git a/src/omega_gpu_matmul_api.h b/src/omega_gpu_matmul_api.h index 6466a4198..af4f5fb09 100644 --- a/src/omega_gpu_matmul_api.h +++ b/src/omega_gpu_matmul_api.h @@ -135,12 +135,22 @@ 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 + * cache kernel_slots deep (0 or <= 8 = unchanged; at most 128). 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); + +/* OPT-IN, after omega_gpu_matmul_reserve: build now the kernel a call of m rows over a k x n weight will use, and + * pin it (a prepared kernel is never evicted). The kernel cache key bakes in grid_x, which depends on the row + * count, so a server prepares every (row count, weight shape) it will run; repeated keys are cache hits. Refused + * with TOO_LARGE, before any build or driver call, when every slot already holds a prepared kernel. */ +int omega_gpu_matmul_prepare(uint32_t m, uint32_t k, uint32_t n); +/* on = 1: from now on a call whose kernel is not cached is refused with OMEGA_GPU_MATMUL_TOO_LARGE ("serving + * reservation exceeded: matmul kernel ... was not prepared") and asks the driver for nothing. 0 lifts it; closing + * the device also ends it. */ +void omega_gpu_matmul_seal(int on); /* Drop the reservation flag (buffers stay allocated, growth is allowed again). */ void omega_gpu_matmul_unreserve(void); diff --git a/src/omega_gpu_serving.c b/src/omega_gpu_serving.c index c35338c06..18e0deef8 100644 --- a/src/omega_gpu_serving.c +++ b/src/omega_gpu_serving.c @@ -12,7 +12,10 @@ int omega_gpu_reserve_serving(const OmegaGpuServingBounds *b) { return rc; } +void omega_gpu_serving_seal(void) { omega_gpu_matmul_seal(1); } + void omega_gpu_serving_release(void) { + omega_gpu_matmul_seal(0); omega_gpu_attention_unreserve(); omega_gpu_matmul_unreserve(); } diff --git a/src/omega_gpu_serving.h b/src/omega_gpu_serving.h index adf463a17..f24c61ca9 100644 --- a/src/omega_gpu_serving.h +++ b/src/omega_gpu_serving.h @@ -12,8 +12,9 @@ * * 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). + * first build of each distinct matmul shape unless the server prepares every shape it will run + * (omega_gpu_matmul_prepare: one small code buffer per shape, pinned) and then seals + * (omega_gpu_serving_seal): after that an unprepared shape is refused, never built. */ #ifndef OMEGA_GPU_SERVING_H #define OMEGA_GPU_SERVING_H @@ -29,7 +30,7 @@ typedef struct { 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 */ + uint32_t kernel_slots; /* matmul kernel cache slots; 0 = leave at 8; at most 128. Qwen3-4B, rows 1..256: 95 distinct shapes (grid_x varies with rows) */ } OmegaGpuServingBounds; /* Opens the device if needed. 0 on success, else the first failing OMEGA_GPU_MATMUL_* / ATTN_* code @@ -39,7 +40,9 @@ typedef struct { * 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.) */ +/* After the server prepared its matmul shapes: refuse any unprepared one from now on (omega_gpu_matmul_seal). */ +void omega_gpu_serving_seal(void); +/* Lift the reservation and the seal: buffers stay, growth is allowed again. (Closing the device also ends both.) */ void omega_gpu_serving_release(void); #endif diff --git a/src/omega_gpu_session.c b/src/omega_gpu_session.c index a8349c655..28fd4eb73 100644 --- a/src/omega_gpu_session.c +++ b/src/omega_gpu_session.c @@ -75,9 +75,25 @@ int omega_gpu_session_set_spin_us(uint32_t us) { } uint32_t omega_gpu_session_spin_us(void) { return __atomic_load_n(&g_spin_us, __ATOMIC_RELAXED); } +/* driver allocations made through this session (omega_gpu_session_alloc_stats) */ +static OmegaGpuAllocStats g_stats; + +void omega_gpu_session_alloc_stats(OmegaGpuAllocStats *out) { + if (!out) return; + out->allocs = __atomic_load_n(&g_stats.allocs, __ATOMIC_RELAXED); + out->alloc_bytes = __atomic_load_n(&g_stats.alloc_bytes, __ATOMIC_RELAXED); + out->alloc_failures = __atomic_load_n(&g_stats.alloc_failures, __ATOMIC_RELAXED); + out->frees = __atomic_load_n(&g_stats.frees, __ATOMIC_RELAXED); +} + int omega_gpu_session_alloc(size_t bytes, NvrmMem *out) { if (!g_open || g_blocked) { omega_gpu_session_set_error("alloc without open device"); return -1; } - if (nvrm_alloc_gpu_uncached(&g_s.ctx.rm, page_up(bytes), out) != 0) { fail_rm("nvrm_alloc_gpu_uncached", &g_s.ctx.rm); memset(out, 0, sizeof *out); return -1; } + if (nvrm_alloc_gpu_uncached(&g_s.ctx.rm, page_up(bytes), out) != 0) { + __atomic_add_fetch(&g_stats.alloc_failures, 1, __ATOMIC_RELAXED); + fail_rm("nvrm_alloc_gpu_uncached", &g_s.ctx.rm); memset(out, 0, sizeof *out); return -1; + } + __atomic_add_fetch(&g_stats.allocs, 1, __ATOMIC_RELAXED); + __atomic_add_fetch(&g_stats.alloc_bytes, (uint64_t)page_up(bytes), __ATOMIC_RELAXED); return 0; } @@ -90,7 +106,10 @@ int omega_gpu_session_alloc_code(size_t code_size, NvrmMem *out) { } void omega_gpu_session_free(NvrmMem *m) { - if (m->cpu && g_open && !g_blocked) (void)nvrm_free(&g_s.ctx.rm, m); + if (m->cpu && g_open && !g_blocked) { + (void)nvrm_free(&g_s.ctx.rm, m); + __atomic_add_fetch(&g_stats.frees, 1, __ATOMIC_RELAXED); + } memset(m, 0, sizeof *m); } diff --git a/src/omega_gpu_session.h b/src/omega_gpu_session.h index d77b33f0e..d00da8e41 100644 --- a/src/omega_gpu_session.h +++ b/src/omega_gpu_session.h @@ -141,6 +141,17 @@ void omega_gpu_session_close(void); /* Device opens so far in this process (diagnostics; a persistent session shows 1). */ uint32_t omega_gpu_session_open_count(void); +/* Driver allocations made through omega_gpu_session_alloc (every GPU API buffer, tensor, scratch growth and kernel + * code buffer) since the process started, for measurement: a serving daemon reads it after warm-up and per request + * to show that serving asks the driver for nothing. Covers this session layer only, not other engines. */ +typedef struct { + uint64_t allocs; /* successful driver allocations */ + uint64_t alloc_bytes; /* their page-rounded bytes */ + uint64_t alloc_failures; /* driver refusals (e.g. NV_ERR_NO_MEMORY) */ + uint64_t frees; /* driver frees */ +} OmegaGpuAllocStats; +void omega_gpu_session_alloc_stats(OmegaGpuAllocStats *out); + #ifdef __cplusplus } #endif diff --git a/tests/.crumb b/tests/.crumb index 65c798564..8ba8d7283 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -110,145 +110,145 @@ "children": [ { "digest": "e7498eca87fe5ca8dc27d92f2d99be0de3e4c804711f9d8cba5bb1e47ca95f74", - "last_commit": "c0369e6", + "last_commit": "28911be", "name": "algebra", "purpose": "Directory tests/algebra. Purpose not yet described by a human." }, { "digest": "d484ca176e9932b098df5664d01bdd679c78f5b89f4d15a77eb30f43d235f570", - "last_commit": "c0369e6", + "last_commit": "834ee93", "name": "ans", "purpose": "Tests 1-10 for ANS (docs/ans/INERTIAL_ALIGNMENT_SPEC_V1.md): freeze/digest, hostile reference mu" }, { "digest": "bd7114b42f99c7e51741abdf24329d20713c73a332bd5f349e04384fd0241e91", - "last_commit": "c0369e6", + "last_commit": "e722877", "name": "brownian", "purpose": "Directory tests/brownian. Purpose not yet described by a human." }, { "digest": "1cbe9561b87b394e200f67996ce1407c377025d07ad033b27cdd542625e277d4", - "last_commit": "c0369e6", + "last_commit": "30bb116", "name": "canonicalization", "purpose": "Directory tests/canonicalization. Purpose not yet described by a human." }, { "digest": "d93d50a07f61b032e5476e0066cad83c42d51427a0b206585e05fff56350e12e", - "last_commit": "c0369e6", + "last_commit": "dce821c", "name": "compiler", "purpose": "Directory tests/compiler. Purpose not yet described by a human." }, { "digest": "47683e6a56ca8e14ee526004b2a90dd08498507e8d4b0b42c3d85d22afb07d28", - "last_commit": "c0369e6", + "last_commit": "28b8793", "name": "constraints", "purpose": "Directory tests/constraints. Purpose not yet described by a human." }, { "digest": "95b2041562face1ba7baa6c57456bf07d77a0f44e093b989ec18f559064e22d9", - "last_commit": "c0369e6", + "last_commit": "49abb02", "name": "crumbline", "purpose": "Directory tests/crumbline. Purpose not yet described by a human." }, { "digest": "d57833fb3f63ef6475cf25e2e16319d818a0a407b825d5b4c5cbdd3cc0e06492", - "last_commit": "c0369e6", + "last_commit": "00f54b6", "name": "dual_experiment", "purpose": "Tests for the DUAL applicability experiment: determinism (same seed, byte-identical results dige" }, { "digest": "fb0a9296a93d065d5e016ad0a950fc6b9691f8ddca12ad0111cee2e35791cdb4", - "last_commit": "c0369e6", + "last_commit": "c819b37", "name": "effect", "purpose": "Directory tests/effect. Purpose not yet described by a human." }, { "digest": "bc2473da101ba5a74c32021a9d27ecb2007897f07a9689ba50ccc99b9030bf0c", - "last_commit": "c0369e6", + "last_commit": "1a147e4", "name": "estimation", "purpose": "Directory tests/estimation. Purpose not yet described by a human." }, { "digest": "de2bc4e310a257dbd21cc5c9624641b137b1b4487d8876cb52469aea2af5b904", - "last_commit": "c0369e6", + "last_commit": "3108fc2", "name": "fabric", "purpose": "Directory tests/fabric. Purpose not yet described by a human." }, { "digest": "90418926706f827aafd72938aee1dea89222cb5e465fba06a8df5a83253e0412", - "last_commit": "c0369e6", + "last_commit": "28b8793", "name": "identity", "purpose": "Directory tests/identity. Purpose not yet described by a human." }, { "digest": "c653c851c8934ce9ac4eabaa32a9df65d858c9ac7a556e76b265b1ee81494443", - "last_commit": "c0369e6", + "last_commit": "4f55f45", "name": "language", "purpose": "Directory tests/language. Purpose not yet described by a human." }, { "digest": "e485aa0a00f0a588e22d55caa704db8581a5da1b53080b311086e6edd38f6044", - "last_commit": "c0369e6", + "last_commit": "30bb116", "name": "malformed", "purpose": "Directory tests/malformed. Purpose not yet described by a human." }, { "digest": "adba9b5ea1c000db36078fca7d1bfef0b2769b73c8fb474024d9409bd48bdbc1", - "last_commit": "c0369e6", + "last_commit": "40d1ea3", "name": "numeric_native", "purpose": "Simulated M16 driver for numeric resource-lifecycle tests. No device execution or numeric parity" }, { "digest": "a4d320b3203650a28c43ea954957d8902d1cc25cbdc8aecd15628c01a92f68f8", - "last_commit": "c0369e6", + "last_commit": "d550b70", "name": "path", "purpose": "Directory tests/path. Purpose not yet described by a human." }, { "digest": "1fd5f914f326a1a51fc343ab633ac411e86bea22beee718b1460db066807f292", - "last_commit": "c0369e6", + "last_commit": "8a2e268", "name": "physics0", "purpose": "PD-0 gates: world self-test, oracle calibration, learner L0/L1/NULL, mutations, OSC differential" }, { "digest": "4619a90a957e77cd3d5680132ab4d1a5ef65f43419ddcc089743ca5b7a7719ee", - "last_commit": "c0369e6", + "last_commit": "8f8babe", "name": "polyglot", "purpose": "Directory tests/polyglot. Purpose not yet described by a human." }, { "digest": "16bea65aa50f0b3ab8b90cb286de048639fa8e635ff84e517e138c6f9f26a059", - "last_commit": "c0369e6", + "last_commit": "3c82389", "name": "program", "purpose": "Directory tests/program. Purpose not yet described by a human." }, { "digest": "ba0c09f67d12b5d7de906129bdbe2f6e0a228e1a34236ecc4b779c523cb2061e", - "last_commit": "c0369e6", + "last_commit": "6fdc4c3", "name": "r16_inventory", "purpose": "Directory tests/r16_inventory. Purpose not yet described by a human." }, { "digest": "000c487afa8f600842eff25a22a671afb5e29b26845105405de9542a21b70746", - "last_commit": "c0369e6", + "last_commit": "79a805d", "name": "r16_negative", "purpose": "Directory tests/r16_negative. Purpose not yet described by a human." }, { "digest": "25cabf47d18353539db28146434d246a2f81bf9c323471566451aca56806f279", - "last_commit": "c0369e6", + "last_commit": "44d8c06", "name": "r16_surface", "purpose": "Directory tests/r16_surface. Purpose not yet described by a human." }, { "digest": "9106f2ea57e8f453322f0a988b61cfcbd06631573c9ff32a777903d27d7741e1", - "last_commit": "c0369e6", + "last_commit": "40d1ea3", "name": "realize", "purpose": "Directory tests/realize. Purpose not yet described by a human." }, { "digest": "381184e79b26920b256dd4a774fb11f17a92afa8ceda4813eada241bac4163a9", - "last_commit": "c0369e6", + "last_commit": "30bb116", "name": "relations", "purpose": "Directory tests/relations. Purpose not yet described by a human." }, @@ -260,38 +260,38 @@ }, { "digest": "b1311025a3bfd8dd7b5e07b1adf1d7ece502a15200f7207f515334f7083b8315", - "last_commit": "c0369e6", + "last_commit": "5ae192a", "name": "searchtrace", "purpose": "Directory tests/searchtrace. Purpose not yet described by a human." }, { "digest": "3ecf636e2923038708cafa062895b8d8f2664dc1865cdff38575911eaa9f1b2d", - "last_commit": "c0369e6", + "last_commit": "54826a3", "name": "train", "purpose": "Directory tests/train. Purpose not yet described by a human." }, { "digest": "75d5e576c38a0497a55e9f5ee79550cb94f9b75008f3e4e126312802704b3f86", - "last_commit": "c0369e6", + "last_commit": "2564f57", "name": "turing", "purpose": "Directory tests/turing. Purpose not yet described by a human." }, { "digest": "3f19e3034ff999211cb13ab8995ca6c38d0d8e1f5bfbbbd621caaa08412d3f1f", - "last_commit": "c0369e6", + "last_commit": "fed4f07", "name": "visor", "purpose": "Directory tests/visor. Purpose not yet described by a human." } ], "children_root": "sha256:e4b130835499158755721627147fd30866f0679d7cdbc125bec754e9c0a94396", - "compiled_at": "2026-10-07T22:12:35Z", + "compiled_at": "2026-10-08T02:13:11Z", "compiler": "crumb-compile/1", - "digest": "sha256:daa6072bbb4f876d9e0e0a8cc88c56e1fe98aad01a4b9b18bfc8cea84845f7f0", + "digest": "sha256:8d75800f19cbf387d4f5cfb71bdff1801719421f07bc378c89be5dd5e3578bbb", "evidence_files": 11, "evidence_root": "sha256:d5c1bceb4d57ef0eb42797c58801b5926509711614c3e38bb7105b6efa3942f2", "files": 819, - "generated_at_commit": "884360a67256afc14fa6e6035292ad609fab337f", - "source_tree": "sha256:468b08b466d9d007c04db67cdc70329c1dc6b17fc06b5e8dd1291c570ad7f21f" + "generated_at_commit": "bc9fa7169bd871651675ec51eb78b629cab4057e", + "source_tree": "sha256:4acdfbc89ea0a591018b4cf23782713865fccb327c9f7c3e9dd47fc572c99388" }, "owner_lane": "", "provenance": { diff --git a/tests/gpu_serving_alloc_count_test.c b/tests/gpu_serving_alloc_count_test.c index b5dbbe285..cfb62168c 100644 --- a/tests/gpu_serving_alloc_count_test.c +++ b/tests/gpu_serving_alloc_count_test.c @@ -82,11 +82,11 @@ typedef struct { } Phase; typedef struct { uint32_t kp, np, gx; } Key; -static Key g_seen[64]; +static Key g_seen[128]; static int g_nseen; static int seen(uint32_t kp, uint32_t np, uint32_t gx) { for (int i = 0; i < g_nseen; i++) if (g_seen[i].kp == kp && g_seen[i].np == np && g_seen[i].gx == gx) return 1; - if (g_nseen < 64) { g_seen[g_nseen].kp = kp; g_seen[g_nseen].np = np; g_seen[g_nseen].gx = gx; g_nseen++; } + if (g_nseen < 128) { g_seen[g_nseen].kp = kp; g_seen[g_nseen].np = np; g_seen[g_nseen].gx = gx; g_nseen++; } return 0; } @@ -318,6 +318,129 @@ static int rollback_mode(void) { /* 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. */ +/* ---- sweep: the sovereign-core daemon's bounds (sc#311, sc#277). Every row count 1..256 over every weight + * shape is a distinct (kp, np, grid_x) for some rows, because grid_x is baked into the kernel and depends on + * the row tiles. prepare = 1: prepare each (rows, shape) and seal, then serving must make no driver call and an + * unprepared shape is refused. prepare = 0 (control, kernel_slots 32 as before): the cache wraps and serving + * allocates, which this mode must report as a failure. */ +#define SWEEP_ROWS 256u +static int sweep_mode(int prepare) { + W_Q = upload(HID, QDIM); W_K = upload(HID, KVDIM); W_O = upload(QDIM, HID); + W_G = upload(HID, INTER); W_D = upload(INTER, HID); W_LM = upload(HID, VOCAB); + OmegaGpuTensor *odd = upload(HID, 512); /* a shape the server never prepares */ + OmegaGpuTensor *w[6] = {W_Q, W_K, W_O, W_G, W_D, W_LM}; + float *x = calloc((size_t)SWEEP_ROWS * INTER, sizeof *x), *y = calloc((size_t)SWEEP_ROWS * VOCAB, sizeof *y); + CHECK(x && y, "sweep buffers"); + if (g_fail) return 1; + OmegaGpuServingBounds b = QWEN3_4B_BOUNDS; + b.max_rows = SWEEP_ROWS; b.max_n = VOCAB; b.max_n_one_row = VOCAB; b.kernel_slots = prepare ? 128 : 32; + CHECK(omega_gpu_reserve_serving(&b) == 0, "reserve failed: %s", omega_gpu_matmul_last_error()); + if (prepare) { + int a0 = fk.alloc_calls; + for (unsigned i = 0; i < 6; i++) { + uint32_t k, n; + omega_gpu_tensor_shape(w[i], &k, &n); + for (uint32_t m = 1; m <= SWEEP_ROWS; m++) { + int rc = omega_gpu_matmul_prepare(m, k, n); + CHECK(rc == OMEGA_GPU_MATMUL_OK, "prepare m=%u %ux%u rc=%d %s", m, k, n, rc, omega_gpu_matmul_last_error()); + } + } + printf("MEASURE sweep prepare: %d driver allocations (one code buffer per distinct kernel)\n", fk.alloc_calls - a0); + omega_gpu_serving_seal(); + } + g_nseen = 0; + OmegaGpuAllocStats st0, st1; + omega_gpu_session_alloc_stats(&st0); + int a0 = fk.alloc_calls, too_large = 0, other = 0; + for (unsigned pass = 0; pass < 2; pass++) + for (uint32_t m = 1; m <= SWEEP_ROWS; m++) + for (unsigned i = 0; i < 6; i++) { + OmegaGpuMatmulInfo info; + int rc = omega_gpu_matmul_resident_f32(m, x, w[i], y, &info); + if (rc == OMEGA_GPU_MATMUL_TOO_LARGE) too_large++; + else if (rc != OMEGA_GPU_MATMUL_OK && rc != OMEGA_GPU_MATMUL_CHIP_FAIL) other++; /* CHIP_FAIL = fake kernel skips writes */ + if (rc != OMEGA_GPU_MATMUL_TOO_LARGE) (void)seen(info.padded_k, info.padded_n, info.grid_x); + } + int made = fk.alloc_calls - a0; + omega_gpu_session_alloc_stats(&st1); + CHECK((int)(st1.allocs - st0.allocs) == made, "session alloc stats %llu != driver %d", + (unsigned long long)(st1.allocs - st0.allocs), made); + printf("MEASURE sweep serving (rows 1..%u x 6 shapes, twice): %d driver allocations, %d distinct kernels, %d refused, %d other\n", + SWEEP_ROWS, made, g_nseen, too_large, other); + CHECK(made == 0, "serving made %d driver allocations", made); + CHECK(too_large == 0 && other == 0, "serving refused %d, other rc %d", too_large, other); + CHECK(g_nseen == 95, "distinct kernels %d, expected 95 for Qwen3-4B at CTA budget 256", g_nseen); + if (prepare) { + int a1 = fk.alloc_calls; + OmegaGpuMatmulInfo info; + int rc = omega_gpu_matmul_resident_f32(1, x, odd, y, &info); + CHECK(rc == OMEGA_GPU_MATMUL_TOO_LARGE && strstr(omega_gpu_matmul_last_error(), "not prepared"), + "unprepared shape rc=%d %s", rc, omega_gpu_matmul_last_error()); + CHECK(fk.alloc_calls == a1, "unprepared shape asked the driver for memory"); + omega_gpu_serving_release(); + rc = omega_gpu_matmul_resident_f32(1, x, odd, y, &info); + CHECK(rc != OMEGA_GPU_MATMUL_TOO_LARGE, "release did not lift the seal: %s", omega_gpu_matmul_last_error()); + } + free(x); free(y); + printf("gpu_serving_alloc_count_test sweep%s: %s (%d failed checks)\n", prepare ? "" : "-skip", g_fail ? "FAIL" : "PASS", g_fail); + return g_fail ? 1 : 0; +} + +/* ---- pin: a full cache of prepared kernels is never evicted. 9 slots, 9 prepared kernels (W_Q at 16..144 rows: + * 9 row tiles, 9 distinct grid_x), then an unprepared call BEFORE any seal must be refused with no driver call, + * and every prepared kernel must still hit. */ +static int pin_mode(void) { + W_Q = upload(HID, QDIM); W_K = upload(HID, KVDIM); + float *x = calloc((size_t)144 * HID, sizeof *x), *y = calloc((size_t)144 * QDIM, sizeof *y); + CHECK(x && y, "pin buffers"); + if (g_fail) return 1; + OmegaGpuServingBounds b = QWEN3_4B_BOUNDS; + b.max_rows = 144; b.kernel_slots = 9; + CHECK(omega_gpu_reserve_serving(&b) == 0, "reserve failed: %s", omega_gpu_matmul_last_error()); + for (uint32_t m = 16; m <= 144; m += 16) { + int rc = omega_gpu_matmul_prepare(m, HID, QDIM); + CHECK(rc == OMEGA_GPU_MATMUL_OK, "prepare m=%u rc=%d %s", m, rc, omega_gpu_matmul_last_error()); + } + int a0 = fk.alloc_calls; + OmegaGpuMatmulInfo info; + int rc = omega_gpu_matmul_resident_f32(1, x, W_K, y, &info); + CHECK(rc == OMEGA_GPU_MATMUL_TOO_LARGE && strstr(omega_gpu_matmul_last_error(), "prepared kernel"), + "unprepared call with a full pinned cache: rc=%d %s", rc, omega_gpu_matmul_last_error()); + CHECK(fk.alloc_calls == a0, "refused call asked the driver for memory"); + for (uint32_t m = 16; m <= 144; m += 16) { + rc = omega_gpu_matmul_resident_f32(m, x, W_Q, y, &info); + 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); + } + CHECK(fk.alloc_calls == a0, "prepared kernels rebuilt: %d driver allocations", fk.alloc_calls - a0); + free(x); free(y); + printf("gpu_serving_alloc_count_test pin: %s (%d failed checks)\n", g_fail ? "FAIL" : "PASS", g_fail); + return g_fail ? 1 : 0; +} + + +/* Sealed without a serving reservation: the scratch buffers could still grow, so every matmul call + * must be refused by name before any driver allocation (review of omega#338). */ +static int seal_bare_mode(void) { + W_Q = upload(HID, QDIM); + float *x = calloc((size_t)144 * HID, sizeof *x), *y = calloc((size_t)144 * QDIM, sizeof *y); + CHECK(x && y, "seal-bare buffers"); + if (g_fail) return 1; + int rc = omega_gpu_matmul_prepare(16, HID, QDIM); + CHECK(rc == OMEGA_GPU_MATMUL_OK, "prepare m=16 rc=%d %s", rc, omega_gpu_matmul_last_error()); + omega_gpu_matmul_seal(1); + int a0 = fk.alloc_calls; + OmegaGpuMatmulInfo info; + rc = omega_gpu_matmul_resident_f32(144, x, W_Q, y, &info); + CHECK(rc == OMEGA_GPU_MATMUL_TOO_LARGE && strstr(omega_gpu_matmul_last_error(), "without a scratch reservation"), + "sealed, unreserved, larger call: rc=%d %s", rc, omega_gpu_matmul_last_error()); + CHECK(fk.alloc_calls == a0, "sealed call without a reservation asked the driver for memory (%d)", fk.alloc_calls - a0); + omega_gpu_matmul_seal(0); + rc = omega_gpu_matmul_resident_f32(16, x, W_Q, y, &info); + CHECK(rc != OMEGA_GPU_MATMUL_TOO_LARGE, "unsealed call still refused: rc=%d %s", rc, omega_gpu_matmul_last_error()); + free(x); free(y); + printf("gpu_serving_alloc_count_test seal-bare: %s (%d failed checks)\n", g_fail ? "FAIL" : "PASS", g_fail); + return g_fail ? 1 : 0; +} 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; @@ -339,6 +462,10 @@ int main(int argc, char **argv) { 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(); + if (strcmp(mode, "sweep") == 0) return sweep_mode(1); + if (strcmp(mode, "pin") == 0) return pin_mode(); + if (strcmp(mode, "seal-bare") == 0) return seal_bare_mode(); + if (strcmp(mode, "sweep-skip") == 0) return sweep_mode(0); begin(&ph[np], "LOAD (session+6 weights)"); W_Q = upload(HID, QDIM); W_K = upload(HID, KVDIM); W_O = upload(QDIM, HID);