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
CUDApp / src / omega_gpu_q38_graph.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #ifndef OMEGA_GPU_Q38_GRAPH_H |
| 3 | #define OMEGA_GPU_Q38_GRAPH_H |
| 4 | /* |
| 5 | * omega_gpu_q38_graph: one Qwen3.8-27B decode token as ONE command list (sc #642, 2026-10-18). The 30B's |
| 6 | * omega_gpu_decode_graph (MoE, hidden 2048) is the template; this one is dense, hidden 5120, and mixes two layer kinds: |
| 7 | * Gated DeltaNet (48 layers) and full attention with head_dim 256 (16 layers, every 4th). |
| 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[16384] = W_qkvz[l] . xq (in_proj_qkv 10240 | in_proj_z 6144) |
| 11 | * bf16 ab ab[96] = W_ab[l] . xq (a/b rows interleaved, uploaded * 2^-64: plain ab out) |
| 12 | * gdn att[6144] = GDN step(mix, mix + 10240, ab) * 2^64 omega_gpu_gdn_record |
| 13 | * attention l: fp8 mix mix[14336] = W_qgkv[l] . xq (q_proj 24 x [q 256 | gate 256], k_proj 1024, v_proj 1024) |
| 14 | * GATHER agg[14336] = permuted q | permuted k | v (8192, the attention kernel's row) | gate (6144) |
| 15 | * attn att[6144] = attention(agg, KV[l], pos) * 2^64 omega_gpu_decode_attn256_record |
| 16 | * GATE att *= sigmoid(agg + 8192) |
| 17 | * every layer: fp8 o o[5120] = W_o[l] . att |
| 18 | * ADD|NORM x += o; xr = RMSNorm(x) * post_norm[l] * 2^64 |
| 19 | * fp8 gu gu[34816] = W_gu[l] . xr (gate 17408 | up 17408) |
| 20 | * SWIGLU act[17408] = silu(gate) * up * 2^64 |
| 21 | * fp8 down o[5120] = W_down[l] . act |
| 22 | * ADD|NORM x += o; xq = RMSNorm(x) * in_norm[l + 1] * 2^64, or (last layer) hidden = RMSNorm(x) * final_norm |
| 23 | * final: bf16 lm_head logits = W_lm . hidden; ARGMAX x2 (122 CTAs over 248320, then 1) |
| 24 | * |
| 25 | * Launches: GDN layer 9, attention layer 10, plus 1 + 3: 48 * 9 + 16 * 10 + 4 = 596 for the whole model. |
| 26 | * Norm weights are passed already folded to (1 + w) (the model's zero-centred RMSNorm). The GDN and attention state live |
| 27 | * in the caller's OmegaGpuGdn (one entry per GDN layer, in model order) and OmegaGpuDecodeAttn256Cache (one per attention |
| 28 | * layer, in model order); the graph maps model layer l to its index of that kind. |
| 29 | * |
| 30 | * Memory: one omega_gpu_session_alloc buffer (GPU-uncached; nvos.h:1117 "For system memory this will not be coherent with |
| 31 | * direct CPU mappings"): the host writes x before the submit and reads the token / logits after. |
| 32 | * Threading and kernel caches: as omega_gpu_decode_graph.h (begin_token takes omega_gpu_session_lock(), end_token |
| 33 | * releases it; the record APIs refuse a cache eviction, so warm each shape up with one non-record call first). |
| 34 | * |
| 35 | * ============ C signatures (for the Rust binding) ============ |
| 36 | * int omega_gpu_q38_graph_create(const OmegaGpuQ38GraphConfig *cfg, OmegaGpuQ38Graph **out); |
| 37 | * void omega_gpu_q38_graph_free(OmegaGpuQ38Graph *g); |
| 38 | * int omega_gpu_q38_graph_set_layer(g, layer, mix, ab, o, gate_up, down, in_norm, post_norm); |
| 39 | * int omega_gpu_q38_graph_set_layer_bf16(g, layer, mix, ab, o, gate_up, down, down2, in_norm, post_norm); |
| 40 | * int omega_gpu_q38_graph_set_final_norm(g, final_norm); |
| 41 | * int omega_gpu_q38_graph_token(g, x_embed, pos, &token, &stats); |
| 42 | * int omega_gpu_q38_graph_read(g, x, hidden, logits); |
| 43 | * int omega_gpu_q38_graph_read_buf(g, which, out, n); |
| 44 | * Return codes: the decode graph's OMEGA_GPU_DGRAPH_* (0 OK, -1 BAD_ARGS, -3 CODEGEN_FAIL, -4 CHIP_FAIL); the failing |
| 45 | * stage is in omega_gpu_session_last_error(). |
| 46 | */ |
| 47 | #include "omega_gpu_decode_graph.h" |
| 48 | #include "omega_gpu_decode_attn256_api.h" |
| 49 | #include "omega_gpu_gdn_api.h" |
| 50 | #include <stdint.h> |
| 51 | #ifdef __cplusplus |
| 52 | extern "C" { |
| 53 | #endif |
| 54 | |
| 55 | #define OMEGA_GPU_Q38_HIDDEN 5120u |
| 56 | #define OMEGA_GPU_Q38_INTER 17408u |
| 57 | #define OMEGA_GPU_Q38_GDN_MIX 16384u /* in_proj_qkv 10240 | in_proj_z 6144 */ |
| 58 | #define OMEGA_GPU_Q38_GDN_QKV 10240u |
| 59 | #define OMEGA_GPU_Q38_AB 96u /* 48 heads x {a, b} */ |
| 60 | #define OMEGA_GPU_Q38_ATT_MIX 14336u /* q_proj 12288 | k_proj 1024 | v_proj 1024 */ |
| 61 | #define OMEGA_GPU_Q38_ATT_QKV 8192u /* the attention kernel's row: q 24 x 256 | k 4 x 256 | v 4 x 256 */ |
| 62 | #define OMEGA_GPU_Q38_MIXER_OUT 6144u /* GDN 48 x 128 = attention 24 x 256 */ |
| 63 | #define OMEGA_GPU_Q38_ROTARY 64u |
| 64 | #define OMEGA_GPU_Q38_MAX_LAYERS 64u |
| 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 | /* Experimental greedy-only exact recheck. The head and finite nonnegative row coefficients |
| 119 | * must describe the same compact screen; roundings must equal the Omega plan's 10384. |
| 120 | * The graph copies coefficients and borrows head. Install after final_norm, before tokens. |
| 121 | * The original head is retained. Disable for sampled requests needing all logits. */ |
| 122 | int omega_gpu_q38_graph_set_exact_head(OmegaGpuQ38Graph *g, const OmegaGpuFp8Tensor *head, |
| 123 | const float *coef, uint32_t roundings); |
| 124 | uint64_t omega_gpu_q38_exact_extra_bytes(void); |
| 125 | void omega_gpu_q38_graph_set_exact_head_on(OmegaGpuQ38Graph *g, int on); |
| 126 | int omega_gpu_q38_graph_exact_head_active(const OmegaGpuQ38Graph *g); |
| 127 | /* 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. |
| 128 | * 1 (default) or 0 (mix then a|b, each behind a barrier); env AIEN_OMEGA_Q38_OVERLAP_AB when never set. Values are identical. */ |
| 129 | void omega_gpu_q38_graph_set_overlap_ab(int on); |
| 130 | /* One whole token: x_embed (f32 [hidden]) -> greedy token; pos: the token's position (attention layers append K/V there, |
| 131 | * GDN layers advance their state). */ |
| 132 | int omega_gpu_q38_graph_token(OmegaGpuQ38Graph *g, const float *x_embed, uint32_t pos, uint32_t *token, OmegaGpuDecodeGraphStats *st); |
| 133 | /* The same in steps (between begin and end only record calls on this thread). */ |
| 134 | int omega_gpu_q38_graph_begin_token(OmegaGpuQ38Graph *g, const float *x_embed); |
| 135 | int omega_gpu_q38_graph_record_layer(OmegaGpuQ38Graph *g, uint32_t layer, uint32_t pos); |
| 136 | int omega_gpu_q38_graph_record_final(OmegaGpuQ38Graph *g); |
| 137 | int omega_gpu_q38_graph_end_token(OmegaGpuQ38Graph *g, uint32_t *token, OmegaGpuDecodeGraphStats *st); |
| 138 | /* Takes the lock; copies the device x, hidden and logits (vocab values) of the last token; any pointer may be NULL. */ |
| 139 | int omega_gpu_q38_graph_read(OmegaGpuQ38Graph *g, float *x, float *hidden, float *logits); |
| 140 | /* Test and trace reads of one intermediate of the last token (f32, n values from the start of the buffer). */ |
| 141 | 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, |
| 142 | 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, |
| 143 | OMEGA_GPU_Q38_BUF_COUNT }; |
| 144 | int omega_gpu_q38_graph_read_buf(OmegaGpuQ38Graph *g, int which, float *out, uint32_t n); |
| 145 | /* The gather table (u32 [(q_heads + kv_heads) * 512]): agg[n] = mix[tab[n]] (host side; the graph uploads it at create). |
| 146 | * 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). */ |
| 147 | int omega_q38_gather_table(uint32_t q_heads, uint32_t kv_heads, uint32_t *tab); |
| 148 | |
| 149 | /* ---- timeline (measurement; as the decode graph) ---- */ |
| 150 | enum { |
| 151 | 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, |
| 152 | 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, |
| 153 | OMEGA_GPU_Q38_K_LM_HEAD, OMEGA_GPU_Q38_K_ARGMAX, OMEGA_GPU_Q38_K_COUNT |
| 154 | }; |
| 155 | void omega_gpu_q38_graph_set_stamps(OmegaGpuQ38Graph *g, int on); |
| 156 | int omega_gpu_q38_graph_stamps(const OmegaGpuQ38Graph *g, uint64_t *stamps, uint8_t *kinds, uint32_t cap); |
| 157 | const char *omega_gpu_q38_graph_kind_name(uint8_t kind); |
| 158 | /* 1: submit after every launch (the comparison path). */ |
| 159 | void omega_gpu_q38_graph_set_flush_each(OmegaGpuQ38Graph *g, int on); |
| 160 | /* TEST ONLY: 1 SWIGLU up half off by one, 2 GATHER index + 1, 3 GATE dropped, 4 post-norm without its weight. 0 none. */ |
| 161 | void omega_gpu_q38_graph_test_set_mutant(OmegaGpuQ38Graph *g, int mutant); |
| 162 | |
| 163 | #ifdef __cplusplus |
| 164 | } |
| 165 | #endif |
| 166 | #endif |