llama.cpp icon indicating copy to clipboard operation
llama.cpp copied to clipboard

Misc. bug: Perf: ROCm/RDNA4 (gfx1201) FlashAttention prefill with head size 512 (Gemma 4 global layers) is ~4x less efficient than head 256

Open DanoPTT opened this issue 1 month ago • 3 comments

Name and Version

build 11572 (097cf33d9), Windows, ROCm/HIP 10.0.0, built with -DGGML_HIP=ON -DGGML_HIP_ROCWMMA_FATTN=ON -DAMDGPU_TARGETS=gfx1201 (same flags as the windows-rocm release job, single target)

Operating systems

Windows

Which llama.cpp modules do you know to be affected?

llama-server

Command line


Problem description & steps to reproduce

Long-context prefill on Gemma 4 31B drops off much faster than on other dense models of the same size on this card. The cause is the FLASH_ATTN_EXT op for the 10 global layers with head size 512.

On gfx1201, ggml_cuda_get_best_fattn_kernel sends head sizes up to 256 to the WMMA (MMA_F16) path. That's the amd_wmma_available(cc) && ... && Q->ne[0] <= 256 condition in fattn.cu. Head 512 therefore falls back to the tile kernel, which reaches only about a quarter of the throughput that WMMA gets for head 256.

llama-bench (-ngl 999 -fa on -ctk q8_0 -ctv q8_0 -ub 512 -b 1024 -p 512 -n 0 -d 0,32768,98304 -r 2):

depth pp512 t/s ms / ubatch
0 990.7 517
32768 333.0 1537
98304 140.7 3639

Isolated FLASH_ATTN_EXT op with the exact prefill shapes (ubatch 512, F32 precision, mask). K/V are strided q8_0 views of a unified KV cache, as llama.cpp creates them. Run with test-backend-ops perf -b ROCm0 --test-file ops.txt; the file is below.

shape time / op effective TFLOPS
D=512, 32 Q / 4 KV heads, kv 32768, q8_0 86.5 ms 12.7
D=512, 32 Q / 4 KV heads, kv 98304, q8_0 276.6 ms 11.9
D=512, 32 Q / 4 KV heads, kv 32768, f16 91.2 ms 12.1
D=512, 32 Q / 4 KV heads, kv 98304, f16 281.8 ms 11.7
D=256, 24 Q / 4 KV heads, kv 32768, q8_0 (Qwen3.x 27B full-attn layer, for reference) 9.1 ms 45.2
D=256, 24 Q / 4 KV heads, kv 98304, q8_0 (same) 27.4 ms 45.1
D=256, 32 Q / 16 KV heads, kv 1536, q8_0 (Gemma 4 SWA layer) 0.60 ms 42.8

TFLOPS are counted as 4 · n_q · n_kv · n_head · D, without discounting the causal mask, so the absolute numbers are generous. The ratio between rows is what matters.

At depth 98304, the 10 global layers take 10 × 276.6 ms ≈ 2.77 s, which is 76 % of the 3.64 s ubatch. The 50 SWA layers together take ~30 ms. KV type has no effect, since f16 is as slow as q8_0.

What I tried

I also ran a fork that removes the <= 256 limit and sends head 512 through the WMMA MMA_F16 path on RDNA4, using the existing RDNA 512/512 config rows. On the same shapes it was not faster: 310.6 ms at kv 98304 against 276.6 ms for tile here, and 103.5 ms against 86.5 ms at kv 32768. So simply enabling MMA for D=512 with the current config rows doesn't help. The tile kernel is the better of the two existing options, but it is still about 4× below what D=256 reaches.

Hardware / model

AMD Radeon AI PRO R9700 (gfx1201, 32 GB, wave 32), Ryzen 5 PRO 4650G. Model: unsloth gemma-4-31B-it-UD-Q5_K_XL.gguf. It has 60 layers: 10 global layers (D=512, 32 Q / 4 KV heads) and 50 SWA layers (D=256, window 1024).

ggml_cuda_init: found 1 ROCm devices (Total VRAM: 32624 MiB):
  Device 0: AMD Radeon AI PRO R9700, gfx1201 (0x1201), VMM: no, Wave Size: 32, VRAM: 32624 MiB

Question / request

Is there a known reason why D=512 can't reach D=256 efficiency on RDNA4, such as LDS or VGPR pressure? Or could the MMA path be made to work for D=512 there, for example by splitting the VKQ accumulator over D or with dedicated 512/512 RDNA4 config rows? Gemma 4 (31B dense and 26B-A4B) uses D=512 for all global layers, so this caps long-context prefill for those models on RDNA4.

I can test patches or run additional perf cases on the R9700.

First Bad Commit

n/a. I don't think this is a regression; I haven't bisected older builds.

Relevant log output

ops.txt for test-backend-ops perf --test-file
74 0 512 32 512 1 5 1065353216 0 0 10 0 4 0 512 512 32 1 4 65536 2048 33554432 8 512 32768 4 1 34 2176 544 71303168 8 512 32768 4 1 34 2176 544 71303168 1 32768 512 1 1 2 65536 33554432 33554432 gemma_full_kv32768
74 0 256 24 512 1 5 1031798784 0 0 10 0 4 0 256 512 24 1 4 24576 1024 12582912 8 256 32768 4 1 34 1088 272 35651584 8 256 32768 4 1 34 1088 272 35651584 1 32768 512 1 1 2 65536 33554432 33554432 v2_full_kv32768
74 0 512 32 512 1 5 1065353216 0 0 10 0 4 0 512 512 32 1 4 65536 2048 33554432 1 512 32768 4 1 2 4096 1024 134217728 1 512 32768 4 1 2 4096 1024 134217728 1 32768 512 1 1 2 65536 33554432 33554432 gemma_full_f16_kv32768
74 0 512 32 512 1 5 1065353216 0 0 10 0 4 0 512 512 32 1 4 65536 2048 33554432 8 512 98304 4 1 34 2176 544 213909504 8 512 98304 4 1 34 2176 544 213909504 1 98304 512 1 1 2 196608 100663296 100663296 gemma_full_kv98304
74 0 256 24 512 1 5 1031798784 0 0 10 0 4 0 256 512 24 1 4 24576 1024 12582912 8 256 98304 4 1 34 1088 272 106954752 8 256 98304 4 1 34 1088 272 106954752 1 98304 512 1 1 2 196608 100663296 100663296 v2_full_kv98304
74 0 512 32 512 1 5 1065353216 0 0 10 0 4 0 512 512 32 1 4 65536 2048 33554432 1 512 98304 4 1 2 4096 1024 402653184 1 512 98304 4 1 2 4096 1024 402653184 1 98304 512 1 1 2 196608 100663296 100663296 gemma_full_f16_kv98304
74 0 256 32 512 1 5 1065353216 0 0 10 0 4 0 256 512 32 1 4 32768 1024 16777216 8 256 1536 16 1 34 4352 272 6684672 8 256 1536 16 1 34 4352 272 6684672 1 1536 512 1 1 2 3072 1572864 1572864 gemma_swa_kv1536
test-backend-ops output
FLASH_ATTN_EXT(name=gemma_full_kv32768,...):       142 runs -  86490.30 us/run
FLASH_ATTN_EXT(name=v2_full_kv32768,...):          265 runs -   9113.17 us/run
FLASH_ATTN_EXT(name=gemma_full_f16_kv32768,...):    94 runs -  91206.17 us/run
FLASH_ATTN_EXT(name=gemma_full_kv98304,...):        58 runs - 276632.55 us/run
FLASH_ATTN_EXT(name=v2_full_kv98304,...):          102 runs -  27438.13 us/run
FLASH_ATTN_EXT(name=gemma_full_f16_kv98304,...):    36 runs - 281815.78 us/run
FLASH_ATTN_EXT(name=gemma_swa_kv1536,...):        2127 runs -    602.33 us/run

DanoPTT avatar Oct 19 '26 21:10 DanoPTT

I reproduced the head-width performance gap on an RX 9060 XT (gfx1200) using a Linux HIP build of llama.cpp at 0a7675746. I ran an isolated FLASH_ATTN_EXT benchmark with Q8_0 K/V, 32 query heads, 4 KV heads, 512 query tokens, and 32,768 cached positions. Both cases used the same attention scale.

Run order D=256 D=512 D=512 / D=256 elapsed time
D=512 first 34.252 ms 149.452 ms 4.36×
D=256 first 34.225 ms 150.607 ms 4.40×

With head counts held fixed, doubling head width took 4.36–4.40× as long for 2× the nominal attention work. In separate GDB runs with the same binary and inputs, I confirmed both host dispatch paths: D=256 called ggml_cuda_flash_attn_ext_mma_f16_case<256, 256, 8, 8> and D=512 called ggml_cuda_flash_attn_ext_tile.

fox5611 avatar Nov 21 '26 02:11 fox5611

On a diagnostic build based on 0a7675746, I also forced D=256 through TILE for the same Q8_0 input. In forward/reverse order, D=256 stock MMA took 33.82/33.89 ms, D=256 forced TILE took 73.81/74.78 ms, and D=512 stock TILE took 152.65/152.57 ms. With both widths on TILE, doubling the head width took 2.04–2.07× as long. At fixed D=256, changing from MMA to TILE took 2.18–2.21× as long.

fox5611 avatar Nov 22 '26 06:11 fox5611

I tested @stew675’s D=512 MMA candidate on an RX 9060 XT (gfx1200).

8:1 performance

On the isolated FLASH_ATTN_EXT workload with D=512, Q8_0 K/V, 32 query heads, 4 KV heads, 512 query tokens, and 32,768 cached positions, stock TILE took 152.86–153.08 ms per run. The D=512 MMA candidate took 183.28–188.22 ms per run. Across four pairs with alternating run order, the candidate took longer in every pair, with a 20.54% median paired increase in elapsed time.

The 4:1 workaround was inactive for this benchmark. Whole-model prefill remains unmeasured.

4:1 correctness

The original candidate passed 30/38 cases in the seeded D=512 sweep. With the 4:1 cases routed through a two-wide grouping, the candidate passed 38/38 in each of three seeded runs and 38/38 in each of three runs with fresh random inputs. The attention ratio stayed at 4:1 in those cases.

The full seeded FLASH_ATTN_EXT suite passed 4,372/4,372 with the workaround enabled. The cause of the original failures remains unknown.

Q8_0

I also tested a narrow change to the D=512 MMA tile loader on my RX 9060 XT. For the 132, 4, 32 loader specialization, I wrote out the six loads in their original order. On the isolated 8:1 workload, this reduced median paired elapsed time by 3.32% with F16 K/V (4/4 pairs faster) and 2.93% with Q8_0 K/V (6/6 rounds faster).

For Q8_0, I timed stock TILE, pre-edit MMA, and edited MMA in the same session using all six run orders. Stock TILE took 152.71–153.33 ms, and edited MMA took 178.34–179.54 ms. Edited MMA still took 17.09% longer than stock by median paired elapsed time. These are operator timings; whole-model prefill remains unmeasured.

fox5611 avatar Nov 22 '26 23:11 fox5611