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_q36_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_q36_graph.h` 198 lines · 16492 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 #ifndef OMEGA_GPU_Q36_GRAPH_H 3 #define OMEGA_GPU_Q36_GRAPH_H 4 /* 5 * omega_gpu_q36_graph: one Qwen3.6-35B-A3B decode token as ONE command list (sc L306, 2026-10-20). The layer mixers are the 6 * Qwen3.8-27B graph's (omega_gpu_q38_graph.h) at the 35B's shapes; the MLP is the 30B decode graph's sparse block 7 * (omega_gpu_decode_graph.h) with a ninth group, the shared expert: 8 * 9 * (layer 0 only) NORM xq = RMSNorm(x) * in_norm[0] * 2^64 (the FP8 GEMV x' row) 10 * GDN layer l: fp8 mix mix[12288] = W_qkvz[l] . xq (in_proj_qkv 8192 | in_proj_z 4096) 11 * bf16 ab ab[64] = W_ab[l] . xq (a/b rows interleaved, uploaded * 2^-64: plain ab out) 12 * gdn att[4096] = GDN step(mix, mix + 8192, ab) * 2^64 omega_gpu_gdn_record (ratio 2) 13 * attention l: fp8 mix mix[9216] = W_qgkv[l] . xq (q_proj 16 x [q 256 | gate 256], k_proj 512, v_proj 512) 14 * GATHER agg[9216] = permuted q | permuted k | v (5120, the attention kernel's row) | gate (4096) 15 * attn att[4096] = attention(agg, KV[l], pos) * 2^64 omega_gpu_decode_attn256_record (16 / 2) 16 * GATE att *= sigmoid(agg + 5120) 17 * every layer: fp8 o o[2048] = W_o[l] . att 18 * ADD x += o 19 * router xr = RMSNorm(x) * post_norm[l] * 2^64, ids[9], wts[9], tables omega_gpu_router_record 20 * (slot 8 = the shared expert, weight sigmoid(shared_expert_gate . xn); omega_gpu_router_alloc_shape) 21 * fp8 gu gu[9][1024] = W_gu[ids] . xr (gate 512 | up 512; tensor of experts + 1, the last the shared one) 22 * SWIGLU act[9][512] = silu(gate) * up * 2^64 23 * fp8 down dn[9][2048] = W_dn[ids] . act[g] 24 * COMBINE|NORM x += sum_g wts[g] * dn[g] (9 groups); xq = RMSNorm(x) * in_norm[l + 1] * 2^64, or (last 25 * layer) hidden = RMSNorm(x) * final_norm 26 * final: bf16 lm_head logits = W_lm . hidden; ARGMAX x2 (122 CTAs over 248320, then 1) 27 * (or the exact two-pass head, omega_gpu_q36_graph_set_exact_head: + 5 launches) 28 * 29 * Launches: GDN layer 10, attention layer 11, plus 1 + 3. 30 * Norm weights are passed already folded to (1 + w) (the model's zero-centred RMSNorm); the post-attention norm lives in the 31 * router (omega_gpu_router_set_layer), the router's directories in the caller's omega_gpu_router_set_directory_fp8. 32 * Memory, threading and kernel caches: as omega_gpu_q38_graph.h. 33 * 34 * ============ C signatures (for the Rust binding) ============ 35 * int omega_gpu_q36_graph_create(const OmegaGpuQ36GraphConfig *cfg, OmegaGpuQ36Graph **out); 36 * void omega_gpu_q36_graph_free(OmegaGpuQ36Graph *g); 37 * int omega_gpu_q36_graph_set_layer(g, layer, mix, ab, o, gate_up, down, in_norm); 38 * int omega_gpu_q36_graph_set_final_norm(g, final_norm); 39 * int omega_gpu_q36_graph_token(g, x_embed, pos, &token, &stats); 40 * int omega_gpu_q36_graph_read(g, x, hidden, logits); 41 * int omega_gpu_q36_graph_read_buf(g, which, out, n); 42 * Return codes: the decode graph's OMEGA_GPU_DGRAPH_*. 43 */ 44 #include "omega_gpu_decode_graph.h" 45 #include "omega_gpu_decode_attn256_api.h" 46 #include "omega_gpu_gdn_api.h" 47 #include "omega_gpu_router_api.h" 48 #include 49 #ifdef __cplusplus 50 extern "C" { 51 #endif 52 53 #define OMEGA_GPU_Q36_HIDDEN 2048u 54 #define OMEGA_GPU_Q36_EXPERT_INTER 512u /* routed and shared expert width */ 55 #define OMEGA_GPU_Q36_GROUPS 9u /* 8 routed + the shared expert */ 56 #define OMEGA_GPU_Q36_GDN_MIX 12288u /* in_proj_qkv 8192 | in_proj_z 4096 */ 57 #define OMEGA_GPU_Q36_GDN_QKV 8192u 58 #define OMEGA_GPU_Q36_AB 64u /* 32 heads x {a, b} */ 59 #define OMEGA_GPU_Q36_ATT_MIX 9216u /* q_proj 8192 | k_proj 512 | v_proj 512 */ 60 #define OMEGA_GPU_Q36_ATT_QKV 5120u /* the attention kernel's row: q 16 x 256 | k 2 x 256 | v 2 x 256 */ 61 #define OMEGA_GPU_Q36_MIXER_OUT 4096u /* GDN 32 x 128 = attention 16 x 256 */ 62 #define OMEGA_GPU_Q36_ROTARY 64u 63 #define OMEGA_GPU_Q36_MAX_LAYERS 64u 64 /* Heads per stroke (conversations through one command list): a 40-layer token records up to 11 * 40 + 4 = 444 launches, 65 * and the list holds OMEGA_GPU_CMDLIST_MAX_LAUNCHES (4096), so eight (3552; the submit splits a list over the ring as it 66 * needs). v1 reads the weights once per head per stroke; v2 batches the dense GEMVs at M = heads, not the list grows further. */ 67 #define OMEGA_GPU_Q36_MAX_HEADS 8u 68 69 typedef struct { 70 uint32_t layers; /* 1..64 (a model prefix in tests) */ 71 uint64_t attn_mask; /* bit l set: model layer l is full attention, else GDN */ 72 uint32_t vocab; /* lm_head rows (248320), <= 2048 * 2048 */ 73 float eps; /* rms_norm_eps (1e-6) */ 74 float eps_l2; /* GDN q/k l2norm eps (1e-6) */ 75 OmegaGpuGdn *gdn; /* omega_gpu_gdn_alloc_ratio(.., 2, ..), >= GDN layers entries, parameters set */ 76 OmegaGpuDecodeAttn256Cache *attn; /* omega_gpu_decode_attn256_cache_alloc_shape(.., 16, 2, 4, ..), q/k norms set */ 77 OmegaGpuRouter *router; /* omega_gpu_router_alloc_shape(layers, 256, 1, ..), every layer and directory set */ 78 const OmegaGpuBf16Tensor *lm_head; /* [vocab][2048] */ 79 } OmegaGpuQ36GraphConfig; 80 81 typedef struct OmegaGpuQ36Graph OmegaGpuQ36Graph; 82 83 int omega_gpu_q36_graph_create(const OmegaGpuQ36GraphConfig *cfg, OmegaGpuQ36Graph **out); 84 void omega_gpu_q36_graph_free(OmegaGpuQ36Graph *g); 85 /* Shapes are checked. GDN layer: mix [1][12288][2048], ab bf16 [64][2048] (row 2h = in_proj_a[h], 2h + 1 = in_proj_b[h], 86 * each value * 2^-64), o [1][2048][4096]. Attention layer: mix [1][9216][2048], ab NULL, o [1][2048][4096]. 87 * Both: gate_up [257][1024][2048], down [257][2048][512] (expert 256 = the shared expert), in_norm f32 [2048] as (1 + w). 88 * The tensors stay owned by the caller. */ 89 int omega_gpu_q36_graph_set_layer(OmegaGpuQ36Graph *g, uint32_t layer, const OmegaGpuFp8Tensor *mix, const OmegaGpuBf16Tensor *ab, 90 const OmegaGpuFp8Tensor *o, const OmegaGpuFp8Tensor *gate_up, const OmegaGpuFp8Tensor *down, 91 const float *in_norm); 92 int omega_gpu_q36_graph_set_final_norm(OmegaGpuQ36Graph *g, const float *final_norm); /* f32 [2048] as (1 + w) */ 93 /* Optional FP8 lm_head: as omega_gpu_q38_graph_set_lm_head_fp8 ([vocab][2048]). */ 94 int omega_gpu_q36_graph_set_lm_head_fp8(OmegaGpuQ36Graph *g, const OmegaGpuFp8Tensor *head); 95 /* Strokes: N heads (conversations) through ONE command list, one submit. create_heads lays out N slots of x, hidden, logits 96 * and token; head 0 uses cfg.gdn / cfg.attn, every head (0 included) can be pointed at other state objects by set_head_state (same shapes; 97 * head swap = pointing a slot at another conversation's objects). The router, the weights and the per-layer scratch are 98 * shared: launches run in order (barriers by default) and every head's layer launches stay contiguous. The one value that 99 * crosses layers, the fused COMBINE|NORM's x' for the next layer (xq), is per head (2026-10-21 02:50Z: shared, every head 100 * read the last head's x'; the stroke gate caught it). create() == create_heads(cfg, 1). */ 101 int omega_gpu_q36_graph_create_heads(const OmegaGpuQ36GraphConfig *cfg, uint32_t heads, OmegaGpuQ36Graph **out); 102 int omega_gpu_q36_graph_set_head_state(OmegaGpuQ36Graph *g, uint32_t head, OmegaGpuGdn *gdn, OmegaGpuDecodeAttn256Cache *attn); 103 /* x_embed f32 [heads][2048], pos [heads] -> tokens [heads]; layer-major (every head's layer l before layer l + 1). */ 104 int omega_gpu_q36_graph_stroke(OmegaGpuQ36Graph *g, const float *x_embed, const uint32_t *pos, uint32_t heads, uint32_t *tokens, 105 OmegaGpuDecodeGraphStats *st); 106 int omega_gpu_q36_graph_begin_stroke(OmegaGpuQ36Graph *g, const float *x_embed, uint32_t heads); 107 int omega_gpu_q36_graph_record_head_layer(OmegaGpuQ36Graph *g, uint32_t head, uint32_t layer, uint32_t pos); 108 int omega_gpu_q36_graph_record_head_final(OmegaGpuQ36Graph *g, uint32_t head); 109 int omega_gpu_q36_graph_end_stroke(OmegaGpuQ36Graph *g, uint32_t *tokens, OmegaGpuDecodeGraphStats *st); 110 /* As read(), for head h of the last stroke. */ 111 int omega_gpu_q36_graph_read_head(OmegaGpuQ36Graph *g, uint32_t head, float *x, float *hidden, float *logits); 112 /* HUM L314 exact two-pass head (bf16 lm_head only; ignored while an FP8 lm_head is set). screen: the fp8b copy of lm_head 113 * ([vocab][2048], one bf16 scale per 128 x 128 block); coef: f32 [vocab] Row bound coefficients, c[r] >= |w_r - w~_r| + 114 * gamma(2100)(|w_r| + |w~_r|) (sc exact_head.rs). The final step then records 115 * XBOUND SCALE h8 = hidden * 2^64; fp8 screen s = W~ . h8; XBOUND lo / hi = s -/+ c|hidden|; ARGMAX x2 tau = max lo; 116 * bf16 MASK logits = W_lm . hidden where hi >= tau (bit-identical to the plain head), -inf elsewhere; ARGMAX x2 117 * so the greedy token is the plain head's by construction, but the logits read back are -inf off the candidates: use it 118 * only for greedy tokens (set_exact_head_on(g, 0) for sampled ones). Copies coef; screen must outlive g. NULL screen 119 * removes it. On by default once set. */ 120 int omega_gpu_q36_graph_set_exact_head(OmegaGpuQ36Graph *g, const OmegaGpuFp8Tensor *screen, const float *coef); 121 void omega_gpu_q36_graph_set_exact_head_on(OmegaGpuQ36Graph *g, int on); /* between tokens */ 122 int omega_gpu_q36_graph_exact_head_active(const OmegaGpuQ36Graph *g); /* head 0: set, on, and no FP8 lm_head */ 123 /* Strokes: the switch is per head (a stroke may mix greedy and sampled rows); set_exact_head_on sets every head. The 124 * heads share the exact head's scratch (h8, s, lo, hi, tau): their final launches run in list order, each head's whole 125 * two-pass sequence before the next head's. */ 126 void omega_gpu_q36_graph_set_head_exact_head_on(OmegaGpuQ36Graph *g, uint32_t head, int on); 127 int omega_gpu_q36_graph_exact_head_active_head(const OmegaGpuQ36Graph *g, uint32_t head); 128 /* One whole token: x_embed (f32 [2048]) -> greedy token at position pos. */ 129 int omega_gpu_q36_graph_token(OmegaGpuQ36Graph *g, const float *x_embed, uint32_t pos, uint32_t *token, OmegaGpuDecodeGraphStats *st); 130 int omega_gpu_q36_graph_begin_token(OmegaGpuQ36Graph *g, const float *x_embed); 131 int omega_gpu_q36_graph_record_layer(OmegaGpuQ36Graph *g, uint32_t layer, uint32_t pos); 132 int omega_gpu_q36_graph_record_final(OmegaGpuQ36Graph *g); 133 int omega_gpu_q36_graph_end_token(OmegaGpuQ36Graph *g, uint32_t *token, OmegaGpuDecodeGraphStats *st); 134 /* Takes the lock; copies the device x, hidden and logits (vocab values) of the last token; any pointer may be NULL. */ 135 int omega_gpu_q36_graph_read(OmegaGpuQ36Graph *g, float *x, float *hidden, float *logits); 136 enum { OMEGA_GPU_Q36_BUF_X, OMEGA_GPU_Q36_BUF_XQ, OMEGA_GPU_Q36_BUF_MIX, OMEGA_GPU_Q36_BUF_AB, OMEGA_GPU_Q36_BUF_AGG, 137 OMEGA_GPU_Q36_BUF_ATT, OMEGA_GPU_Q36_BUF_O, OMEGA_GPU_Q36_BUF_GU, OMEGA_GPU_Q36_BUF_ACT, OMEGA_GPU_Q36_BUF_HIDDEN, 138 OMEGA_GPU_Q36_BUF_DN, OMEGA_GPU_Q36_BUF_COUNT }; 139 int omega_gpu_q36_graph_read_buf(OmegaGpuQ36Graph *g, int which, float *out, uint32_t n); 140 /* Prefill (L369): a prompt chunk of n tokens (embeddings x_embed f32 [n][2048]) of the conversation in head slot `head` at 141 * positions pos0 .. pos0 + n - 1, as if each token went through token() in turn: every step uses the decode step's kernels 142 * (the dense trunk and the experts through the fp8 gemm record path, bit-identical to the GEMV per row; a|b and the router 143 * in their ROWS forms; the GDN step and the attention launched per token in order on the head's own state). Per layer two 144 * lists: the mixers and the router, then (after a host sort of the expert picks) the experts and COMBINE|NORM. want_token: 145 * the last token's head and argmax as record_head_final (token, read_head(head) as after a token). prefill_alloc(cap) 146 * sizes the buffers for chunks of up to cap tokens (1 .. OMEGA_GPU_Q36_PREFILL_MAX_ROWS; nothing shrinks). 147 * TEST ONLY: AIEN_OMEGA_Q36_PF_MUTANT=1 swaps the last two tokens' GDN steps, 2 drops one row of every expert segment, 148 * 3 defers each GDN output copy past the next token's step, 5 gives the multi-token GDN a|b row stride 0 (every token 149 * reads token 0's gates), 6 drops the attention prep launch (the chunk's own K/V rows never reach the cache). The GDN runs 150 * as ONE multi-token launch per layer (the decode step's arithmetic in a loop, bit-identical to the per-token launches; 151 * mutants 1 and 3 use the per-token form); AIEN_OMEGA_Q36_PF_GDN=0 forces the per-token launches. Each attention layer is 152 * one prep launch (every token's K/V rows) and one attend launch (grid (splits, tokens x rows), the decode kernel per 153 * token, bit-identical; omega_gpu_decode_attn256_multi_record); AIEN_OMEGA_Q36_PF_ATTN=0 forces the per-token launches. */ 154 #define OMEGA_GPU_Q36_PREFILL_MAX_ROWS 128u /* x 8 routed expert rows <= the fp8 gemm's 1024 (the shared expert is its own GEMM) */ 155 int omega_gpu_q36_graph_prefill_alloc(OmegaGpuQ36Graph *g, uint32_t cap); 156 uint32_t omega_gpu_q36_graph_prefill_cap(const OmegaGpuQ36Graph *g); 157 /* fp8 GEMM weight bytes every prefill call on g so far streamed (a running count: take the difference around a call): every expert once per 4 of its rows, the dense mix/o once per 8 158 * rows (the ceiling receipt: these bytes over the call time against the memory bandwidth). The bf16 a|b, router and head 159 * reads are not counted. */ 160 uint64_t omega_gpu_q36_graph_prefill_streamed(const OmegaGpuQ36Graph *g); 161 int omega_gpu_q36_graph_prefill(OmegaGpuQ36Graph *g, uint32_t head, const float *x_embed, uint32_t n, uint32_t pos0, int want_token, 162 uint32_t *token, OmegaGpuDecodeGraphStats *st); 163 /* The gather table (u32 [9216]): agg[n] = mix[tab[n]]. */ 164 void omega_q36_gather_table(uint32_t *tab); 165 /* the FP8 GEMM's x' lane permutation as a GATHER table (k a multiple of 512) */ 166 void omega_q36_perm_table(uint32_t *tab, uint32_t k); 167 168 /* The batch (v2, L312 "the ignition"): with set_batch(g, 1), stroke() records each layer for every conversation at once: 169 * the dense GEMVs become one grouped GEMM over the N rows (mix, o: one weight read per layer for all N), the expert blocks 170 * one grouped GEMM over the N x 9 rows sorted by expert (GROUP kernel: every expert any conversation picked is read once), 171 * the per-conversation state kernels (GDN, attention, router, COMBINE) stay per head. Values are those of the per-head 172 * stroke (the gate: every head word-identical to its run alone). The final head (lever 1): the bf16 lm_head read once per 173 * group of up to OMEGA_BF16_MAX_XBATCH heads (the XBATCH GEMV form), then the argmax per head; the FP8 head stays per head. 174 * set_batch pins the GEMM and XBATCH kernels (session, once); AIEN_OMEGA_Q36_BATCH=1 at create does the same. 175 * record_layer_heads is the per-layer step stroke() uses. */ 176 int omega_gpu_q36_graph_set_batch(OmegaGpuQ36Graph *g, int on); 177 int omega_gpu_q36_graph_batch(const OmegaGpuQ36Graph *g); 178 int omega_gpu_q36_graph_record_layer_heads(OmegaGpuQ36Graph *g, uint32_t layer, const uint32_t *pos); 179 180 enum { 181 OMEGA_GPU_Q36_K_NORM = 0, OMEGA_GPU_Q36_K_MIX, OMEGA_GPU_Q36_K_AB, OMEGA_GPU_Q36_K_GDN, OMEGA_GPU_Q36_K_GATHER, OMEGA_GPU_Q36_K_ATTN, 182 OMEGA_GPU_Q36_K_GATE, OMEGA_GPU_Q36_K_O, OMEGA_GPU_Q36_K_RES, OMEGA_GPU_Q36_K_ROUTER, OMEGA_GPU_Q36_K_GATE_UP, OMEGA_GPU_Q36_K_SWIGLU, 183 OMEGA_GPU_Q36_K_DOWN, OMEGA_GPU_Q36_K_LM_HEAD, OMEGA_GPU_Q36_K_ARGMAX, OMEGA_GPU_Q36_K_PERM, OMEGA_GPU_Q36_K_GROUP, OMEGA_GPU_Q36_K_COUNT 184 }; 185 void omega_gpu_q36_graph_set_stamps(OmegaGpuQ36Graph *g, int on); 186 int omega_gpu_q36_graph_stamps(const OmegaGpuQ36Graph *g, uint64_t *stamps, uint8_t *kinds, uint32_t cap); 187 const char *omega_gpu_q36_graph_kind_name(uint8_t kind); 188 void omega_gpu_q36_graph_set_flush_each(OmegaGpuQ36Graph *g, int on); 189 /* TEST ONLY: 1 SWIGLU up half off by one, 2 GATHER index + 1, 3 GATE dropped, 4 COMBINE skips the shared expert, 190 * 5 the exact head's MASK keeps the rows it should drop and drops the candidates (the greedy gate must catch it), 191 * 6 the exact head's XBOUND drops the c|h| bound (too weak to change most tokens: the host gate saw one golden of five 192 * diverge at its full scale x0). 0 none. */ 193 void omega_gpu_q36_graph_test_set_mutant(OmegaGpuQ36Graph *g, int mutant); 194 195 #ifdef __cplusplus 196 } 197 #endif 198 #endif