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