Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/patches/9dd8f8070636036f # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers ## GitHub aien-dev/omega-mv#332 (code only) closed derived `9dd8f8070636036f` #332 Imported from GitHub aien-dev/omega-mv#332 (code only) GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed). Base `01f6a74636b8383b010cdb95597839582c415c27` · Head `ea986f279c51fd435e8a7daec79c4c2808cd4fcc` ### Apply it ``` git checkout -b patch-9dd8f8070636036f 01f6a74636b8383b010cdb95597839582c415c27 curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/9dd8f8070636036f.patch | git am ``` Raw patch (mbox) · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/omega-mv 332 ea986f279c51fd435e8a7daec79c4c2808cd4fcc ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":332,"url":"https://github.com/aien-dev/omega-mv/pull/332","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:37:22Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/.crumb b/.crumb index cc8d328d0..21230e5d5 100644 --- a/.crumb +++ b/.crumb @@ -63,8 +63,8 @@ "purpose": "Directory doctrine. Purpose not yet described by a human." }, { - "digest": "f2d9839725a355ae85e54d047a70e3641d94cb6547df33eb532e16d925f544fe", - "last_commit": "917e8b9", + "digest": "39433fa0e7399d9b252e4eba35651c36d385fdbe8936064fb5fb31fbd63e7dc1", + "last_commit": "78d719d", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, @@ -88,13 +88,13 @@ }, { "digest": "0f1131a82d19f6923dd6136590b9a406245f967e1d3787dcd0d873d4906c40e6", - "last_commit": "957b5a3", + "last_commit": "01f6a74", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "50c5bccc22e7485d52a010c7f442cd90d934e43fa2fa49edcbd02ec90f11f8c6", - "last_commit": "957b5a3", + "digest": "240bd58ed4724aab6e803da398cfc13b84108ce1b041ddb67dd0ff07f9657e55", + "last_commit": "78d719d", "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:8614cb8b892038007ca45a2139ff8c2c2bc3565fc45862ca1248c36a96a109cc", - "compiled_at": "2026-10-07T16:27:12Z", + "children_root": "sha256:a17d2cd9e7c7f11f617bbf1747090b2ee2306e3d3ccec3a3f598e9b23741765e", + "compiled_at": "2026-10-07T21:46:01Z", "compiler": "crumb-compile/1", - "digest": "sha256:8527daa5dee1c0172c703de4611b4acad60897929db3fb5b7e9d974061913a43", + "digest": "sha256:f8144dfd45bfa815261e79c9dc10299fd8d325cc8842b34be1ad309a9d9fe1d8", "evidence_files": 4818, "evidence_root": "sha256:07642731585ad396c5d9b566a4b8df7807018f494d322dffce41bd04fb51e553", - "files": 6658, - "generated_at_commit": "957b5a387ad5e8ff373696fff56b95946b402618", - "source_tree": "sha256:56988c533416210137d1db8e6dd66e6eee7a193ff506b7cbbb5b20a1b54c15cf" + "files": 6659, + "generated_at_commit": "78d719dda6b72ff28e3f03f9256f4796cbe8cd1f", + "source_tree": "sha256:6dfa3c87745f8ccc48962e16509340bd894280dfb8610d487aed2ed6d58fb735" }, "owner_lane": "", "provenance": { diff --git a/mk/.crumb b/mk/.crumb index 58d54f96a..06a7bf9c2 100644 --- a/mk/.crumb +++ b/mk/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-07T02:42:06Z", + "compiled_at": "2026-10-07T21:28:08Z", "compiler": "crumb-compile/1", - "digest": "sha256:f2d9839725a355ae85e54d047a70e3641d94cb6547df33eb532e16d925f544fe", + "digest": "sha256:39433fa0e7399d9b252e4eba35651c36d385fdbe8936064fb5fb31fbd63e7dc1", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 60, - "generated_at_commit": "e09adff92bf966b6b3471c129f8eb6ba1a211fb7", - "source_tree": "sha256:d0122a82af21d75c1155b949e87e9e60e0839977259af31d54afe657497a79ca" + "generated_at_commit": "01f6a74636b8383b010cdb95597839582c415c27", + "source_tree": "sha256:39973ddb9cb9f8eda311b0a3b156ffb8fc6808eca9bd59ec88b1a302c27588e6" }, "owner_lane": "", "provenance": { diff --git a/mk/omega-gpu-engine.mk b/mk/omega-gpu-engine.mk index 39ac34545..f9401f495 100644 --- a/mk/omega-gpu-engine.mk +++ b/mk/omega-gpu-engine.mk @@ -70,3 +70,17 @@ build/test_omega_gpu_engine_gb10.o: tests/test_omega_gpu_engine_gb10.c src/omega @mkdir -p build gcc -std=gnu11 -O2 -Wall -Wextra -Werror -ffp-contract=off -fno-fast-math -pthread -Isrc $(GPU_ENGINE_PHYS_INC) -c -o $@ tests/test_omega_gpu_engine_gb10.c 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 \ + 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) + @mkdir -p build + gcc -std=gnu11 -O2 -Wall -Wextra -Werror -D_GNU_SOURCE -pthread -ffp-contract=off -Isrc -Itests $(GPU_ENGINE_PHYS_INC) \ + -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 diff --git a/tests/.crumb b/tests/.crumb index 6d83511a8..ea213c878 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -254,7 +254,7 @@ }, { "digest": "930ee5e080ab63c9c6b20b7a1eade45f6b249246dccdb3039a01c5c31a217368", - "last_commit": "957b5a3", + "last_commit": "01f6a74", "name": "runtime", "purpose": "Directory tests/runtime. Purpose not yet described by a human." }, @@ -284,14 +284,14 @@ } ], "children_root": "sha256:e4b130835499158755721627147fd30866f0679d7cdbc125bec754e9c0a94396", - "compiled_at": "2026-10-07T16:27:12Z", + "compiled_at": "2026-10-07T21:46:01Z", "compiler": "crumb-compile/1", - "digest": "sha256:50c5bccc22e7485d52a010c7f442cd90d934e43fa2fa49edcbd02ec90f11f8c6", + "digest": "sha256:240bd58ed4724aab6e803da398cfc13b84108ce1b041ddb67dd0ff07f9657e55", "evidence_files": 11, "evidence_root": "sha256:d5c1bceb4d57ef0eb42797c58801b5926509711614c3e38bb7105b6efa3942f2", - "files": 818, - "generated_at_commit": "957b5a387ad5e8ff373696fff56b95946b402618", - "source_tree": "sha256:91def5ed0970231343e99d20484b6e51dd3cb1bcbccc87678468cc7db7926702" + "files": 819, + "generated_at_commit": "78d719dda6b72ff28e3f03f9256f4796cbe8cd1f", + "source_tree": "sha256:aec1271c353ef0760180402bc39cec4c3f00209aeb9e408aee856360a48ca83e" }, "owner_lane": "", "provenance": { diff --git a/tests/fake_m16_native.c b/tests/fake_m16_native.c index 4c77b7f73..d8888e9fa 100644 --- a/tests/fake_m16_native.c +++ b/tests/fake_m16_native.c @@ -14,7 +14,6 @@ #include #include -#define FK_LIVE_MAX 64 FakeDriver fk; @@ -152,6 +151,12 @@ static void run_kernel(const uint32_t *q) uint32_t *c = (uint32_t *)(uintptr_t)(((uint64_t)args[5] << 32) | args[4]); uint32_t n = args[6]; uint64_t sem = ((uint64_t)q[16] << 32) | q[15]; + + if (fk.kernel_mode == FK_KERNEL_SKIP) { /* the kernel is not modelled: count the launch, release the semaphore */ + fk.kernel_ran++; + if (fk.sem_mode != FK_SYNC_NEVER) *word_at(sem) = q[17] + (fk.sem_mode == FK_SYNC_OVERSHOOT ? 1u : 0u); + return; + } uint64_t count = n < threads ? n : threads; uint32_t i; int ok = 1; @@ -320,7 +325,7 @@ int nvrm_alloc(Nvrm *rm, uint64_t size, NvrmMem *out) int mode = -1; fk.alloc_calls++; logev(FK_EV_ALLOC, size, 0, 0); - if (!out || size == 0 || size > 0x100000u) return -1; + if (!out || size == 0 || size > (fk.alloc_max ? fk.alloc_max : 0x100000u)) return -1; if (fk.alloc_fail_nth != 0 && fk.alloc_fail_nth == fk.alloc_calls) { mode = fk.alloc_mode; if (mode == FK_ALLOC_FAIL) return inject(rm, -1); @@ -332,6 +337,8 @@ int nvrm_alloc(Nvrm *rm, uint64_t size, NvrmMem *out) out->cpu = p; out->va = (uint64_t)(uintptr_t)p; live_add(out->handle, p); + if (fk.alloc_size_n < FK_SIZE_LOG) fk.alloc_size_log[fk.alloc_size_n] = size; + fk.alloc_size_n++; if (fk.alloc_calls >= 1 && fk.alloc_calls <= 16) fk.alloc_va[fk.alloc_calls - 1] = out->va; if (mode == FK_ALLOC_NO_CPU) out->cpu = NULL; else if (mode == FK_ALLOC_SHORT) out->size = size / 2u; @@ -352,6 +359,8 @@ int nvrm_free(Nvrm *rm, NvrmMem *m) fk.free_calls++; logev(FK_EV_FREE, m->handle, 0, 0); if (fk.free_fail_nth != 0 && fk.free_fail_nth == fk.free_calls) return inject(rm, -1); + if (fk.free_size_n < FK_SIZE_LOG) fk.free_size_log[fk.free_size_n] = m->size; + fk.free_size_n++; if (!live_release(m->handle)) { fk.double_free++; errno = EINVAL; diff --git a/tests/fake_m16_native.h b/tests/fake_m16_native.h index a3f23e31b..bd17562bf 100644 --- a/tests/fake_m16_native.h +++ b/tests/fake_m16_native.h @@ -63,7 +63,12 @@ enum { FK_W_UNKNOWN = 0, FK_W_MARKER1, FK_W_MARKER2, FK_W_SEM, FK_W_COUNT }; enum { FK_ALLOC_FAIL = 0, FK_ALLOC_NO_CPU, FK_ALLOC_SHORT, FK_ALLOC_MISALIGNED }; enum { FK_SYNC_NORMAL = 0, FK_SYNC_NEVER, FK_SYNC_OVERSHOOT }; -enum { FK_KERNEL_OK = 0, FK_KERNEL_NO_WRITE, FK_KERNEL_HALF, FK_KERNEL_WRONG }; +/* FK_KERNEL_SKIP: run no kernel at all and only release the completion semaphore (for tests that count + * driver calls with kernels the fake does not model, such as the matmul and attention launches). */ +enum { FK_KERNEL_OK = 0, FK_KERNEL_NO_WRITE, FK_KERNEL_HALF, FK_KERNEL_WRONG, FK_KERNEL_SKIP }; + +#define FK_SIZE_LOG 8192 +#define FK_LIVE_MAX 128 /* live driver buffers the fake can track at once */ typedef struct { int kind; @@ -78,6 +83,7 @@ typedef struct { int channel_fail; int submit_fail; int close_fail; + uint64_t alloc_max; /* largest nvrm_alloc the fake grants, 0 = the default 1 MiB */ int alloc_fail_nth; /* 1-based nvrm_alloc call that misbehaves, 0 = none */ int alloc_mode; /* FK_ALLOC_* for that call */ int free_fail_nth; /* 1-based real nvrm_free call that fails, 0 = none */ @@ -135,6 +141,11 @@ typedef struct { uint64_t wait_ms[FK_W_COUNT]; uint32_t wait_expected[FK_W_COUNT]; + uint64_t alloc_size_log[FK_SIZE_LOG]; /* size asked by the nth granted nvrm_alloc (0-based), first FK_SIZE_LOG only */ + int alloc_size_n; + uint64_t free_size_log[FK_SIZE_LOG]; /* size of the nth real nvrm_free */ + int free_size_n; + FakeEvent ev[FK_EV_MAX]; int nev; int ev_overflow; diff --git a/tests/gpu_serving_alloc_count_test.c b/tests/gpu_serving_alloc_count_test.c new file mode 100644 index 000000000..7755cbb47 --- /dev/null +++ b/tests/gpu_serving_alloc_count_test.c @@ -0,0 +1,260 @@ +/* + * gpu_serving_alloc_count_test: MEASUREMENT of driver (RM) allocations and frees made by the real + * native GPU stack (session + matmul API + attention API) while it "serves" a Qwen3-4B-shaped + * sequence, on the simulated driver of tests/fake_m16_native.c. Host only, opens no device. + * + * Why: sovereign-core design note docs/design/gb10-weight-ownership.md (sc#277, sc PR #292) argues + * that omega still asks the driver for memory while serving, and that one refused request faults + * the GPU session for the rest of the process. This test counts those requests. + * + * What it asserts: only bookkeeping that is true today (the session opens once, the fake saw every + * request, no double free, nothing latched, the call mix is what the report says it is). It does + * NOT assert the allocation counts: they are printed as MEASURE lines. A later change that reserves + * serving memory up front would lower them and this test would still pass; the numbers in the PR + * that made it are the record of today's behaviour. + * + * What it does not model: the kernels. The fake runs in FK_KERNEL_SKIP mode, so launches are + * counted and complete, but no matmul or attention result is computed. The matmul API therefore + * reports its own poison check (CHIP_FAIL) and the attention API reports UNWRITTEN; those return + * codes are expected here and are not errors of the allocation path. Elementwise kernels (rmsnorm, + * rope, swiglu) are not exercised; the design note's audit says they stop allocating after the + * first full-size call. + * + * Shapes (Qwen3-4B): hidden 2560, 32 q heads, 8 kv heads, head_dim 128, intermediate 9728, + * vocab 151936, 36 layers. One resident tensor per distinct weight shape stands in for the 36 + * layers (resident-weight allocations are a load-time cost, counted in phase LOAD only). + * Call pattern per layer follows crates/aien-inference-abi/src/transformer_backend.rs: + * decode (m = 1): q, k, v, attention(seq), o, gate, up, down ; after the layers lm_head (m = 1) + * prefill chunk: the same with m = chunk rows (<= 128), attention once per token at seq = + * offset + t + 1, lm_head with m = 1 after the chunk + * CTA budget 256 (the daemon value in the audit). + */ +#include +#include +#include + +#include "fake_m16_native.h" +#include "omega_gpu_attention_api.h" +#include "omega_gpu_matmul_api.h" +#include "omega_gpu_session.h" + +/* The two driver symbols the session needs that the fake (written for the vector engine) lacks. */ +int nvrm_open(Nvrm *rm) { (void)rm; fk.device_open = 1; return 0; } +int nvrm_close(Nvrm *rm) { (void)rm; return 0; } +void nvrm_retire(Nvrm *rm, uint32_t upto) { (void)rm; (void)upto; } + +#define HID 2560u +#define QDIM 4096u +#define KVDIM 1024u +#define INTER 9728u +#define VOCAB 151936u +#define LAYERS 36u +#define QH 32u +#define KVH 8u +#define HD 128u +#define CHUNK 128u + +static int g_fail; +#define CHECK(c, ...) do { if (!(c)) { g_fail++; printf("FAIL %s:%d: ", __FILE__, __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0) + +/* ---- the weights: one tensor per distinct shape ---- */ +static OmegaGpuTensor *W_Q, *W_K, *W_O, *W_G, *W_D, *W_LM; + +static OmegaGpuTensor *upload(uint32_t k, uint32_t n) { + uint16_t *z = calloc((size_t)k * n, sizeof *z); + OmegaGpuTensor *t = NULL; + CHECK(z != NULL, "calloc %ux%u", k, n); + int rc = z ? omega_gpu_tensor_upload_bf16(k, n, z, &t) : -1; + CHECK(rc == OMEGA_GPU_MATMUL_OK, "upload %ux%u rc=%d %s", k, n, rc, omega_gpu_matmul_last_error()); + free(z); + return t; +} + +/* ---- per-phase counters ---- */ +typedef struct { + const char *name; + int allocs, frees; + uint64_t alloc_bytes, free_bytes; + int mm_calls, mm_miss, mm_remiss, mm_other_allocs; /* code-cache misses; misses on an already-seen shape; scratch growth */ + int at_calls, at_allocs; + int a0, f0; /* log positions at phase start */ +} Phase; + +typedef struct { uint32_t kp, np, gx; } Key; +static Key g_seen[64]; +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++; } + return 0; +} + +static Phase *P; /* current phase */ +static int g_rc_mm_ok, g_rc_mm_chipfail, g_rc_at_ok, g_rc_at_unwritten, g_rc_bad; + +static float *g_x, *g_y; /* activation in / result out, sized for CHUNK x INTER and CHUNK x VOCAB-free */ +static float *g_logits; /* 1 x VOCAB */ +static float *g_k, *g_v; /* host KV for the attention call, 4096 x 1024 f32 each */ +static float g_q[QDIM], g_attn[QDIM]; + +static void mm(uint32_t m, const OmegaGpuTensor *w, float *out) { + uint32_t k, n; + omega_gpu_tensor_shape(w, &k, &n); + int a_before = fk.alloc_calls; + OmegaGpuMatmulInfo info; + int rc = omega_gpu_matmul_resident_f32(m, g_x, w, out, &info); + int made = fk.alloc_calls - a_before; + P->mm_calls++; + if (rc == OMEGA_GPU_MATMUL_OK) g_rc_mm_ok++; + else if (rc == OMEGA_GPU_MATMUL_CHIP_FAIL) g_rc_mm_chipfail++; + else { g_rc_bad++; printf("unexpected matmul rc=%d (%s)\n", rc, omega_gpu_matmul_rc_name(rc)); } + int miss = info.kernel_cache_hit ? 0 : 1; /* at most one miss per launch; m <= 128 rows is one launch here */ + if (miss) { + P->mm_miss++; + if (seen(info.padded_k, info.padded_n, info.grid_x)) P->mm_remiss++; + } else { + (void)seen(info.padded_k, info.padded_n, info.grid_x); + } + P->mm_other_allocs += made - miss; +} + +static void attn(uint32_t seq) { + int a_before = fk.alloc_calls; + OmegaGpuAttnInfo info; + int rc = omega_gpu_gqa_attention_f32(g_q, g_k, g_v, seq, QH, KVH, HD, g_attn, &info); + P->at_calls++; + P->at_allocs += fk.alloc_calls - a_before; + if (rc == OMEGA_GPU_ATTN_OK) g_rc_at_ok++; + else if (rc == OMEGA_GPU_ATTN_UNWRITTEN) g_rc_at_unwritten++; + else { g_rc_bad++; printf("unexpected attention rc=%d (%s)\n", rc, omega_gpu_attention_rc_name(rc)); } +} + +/* One transformer step over `rows` rows of a sequence that already holds `offset` tokens. */ +static void step(uint32_t rows, uint32_t offset) { + for (uint32_t l = 0; l < LAYERS; l++) { + mm(rows, W_Q, g_y); mm(rows, W_K, g_y); mm(rows, W_K, g_y); + for (uint32_t t = 0; t < rows; t++) attn(offset + t + 1); + mm(rows, W_O, g_y); mm(rows, W_G, g_y); mm(rows, W_G, g_y); mm(rows, W_D, g_y); + } + mm(1, W_LM, g_logits); +} + +static void begin(Phase *p, const char *name) { + memset(p, 0, sizeof *p); + p->name = name; + p->a0 = fk.alloc_size_n; + p->f0 = fk.free_size_n; + p->allocs = fk.alloc_calls; /* start values, turned into deltas by end() */ + p->frees = fk.free_calls; + P = p; +} + +static void sizes(const char *kind, const uint64_t *log, int from, int to) { + /* distinct sizes with counts, in order of first appearance, at most 6 shown */ + uint64_t seen_sz[256]; int cnt[256]; int ns = 0; + for (int i = from; i < to && i < FK_SIZE_LOG; i++) { + int j; + for (j = 0; j < ns; j++) if (seen_sz[j] == log[i]) break; + if (j == ns && ns < 256) { seen_sz[ns] = log[i]; cnt[ns] = 0; ns++; } + if (j < 256) cnt[j]++; + } + printf("MEASURE %s sizes (%d distinct):", kind, ns); + for (int j = 0; j < ns && j < 6; j++) printf(" %llux%d", (unsigned long long)seen_sz[j], cnt[j]); + if (ns > 6) printf(" ... last %llux%d", (unsigned long long)seen_sz[ns - 1], cnt[ns - 1]); + printf("\n"); +} + +static void end(Phase *p, int sized) { + int a1 = fk.alloc_size_n, f1 = fk.free_size_n; + p->allocs = fk.alloc_calls - p->allocs; + p->frees = fk.free_calls - p->frees; + for (int i = p->a0; i < a1 && i < FK_SIZE_LOG; i++) p->alloc_bytes += fk.alloc_size_log[i]; + for (int i = p->f0; i < f1 && i < FK_SIZE_LOG; i++) p->free_bytes += fk.free_size_log[i]; + printf("MEASURE phase %-22s allocs %4d frees %4d alloc_bytes %12llu matmul_calls %5d (code-cache misses %d, of which on a shape seen before %d, other scratch growth %d) attention_calls %5d (allocs %d)\n", + p->name, p->allocs, p->frees, (unsigned long long)p->alloc_bytes, p->mm_calls, p->mm_miss, p->mm_remiss, p->mm_other_allocs, + p->at_calls, p->at_allocs); + if (sized && p->allocs) { sizes("alloc", fk.alloc_size_log, p->a0, a1); sizes("free ", fk.free_size_log, p->f0, f1); } + CHECK(a1 <= FK_SIZE_LOG && f1 <= FK_SIZE_LOG, "size log overflow (%d allocs, %d frees)", a1, f1); +} + +int main(void) { + fake_reset(); + fk.kernel_mode = FK_KERNEL_SKIP; + fk.alloc_max = 1ull << 31; + omega_gpu_matmul_set_cta_budget(256); + + g_x = calloc((size_t)CHUNK * INTER, sizeof *g_x); + g_y = calloc((size_t)CHUNK * INTER, sizeof *g_y); + g_logits = calloc(VOCAB, sizeof *g_logits); + g_k = calloc((size_t)4096 * KVDIM, sizeof *g_k); + g_v = calloc((size_t)4096 * KVDIM, sizeof *g_v); + CHECK(g_x && g_y && g_logits && g_k && g_v, "host buffers"); + if (g_fail) return 1; + + Phase ph[8]; + int np = 0; + + begin(&ph[np], "LOAD (session+6 weights)"); + 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); + end(&ph[np], 1); np++; + CHECK(omega_gpu_session_open_count() == 1, "session opened %u times", omega_gpu_session_open_count()); + + /* WARM-UP: one decode step at context 1. Everything after this phase is "serving". */ + begin(&ph[np], "WARMUP decode ctx=1"); + step(1, 0); + end(&ph[np], 1); np++; + const int after_warm_allocs = fk.alloc_calls, after_warm_frees = fk.free_calls; + + uint32_t ctx = 0; + const uint32_t N1 = 100, D1 = 64, N2 = 200, D2 = 64; + + begin(&ph[np], "PREFILL-A 100 tokens"); + for (uint32_t off = 0; off < N1; off += CHUNK) { uint32_t r = N1 - off < CHUNK ? N1 - off : CHUNK; step(r, ctx + off); } + ctx += N1; + end(&ph[np], 1); np++; + + begin(&ph[np], "DECODE-A 64 tokens"); + for (uint32_t i = 0; i < D1; i++) { step(1, ctx); ctx++; } + end(&ph[np], 1); np++; + + begin(&ph[np], "PREFILL-B 200 tokens"); + for (uint32_t off = 0; off < N2; off += CHUNK) { uint32_t r = N2 - off < CHUNK ? N2 - off : CHUNK; step(r, ctx + off); } + ctx += N2; + end(&ph[np], 1); np++; + + begin(&ph[np], "DECODE-B 64 tokens"); + for (uint32_t i = 0; i < D2; i++) { step(1, ctx); ctx++; } + end(&ph[np], 1); np++; + + 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 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", + g_rc_mm_ok, g_rc_mm_chipfail, g_rc_at_ok, g_rc_at_unwritten, g_rc_bad); + + /* ---- what is true today and stays true ---- */ + CHECK(g_rc_bad == 0, "%d calls returned a code other than the expected poison-check ones", g_rc_bad); + 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); + 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); + + /* teardown: tensors first, then the device (the on-close hooks free the API scratch) */ + omega_gpu_tensor_free(W_Q); omega_gpu_tensor_free(W_K); omega_gpu_tensor_free(W_O); + omega_gpu_tensor_free(W_G); omega_gpu_tensor_free(W_D); omega_gpu_tensor_free(W_LM); + omega_gpu_device_close(); + 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); + return g_fail ? 1 : 0; +} ```