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
omega-mv / src / omega_gpu_cmdlist.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef OMEGA_GPU_CMDLIST_H |
| 18 | #define OMEGA_GPU_CMDLIST_H |
| 19 | /* |
| 20 | * omega_gpu_cmdlist: many kernel launches in ONE submission (OWN-30B W-L2, decode launch collapse, 2026-10-13). |
| 21 | * |
| 22 | * Why: omega_gpu_session_launch submits one kernel per GPFIFO entry and polls two markers before it returns, so a |
| 23 | * Qwen3-30B decode token (289 launches) pays the submit + completion round trip 289 times. A command list records N |
| 24 | * launches, then submits them as one method stream with one completion (the C3 tail: marker, L2_FLUSH_DIRTY, second |
| 25 | * marker on the uncached page) at the end. |
| 26 | * |
| 27 | * What each launch gets (all buffers from omega_gpu_session_alloc, GPU-uncached; nvos.h:1117 read 2026-10-13): |
| 28 | * - its own constant bank 0 (1 KB: the 224 driver words patched for this bank, arguments at 0x380), CPU-written; |
| 29 | * - its own QMD slot (the session's QMD1 words with this launch's bank and semaphore), CPU-written; |
| 30 | * (completion markers: the session marker page at 0x100, the one page the platform lets the host poll) |
| 31 | * - its own release semaphore word, set to INTERMEDIATE_INIT (5) by the CPU and released to DONE (6) by the QMD when |
| 32 | * the grid completes. submit() checks every word: a launch that never ran is caught by name (index). |
| 33 | * |
| 34 | * Ordering between launches (the race this module must not create): Mesa NVK, the working open driver for this class, |
| 35 | * serialises dependent dispatches with WAIT_FOR_IDLE (NVCEC0 0x0110) or INVALIDATE_SHADER_CACHES ("This is also |
| 36 | * implicitly a WFI", nvk_cmd_buffer.c:633); without one, consecutive grids of one channel may overlap. Every launch |
| 37 | * here is preceded by WAIT_FOR_IDLE and the session's INVALIDATE_SHADER_CACHES_ALL (instruction, data, constant). |
| 38 | * OMEGA_GPU_CMDLIST_BARRIER_NONE exists only as the negative control of the chip test. |
| 39 | * |
| 40 | * Launch methods (opts.mode): |
| 41 | * PCAS QMD in memory, SEND_PCAS_A (qmd >> 8) + SEND_SIGNALING_PCAS2_B INVALIDATE_COPY_SCHEDULE (clcec0.h 0x02b4 / |
| 42 | * 0x02c0; the NVK dispatch sequence, nvk_cmd_dispatch.c), no dependent QMD: 8 words per launch, a whole |
| 43 | * decode token fits one 64 KB ring. |
| 44 | * INLINE the session's own chip-proven words per launch (QMD0 null grid + QMD1 inline), bank from memory: ~200 words |
| 45 | * per launch, so the list is cut into several submissions, each waited before the next (the ring is copied to |
| 46 | * offset 0 on every submit). |
| 47 | * |
| 48 | * Threading: the caller holds omega_gpu_session_lock() around add / submit (one channel, as for every API). |
| 49 | * Uncertain completion: a marker timeout latches this module and then forces the session latch (a 1 ms session |
| 50 | * launch queued behind the stuck list times out and latches the process), so no API keeps launching on a channel |
| 51 | * that may still be busy. |
| 52 | */ |
| 53 | #include "omega_gpu_session.h" |
| 54 | #include <stdint.h> |
| 55 | |
| 56 | #ifdef __cplusplus |
| 57 | extern "C" { |
| 58 | #endif |
| 59 | |
| 60 | #define OMEGA_GPU_CMDLIST_MAX_LAUNCHES 4096u |
| 61 | #define OMEGA_GPU_CMDLIST_BANK_BYTES 0x400u |
| 62 | #define OMEGA_GPU_CMDLIST_QMD_BYTES 0x400u /* QMD1 at +0, QMD0 (INLINE mode) at +0x200 */ |
| 63 | |
| 64 | typedef enum { OMEGA_GPU_CMDLIST_PCAS = 0, OMEGA_GPU_CMDLIST_INLINE = 1 } OmegaGpuCmdListMode; |
| 65 | /* Barrier before every launch after the first of a submission (the first always gets the full WFI + invalidate ALL): |
| 66 | * WFI WAIT_FOR_IDLE + INVALIDATE_SHADER_CACHES ALL (instruction, data, constant). The default since cut 1b. |
| 67 | * NONE nothing (TEST ONLY: negative control; consecutive grids may overlap). |
| 68 | * WFI_ONLY WAIT_FOR_IDLE, no cache invalidate (W-L2c launch-cost study: isolates the drain from the invalidate). |
| 69 | * WFI_DATA WAIT_FOR_IDLE + INVALIDATE_SHADER_CACHES {data} (keeps the instruction and constant caches warm). |
| 70 | * FLUSH_DATA INVALIDATE_SHADER_CACHES {data, flush_data} alone, which NVK uses for a storage-write to storage-read |
| 71 | * dependency between dispatches ("This is also implicitly a WFI", nvk_cmd_buffer.c:633). */ |
| 72 | typedef enum { OMEGA_GPU_CMDLIST_BARRIER_WFI = 0, OMEGA_GPU_CMDLIST_BARRIER_NONE = 1 /* TEST ONLY: negative control */, |
| 73 | OMEGA_GPU_CMDLIST_BARRIER_WFI_ONLY = 2, OMEGA_GPU_CMDLIST_BARRIER_WFI_DATA = 3, OMEGA_GPU_CMDLIST_BARRIER_FLUSH_DATA = 4 } OmegaGpuCmdListBarrier; |
| 74 | const char *omega_gpu_cmdlist_barrier_name(OmegaGpuCmdListBarrier b); |
| 75 | |
| 76 | typedef struct { |
| 77 | OmegaGpuCmdListMode mode; |
| 78 | OmegaGpuCmdListBarrier barrier; |
| 79 | uint64_t timeout_ms; /* per submission; 0 = 20000 */ |
| 80 | uint32_t spin_us; /* marker spin window before the platform poll; 0 = the session's process-wide setting */ |
| 81 | int stamps; /* 1: a GPU timestamp report before the first launch and after every launch (after its WAIT_FOR_IDLE), read |
| 82 | back by omega_gpu_cmdlist_stamps. Costs 6 words + the report per launch. 0 = off. */ |
| 83 | int skip_launch; /* TEST ONLY: >= 1 drops launch (skip_launch - 1) from the method stream (its slot stays |
| 84 | recorded), so the per-launch semaphore check must fail. 0 = off. */ |
| 85 | int first_barrier_none; /* C4 streams: no WAIT_FOR_IDLE + invalidate before launch 0 (the CPU-written code, banks and QMDs |
| 86 | must not sit in a stale cache line: fresh slots or unchanged contents). A WAIT_FOR_IDLE may also |
| 87 | wait for grids of another queue of the group; the streams test measures both. 0 = the barrier. */ |
| 88 | } OmegaGpuCmdListOpts; |
| 89 | |
| 90 | typedef struct { |
| 91 | uint32_t launches; /* recorded launches submitted */ |
| 92 | uint32_t submits; /* GPFIFO entries (1 in PCAS mode below the ring size) */ |
| 93 | uint32_t words; /* method words over all submissions */ |
| 94 | uint64_t elapsed_ns; /* first submit to last second marker */ |
| 95 | uint64_t host_ns; /* building the method stream (CPU) */ |
| 96 | uint32_t sems_done; /* launch semaphores found DONE */ |
| 97 | int32_t first_missing; /* index of the first launch whose semaphore was not DONE, -1 if none */ |
| 98 | } OmegaGpuCmdListStats; |
| 99 | |
| 100 | typedef struct OmegaGpuCmdList OmegaGpuCmdList; |
| 101 | |
| 102 | /* Takes the session lock itself. Allocates the slots for up to cap launches. 0 ok, -1 (session error names it). A list |
| 103 | * belongs to the device it was made on: after omega_gpu_session_recover, add / submit / wait_for refuse it (C5). */ |
| 104 | int omega_gpu_cmdlist_create(uint32_t cap, OmegaGpuCmdList **out); |
| 105 | void omega_gpu_cmdlist_free(OmegaGpuCmdList *cl); |
| 106 | /* Caller holds the lock. Empties the list (slots are rewritten by the next adds). */ |
| 107 | void omega_gpu_cmdlist_reset(OmegaGpuCmdList *cl); |
| 108 | uint32_t omega_gpu_cmdlist_count(const OmegaGpuCmdList *cl); |
| 109 | /* Caller holds the lock. Marks the most recently added launch as sharing NO data with the launch before it (neither reads |
| 110 | * what the other writes): submit emits no barrier before it, so the two grids may overlap on the chip (W-L2d: a weight |
| 111 | * prefetch beside the latency-bound attention). The launch after it gets the normal barrier, which waits for both. |
| 112 | * Ignored for the first launch of a submission. 0 ok, -1 (no launch recorded). */ |
| 113 | int omega_gpu_cmdlist_mark_independent(OmegaGpuCmdList *cl); |
| 114 | /* Caller holds the lock. Writes the launch's bank and QMD into the next slot. The launch's code must stay resident |
| 115 | * until submit returns. Returns the launch index, or -1 (full, bad arguments, QMD refused). L->timeout_ms and |
| 116 | * L->spin_us are ignored (the list has one completion). */ |
| 117 | int omega_gpu_cmdlist_add(OmegaGpuCmdList *cl, const OmegaGpuLaunch *L); |
| 118 | /* Single-token hd256 launch. The caller holds the session lock, as for cmdlist_add. */ |
| 119 | int omega_gpu_cmdlist_record_attn256(OmegaGpuCmdList *cl, const uint64_t spec[16], uint64_t code_va, |
| 120 | uint32_t gprs, uint32_t grid_y, uint32_t output_words); |
| 121 | /* Caller holds the lock, device open, not latched. Submits every recorded launch in order, waits for the completion, |
| 122 | * checks every launch semaphore. 0 ok; -1 with the stage named (omega_gpu_session_last_error). */ |
| 123 | int omega_gpu_cmdlist_submit(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *opts, OmegaGpuCmdListStats *st); |
| 124 | /* L136 (roadblock R3): caller holds the lock. List a on the session queue and list b on extra queue 1 |
| 125 | * (omega_gpu_session_extra_queue; needs AIEN_OMEGA_GPU_CHANNELS >= 2 at device open), both submitted before either is |
| 126 | * waited, so the two may run at the same time. The lists must share no written buffer. PCAS mode, no stamps, each list in |
| 127 | * one submission. Both stats get the pair wall time (first submit to the last marker). 0 ok; -1 with the stage named; |
| 128 | * a marker timeout latches the session. */ |
| 129 | int omega_gpu_cmdlist_submit_pair(OmegaGpuCmdList *a, OmegaGpuCmdList *b, const OmegaGpuCmdListOpts *opts, OmegaGpuCmdListStats *sa, OmegaGpuCmdListStats *sb); |
| 130 | /* C4 events. Caller holds the lock. The next launch added to cl waits, on the chip, until launch `index` of list `other` |
| 131 | * (already added there) has completed: a host semaphore ACQUIRE on that launch's release semaphore (DONE), emitted |
| 132 | * before the launch's barrier, which is always the full WAIT_FOR_IDLE + invalidate (whatever the options say), so the |
| 133 | * launch reads what the other launch wrote. A list waits on one other list only, and a list with waits runs only in |
| 134 | * submit_pair with that list (submit refuses it; submit_pair refuses a wait outside the pair or past its launches). |
| 135 | * 0 ok, -1 (no such launch, a second other list, or a list made before a session recovery). */ |
| 136 | int omega_gpu_cmdlist_wait_for(OmegaGpuCmdList *cl, const OmegaGpuCmdList *other, uint32_t index); |
| 137 | /* 1 once a timeout latched this module, until omega_gpu_session_recover (C5). */ |
| 138 | int omega_gpu_cmdlist_is_latched(void); |
| 139 | |
| 140 | /* After a submit with opts.stamps = 1: out[0] = GPU timestamp (ns, PTIMER) before launch 0, out[i + 1] = after launch i completed |
| 141 | * (taken after its WAIT_FOR_IDLE). Returns the count written (launches + 1) or -1 (stamps off, slot unwritten, cap too small). */ |
| 142 | int omega_gpu_cmdlist_stamps(const OmegaGpuCmdList *cl, uint64_t *out, uint32_t cap); |
| 143 | |
| 144 | #ifdef __cplusplus |
| 145 | } |
| 146 | #endif |
| 147 | #endif |