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_attn256.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_attn256.h` 131 lines · 10835 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 #ifndef OMEGA_BLACKWELL_CODEGEN_ATTN256_H 3 #define OMEGA_BLACKWELL_CODEGEN_ATTN256_H 4 /* 5 * omega_blackwell_codegen_attn256: decode attention for Qwen3.8-27B in ONE launch per layer (omega-mv#544, 6 * 2026-10-18). A derivative of omega_blackwell_codegen_attn.h (head_dim 128, 8 q per kv) for the 27B's full-attention 7 * layers: 24 query heads, 4 KV heads (6 q per kv), head_dim 256, per-head RMSNorm on q and k (the caller folds the 8 * model's zero-centred weights to 1 + w), rotary embedding from a host-built cos/sin table, the new token's K/V 9 * appended to the GPU-resident bf16 cache inside the kernel, softmax over the whole context, output scaled by 10 * `out_scale` ready for the gate (sigmoid(gate) * out happens outside) and o_proj. 11 * 12 * Partial RoPE by permutation (design decision 1 of #544): the kernel rotates the standard half pairs (i, i + 128) of 13 * its 256-wide head with table row [cos 128 | sin 128]; the host builds the table so only the first P pairs rotate 14 * (inv_freq_j = theta^(-2j / rotary_dim), rotary_dim = 2P) and the rest are identity (cos 1, sin 0). The engine 15 * permutes the q and k dims (and q_norm / k_norm) at load with omega_dattn256_perm (omega_gpu_decode_attn256_api.h) 16 * so the model's rotate-half within its first rotary_dim dims lands on those P pairs. Scores are invariant to a 17 * shared q/k permutation; V and the output are not permuted. The K cache holds permuted rows. 18 * 19 * Grid: (splits, kv_heads). CTA (cx, cy) handles KV head cy and the positions [cx*per_split, min(+per_split, pos+1)); 20 * the host picks splits = ceil((pos+1)/per_split), so no CTA is empty. 8 warps = 256 threads per CTA. 21 * 22 * Per CTA: 23 * prologue one norm + rope block for all 8 warps (8 f32 per lane): warp h < 6 builds q'[h] = RoPE(table[pos], 24 * RMSNorm(q[cy*6+h], q_norm, eps)) * qk_scale, staged in shared memory as q'[h][256]; warp 6 builds 25 * k_new = RoPE(RMSNorm(k[cy], k_norm)) the same way (k_norm lives 1024 bytes after q_norm); warp 7 runs 26 * the block on v[cy] (result discarded) and reloads the raw v. In the CTA whose range holds `pos` (the 27 * last split) warps 6 and 7 write k_new and v_new as bf16 into the cache at [cy][pos], then MEMBAR + 28 * BAR.SYNC, so its own stream reads them back. 29 * chunks of 256 positions: score pass (thread = position: the 512-byte bf16 K row streamed in 32 quads with a 30 * `depth`-deep prefetch, 6 dot products against q' from shared memory, scores to shared s[t][8], 6 used); 31 * V pass (warp pair: warp w handles positions (w>>1) + 4i of the chunk and the dims 128*(w&1) .. +127; 32 * lane = 4 dims, LDG.64 per position; online softmax per warp: running max, alpha = EX2(m_old - m_new) 33 * applied only when the max moved (warp-uniform branch), p = EX2(s - m), acc += p * v, l += p. The two 34 * warps of a pair see the same positions, so their (m, l) are bit-identical). 35 * epilogue warps combine (m over all 8, l over the even warps, acc over the 4 warps that hold each dim half) 36 * through shared memory and write the CTA partial {m[6], l[6], acc[6][256]} to partials[cy][cx]; then 37 * one ATOMG.ADD on counters[cy]; the last CTA of the KV head combines the partials with the usual max 38 * rescale and writes out[(cy*6+h)*256+d] * out_scale, and resets the counter. 39 * 40 * Numbers (f32 on the chip, double in the host reference, tolerances in tests/gpu_decode_attn256_test.c): 41 * scores in log2 units: q' already carries (1/sqrt(256)) * log2(e), so p = EX2(s - m) = exp((q.k)/sqrt(d) - M). 42 * bf16 K/V are widened by a 16-bit shift; the new K/V are rounded to bf16 round-to-nearest-even before storing. 43 * 44 * Shared memory stays under the 16 KiB QMD cap (14896 bytes), registers under the chip allocator's 126. 45 * The host simulator (tests/bw_warp_sim_k.h) runs this kernel at 8 warps; a simulator pass is never a chip result. 46 */ 47 #include "omega_blackwell_codegen.h" 48 #include 49 50 #define OMEGA_DATTN256_HEAD_DIM 256u 51 #define OMEGA_DATTN256_Q_PER_KV 6u 52 #define OMEGA_DATTN256_KV_HEADS 4u 53 #define OMEGA_DATTN256_Q_HEADS 24u 54 #define OMEGA_DATTN256_WARPS 8u 55 #define OMEGA_DATTN256_CHUNK 256u /* positions per score pass = threads per CTA */ 56 #define OMEGA_DATTN256_ROW_BYTES 512u /* one bf16 K or V row */ 57 #define OMEGA_DATTN256_ROPE_ROW_BYTES 1024u /* cos[128] then sin[128], f32 */ 58 #define OMEGA_DATTN256_PARTIAL_WORDS 1548u /* m[6], l[6], acc[6][256] */ 59 #define OMEGA_DATTN256_MAX_DEPTH 4u 60 /* bytes past the last row of a K or V plane the prefetch may touch (clamped reads of the last rows) */ 61 #define OMEGA_DATTN256_PLANE_SLACK (OMEGA_DATTN256_MAX_DEPTH * 4u * OMEGA_DATTN256_ROW_BYTES + 64u) 62 #define OMEGA_DATTN256_QKV_WORDS (OMEGA_DATTN256_Q_HEADS * 256u + 2u * OMEGA_DATTN256_KV_HEADS * 256u) /* 8192 */ 63 #define OMEGA_DATTN256_OUT_WORDS (OMEGA_DATTN256_Q_HEADS * 256u) /* 6144 */ 64 65 /* constant bank: every address at 0x380 + 8n, then the scalars */ 66 #define OMEGA_DATTN256_ARG_QKV 0x380u /* f32 [24*256 q | 4*256 k | 4*256 v] (the gate is NOT part of it) */ 67 #define OMEGA_DATTN256_ARG_KC 0x388u /* bf16 K cache base: [kv_head][max_pos][256], kv_stride bytes per head */ 68 #define OMEGA_DATTN256_ARG_VC 0x390u /* bf16 V cache base, same layout */ 69 #define OMEGA_DATTN256_ARG_ROPE 0x398u /* f32 [max_pos][cos 128 | sin 128] */ 70 #define OMEGA_DATTN256_ARG_QN 0x3a0u /* f32 [256] q_norm weight (1 + w, permuted) */ 71 #define OMEGA_DATTN256_ARG_KN 0x3a8u /* f32 [256] k_norm weight (1 + w, permuted); MUST be ARG_QN + 1024 bytes: the kernel addresses it so */ 72 #define OMEGA_DATTN256_ARG_OUT 0x3b0u /* f32 [24][256] */ 73 #define OMEGA_DATTN256_ARG_PART 0x3b8u /* f32 [kv_heads][splits][1548] */ 74 #define OMEGA_DATTN256_ARG_CNT 0x3c0u /* u32 [kv_heads], zero before the launch */ 75 #define OMEGA_DATTN256_ARG_POS 0x3c8u /* u32 position of the new token; context = pos + 1 */ 76 #define OMEGA_DATTN256_ARG_PER_SPLIT 0x3ccu /* u32 positions per CTA (multiple of 256) */ 77 #define OMEGA_DATTN256_ARG_SPLITS 0x3d0u /* u32 = grid_x */ 78 #define OMEGA_DATTN256_ARG_KV_STRIDE 0x3d4u /* u32 bytes per KV head plane */ 79 #define OMEGA_DATTN256_ARG_EPS 0x3d8u /* f32 rms eps */ 80 #define OMEGA_DATTN256_ARG_OUT_SCALE 0x3dcu /* f32 multiplier on the output */ 81 #define OMEGA_DATTN256_ARG_QK_SCALE 0x3e0u /* f32 (1/sqrt(256)) * log2(e) */ 82 #define OMEGA_DATTN256_ARGS_WORDS 25u 83 /* L369 multi-token mode (spec.multi = 1) only: */ 84 #define OMEGA_DATTN256_ARG_XSTRIDE 0x3e4u /* u32 bytes between token t and t+1 qkv rows */ 85 #define OMEGA_DATTN256_ARG_OSTRIDE 0x3e8u /* u32 bytes between output rows (attend) */ 86 #define OMEGA_DATTN256_ARG_PSTRIDE 0x3ecu /* u32 bytes between tokens' partial blocks (attend; >= vy * splits * 6192) */ 87 #define OMEGA_DATTN256_MULTI_ARGS_WORDS 28u 88 89 /* shared memory map (bytes) */ 90 #define OMEGA_DATTN256_SM_Q 0u /* q'[6][256] f32: 6144 (warps 6, 7 stage k and v at 6144.. 8191: inside SM_S, unused then) */ 91 #define OMEGA_DATTN256_SM_S 6144u /* s[256][8] f32 (6 used): 8192; reused as the reduce buffer r[8 warps][256] */ 92 #define OMEGA_DATTN256_SM_M 14336u /* m[8 warps][8] then the head maxes M[8]: 256 + 32 */ 93 #define OMEGA_DATTN256_SM_L 14624u /* l[8 warps][8]: 256 */ 94 #define OMEGA_DATTN256_SM_FLAG 14880u /* the atomic's old value: 16 */ 95 #define OMEGA_DATTN256_SHARED_BYTES 14896u 96 97 typedef struct { 98 uint32_t depth; /* prefetch depth of the K and V streams, 1..4 */ 99 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 100 * written to the cache; 4 the cross-split combine ignores the max (differs only when splits > 1); 101 * 5 the odd warps' dim half is dropped from the partial (dims 128..255 wrong). */ 102 uint32_t prep; /* 1: the prefill prep launch (omega-mv#545): grid (m, 4), CTA (cx, cy) runs this kernel's prologue for 103 * token cx at position ARG_POS + cx (qkv row cx, 32768 bytes per row), writes k_new / v_new into the 104 * cache rows as the decode step does (same instruction sequence, so the same bits), writes q' as bf16 105 * to ARG_OUT in the attend kernel's A-fragment layout (OMEGA_PATTN256_QF_*), and exits. Mutants 1 106 * and 3 apply; ARG_PART, ARG_CNT, ARG_PER_SPLIT, ARG_SPLITS and ARG_OUT_SCALE are not read. */ 107 uint32_t q_heads, kv_heads, q_per_cta; /* Q36 (sc L306): the model shape. 0, 0, 0 = 24, 4, 6 (Qwen3.8-27B, the program 108 * before these fields existed). Qwen3.6-35B-A3B: 16, 2, 4. The grid is (splits, q_heads / q_per_cta): CTA row cy 109 * ("virtual head") owns q heads cy*q_per_cta .. +q_per_cta-1 and reads KV head cy / G, G = (q_heads / q_per_cta) / 110 * kv_heads (1 or 2; with 2 both CTA rows of a KV head write the same new K/V bits to the cache). q_per_cta <= 6 111 * (warps 6, 7 build k and v). Appended last. */ 112 uint32_t multi; /* L369: 1 = T prompt tokens at positions ARG_POS + t, qkv rows ARG_XSTRIDE bytes apart (per_split must be 256; 113 * vy a power of two). With prep = 1: grid (T, vy), CTA (t, cy) writes token t's k_new / v_new into the cache 114 * exactly as prep does and nothing else (no q'). With prep = 0 (attend): grid (splits_max, T * vy), CTA (cx, t * 115 * vy + cy) is the decode step of token t (splits = pos_t / 256 + 1; CTAs with cx >= splits exit at once) WITHOUT 116 * the K/V append (the prep launch before it wrote every token's rows; a split never reads past its token); 117 * out row t at ARG_OUT + t * ARG_OSTRIDE, partials at ARG_PART + t * ARG_PSTRIDE, counters at ARG_CNT + t * vy * 4. 118 * Every value is the decode launch's, bit for bit (same instructions, same combine order). */ 119 } OmegaDecodeAttn256Spec; 120 121 /* The shape a spec selects: 0 ok, -1 unsupported. */ 122 typedef struct { uint32_t qh, kvh, h, vy, g, qkv_words, out_words; } OmegaDattn256Shape; 123 int omega_dattn256_shape(const OmegaDecodeAttn256Spec *spec, OmegaDattn256Shape *s); 124 125 /* Builds the IR program (register-allocated) into *prog. 0 ok, -1 refused (depth 0 or > 4, mutant outside 0..5, 126 * or the program outgrew BW_MAX_IR_INSNS / BW_MAX_VREGS). */ 127 int omega_blackwell_codegen_decode_attn256_prog(const OmegaDecodeAttn256Spec *spec, BlackwellIRProgram *prog); 128 /* Instruction and peak-register counts of the program (host, no device). */ 129 int omega_decode_attn256_prog_stats(const OmegaDecodeAttn256Spec *spec, uint32_t *insns, uint32_t *gprs); 130 131 #endif