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_blackwell_codegen_attn.h
| 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_BLACKWELL_CODEGEN_ATTN_H |
| 18 | #define OMEGA_BLACKWELL_CODEGEN_ATTN_H |
| 19 | /* |
| 20 | * omega_blackwell_codegen_attn: decode attention for Qwen3-30B-A3B in ONE launch per layer (OWN-30B cut 8, |
| 21 | * 2026-10-13). Grouped-query attention with 8 query heads per KV head, head_dim 128, per-head RMSNorm on q |
| 22 | * and k, rotary embedding from a host-built cos/sin table, the new token's K/V appended to a GPU-resident |
| 23 | * bf16 cache inside the kernel, softmax over the whole context, output ready for o_proj (scaled by |
| 24 | * `out_scale`, so it can be the x' row of the FP8 GEMV without a host round trip). |
| 25 | * |
| 26 | * Grid: (splits, kv_heads). CTA (cx, cy) handles KV head cy and the positions [cx*per_split, min(+per_split, |
| 27 | * pos+1)) of the context; the host picks splits = ceil((pos+1)/per_split), so no CTA is empty. 8 warps = 256 |
| 28 | * threads per CTA (fixed: warp h = query head h of the group in the prologue, lane = position in the score pass). |
| 29 | * |
| 30 | * Per CTA: |
| 31 | * prologue warp h: q[cy*8+h] (f32 quad per lane) -> RMSNorm(q_norm, eps) -> RoPE(table[pos]) -> * qk_scale, |
| 32 | * staged in shared memory as q'[h][128]. Every warp also computes k_new = RoPE(RMSNorm(k[cy])) |
| 33 | * and v_new; the CTA whose range holds `pos` (the last split) writes them as bf16 into the cache at |
| 34 | * [cy][pos] (warp 0 K, warp 1 V), then MEMBAR + BAR.SYNC, so its own stream reads them back. |
| 35 | * chunks of 256 positions: score pass (thread = position: the bf16 K row streamed in 16 quads with a |
| 36 | * `depth`-deep prefetch, 8 dot products against q' from shared memory, scores to shared s[t][8]); |
| 37 | * V pass (warp w: positions w, w+8, ...; lane = 4 dims; online softmax per warp: running max, |
| 38 | * alpha = EX2(m_old - m_new) applied only when the max moved (warp-uniform branch), p = EX2(s - m), |
| 39 | * acc += p * v, l += p). |
| 40 | * epilogue warps combine (m, l, acc) through shared memory and write the CTA partial |
| 41 | * {m[8], l[8], acc[8][128]} to partials[cy][cx]; then one ATOMG.ADD on counters[cy]; the last CTA |
| 42 | * of the KV head combines the partials with the usual max rescale and writes out[(cy*8+h)*128+d] |
| 43 | * * out_scale, and resets the counter. |
| 44 | * |
| 45 | * Numbers (f32 on the chip, double in the host reference, tolerances in tests/gpu_decode_attn_test.c): |
| 46 | * scores in log2 units: q' already carries (1/sqrt(128)) * log2(e), so p = EX2(s - m) = exp((q.k)/sqrt(d) - M). |
| 47 | * bf16 K/V are widened by a 16-bit shift; the new K/V are rounded to bf16 round-to-nearest-even before storing |
| 48 | * (the reference rounds the same way, so the attention of the new token sees the stored value). |
| 49 | * |
| 50 | * The host simulator (tests/bw_warp_sim_k.h) runs this kernel at 8 warps; a simulator pass is never a chip result. |
| 51 | */ |
| 52 | #include "omega_blackwell_codegen.h" |
| 53 | #include <stdint.h> |
| 54 | |
| 55 | #define OMEGA_DATTN_HEAD_DIM 128u |
| 56 | #define OMEGA_DATTN_Q_PER_KV 8u |
| 57 | #define OMEGA_DATTN_WARPS 8u |
| 58 | #define OMEGA_DATTN_CHUNK 256u /* positions per score pass = threads per CTA */ |
| 59 | #define OMEGA_DATTN_ROW_BYTES 256u /* one bf16 K or V row */ |
| 60 | #define OMEGA_DATTN_ROPE_ROW_BYTES 512u /* cos[64] then sin[64], f32 */ |
| 61 | #define OMEGA_DATTN_PARTIAL_WORDS 1040u /* m[8], l[8], acc[8][128] */ |
| 62 | #define OMEGA_DATTN_MAX_DEPTH 4u |
| 63 | /* bytes past the last row of a K or V plane the prefetch may touch (clamped reads of the last rows) */ |
| 64 | #define OMEGA_DATTN_PLANE_SLACK (OMEGA_DATTN_MAX_DEPTH * 8u * OMEGA_DATTN_ROW_BYTES + 64u) |
| 65 | |
| 66 | /* constant bank: every address at 0x380 + 8n, then the scalars */ |
| 67 | #define OMEGA_DATTN_ARG_QKV 0x380u /* f32 [32*128 q | 4*128 k | 4*128 v] */ |
| 68 | #define OMEGA_DATTN_ARG_KC 0x388u /* bf16 K cache base: [kv_head][max_pos][128], kv_stride bytes per head */ |
| 69 | #define OMEGA_DATTN_ARG_VC 0x390u /* bf16 V cache base, same layout */ |
| 70 | #define OMEGA_DATTN_ARG_ROPE 0x398u /* f32 [max_pos][cos 64 | sin 64] */ |
| 71 | #define OMEGA_DATTN_ARG_QN 0x3a0u /* f32 [128] q_norm weight */ |
| 72 | #define OMEGA_DATTN_ARG_KN 0x3a8u /* f32 [128] k_norm weight */ |
| 73 | #define OMEGA_DATTN_ARG_OUT 0x3b0u /* f32 [32][128] */ |
| 74 | #define OMEGA_DATTN_ARG_PART 0x3b8u /* f32 [kv_heads][splits][1040] */ |
| 75 | #define OMEGA_DATTN_ARG_CNT 0x3c0u /* u32 [kv_heads], zero before the launch */ |
| 76 | #define OMEGA_DATTN_ARG_POS 0x3c8u /* u32 position of the new token; context = pos + 1 */ |
| 77 | #define OMEGA_DATTN_ARG_PER_SPLIT 0x3ccu /* u32 positions per CTA (multiple of 256) */ |
| 78 | #define OMEGA_DATTN_ARG_SPLITS 0x3d0u /* u32 = grid_x */ |
| 79 | #define OMEGA_DATTN_ARG_KV_STRIDE 0x3d4u /* u32 bytes per KV head plane */ |
| 80 | #define OMEGA_DATTN_ARG_EPS 0x3d8u /* f32 rms eps */ |
| 81 | #define OMEGA_DATTN_ARG_OUT_SCALE 0x3dcu /* f32 multiplier on the output (2^64 for the FP8 GEMV x' row, 1 otherwise) */ |
| 82 | #define OMEGA_DATTN_ARG_QK_SCALE 0x3e0u /* f32 (1/sqrt(128)) * log2(e) */ |
| 83 | #define OMEGA_DATTN_ARGS_WORDS 25u |
| 84 | |
| 85 | /* shared memory map (bytes) */ |
| 86 | #define OMEGA_DATTN_SM_Q 0u /* q'[8][128] f32: 4096 */ |
| 87 | #define OMEGA_DATTN_SM_S 4096u /* s[256][8] f32: 8192; reused as the reduce buffer r[8 warps][256] */ |
| 88 | #define OMEGA_DATTN_SM_M 12288u /* m[8 warps][8] then the head maxes M[8]: 256 + 32 */ |
| 89 | #define OMEGA_DATTN_SM_L 12576u /* l[8 warps][8]: 256 */ |
| 90 | #define OMEGA_DATTN_SM_FLAG 12832u /* the atomic's old value: 16 */ |
| 91 | #define OMEGA_DATTN_SM_K 12864u /* k_new[128] f32 staging: 512 */ |
| 92 | #define OMEGA_DATTN_SHARED_BYTES 13376u |
| 93 | |
| 94 | typedef struct { |
| 95 | uint32_t depth; /* prefetch depth of the K and V streams, 1..4 */ |
| 96 | int mutant; /* TEST ONLY. 0 none; 1 no RoPE on q and k; 2 V read one position late; 3 the new K/V are not |
| 97 | * written to the cache; 4 the cross-split combine ignores the max (differs only when splits > 1). */ |
| 98 | } OmegaDecodeAttnSpec; |
| 99 | |
| 100 | /* Builds the IR program (register-allocated) into *prog. 0 ok, -1 refused (depth 0 or > 4, mutant outside 0..4, |
| 101 | * or the program outgrew BW_MAX_IR_INSNS / BW_MAX_VREGS). */ |
| 102 | int omega_blackwell_codegen_decode_attn_prog(const OmegaDecodeAttnSpec *spec, BlackwellIRProgram *prog); |
| 103 | /* Instruction and peak-register counts of the program (host, no device). */ |
| 104 | int omega_decode_attn_prog_stats(const OmegaDecodeAttnSpec *spec, uint32_t *insns, uint32_t *gprs); |
| 105 | |
| 106 | #endif |