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_decode_attn256_api.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #ifndef OMEGA_GPU_DECODE_ATTN256_API_H |
| 3 | #define OMEGA_GPU_DECODE_ATTN256_API_H |
| 4 | /* |
| 5 | * omega_gpu_decode_attn256_api: one-launch decode attention for Qwen3.8-27B (omega-mv#544, 2026-10-18): head_dim 256, |
| 6 | * 24 query heads over 4 KV heads, partial RoPE by permutation. A derivative of omega_gpu_decode_attn_api.h (the 30B's |
| 7 | * head_dim 128 kernel, untouched); the kernel is described in omega_blackwell_codegen_attn256.h. Return codes and the |
| 8 | * bf16 helpers are shared with that API (OMEGA_GPU_DATTN_*, omega_dattn_bf16_rne). |
| 9 | * |
| 10 | * int omega_gpu_decode_attn256_cache_alloc(uint32_t layers, uint32_t max_pos, double rope_theta, uint32_t rotary_dim, |
| 11 | * OmegaGpuDecodeAttn256Cache **out); |
| 12 | * GPU-resident bf16 KV cache for `layers` layers of 4 KV heads x `max_pos` positions x 256 (one allocation per |
| 13 | * layer, 2 * 4 * max_pos * 512 bytes + slack), the RoPE table (f32 [max_pos][cos 128 | sin 128], built in double: |
| 14 | * pair j < rotary_dim / 2 rotates with inv_freq theta^(-2j / rotary_dim), the rest is identity), the per-layer |
| 15 | * q_norm / k_norm weights, the cross-split partials and counters. rotary_dim: even, 2..256 (64 for Qwen3.8-27B, |
| 16 | * 256 = full RoPE). Opens the session. The Qwen3.8-27B cache at 131072 positions is 512 MiB per layer. |
| 17 | * void omega_gpu_decode_attn256_cache_free(OmegaGpuDecodeAttn256Cache *c); |
| 18 | * int omega_gpu_decode_attn256_set_norms(c, layer, q_norm, k_norm): 256 f32 each, ALREADY folded to 1 + w (the model's |
| 19 | * zero-centred RMSNorm) and permuted with omega_dattn256_perm_vec. |
| 20 | * int omega_gpu_decode_attn256(c, layer, pos, qkv, eps, out_scale, out, info); |
| 21 | * qkv: f32 [8192] = q 24 heads x 256 (permuted dims, gate NOT included), then k 4 x 256 (permuted), then v 4 x 256 |
| 22 | * (NOT permuted); pos: the new token's position (< max_pos; positions 0..pos-1 already in the cache for that |
| 23 | * layer). Appends K/V at pos and writes out[24][256] * out_scale in the model's dim order (V and the output are |
| 24 | * never permuted). The gate (sigmoid of the second half of each head's q_proj rows) is applied by the caller. |
| 25 | * Returns OMEGA_GPU_DATTN_OK or an OMEGA_GPU_DATTN_* code; the chip failure text is in |
| 26 | * omega_gpu_decode_attn_last_error(). |
| 27 | * Test helpers: cache_write moves `count` consecutive bf16 K and V rows (256 each; K rows in permuted order) of a layer |
| 28 | * and KV head into the cache in one sync; cache_read returns one row of each. |
| 29 | * |
| 30 | * The permutation (design decision 1 of #544; rotary_dim R = 2P, P pairs; slot i of the kernel's head holds the model's |
| 31 | * dim omega_dattn256_perm_slot(i, R)): |
| 32 | * i < P: model dim i (first half of the rotated dims) |
| 33 | * P <= i < 128: model dim R + (i - P) (unrotated dims, in order) |
| 34 | * 128 <= i < 128+P: model dim P + (i - 128) (second half of the rotated dims: the partner of slot i - 128) |
| 35 | * 128+P <= i: model dim i (the rest of the unrotated dims) |
| 36 | * so the kernel's rotate-half pairs (i, i + 128), i < P, are exactly the model's pairs (j, j + P) and the table's |
| 37 | * identity pairs i >= P leave the other dims alone. R = 256 gives the identity permutation. q, k and the two norm |
| 38 | * weights are permuted; the K cache holds permuted rows; V and the output are not permuted. |
| 39 | * |
| 40 | * Launch shape: grid (splits, 4), 256 threads, splits = ceil((pos + 1) / per_split), per_split a multiple of 256. |
| 41 | * Every buffer is GPU-uncached host-visible session memory (nvos.h:1117 rule), launched through |
| 42 | * omega_gpu_session_launch. The counters must be zero before a launch; the last CTA of each KV head resets its |
| 43 | * counter, and the API verifies the four words are zero after each call. |
| 44 | */ |
| 45 | #include "omega_blackwell_codegen_attn256.h" |
| 46 | #include "omega_gpu_decode_attn_api.h" |
| 47 | #include <stdint.h> |
| 48 | #include <stddef.h> |
| 49 | |
| 50 | #define OMEGA_GPU_DATTN256_PER_SPLIT 256u |
| 51 | #define OMEGA_GPU_DATTN256_MAX_LAYERS 64u |
| 52 | #define OMEGA_GPU_DATTN256_MAX_POS 131072u |
| 53 | #define OMEGA_GPU_DATTN256_DEPTH 4u |
| 54 | |
| 55 | typedef struct OmegaGpuDecodeAttn256Cache OmegaGpuDecodeAttn256Cache; |
| 56 | |
| 57 | typedef struct { |
| 58 | uint32_t layer, pos, splits, per_split, depth; |
| 59 | uint32_t grid_x, grid_y, threads; |
| 60 | uint64_t kv_bytes; /* K + V bytes the launch streams: 2 * 4 * (pos + 1) * 512 */ |
| 61 | uint64_t elapsed_ns; /* marker-to-marker chip time */ |
| 62 | uint64_t call_ns; /* whole call */ |
| 63 | uint32_t completion_marker; |
| 64 | int kernel_cache_hit; |
| 65 | } OmegaGpuDecodeAttn256Info; |
| 66 | |
| 67 | int omega_gpu_decode_attn256_cache_alloc(uint32_t layers, uint32_t max_pos, double rope_theta, uint32_t rotary_dim, OmegaGpuDecodeAttn256Cache **out); |
| 68 | /* Q36 (sc L306): the same for another model shape (OmegaDecodeAttn256Spec.q_heads / kv_heads / q_per_cta; 0, 0, 0 = the 27B, |
| 69 | * the plain alloc). Qwen3.6-35B-A3B: 16, 2, 4. qkv rows are (q_heads + 2 kv_heads) * 256 f32, out q_heads * 256; the cache has |
| 70 | * kv_heads planes; partials and split counters one per CTA row (q_heads / q_per_cta <= 16). */ |
| 71 | int omega_gpu_decode_attn256_cache_alloc_shape(uint32_t layers, uint32_t max_pos, double rope_theta, uint32_t rotary_dim, uint32_t q_heads, |
| 72 | uint32_t kv_heads, uint32_t q_per_cta, OmegaGpuDecodeAttn256Cache **out); |
| 73 | void omega_gpu_decode_attn256_cache_free(OmegaGpuDecodeAttn256Cache *c); |
| 74 | int omega_gpu_decode_attn256_set_norms(OmegaGpuDecodeAttn256Cache *c, uint32_t layer, const float *q_norm, const float *k_norm); |
| 75 | int omega_gpu_decode_attn256(OmegaGpuDecodeAttn256Cache *c, uint32_t layer, uint32_t pos, const float *qkv, float eps, |
| 76 | float out_scale, float *out, OmegaGpuDecodeAttn256Info *info); |
| 77 | /* Record path (decode graph; omega_gpu_cmdlist.h): the launch appended to cl instead of submitted. qkv_va: f32 [8192] on |
| 78 | * the device; out_va: f32 [24 * 256]. The caller holds omega_gpu_session_lock(). A kernel cache miss that would evict |
| 79 | * the resident kernel is refused (warm up with one non-record call first). */ |
| 80 | struct OmegaGpuCmdList; |
| 81 | int omega_gpu_decode_attn256_record(struct OmegaGpuCmdList *cl, OmegaGpuDecodeAttn256Cache *c, uint32_t layer, uint32_t pos, uint64_t qkv_va, |
| 82 | float eps, float out_scale, uint64_t out_va); |
| 83 | /* Caller holds the lock, after the list ran: OK when the four split counters are zero, else CHIP_FAIL. */ |
| 84 | int omega_gpu_decode_attn256_counters_check(OmegaGpuDecodeAttn256Cache *c); |
| 85 | /* L369 multi-token prompt attention: tokens prompt tokens of one conversation at positions pos0 .. pos0 + tokens - 1 (qkv rows |
| 86 | * xstride bytes apart, outputs ostride bytes apart), every output bit-identical to `tokens` decode records in order. Records |
| 87 | * one prep launch (every token's K/V rows, the decode step's own instructions) and the attend launches (grid (splits, n * vy), |
| 88 | * groups of n tokens whose partials fit a shared 64 MiB buffer). per_split must be the default 256. multi_prepare builds the |
| 89 | * two kernels and the buffer (call before recording, outside the record lock). TEST: set_multi_mutant(1) drops the prep launch. */ |
| 90 | int omega_gpu_decode_attn256_multi_prepare(OmegaGpuDecodeAttn256Cache *c); |
| 91 | int omega_gpu_decode_attn256_multi_record(struct OmegaGpuCmdList *cl, OmegaGpuDecodeAttn256Cache *c, uint32_t layer, uint32_t pos0, uint32_t tokens, |
| 92 | uint64_t qkv_va, uint32_t xstride, float eps, float out_scale, uint64_t out_va, uint32_t ostride); |
| 93 | void omega_gpu_decode_attn256_test_set_multi_mutant(int mutant); |
| 94 | int omega_gpu_decode_attn256_cache_write(OmegaGpuDecodeAttn256Cache *c, uint32_t layer, uint32_t kv_head, uint32_t pos, uint32_t count, |
| 95 | const uint16_t *k_bf16, const uint16_t *v_bf16); /* count rows from pos, one sync */ |
| 96 | int omega_gpu_decode_attn256_cache_read(OmegaGpuDecodeAttn256Cache *c, uint32_t layer, uint32_t kv_head, uint32_t pos, |
| 97 | uint16_t *k_bf16, uint16_t *v_bf16); |
| 98 | /* Read-only view of a layer of the cache for the prefill attention: device addresses only. */ |
| 99 | typedef struct { |
| 100 | uint32_t layers, max_pos, rotary_dim; |
| 101 | double theta; |
| 102 | uint64_t k_va, v_va; /* K and V plane bases of `layer` (row (kv_head, pos) at + (kv_head * max_pos + pos) * 512) */ |
| 103 | uint64_t rope_va; /* f32 [max_pos][cos 128 | sin 128] */ |
| 104 | uint64_t q_norm_va, k_norm_va; /* f32 [256] each for `layer` */ |
| 105 | uint64_t part_va, cnt_va; /* the cross-split partials and counters (prefill may reuse them) */ |
| 106 | uint32_t q_heads, kv_heads, q_per_cta; /* the cache's head shape, resolved (24 / 4 / 6 for the 27B) */ |
| 107 | uint32_t qkv_words, out_words; /* f32 per token row of the q|k|v input and of the output */ |
| 108 | } OmegaGpuDecodeAttn256CacheView; |
| 109 | int omega_gpu_decode_attn256_cache_view(const OmegaGpuDecodeAttn256Cache *c, uint32_t layer, OmegaGpuDecodeAttn256CacheView *out); |
| 110 | |
| 111 | /* ---- measurement knobs (tests; each clears the kernel cache) ---- */ |
| 112 | void omega_gpu_decode_attn256_set_depth(uint32_t depth); /* 0 = default (OMEGA_GPU_DATTN256_DEPTH) */ |
| 113 | void omega_gpu_decode_attn256_set_per_split(uint32_t per_split); /* 0 = default; multiple of 256 */ |
| 114 | void omega_gpu_decode_attn256_test_set_mutant(int mutant); /* TEST ONLY: OmegaDecodeAttn256Spec.mutant */ |
| 115 | |
| 116 | /* ---- host side ---- */ |
| 117 | /* RoPE table row for `pos`: cos[128] then sin[128], f32 from double; pairs j < rotary_dim/2 rotate with |
| 118 | * inv_freq[j] = theta^(-2j/rotary_dim), the rest are cos 1, sin 0 */ |
| 119 | void omega_dattn256_rope_row(uint32_t pos, double theta, uint32_t rotary_dim, float *row256); |
| 120 | /* the model dim held by kernel slot i (see above); rotary_dim even, 2..256 */ |
| 121 | #include "cudapp_native.h" |
| 122 | static inline uint32_t omega_dattn256_perm_slot(uint32_t i, uint32_t rotary_dim) { |
| 123 | uint64_t r = 0; |
| 124 | if (omega_ds_slot(&r, i, rotary_dim) != OMEGA_OK) __builtin_trap(); |
| 125 | return (uint32_t)r; |
| 126 | } |
| 127 | /* dst[i] = src[perm_slot(i)] for 256 f32 (q, k, norm weights); src and dst must not overlap */ |
| 128 | static inline void omega_dattn256_perm_vec(const float *src, float *dst, uint32_t rotary_dim) { |
| 129 | uint64_t r = 0; |
| 130 | if (omega_ds_vecf(&r, src, 256, dst, 256, rotary_dim) != OMEGA_OK || r) __builtin_trap(); |
| 131 | } |
| 132 | static inline void omega_dattn256_perm_vec_bf16(const uint16_t *src, uint16_t *dst, uint32_t rotary_dim) { |
| 133 | uint64_t r = 0; |
| 134 | if (omega_ds_vecb(&r, src, 256, dst, 256, rotary_dim) != OMEGA_OK || r) __builtin_trap(); |
| 135 | } |
| 136 | /* The double-precision reference of the whole layer step lives in tests/gpu_decode_attn256_test.c (libm). */ |
| 137 | |
| 138 | #endif |