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_decode_graph.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_DECODE_GRAPH_H |
| 18 | #define OMEGA_GPU_DECODE_GRAPH_H |
| 19 | /* |
| 20 | * omega_gpu_decode_graph: one Qwen3-30B-A3B decode token as ONE command list (OWN-30B W-L2a, 2026-10-13). |
| 21 | * |
| 22 | * Today's token (sc qwen3_moe_omega.rs step()) is 6 synchronous launches per layer + lm_head with the residual x |
| 23 | * round-tripping through the host between them. Here x stays on the device and every step is recorded into one |
| 24 | * omega_gpu_cmdlist and submitted once: |
| 25 | * |
| 26 | * (layer 0 only) RES NORM xq = RMSNorm(x) * input_norm[0] * 2^64 (the FP8 x' row) |
| 27 | * per layer l: fp8 qkv qkv = W_qkv[l] . xq omega_gpu_fp8_gemv_grouped_record |
| 28 | * decode attn attn = attention(qkv, KV[l], pos) * 2^64 omega_gpu_decode_attn_record |
| 29 | * fp8 o o = W_o[l] . attn |
| 30 | * RES ADD x += o |
| 31 | * router xr = RMSNorm(x) * post_norm[l] * 2^64, ids, wts, tables omega_gpu_router_record |
| 32 | * fp8 gate_up gu[8][1536] = W_gu[ids] . xr (router's table) |
| 33 | * SWIGLU act[8][768] = silu(gate) * up * 2^64 |
| 34 | * fp8 down dn[8][2048] = W_dn[ids] . act[g] (router's table) |
| 35 | * RES COMBINE|NORM x += sum_g wts[g] * dn[g]; then |
| 36 | * xq = RMSNorm(x) * input_norm[l+1] * 2^64 (not the last layer), or |
| 37 | * hidden = RMSNorm(x) * final_norm (the last layer) |
| 38 | * final: bf16 lm_head logits = W_lm . hidden omega_gpu_bf16_gemv_record |
| 39 | * ARGMAX x2 token = lowest index of max(logits) (75 CTAs, then 1, for 151936) |
| 40 | * |
| 41 | * Launches: 9 per layer + 4 (48 layers: 436). The glue kernels are omega_blackwell_codegen_dec.h (chip parity PASS). |
| 42 | * Numerics vs the host-glue path: identical operations except the RMSNorm (f32 FFMA sum of squares + MUFU.RSQ vs the |
| 43 | * host's f64 sum and 1/sqrt) and SwiGLU (MUFU EX2/RCP vs expf): logits close, not bit-identical; the argmax is the |
| 44 | * host's rule (lowest index on ties). |
| 45 | * |
| 46 | * Memory: every buffer is omega_gpu_session_alloc (GPU-uncached; nvos.h:1117 "For system memory this will not be |
| 47 | * coherent with direct CPU mappings"): the host writes x before the submit and reads the token / logits after. |
| 48 | * |
| 49 | * Threading: begin_token takes omega_gpu_session_lock() and end_token (or abort_token) releases it. Between them only |
| 50 | * record calls (this file's and the four APIs' *_record) may run on this thread; a non-record API call would |
| 51 | * self-deadlock. |
| 52 | * Lock and token state after each call (omega #518; tests/test_omega_gpu_decode_graph_abort.c enforces it): |
| 53 | * begin_token OK: lock held, in the token; exactly one end_token or abort_token is owed. |
| 54 | * BAD_ARGS (NULL graph, already in a token): nothing taken, nothing changed. |
| 55 | * CHIP_FAIL (device open refused or latched, x / token visibility sync refused): lock RELEASED, out of |
| 56 | * the token, list empty; nothing owed (end_token and abort_token answer BAD_ARGS). Nothing was |
| 57 | * submitted, so nothing is uncertain; the next begin_token may simply be called again (a latched |
| 58 | * process fails it again at once: the graph never retries on its own). |
| 59 | * record_* lock still held, still in the token, whatever they return; a failure sticks (later records refuse). |
| 60 | * end_token always releases the lock and leaves the token, success or failure. |
| 61 | * abort_token releases the lock and leaves the token without submitting what was recorded. |
| 62 | * token (one-shot) returns with the lock released in every case. |
| 63 | * The four APIs refuse a record that would evict a cached kernel: run one plain call per shape (or one token with the |
| 64 | * host path) first, or simply record the first token (an empty cache slot is filled, never evicted). |
| 65 | * |
| 66 | * ============ C signatures (for the Rust binding) ============ |
| 67 | * int omega_gpu_decode_graph_create(const OmegaGpuDecodeGraphConfig *cfg, OmegaGpuDecodeGraph **out); |
| 68 | * void omega_gpu_decode_graph_free(OmegaGpuDecodeGraph *g); |
| 69 | * int omega_gpu_decode_graph_set_layer(OmegaGpuDecodeGraph *g, uint32_t layer, const OmegaGpuFp8Tensor *qkv, |
| 70 | * const OmegaGpuFp8Tensor *o, const OmegaGpuFp8Tensor *gate_up, const OmegaGpuFp8Tensor *down, const float *input_norm); |
| 71 | * int omega_gpu_decode_graph_set_final_norm(OmegaGpuDecodeGraph *g, const float *final_norm); |
| 72 | * int omega_gpu_decode_graph_token(OmegaGpuDecodeGraph *g, const float *x_embed, uint32_t pos, uint32_t *token, |
| 73 | * OmegaGpuDecodeGraphStats *st); begin + every layer + final + end |
| 74 | * int omega_gpu_decode_graph_begin_token(OmegaGpuDecodeGraph *g, const float *x_embed); |
| 75 | * int omega_gpu_decode_graph_record_layer(OmegaGpuDecodeGraph *g, uint32_t layer, uint32_t pos); |
| 76 | * int omega_gpu_decode_graph_record_final(OmegaGpuDecodeGraph *g); |
| 77 | * int omega_gpu_decode_graph_end_token(OmegaGpuDecodeGraph *g, uint32_t *token, OmegaGpuDecodeGraphStats *st); |
| 78 | * int omega_gpu_decode_graph_abort_token(OmegaGpuDecodeGraph *g); |
| 79 | * int omega_gpu_decode_graph_record_res(OmegaGpuDecodeGraph *g, uint32_t mode, uint64_t x_va, uint64_t a_va, uint64_t w_va, |
| 80 | * uint64_t nw_va, uint64_t y_va, float eps, float scale); |
| 81 | * int omega_gpu_decode_graph_record_swiglu(OmegaGpuDecodeGraph *g, uint64_t gu_va, uint64_t act_va, float scale); |
| 82 | * struct OmegaGpuCmdList *omega_gpu_decode_graph_list(OmegaGpuDecodeGraph *g); |
| 83 | * void omega_gpu_decode_graph_vas(const OmegaGpuDecodeGraph *g, OmegaGpuDecodeGraphVas *v); |
| 84 | * int omega_gpu_decode_graph_read(OmegaGpuDecodeGraph *g, float *x, float *hidden, float *logits); |
| 85 | * Return codes: 0 OK, negative OMEGA_GPU_DGRAPH_* (the failing stage in omega_gpu_session_last_error()). |
| 86 | */ |
| 87 | #include "omega_gpu_fp8_api.h" |
| 88 | #include "omega_gpu_router_api.h" |
| 89 | #include "omega_gpu_decode_attn_api.h" |
| 90 | #include "omega_gpu_bf16_gemv_api.h" |
| 91 | #include <stdint.h> |
| 92 | |
| 93 | #ifdef __cplusplus |
| 94 | extern "C" { |
| 95 | #endif |
| 96 | |
| 97 | enum { |
| 98 | OMEGA_GPU_DGRAPH_OK = 0, |
| 99 | OMEGA_GPU_DGRAPH_BAD_ARGS = -1, /* NULL, bad layer, shape mismatch, layer not set, call out of order */ |
| 100 | OMEGA_GPU_DGRAPH_CODEGEN_FAIL = -3, |
| 101 | OMEGA_GPU_DGRAPH_CHIP_FAIL = -4, /* device, alloc, a record refused, submit, semaphore, counter or output check */ |
| 102 | }; |
| 103 | |
| 104 | #define OMEGA_GPU_DGRAPH_HIDDEN 2048u |
| 105 | #define OMEGA_GPU_DGRAPH_QKV 5120u /* 32 * 128 + 2 * 4 * 128 */ |
| 106 | #define OMEGA_GPU_DGRAPH_ATTN 4096u /* 32 * 128 */ |
| 107 | #define OMEGA_GPU_DGRAPH_TOPK 8u |
| 108 | #define OMEGA_GPU_DGRAPH_INTER 768u |
| 109 | #define OMEGA_GPU_DGRAPH_MAX_LAYERS 64u |
| 110 | |
| 111 | typedef struct { |
| 112 | uint32_t layers; /* 1..OMEGA_GPU_DGRAPH_MAX_LAYERS, <= the router's and the KV cache's */ |
| 113 | uint32_t vocab; /* lm_head rows (151936 for Qwen3-30B-A3B), <= 2048 * 2048 */ |
| 114 | float eps; /* rms_norm_eps */ |
| 115 | int norm_topk; /* config.json norm_topk_prob (1 for Qwen3-30B-A3B) */ |
| 116 | OmegaGpuRouter *router; /* per-layer router weights, post-attention norms and expert directories set */ |
| 117 | OmegaGpuDecodeAttnCache *attn; /* KV cache with the per-layer q/k norms set */ |
| 118 | const OmegaGpuBf16Tensor *lm_head; /* [vocab][2048] */ |
| 119 | } OmegaGpuDecodeGraphConfig; |
| 120 | |
| 121 | typedef struct { |
| 122 | uint32_t launches; /* launches recorded for the token */ |
| 123 | uint32_t submits; /* submissions (1 normally; = launches with flush_each) */ |
| 124 | uint64_t chip_ns; /* sum of the list submissions' first-submit-to-last-marker time */ |
| 125 | uint64_t record_ns; /* host: begin_token to the submit (recording) */ |
| 126 | uint64_t total_ns; /* host: begin_token to end_token return */ |
| 127 | uint32_t sems_done; /* launch semaphores found DONE */ |
| 128 | } OmegaGpuDecodeGraphStats; |
| 129 | |
| 130 | /* Device addresses of the graph's buffers (all f32 unless noted). */ |
| 131 | typedef struct { |
| 132 | uint64_t x; /* [2048] residual, device resident across the token */ |
| 133 | uint64_t xq; /* [2048] input-norm x' (* 2^64) */ |
| 134 | uint64_t qkv; /* [5120] */ |
| 135 | uint64_t attn; /* [4096] attention output * 2^64 */ |
| 136 | uint64_t o; /* [2048] */ |
| 137 | uint64_t gu; /* [8][1536] */ |
| 138 | uint64_t act; /* [8][768] * 2^64 */ |
| 139 | uint64_t dn; /* [8][2048] */ |
| 140 | uint64_t hidden; /* [2048] final norm output */ |
| 141 | uint64_t logits; /* [ctas * 2048], tail padded with -1e30 */ |
| 142 | uint64_t token; /* u32 greedy token, then f32 its logit */ |
| 143 | } OmegaGpuDecodeGraphVas; |
| 144 | |
| 145 | typedef struct OmegaGpuDecodeGraph OmegaGpuDecodeGraph; |
| 146 | |
| 147 | int omega_gpu_decode_graph_create(const OmegaGpuDecodeGraphConfig *cfg, OmegaGpuDecodeGraph **out); |
| 148 | void omega_gpu_decode_graph_free(OmegaGpuDecodeGraph *g); |
| 149 | /* Shapes are checked: qkv [1][5120][2048], o [1][2048][4096], gate_up [128][1536][2048], down [128][2048][768]. |
| 150 | * input_norm: the checkpoint's input_layernorm.weight widened to f32 [2048]. The tensors stay owned by the caller. */ |
| 151 | int omega_gpu_decode_graph_set_layer(OmegaGpuDecodeGraph *g, uint32_t layer, const OmegaGpuFp8Tensor *qkv, const OmegaGpuFp8Tensor *o, |
| 152 | const OmegaGpuFp8Tensor *gate_up, const OmegaGpuFp8Tensor *down, const float *input_norm); |
| 153 | int omega_gpu_decode_graph_set_final_norm(OmegaGpuDecodeGraph *g, const float *final_norm); |
| 154 | |
| 155 | /* One whole token: x_embed (f32 [2048], the token's embedding row) -> greedy token. pos: the token's position. */ |
| 156 | int omega_gpu_decode_graph_token(OmegaGpuDecodeGraph *g, const float *x_embed, uint32_t pos, uint32_t *token, OmegaGpuDecodeGraphStats *st); |
| 157 | |
| 158 | /* The same in steps. begin_token takes the session lock, empties the list and (x_embed non-NULL) writes x. On |
| 159 | * CHIP_FAIL it has already released the lock and left the token (see Threading above): do not call end_token. */ |
| 160 | int omega_gpu_decode_graph_begin_token(OmegaGpuDecodeGraph *g, const float *x_embed); |
| 161 | int omega_gpu_decode_graph_record_layer(OmegaGpuDecodeGraph *g, uint32_t layer, uint32_t pos); |
| 162 | int omega_gpu_decode_graph_record_final(OmegaGpuDecodeGraph *g); |
| 163 | /* Submits, checks every launch semaphore, the attention split counters and the token output (poisoned before the |
| 164 | * submit), reads the token (may be NULL), releases the lock. Always releases the lock, also after an earlier failed |
| 165 | * record (then returns that failure without submitting). */ |
| 166 | int omega_gpu_decode_graph_end_token(OmegaGpuDecodeGraph *g, uint32_t *token, OmegaGpuDecodeGraphStats *st); |
| 167 | /* Abandons a begun token: drops the recorded launches WITHOUT submitting them, leaves the token, releases the lock. |
| 168 | * OK, or BAD_ARGS when no token is begun. Nothing reaches the device, so nothing is uncertain; launches flush_each |
| 169 | * already submitted stay done. The device x keeps what begin_token wrote; the timeline is cleared. */ |
| 170 | int omega_gpu_decode_graph_abort_token(OmegaGpuDecodeGraph *g); |
| 171 | |
| 172 | /* Glue records (between begin and end): RES (omega_blackwell_codegen_dec.h modes ADD / COMBINE / NORM) and SWIGLU. */ |
| 173 | int omega_gpu_decode_graph_record_res(OmegaGpuDecodeGraph *g, uint32_t mode, uint64_t x_va, uint64_t a_va, uint64_t w_va, |
| 174 | uint64_t nw_va, uint64_t y_va, float eps, float scale); |
| 175 | int omega_gpu_decode_graph_record_swiglu(OmegaGpuDecodeGraph *g, uint64_t gu_va, uint64_t act_va, float scale); |
| 176 | |
| 177 | struct OmegaGpuCmdList *omega_gpu_decode_graph_list(OmegaGpuDecodeGraph *g); |
| 178 | void omega_gpu_decode_graph_vas(const OmegaGpuDecodeGraph *g, OmegaGpuDecodeGraphVas *v); |
| 179 | /* Takes the lock; copies the device x, hidden and logits (vocab values) of the last token; any pointer may be NULL. */ |
| 180 | int omega_gpu_decode_graph_read(OmegaGpuDecodeGraph *g, float *x, float *hidden, float *logits); |
| 181 | |
| 182 | /* ---- timeline (measurement) ---- */ |
| 183 | /* One kind per recorded launch, in list order. */ |
| 184 | enum { |
| 185 | OMEGA_GPU_DGRAPH_K_NORM = 0, OMEGA_GPU_DGRAPH_K_QKV, OMEGA_GPU_DGRAPH_K_ATTN, OMEGA_GPU_DGRAPH_K_O, OMEGA_GPU_DGRAPH_K_ADD, |
| 186 | OMEGA_GPU_DGRAPH_K_ROUTER, OMEGA_GPU_DGRAPH_K_GATE_UP, OMEGA_GPU_DGRAPH_K_SWIGLU, OMEGA_GPU_DGRAPH_K_DOWN, OMEGA_GPU_DGRAPH_K_COMBINE, |
| 187 | OMEGA_GPU_DGRAPH_K_LM_HEAD, OMEGA_GPU_DGRAPH_K_ARGMAX, OMEGA_GPU_DGRAPH_K_COUNT |
| 188 | }; |
| 189 | /* 1: every token also records a GPU timestamp (ns) before the first launch and after each launch (after its WAIT_FOR_IDLE). |
| 190 | * Call between tokens. Adds one report per launch to the list. 0 = off (default). */ |
| 191 | void omega_gpu_decode_graph_set_stamps(OmegaGpuDecodeGraph *g, int on); |
| 192 | /* Barrier between the launches of a token: 0 WFI + invalidate all (default), 2 WFI only, 3 WFI + invalidate data, 4 NVK-style |
| 193 | * flush_data (implicit WFI). Process-wide, read once (env AIEN_OMEGA_DGRAPH_BARRIER when never set). 1 (none) is refused. */ |
| 194 | void omega_gpu_decode_graph_set_barrier(int b); |
| 195 | int omega_gpu_decode_graph_barrier(void); |
| 196 | /* The last token with stamps on: stamps[0 .. n] (n + 1 values), kinds[0 .. n). Returns n (launches) or BAD_ARGS |
| 197 | * (stamps off, the token failed, buffers smaller than launches + 1). Launch i took stamps[i + 1] - stamps[i] ns. */ |
| 198 | int omega_gpu_decode_graph_stamps(const OmegaGpuDecodeGraph *g, uint64_t *stamps, uint8_t *kinds, uint32_t cap); |
| 199 | const char *omega_gpu_decode_graph_kind_name(uint8_t kind); |
| 200 | |
| 201 | /* ---- TEST ONLY ---- */ |
| 202 | /* 1: submit after every launch (same kernels and arguments, one submission each: the batching comparison). */ |
| 203 | void omega_gpu_decode_graph_set_flush_each(OmegaGpuDecodeGraph *g, int on); |
| 204 | /* 1 SWIGLU mutant (up half read at +767); 2 COMBINE skips expert 7; 3 wrong wiring: attention written at scale 1 |
| 205 | * instead of 2^64 (the o_proj then sees x' 2^64 too small). 0 none. */ |
| 206 | void omega_gpu_decode_graph_test_set_mutant(OmegaGpuDecodeGraph *g, int mutant); |
| 207 | /* TEST ONLY: negative control for the barrier study: lets barrier 1 (none) through, which set_barrier refuses. The token is a |
| 208 | * dependency chain, so the comparison against the per-launch path (B) must fail. */ |
| 209 | void omega_gpu_decode_graph_test_set_barrier_none(void); |
| 210 | /* Weight prefetch beside the attention (W-L2d): 1 = each layer records an independent o_proj GEMV on scratch buffers right after |
| 211 | * the attention launch (no barrier between them), pulling the o_proj weights into L2 while the attention runs; useful only |
| 212 | * with GPU-cached weights (AIEN_OMEGA_FP8_WEIGHT_CACHE=1). Process-wide; env AIEN_OMEGA_DGRAPH_PREFETCH ("1") when never set. |
| 213 | * Outputs unchanged by construction (the prefetch writes only its own scratch y). Default 0. */ |
| 214 | void omega_gpu_decode_graph_set_prefetch(int on); |
| 215 | int omega_gpu_decode_graph_prefetch(void); |
| 216 | /* TEST ONLY red for the prefetch run: the real o_proj is also marked independent (unordered after the attention): must FAIL. */ |
| 217 | void omega_gpu_decode_graph_test_set_prefetch_red(int on); |
| 218 | |
| 219 | #ifdef __cplusplus |
| 220 | } |
| 221 | #endif |
| 222 | #endif |