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