diff --git a/docs/numeric/OMEGA_GPU_ENGINE.md b/docs/numeric/OMEGA_GPU_ENGINE.md new file mode 100644 index 000000000..70e8fe34b --- /dev/null +++ b/docs/numeric/OMEGA_GPU_ENGINE.md @@ -0,0 +1,475 @@ +# Omega GPU Engine: design (phase A2, reconciled to Drake's first-cut plan) + +Status: DESIGN ONLY. Nothing here launches a GPU kernel yet. Every test and +chip label for this work is NOT_RUN (host NOT_RUN, chip NOT_RUN). No receipt +exists today. + +Plan authority: Drake's pasted first-cut plan, items 1 to 11, 2026-10-02 +(`~/handoffs/2026-10-02-DEEP2-first-cut-drake.md`), plus the queen rulings in +that file. This document, the header and the stub were reconciled to it in the +second commit of omega#218. Where the first draft differed, the change is +named in section 12. + +Sources. Facts below come from two read-only scout reports, cited as A1 and +A1b: `~/handoffs/2026-10-02-DEEP2-A1-scout.md` and +`~/handoffs/2026-10-02-DEEP2-A1b-scout.md`. A1 section names are quoted as +"A1 File N" or "A1 (a)..(f)". Line numbers are those the scouts recorded +(MAIN f633e343 for A1, 2db71da for A1b; PR198 lines are marked "PR198" and +may have drifted because that PR has since moved, A1b section 6). Source +lines quoted as "now" were read directly on this branch (base 2db71da). +Anything neither scout nor this cut verified is labeled UNVERIFIED with a +confidence. + +## 1. What it is, and why + +The Omega GPU Engine is one small module that takes "here is my already-built +GPU program, its two inputs, and the buffer I expect it to fill" and returns +either SUCCESS or exactly one named failure that says which step broke. + +Why: several launcher files repeat the same open, allocate, copy-in, +pushbuffer, submit, wait, readback, close sequence (A1 Files 1 to 5, A1b +section 4: "What NUM duplicates from SUB"). They report errors in three +different styles (the GB10_DEVFAIL macros, the LDST_DEVERR line, and a bare +`-1`, A1 macro section and File 4). The divsqrt second-marker wait fails +silently with no step name (A1 headline 2: divsqrt 1160-1162). +`omega_blackwell_submit.c` has no semaphore wait and no second-marker +protection, "C3" (A1 headline 3 and A1b section 1: instant compare at line +207, no L2 flush, no marker2). + +Execution paths found (plan item 1, from the scouts): `omega_blackwell_submit.c` +runs vector addition, integer matrix multiplication and tensor matrix +multiplication on its own (A1 File 4). Separate executors handle ordinary +numeric operations, reduction chunks and DIV/SQRT/transcendentals (A1 Files +1 to 3). `omega_accelerator_world.c` has persistent vector, matrix and queued +submission paths, `rx_resident_gpu.c` has a resident-kernel lifecycle, and +`omega_world_gates.c` submits directly for qualification (A1b sections 2 and +3). The differences that matter (plan item 3): ordinary numeric execution +checks only the host marker; reduction also waits for the release semaphore +and uses a CPU barrier; DIV/SQRT also flushes GPU cache writes and waits for a +second marker; persistent world execution tracks in-flight ownership; the +resident kernel has its own cooperative shutdown protocol. + +## 2. Motivating case (E1) + +REPORTED, RECEIPT NOT READ (source: Drake/queen message 2026-10-02, relayed +from the E1 owner; no receipt path available to this cut): the live rc=-4 +failures on the reduce chip run are marker2 wait timeouts. The second marker, +written after the L2 flush, was not seen within 5000 ms. The reduce launcher +waits 5 s while ldst and divsqrt wait 600 s (A1 File 2: reduce 5000 ms at +449-450 and 473-477 PR198; A1 File 1: divsqrt 600000 ms at 1156-1169; A1 +File 5: ldst 600000 ms at 362-367 PR198). + +So the result must say WHICH wait timed out and AFTER HOW LONG, and each +timeout is a per-step input from the caller, never hard-coded in the engine. +For contrast, today ldst gives its marker wait and its semaphore wait the same +step name "wait" (A1 File 5), so a timeout there is ambiguous. E1 is not +touched by this cut. + +## 3. The engine seed, and what the module owns + +Seed (plan item 4 and item 8). The engine is extracted from the vector +lifecycle that already lives inside `src/omega_blackwell_submit.c`: +`omega_blackwell_execute_vector` (A1 File 4, lines 24 to 226 recorded by the +scout). That path is the FIRST MIGRATED PATH of the engine. It already does +open (45), channel (46), pushbuffer and buffer allocation (50, 58-64), copy-in, +a poison pre-fill of the output, QMD build, `dsb sy` (128), submit (200), a +5000 ms marker wait (203), an equality check of the semaphore word without a +wait (207), readback (210) and close (225) (A1 File 4). Reading the source +now, the poison pre-fill is at lines 74 to 80 and uses +`OMEGA_VECTOR_POISON_VALUE`, which is `0xdeadbeefU` (`src/omega_vector.h:10`). +This differs from the scout line "no poison fill" for that file (A1 File 4 +common row), so the source wins and the scout line is recorded as a +discrepancy. The engine strengthens the path with: the second marker after an +L2 flush (C3), a real release semaphore wait, a visibility barrier before +readback, a post-readback check that no output word is still its poison word, +named failure steps and the uncertain-completion rule below. + +The seed reuses the existing launch-descriptor builders (the QMD and +pushbuffer code in `omega_blackwell_submit.c`, which moves to the GB10 backend +`src/omega_blackwell_engine.c/.h`) and the unwritten-output idea of +`src/omega_unwritten_trap.h` (A1 (a)). The trap module takes a single poison +word, while the engine job carries one poison word per output word, so the +engine core (A3) does its own word comparison against the poison array and +does not call `omega_unwritten_scan`. This is a design choice of this cut, not +something a scout verified. + +Poison responsibility (plan item 8). The wrapper (the vector operation that +calls the engine) supplies poison values that are guaranteed to differ from the +expected outputs. The engine writes those words to the device output before +submit and afterwards only checks whether each output word changed. The +engine never chooses poison. Math verification (the oracle that compares the +output against the expected arithmetic result, today +`omega_blackwell_gates.c:191` for the poison test and the parity code in the +vector executor) stays in the wrapper. + +OWNS: +1. Submission of one already-built program. +2. Buffer allocation and copy-in. +3. The completion rule, in one fixed order: wait for the first marker, then + the second marker after the L2 flush (C3), then the release semaphore, then + a visibility barrier before readback. Host order follows divsqrt (A1 File + 1: marker 1156, marker2 1160, semaphore 1168, barrier 1171), which PR198 + copies into reduce (A1 File 2: 470, 473-477, 482-485). +4. Per-step timeouts, recorded in the result. +5. Copy-out. +6. Cleanup on every exit where completion is known, success or failure. +7. The poison write before and the unchanged-word check after. +8. One error vocabulary, with the failing step named. +9. The uncertain-completion block (section 5). + +DOES NOT OWN: +1. Kernel generation or codegen. +2. Launch-geometry choice. +3. Numeric contracts, math verification, or what a gate measures. +4. Choosing poison values. +5. Evidence directories and receipts. +6. Physics pin, lock and quiet-flag policy. These stay in the scripts and the + chip_run wrapper (A1b section 5: `check-physics-lock`). +7. Retries. +8. Scheduling. +9. Running more than one program per call. + +Three clarifications, each with evidence: + +- A. Pre-device refusals stay with the caller where they are about numeric + contracts. Argument checks, kernel building and QMD preflight happen before + any device is opened and return BAD_ARGS or OPERANDS (A1 File 1: 1023-1046; + File 2: 318; File 5: 234-255). The engine's own INVALID_ARGS covers only a + malformed job (null pointers, wrong lengths, zero timeouts), checked before + any device is touched. +- B. The Blackwell backend, not the engine core and not the caller, turns the + job into a QMD (the hardware work descriptor) and a pushbuffer. Evidence: + today the QMD is built after allocation because it needs the GPU addresses + of the buffers (A1 File 2: allocs 333-336 then "qmd" at 370; File 3: allocs + 113-123 then "qmd" at 180; File 4: QMD invariant at 118). +- C. One program per call. The reduce level loop calls the chunk launcher + repeatedly (A1 File 2: `omega_reduce_gb10` 467-517) and the loop stays in + the caller. Persistent device contexts (`omega_accelerator_world.c` and the + resident seat in `src/runtime/rx_resident_gpu.c`) keep one device open across + many submits (A1b sections 2 and 3). They are mapped here but not absorbed in + v1: the engine opens and closes per call, like the launchers do today. + `src/runtime` is M20-owned: reuse ideas only, never edit. + +## 4. The contract + +Public entry point, in `src/omega_gpu_engine.h`: + + int omega_gpu_execute(const OmegaGpuJob *job, OmegaGpuResult *result); + +It returns 0 (`OMEGA_GPU_ENGINE_OK`) on success, otherwise the failure code, +which is also stored in `result->failure`. + +### 4.1 The job (plan item 5) + +| field | meaning | +|---|---| +| program, program_len | encoded program bytes, already built by the caller | +| layout | `OMEGA_GPU_LAYOUT_VECTOR_1D` only; later layouts are separate qualified cuts | +| element_count | number of 32-bit elements, greater than 0 | +| input_a, input_a_len | first input, `element_count * 4` bytes | +| input_b, input_b_len | second input, `element_count * 4` bytes | +| output, output_len | host output buffer, 4-byte aligned, `element_count * 4` bytes | +| poison, poison_count | wrapper-supplied poison words, one per output word, `poison_count == element_count` | +| timeouts | `marker_ms`, `release_semaphore_ms`, `marker2_ms`, `visibility_ms`, each greater than 0 (`marker2_ms` is ignored with the A/B flag below) | +| flags | zero means every protection ON. `OMEGA_GPU_ENGINE_FLAG_NO_C3` exists only for the A/B control arm that PR198 keeps under `OMEGA_C3_PROTECT_OFF` (A1 headline 1) | + +The 4-byte element size follows the vector executor's buffer sizing +`(n*4+0xfff)&~0xfff` (A1 File 4: 55-56). Anything else is INVALID_ARGS. + +### 4.2 The result (plan items 6 and 7) + +The result records: the failure name; the last COMPLETED state; the precise +failed step (an enum, `OMEGA_GPU_ENGINE_STEP_*`, with names); which wait +failed (`MARKER`, `RELEASE_SEMAPHORE` or `MARKER2`), its configured limit and +the milliseconds actually waited; the driver return code, `errno` captured +right after the failing call, and the driver text; the observed marker, +marker2 and semaphore words, with a mask saying which are meaningful; whether +context and allocations were retained; whether cleanup failed; and the number +of still-poison words per output (`output_unchanged_words`, one output today). +The vector path uses one semaphore word (A1 File 4: `hsem` at lines 124-126 +now; A1b section 1), so the result carries one. + +### 4.3 States (plan item 6) + +INITIAL -> PREPARED -> SUBMITTED -> GPU_COMPLETE -> OUTPUT_VISIBLE -> +OUTPUT_PRODUCED -> SUCCESS. + +INITIAL is "nothing completed yet" and is the last state of a run that fails +before PREPARED (the device may or may not be open). Success is reported only +at SUCCESS. The result always records the last state completed, so a failure +says how far the run got. The earlier draft of this document called the first +state NONE and the last state COMMITTED. + +### 4.4 Failures (plan item 7) + +| name | meaning | +|---|---| +| DEVICE_OPEN | the device could not be opened | +| CHANNEL_CREATE | the channel could not be created | +| ALLOC | a device buffer could not be allocated | +| PREPARE | copy-in, the device-side poison fill, or the descriptor build failed | +| SUBMIT | submitting the work failed | +| COMPLETION_WAIT | the marker wait or the marker2 wait failed or timed out; `wait` says MARKER or MARKER2, with `wait_timeout_ms` and `waited_ms` | +| RELEASE_WAIT | the release semaphore wait failed or timed out; `wait` is RELEASE_SEMAPHORE, with `wait_timeout_ms` and `waited_ms` | +| VISIBILITY_WAIT | the visibility barrier failed or timed out, or the readback failed | +| OUTPUT_UNCHANGED | after readback, some output words still equal their poison words; the result gives the unchanged-word count per output | +| INVALID_ARGS | the job or the result pointer is malformed | +| INTERNAL_INVARIANT | the engine or its backend broke its own contract (incomplete backend table, a stub that is not implemented yet, an impossible state) | +| CLEANUP | free or close failed after an otherwise good run | +| UNCERTAIN_COMPLETION_BLOCKED | an earlier job in this process left completion uncertain; this job was refused without touching the device | + +Reconciliation note. Plan item 7 lists "completion wait" and "release wait" +as two names. The lieutenant brief for this cut said the completion wait names +which wait it was (MARKER, RELEASE_SEMAPHORE or MARKER2). This document keeps +both names distinct and splits the waits so no name overlaps: COMPLETION_WAIT +covers MARKER and MARKER2, and RELEASE_WAIT covers RELEASE_SEMAPHORE. The +`wait` field names the wait in both cases. If the queen wants all three waits +under COMPLETION_WAIT, RELEASE_WAIT would then have no meaning, so that is +left as a decision for the queen. + +Preserved on every failure: driver return code, `errno`, driver text, and the +observed marker, marker2 and semaphore words. + +Cleanup runs on every exit where completion is known. If cleanup itself fails +after an earlier failure, the first failure is kept in the result and +`cleanup_failed` is set; the engine never overwrites the first failure. If +cleanup fails after an otherwise good run, the failure is CLEANUP, the last +state stays OUTPUT_PRODUCED, and the run is not SUCCESS. + +What the engine promises: "faithfully launched, the GPU completed, and these +bytes are not their poison words". It never judges whether the math is correct. + +### 4.5 Transitions + +| # | Transition | Guard (check) | Failure name, step | Launcher step today (A1 / A1b) | +|---|---|---|---|---| +| 0 | start -> INITIAL | the engine is not blocked; the job and result are well formed; the backend table is complete | UNCERTAIN_COMPLETION_BLOCKED, step BLOCKED; INVALID_ARGS, step VALIDATE; INTERNAL_INVARIANT, step BACKEND_CHECK | arg checks: divsqrt 1023, reduce 318 (BAD_ARGS) | +| 1 | INITIAL -> PREPARED | device and channel open; all buffers allocated; inputs copied in; output poison written; descriptors built | DEVICE_OPEN (OPEN_DEVICE); CHANNEL_CREATE (CREATE_CHANNEL); ALLOC (ALLOC); PREPARE (COPY_IN, FILL_POISON, BUILD) | vector: open 45, channel 46, alloc 50 and 58-64, qmd 118; divsqrt: open 1059, channel 1060, alloc 1062 and 1073, fills 1075-1078; reduce 324-341; ldst 268-286 | +| 2 | PREPARED -> SUBMITTED | the backend submits the work | SUBMIT (SUBMIT) | "submit": vector 200, divsqrt 1154, reduce 448, simt 263, ldst 360 | +| 3 | SUBMITTED -> GPU_COMPLETE | first marker seen; then marker2 (C3); then the release semaphore; each within its own timeout | COMPLETION_WAIT (WAIT_MARKER or WAIT_MARKER2); RELEASE_WAIT (WAIT_RELEASE_SEMAPHORE). Any non-zero wait result is an uncertain completion (section 5) | marker: vector 203 (5 s), divsqrt 1156 (600 s), reduce 449 (5 s), simt 267 (5 s), ldst 362 (600 s); marker2: divsqrt 1160-1162 (SILENT), reduce and ldst PR198 only; semaphore: divsqrt 1168, reduce 455, ldst 367; vector has an instant compare only at 207, simt has none | +| 4 | GPU_COMPLETE -> OUTPUT_VISIBLE | the barrier (`dsb sy`) succeeds within `visibility_ms` and the output is copied back | VISIBILITY_WAIT (BARRIER or COPY_OUT) | dsb + memcpy: divsqrt 1171-1172, reduce 458-459; vector 128 is before submit and the readback at 210 has no barrier; simt has none (271) | +| 5 | OUTPUT_VISIBLE -> OUTPUT_PRODUCED | no output word still equals its poison word | OUTPUT_UNCHANGED (SCAN), with the unchanged-word count per output | the vector executor poisons at lines 74-80 but the check lives in `omega_blackwell_gates.c:191`; `tests/test_omega_unwritten_trap_gb10.c:158` scans after the call (A1 (b)) | +| 6 | OUTPUT_PRODUCED -> SUCCESS | every buffer freed and the device closed without error | CLEANUP (FREE or CLOSE); last state stays OUTPUT_PRODUCED | close: vector 225, divsqrt 1173, reduce 460, simt 273, ldst 370; none frees buffers | + +Failures at steps 1, 2 and 4 and the late failures at 5 and 6 run cleanup +(free every allocated buffer, then close) before returning. A failure at step 3 +does not: see section 5. + +Mapping to the existing numeric codes (`src/omega_numeric.h:271-276`, A1 (d): +OK 0, BAD_ARGS -1, NOT_ENCODED -2, OPERANDS -3, DEVICE -4, FPENV -5; no +"unwritten" code exists; the next free value is -6, which is an inference): + +| engine result | old code a migrating caller reports | +|---|---| +| OK | OK (0) | +| DEVICE_OPEN, CHANNEL_CREATE, ALLOC, PREPARE, SUBMIT, COMPLETION_WAIT, RELEASE_WAIT, VISIBILITY_WAIT, CLEANUP, UNCERTAIN_COMPLETION_BLOCKED, INTERNAL_INVARIANT | ERR_DEVICE (-4) | +| INVALID_ARGS | ERR_BAD_ARGS (-1) | +| OUTPUT_UNCHANGED | ERR_DEVICE (-4) during migration; PROPOSED new `OMEGA_NUMERIC_ERR_UNWRITTEN` = -6 in a later PR | + +This cut does not edit `omega_numeric.h`. Callers keep reporting -4 during +migration, but the engine result carries the precise name, step, wait and +time, so the old code no longer hides the cause. The vector executor returns +a bare -1 for every failure today (A1 File 4), so its wrapper decides its own +mapping when it migrates. + +## 5. Uncertain completion rule (plan item 7) + +If the engine cannot tell whether the GPU finished, it RETAINS the device +context and every allocation: no free, no close. "Cannot tell" means any +non-zero result from a completion wait (marker, marker2 or release +semaphore): a timeout (`OMEGA_GPU_BACKEND_TIMEOUT`), an unknown device state +(`OMEGA_GPU_BACKEND_UNKNOWN_STATE`), or a driver fault, because in each case the GPU may +still be running and may still write the buffers. The reason to retain: freeing +memory or closing the context under a running GPU could let it write into +memory that has been reused. (UNVERIFIED, 70%: the exact driver behavior on +freeing under a running channel was not measured by anyone in this program.) + +The engine then: +1. records in the result that context and allocations were retained + (`retained = 1`), the failure (COMPLETION_WAIT or RELEASE_WAIT), the wait, + `waited_ms`, driver return code, `errno`, driver text and the observed + marker, marker2 and semaphore words; +2. sets a process-wide block; +3. refuses every later `omega_gpu_execute` call in that process, in order + before argument checks, with UNCERTAIN_COMPLETION_BLOCKED at step BLOCKED + and last state INITIAL. + +Stub-level API for the block, in `src/omega_gpu_engine.h`: +`omega_gpu_engine_is_blocked()` returns 1 once blocked. A test-only +`omega_gpu_engine_test_reset_block()` clears it. A second test-only function, +`omega_gpu_engine_test_force_block()`, was added so the compile test can +exercise the blocked result before the real core exists. Production code +must never call either test function. There is no production way to clear the +block: recovery is a new process, and whether that is safe is unproven (section +10). + +## 6. Backend seam + +The engine core talks to the device only through the function table +`OmegaGpuBackend` in `src/omega_gpu_engine.h`, which carries its own context +pointer. The table: open_device, create_channel, alloc, copy_in, fill_poison, +build, submit, wait_marker, wait_marker2, wait_release_semaphore, barrier, +copy_out, diagnostics, free_buf, close. This lets a fake backend drive every +transition and every failure name in host tests. Compared with the first +draft: open is split into open_device and create_channel so DEVICE_OPEN and +CHANNEL_CREATE are told apart; `build_and_submit` is split into `build` and +`submit` so PREPARE and SUBMIT are told apart; the `diagnostics` callback +returns the raw driver code, driver text and observed sync words; and the +roles are the vector layout's (program, input A, input B, output, backend +scratch). + +A wait callback returns 0 when satisfied, `OMEGA_GPU_BACKEND_TIMEOUT` on +timeout, `OMEGA_GPU_BACKEND_UNKNOWN_STATE` when the device reports a state the +backend cannot classify, and any other non-zero value for a driver fault. The +engine measures `waited_ms` itself and captures `errno` right after a failing +call. The barrier callback uses the same codes, but a barrier failure is a +VISIBILITY_WAIT, which is not an uncertain completion because completion is +already known at that point. + +Default backend. `omega_gpu_execute` uses a process-wide default backend +pointer. Real code sets it with `omega_gpu_engine_set_backend`; host tests set +a fake. With none set, `omega_gpu_execute` returns the defined stub result +(INTERNAL_INVARIANT, step NOT_IMPLEMENTED) until the A3 core exists. + +Replaced call. The first draft had `omega_gpu_engine_run(backend, ctx, +request, result)`. It is replaced by `omega_gpu_execute(job, result)` plus the +default-backend setter, because plan item 5 fixes the public call. Nothing +needed the explicit-backend call: a test that wants a fake sets it as the +default. The first draft's `OmegaGpuEngineRequest` and its multi-buffer, flags +and opaque-launch-descriptor fields are replaced by `OmegaGpuJob` +(VECTOR_1D only). + +The real Blackwell GB10 backend wraps the same `m16_native_*` calls the +launchers use today: open, create_channel, alloc_memory, free_memory, +submit_methods, wait_marker, close (declared in `physics/m16/m16_native.h`, +A1 (f)). No fake of these exists anywhere today (A1 (f), A1b section 5), so +this seam is the first. + +CPU_ONLY seam. The device backend is compiled out under +`OMEGA_NUMERIC_CPU_ONLY`, exactly like the reduce launcher: include guards at +`src/omega_numeric_reduce_gb10.c:38-41`, the executor guarded at 258, and the +early return at 479-480 (A1 (c)). The engine core and the fake backend need no +physics headers and build under that flag. The header includes no physics +header at all. + +## 7. What this cut changes + +This commit changes no gate measurement, no kernel, no launcher and no +evidence file. It changes only: this document, `src/omega_gpu_engine.h`, +`src/omega_gpu_engine.c` (still a stub) and +`tests/test_omega_gpu_engine_compile.c`. `mk/omega-gpu-engine.mk` is unchanged. +The top-level Makefile is not edited (omega#198 edits it). No launcher file and +not `src/omega_blackwell_submit.c` is edited in this cut. None of the files in +the omega#198 list is touched (A1b section 6). + +## 8. Phase plan (corrected) + +Receipts that exist today: NONE. All labels are NOT_RUN. + +Correction to the first draft: Phase A may edit `src/omega_blackwell_submit.c` +and `.h`. Drake asked for that twice (queen ruling). The condition is that any +PR touching `omega_blackwell_submit.c` stays DRAFT and cannot merge until the +M17 and M18 gates and the vector chip test have run in Phase B. The reason is +the file's sha256 pin: `tests/run_m17_gates.sh:66-67,129-130` and +`src/omega_blackwell_gates.c:1166-1167` (A1b section 1). Existing blackwell +chip gates must keep measuring the same things: no gate changes. + +Phase A (light lane only, no GPU): +1. A2 (omega#218, draft): this document, the header, the compiling stub and + `make test-gpu-engine-compile`, which checks the stub's exact return values, + that every enum name is distinct, and that the block functions behave as + specified. +2. A3 (stacked): the engine core in `src/omega_gpu_engine.c`. +3. `src/omega_blackwell_engine.c/.h`: the GB10 backend extracted from + `omega_blackwell_execute_vector`. +4. Edit `src/omega_blackwell_submit.c/.h` so the vector executor becomes a + wrapper over the engine (it supplies poison guaranteed to differ from its + expected outputs, and keeps the arithmetic oracle). DRAFT only. +5. `tests/test_omega_gpu_engine.c`: host test of the REAL engine core against a + simulated driver, driving every transition and every failure name, the + uncertain-completion block, and cleanup-failure precedence. +6. A mutation sweep on the shared runner `tools/mutation_runner.sh` + (merged in omega#204, no Python; edit kinds sed, marker-sed and replace). + Each mutant removes one protection (poison write, unchanged check, marker2 + wait, release wait, barrier, retain-on-uncertain, block, cleanup) and must be + KILLED by the host test. +7. `tests/test_omega_gpu_engine_gb10.c` and + `tools/manifests/gpu_engine.chiprun`, written now in the format of + `tools/manifests/unwritten_trap.chiprun` over `tools/chip_run.sh` + (omega#205, both on origin/main). Written in Phase A, run only in Phase B. +8. Before any chip test (plan item 10): the host test above, the mutant sweep, + the existing numeric, reduction and DIV/SQRT host regressions, and a + production build at the pinned PHYSICS version. + +Phase B, held until the queen says the GPU is released. Conversion order is +unchanged, with real GB10 tests before the next step starts: +1. The vector chip test, and the M17 and M18 gates for the edited + `omega_blackwell_submit.c` (the sha256 pin is re-sealed by a chip rerun). +2. Reduce launcher: the FIRST NUMERIC migration. +3. Load/store launcher. +4. Base executor (`omega_numeric_gb10.c`), then divsqrt, then the remaining + executors in `omega_blackwell_submit.c`. +5. A final PR deletes the old duplicated launch code. + +Converting `omega_numeric_gb10.c` and the remaining `omega_blackwell_submit.c` +executors also adds the semaphore wait and the C3 protection they lack today +(A1 headline 3 and 4): a behavior change, tested on the chip. The numeric +launchers do not link `omega_blackwell_submit.c` today (A1b section 1), so +their chip build recipes (`tests/run_numeric_gates.sh:156-163`, +`tests/run_reduce_chip.sh:64`, `tests/run_tensor_chip.sh:67`) must gain the +engine sources at the step that converts them. + +## 9. Order note + +Vector addition is the engine SEED: it proves the engine using the path +already inside `omega_blackwell_submit.c`. Reduce stays the first numeric +migration in Phase B. + +## 10. Unproven until chip + +Unproven assumptions: sufficiency of this synchronization sequence for the vector path; cache visibility under faults; safe recovery after uncertain completion; suitability for persistent kernels. Host tests cannot prove those. E1 untouched; this cut is not chip-qualified. + +## 11. Open questions and UNVERIFIED items + +Carried from A1 and A1b, plus new ones from this cut: +1. The E1 marker2 timeout case in section 2 is reported, receipt not read. +2. How `src/omega_numeric_gb10.c` is compiled in the main binary: not read, 70% + a glob or a tool compiles it (A1 open questions). +3. PR198 line numbers in this document may have drifted; the head moved from + 617971aa to 44741c1b (A1b section 6). +4. No GPU run was done by the scouts or by this cut; chip behavior is code + reading only. +5. The A1 scout said `omega_blackwell_submit.c` has no poison fill; the source + on this branch fills the vector output with `0xdeadbeef` (lines 74-80, and + at 286 for the matrix path). Poison is uniform there, so the wrapper will + supply an array filled with that word. +6. Whether the NUM files carry their own 18 setup words: 70% yes (A1b open + question 3). Three implementations of those words likely exist, which the + backend should unify. +7. `omega_world_dispatch_matmul` was read only by grep (A1b open question 5). +8. How `--run-m18-gates` is invoked from the Makefile or tools: 70% a tools + script (A1b open question 2). +9. A legitimate output equal to its poison word would be reported as unchanged. + The wrapper must pick poison guaranteed to differ from the expected output; + the engine cannot check that. Likelihood unmeasured. +10. The numeric code -6 for "unwritten" is a proposal only. +11. The clock used for `waited_ms` is left to A3. +12. Whether a failed SUBMIT can leave work queued on the GPU is unknown (no + evidence read). This cut treats SUBMIT as a known failure and runs cleanup; + the queen may want SUBMIT to count as uncertain too. UNVERIFIED, 50%. +13. Whether the COMPLETION_WAIT / RELEASE_WAIT split in section 4.4 matches + the queen's intent. + +## 12. Changes from the first draft (omega#218 commit 6e077f1) + +| first draft | now | +|---|---| +| `omega_gpu_engine_run(backend, ctx, request, result)`, `OmegaGpuEngineRequest/Result` | `omega_gpu_execute(job, result)`, `OmegaGpuJob/Result`, default backend setter | +| states NONE .. COMMITTED | states INITIAL .. SUCCESS | +| failures DEVICE_OPEN, DEVICE_ALLOC, DEVICE_SUBMIT, GPU_COMPLETION_TIMEOUT, OUTPUT_NOT_VISIBLE, OUTPUT_NOT_WRITTEN, DEVICE_ERROR | DEVICE_OPEN, CHANNEL_CREATE, ALLOC, PREPARE, SUBMIT, COMPLETION_WAIT, RELEASE_WAIT, VISIBILITY_WAIT, OUTPUT_UNCHANGED, INVALID_ARGS, INTERNAL_INVARIANT, CLEANUP, UNCERTAIN_COMPLETION_BLOCKED | +| wait name SEMAPHORE | wait name RELEASE_SEMAPHORE | +| step as a string | step as an enum with names | +| poison per output buffer, default 0x55555555, engine default | poison array supplied by the wrapper, engine never chooses | +| flag NO_POISON | removed: poison is always on | +| Phase A may not edit `omega_blackwell_submit.c` | Phase A may edit it as a DRAFT that cannot merge before the M17/M18 gates and the vector chip test | +| mutation tool: unspecified | shared shell runner `tools/mutation_runner.sh` | diff --git a/mk/omega-gpu-engine.mk b/mk/omega-gpu-engine.mk new file mode 100644 index 000000000..fc5cd08ca --- /dev/null +++ b/mk/omega-gpu-engine.mk @@ -0,0 +1,9 @@ +# Omega GPU Engine (docs/numeric/OMEGA_GPU_ENGINE.md), phase A2: compile check +# of the header and the stub. Picked up by `-include mk/*.mk`. Host only: opens +# no device, needs no physics headers, not part of `all` or `test`. +.PHONY: test-gpu-engine-compile +build/test_omega_gpu_engine_compile: tests/test_omega_gpu_engine_compile.c src/omega_gpu_engine.c src/omega_gpu_engine.h + @mkdir -p build + gcc -std=gnu11 -O2 -Wall -Wextra -Werror -Isrc -DOMEGA_NUMERIC_CPU_ONLY -o $@ tests/test_omega_gpu_engine_compile.c src/omega_gpu_engine.c +test-gpu-engine-compile: build/test_omega_gpu_engine_compile + ./build/test_omega_gpu_engine_compile diff --git a/src/omega_gpu_engine.c b/src/omega_gpu_engine.c new file mode 100644 index 000000000..4a30c2ff3 --- /dev/null +++ b/src/omega_gpu_engine.c @@ -0,0 +1,173 @@ +/* + * Omega GPU Engine, A2b compile stub. See src/omega_gpu_engine.h and + * docs/numeric/OMEGA_GPU_ENGINE.md. No launch logic yet: omega_gpu_execute + * checks the block, validates the job, then reports INTERNAL_INVARIANT at step + * NOT_IMPLEMENTED. The real engine core (A3) replaces omega_gpu_execute and + * sets the block on uncertain completion; the name functions, the backend + * pointer and the block query below are final. + */ +#include "omega_gpu_engine.h" + +#include +#include + +static const char *const FAILURE_NAMES[OMEGA_GPU_ENGINE_FAILURE_COUNT] = { + "OK", + "DEVICE_OPEN", + "CHANNEL_CREATE", + "ALLOC", + "PREPARE", + "SUBMIT", + "COMPLETION_WAIT", + "RELEASE_WAIT", + "VISIBILITY_WAIT", + "OUTPUT_UNCHANGED", + "INVALID_ARGS", + "INTERNAL_INVARIANT", + "CLEANUP", + "UNCERTAIN_COMPLETION_BLOCKED", +}; + +static const char *const STATE_NAMES[OMEGA_GPU_ENGINE_STATE_COUNT] = { + "INITIAL", + "PREPARED", + "SUBMITTED", + "GPU_COMPLETE", + "OUTPUT_VISIBLE", + "OUTPUT_PRODUCED", + "SUCCESS", +}; + +static const char *const WAIT_NAMES[OMEGA_GPU_ENGINE_WAIT_COUNT] = { + "NONE", + "MARKER", + "RELEASE_SEMAPHORE", + "MARKER2", +}; + +static const char *const STEP_NAMES[OMEGA_GPU_ENGINE_STEP_COUNT] = { + "NONE", + "VALIDATE", + "BACKEND_CHECK", + "BLOCKED", + "NOT_IMPLEMENTED", + "OPEN_DEVICE", + "CREATE_CHANNEL", + "ALLOC", + "COPY_IN", + "FILL_POISON", + "BUILD", + "SUBMIT", + "WAIT_MARKER", + "WAIT_MARKER2", + "WAIT_RELEASE_SEMAPHORE", + "BARRIER", + "COPY_OUT", + "SCAN", + "FREE", + "CLOSE", +}; + +static _Atomic(const OmegaGpuBackend *) g_backend; +static atomic_int g_blocked; + +const char *omega_gpu_engine_failure_name(int failure) +{ + if (failure < 0 || failure >= (int)OMEGA_GPU_ENGINE_FAILURE_COUNT) return "UNKNOWN"; + return FAILURE_NAMES[failure]; +} + +const char *omega_gpu_engine_state_name(int state) +{ + if (state < 0 || state >= (int)OMEGA_GPU_ENGINE_STATE_COUNT) return "UNKNOWN"; + return STATE_NAMES[state]; +} + +const char *omega_gpu_engine_wait_name(int wait) +{ + if (wait < 0 || wait >= (int)OMEGA_GPU_ENGINE_WAIT_COUNT) return "UNKNOWN"; + return WAIT_NAMES[wait]; +} + +const char *omega_gpu_engine_step_name(int step) +{ + if (step < 0 || step >= (int)OMEGA_GPU_ENGINE_STEP_COUNT) return "UNKNOWN"; + return STEP_NAMES[step]; +} + +void omega_gpu_engine_set_backend(const OmegaGpuBackend *backend) +{ + atomic_store(&g_backend, backend); +} + +int omega_gpu_engine_is_blocked(void) +{ + return atomic_load(&g_blocked) ? 1 : 0; +} + +void omega_gpu_engine_test_reset_block(void) +{ + atomic_store(&g_blocked, 0); +} + +void omega_gpu_engine_test_force_block(void) +{ + atomic_store(&g_blocked, 1); +} + +static int backend_complete(const OmegaGpuBackend *b) +{ + return b->open_device && b->create_channel && b->alloc && b->copy_in && b->fill_poison && + b->build && b->submit && b->wait_marker && b->wait_marker2 && + b->wait_release_semaphore && b->barrier && b->copy_out && b->diagnostics && + b->free_buf && b->close; +} + +/* Returns 1 when the job is well formed for the VECTOR_1D layout. */ +static int job_valid(const OmegaGpuJob *job) +{ + uint64_t bytes; + if (!job->program || job->program_len == 0) return 0; + if (job->layout != OMEGA_GPU_LAYOUT_VECTOR_1D) return 0; + if (job->element_count == 0) return 0; + bytes = (uint64_t)job->element_count * 4u; + if (!job->input_a || (uint64_t)job->input_a_len != bytes) return 0; + if (!job->input_b || (uint64_t)job->input_b_len != bytes) return 0; + if (!job->output || (uint64_t)job->output_len != bytes) return 0; + if (((uintptr_t)job->output & 3u) != 0) return 0; + if (!job->poison || (uint64_t)job->poison_count != (uint64_t)job->element_count) return 0; + if ((job->flags & ~(uint32_t)OMEGA_GPU_ENGINE_FLAG_NO_C3) != 0) return 0; + if (job->timeouts.marker_ms == 0) return 0; + if (job->timeouts.release_semaphore_ms == 0) return 0; + if (job->timeouts.visibility_ms == 0) return 0; + if ((job->flags & OMEGA_GPU_ENGINE_FLAG_NO_C3) == 0 && job->timeouts.marker2_ms == 0) return 0; + return 1; +} + +static int stop(OmegaGpuResult *result, int failure, int step) +{ + result->failure = failure; + result->failed_step = step; + return failure; +} + +int omega_gpu_execute(const OmegaGpuJob *job, OmegaGpuResult *result) +{ + const OmegaGpuBackend *b; + if (!result) return OMEGA_GPU_ENGINE_INVALID_ARGS; + memset(result, 0, sizeof *result); + result->failure = OMEGA_GPU_ENGINE_OK; + result->last_state = OMEGA_GPU_ENGINE_STATE_INITIAL; + result->failed_step = OMEGA_GPU_ENGINE_STEP_NONE; + result->wait = OMEGA_GPU_ENGINE_WAIT_NONE; + if (omega_gpu_engine_is_blocked()) + return stop(result, OMEGA_GPU_ENGINE_UNCERTAIN_COMPLETION_BLOCKED, + OMEGA_GPU_ENGINE_STEP_BLOCKED); + if (!job || !job_valid(job)) + return stop(result, OMEGA_GPU_ENGINE_INVALID_ARGS, OMEGA_GPU_ENGINE_STEP_VALIDATE); + b = atomic_load(&g_backend); + if (b && !backend_complete(b)) + return stop(result, OMEGA_GPU_ENGINE_INTERNAL_INVARIANT, + OMEGA_GPU_ENGINE_STEP_BACKEND_CHECK); + return stop(result, OMEGA_GPU_ENGINE_INTERNAL_INVARIANT, OMEGA_GPU_ENGINE_STEP_NOT_IMPLEMENTED); +} diff --git a/src/omega_gpu_engine.h b/src/omega_gpu_engine.h new file mode 100644 index 000000000..cc72ea57c --- /dev/null +++ b/src/omega_gpu_engine.h @@ -0,0 +1,243 @@ +/* + * Omega GPU Engine: run one already-built GPU kernel job and return SUCCESS or + * exactly one named failure. + * + * Purpose: today several launcher files repeat the same open / alloc / copy-in / + * pushbuffer / submit / completion-wait / readback / close sequence with three + * different error styles. This module owns that sequence once. Design: + * docs/numeric/OMEGA_GPU_ENGINE.md (the first-cut plan is Drake's, 2026-10-02). + * + * Public entry point: + * int omega_gpu_execute(const OmegaGpuJob *job, OmegaGpuResult *result); + * It returns OMEGA_GPU_ENGINE_OK (0) on success, otherwise the failure code, + * which is also stored in result->failure. + * + * States: INITIAL -> PREPARED -> SUBMITTED -> GPU_COMPLETE -> OUTPUT_VISIBLE -> + * OUTPUT_PRODUCED -> SUCCESS. The result records the last completed state and + * the precise failed step. (The earlier draft of this header called the final + * state COMMITTED and the first one NONE.) + * + * Poison: the wrapper supplies one poison word per output word. The engine + * writes those words to the device output before submit and, after readback, + * checks only whether any word is still equal to its poison word. The engine + * never chooses poison and never judges whether the math is right. + * + * Uncertain completion: if the engine cannot tell whether the GPU finished (a + * completion wait timed out, or the device returned an unknown state), it keeps + * the device context and every allocation (no free, no close), records that in + * the result, and blocks every later omega_gpu_execute call in this process, + * which then returns UNCERTAIN_COMPLETION_BLOCKED. + * + * STATUS: A2b stub. omega_gpu_execute launches nothing yet. It validates its + * arguments and reports INTERNAL_INVARIANT at step NOT_IMPLEMENTED. This cut is + * not chip-qualified (host NOT_RUN, chip NOT_RUN). + */ +#ifndef OMEGA_GPU_ENGINE_H +#define OMEGA_GPU_ENGINE_H + +#include +#include + +/* One failure name per run. OK is 0, so the return value reads as a boolean. */ +typedef enum { + OMEGA_GPU_ENGINE_OK = 0, + OMEGA_GPU_ENGINE_DEVICE_OPEN, /* device could not be opened */ + OMEGA_GPU_ENGINE_CHANNEL_CREATE, /* channel could not be created */ + OMEGA_GPU_ENGINE_ALLOC, /* a device buffer could not be allocated */ + OMEGA_GPU_ENGINE_PREPARE, /* copy-in, poison fill or descriptor build failed */ + OMEGA_GPU_ENGINE_SUBMIT, /* submitting the work failed */ + OMEGA_GPU_ENGINE_COMPLETION_WAIT, /* marker or marker2 wait failed or timed out */ + OMEGA_GPU_ENGINE_RELEASE_WAIT, /* release semaphore wait failed or timed out */ + OMEGA_GPU_ENGINE_VISIBILITY_WAIT, /* barrier or readback failed or timed out */ + OMEGA_GPU_ENGINE_OUTPUT_UNCHANGED, /* after readback, output words still equal their poison */ + OMEGA_GPU_ENGINE_INVALID_ARGS, /* the job or the result pointer is malformed */ + OMEGA_GPU_ENGINE_INTERNAL_INVARIANT, /* the engine or its backend broke its own contract */ + OMEGA_GPU_ENGINE_CLEANUP, /* free or close failed after an otherwise good run */ + OMEGA_GPU_ENGINE_UNCERTAIN_COMPLETION_BLOCKED, /* an earlier job left completion uncertain */ + OMEGA_GPU_ENGINE_FAILURE_COUNT /* not a failure: number of names above */ +} OmegaGpuEngineFailure; + +/* How far a run got. Success is only SUCCESS. */ +typedef enum { + OMEGA_GPU_ENGINE_STATE_INITIAL = 0, /* nothing completed yet (device may be unopened) */ + OMEGA_GPU_ENGINE_STATE_PREPARED, + OMEGA_GPU_ENGINE_STATE_SUBMITTED, + OMEGA_GPU_ENGINE_STATE_GPU_COMPLETE, + OMEGA_GPU_ENGINE_STATE_OUTPUT_VISIBLE, + OMEGA_GPU_ENGINE_STATE_OUTPUT_PRODUCED, + OMEGA_GPU_ENGINE_STATE_SUCCESS, + OMEGA_GPU_ENGINE_STATE_COUNT /* not a state: number of names above */ +} OmegaGpuEngineState; + +/* Which completion wait failed (meaningful for COMPLETION_WAIT and RELEASE_WAIT). */ +typedef enum { + OMEGA_GPU_ENGINE_WAIT_NONE = 0, + OMEGA_GPU_ENGINE_WAIT_MARKER, + OMEGA_GPU_ENGINE_WAIT_RELEASE_SEMAPHORE, + OMEGA_GPU_ENGINE_WAIT_MARKER2, + OMEGA_GPU_ENGINE_WAIT_COUNT /* not a wait: number of names above */ +} OmegaGpuEngineWait; + +/* The precise step that failed (the step the run was in when it stopped). */ +typedef enum { + OMEGA_GPU_ENGINE_STEP_NONE = 0, /* no step failed (success) */ + OMEGA_GPU_ENGINE_STEP_VALIDATE, + OMEGA_GPU_ENGINE_STEP_BACKEND_CHECK, + OMEGA_GPU_ENGINE_STEP_BLOCKED, + OMEGA_GPU_ENGINE_STEP_NOT_IMPLEMENTED, + OMEGA_GPU_ENGINE_STEP_OPEN_DEVICE, + OMEGA_GPU_ENGINE_STEP_CREATE_CHANNEL, + OMEGA_GPU_ENGINE_STEP_ALLOC, + OMEGA_GPU_ENGINE_STEP_COPY_IN, + OMEGA_GPU_ENGINE_STEP_FILL_POISON, + OMEGA_GPU_ENGINE_STEP_BUILD, + OMEGA_GPU_ENGINE_STEP_SUBMIT, + OMEGA_GPU_ENGINE_STEP_WAIT_MARKER, + OMEGA_GPU_ENGINE_STEP_WAIT_MARKER2, + OMEGA_GPU_ENGINE_STEP_WAIT_RELEASE_SEMAPHORE, + OMEGA_GPU_ENGINE_STEP_BARRIER, + OMEGA_GPU_ENGINE_STEP_COPY_OUT, + OMEGA_GPU_ENGINE_STEP_SCAN, + OMEGA_GPU_ENGINE_STEP_FREE, + OMEGA_GPU_ENGINE_STEP_CLOSE, + OMEGA_GPU_ENGINE_STEP_COUNT /* not a step: number of names above */ +} OmegaGpuEngineStep; + +/* Argument layouts the engine accepts. Later layouts are separate qualified cuts. */ +typedef enum { + OMEGA_GPU_LAYOUT_VECTOR_1D = 0, /* two inputs, one output, element_count 32-bit words each */ + OMEGA_GPU_LAYOUT_COUNT /* not a layout: number of layouts above */ +} OmegaGpuLayout; + +/* Job flags. A zero flags word means every protection ON. */ +#define OMEGA_GPU_ENGINE_FLAG_NO_C3 0x1u /* A/B control arm only: skip L2 flush and marker2 */ + +#define OMEGA_GPU_ENGINE_JOB_OUTPUTS 1u /* VECTOR_1D has exactly one output buffer */ +#define OMEGA_GPU_ENGINE_SEMAPHORE_WORDS 1u /* the vector path uses one semaphore word */ +#define OMEGA_GPU_ENGINE_DRV_TEXT_BYTES 128u + +/* Per-step timeouts in milliseconds. Never hard-coded by the engine. Each wait + * that is enabled needs a value greater than 0 (marker2_ms is ignored with NO_C3). */ +typedef struct { + uint64_t marker_ms; + uint64_t release_semaphore_ms; + uint64_t marker2_ms; + uint64_t visibility_ms; +} OmegaGpuTimeouts; + +typedef struct { + const void *program; /* encoded program bytes, already built by the caller */ + size_t program_len; /* bytes, > 0 */ + int layout; /* OmegaGpuLayout; only VECTOR_1D today */ + uint32_t element_count; /* > 0; each buffer below holds element_count 32-bit words */ + const void *input_a; /* element_count * 4 bytes */ + size_t input_a_len; + const void *input_b; /* element_count * 4 bytes */ + size_t input_b_len; + void *output; /* host destination, 4-byte aligned, element_count * 4 bytes */ + size_t output_len; + const uint32_t *poison; /* wrapper-supplied, one word per output word */ + size_t poison_count; /* must equal element_count */ + OmegaGpuTimeouts timeouts; + uint32_t flags; /* OMEGA_GPU_ENGINE_FLAG_* */ +} OmegaGpuJob; + +typedef struct { + int failure; /* OmegaGpuEngineFailure, OK on success */ + int last_state; /* last COMPLETED OmegaGpuEngineState */ + int failed_step; /* OmegaGpuEngineStep, NONE on success */ + int wait; /* OmegaGpuEngineWait, NONE unless a completion or release wait failed */ + uint64_t wait_timeout_ms; /* the configured limit of that wait */ + uint64_t waited_ms; /* how long the engine actually waited before giving up */ + int drv_rc; /* driver return code of the failing call, 0 if none */ + int err_no; /* errno captured right after the failing call, 0 if none */ + char drv_text[OMEGA_GPU_ENGINE_DRV_TEXT_BYTES]; /* driver message, NUL terminated, "" if none */ + uint32_t sync_valid; /* OMEGA_GPU_SYNC_* mask: which observed values below are meaningful */ + uint32_t marker; /* observed first marker word */ + uint32_t marker2; /* observed second marker word */ + uint32_t semaphore[OMEGA_GPU_ENGINE_SEMAPHORE_WORDS]; /* observed semaphore words */ + int retained; /* 1 when context and allocations were kept (uncertain completion) */ + int cleanup_failed; /* 1 when free or close failed (first failure is never overwritten) */ + size_t n_outputs; /* number of valid entries in output_unchanged_words */ + uint64_t output_unchanged_words[OMEGA_GPU_ENGINE_JOB_OUTPUTS]; /* words still equal to poison, per output */ +} OmegaGpuResult; + +#define OMEGA_GPU_SYNC_MARKER 0x1u +#define OMEGA_GPU_SYNC_MARKER2 0x2u +#define OMEGA_GPU_SYNC_SEMAPHORE 0x4u + +/* + * Backend seam. A fake backend implements these to drive every transition in + * host tests; the Blackwell GB10 backend (src/omega_blackwell_engine.c, later + * cut) wraps the m16_native_* calls the launchers use today. All callbacks + * must be non-NULL. Every callback returns 0 on success. The three wait + * callbacks and the barrier may also return: + */ +#define OMEGA_GPU_BACKEND_TIMEOUT 1 /* the wait ran out of time */ +#define OMEGA_GPU_BACKEND_UNKNOWN_STATE 2 /* the device reported a state the backend cannot classify */ +/* Any other nonzero value is a driver fault; the backend reports its raw driver code via diagnostics. */ + +typedef enum { + OMEGA_GPU_BUF_SCRATCH = 0, /* backend-internal: pushbuffer, marker page, QMD */ + OMEGA_GPU_BUF_PROGRAM, + OMEGA_GPU_BUF_INPUT_A, + OMEGA_GPU_BUF_INPUT_B, + OMEGA_GPU_BUF_OUTPUT, + OMEGA_GPU_BUF_ROLE_COUNT /* not a role: number of roles above */ +} OmegaGpuBufRole; + +typedef struct { + int drv_rc; + char drv_text[OMEGA_GPU_ENGINE_DRV_TEXT_BYTES]; + uint32_t sync_valid; /* OMEGA_GPU_SYNC_* */ + uint32_t marker; + uint32_t marker2; + uint32_t semaphore[OMEGA_GPU_ENGINE_SEMAPHORE_WORDS]; +} OmegaGpuBackendDiag; + +typedef struct { + void *ctx; /* passed to every callback; owned by the backend */ + int (*open_device)(void *ctx); + int (*create_channel)(void *ctx); + int (*alloc)(void *ctx, int role, size_t bytes); + int (*copy_in)(void *ctx, int role, const void *src, size_t bytes); + int (*fill_poison)(void *ctx, const uint32_t *words, size_t count); /* device-side output pre-fill */ + int (*build)(void *ctx, const OmegaGpuJob *job); /* descriptors, QMD, pushbuffer */ + int (*submit)(void *ctx); + int (*wait_marker)(void *ctx, uint64_t timeout_ms); + int (*wait_marker2)(void *ctx, uint64_t timeout_ms); + int (*wait_release_semaphore)(void *ctx, uint64_t timeout_ms); + int (*barrier)(void *ctx, uint64_t timeout_ms); /* visibility before readback */ + int (*copy_out)(void *ctx, void *dst, size_t bytes); + void (*diagnostics)(void *ctx, OmegaGpuBackendDiag *diag); /* driver rc, text, observed words */ + int (*free_buf)(void *ctx, int role); + int (*close)(void *ctx); +} OmegaGpuBackend; + +/* Runs one job on the process-wide default backend. See the header comment. */ +int omega_gpu_execute(const OmegaGpuJob *job, OmegaGpuResult *result); + +/* Sets the process-wide default backend (NULL clears it). The pointer must stay + * valid while it is set. With none set, omega_gpu_execute returns the defined + * stub result: INTERNAL_INVARIANT at step NOT_IMPLEMENTED. Real code sets it; + * host tests set a fake. */ +void omega_gpu_engine_set_backend(const OmegaGpuBackend *backend); + +/* 1 once an uncertain completion has blocked this process, else 0. */ +int omega_gpu_engine_is_blocked(void); + +/* TEST ONLY. Clears the block. Production code must never call this: a blocked + * process stays blocked because the GPU may still be running. */ +void omega_gpu_engine_test_reset_block(void); + +/* TEST ONLY. Forces the blocked state so tests can exercise the blocked + * result without a real uncertain completion. */ +void omega_gpu_engine_test_force_block(void); + +/* Static name strings, never NULL. Out-of-range values give "UNKNOWN". */ +const char *omega_gpu_engine_failure_name(int failure); +const char *omega_gpu_engine_state_name(int state); +const char *omega_gpu_engine_wait_name(int wait); +const char *omega_gpu_engine_step_name(int step); + +#endif diff --git a/tests/test_omega_gpu_engine_compile.c b/tests/test_omega_gpu_engine_compile.c new file mode 100644 index 000000000..b7ac55725 --- /dev/null +++ b/tests/test_omega_gpu_engine_compile.c @@ -0,0 +1,157 @@ +/* Omega GPU Engine A2b: the stub returns its defined results, every enum name + * is distinct, and the uncertain-completion block functions behave as specified. */ +#include "omega_gpu_engine.h" +#include +#include + +static int bad; +#define CHECK(c) do { if (!(c)) { printf("[FAIL] line %d: %s\n", __LINE__, #c); bad++; } } while (0) + +static int f_ctx(void *c) { (void)c; return 0; } +static int f_alloc(void *c, int r, size_t n) { (void)c; (void)r; (void)n; return 0; } +static int f_in(void *c, int r, const void *s, size_t n) { (void)c; (void)r; (void)s; (void)n; return 0; } +static int f_poison(void *c, const uint32_t *w, size_t n) { (void)c; (void)w; (void)n; return 0; } +static int f_build(void *c, const OmegaGpuJob *j) { (void)c; (void)j; return 0; } +static int f_wait(void *c, uint64_t ms) { (void)c; (void)ms; return 0; } +static int f_out(void *c, void *d, size_t n) { (void)c; (void)d; (void)n; return 0; } +static void f_diag(void *c, OmegaGpuBackendDiag *d) { (void)c; (void)d; } +static int f_free(void *c, int r) { (void)c; (void)r; return 0; } + +static int all_distinct(const char *(*name)(int), int count) +{ + for (int i = 0; i < count; i++) { + CHECK(name(i) != NULL && strcmp(name(i), "UNKNOWN") != 0); + for (int j = i + 1; j < count; j++) CHECK(strcmp(name(i), name(j)) != 0); + } + return strcmp(name(-1), "UNKNOWN") == 0 && strcmp(name(count), "UNKNOWN") == 0; +} + +/* Expects the stub's full defined result for a job refused at `step` with `failure`. */ +static void expect_result(const OmegaGpuResult *r, int failure, int step) +{ + CHECK(r->failure == failure); + CHECK(r->failed_step == step); + CHECK(r->last_state == OMEGA_GPU_ENGINE_STATE_INITIAL); + CHECK(r->wait == OMEGA_GPU_ENGINE_WAIT_NONE); + CHECK(r->wait_timeout_ms == 0 && r->waited_ms == 0); + CHECK(r->drv_rc == 0 && r->err_no == 0 && r->drv_text[0] == '\0'); + CHECK(r->sync_valid == 0 && r->marker == 0 && r->marker2 == 0 && r->semaphore[0] == 0); + CHECK(r->retained == 0 && r->cleanup_failed == 0); + CHECK(r->n_outputs == 0 && r->output_unchanged_words[0] == 0); +} + +int main(void) +{ + CHECK(all_distinct(omega_gpu_engine_failure_name, OMEGA_GPU_ENGINE_FAILURE_COUNT)); + CHECK(all_distinct(omega_gpu_engine_state_name, OMEGA_GPU_ENGINE_STATE_COUNT)); + CHECK(all_distinct(omega_gpu_engine_wait_name, OMEGA_GPU_ENGINE_WAIT_COUNT)); + CHECK(all_distinct(omega_gpu_engine_step_name, OMEGA_GPU_ENGINE_STEP_COUNT)); + CHECK(OMEGA_GPU_ENGINE_OK == 0 && strcmp(omega_gpu_engine_failure_name(0), "OK") == 0); + CHECK(strcmp(omega_gpu_engine_failure_name(OMEGA_GPU_ENGINE_COMPLETION_WAIT), "COMPLETION_WAIT") == 0); + CHECK(strcmp(omega_gpu_engine_failure_name(OMEGA_GPU_ENGINE_UNCERTAIN_COMPLETION_BLOCKED), + "UNCERTAIN_COMPLETION_BLOCKED") == 0); + CHECK(strcmp(omega_gpu_engine_state_name(OMEGA_GPU_ENGINE_STATE_INITIAL), "INITIAL") == 0); + CHECK(strcmp(omega_gpu_engine_state_name(OMEGA_GPU_ENGINE_STATE_SUCCESS), "SUCCESS") == 0); + CHECK(OMEGA_GPU_ENGINE_STATE_INITIAL == 0 && OMEGA_GPU_ENGINE_STATE_COUNT == 7); + CHECK(strcmp(omega_gpu_engine_wait_name(OMEGA_GPU_ENGINE_WAIT_RELEASE_SEMAPHORE), "RELEASE_SEMAPHORE") == 0); + CHECK(strcmp(omega_gpu_engine_step_name(OMEGA_GPU_ENGINE_STEP_NOT_IMPLEMENTED), "NOT_IMPLEMENTED") == 0); + + unsigned char code[4] = {0}; + uint32_t ina[4] = {0}, inb[4] = {0}, outw[4] = {0}, poison[4] = {1, 2, 3, 4}; + OmegaGpuJob job; + memset(&job, 0, sizeof job); + job.program = code; job.program_len = sizeof code; + job.layout = OMEGA_GPU_LAYOUT_VECTOR_1D; job.element_count = 4; + job.input_a = ina; job.input_a_len = sizeof ina; + job.input_b = inb; job.input_b_len = sizeof inb; + job.output = outw; job.output_len = sizeof outw; + job.poison = poison; job.poison_count = 4; + job.timeouts.marker_ms = 1; job.timeouts.release_semaphore_ms = 1; + job.timeouts.marker2_ms = 1; job.timeouts.visibility_ms = 1; + + OmegaGpuResult r; + + /* Not blocked at start; no backend set: the defined stub result. */ + omega_gpu_engine_test_reset_block(); + CHECK(omega_gpu_engine_is_blocked() == 0); + memset(&r, 0x7f, sizeof r); + CHECK(omega_gpu_execute(&job, &r) == OMEGA_GPU_ENGINE_INTERNAL_INVARIANT); + expect_result(&r, OMEGA_GPU_ENGINE_INTERNAL_INVARIANT, OMEGA_GPU_ENGINE_STEP_NOT_IMPLEMENTED); + + /* A missing result pointer is refused and nothing is written. */ + CHECK(omega_gpu_execute(&job, NULL) == OMEGA_GPU_ENGINE_INVALID_ARGS); + + /* Counterexamples: each malformed job is INVALID_ARGS at VALIDATE, never NOT_IMPLEMENTED. */ + memset(&r, 0x7f, sizeof r); + CHECK(omega_gpu_execute(NULL, &r) == OMEGA_GPU_ENGINE_INVALID_ARGS); + expect_result(&r, OMEGA_GPU_ENGINE_INVALID_ARGS, OMEGA_GPU_ENGINE_STEP_VALIDATE); + + OmegaGpuJob bad_job; +#define EXPECT_INVALID(edit) do { bad_job = job; edit; memset(&r, 0x7f, sizeof r); \ + CHECK(omega_gpu_execute(&bad_job, &r) == OMEGA_GPU_ENGINE_INVALID_ARGS); \ + expect_result(&r, OMEGA_GPU_ENGINE_INVALID_ARGS, OMEGA_GPU_ENGINE_STEP_VALIDATE); } while (0) + EXPECT_INVALID(bad_job.program = NULL); + EXPECT_INVALID(bad_job.program_len = 0); + EXPECT_INVALID(bad_job.layout = OMEGA_GPU_LAYOUT_COUNT); + EXPECT_INVALID(bad_job.element_count = 0); + EXPECT_INVALID(bad_job.input_a = NULL); + EXPECT_INVALID(bad_job.input_a_len = sizeof ina - 4); + EXPECT_INVALID(bad_job.input_b = NULL); + EXPECT_INVALID(bad_job.input_b_len = sizeof inb + 4); + EXPECT_INVALID(bad_job.output = NULL); + EXPECT_INVALID(bad_job.output_len = sizeof outw - 4); + EXPECT_INVALID(bad_job.output = (unsigned char *)outw + 1); /* not 4-byte aligned */ + EXPECT_INVALID(bad_job.poison = NULL); + EXPECT_INVALID(bad_job.poison_count = 3); + EXPECT_INVALID(bad_job.flags = 0x2u); /* unknown flag */ + EXPECT_INVALID(bad_job.timeouts.marker_ms = 0); + EXPECT_INVALID(bad_job.timeouts.release_semaphore_ms = 0); + EXPECT_INVALID(bad_job.timeouts.visibility_ms = 0); + EXPECT_INVALID(bad_job.timeouts.marker2_ms = 0); /* C3 on, so marker2 needs a timeout */ + + /* With NO_C3 a zero marker2 timeout is allowed. */ + bad_job = job; bad_job.flags = OMEGA_GPU_ENGINE_FLAG_NO_C3; bad_job.timeouts.marker2_ms = 0; + memset(&r, 0x7f, sizeof r); + CHECK(omega_gpu_execute(&bad_job, &r) == OMEGA_GPU_ENGINE_INTERNAL_INVARIANT); + expect_result(&r, OMEGA_GPU_ENGINE_INTERNAL_INVARIANT, OMEGA_GPU_ENGINE_STEP_NOT_IMPLEMENTED); + + /* Backend table: incomplete is refused, complete reaches the stub result, NULL clears. */ + OmegaGpuBackend be = { + .ctx = NULL, .open_device = f_ctx, .create_channel = f_ctx, .alloc = f_alloc, + .copy_in = f_in, .fill_poison = f_poison, .build = f_build, .submit = f_ctx, + .wait_marker = f_wait, .wait_marker2 = f_wait, .wait_release_semaphore = f_wait, + .barrier = f_wait, .copy_out = f_out, .diagnostics = f_diag, .free_buf = f_free, + .close = f_ctx, + }; + omega_gpu_engine_set_backend(&be); + memset(&r, 0x7f, sizeof r); + CHECK(omega_gpu_execute(&job, &r) == OMEGA_GPU_ENGINE_INTERNAL_INVARIANT); + expect_result(&r, OMEGA_GPU_ENGINE_INTERNAL_INVARIANT, OMEGA_GPU_ENGINE_STEP_NOT_IMPLEMENTED); + be.close = NULL; + memset(&r, 0x7f, sizeof r); + CHECK(omega_gpu_execute(&job, &r) == OMEGA_GPU_ENGINE_INTERNAL_INVARIANT); + expect_result(&r, OMEGA_GPU_ENGINE_INTERNAL_INVARIANT, OMEGA_GPU_ENGINE_STEP_BACKEND_CHECK); + omega_gpu_engine_set_backend(NULL); + memset(&r, 0x7f, sizeof r); + CHECK(omega_gpu_execute(&job, &r) == OMEGA_GPU_ENGINE_INTERNAL_INVARIANT); + expect_result(&r, OMEGA_GPU_ENGINE_INTERNAL_INVARIANT, OMEGA_GPU_ENGINE_STEP_NOT_IMPLEMENTED); + + /* Block: forced block refuses every job, even a malformed one, until reset. */ + omega_gpu_engine_test_force_block(); + CHECK(omega_gpu_engine_is_blocked() == 1); + memset(&r, 0x7f, sizeof r); + CHECK(omega_gpu_execute(&job, &r) == OMEGA_GPU_ENGINE_UNCERTAIN_COMPLETION_BLOCKED); + expect_result(&r, OMEGA_GPU_ENGINE_UNCERTAIN_COMPLETION_BLOCKED, OMEGA_GPU_ENGINE_STEP_BLOCKED); + memset(&r, 0x7f, sizeof r); + CHECK(omega_gpu_execute(NULL, &r) == OMEGA_GPU_ENGINE_UNCERTAIN_COMPLETION_BLOCKED); + expect_result(&r, OMEGA_GPU_ENGINE_UNCERTAIN_COMPLETION_BLOCKED, OMEGA_GPU_ENGINE_STEP_BLOCKED); + CHECK(omega_gpu_engine_is_blocked() == 1); /* a refusal does not clear the block */ + omega_gpu_engine_test_reset_block(); + CHECK(omega_gpu_engine_is_blocked() == 0); + memset(&r, 0x7f, sizeof r); + CHECK(omega_gpu_execute(&job, &r) == OMEGA_GPU_ENGINE_INTERNAL_INVARIANT); + expect_result(&r, OMEGA_GPU_ENGINE_INTERNAL_INVARIANT, OMEGA_GPU_ENGINE_STEP_NOT_IMPLEMENTED); + + printf("SUMMARY failed=%d\n", bad); + return bad ? 1 : 0; +}