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_q38_graph.h
| 1 | #ifndef OMEGA_GPU_Q38_GRAPH_H |
| 2 | #define OMEGA_GPU_Q38_GRAPH_H |
| 3 | /* |
| 4 | * omega_gpu_q38_graph: one Qwen3.8-27B decode token as ONE command list (sc #642, 2026-10-18). The 30B's |
| 5 | * omega_gpu_decode_graph (MoE, hidden 2048) is the template; this one is dense, hidden 5120, and mixes two layer kinds: |
| 6 | * Gated DeltaNet (48 layers) and full attention with head_dim 256 (16 layers, every 4th). |
| 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[16384] = W_qkvz[l] . xq (in_proj_qkv 10240 | in_proj_z 6144) |
| 10 | * bf16 ab ab[96] = W_ab[l] . xq (a/b rows interleaved, uploaded * 2^-64: plain ab out) |
| 11 | * gdn att[6144] = GDN step(mix, mix + 10240, ab) * 2^64 omega_gpu_gdn_record |
| 12 | * attention l: fp8 mix mix[14336] = W_qgkv[l] . xq (q_proj 24 x [q 256 | gate 256], k_proj 1024, v_proj 1024) |
| 13 | * GATHER agg[14336] = permuted q | permuted k | v (8192, the attention kernel's row) | gate (6144) |
| 14 | * attn att[6144] = attention(agg, KV[l], pos) * 2^64 omega_gpu_decode_attn256_record |
| 15 | * GATE att *= sigmoid(agg + 8192) |
| 16 | * every layer: fp8 o o[5120] = W_o[l] . att |
| 17 | * ADD|NORM x += o; xr = RMSNorm(x) * post_norm[l] * 2^64 |
| 18 | * fp8 gu gu[34816] = W_gu[l] . xr (gate 17408 | up 17408) |
| 19 | * SWIGLU act[17408] = silu(gate) * up * 2^64 |
| 20 | * fp8 down o[5120] = W_down[l] . act |
| 21 | * ADD|NORM x += o; xq = RMSNorm(x) * in_norm[l + 1] * 2^64, or (last layer) hidden = RMSNorm(x) * final_norm |
| 22 | * final: bf16 lm_head logits = W_lm . hidden; ARGMAX x2 (122 CTAs over 248320, then 1) |
| 23 | * |
| 24 | * Launches: GDN layer 9, attention layer 10, plus 1 + 3: 48 * 9 + 16 * 10 + 4 = 596 for the whole model. |
| 25 | * Norm weights are passed already folded to (1 + w) (the model's zero-centred RMSNorm). The GDN and attention state live |
| 26 | * in the caller's OmegaGpuGdn (one entry per GDN layer, in model order) and OmegaGpuDecodeAttn256Cache (one per attention |
| 27 | * layer, in model order); the graph maps model layer l to its index of that kind. |
| 28 | * |
| 29 | * Memory: one omega_gpu_session_alloc buffer (GPU-uncached; nvos.h:1117 "For system memory this will not be coherent with |
| 30 | * direct CPU mappings"): the host writes x before the submit and reads the token / logits after. |
| 31 | * Threading and kernel caches: as omega_gpu_decode_graph.h (begin_token takes omega_gpu_session_lock(), end_token |
| 32 | * releases it; the record APIs refuse a cache eviction, so warm each shape up with one non-record call first). |
| 33 | * |
| 34 | * ============ C signatures (for the Rust binding) ============ |
| 35 | * int omega_gpu_q38_graph_create(const OmegaGpuQ38GraphConfig *cfg, OmegaGpuQ38Graph **out); |
| 36 | * void omega_gpu_q38_graph_free(OmegaGpuQ38Graph *g); |
| 37 | * int omega_gpu_q38_graph_set_layer(g, layer, mix, ab, o, gate_up, down, in_norm, post_norm); |
| 38 | * int omega_gpu_q38_graph_set_layer_bf16(g, layer, mix, ab, o, gate_up, down, down2, in_norm, post_norm); |
| 39 | * int omega_gpu_q38_graph_set_final_norm(g, final_norm); |
| 40 | * int omega_gpu_q38_graph_token(g, x_embed, pos, &token, &stats); |
| 41 | * int omega_gpu_q38_graph_read(g, x, hidden, logits); |
| 42 | * int omega_gpu_q38_graph_read_buf(g, which, out, n); |
| 43 | * Return codes: the decode graph's OMEGA_GPU_DGRAPH_* (0 OK, -1 BAD_ARGS, -3 CODEGEN_FAIL, -4 CHIP_FAIL); the failing |
| 44 | * stage is in omega_gpu_session_last_error(). |
| 45 | */ |
| 46 | #include "omega_gpu_decode_graph.h" |
| 47 | #include "omega_gpu_decode_attn256_api.h" |
| 48 | #include "omega_gpu_gdn_api.h" |
| 49 | #include <stdint.h> |
| 50 | #ifdef __cplusplus |
| 51 | extern "C" { |
| 52 | #endif |
| 53 | |
| 54 | #define OMEGA_GPU_Q38_HIDDEN 5120u |
| 55 | #define OMEGA_GPU_Q38_INTER 17408u |
| 56 | #define OMEGA_GPU_Q38_GDN_MIX 16384u /* in_proj_qkv 10240 | in_proj_z 6144 */ |
| 57 | #define OMEGA_GPU_Q38_GDN_QKV 10240u |
| 58 | #define OMEGA_GPU_Q38_AB 96u /* 48 heads x {a, b} */ |
| 59 | #define OMEGA_GPU_Q38_ATT_MIX 14336u /* q_proj 12288 | k_proj 1024 | v_proj 1024 */ |
| 60 | #define OMEGA_GPU_Q38_ATT_QKV 8192u /* the attention kernel's row: q 24 x 256 | k 4 x 256 | v 4 x 256 */ |
| 61 | #define OMEGA_GPU_Q38_MIXER_OUT 6144u /* GDN 48 x 128 = attention 24 x 256 */ |
| 62 | #define OMEGA_GPU_Q38_ROTARY 64u |
| 63 | #define OMEGA_GPU_Q38_MAX_LAYERS 64u |
| 64 | #define OMEGA_GPU_Q38_MAX_HEADS 8u |
| 65 | |
| 66 | /* Model shape (2026-10-27): the widths above and in the comments are the Qwen3.8-27B shape, which is also each width's |
| 67 | * maximum. A zero shape field of the config takes its 27B value, and the 27B builds the same kernels as before the fields |
| 68 | * existed. Qwen3.5-9B: hidden 4096, inter 12288, q_heads 16, kv_heads 4, gdn_heads 32. The other widths follow (head_dim |
| 69 | * 256, 16 DeltaNet key heads of 128): |
| 70 | * GDN qkv (32 + gdn_heads) * 128 GDN mix qkv + gdn_heads * 128 a|b 2 * gdn_heads |
| 71 | * ATT mix (q_heads + kv_heads) * 512 ATT qkv q_heads * 256 + kv_heads * 512 |
| 72 | * mixer out q_heads * 256, which must equal gdn_heads * 128 */ |
| 73 | typedef struct { |
| 74 | uint32_t layers; /* 1..64 (a model prefix in tests) */ |
| 75 | uint64_t attn_mask; /* bit l set: model layer l is full attention, else GDN */ |
| 76 | uint32_t vocab; /* lm_head rows (248320), <= 2048 * 2048 */ |
| 77 | float eps; /* rms_norm_eps (1e-6) */ |
| 78 | float eps_l2; /* GDN q/k l2norm eps (1e-6) */ |
| 79 | OmegaGpuGdn *gdn; /* >= (layers - attention layers) entries, parameters set, gdn_heads value heads */ |
| 80 | OmegaGpuDecodeAttn256Cache *attn; /* >= attention layers entries, q/k norms set; rotary 64; q_heads / kv_heads heads */ |
| 81 | const OmegaGpuBf16Tensor *lm_head; /* [vocab][hidden] */ |
| 82 | uint32_t hidden; /* residual width: a multiple of 1024, <= 5120 (0: 5120) */ |
| 83 | uint32_t inter; /* MLP width: a multiple of 256, <= 17408 (0: 17408) */ |
| 84 | uint32_t q_heads; /* attention q heads, <= 24 (0: 24) */ |
| 85 | uint32_t kv_heads; /* attention kv heads, divides q_heads, q_heads / kv_heads <= 8 (0: 4) */ |
| 86 | uint32_t gdn_heads; /* DeltaNet value heads: 32 or 48 (0: 48) */ |
| 87 | } OmegaGpuQ38GraphConfig; |
| 88 | |
| 89 | typedef struct OmegaGpuQ38Graph OmegaGpuQ38Graph; |
| 90 | |
| 91 | int omega_gpu_q38_graph_create(const OmegaGpuQ38GraphConfig *cfg, OmegaGpuQ38Graph **out); |
| 92 | void omega_gpu_q38_graph_free(OmegaGpuQ38Graph *g); |
| 93 | /* Shapes are checked (27B widths shown; the config's shape in general). GDN layer: mix [1][16384][5120], ab bf16 [96][5120] |
| 94 | * (row 2h = in_proj_a[h], 2h + 1 = in_proj_b[h], each value * 2^-64), o [1][5120][6144]. Attention layer: mix [1][14336][5120], |
| 95 | * ab NULL, o [1][5120][6144]. Both: gate_up [1][34816][5120], down [1][5120][17408], in_norm and post_norm f32 [5120] as |
| 96 | * (1 + w). The tensors stay owned by the caller. */ |
| 97 | int omega_gpu_q38_graph_set_layer(OmegaGpuQ38Graph *g, uint32_t layer, const OmegaGpuFp8Tensor *mix, const OmegaGpuBf16Tensor *ab, |
| 98 | const OmegaGpuFp8Tensor *o, const OmegaGpuFp8Tensor *gate_up, const OmegaGpuFp8Tensor *down, |
| 99 | const float *in_norm, const float *post_norm); |
| 100 | /* The same layer with bf16 projections (2026-10-27, the BF16 head-to-head of the 9B): mix, o, gate_up and down are |
| 101 | * OmegaGpuBf16Tensor [n][k] with the set_layer shapes and rows (mix rows in the FP8 stacking order), every value * 2^-64 (as |
| 102 | * ab: the GEMV inputs hold x * 2^64, so a power-of-two pre-scale gives plain outputs; the caller checks the shift is exact). |
| 103 | * Recorded with omega_gpu_bf16_gemv_record, which stages the f32 x row in shared memory (32 KiB descriptor cap: k <= 8192). |
| 104 | * down2 NULL: down is [hidden][inter]. Else (inter > 8192, the 9B's 12288) down is columns 0..inter/2 and down2 columns |
| 105 | * inter/2..inter, both [hidden][inter/2]: the layer records down -> x += (K_ADD) -> down2 -> the usual ADDNORM, two |
| 106 | * launches more. The plain kernels for widths hidden, mixer out and the down K are prepared and pinned here (CODEGEN_FAIL |
| 107 | * if they cannot be). Layers may mix: each layer is FP8 or bf16 by its last set call. */ |
| 108 | int omega_gpu_q38_graph_set_layer_bf16(OmegaGpuQ38Graph *g, uint32_t layer, const OmegaGpuBf16Tensor *mix, const OmegaGpuBf16Tensor *ab, |
| 109 | const OmegaGpuBf16Tensor *o, const OmegaGpuBf16Tensor *gate_up, const OmegaGpuBf16Tensor *down, |
| 110 | const OmegaGpuBf16Tensor *down2, const float *in_norm, const float *post_norm); |
| 111 | int omega_gpu_q38_graph_set_final_norm(OmegaGpuQ38Graph *g, const float *final_norm); /* f32 [hidden] as (1 + w) */ |
| 112 | /* Optional FP8 lm_head (qwen38/decode-speed): an FP8 tensor [vocab][hidden] (experts 1, e.g. from omega_gpu_fp8_quantize_bf16) |
| 113 | * replaces the bf16 lm_head in every later token (half the bytes); NULL goes back to bf16. The tensor must outlive its use. |
| 114 | * While set, the device hidden buffer (OMEGA_GPU_Q38_BUF_HIDDEN) holds hidden x 2^64 (the FP8 GEMV input scale); |
| 115 | * omega_gpu_q38_graph_read returns the unscaled hidden (exact power-of-two rescale). Logits are those of the quantised head: |
| 116 | * callers own the drift study against the bf16 head. */ |
| 117 | int omega_gpu_q38_graph_set_lm_head_fp8(OmegaGpuQ38Graph *g, const OmegaGpuFp8Tensor *head); |
| 118 | /* Strokes: N heads (conversations) through ONE graph. create_heads lays out N slots of x, hidden, xq, logits and token; |
| 119 | * head 0 uses cfg.gdn / cfg.attn, every head (0 included) can be pointed at other state objects by set_head_state (same |
| 120 | * shapes). create() == create_heads(cfg, 1). */ |
| 121 | int omega_gpu_q38_graph_create_heads(const OmegaGpuQ38GraphConfig *cfg, uint32_t heads, OmegaGpuQ38Graph **out); |
| 122 | int omega_gpu_q38_graph_set_head_state(OmegaGpuQ38Graph *g, uint32_t head, OmegaGpuGdn *gdn, OmegaGpuDecodeAttn256Cache *attn); |
| 123 | /* x_embed f32 [heads][hidden], pos [heads] -> tokens [heads]; layer-major. */ |
| 124 | int omega_gpu_q38_graph_stroke(OmegaGpuQ38Graph *g, const float *x_embed, const uint32_t *pos, uint32_t heads, uint32_t *tokens, |
| 125 | OmegaGpuDecodeGraphStats *st); |
| 126 | int omega_gpu_q38_graph_begin_stroke(OmegaGpuQ38Graph *g, const float *x_embed, uint32_t heads); |
| 127 | int omega_gpu_q38_graph_record_head_layer(OmegaGpuQ38Graph *g, uint32_t head, uint32_t layer, uint32_t pos); |
| 128 | int omega_gpu_q38_graph_record_head_final(OmegaGpuQ38Graph *g, uint32_t head); |
| 129 | int omega_gpu_q38_graph_end_stroke(OmegaGpuQ38Graph *g, uint32_t *tokens, OmegaGpuDecodeGraphStats *st); |
| 130 | /* As read(), for head h of the last stroke. */ |
| 131 | int omega_gpu_q38_graph_read_head(OmegaGpuQ38Graph *g, uint32_t head, float *x, float *hidden, float *logits); |
| 132 | /* Batched dense decode (v2): with set_batch(g, 1), stroke() records each FP8 layer for every head at once using grouped |
| 133 | * FP8 GEMM (mix, o, gate_up, down read once per layer for all heads; GDN and attention stay per head). bf16 layers fall |
| 134 | * back to per-head launches. set_batch pins the FP8 GEMM shapes (H, mout, I); AIEN_OMEGA_Q38_BATCH=1 at create does the same. */ |
| 135 | int omega_gpu_q38_graph_set_batch(OmegaGpuQ38Graph *g, int on); |
| 136 | int omega_gpu_q38_graph_batch(const OmegaGpuQ38Graph *g); |
| 137 | int omega_gpu_q38_graph_record_layer_heads(OmegaGpuQ38Graph *g, uint32_t layer, const uint32_t *pos); |
| 138 | /* 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 |
| 139 | * ([vocab][hidden], 128 x 128 block scales); coef: f32 [vocab] row bound coefficients; roundings: rounding count used to |
| 140 | * build coef (e.g. 10384 for hidden 5120, must be > 0 when screen != NULL). Copies coef; screen must outlive g. NULL screen |
| 141 | * removes it. On by default once set. */ |
| 142 | uint64_t omega_gpu_q38_exact_extra_bytes(void); |
| 143 | int omega_gpu_q38_graph_set_exact_head(OmegaGpuQ38Graph *g, const OmegaGpuFp8Tensor *screen, const float *coef, uint32_t roundings); |
| 144 | void omega_gpu_q38_graph_set_exact_head_on(OmegaGpuQ38Graph *g, int on); |
| 145 | int omega_gpu_q38_graph_exact_head_active(const OmegaGpuQ38Graph *g); |
| 146 | void omega_gpu_q38_graph_set_head_exact_head_on(OmegaGpuQ38Graph *g, uint32_t head, int on); |
| 147 | int omega_gpu_q38_graph_exact_head_active_head(const OmegaGpuQ38Graph *g, uint32_t head); |
| 148 | /* GDN layers record the a|b GEMV first and the mix GEMV beside it (no barrier between the two; both only read xq), process-wide. |
| 149 | * 1 (default) or 0 (mix then a|b, each behind a barrier); env AIEN_OMEGA_Q38_OVERLAP_AB when never set. Values are identical. */ |
| 150 | void omega_gpu_q38_graph_set_overlap_ab(int on); |
| 151 | /* One whole token: x_embed (f32 [hidden]) -> greedy token; pos: the token's position (attention layers append K/V there, |
| 152 | * GDN layers advance their state). */ |
| 153 | int omega_gpu_q38_graph_token(OmegaGpuQ38Graph *g, const float *x_embed, uint32_t pos, uint32_t *token, OmegaGpuDecodeGraphStats *st); |
| 154 | /* The same in steps (between begin and end only record calls on this thread). */ |
| 155 | int omega_gpu_q38_graph_begin_token(OmegaGpuQ38Graph *g, const float *x_embed); |
| 156 | int omega_gpu_q38_graph_record_layer(OmegaGpuQ38Graph *g, uint32_t layer, uint32_t pos); |
| 157 | int omega_gpu_q38_graph_record_final(OmegaGpuQ38Graph *g); |
| 158 | int omega_gpu_q38_graph_end_token(OmegaGpuQ38Graph *g, uint32_t *token, OmegaGpuDecodeGraphStats *st); |
| 159 | /* Takes the lock; copies the device x, hidden and logits (vocab values) of the last token; any pointer may be NULL. */ |
| 160 | int omega_gpu_q38_graph_read(OmegaGpuQ38Graph *g, float *x, float *hidden, float *logits); |
| 161 | /* Test and trace reads of one intermediate of the last token (f32, n values from the start of the buffer). */ |
| 162 | enum { OMEGA_GPU_Q38_BUF_X, OMEGA_GPU_Q38_BUF_XQ, OMEGA_GPU_Q38_BUF_MIX, OMEGA_GPU_Q38_BUF_AB, OMEGA_GPU_Q38_BUF_AGG, |
| 163 | OMEGA_GPU_Q38_BUF_ATT, OMEGA_GPU_Q38_BUF_O, OMEGA_GPU_Q38_BUF_GU, OMEGA_GPU_Q38_BUF_ACT, OMEGA_GPU_Q38_BUF_HIDDEN, |
| 164 | OMEGA_GPU_Q38_BUF_COUNT }; |
| 165 | int omega_gpu_q38_graph_read_buf(OmegaGpuQ38Graph *g, int which, float *out, uint32_t n); |
| 166 | /* The gather table (u32 [(q_heads + kv_heads) * 512]): agg[n] = mix[tab[n]] (host side; the graph uploads it at create). |
| 167 | * 24 / 4 is the 27B table. Returns -1 for a shape the graph refuses (q_heads 0 or above 24, kv_heads 0 or not dividing). */ |
| 168 | int omega_q38_gather_table(uint32_t q_heads, uint32_t kv_heads, uint32_t *tab); |
| 169 | /* the FP8 GEMM's x' lane permutation as a GATHER table (k a multiple of 512) */ |
| 170 | void omega_q38_perm_table(uint32_t *tab, uint32_t k); |
| 171 | |
| 172 | /* ---- timeline (measurement; as the decode graph) ---- */ |
| 173 | enum { |
| 174 | OMEGA_GPU_Q38_K_NORM = 0, OMEGA_GPU_Q38_K_MIX, OMEGA_GPU_Q38_K_AB, OMEGA_GPU_Q38_K_GDN, OMEGA_GPU_Q38_K_GATHER, OMEGA_GPU_Q38_K_ATTN, |
| 175 | OMEGA_GPU_Q38_K_GATE, OMEGA_GPU_Q38_K_O, OMEGA_GPU_Q38_K_RES, OMEGA_GPU_Q38_K_GATE_UP, OMEGA_GPU_Q38_K_SWIGLU, OMEGA_GPU_Q38_K_DOWN, |
| 176 | OMEGA_GPU_Q38_K_LM_HEAD, OMEGA_GPU_Q38_K_ARGMAX, OMEGA_GPU_Q38_K_PERM, OMEGA_GPU_Q38_K_COUNT |
| 177 | }; |
| 178 | void omega_gpu_q38_graph_set_stamps(OmegaGpuQ38Graph *g, int on); |
| 179 | int omega_gpu_q38_graph_stamps(const OmegaGpuQ38Graph *g, uint64_t *stamps, uint8_t *kinds, uint32_t cap); |
| 180 | const char *omega_gpu_q38_graph_kind_name(uint8_t kind); |
| 181 | /* 1: submit after every launch (the comparison path). */ |
| 182 | void omega_gpu_q38_graph_set_flush_each(OmegaGpuQ38Graph *g, int on); |
| 183 | /* TEST ONLY: 1 SWIGLU up half off by one, 2 GATHER index + 1, 3 GATE dropped, 4 post-norm without its weight, |
| 184 | * 5 exact head MASK inverted, 6 exact head XBOUND drops c|h|. 0 none. */ |
| 185 | void omega_gpu_q38_graph_test_set_mutant(OmegaGpuQ38Graph *g, int mutant); |
| 186 | |
| 187 | #ifdef __cplusplus |
| 188 | } |
| 189 | #endif |
| 190 | #endif |