interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / src / omega_blackwell_codegen_pattn.h

84 lines · 6191 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2#ifndef OMEGA_BLACKWELL_CODEGEN_PATTN_H
3#define OMEGA_BLACKWELL_CODEGEN_PATTN_H
4/*
5 * omega_blackwell_codegen_pattn: causal prefill attention for Qwen3-30B-A3B on tensor cores (OWN-30B L1 attention,
6 * 2026-10-13). One launch per chunk of up to 64 consecutive tokens of one layer. New file; uses the opcodes of
7 * omega_blackwell_codegen.h only (SHFL.BFLY and F2FP.BF16.F32.PACK_AB are native encoder entries since training R5;
8 * before R5 they were placeholders patched after encoding).
9 *
10 * Grid (ceil(mc / W), 4): CTA (cx, cy) serves KV head cy and the chunk tokens cx*W .. cx*W+W-1 (W = warps, 4 or 8).
11 * Row tile = 16 query rows = 2 tokens x the 8 query heads of the group: row g (0..7) = token 2*tile, head cy*8+g;
12 * row g+8 = token 2*tile+1, same head. Warp w: tile w/2, output dims 64*(w%2) .. +63 (two warps share a tile and
13 * both compute its scores; each keeps half of O: 8 HMMA n-tiles = 32 f32 registers).
14 *
15 * Prologue (all warps, CTA barriers; uniform trip counts):
16 * K/V append: every CTA computes K = bf16(RoPE(RMSNorm(k) * k_norm)) and V = bf16(v) for the chunk tokens
17 * 0 .. nk-1 (nk = min(cx*W + W, mc)) with the decode kernel's exact instruction sequence (quad per lane,
18 * SHFL.DOWN sum, lane 0's sum through shared memory, RSQ, partner half through shared memory) and stores them
19 * into the cache rows pos0 + t. CTAs write identical bits to the rows they share. MEMBAR.SYS + BAR.SYNC, then
20 * the CTA reads its own rows back from the cache.
21 * Q: q' = bf16(RoPE(RMSNorm(q) * q_norm) * qk_scale) per (token, head) item, staged in shared memory in HMMA
22 * A-fragment order (2 tiles per round), then each warp loads its 8 fragments (32 registers) for the whole run.
23 * Main loop over key blocks of 32 (kb = 0, 32, .. <= kend = pos0 + nk - 1):
24 * stage K (fragment-major, B of S = Q K^T) and V (transposed pairs, B of O = P V) of the block in shared memory
25 * (16 KB), S = Q K^T (8 k-steps x 4 n-tiles, HMMA.16816.F32.BF16), causal mask (key > row position: s += -1e30,
26 * skipped when the whole block is at or below the tile's first token), online softmax in log2 units (EX2), O
27 * rescaled by alpha = EX2(m_old - m_new), P packed to bf16 (F2FP), O += P V (2 k-steps x 8 n-tiles).
28 * Epilogue: l summed over the quad (SHFL.BFLY), out = O * RCP(l) * out_scale for tokens < mc.
29 * Split modes (spec.mode, 2026-10-13 cut 3): an APPEND launch then an ATTEND launch over the same mc tokens do the
30 * fused kernel's work with no redundant appends, so one pair of launches can cover a whole prompt (grid ceil(mc/W) x 4)
31 * instead of 64-token chunks of 8 x 4 CTAs. The appended rows are the same instruction sequence (identical bits).
32 *
33 * The host simulator (tests/bw_warp_sim_pa.h) runs the kernel with the chip-exact HMMA model; EX2/RSQ/RCP are
34 * exact there and approximate on the chip. A simulator pass is never a chip result.
35 */
36#include "omega_blackwell_codegen.h"
37#include <stdint.h>
38
39#define OMEGA_PATTN_CHUNK 64u /* tokens per launch */
40#define OMEGA_PATTN_KEYS 32u /* keys per main-loop block */
41#define OMEGA_PATTN_QKV_WORDS 5120u /* f32 per token row of the fused q|k|v projection */
42#define OMEGA_PATTN_OUT_WORDS 4096u /* f32 per token row of the output (32 heads x 128) */
43#define OMEGA_PATTN_SHARED_BYTES 16384u /* inside the QMD shared cap (omega_blackwell_qmd.h) */
44
45/* constant bank 0 at 0x380 */
46#define OMEGA_PATTN_ARG_QKV 0x380u /* f32 [64][5120]: row t = chunk token t (position pos0 + t) */
47#define OMEGA_PATTN_ARG_KC 0x388u /* bf16 K plane base [kv_head][max_pos][128] */
48#define OMEGA_PATTN_ARG_VC 0x390u /* bf16 V plane base */
49#define OMEGA_PATTN_ARG_ROPE 0x398u /* f32 [max_pos][cos 64 | sin 64] */
50#define OMEGA_PATTN_ARG_QN 0x3a0u /* f32 [128] q_norm */
51#define OMEGA_PATTN_ARG_KN 0x3a8u /* f32 [128] k_norm */
52#define OMEGA_PATTN_ARG_OUT 0x3b0u /* f32 [64][4096] */
53#define OMEGA_PATTN_ARG_POS0 0x3b8u /* u32 position of chunk token 0 */
54#define OMEGA_PATTN_ARG_MC 0x3bcu /* u32 tokens in this chunk, 1..64 */
55#define OMEGA_PATTN_ARG_KV_STRIDE 0x3c0u /* u32 bytes per KV head plane (max_pos * 256) */
56#define OMEGA_PATTN_ARG_EPS 0x3c4u /* f32 */
57#define OMEGA_PATTN_ARG_OUT_SCALE 0x3c8u /* f32 */
58#define OMEGA_PATTN_ARG_QK_SCALE 0x3ccu /* f32 (1/sqrt(128)) * log2(e) */
59#define OMEGA_PATTN_ARG_KC2 0x3d0u /* APPEND with spec.mirror: second K plane base (GPU-cached mirror) */
60#define OMEGA_PATTN_ARG_VC2 0x3d8u /* second V plane base */
61#define OMEGA_PATTN_ARG_KV_STRIDE2 0x3e0u /* u32 bytes per KV head plane of the mirror */
62#define OMEGA_PATTN_ARGS_WORDS 25u
63
64typedef struct {
65 uint32_t warps; /* 4 or 8 (tokens per CTA) */
66 int mutant; /* TEST ONLY. 0 none; 1 no RoPE on q and k; 2 no causal mask; 3 the new K/V are not written;
67 * 4 O is never rescaled by alpha. Each must be caught by the gates. */
68 uint32_t mode; /* OMEGA_PATTN_MODE_*: 0 fused (append + attend, chunks of <= 64 tokens);
69 * 1 append only: CTA (cx, cy) appends just its own tokens cx*W .. cx*W+W-1 (< mc), then exits;
70 * 2 attend only: no append (a mode-1 launch over the same tokens ran before), any mc >= 1. */
71 uint32_t mirror; /* APPEND only: 1 also stores each new row into the mirror planes (ARG_KC2 / VC2 / KV_STRIDE2), e.g. a
72 * GPU-cached copy of the layer's K/V that the ATTEND launch then reads instead of the uncached cache */
73} OmegaPrefillAttnSpec;
74#define OMEGA_PATTN_MODE_FUSED 0u
75#define OMEGA_PATTN_MODE_APPEND 1u
76#define OMEGA_PATTN_MODE_ATTEND 2u
77
78int omega_pattn_spec_ok(const OmegaPrefillAttnSpec *spec);
79/* Builds the register-allocated IR program. 0 ok, -1 refused or over BW_MAX_IR_INSNS / BW_MAX_VREGS. */
80int omega_blackwell_codegen_prefill_attn_prog(const OmegaPrefillAttnSpec *spec, BlackwellIRProgram *prog);
81/* Instruction and peak-register counts (host, no device). */
82int omega_prefill_attn_prog_stats(const OmegaPrefillAttnSpec *spec, uint32_t *insns, uint32_t *gprs);
83
84#endif