interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / omega_blackwell_codegen_pattn.h

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