Repository navigation
perf(hip): native zero-spill RDNA3.5/4 Wave32 WMMA FlashAttention-2 and Paged Attention - #1046
Conversation
… GQA FlashDecoding Split-KV
davide221
left a comment
There was a problem hiding this comment.
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<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).
</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 < 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).
<suggestion>
__shared__ int32_t blk_seq_lds[32];
__shared__ int32_t blk_kv_lds[32];
</suggestion>
</violation>
</file>
Issue overview
-
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) -
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
| __shared__ int32_t blk_seq_lds[16]; | ||
| __shared__ int32_t blk_kv_lds[16]; |
There was a problem hiding this comment.
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).
| 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; |
There was a problem hiding this comment.
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.
| 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]; |
…record wmma256 launches
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 existingLUCE_PAGED_WMMAandGGML_CUDA_PAGED_ATTN_FORCE_PARTITIONS, and records standardggml_backend_cuda_record_fattn_mma256_launch()/ggml_backend_cuda_record_paged_attn_wmma256_launch()telemetry).What's Included
fattn-rdna-wmma.cuh,fattn.cu):D=128(BM=128,BN=32) andD=256via Warp-Split-D (SPLIT_D=2, 128 dims/warp) with XOR-swizzled bank-conflict-freeKV_lds, 4-rowds_write_b64V-transpose,exp2fonline softmax, and zero-mask tile fast path.(q_tok, head_in_gqa_group)into shared WMMA tiles (up to 6×–8× less KV bandwidth).nqspeculative verify (nq=4..32).paged-attn-rdna-wmma.cuh,paged-attn.cu):parent_ids != nullptr) on both R9700 & Strix Halo and all Strix Halo F16/prefill paged attention (previously legacypaged_attn_wmmawas RDNA4-only and rejected all tree verify calls, falling back topaged_attn_quantize_q+ DP4Apaged_attn_decode).SPLIT_D=4(64dims/warp, 0 B register spill on bothgfx1201andgfx1151) andSPLIT_D=2(BM=64, up to 48 queries sharing one KV load ongfx1201), withblk_seq_lds[32]/blk_kv_lds[32]supporting MHA (gqa_ratio=1) prefill.tree_vis_mask) once before the KV loop (1-instruction bit test per candidate) and reuses existingpaged_attn_combine<256>directly.ISA Audit & Verification (
gfx1201+gfx1151)ctest): 166/166 passed (0failed) on R9700 (gfx1201) and 166/166 passed (0failed) on Strix Halo (gfx1151), includingtest_fattn_mma256,test_paged_attn_wmma,paged_attn_vdot2_route, andtest_model_smoke.PagedAttention.*.llvm-readelf --notes):private_segment_fixed_size = 0on all launchedpaged_attn_rdna_wmma_kernelvariants across bothgfx1201andgfx1151.paged-attn, 25/25 cases across both GPUs including MHAgqa=1, R=64):max_diff ≤ 2e-5,rmse ≤ 4e-6,0NaNs acrossQ8_0/F16,B=1..8,tree_width=4/8/16, ragged branching trees,S=64..16384, and mixed-sequence causal prefill.fattn-rdna-wmma.cuh: 2.08×–2.56× prefill TFLOPS; 2.15×–2.53×D=256verify on R9700, 1.53×–1.93× on Strix Halo.paged-attn-rdna-wmma.cuh: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-VGPRpaged_attn_wmma(dec_q8_B1_S4873:94.5 → 80.4 us).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× fasterQ8_0paged prefill (1570.4 → 786.4 us).bench_conc.py,origin/mainvs full stack):q27_r9700 B=8long_batch(4.9kcached):160.0 → 188.1 tok/s(+17.6%),avg_dec: 2051 → 1757 ms(-14.4%).q27_halo B=4long_batch(3.0kcached): up to78.6 → 92.2 tok/s(+17.5%),avg_dec: 4099 → 3505 ms(-14.5%).