Skip to content

perf(hip): native zero-spill RDNA3.5/4 Wave32 WMMA FlashAttention-2 and Paged Attention - #1046

Merged
davide221 merged 4 commits into
mainfrom
perf/rdna-wmma-attention
Nov 21, 2026
Merged

davide221 merged 4 commits into
mainfrom
perf/rdna-wmma-attention

Conversation

@davide221

@davide221 davide221 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Native zero-spill RDNA3.5 (gfx1151, Strix Halo) and RDNA4 (gfx1201, R9700) Wave32 WMMA + Warp-Split-D attention kernels for both FlashAttention-2 (fattn-rdna-wmma.cuh) and Paged Attention (paged-attn-rdna-wmma.cuh). Introduces 0 new env vars (reuses existing LUCE_PAGED_WMMA and GGML_CUDA_PAGED_ATTN_FORCE_PARTITIONS, and records standard ggml_backend_cuda_record_fattn_mma256_launch() / ggml_backend_cuda_record_paged_attn_wmma256_launch() telemetry).

What's Included

  1. FlashAttention-2 + GQA FlashDecoding Split-KV (fattn-rdna-wmma.cuh, fattn.cu):
    • D=128 (BM=128, BN=32) and D=256 via Warp-Split-D (SPLIT_D=2, 128 dims/warp) with XOR-swizzled bank-conflict-free KV_lds, 4-row ds_write_b64 V-transpose, exp2f online softmax, and zero-mask tile fast path.
    • GQA Head-Packing: packs (q_tok, head_in_gqa_group) into shared WMMA tiles (up to 6×–8× less KV bandwidth).
    • Split-KV FlashDecoding: saturates all CUs on small-nq speculative verify (nq=4..32).
  2. Paged Attention WMMA + Warp-Split-D (paged-attn-rdna-wmma.cuh, paged-attn.cu):
    • Unlocks WMMA paged attention for all speculative tree/chain verify (parent_ids != nullptr) on both R9700 & Strix Halo and all Strix Halo F16/prefill paged attention (previously legacy paged_attn_wmma was RDNA4-only and rejected all tree verify calls, falling back to paged_attn_quantize_q + DP4A paged_attn_decode).
    • Uses SPLIT_D=4 (64 dims/warp, 0 B register spill on both gfx1201 and gfx1151) and SPLIT_D=2 (BM=64, up to 48 queries sharing one KV load on gfx1201), with blk_seq_lds[32]/blk_kv_lds[32] supporting MHA (gqa_ratio=1) prefill.
    • Precomputes a 64-bit ancestor visibility bitmask (tree_vis_mask) once before the KV loop (1-instruction bit test per candidate) and reuses existing paged_attn_combine<256> directly.

ISA Audit & Verification (gfx1201 + gfx1151)

  • Full ROCm CI GPU test suite (ctest): 166/166 passed (0 failed) on R9700 (gfx1201) and 166/166 passed (0 failed) on Strix Halo (gfx1151), including test_fattn_mma256, test_paged_attn_wmma, paged_attn_vdot2_route, and test_model_smoke.PagedAttention.*.
  • Register spill (llvm-readelf --notes): private_segment_fixed_size = 0 on all launched paged_attn_rdna_wmma_kernel variants across both gfx1201 and gfx1151.
  • Differential parity (paged-attn, 25/25 cases across both GPUs including MHA gqa=1, R=64): max_diff ≤ 2e-5, rmse ≤ 4e-6, 0 NaNs across Q8_0/F16, B=1..8, tree_width=4/8/16, ragged branching trees, S=64..16384, and mixed-sequence causal prefill.
  • Microbench speedups:
    • fattn-rdna-wmma.cuh: 2.08×–2.56× prefill TFLOPS; 2.15×–2.53× D=256 verify on R9700, 1.53×–1.93× on Strix Halo.
    • paged-attn-rdna-wmma.cuh:
      • R9700 (gfx1201): 1.50×–2.76× faster tree verify (tree_q8_B8_w4_S4873: 643.6 → 345.4 us, tree_q8_B4_w16_S4873: 1035.2 → 375.7 us), 1.09×–1.18× faster non-tree decode vs legacy 244-VGPR paged_attn_wmma (dec_q8_B1_S4873: 94.5 → 80.4 us).
      • Strix Halo (gfx1151): 1.11×–1.72× faster tree verify (tree_q8_B4_w4_S16384: 1595.0 → 1245.3 us, tree_f16_B8_w4_S4873: 1316.3 → 764.2 us), 2.00× faster Q8_0 paged prefill (1570.4 → 786.4 us).
  • End-to-end concurrent (bench_conc.py, origin/main vs full stack):
    • q27_r9700 B=8 long_batch (4.9k cached): 160.0 → 188.1 tok/s (+17.6%), avg_dec: 2051 → 1757 ms (-14.4%).
    • q27_halo B=4 long_batch (3.0k cached): up to 78.6 → 92.2 tok/s (+17.5%), avg_dec: 4099 → 3505 ms (-14.5%).

@davide221 davide221 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review

Found 2 issues.

Copy to agent

Use the copy button in the top-right corner of the block, then paste into your agent.

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.

Repository: Luce-Org/lucebox
Reviewed commit: 6a22c7062e86667aef67ce06a9499820d9c4809f
Verify the findings against the current code, run relevant checks for any fixes, and explain any findings you reject.

<file name="server/deps/llama.cpp/ggml/src/ggml-cuda/paged-attn-rdna-wmma.cuh">

<violation number="1" line="135" end_line="136">
blk_seq_lds/blk_kv_lds sized 16 but rows_per_blk can be 32 on RDNA3.5 non-tree path

The non-tree dispatch for RDNA3.5 (`else { split_d = 4; nq_groups = 2; rows_per_blk = std::min&lt;int32_t&gt;(32 / gqa_ratio, n_rows); }`, line ~853) allows `rows_per_blk` up to 32 when `gqa_ratio == 1` (MHA) and `n_rows &gt;= 17`. The kernel then executes `if (tid &lt; blk_row_count) { ... blk_seq_lds[tid] = ps_out; blk_kv_lds[tid] = kv_out; }` (lines 232-254) with `tid` up to 255 and `blk_row_count` up to 32, writing indices 16..31 into `__shared__ int32_t blk_seq_lds[16]` / `blk_kv_lds[16]` — an out-of-bounds shared-memory write that corrupts both arrays (and the dedup/`cur_max_kv`/`cur_min_kv` computation at lines 319-334 that reads them back with indices up to 31). Result: wrong sequence lengths and wrong masking, i.e. silently incorrect attention output or garbage, for any non-tree (prefill/decode with n_rows&gt;16) MHA (`n_head == n_head_kv`) call on gfx1151. Fix: cap `rows_per_blk` at 16 in the RDNA3.5 non-tree branch (or enlarge both arrays to 32 and keep the indexing).
</violation>

<violation number="2" line="232" end_line="254">
blk_seq_lds/blk_kv_lds sized 16 but rows_per_block can be 32, overflowing shared memory on RDNA3.5 non-tree prefill

The kernel stages per-row sequence/KV-length info in `__shared__ int32_t blk_seq_lds[16]` / `blk_kv_lds[16]` (lines 135-136) and fills them with `if (tid &lt; blk_row_count)` (line 232). But the host launcher for the non-tree RDNA3 path picks `rows_per_blk = std::min&lt;int32_t&gt;(32 / gqa_ratio, n_rows)` (line 853), which is **32** when `gqa_ratio == 1` (n_head == n_head_kv) and n_rows &gt; 16 — exactly the configuration this PR enables on Strix Halo ("all Strix Halo F16/paged prefill", and Q8_0 prefill: the mode==1 gate only defers Q8_0 with n_rows &lt;= 16). With `blk_row_count` up to 32, threads tid 16..31 write `blk_seq_lds[tid]` past the end of the 16-int array (clobbering `blk_kv_lds[0..15]`, which are being written by other threads in the same pass) and `blk_kv_lds[tid]` past both arrays, corrupting adjacent shared variables / writing outside the declared shared allocation. The subsequent `for (j = 0; j &lt; blk_row_count; ++j)` scans (lines 327-334) then read garbage, so per-sequence `cur_max_kv`/`cur_min_kv`/`all_same_seq` are wrong and the online-softmax masking (need_mask fast path) miscomputes: rows attend to tokens beyond their own sequence length. Fix by sizing both arrays to 32 (or capping `rows_per_blk` at 16 for the non-tree path).

<suggestion>
__shared__ int32_t blk_seq_lds[32];
__shared__ int32_t blk_kv_lds[32];
</suggestion>
</violation>

</file>
Issue overview
  1. blk_seq_lds/blk_kv_lds sized 16 but rows_per_blk can be 32 on RDNA3.5 non-tree path · confirmed (server/deps/llama.cpp/ggml/src/ggml-cuda/paged-attn-rdna-wmma.cuh:135-136)

  2. blk_seq_lds/blk_kv_lds sized 16 but rows_per_block can be 32, overflowing shared memory on RDNA3.5 non-tree prefill · confirmed (server/deps/llama.cpp/ggml/src/ggml-cuda/paged-attn-rdna-wmma.cuh:232-254)

Reviewed 6a22c706 · 2 candidate issues checked, kept at ≥70% confidence · add the code-review label to review again

Comment on lines +135 to +136
__shared__ int32_t blk_seq_lds[16];
__shared__ int32_t blk_kv_lds[16];

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blk_seq_lds/blk_kv_lds sized 16 but rows_per_blk can be 32 on RDNA3.5 non-tree path · confirmed

The non-tree dispatch for RDNA3.5 (else { split_d = 4; nq_groups = 2; rows_per_blk = std::min<int32_t>(32 / gqa_ratio, n_rows); }, line ~853) allows rows_per_blk up to 32 when gqa_ratio == 1 (MHA) and n_rows >= 17. The kernel then executes if (tid < blk_row_count) { ... blk_seq_lds[tid] = ps_out; blk_kv_lds[tid] = kv_out; } (lines 232-254) with tid up to 255 and blk_row_count up to 32, writing indices 16..31 into __shared__ int32_t blk_seq_lds[16] / blk_kv_lds[16] — an out-of-bounds shared-memory write that corrupts both arrays (and the dedup/cur_max_kv/cur_min_kv computation at lines 319-334 that reads them back with indices up to 31). Result: wrong sequence lengths and wrong masking, i.e. silently incorrect attention output or garbage, for any non-tree (prefill/decode with n_rows>16) MHA (n_head == n_head_kv) call on gfx1151. Fix: cap rows_per_blk at 16 in the RDNA3.5 non-tree branch (or enlarge both arrays to 32 and keep the indexing).

Checked (95%): blk_seq_lds/blk_kv_lds are declared as [16] (lines 135-136), but the non-tree RDNA3.5 branch sets rows_per_blk = min(32/gqa_ratio, n_rows) (line 853), allowing up to 32; blk_row_count = row_limit - row_base can then be up to 32 and the kernel writes blk_seq_lds[tid]/blk_kv_lds[tid] for tid < blk_row_count (lines 232-254), an OOB shared-memory write, with indices up to 31 read back at lines 319-334. The path is reachable in default mode (mode==1 rejects only is_r3 Q8_0 n_rows<=16 and is_r4 n_rows>16, so MHA gqa_ratio=1 with n_rows>16 on gfx1151 reaches it).

Comment on lines +232 to +254
if (tid < blk_row_count) {
const int32_t rr = row_base + tid;
const int32_t ps_raw = active_slot_ids
? *(const int32_t *)(active_slot_ids + (int64_t) rr * asi_nb0)
: rr;
int32_t ps_out = -1;
int32_t kv_out = 0;
const int32_t qpos = query_positions
? *(const int32_t *)(query_positions + (int64_t) rr * qpos_nb0)
: -1;
if (ps_raw >= 0 && ps_raw < n_table_seq && (!query_positions || qpos >= 0)) {
int32_t ksl = *(const int32_t *)(kv_seq_lens + (int64_t) ps_raw * ksl_nb0);
if (query_positions && qpos < ksl) {
ksl = qpos + 1;
}
const int32_t kl = ksl <= 0 ? 0 : (ksl < table_capacity ? ksl : (int32_t) table_capacity);
if (kl > 0) {
ps_out = ps_raw;
kv_out = kl;
}
}
blk_seq_lds[tid] = ps_out;
blk_kv_lds[tid] = kv_out;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blk_seq_lds/blk_kv_lds sized 16 but rows_per_block can be 32, overflowing shared memory on RDNA3.5 non-tree prefill · confirmed

The kernel stages per-row sequence/KV-length info in __shared__ int32_t blk_seq_lds[16] / blk_kv_lds[16] (lines 135-136) and fills them with if (tid < blk_row_count) (line 232). But the host launcher for the non-tree RDNA3 path picks rows_per_blk = std::min<int32_t>(32 / gqa_ratio, n_rows) (line 853), which is 32 when gqa_ratio == 1 (n_head == n_head_kv) and n_rows > 16 — exactly the configuration this PR enables on Strix Halo ("all Strix Halo F16/paged prefill", and Q8_0 prefill: the mode==1 gate only defers Q8_0 with n_rows <= 16). With blk_row_count up to 32, threads tid 16..31 write blk_seq_lds[tid] past the end of the 16-int array (clobbering blk_kv_lds[0..15], which are being written by other threads in the same pass) and blk_kv_lds[tid] past both arrays, corrupting adjacent shared variables / writing outside the declared shared allocation. The subsequent for (j = 0; j < blk_row_count; ++j) scans (lines 327-334) then read garbage, so per-sequence cur_max_kv/cur_min_kv/all_same_seq are wrong and the online-softmax masking (need_mask fast path) miscomputes: rows attend to tokens beyond their own sequence length. Fix by sizing both arrays to 32 (or capping rows_per_blk at 16 for the non-tree path).

Checked (92%): The non-tree RDNA3 launcher picks rows_per_blk = min(32/gqa_ratio, n_rows) (paged-attn-rdna-wmma.cuh:853), which is up to 32 when gqa_ratio==1 and n_rows>16, and the mode==1 gate (lines 790-797) allows exactly this config on RDNA3 (F16 any n_rows>16; Q8_0 n_rows>16). In the kernel, threads with tid 16..31 then write blk_seq_lds[tid]/blk_kv_lds[tid] (lines 232-254) past the 16-int arrays declared at lines 135-136, clobbering adjacent shared memory (including each other, unsynchronized), and the scans at lines 319-334 read blk_seq_lds[0..blk_row_count-1] out of bounds, so cur_max_kv/cur_min_kv/all_same_seq are garbage and the masking fast path (line 492-493) miscomputes. gqa_ratio==1 (MHA) is explicitly allowed by the gate at line 769 and occurs in normal prefill use; the tree path is unaffected because the arrays are only touched under !IS_TREE.

Suggested change
if (tid < blk_row_count) {
const int32_t rr = row_base + tid;
const int32_t ps_raw = active_slot_ids
? *(const int32_t *)(active_slot_ids + (int64_t) rr * asi_nb0)
: rr;
int32_t ps_out = -1;
int32_t kv_out = 0;
const int32_t qpos = query_positions
? *(const int32_t *)(query_positions + (int64_t) rr * qpos_nb0)
: -1;
if (ps_raw >= 0 && ps_raw < n_table_seq && (!query_positions || qpos >= 0)) {
int32_t ksl = *(const int32_t *)(kv_seq_lens + (int64_t) ps_raw * ksl_nb0);
if (query_positions && qpos < ksl) {
ksl = qpos + 1;
}
const int32_t kl = ksl <= 0 ? 0 : (ksl < table_capacity ? ksl : (int32_t) table_capacity);
if (kl > 0) {
ps_out = ps_raw;
kv_out = kl;
}
}
blk_seq_lds[tid] = ps_out;
blk_kv_lds[tid] = kv_out;
__shared__ int32_t blk_seq_lds[32];
__shared__ int32_t blk_kv_lds[32];

@davide221
davide221 merged commit 175a987 into main Nov 21, 2026
13 of 14 checks passed
@davide221
davide221 deleted the perf/rdna-wmma-attention branch November 21, 2026 01:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant