Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/omega_gpu_decode_attn_api.h # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / omega_gpu_decode_attn_api.h` 124 lines ยท 8070 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef OMEGA_GPU_DECODE_ATTN_API_H 18 #define OMEGA_GPU_DECODE_ATTN_API_H 19 /* 20 * omega_gpu_decode_attn_api: one-launch decode attention for Qwen3-30B-A3B (OWN-30B cut 8, 2026-10-13). 21 * C signatures for the Rust side first; the kernel is described in omega_blackwell_codegen_attn.h. 22 * 23 * int omega_gpu_decode_attn_cache_alloc(uint32_t layers, uint32_t max_pos, double rope_theta, OmegaGpuDecodeAttnCache **out); 24 * GPU-resident bf16 KV cache for `layers` layers of 4 KV heads x `max_pos` positions x 128 (one allocation per 25 * layer, 2 * 4 * max_pos * 256 bytes + slack), the RoPE table (f32 cos/sin per position, built in double from 26 * rope_theta), the per-layer q_norm/k_norm weights, the cross-split partials and counters. Opens the session. 27 * void omega_gpu_decode_attn_cache_free(OmegaGpuDecodeAttnCache *c); 28 * int omega_gpu_decode_attn_set_norms(OmegaGpuDecodeAttnCache *c, uint32_t layer, const float *q_norm, const float *k_norm); 29 * 128 f32 each (the checkpoint's bf16 q_norm.weight / k_norm.weight widened by the caller). 30 * int omega_gpu_decode_attn(OmegaGpuDecodeAttnCache *c, uint32_t layer, uint32_t pos, const float *qkv, float eps, 31 * float out_scale, float *out, OmegaGpuDecodeAttnInfo *info); 32 * qkv: f32 [5120] as the fused q/k/v GEMV writes it (q 32 heads x 128, then k 4 x 128, then v 4 x 128), pos: the 33 * new token's position (< max_pos; the context is positions 0..pos, 0..pos-1 must already be in the cache for 34 * that layer). The kernel appends K/V at pos and writes out[32][128] * out_scale (2^64 makes it the x' row of 35 * the FP8 o_proj GEMV, omega_gpu_fp8_gemv_* with x_prescaled; 1.0 gives plain attention output). 36 * Returns OMEGA_GPU_DATTN_OK or a code below; the chip failure text is in omega_gpu_decode_attn_last_error(). 37 * Test helpers: cache_write moves `count` consecutive bf16 K and V rows (128 each) of a layer and KV head into the 38 * cache in one sync; cache_read returns one row of each. 39 * The double-precision oracle the tests compare against (omega_decode_attn_reference) is defined in the test. 40 * 41 * Launch shape: grid (splits, 4), 256 threads, splits = ceil((pos + 1) / per_split), per_split a multiple of 256 42 * (default OMEGA_GPU_DATTN_PER_SPLIT; omega_gpu_decode_attn_set_per_split overrides). Every buffer is GPU-uncached 43 * host-visible session memory (nvos.h:1117 rule), launched through omega_gpu_session_launch like the FP8 GEMV. 44 * The counters must be zero before a launch; the last CTA of each KV head resets its counter, and the API verifies 45 * the four words are zero after each call (a non-zero word means a CTA never arrived: CHIP_FAIL). 46 */ 47 #include "omega_blackwell_codegen_attn.h" 48 #include 49 #include 50 51 enum { 52 OMEGA_GPU_DATTN_OK = 0, 53 OMEGA_GPU_DATTN_BAD_ARGS = -1, 54 OMEGA_GPU_DATTN_TOO_LARGE = -2, 55 OMEGA_GPU_DATTN_CODEGEN_FAIL = -3, 56 OMEGA_GPU_DATTN_CHIP_FAIL = -4 57 }; 58 59 #define OMEGA_GPU_DATTN_KV_HEADS 4u 60 #define OMEGA_GPU_DATTN_Q_HEADS 32u 61 #define OMEGA_GPU_DATTN_QKV_WORDS (32u * 128u + 2u * 4u * 128u) 62 #define OMEGA_GPU_DATTN_PER_SPLIT 256u 63 #define OMEGA_GPU_DATTN_MAX_LAYERS 64u 64 #define OMEGA_GPU_DATTN_MAX_POS 131072u 65 66 typedef struct OmegaGpuDecodeAttnCache OmegaGpuDecodeAttnCache; 67 68 typedef struct { 69 uint32_t layer, pos, splits, per_split, depth; 70 uint32_t grid_x, grid_y, threads; 71 uint64_t kv_bytes; /* K + V bytes the launch streams: 2 * 4 * (pos + 1) * 256 */ 72 uint64_t elapsed_ns; /* marker-to-marker chip time */ 73 uint64_t call_ns; /* whole call */ 74 uint32_t completion_marker; 75 int kernel_cache_hit; 76 } OmegaGpuDecodeAttnInfo; 77 78 int omega_gpu_decode_attn_cache_alloc(uint32_t layers, uint32_t max_pos, double rope_theta, OmegaGpuDecodeAttnCache **out); 79 void omega_gpu_decode_attn_cache_free(OmegaGpuDecodeAttnCache *c); 80 int omega_gpu_decode_attn_set_norms(OmegaGpuDecodeAttnCache *c, uint32_t layer, const float *q_norm, const float *k_norm); 81 int omega_gpu_decode_attn(OmegaGpuDecodeAttnCache *c, uint32_t layer, uint32_t pos, const float *qkv, float eps, 82 float out_scale, float *out, OmegaGpuDecodeAttnInfo *info); 83 /* Record path (OWN-30B W-L2a, decode graph; omega_gpu_cmdlist.h): the launch of omega_gpu_decode_attn appended to cl 84 * instead of submitted. qkv_va: f32 [5120] on the device (the fused q|k|v GEMV output); out_va: f32 [32 * 128] written 85 * * out_scale when the list runs (2^64 = the o_proj FP8 x' row). pos and the split count are fixed at record time. 86 * The caller holds omega_gpu_session_lock(). Nothing staged, poisoned or read back; the split counters rely on the 87 * kernel's own reset (checked after the list by omega_gpu_decode_attn_counters_check). A kernel cache miss that would 88 * evict the resident kernel is refused (warm up with one non-record call first). */ 89 struct OmegaGpuCmdList; 90 int omega_gpu_decode_attn_record(struct OmegaGpuCmdList *cl, OmegaGpuDecodeAttnCache *c, uint32_t layer, uint32_t pos, uint64_t qkv_va, 91 float eps, float out_scale, uint64_t out_va); 92 /* Caller holds the lock, after the list ran: OK when the four split counters are zero, else CHIP_FAIL. */ 93 int omega_gpu_decode_attn_counters_check(OmegaGpuDecodeAttnCache *c); 94 int omega_gpu_decode_attn_cache_write(OmegaGpuDecodeAttnCache *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_attn_cache_read(OmegaGpuDecodeAttnCache *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 (omega_gpu_prefill_attn_api.h): device addresses only. */ 99 typedef struct { 100 uint32_t layers, max_pos; 101 uint64_t k_va, v_va; /* K and V plane bases of `layer` (row (kv_head, pos) at + (kv_head * max_pos + pos) * 256) */ 102 uint64_t rope_va; /* f32 [max_pos][cos 64 | sin 64] */ 103 uint64_t q_norm_va, k_norm_va; /* f32 [128] each for `layer` */ 104 } OmegaGpuDecodeAttnCacheView; 105 int omega_gpu_decode_attn_cache_view(const OmegaGpuDecodeAttnCache *c, uint32_t layer, OmegaGpuDecodeAttnCacheView *out); 106 const char *omega_gpu_decode_attn_last_error(void); 107 const char *omega_gpu_decode_attn_rc_name(int rc); 108 109 /* ---- measurement knobs (tests; each clears the kernel cache) ---- */ 110 void omega_gpu_decode_attn_set_depth(uint32_t depth); /* 0 = default (OMEGA_GPU_DATTN_DEPTH) */ 111 void omega_gpu_decode_attn_set_per_split(uint32_t per_split); /* 0 = default; multiple of 256 */ 112 void omega_gpu_decode_attn_test_set_mutant(int mutant); /* TEST ONLY: OmegaDecodeAttnSpec.mutant */ 113 #define OMEGA_GPU_DATTN_DEPTH 4u 114 115 /* ---- host side ---- */ 116 /* bf16 round-to-nearest-even of an f32, the rounding the kernel applies to the appended K/V */ 117 uint16_t omega_dattn_bf16_rne(float f); 118 float omega_dattn_bf16_f32(uint16_t b); 119 /* RoPE table row for `pos`: cos[64] then sin[64], f32 from double, inv_freq[i] = theta^(-2i/128) */ 120 void omega_dattn_rope_row(uint32_t pos, double theta, float *row128); 121 /* The double-precision reference of the whole layer step (omega_decode_attn_reference) lives in 122 * tests/gpu_decode_attn_test.c: it needs libm and the library links without it. */ 123 124 #endif