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_attention_api.h
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | #ifndef OMEGA_GPU_ATTENTION_API_H |
| 3 | #define OMEGA_GPU_ATTENTION_API_H |
| 4 | /* |
| 5 | * omega_gpu_attention_api: native (no CUDA) decode attention for the AIEN |
| 6 | * inference stack (FB-1 cut 5, 2026-10-04). |
| 7 | * |
| 8 | * Two kernel families, one query per (sequence, query head), generated by our |
| 9 | * own codegen (src/omega_blackwell_codegen.c IR) and launched through the same |
| 10 | * chip-proven sequence as cuts 1b and 4: every buffer GPU-uncached (nvos.h |
| 11 | * NVOS32_ATTR2_GPU_CACHEABLE: system memory cached by the GPU is not coherent |
| 12 | * with CPU mappings), shader caches invalidated before the launch (clcec0.h |
| 13 | * NVCEC0_INVALIDATE_SHADER_CACHES, cut 1b finding), first marker, |
| 14 | * L2_FLUSH_DIRTY, second marker (C3 tail), poison-filled output scanned for |
| 15 | * unwritten words after readback. |
| 16 | * |
| 17 | * The math follows aien-sovereign-core crates/aien-inference-abi/src/backend.rs |
| 18 | * (ReferenceCpuBackend / TensorBackend defaults), read 2026-10-04 at main 8b5caed: |
| 19 | * gqa_attention(out, q, k_cache, v_cache, seq_len, num_q_heads, num_kv_heads, head_dim) |
| 20 | * k_cache / v_cache are f32 [seq_len][num_kv_heads][head_dim]; query head h reads |
| 21 | * kv head h / (num_q_heads / num_kv_heads); score = (q . k) / sqrt(head_dim); |
| 22 | * softmax over all seq_len tokens (no mask: one query attends to the whole history); |
| 23 | * out[h] = sum_t softmax_t v_t. seq_len == 0 writes zeros. |
| 24 | * paged_attention(out, q, pool, block_ids, context_len, layer_idx, ...) |
| 25 | * the KV pool is bf16 in aien-kv-cache's canonical layout (KvLayout strides in |
| 26 | * bytes: block, layer, kv plane (0 = K, 1 = V), token, head); token t lives in |
| 27 | * block_ids[t / block_size] at slot t % block_size; tokens whose block index is |
| 28 | * past the end of block_ids are dropped; context_len == 0 or no blocks writes zeros. |
| 29 | * paged_attention_batch: per sequence s, context_lens[s] <= 0 means 0, the block |
| 30 | * table row is block_tables[s*max_blocks .. (s+1)*max_blocks] (empty if that row |
| 31 | * runs past the array), negative entries are removed (the rest close up). |
| 32 | * |
| 33 | * Chip math: scores in f32 (FFMA over the head), online softmax per chunk of |
| 34 | * head_dim tokens: running max (FMNMX), p = EX2((s - m) * log2 e) folded into the |
| 35 | * score scale, rescale factor alpha = EX2(m_old - m_new), f32 sums, final MUFU.RCP. |
| 36 | * bf16 K is read as 32-bit pairs (LDG.E) and widened by a 16-bit shift (bf16 is |
| 37 | * the top half of an f32); bf16 V by LDG.E.U16 and the same shift. f32 K/V are |
| 38 | * read directly. Tolerances are stated and justified in tests/gpu_attention_test.c. |
| 39 | * |
| 40 | * Launch envelope: at most OMEGA_GPU_ATTN_MAX_CTAS / num_q_heads sequences per launch (investigation |
| 41 | * I42, same as cuts 1b and 4): grid = (num_q_heads, sequences per launch * context splits); the paged |
| 42 | * path adds up to 2^OMEGA_GPU_ATTN_MAX_LOG2SPLIT context splits per (sequence, head) within |
| 43 | * OMEGA_GPU_ATTN_SPLIT_MAX_CTAS (cut F, the rows path proved 1024 CTAs on the chip). |
| 44 | * Shared memory: the QMD declares 16 * head_dim bytes (clcec0qmd.h SHARED_MEMORY_SIZE_SHIFTED7, |
| 45 | * src/omega_blackwell_qmd.c word 36); the kernel uses exactly 4 arrays of head_dim words: |
| 46 | * 1024 bytes for head_dim 64 (TinyLlama, Llama-3.2-1B; the QMD default) and 2048 bytes for |
| 47 | * head_dim 128 (Qwen3-4B; GB10 parity PASS 126/0 at 7dd92dc, evidence/FB1-CUT5-HD128-7dd92dc). |
| 48 | * head_dim is a kernel parameter (64 or 128): threads per CTA = head_dim = chunk size, one warp |
| 49 | * partial per 32 threads in the softmax reductions. |
| 50 | * |
| 51 | * Staging: the host copies q, the referenced KV blocks' layer slice, the (compacted, |
| 52 | * renumbered) block tables and the context lengths into GPU-uncached staging buffers |
| 53 | * for each launch and copies the result back. The kernel itself walks the block |
| 54 | * table. Resident pool (2026-10-12): a paged call whose pool pointer is the allocation |
| 55 | * omega_gpu_attention_pool_alloc returned reads the KV in place instead (no staging; see |
| 56 | * that declaration for the one rule). Since the attention hardening cut |
| 57 | * (2026-10-04) a launch stages each physical block's layer slice once however many |
| 58 | * sequences of the batch reference it (shared CoW prefixes), and OmegaGpuAttnInfo |
| 59 | * reports the bytes staged against the per-sequence (naive) figure. |
| 60 | * |
| 61 | * Cut 4b (2026-10-04): the device, channel and launch scratch are the process-wide |
| 62 | * session shared with the matmul and elementwise APIs (omega_gpu_session.h): one |
| 63 | * open per process instead of one per call (~40 ms), kernels resident after first |
| 64 | * use, staging buffers reused at their high-water mark (the KV slice is written |
| 65 | * straight into the device staging buffer). Calls serialise on one lock. |
| 66 | */ |
| 67 | #include <stdbool.h> |
| 68 | #include <stddef.h> |
| 69 | #include <stdint.h> |
| 70 | #include "omega_blackwell_matmul.h" /* OmegaBlackwellKernel */ |
| 71 | #ifdef __cplusplus |
| 72 | extern "C" { |
| 73 | #endif |
| 74 | |
| 75 | enum { |
| 76 | OMEGA_GPU_ATTN_OK = 0, |
| 77 | OMEGA_GPU_ATTN_BAD_ARGS = -1, /* NULL pointer, zero dimension, head mapping or block id out of range */ |
| 78 | OMEGA_GPU_ATTN_TOO_LARGE = -2, /* head_dim > 256, num_q_heads > 1024, KV planes past the kernel's 32-bit offsets, a serving reservation exceeded */ |
| 79 | OMEGA_GPU_ATTN_CODEGEN_FAIL = -3, /* our codegen refused or exceeded the register budget */ |
| 80 | OMEGA_GPU_ATTN_CHIP_FAIL = -4, /* device open, alloc, submit, marker or release failed */ |
| 81 | OMEGA_GPU_ATTN_UNWRITTEN = -5 /* output words still equal to their poison after the run */ |
| 82 | }; |
| 83 | |
| 84 | /* The tuned kernel (the fast path, unchanged since 0.5): head_dim 64 or 128, at most MAX_Q_HEADS query heads, a |
| 85 | * power-of-two GQA ratio, contiguous context up to MAX_GQA_CTX. Every other valid shape (0.6.0, AttnGeneral) runs on |
| 86 | * the general kernel, see omega_gpu_attention_general below; the bounds of these constants are the tuned kernel's only. */ |
| 87 | #define OMEGA_GPU_ATTN_HEAD_DIM 64u /* the original head_dim; threads per CTA = head_dim (one thread per dimension / per token of a chunk) */ |
| 88 | #define OMEGA_GPU_ATTN_HEAD_DIM_128 128u /* the second tuned head_dim (Qwen3-4B) */ |
| 89 | #define OMEGA_GPU_ATTN_MAX_CTAS 64u |
| 90 | /* Cut F (2026-10-12): a paged call splits each (sequence, head) over up to 2^MAX_LOG2SPLIT CTAs along the context |
| 91 | * (whole chunks each, at least one chunk per split since cut J, 2026-10-12, which also raised the cap from 8 to 16: at |
| 92 | * 1268 tokens the busiest CTA went from three chunks to two; the launch takes the largest count in the batch) within |
| 93 | * SPLIT_MAX_CTAS CTAs per launch (the rows path proved 1024 on the chip); the CTAs write unnormalised partials |
| 94 | * (acc[head_dim], running max, running sum) that the host combines, also when the split is 1 (the gqa f32 and rows |
| 95 | * kernels keep the direct output and are byte for byte the ones before). Each sequence picks its own split count |
| 96 | * from its own context, so a result never depends on the other sequences in a launch. */ |
| 97 | #define OMEGA_GPU_ATTN_MAX_LOG2SPLIT 4u |
| 98 | #define OMEGA_GPU_ATTN_SPLIT_MAX_CTAS 1024u |
| 99 | #define OMEGA_GPU_ATTN_MAX_Q_HEADS 64u /* tuned kernel only; the general kernel takes up to GENERAL_MAX_Q_HEADS */ |
| 100 | #define OMEGA_GPU_ATTN_MAX_GQA_CTX 4096u /* tuned contiguous kernel: one block of 2^12 slots; longer contexts run on the general kernel */ |
| 101 | /* General kernel domain (0.6.0): head_dim 1..256 (any value, odd included), num_q_heads 1..1024, any num_kv_heads |
| 102 | * dividing it (MHA, GQA of any ratio, MQA), contexts up to the 32-bit offset bound (2 * kv_len * num_kv_heads * |
| 103 | * head_dim * element bytes <= 0xffffffff, TOO_LARGE past it), causal or not, an optional sliding window. */ |
| 104 | #define OMEGA_GPU_ATTN_GENERAL_MAX_HEAD_DIM 256u |
| 105 | #define OMEGA_GPU_ATTN_GENERAL_MAX_Q_HEADS 1024u |
| 106 | #define OMEGA_GPU_ATTN_WINDOW_NONE (-1) |
| 107 | |
| 108 | /* Mirror of aien-kv-cache KvLayoutDesc (crates/aien-kv-cache/src/lib.rs, main 8b5caed), |
| 109 | * field for field, strides in bytes. */ |
| 110 | typedef struct { |
| 111 | uint64_t block_stride_bytes; |
| 112 | uint64_t layer_stride_bytes; |
| 113 | uint64_t kv_plane_stride_bytes; |
| 114 | uint64_t token_stride_bytes; |
| 115 | uint64_t head_stride_bytes; |
| 116 | uint64_t pool_bytes; |
| 117 | uint32_t num_blocks; |
| 118 | uint32_t num_layers; |
| 119 | uint32_t block_size; /* must be a power of two (the kernel shifts and masks) */ |
| 120 | } OmegaGpuKvLayout; |
| 121 | |
| 122 | typedef struct { |
| 123 | uint32_t chip_calls; /* launches made */ |
| 124 | uint64_t elapsed_ns; /* sum of submit-to-second-marker over the launches */ |
| 125 | uint64_t call_ns; /* wall time of the whole API call (staging, launches, copy-out) */ |
| 126 | uint32_t completion_marker; /* first marker word of the last launch */ |
| 127 | uint32_t unwritten_words; /* poison words left in the outputs (0 on OK) */ |
| 128 | uint32_t gpr_count, insn_count; |
| 129 | bool kernel_cache_hit; /* the kernel was already generated */ |
| 130 | uint32_t ctas_last_launch, threads_per_cta; |
| 131 | char target_chip[64]; |
| 132 | uint32_t sm_architecture; |
| 133 | /* Appended (attention hardening cut, 2026-10-04): transfer accounting for the call. |
| 134 | * Existing field order above is ABI; sovereign-core mirrors this struct field for field. */ |
| 135 | uint64_t kv_bytes_staged; /* KV layer-slice bytes actually copied into staging (sum over launches) */ |
| 136 | uint64_t kv_bytes_naive; /* what per-sequence staging would have copied (every sequence its own blocks) */ |
| 137 | uint32_t kv_blocks_logical; /* block references summed over sequences, truncated to each context */ |
| 138 | uint32_t kv_blocks_unique; /* distinct (physical block, layer) slices staged, summed over launches */ |
| 139 | uint64_t q_bytes, tab_bytes, out_bytes; /* q, block-table/context and result traffic (sum over launches) */ |
| 140 | uint32_t kv_source; /* OMEGA_GPU_ATTN_KV_STAGED (slices copied per call) or OMEGA_GPU_ATTN_KV_RESIDENT (read in place) */ |
| 141 | } OmegaGpuAttnInfo; |
| 142 | #define OMEGA_GPU_ATTN_KV_STAGED 0u |
| 143 | #define OMEGA_GPU_ATTN_KV_RESIDENT 1u |
| 144 | |
| 145 | /* Contiguous f32 KV (TensorBackend::gqa_attention). q and out: [num_q_heads][head_dim]. */ |
| 146 | int omega_gpu_gqa_attention_f32(const float *q, const float *k_cache, const float *v_cache, |
| 147 | uint32_t seq_len, uint32_t num_q_heads, uint32_t num_kv_heads, |
| 148 | uint32_t head_dim, float *out, OmegaGpuAttnInfo *info); |
| 149 | |
| 150 | /* Contiguous f32 KV, many query rows in one call (the prompt read, 2026-10-12). q and out are |
| 151 | * [num_rows][num_q_heads][head_dim]; k_cache and v_cache hold base_len + num_rows tokens as |
| 152 | * [token][num_kv_heads][head_dim]. Row i attends over the first base_len + i + 1 tokens (causal: |
| 153 | * its own token and everything before it). The KV is staged once per call; the rows go to the |
| 154 | * chip in launches of up to OMEGA_GPU_ATTN_ROWS_MAX_CTAS / num_q_heads rows (grid y = row, the |
| 155 | * same kernel as omega_gpu_gqa_attention_f32, so one row per launch is bit-identical to that |
| 156 | * call). Under a serving reservation the rows per launch are also bounded by the q buffer |
| 157 | * omega_gpu_attention_reserve_rows sized. Shapes outside the tuned kernel (see OMEGA_GPU_ATTN_HEAD_DIM) and |
| 158 | * base_len + num_rows > OMEGA_GPU_ATTN_MAX_GQA_CTX run on the general kernel (omega_gpu_attention_general, causal). */ |
| 159 | #define OMEGA_GPU_ATTN_ROWS_MAX_CTAS 1024u |
| 160 | int omega_gpu_gqa_attention_rows_f32(const float *q, const float *k_cache, const float *v_cache, |
| 161 | uint32_t base_len, uint32_t num_rows, uint32_t num_q_heads, |
| 162 | uint32_t num_kv_heads, uint32_t head_dim, float *out, |
| 163 | OmegaGpuAttnInfo *info); |
| 164 | |
| 165 | /* Paged bf16 KV (TensorBackend::paged_attention). pool points at the pool base. */ |
| 166 | int omega_gpu_paged_attention_bf16(const float *q, const void *pool, const OmegaGpuKvLayout *layout, |
| 167 | const uint32_t *block_ids, uint32_t num_block_ids, |
| 168 | uint32_t context_len, uint32_t layer_idx, |
| 169 | uint32_t num_q_heads, uint32_t num_kv_heads, uint32_t head_dim, |
| 170 | float *out, OmegaGpuAttnInfo *info); |
| 171 | |
| 172 | /* Batched (TensorBackend::paged_attention_batch). block_tables: [num_seqs][max_blocks_per_seq] i32 |
| 173 | * (negative = none), context_lens: [num_seqs] i32. q and out: [num_seqs][num_q_heads][head_dim]. */ |
| 174 | int omega_gpu_paged_attention_batch_bf16(const float *q, const void *pool, const OmegaGpuKvLayout *layout, |
| 175 | const int32_t *block_tables, const int32_t *context_lens, |
| 176 | uint32_t max_blocks_per_seq, uint32_t num_seqs, uint32_t layer_idx, |
| 177 | uint32_t num_q_heads, uint32_t num_kv_heads, uint32_t head_dim, |
| 178 | float *out, OmegaGpuAttnInfo *info); |
| 179 | |
| 180 | /* General attention (0.6.0, AttnGeneral): the FlashAttention-style domain on one kernel that takes every shape. |
| 181 | * q and out: [num_rows][num_q_heads][head_dim] f32. k and v: kv_len tokens, f32 or bf16 (kv_bf16), element |
| 182 | * (k + t * kv_token_stride + kvh * kv_head_stride)[0 .. head_dim) for token t, kv head kvh; strides in ELEMENTS, 0 = |
| 183 | * dense [token][num_kv_heads][head_dim]. Query head h reads kv head h / (num_q_heads / num_kv_heads). Row i sits at |
| 184 | * position p = q_pos0 + i and attends to keys [lo, hi): hi = p + 1 when causal, else p + window_right + 1 when |
| 185 | * window_right >= 0, else kv_len; lo = p - window_left when window_left >= 0, else 0; both clamped to [0, kv_len]. |
| 186 | * An empty range writes zeros. score = (q . k) * scale (scale 0 = 1 / sqrt(head_dim)); softmax and the accumulators in |
| 187 | * f32 (online softmax over chunks of roundup(head_dim, 32) tokens). q and k are taken AFTER rotary embedding (RoPE is |
| 188 | * the caller's, as in FlashAttention). The KV is gathered densely and staged once per call; rows go to the chip in |
| 189 | * launches of up to OMEGA_GPU_ATTN_ROWS_MAX_CTAS / num_q_heads rows, one CTA per (row, query head), roundup(head_dim, 32) |
| 190 | * threads. The tuned entries above route their out-of-domain shapes here; the paged entries do the same with one CTA |
| 191 | * per (sequence, head) and no context split. kv_len 0 writes zeros (k and v may then be NULL). */ |
| 192 | typedef struct { |
| 193 | uint32_t num_rows, num_q_heads, num_kv_heads, head_dim; |
| 194 | uint32_t kv_len, q_pos0; |
| 195 | uint32_t causal; /* nonzero: hi = p + 1 (window_right is ignored) */ |
| 196 | int32_t window_left, window_right; /* OMEGA_GPU_ATTN_WINDOW_NONE (-1) = unbounded on that side */ |
| 197 | float scale; /* 0 = 1 / sqrt(head_dim); negative or NaN is BAD_ARGS */ |
| 198 | uint32_t kv_bf16; /* nonzero: k / v are bf16, else f32 */ |
| 199 | uint64_t kv_token_stride, kv_head_stride; /* elements; 0 = dense */ |
| 200 | } OmegaGpuAttnGeneralDesc; |
| 201 | int omega_gpu_attention_general(const OmegaGpuAttnGeneralDesc *desc, const float *q, const void *k, const void *v, |
| 202 | float *out, OmegaGpuAttnInfo *info); |
| 203 | |
| 204 | /* Resident KV pool (2026-10-12). One GPU-uncached, host-visible allocation of `bytes` that the caller |
| 205 | * writes its paged bf16 KV into directly (sovereign-core's pool buffer). A paged call whose `pool` |
| 206 | * pointer is this allocation reads the KV in place: nothing is staged, the block table carries the |
| 207 | * physical block ids and the kernel's block stride is the layout's block stride. The kernel forms |
| 208 | * block_id * block_stride + the in-block offsets in 32 bits, so a call takes the resident path only |
| 209 | * when block_stride_bytes and, for every block it references, block_id * block_stride_bytes + |
| 210 | * layer_stride_bytes fit in 32 bits, and the layout's pool_bytes fit the allocation; otherwise that |
| 211 | * call stages exactly as before. OmegaGpuAttnInfo.kv_source says which path ran. One pool per |
| 212 | * process: a second alloc is refused (BAD_ARGS) until omega_gpu_attention_pool_free. The alloc opens |
| 213 | * the session (CHIP_FAIL carries the driver text in omega_gpu_attention_last_error()); under the host |
| 214 | * simulator it is plain host memory. The caller must not free the pool while a call is in flight. */ |
| 215 | int omega_gpu_attention_pool_alloc(size_t bytes, void **cpu); |
| 216 | void omega_gpu_attention_pool_free(void); |
| 217 | |
| 218 | const char *omega_gpu_attention_rc_name(int rc); |
| 219 | void omega_gpu_attention_cache_clear(void); |
| 220 | |
| 221 | /* OPT-IN serving reservation (omega_gpu_reserve_serving in omega_gpu_serving.h calls this). Allocates the KV staging |
| 222 | * pool (2 planes f32, 8 KiB per token at 8 kv heads x 128), q, output and table buffers once for contexts up to |
| 223 | * max_context and up to max_seqs sequences per call. A later call that fits never allocates; one that does not |
| 224 | * returns OMEGA_GPU_ATTN_TOO_LARGE ("serving reservation exceeded" in omega_gpu_attention_last_error()) and asks the |
| 225 | * driver for nothing. Never called = behaviour unchanged. The f32 contiguous path still refuses contexts past |
| 226 | * OMEGA_GPU_ATTN_MAX_GQA_CTX on its own. kv_block_size (power of two, 0 = no paged use) sizes the pool for the paged bf16 |
| 227 | * path too: it stages whole blocks, so under half a block of context it needs more than the f32 formula. The pool is |
| 228 | * sized in BYTES for the larger of the two paths: the paged path therefore admits up to about 2 x max_context tokens |
| 229 | * (whole blocks of the same bytes); what it refuses is a call whose staging exceeds the reserved bytes. max_seqs is |
| 230 | * checked per launch chunk (the batched call splits sequences into chunks of 64 / num_q_heads), so a call with more |
| 231 | * sequences than max_seqs succeeds when every chunk fits. A paged or batched call refused on a LATER chunk has already |
| 232 | * written the outputs of the earlier chunks: treat TOO_LARGE as "output incomplete". If the reserve fails part way it |
| 233 | * rolls back: none of this API's buffers stays flagged. 0 on success, else an OMEGA_GPU_ATTN_* code. */ |
| 234 | int omega_gpu_attention_reserve(uint32_t max_context, uint32_t max_seqs, uint32_t num_q_heads, uint32_t num_kv_heads, uint32_t head_dim, uint32_t kv_block_size); |
| 235 | /* Grows the reservation's q, output and table buffers (never shrinks them) so that |
| 236 | * omega_gpu_gqa_attention_rows_f32 can send min(max_rows, OMEGA_GPU_ATTN_ROWS_MAX_CTAS / num_q_heads) |
| 237 | * rows per launch. The KV staging pool is untouched: the rows of one call share one staged context, |
| 238 | * which omega_gpu_attention_reserve already sized for max_context tokens. Call it after |
| 239 | * omega_gpu_attention_reserve (omega_gpu_reserve_serving does, with its max_rows). Without it the |
| 240 | * rows path still works under a reservation, in launches of as many rows as the q buffer holds. */ |
| 241 | int omega_gpu_attention_reserve_rows(uint32_t max_rows, uint32_t num_q_heads, uint32_t head_dim); |
| 242 | void omega_gpu_attention_unreserve(void); |
| 243 | |
| 244 | /* Stage name of the most recent CHIP_FAIL ("" if none), shared with the other GPU APIs |
| 245 | * (omega_gpu_session_last_error); an open failure carries the driver text. */ |
| 246 | const char *omega_gpu_attention_last_error(void); |
| 247 | |
| 248 | /* Test hooks. Mutants are deliberately wrong kernels the parity test must catch; |
| 249 | * never set in production. */ |
| 250 | typedef enum { |
| 251 | OMEGA_GPU_ATTN_MUTANT_NONE = 0, |
| 252 | OMEGA_GPU_ATTN_MUTANT_NO_MAX, /* softmax without the running-max subtraction (overflows for large scores) */ |
| 253 | OMEGA_GPU_ATTN_MUTANT_KV_HEAD, /* reads kv head (h / ratio) ^ 1 */ |
| 254 | OMEGA_GPU_ATTN_MUTANT_SLOT, /* K read from slot (t + 1) % block_size while V keeps slot t (K/V mismatch) */ |
| 255 | OMEGA_GPU_ATTN_MUTANT_NO_RESCALE, /* the accumulator is not rescaled when the max moves between chunks */ |
| 256 | /* Head-wiring mutants (attention hardening cut, 2026-10-04). Both swap the rows of |
| 257 | * query heads h and h ^ 1, so they need num_q_heads even (>= 2); never run them on a |
| 258 | * one-head shape (row h ^ 1 of the last sequence would be past the buffer). */ |
| 259 | OMEGA_GPU_ATTN_MUTANT_Q_ROW, /* q loaded from query-head row h ^ 1 (kv head and output row stay h) */ |
| 260 | OMEGA_GPU_ATTN_MUTANT_OUT_ROW, /* the correct result for head h stored in flattened output row h ^ 1 */ |
| 261 | OMEGA_GPU_ATTN_MUTANT_COUNT /* one past the last mutant: every battery enumerates 1 .. COUNT-1 */ |
| 262 | } OmegaGpuAttnMutant; |
| 263 | void omega_gpu_attention_test_set_mutant(OmegaGpuAttnMutant m); |
| 264 | |
| 265 | /* TEST ONLY (omega #308 regression R2). 0 = production branch-free chunk loop; 1 = the |
| 266 | * lane-dependent j >= ctx branch restored inside a BSSY/BSYNC reconvergence region; |
| 267 | * 2 = that kernel with the BSYNC dropped (negative control: the host simulator must refuse |
| 268 | * it). Clears the kernel cache. */ |
| 269 | void omega_gpu_attention_test_set_divergent(int mode); |
| 270 | /* "none", "NO_MAX", ... ; "?" past COUNT. The table is checked against COUNT at compile time. */ |
| 271 | const char *omega_gpu_attention_mutant_name(OmegaGpuAttnMutant m); |
| 272 | |
| 273 | /* Generate a kernel without launching (host tests, nvdisasm listings). |
| 274 | * log2_block_size: shift for token -> block; log2_gqa: shift for query head -> kv head. */ |
| 275 | int omega_gpu_attention_codegen(bool kv_f32, uint32_t log2_block_size, uint32_t log2_gqa, |
| 276 | OmegaBlackwellKernel *kernel); |
| 277 | |
| 278 | /* The same with an explicit head_dim (64 or 128; anything else is TOO_LARGE). The 4-argument |
| 279 | * omega_gpu_attention_codegen above is head_dim 64 and emits the same bytes as before head_dim 128 existed. */ |
| 280 | int omega_gpu_attention_codegen_hd(bool kv_f32, uint32_t log2_block_size, uint32_t log2_gqa, uint32_t head_dim, |
| 281 | OmegaBlackwellKernel *kernel); |
| 282 | |
| 283 | /* The general kernel (head_dim 1..256; log2_block_size 31 = one contiguous block). */ |
| 284 | int omega_gpu_attention_codegen_general(bool kv_f32, uint32_t log2_block_size, uint32_t head_dim, OmegaBlackwellKernel *kernel); |
| 285 | |
| 286 | /* Test hooks of the general path (0.6.0). Mutants are planted defects the sweep must catch. */ |
| 287 | typedef enum { |
| 288 | OMEGA_GPU_ATTN_GMUT_NONE = 0, |
| 289 | OMEGA_GPU_ATTN_GMUT_GQA_MAP, /* host: query head h reads kv head h % num_kv_heads (interleaved, not grouped) */ |
| 290 | OMEGA_GPU_ATTN_GMUT_TAIL_CHUNK, /* kernel: a chunk that reaches past hi is skipped (drops the partial tail tile) */ |
| 291 | OMEGA_GPU_ATTN_GMUT_TAIL_DIMS, /* kernel: the dot product drops the head_dim % 4 tail dimensions */ |
| 292 | OMEGA_GPU_ATTN_GMUT_CAUSAL, /* kernel: the last key of every non-empty range is masked (causal off by one) */ |
| 293 | OMEGA_GPU_ATTN_GMUT_SCALE, /* host: score scale 1 / 8 for every head_dim (the pre-0.6 helper's assumption) */ |
| 294 | OMEGA_GPU_ATTN_GMUT_COUNT |
| 295 | } OmegaGpuAttnGeneralMutant; |
| 296 | void omega_gpu_attention_test_set_general_mutant(OmegaGpuAttnGeneralMutant m); /* clears the kernel cache */ |
| 297 | const char *omega_gpu_attention_general_mutant_name(OmegaGpuAttnGeneralMutant m); |
| 298 | /* Route shapes the tuned kernel takes through the general kernel as well (sweep coverage at head_dim 64 / 128). */ |
| 299 | void omega_gpu_attention_test_force_general(bool on); |
| 300 | /* Threads per CTA of the kernel the simulator is running (set before each simulated launch). */ |
| 301 | uint32_t omega_gpu_attention_test_threads(void); |
| 302 | |
| 303 | /* One launch as the launcher sees it (test hook below). Pointers are host buffers; |
| 304 | * params are the eight argument words after the four buffer addresses: |
| 305 | * max_blocks, scale bits, block stride, token stride, kv plane stride, head stride, |
| 306 | * num_q_heads, context-array byte offset. */ |
| 307 | typedef struct { |
| 308 | const void *q; size_t q_bytes; |
| 309 | const void *pool; size_t pool_bytes; |
| 310 | const void *tab; size_t tab_bytes; |
| 311 | void *out; size_t out_bytes; |
| 312 | uint32_t grid_x, grid_y; |
| 313 | uint32_t params[8]; |
| 314 | uint64_t pool_va; /* resident pool: chip address of the layer base (`pool` is its host mapping, pool_bytes 0); 0 = stage `pool` */ |
| 315 | } OmegaGpuAttnLaunch; |
| 316 | |
| 317 | /* Host simulator hook (tests): when set, every launch runs the allocated IR program |
| 318 | * through `sim` instead of the chip. Lets the whole battery run on the host first |
| 319 | * (review before chip time). The program is post register allocation. */ |
| 320 | struct BlackwellIRProgramTag; |
| 321 | typedef int (*OmegaGpuAttnSim)(const void *prog /* const BlackwellIRProgram * */, const OmegaGpuAttnLaunch *L, OmegaGpuAttnInfo *info); |
| 322 | void omega_gpu_attention_test_set_simulator(OmegaGpuAttnSim sim); |
| 323 | /* The shared memory the launcher declares for the kernel the simulator is running (set before each simulated launch). */ |
| 324 | uint32_t omega_gpu_attention_test_shared_bytes(void); |
| 325 | /* Query heads one CTA serves (cut K): hd 128 groups min(2^log2_gqa, 4) heads of one kv head; hd 64 keeps one head per CTA. */ |
| 326 | uint32_t omega_gpu_attention_heads_per_cta(uint32_t head_dim, uint32_t log2_gqa); |
| 327 | |
| 328 | /* Shared-prefix staging dedupe (attention hardening cut, 2026-10-04): within one launch |
| 329 | * every (physical block, layer) slice referenced by the batch is staged once, keyed by the |
| 330 | * physical block id, and every sequence's compact table points at that one staged slot. |
| 331 | * On by default. `false` selects the legacy per-sequence staging (every sequence copies |
| 332 | * its own blocks), kept as the reference fallback and as the negative control of the |
| 333 | * staging-byte tests. Test hook; never cleared in production. */ |
| 334 | void omega_gpu_attention_test_set_dedupe(bool on); |
| 335 | /* Test hook: force the context split (log2, 0 = one CTA per head) on every paged call; -1 = automatic. */ |
| 336 | void omega_gpu_attention_test_set_split(int log2split); |
| 337 | |
| 338 | /* The resident pool's per-block fit rule (see omega_gpu_attention_pool_alloc), exposed so the |
| 339 | * 4 GiB edge can be checked without allocating a 4 GiB pool. Test hook. */ |
| 340 | bool omega_gpu_attention_test_resident_fits(const OmegaGpuKvLayout *layout, uint32_t block_id); |
| 341 | |
| 342 | #ifdef __cplusplus |
| 343 | } |
| 344 | #endif |
| 345 | #endif /* OMEGA_GPU_ATTENTION_API_H */ |