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/5f451610dcbda924 # 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 ## fix(omega): decode graph lock and token recovery (#518) closed `5f451610dcbda924` #588 · opened 2026-10-19 20:03 UTC · closed 2026-10-30 18:05 UTC Imported from GitHub aien-dev/omega-mv#588, by drakestapleton Base `62d1d0d668f897386a0e311374f8dbcc5c32dde8` · Head `92391a0bfdb3b37b4a7a0a1035a756426f96ff98` drakestapleton wrote on 2026-10-19 20:03 UTC omega #518: a failed or abandoned decode-graph token must not leave the session lock held or the graph in a half-open token. Fix in `src/omega_gpu_decode_graph.c/.h` (begin_token releases the lock on failure, new `abort_token`, one-shot path keeps its old behaviour, latched session refuses). Tests: `tests/test_omega_gpu_decode_graph_abort.c` (5 cases on a fake platform whose lock refuses a second acquisition) plus 5 mutants of the fix that must each make it fail. Red before green: against main's decode graph the new test fails 1 of 5 cases. Last commit registers `make test-gpu-decode-graph` as gate OMEGA-GPU-DECODE-GRAPH (no gate ran the GPU host suites), then the crumb recompile. NOT RUN: real GB10 chip run (tools/run_gpu_decode_graph_chip.sh); real-chip token parity is expected unchanged. Handoff: ~/handoffs/2026-10-19-omega-518.md. ### Fetch it A git bundle of the pull request's head, 22235 bytes. ``` curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/5f451610dcbda924.bundle -o 5f451610dcbda924.bundle git fetch 5f451610dcbda924.bundle refs/pull/588/head ``` Raw bundle · 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 588 92391a0bfdb3b37b4a7a0a1035a756426f96ff98 ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":588,"url":"https://github.com/aien-dev/omega-mv/pull/588","export_sha256":"7319147b350ad908bf0b731068ec3be7b8dee848041dc862f71455e8e4680140","imported_utc":"2026-11-18T16:35:52Z"} ``` ### Changes ``` diff --git a/.crumb b/.crumb index f3e51fe46..16cf4c1dc 100644 --- a/.crumb +++ b/.crumb @@ -72,38 +72,38 @@ }, { "digest": "f765ad87fdddd9b47f690b07298c951a0f7e7675dc5de4ffe44e33a0c14b1986", - "last_commit": "a8a6a3d3c", + "last_commit": "62d1d0d66", "name": "spec", "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "aaf5acb82fd4c720cd60c85fd6087f636c72a98e2f3740c9295dcfa525dc64d7", - "last_commit": "bf54b19fa", + "digest": "5c711c0e2e5fefff471f186375a45809c922724e69e2578b82e03694254ec6ae", + "last_commit": "db226038f", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "5395ff8ff9afceebe8e453788f80a3064b831513cc4cb92fd55869115e06b874", - "last_commit": "a8a6a3d3c", + "digest": "21fc9659812f42e8b0d681d3917524cef83f4c8ccee5ad90b85d7ff091fa3484", + "last_commit": "db226038f", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { "digest": "308db0e6cf45c0a48f01f5f5f228450f191389de68006915b9ecf9c65b9a2e29", - "last_commit": "a8a6a3d3c", + "last_commit": "62d1d0d66", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:07bf1706dcffbad66c0353871ff67a6749055da03cee8d8ebdf9a4c91e36bddc", - "compiled_at": "2026-10-30T12:31:50Z", + "children_root": "sha256:26dde07ce8f46ca7806382d3606f0dfaa993897542fa54f5ec11c1a88a6d33c2", + "compiled_at": "2026-10-30T14:46:52Z", "compiler": "crumb-compile/1", - "digest": "sha256:904fca8c0e0cf3e3a5e5adb73393e01c7018b920bb542d289904240f70e400d6", + "digest": "sha256:7002879e314b3877e6bb7184b2455c5845e01886fb691b2983e465a2c04c0100", "evidence_files": 12622, "evidence_root": "sha256:b1a08e2ce56e6feada0d8a31f06fbd7adee57adc1c1ad8dec220293ee5d439f9", - "files": 17849, - "generated_at_commit": "52a82f392fa513ebbc2d9d719a0f14c385289b06", - "source_tree": "sha256:75458da6553a0ee5ea2af95f46afb658e8a3f6e53cca6e3b4b9874035f78c2ed" + "files": 17851, + "generated_at_commit": "db226038f539957366e49d6e3c3f762d06320cfc", + "source_tree": "sha256:39eda429c650d441ff6355812799bcd5eee6f7e9ec2d584cf37bf0193dde021f" }, "owner_lane": "", "provenance": { diff --git a/Makefile b/Makefile index 6b53cd124..6d975d896 100644 --- a/Makefile +++ b/Makefile @@ -2733,10 +2733,39 @@ test-gpu-isolation: $(GPU_ISOLATION_TEST) # host-only; the chip gate is tools/run_gpu_decode_graph_chip.sh under the GB10 lock. .PHONY: test-gpu-decode-graph GPU_DECODE_GRAPH_TEST = $(OUT_DIR)/gpu_decode_graph_test +GPU_DGRAPH_ABORT_TEST = $(OUT_DIR)/test_omega_gpu_decode_graph_abort $(GPU_DECODE_GRAPH_TEST): tests/gpu_decode_graph_test.c src/omega_gpu_decode_graph.h src/omega_gpu_cmdlist.h $(OUT_DIR)/libomega_gpu.a $(CC) $(CFLAGS) -ffp-contract=off -o $@ tests/gpu_decode_graph_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm -test-gpu-decode-graph: $(GPU_DECODE_GRAPH_TEST) +test-gpu-decode-graph: $(GPU_DECODE_GRAPH_TEST) $(GPU_DGRAPH_ABORT_TEST) test-gpu-decode-graph-abort-mutants ./$(GPU_DECODE_GRAPH_TEST) --host-only + ./$(GPU_DGRAPH_ABORT_TEST) + +# omega #518: lock and token-state recovery of the split API (failed begin_token, abort_token, end_token after a failed +# record, latched session) on a fake platform whose lock refuses a second acquisition. Host only, opens no device. +# Each mutant breaks one rule in a copy of omega_gpu_decode_graph.c linked ahead of the library; the test must FAIL on it. +.PHONY: test-gpu-decode-graph-abort-mutants +GPU_DGRAPH_MUT_DIR = $(OUT_DIR)/dgraph_mut +$(GPU_DGRAPH_ABORT_TEST): tests/test_omega_gpu_decode_graph_abort.c src/omega_gpu_decode_graph.h src/omega_gpu_platform.h $(OUT_DIR)/libomega_gpu.a + $(CC) $(CFLAGS) -o $@ tests/test_omega_gpu_decode_graph_abort.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm +test-gpu-decode-graph-abort-mutants: $(OUT_DIR)/libomega_gpu.a + @mkdir -p $(GPU_DGRAPH_MUT_DIR) + @set -e; killed=0; \ + dmut() { name=$$1; script=$$2; \ + sed -e "$$script" src/omega_gpu_decode_graph.c > $(GPU_DGRAPH_MUT_DIR)/$$name.c; \ + if cmp -s src/omega_gpu_decode_graph.c $(GPU_DGRAPH_MUT_DIR)/$$name.c; then echo "mutant $$name: pattern not found"; exit 1; fi; \ + $(CC) $(filter-out -MMD -MP,$(CFLAGS)) -c -o $(GPU_DGRAPH_MUT_DIR)/$$name.o $(GPU_DGRAPH_MUT_DIR)/$$name.c; \ + $(CC) $(filter-out -MMD -MP,$(CFLAGS)) -o $(GPU_DGRAPH_MUT_DIR)/$$name tests/test_omega_gpu_decode_graph_abort.c \ + $(GPU_DGRAPH_MUT_DIR)/$$name.o $(OUT_DIR)/libomega_gpu.a -lpthread -lm; \ + if ./$(GPU_DGRAPH_MUT_DIR)/$$name > $(GPU_DGRAPH_MUT_DIR)/$$name.log 2>&1; then echo "mutant $$name SURVIVED"; exit 1; fi; \ + nfail=$$(grep -c '^FAIL' $(GPU_DGRAPH_MUT_DIR)/$$name.log || true); \ + if [ "$$nfail" -eq 0 ]; then echo "mutant $$name: nonzero exit but no FAIL case (did not run?)"; cat $(GPU_DGRAPH_MUT_DIR)/$$name.log; exit 1; fi; \ + echo "mutant $$name killed: $$nfail case(s) failed"; killed=$$((killed+1)); }; \ + dmut begin-fail-keeps-lock 's| omega_gpu_session_unlock(); /\* begin_fail releases \*/| ;|'; \ + dmut begin-fail-stays-in-token 's| g->in_token = 0; g->failed = 1;| g->failed = 1;|'; \ + dmut oneshot-old-bad-args-only 's|if (brc != OMEGA_GPU_DGRAPH_OK) {|if (brc == OMEGA_GPU_DGRAPH_BAD_ARGS) {|'; \ + dmut abort-keeps-lock 's|g->n_stamp = 0; /\* no timeline for an abandoned token \*/|&\n return OMEGA_GPU_DGRAPH_OK;|'; \ + dmut abort-submits 's|^int omega_gpu_decode_graph_abort_token(OmegaGpuDecodeGraph \*g) {$$|&\n if (g \&\& g->in_token) return omega_gpu_decode_graph_end_token(g, NULL, NULL);|'; \ + echo "test-gpu-decode-graph-abort-mutants: $$killed of 5 mutants killed" # OWN-30B W-L2c (2026-10-14): per-launch fixed cost inside a command list (tests/gpu_launch_cost_test.c, chip only, GB10 lock). GPU_LAUNCH_COST_TEST = $(OUT_DIR)/gpu_launch_cost_test diff --git a/README.md b/README.md index 1cfed2960..e62d7203b 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ make PHYSICS_DIR=/nonexistent PHYSICS_LOCK_CHECK=0 test-numeric-cpu # E1 CP make PHYSICS_DIR=/nonexistent PHYSICS_LOCK_CHECK=0 test-realize ``` -The full omegatool build (`make`) links the `physics` repository at the commit in `physics.lock`. Some older milestone gate scripts rewrite their receipts in place, so do not run them on a clean checkout you mean to keep; prefer the `make test-*` targets above. Targets that touch the GB10 need the DGX Spark, run one at a time, and are never killed mid-run. The M19 clean-clone gate rebuilds the tree in a temporary directory and runs the milestone gates there; when that nested build or nested run fails, it now prints the tail of its own log to stderr, so a failure names its cause instead of reporting a bare FAIL. CI definitions in `.github/workflows/` list the suites that run on every push. +The full omegatool build (`make`) links the `physics` repository at the commit in `physics.lock`. Some older milestone gate scripts rewrite their receipts in place, so do not run them on a clean checkout you mean to keep; prefer the `make test-*` targets above. Some GPU suites build against physics but still open no device: `make test-gpu-decode-graph` checks the decode graph's glue kernels and, on a fake platform, that a failed or abandoned decode token always gives the GPU session lock back (shown on the fake only; the real-chip decode run is `tools/run_gpu_decode_graph_chip.sh`). Targets that touch the GB10 need the DGX Spark, run one at a time, and are never killed mid-run. The M19 clean-clone gate rebuilds the tree in a temporary directory and runs the milestone gates there; when that nested build or nested run fails, it now prints the tail of its own log to stderr, so a failure names its cause instead of reporting a bare FAIL. CI definitions in `.github/workflows/` list the suites that run on every push. ## Repository layout diff --git a/src/.crumb b/src/.crumb index 7c4b83da7..e24cd59c8 100644 --- a/src/.crumb +++ b/src/.crumb @@ -188,14 +188,14 @@ } ], "children_root": "sha256:2f729945214be989a74d7c2064ae4ba3c2deb52ebea3f334b459198a7bd032ed", - "compiled_at": "2026-10-30T02:49:13Z", + "compiled_at": "2026-10-30T14:45:31Z", "compiler": "crumb-compile/1", - "digest": "sha256:aaf5acb82fd4c720cd60c85fd6087f636c72a98e2f3740c9295dcfa525dc64d7", + "digest": "sha256:5c711c0e2e5fefff471f186375a45809c922724e69e2578b82e03694254ec6ae", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 568, - "generated_at_commit": "2cb1ccb5d3c1a1ce1cb94a7a3dd3c221395c4e15", - "source_tree": "sha256:7f1fef46a27e8baf5cf567bb590d417ede3767c5f07e714d0f0812af0645bb4b" + "generated_at_commit": "72e0ed1878d01d4df242c595e7dedb54901246c0", + "source_tree": "sha256:91fe777598fbf2df7716f59b0caf043e15d9628898cc34558963dd64c17b9c48" }, "owner_lane": "", "provenance": { diff --git a/src/omega_gpu_decode_graph.c b/src/omega_gpu_decode_graph.c index bba753564..43717c2a4 100644 --- a/src/omega_gpu_decode_graph.c +++ b/src/omega_gpu_decode_graph.c @@ -300,6 +300,14 @@ int omega_gpu_decode_graph_record_swiglu(OmegaGpuDecodeGraph *g, uint64_t gu_va, return add_glue(g, OMEGA_GPU_DGRAPH_K_SWIGLU, &g->k[g->mutant == 1 ? K_SW_M1 : K_SW], OMEGA_DEC_INTER / OMEGA_DEC_THREADS, OMEGA_DEC_TOPK, (size_t)OMEGA_DEC_TOPK * OMEGA_DEC_INTER, a, OMEGA_DEC_SW_WORDS); } +/* A failed begin leaves no token behind: list emptied, out of the token, lock released (no end_token owed, omega #518). */ +static int begin_fail(OmegaGpuDecodeGraph *g) { + omega_gpu_cmdlist_reset(g->cl); + g->in_token = 0; g->failed = 1; + omega_gpu_session_unlock(); /* begin_fail releases */ + return OMEGA_GPU_DGRAPH_CHIP_FAIL; +} + int omega_gpu_decode_graph_begin_token(OmegaGpuDecodeGraph *g, const float *x_embed) { if (!g || g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; omega_gpu_session_lock(); @@ -308,11 +316,11 @@ int omega_gpu_decode_graph_begin_token(OmegaGpuDecodeGraph *g, const float *x_em memset(&g->acc, 0, sizeof g->acc); g->n_kind = 0; g->tagged_in_list = 0; g->n_stamp = 0; omega_gpu_cmdlist_reset(g->cl); - if (!omega_gpu_session_open()) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); + if (!omega_gpu_session_open()) return begin_fail(g); uint32_t *tok = cpu_of(g, g->lo.token); tok[0] = TOKEN_POISON; tok[1] = TOKEN_POISON; if (x_embed) memcpy(cpu_of(g, g->lo.x), x_embed, OMEGA_GPU_DGRAPH_HIDDEN * 4u); - if (omega_gpu_session_sync_to_gpu(&g->buf) != 0) return fail(g, NULL, OMEGA_GPU_DGRAPH_CHIP_FAIL); + if (omega_gpu_session_sync_to_gpu(&g->buf) != 0) return begin_fail(g); return OMEGA_GPU_DGRAPH_OK; } @@ -389,9 +397,18 @@ int omega_gpu_decode_graph_end_token(OmegaGpuDecodeGraph *g, uint32_t *token, Om return rc; } +int omega_gpu_decode_graph_abort_token(OmegaGpuDecodeGraph *g) { + if (!g || !g->in_token) return OMEGA_GPU_DGRAPH_BAD_ARGS; + omega_gpu_cmdlist_reset(g->cl); + g->in_token = 0; g->tagged_in_list = 0; g->n_stamp = 0; /* no timeline for an abandoned token */ + omega_gpu_session_unlock(); + return OMEGA_GPU_DGRAPH_OK; +} + int omega_gpu_decode_graph_token(OmegaGpuDecodeGraph *g, const float *x_embed, uint32_t pos, uint32_t *token, OmegaGpuDecodeGraphStats *st) { if (!g) return OMEGA_GPU_DGRAPH_BAD_ARGS; - if (omega_gpu_decode_graph_begin_token(g, x_embed) == OMEGA_GPU_DGRAPH_BAD_ARGS) return OMEGA_GPU_DGRAPH_BAD_ARGS; + const int brc = omega_gpu_decode_graph_begin_token(g, x_embed); + if (brc != OMEGA_GPU_DGRAPH_OK) { if (st) memset(st, 0, sizeof *st); return brc; } /* nothing begun, lock free */ for (uint32_t l = 0; l < g->cfg.layers && !g->failed; l++) (void)omega_gpu_decode_graph_record_layer(g, l, pos); if (!g->failed) (void)omega_gpu_decode_graph_record_final(g); return omega_gpu_decode_graph_end_token(g, token, st); diff --git a/src/omega_gpu_decode_graph.h b/src/omega_gpu_decode_graph.h index c213585b2..bb8a78b32 100644 --- a/src/omega_gpu_decode_graph.h +++ b/src/omega_gpu_decode_graph.h @@ -46,8 +46,20 @@ * Memory: every buffer is omega_gpu_session_alloc (GPU-uncached; nvos.h:1117 "For system memory this will not be * coherent with direct CPU mappings"): the host writes x before the submit and reads the token / logits after. * - * Threading: begin_token takes omega_gpu_session_lock() and end_token releases it. Between them only record calls - * (this file's and the four APIs' *_record) may run on this thread; a non-record API call would self-deadlock. + * Threading: begin_token takes omega_gpu_session_lock() and end_token (or abort_token) releases it. Between them only + * record calls (this file's and the four APIs' *_record) may run on this thread; a non-record API call would + * self-deadlock. + * Lock and token state after each call (omega #518; tests/test_omega_gpu_decode_graph_abort.c enforces it): + * begin_token OK: lock held, in the token; exactly one end_token or abort_token is owed. + * BAD_ARGS (NULL graph, already in a token): nothing taken, nothing changed. + * CHIP_FAIL (device open refused or latched, x / token visibility sync refused): lock RELEASED, out of + * the token, list empty; nothing owed (end_token and abort_token answer BAD_ARGS). Nothing was + * submitted, so nothing is uncertain; the next begin_token may simply be called again (a latched + * process fails it again at once: the graph never retries on its own). + * record_* lock still held, still in the token, whatever they return; a failure sticks (later records refuse). + * end_token always releases the lock and leaves the token, success or failure. + * abort_token releases the lock and leaves the token without submitting what was recorded. + * token (one-shot) returns with the lock released in every case. * The four APIs refuse a record that would evict a cached kernel: run one plain call per shape (or one token with the * host path) first, or simply record the first token (an empty cache slot is filled, never evicted). * @@ -63,6 +75,7 @@ * int omega_gpu_decode_graph_record_layer(OmegaGpuDecodeGraph *g, uint32_t layer, uint32_t pos); * int omega_gpu_decode_graph_record_final(OmegaGpuDecodeGraph *g); * int omega_gpu_decode_graph_end_token(OmegaGpuDecodeGraph *g, uint32_t *token, OmegaGpuDecodeGraphStats *st); + * int omega_gpu_decode_graph_abort_token(OmegaGpuDecodeGraph *g); * int omega_gpu_decode_graph_record_res(OmegaGpuDecodeGraph *g, uint32_t mode, uint64_t x_va, uint64_t a_va, uint64_t w_va, * uint64_t nw_va, uint64_t y_va, float eps, float scale); * int omega_gpu_decode_graph_record_swiglu(OmegaGpuDecodeGraph *g, uint64_t gu_va, uint64_t act_va, float scale); @@ -142,7 +155,8 @@ int omega_gpu_decode_graph_set_final_norm(OmegaGpuDecodeGraph *g, const float *f /* One whole token: x_embed (f32 [2048], the token's embedding row) -> greedy token. pos: the token's position. */ int omega_gpu_decode_graph_token(OmegaGpuDecodeGraph *g, const float *x_embed, uint32_t pos, uint32_t *token, OmegaGpuDecodeGraphStats *st); -/* The same in steps. begin_token takes the session lock, empties the list and (x_embed non-NULL) writes x. */ +/* The same in steps. begin_token takes the session lock, empties the list and (x_embed non-NULL) writes x. On + * CHIP_FAIL it has already released the lock and left the token (see Threading above): do not call end_token. */ int omega_gpu_decode_graph_begin_token(OmegaGpuDecodeGraph *g, const float *x_embed); int omega_gpu_decode_graph_record_layer(OmegaGpuDecodeGraph *g, uint32_t layer, uint32_t pos); int omega_gpu_decode_graph_record_final(OmegaGpuDecodeGraph *g); @@ -150,6 +164,10 @@ int omega_gpu_decode_graph_record_final(OmegaGpuDecodeGraph *g); * submit), reads the token (may be NULL), releases the lock. Always releases the lock, also after an earlier failed * record (then returns that failure without submitting). */ int omega_gpu_decode_graph_end_token(OmegaGpuDecodeGraph *g, uint32_t *token, OmegaGpuDecodeGraphStats *st); +/* Abandons a begun token: drops the recorded launches WITHOUT submitting them, leaves the token, releases the lock. + * OK, or BAD_ARGS when no token is begun. Nothing reaches the device, so nothing is uncertain; launches flush_each + * already submitted stay done. The device x keeps what begin_token wrote; the timeline is cleared. */ +int omega_gpu_decode_graph_abort_token(OmegaGpuDecodeGraph *g); /* Glue records (between begin and end): RES (omega_blackwell_codegen_dec.h modes ADD / COMBINE / NORM) and SWIGLU. */ int omega_gpu_decode_graph_record_res(OmegaGpuDecodeGraph *g, uint32_t mode, uint64_t x_va, uint64_t a_va, uint64_t w_va, diff --git a/tests/.crumb b/tests/.crumb index 5fbfcb29c..dbd2777c1 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -302,14 +302,14 @@ } ], "children_root": "sha256:0c57282ff8cf1d34e5685e2729755f2978917e66debb5423961033be9d25c902", - "compiled_at": "2026-10-30T12:31:50Z", + "compiled_at": "2026-10-30T14:45:31Z", "compiler": "crumb-compile/1", - "digest": "sha256:5395ff8ff9afceebe8e453788f80a3064b831513cc4cb92fd55869115e06b874", + "digest": "sha256:21fc9659812f42e8b0d681d3917524cef83f4c8ccee5ad90b85d7ff091fa3484", "evidence_files": 16, "evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a", - "files": 1386, - "generated_at_commit": "52a82f392fa513ebbc2d9d719a0f14c385289b06", - "source_tree": "sha256:a3b853bb9c0edfc5f1413ccaa2698cc6ea920f2e8795756121667cc9d37f6679" + "files": 1388, + "generated_at_commit": "72e0ed1878d01d4df242c595e7dedb54901246c0", + "source_tree": "sha256:3e132e0e19f98ffba34d448fdb432cb287ddb4127a38a54cec4de4133fc68f8e" }, "owner_lane": "", "provenance": { diff --git a/tests/gates/omega-gpu-decode-graph.gate b/tests/gates/omega-gpu-decode-graph.gate new file mode 100644 index 000000000..2eed6c402 --- /dev/null +++ b/tests/gates/omega-gpu-decode-graph.gate @@ -0,0 +1,24 @@ +# tests/gates/omega-gpu-decode-graph.gate +# Host-only decode-graph suite (omega #518): make test-gpu-decode-graph (decode graph test, abort/lock-recovery test, 5 mutants). No device. +gate: OMEGA-GPU-DECODE-GRAPH +manifest_version: 2 +owner: omega +requires: + - host +inputs: + - Makefile + - mk/ + - src/ + - tests/ + - tools/ + - physics.lock + - tests/gates/ +run: + exec: tests/gates/run.sh + args: ["gpu-decode-graph"] +expects: + exit: 0 + verdict: PASS +timeout: 20m +exactness: EXACT +cache_scope: machine diff --git a/tests/gates/run.sh b/tests/gates/run.sh index 66da8e1a9..89d7226df 100755 --- a/tests/gates/run.sh +++ b/tests/gates/run.sh @@ -169,6 +169,7 @@ case "$step" in ;; numeric2) make $NOPHYS test-numeric-reduce-cpu test-divsqrt-host ;; tensor) make $NOPHYS test-tensor; make $NOPHYS test-tensor-store; make $NOPHYS test-m20-receipt; make $NOPHYS test-tensor-mutations ;; + gpu-decode-graph) physics_pin; make test-gpu-decode-graph ;; # host-suites-3.yml numeric-lifecycle) make $NOPHYS test-numeric-lifecycle ;; numeric-transc-digest) diff --git a/tests/test_omega_gpu_decode_graph_abort.c b/tests/test_omega_gpu_decode_graph_abort.c new file mode 100644 index 000000000..4fc8f07fd --- /dev/null +++ b/tests/test_omega_gpu_decode_graph_abort.c @@ -0,0 +1,440 @@ +/* test_omega_gpu_decode_graph_abort: lock and token-state recovery of the decode graph's split API (omega #518) on a + * FAKE OmegaGpuPlatform. Host program, links libomega_gpu.a with the fake installed first; opens no device, touches + * no GPU. + * + * The fake's session lock REFUSES a second acquisition while held (counted in fk.double_locks) instead of blocking, + * so a lock left held by a failed call shows up as a count, not a hung test. The fake device never runs a kernel: a + * submitted list's markers never land (the cmdlist times out on the fake clock), and the graph's token slot is written + * by the "device" at the gpu->cpu sync only when a case arms fk.dev_token (a token the device produced). + * + * Contract under test (omega_gpu_decode_graph.h): a failed begin_token returns with the session lock released and the + * graph out of the token (no end_token owed); abort_token drops a begun token without submitting anything; end_token + * after a failed record submits nothing and releases the lock; an uncertain completion latches the process and every + * later begin fails without a retry or a submission. After each failure: set_final_norm, read, free and the next + * valid token neither deadlock nor see a stale token state. + * + * Each case runs in its own child process (a latch lasts for the life of the process). + */ +#include "omega_gpu_decode_graph.h" +#include "omega_gpu_session.h" +#include "omega_gpu_platform.h" +#include "omega_blackwell_codegen_dec.h" + +#include +#include +#include +#include +#include + +static int case_ok; +#define EXPECT(c) do { if (!(c)) { case_ok = 0; printf(" expect failed at line %d: %s\n", __LINE__, #c); } } while (0) + +/* ---- the fake platform ---- */ +#define NB OMEGA_GPU_HANDLE_SLOTS +#define VA_BASE 0x100000000ull +#define VA_STRIDE 0x1000000ull +static struct { + OmegaGpuHandleTable t; + void *mem[NB]; + OmegaGpuBufferDesc desc[NB]; + OmegaGpuDeviceHandle dev; + OmegaGpuQueueHandle q; + OmegaGpuBufferHandle ring; + uint64_t ns; + unsigned locks_held, double_locks, bad_unlocks; + unsigned submits, dev_opens; + int fail_open, fail_c2g; /* refuse device open / every cpu->gpu visibility sync */ + uint64_t dev_token_va; /* nonzero: the device wrote dev_token there (seen at the gpu->cpu sync) */ + uint32_t dev_token; +} fk; + +static uint64_t f_now_ns(void *c) { (void)c; fk.ns += 1000u; return fk.ns; } +static uint64_t f_now_ms(void *c) { return f_now_ns(c) / 1000000u; } +static int f_wait(void *c, uint64_t ms) { (void)c; fk.ns += ms * 1000000u; return 0; } +static int f_lock(void *c, uint32_t id) { + (void)c; + if (id != OMEGA_GPU_SESSION_LOCK_ID) return OMEGA_GPU_PLAT_REFUSED_ARGS; + if (fk.locks_held) { fk.double_locks++; return OMEGA_GPU_PLAT_REFUSED_BUSY; } /* a real lock would block here forever */ + fk.locks_held = 1; + return 0; +} +static int f_unlock(void *c, uint32_t id) { + (void)c; + if (id != OMEGA_GPU_SESSION_LOCK_ID || !fk.locks_held) { fk.bad_unlocks++; return OMEGA_GPU_PLAT_REFUSED_ARGS; } + fk.locks_held = 0; + return 0; +} +static const char *f_diag(void *c) { (void)c; return fk.fail_open ? "fake device: open refused" : ""; } + +static int f_dev_open(void *c, uint32_t ord, OmegaGpuDeviceHandle *out) { + (void)c; + if (fk.fail_open) return OMEGA_GPU_PLAT_REFUSED_DEVICE; + if (ord != 0 || fk.dev.generation) return OMEGA_GPU_PLAT_REFUSED_LIMIT; + int rc = omega_gpu_handles_open(&fk.t, OMEGA_GPU_HANDLE_DEVICE, 0, 1, 0, &out->index, &out->generation); + if (rc == 0) { fk.dev = *out; fk.dev_opens++; } + return rc; +} +static int f_dev_close(void *c, OmegaGpuDeviceHandle d) { + (void)c; + int rc = omega_gpu_handles_check(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); + if (rc) return rc; + /* every handle of the device goes stale; the host memory stays mapped (leaked) so a graph that outlives the device + * writes into its own stale buffer, not freed memory: this test checks the lock, not use after close */ + for (uint32_t i = 0; i < NB; i++) + if (fk.t.live[i] && i != d.index) { (void)omega_gpu_handles_close(&fk.t, i, fk.t.generation[i], fk.t.kind[i]); fk.mem[i] = NULL; } + (void)omega_gpu_handles_close(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); + memset(&fk.dev, 0, sizeof fk.dev); memset(&fk.q, 0, sizeof fk.q); + return 0; +} + +static OmegaGpuPlatformCaps g_caps = { + .abi_major = OMEGA_GPU_PLATFORM_ABI_MAJOR, .abi_minor = OMEGA_GPU_PLATFORM_ABI_MINOR, + .services = OMEGA_GPU_SVC_REQUIRED_SESSION, .max_devices = 1, .max_buffers = 1000, .max_queues = 1, .max_inflight = 1, + .max_buffer_bytes = 1ull << 30, .max_total_bytes = 1ull << 32, +}; + +static int f_alloc(void *c, OmegaGpuDeviceHandle d, const OmegaGpuBufferDesc *desc, OmegaGpuBufferHandle *o, + OmegaCpuView *cpu, OmegaGpuVa *g, OmegaGpuBusAddr *b) { + (void)c; + int rc = omega_gpu_handles_check(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); + if (!rc) rc = omega_gpu_platform_check_buffer(desc, &g_caps); + if (!rc) rc = omega_gpu_handles_open(&fk.t, desc->kind, desc->bytes, 1000, 1ull << 32, &o->index, &o->generation); + if (rc) return rc; + void *m = calloc(1, desc->bytes); + if (!m) { (void)omega_gpu_handles_close(&fk.t, o->index, o->generation, desc->kind); return OMEGA_GPU_PLAT_REFUSED_LIMIT; } + fk.mem[o->index] = m; fk.desc[o->index] = *desc; + cpu->ptr = m; cpu->len = desc->bytes; g->va = VA_BASE + (uint64_t)o->index * VA_STRIDE; b->iova = 0; + return 0; +} +static int f_free(void *c, OmegaGpuBufferHandle h) { + (void)c; + if (h.index >= NB) return OMEGA_GPU_PLAT_REFUSED_BAD_HANDLE; + int rc = omega_gpu_handles_close(&fk.t, h.index, h.generation, fk.t.kind[h.index]); + if (rc) return rc; + free(fk.mem[h.index]); fk.mem[h.index] = NULL; + return 0; +} +static int buf_live(OmegaGpuBufferHandle h) { return h.index < NB && fk.t.live[h.index] && fk.t.generation[h.index] == h.generation; } +static void *va_to_cpu(uint64_t va) { + for (uint32_t i = 0; i < NB; i++) { + if (!fk.mem[i] || !fk.t.live[i]) continue; + const uint64_t base = VA_BASE + (uint64_t)i * VA_STRIDE; + if (va >= base && va < base + fk.desc[i].bytes) return (uint8_t *)fk.mem[i] + (va - base); + } + return NULL; +} +static int sync_check(OmegaGpuBufferHandle h, uint64_t off, uint64_t len) { + if (!buf_live(h)) return OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE; + if (len == 0 || off > fk.desc[h.index].bytes || len > fk.desc[h.index].bytes - off) return OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH; + return 0; +} +static int f_c2g(void *c, OmegaGpuBufferHandle h, uint64_t off, uint64_t len) { + (void)c; + if (fk.fail_c2g) return OMEGA_GPU_PLAT_REFUSED_DEVICE; + return sync_check(h, off, len); +} +static int f_g2c(void *c, OmegaGpuBufferHandle h, uint64_t off, uint64_t len) { + (void)c; + int rc = sync_check(h, off, len); + if (rc == 0 && fk.dev_token_va) { uint32_t *t = va_to_cpu(fk.dev_token_va); if (t) t[0] = fk.dev_token; } + return rc; +} +static int f_poll(void *c, OmegaGpuBufferHandle h, uint64_t off, uint32_t want, uint64_t ms, uint32_t *seen) { + (void)c; + if (!buf_live(h)) return OMEGA_GPU_PLAT_REFUSED_STALE_HANDLE; + if (!(fk.desc[h.index].rights & OMEGA_GPU_RIGHT_HOST_POLLED)) return OMEGA_GPU_PLAT_REFUSED_BAD_RIGHTS; + const volatile uint32_t *w = (const volatile uint32_t *)((uint8_t *)fk.mem[h.index] + off); + *seen = *w; + if ((int32_t)(*w - want) >= 0) return 0; + fk.ns += ms * 1000000u; + return OMEGA_GPU_POLL_TIMED_OUT; +} +static int f_qopen(void *c, OmegaGpuDeviceHandle d, OmegaGpuBufferHandle ring, OmegaGpuQueueHandle *o) { + (void)c; + int rc = omega_gpu_handles_check(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); + if (!rc && (!buf_live(ring) || fk.t.kind[ring.index] != OMEGA_GPU_HANDLE_BUF_QUEUE)) rc = OMEGA_GPU_PLAT_REFUSED_BAD_KIND; + if (!rc) rc = omega_gpu_handles_open(&fk.t, OMEGA_GPU_HANDLE_QUEUE, 0, 1, 0, &o->index, &o->generation); + if (!rc) { fk.q = *o; fk.ring = ring; } + return rc; +} +static int f_qclose(void *c, OmegaGpuQueueHandle q) { + (void)c; + int rc = omega_gpu_handles_close(&fk.t, q.index, q.generation, OMEGA_GPU_HANDLE_QUEUE); + if (!rc) memset(&fk.q, 0, sizeof fk.q); + return rc; +} +/* The fake device accepts the words and runs nothing: no marker, semaphore or output ever lands. */ +static int f_submit(void *c, OmegaGpuQueueHandle q, const uint32_t *w, uint32_t n) { + (void)c; (void)w; + int rc = omega_gpu_handles_check(&fk.t, q.index, q.generation, OMEGA_GPU_HANDLE_QUEUE); + if (rc) return rc; + if ((uint64_t)n * 4u > fk.desc[fk.ring.index].bytes) return OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH; + fk.submits++; + return 0; +} +static int f_retire(void *c, OmegaGpuQueueHandle q) { (void)c; (void)q; return 0; } +static void *f_halloc(void *c, size_t b) { (void)c; return malloc(b); } +static void f_hfree(void *c, void *p) { (void)c; free(p); } + +static OmegaGpuPlatform g_plat = { + .ctx = &fk, .time_now_ms = f_now_ms, .wait_ms = f_wait, .lock = f_lock, .unlock = f_unlock, + .dma_alloc = f_alloc, .dma_free = f_free, .sync_cpu_to_gpu = f_c2g, .sync_gpu_to_cpu = f_g2c, .poll_word = f_poll, + .device_open = f_dev_open, .device_close = f_dev_close, .diag_text = f_diag, + .queue_open = f_qopen, .queue_close = f_qclose, .queue_submit = f_submit, .queue_retire = f_retire, + .time_now_ns = f_now_ns, .host_alloc = f_halloc, .host_free = f_hfree, +}; + +/* ---- a one-layer graph on the fake ---- */ +#define LAYERS 1u +#define VOCAB 256u +#define MAX_POS 256u +typedef struct { + OmegaGpuDecodeAttnCache *attn; + OmegaGpuRouter *router; + OmegaGpuBf16Tensor *lm; + OmegaGpuDecodeGraph *g; +} World; + +static int world_build(World *w) { + memset(w, 0, sizeof *w); + static uint16_t lm_w[VOCAB * OMEGA_GPU_DGRAPH_HIDDEN]; /* zeros: finite bf16 */ + if (omega_gpu_decode_attn_cache_alloc(LAYERS, MAX_POS, 1e6, &w->attn) != 0) return -1; + if (omega_gpu_router_alloc(LAYERS, &w->router) != 0) return -1; + if (omega_gpu_bf16_tensor_upload(VOCAB, OMEGA_GPU_DGRAPH_HIDDEN, lm_w, &w->lm) != 0) return -1; + OmegaGpuDecodeGraphConfig cfg; memset(&cfg, 0, sizeof cfg); + cfg.layers = LAYERS; cfg.vocab = VOCAB; cfg.eps = 1e-6f; cfg.norm_topk = 1; + cfg.router = w->router; cfg.attn = w->attn; cfg.lm_head = w->lm; + return omega_gpu_decode_graph_create(&cfg, &w->g) == OMEGA_GPU_DGRAPH_OK ? 0 : -1; +} +static void world_free(World *w) { + omega_gpu_decode_graph_free(w->g); + omega_gpu_bf16_tensor_free(w->lm); + omega_gpu_router_free(w->router); + omega_gpu_decode_attn_cache_free(w->attn); + memset(w, 0, sizeof *w); +} + +static float g_x[OMEGA_GPU_DGRAPH_HIDDEN], g_norm[OMEGA_GPU_DGRAPH_HIDDEN]; + +/* The postcondition of every failed or finished call: lock free, never taken twice, never released twice. */ +#define EXPECT_LOCK_FREE() do { EXPECT(fk.locks_held == 0); EXPECT(fk.double_locks == 0); EXPECT(fk.bad_unlocks == 0); } while (0) + +/* Not in a token any more: the between-token calls run (BAD_ARGS here would mean in_token stuck). */ +static void expect_between_tokens(World *w) { + EXPECT(omega_gpu_decode_graph_set_final_norm(w->g, g_norm) == OMEGA_GPU_DGRAPH_OK); + EXPECT_LOCK_FREE(); + float x[OMEGA_GPU_DGRAPH_HIDDEN]; + EXPECT(omega_gpu_decode_graph_read(w->g, x, NULL, NULL) == OMEGA_GPU_DGRAPH_OK); + EXPECT_LOCK_FREE(); +} + +/* A valid split-API token: begin, nothing recorded, end; the fake device "wrote" the token. */ +static void expect_valid_token(World *w, uint32_t want) { + OmegaGpuDecodeGraphVas v; omega_gpu_decode_graph_vas(w->g, &v); + fk.dev_token_va = v.token; fk.dev_token = want; + const unsigned submits = fk.submits; + EXPECT(omega_gpu_decode_graph_begin_token(w->g, g_x) == OMEGA_GPU_DGRAPH_OK); + EXPECT(fk.locks_held == 1); + uint32_t tok = 0xdeadu; + OmegaGpuDecodeGraphStats st; memset(&st, 0xff, sizeof st); + EXPECT(omega_gpu_decode_graph_end_token(w->g, &tok, &st) == OMEGA_GPU_DGRAPH_OK); + EXPECT(tok == want && st.launches == 0 && st.submits == 0); + EXPECT(fk.submits == submits); /* an empty list submits nothing */ + EXPECT_LOCK_FREE(); + fk.dev_token_va = 0; +} + +static int install(void) { + memset(&fk, 0, sizeof fk); omega_gpu_handles_init(&fk.t); + for (unsigned i = 0; i < OMEGA_GPU_DGRAPH_HIDDEN; i++) { g_x[i] = (float)(i % 7) - 3.0f; g_norm[i] = 1.0f; } + return omega_gpu_session_set_platform(&g_plat, &g_caps); +} + +/* Visibility sync (cpu->gpu of x and the poisoned token) refused: split and one-shot paths, repeated, then recovery. */ +static void c_sync_fails(void) { + World w; + if (install() != OMEGA_GPU_PLAT_OK) { EXPECT(!"fake platform installs"); return; } /* never fall back to the real driver */ + if (world_build(&w) != 0) { EXPECT(!"world builds on the fake"); return; } + EXPECT_LOCK_FREE(); + const unsigned submits = fk.submits; + fk.fail_c2g = 1; + for (int rep = 0; rep < 3; rep++) { /* same outcome every time */ + EXPECT(omega_gpu_decode_graph_begin_token(w.g, g_x) == OMEGA_GPU_DGRAPH_CHIP_FAIL); + EXPECT_LOCK_FREE(); + } + /* nothing owed after a failed begin: end and abort see no token */ + uint32_t tok = 7; + EXPECT(omega_gpu_decode_graph_end_token(w.g, &tok, NULL) == OMEGA_GPU_DGRAPH_BAD_ARGS && tok == 7); + EXPECT(omega_gpu_decode_graph_abort_token(w.g) == OMEGA_GPU_DGRAPH_BAD_ARGS); + EXPECT(omega_gpu_decode_graph_record_final(w.g) == OMEGA_GPU_DGRAPH_BAD_ARGS); + EXPECT_LOCK_FREE(); + /* one-shot: the begin failure comes back as CHIP_FAIL with zeroed stats, lock free */ + OmegaGpuDecodeGraphStats st; memset(&st, 0xff, sizeof st); + tok = 7; + EXPECT(omega_gpu_decode_graph_token(w.g, g_x, 0, &tok, &st) == OMEGA_GPU_DGRAPH_CHIP_FAIL); + EXPECT(tok == 7 && st.launches == 0 && st.submits == 0 && st.total_ns == 0); + EXPECT_LOCK_FREE(); + EXPECT(fk.submits == submits); /* nothing reached the device, nothing retried */ + EXPECT(!omega_gpu_session_is_blocked()); + fk.fail_c2g = 0; + expect_between_tokens(&w); + expect_valid_token(&w, 42u); + expect_valid_token(&w, 43u); /* and again */ + world_free(&w); + EXPECT_LOCK_FREE(); +} + +/* Device open refused: the session was closed (every handle stale), the reopen in begin_token fails. */ +static void c_open_fails(void) { + World w; + if (install() != OMEGA_GPU_PLAT_OK) { EXPECT(!"fake platform installs"); return; } /* never fall back to the real driver */ + if (world_build(&w) != 0) { EXPECT(!"world builds on the fake"); return; } + omega_gpu_session_close(); + EXPECT_LOCK_FREE(); + fk.fail_open = 1; + for (int rep = 0; rep < 3; rep++) { + EXPECT(omega_gpu_decode_graph_begin_token(w.g, g_x) == OMEGA_GPU_DGRAPH_CHIP_FAIL); + EXPECT_LOCK_FREE(); + } + EXPECT(omega_gpu_decode_graph_token(w.g, g_x, 0, NULL, NULL) == OMEGA_GPU_DGRAPH_CHIP_FAIL); + EXPECT_LOCK_FREE(); + EXPECT(omega_gpu_decode_graph_end_token(w.g, NULL, NULL) == OMEGA_GPU_DGRAPH_BAD_ARGS); + /* the graph's buffers belong to the closed device: set_final_norm and read reach the stale sync and refuse (not + * BAD_ARGS: the graph is out of the token), nothing hangs */ + EXPECT(omega_gpu_decode_graph_set_final_norm(w.g, g_norm) == OMEGA_GPU_DGRAPH_CHIP_FAIL); + EXPECT(omega_gpu_decode_graph_read(w.g, NULL, NULL, NULL) == OMEGA_GPU_DGRAPH_CHIP_FAIL); + EXPECT_LOCK_FREE(); + fk.fail_open = 0; + /* reopen succeeds; the old graph's buffer is stale, so its begin fails at the visibility sync, lock free */ + EXPECT(omega_gpu_decode_graph_begin_token(w.g, g_x) == OMEGA_GPU_DGRAPH_CHIP_FAIL); + EXPECT_LOCK_FREE(); + world_free(&w); + EXPECT_LOCK_FREE(); + /* a fresh graph on the reopened device runs a valid token */ + if (world_build(&w) != 0) { EXPECT(!"world rebuilds after the reopen"); return; } + expect_valid_token(&w, 5u); + world_free(&w); + EXPECT_LOCK_FREE(); +} + +/* The caller abandons a begun token (cancellation): nothing submitted, lock released, the next token runs. */ +static void c_abort(void) { + World w; + if (install() != OMEGA_GPU_PLAT_OK) { EXPECT(!"fake platform installs"); return; } /* never fall back to the real driver */ + if (world_build(&w) != 0) { EXPECT(!"world builds on the fake"); return; } + OmegaGpuDecodeGraphVas v; omega_gpu_decode_graph_vas(w.g, &v); + const unsigned submits = fk.submits; + /* abort after a recorded launch */ + EXPECT(omega_gpu_decode_graph_begin_token(w.g, g_x) == OMEGA_GPU_DGRAPH_OK); + EXPECT(omega_gpu_decode_graph_record_res(w.g, OMEGA_DEC_RES_NORM, v.x, 0, 0, v.x, v.xq, 1e-6f, 1.0f) == OMEGA_GPU_DGRAPH_OK); + EXPECT(omega_gpu_decode_graph_abort_token(w.g) == OMEGA_GPU_DGRAPH_OK); + EXPECT_LOCK_FREE(); + EXPECT(fk.submits == submits); + EXPECT(omega_gpu_decode_graph_abort_token(w.g) == OMEGA_GPU_DGRAPH_BAD_ARGS); /* only once */ + EXPECT(omega_gpu_decode_graph_end_token(w.g, NULL, NULL) == OMEGA_GPU_DGRAPH_BAD_ARGS); + EXPECT_LOCK_FREE(); + /* abort after a failed record */ + EXPECT(omega_gpu_decode_graph_begin_token(w.g, g_x) == OMEGA_GPU_DGRAPH_OK); + EXPECT(omega_gpu_decode_graph_record_res(w.g, 0xffu, v.x, 0, 0, 0, v.xq, 1e-6f, 1.0f) == OMEGA_GPU_DGRAPH_BAD_ARGS); + EXPECT(omega_gpu_decode_graph_abort_token(w.g) == OMEGA_GPU_DGRAPH_OK); + EXPECT_LOCK_FREE(); + EXPECT(fk.submits == submits); + /* abort with nothing begun */ + EXPECT(omega_gpu_decode_graph_abort_token(NULL) == OMEGA_GPU_DGRAPH_BAD_ARGS); + expect_between_tokens(&w); + expect_valid_token(&w, 9u); + world_free(&w); + EXPECT_LOCK_FREE(); +} + +/* end_token after a failed record: no submit, the failure returned, lock released, the next token runs. */ +static void c_end_after_record_fail(void) { + World w; + if (install() != OMEGA_GPU_PLAT_OK) { EXPECT(!"fake platform installs"); return; } /* never fall back to the real driver */ + if (world_build(&w) != 0) { EXPECT(!"world builds on the fake"); return; } + OmegaGpuDecodeGraphVas v; omega_gpu_decode_graph_vas(w.g, &v); + const unsigned submits = fk.submits; + EXPECT(omega_gpu_decode_graph_begin_token(w.g, g_x) == OMEGA_GPU_DGRAPH_OK); + EXPECT(omega_gpu_decode_graph_record_res(w.g, OMEGA_DEC_RES_NORM, v.x, 0, 0, v.x, v.xq, 1e-6f, 1.0f) == OMEGA_GPU_DGRAPH_OK); + EXPECT(omega_gpu_decode_graph_record_layer(w.g, 0, 0) == OMEGA_GPU_DGRAPH_BAD_ARGS); /* layer 0 never set */ + EXPECT(omega_gpu_decode_graph_record_final(w.g) == OMEGA_GPU_DGRAPH_CHIP_FAIL); /* failed sticks */ + uint32_t tok = 7; + EXPECT(omega_gpu_decode_graph_end_token(w.g, &tok, NULL) == OMEGA_GPU_DGRAPH_CHIP_FAIL && tok == 7); + EXPECT(fk.submits == submits); + EXPECT_LOCK_FREE(); + /* one-shot with an unset layer: the same, deterministically */ + for (int rep = 0; rep < 2; rep++) { + EXPECT(omega_gpu_decode_graph_token(w.g, g_x, 0, &tok, NULL) == OMEGA_GPU_DGRAPH_CHIP_FAIL && tok == 7); + EXPECT(fk.submits == submits); + EXPECT_LOCK_FREE(); + } + expect_between_tokens(&w); + expect_valid_token(&w, 11u); + world_free(&w); + EXPECT_LOCK_FREE(); +} + +/* An uncertain completion: the submitted list never completes, the process latches. end_token reports the failure (never + * a token); every later begin fails at once with the lock released, submits nothing and retries nothing. */ +static void c_latched(void) { + World w; + if (install() != OMEGA_GPU_PLAT_OK) { EXPECT(!"fake platform installs"); return; } /* never fall back to the real driver */ + if (world_build(&w) != 0) { EXPECT(!"world builds on the fake"); return; } + OmegaGpuDecodeGraphVas v; omega_gpu_decode_graph_vas(w.g, &v); + fk.dev_token_va = v.token; fk.dev_token = 3u; /* even a token in the slot must not be claimed after a timeout */ + EXPECT(omega_gpu_decode_graph_begin_token(w.g, g_x) == OMEGA_GPU_DGRAPH_OK); + EXPECT(omega_gpu_decode_graph_record_res(w.g, OMEGA_DEC_RES_NORM, v.x, 0, 0, v.x, v.xq, 1e-6f, 1.0f) == OMEGA_GPU_DGRAPH_OK); + uint32_t tok = 7; + EXPECT(omega_gpu_decode_graph_end_token(w.g, &tok, NULL) == OMEGA_GPU_DGRAPH_CHIP_FAIL && tok == 7); + EXPECT(fk.submits >= 1); + EXPECT(omega_gpu_session_is_blocked()); + EXPECT_LOCK_FREE(); + const unsigned submits = fk.submits, opens = fk.dev_opens; + for (int rep = 0; rep < 3; rep++) { + EXPECT(omega_gpu_decode_graph_begin_token(w.g, g_x) == OMEGA_GPU_DGRAPH_CHIP_FAIL); + EXPECT_LOCK_FREE(); + EXPECT(omega_gpu_decode_graph_token(w.g, g_x, 0, &tok, NULL) == OMEGA_GPU_DGRAPH_CHIP_FAIL && tok == 7); + EXPECT_LOCK_FREE(); + } + EXPECT(fk.submits == submits && fk.dev_opens == opens); /* no retry, no reopen */ + EXPECT(omega_gpu_decode_graph_end_token(w.g, NULL, NULL) == OMEGA_GPU_DGRAPH_BAD_ARGS); + EXPECT(omega_gpu_decode_graph_abort_token(w.g) == OMEGA_GPU_DGRAPH_BAD_ARGS); + EXPECT(omega_gpu_decode_graph_set_final_norm(w.g, g_norm) == OMEGA_GPU_DGRAPH_OK); + (void)omega_gpu_decode_graph_read(w.g, NULL, NULL, NULL); /* latched: refused or parked, but it returns */ + EXPECT_LOCK_FREE(); + world_free(&w); + EXPECT_LOCK_FREE(); + EXPECT(omega_gpu_session_is_blocked()); +} + +typedef struct { const char *name; void (*fn)(void); } Case; +static const Case CASES[] = { + { "visibility sync refused at begin: split + one-shot recover", c_sync_fails }, + { "device open refused at begin: lock free, reopen recovers", c_open_fails }, + { "abort_token: cancel a begun token, nothing submitted", c_abort }, + { "end_token after a failed record: no submit, lock free", c_end_after_record_fail }, + { "latched session: begin fails at once, no retry", c_latched }, +}; + +int main(int argc, char **argv) { + const unsigned n = sizeof CASES / sizeof CASES[0]; + if (argc == 2) { /* one case in this process (debugging): its index */ + const unsigned i = (unsigned)atoi(argv[1]); + if (i >= n) return 2; + case_ok = 1; CASES[i].fn(); + printf("%s %s\n", case_ok ? "ok " : "FAIL", CASES[i].name); + return case_ok ? 0 : 1; + } + unsigned failed = 0; + for (unsigned i = 0; i < n; i++) { + fflush(stdout); + pid_t pid = fork(); + if (pid == 0) { case_ok = 1; CASES[i].fn(); fflush(stdout); _exit(case_ok ? 0 : 1); } + int status = 0; + int ok = pid > 0 && waitpid(pid, &status, 0) == pid && WIFEXITED(status) && WEXITSTATUS(status) == 0; + printf("%s %s\n", ok ? "ok " : "FAIL", CASES[i].name); + if (!ok) failed++; + } + printf("test_omega_gpu_decode_graph_abort: %u of %u cases passed: %s\n", n - failed, n, failed ? "FAIL" : "PASS"); + return failed ? 1 : 0; +} ```