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