Repository navigation
[triton] Optimized Unified Attention for Gemma-4-31b - #4044
Conversation
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
|
44efba5 to
ab0f4b3
Compare
There was a problem hiding this comment.
Pull request overview
Retunes Triton unified-attention config selection in AITER to better handle Gemma-4’s larger head sizes (256/512) on CDNA (gfx950), addressing performance regressions and avoiding LDS-related decode failures without changing kernel math.
Changes:
- Adjust
select_2d_configprefill and decode heuristics forhead_size >= 256/512on non-RDNA targets (warps/stages/tile sizing). - Adjust
select_3d_configsplit-KV heuristics forhead_size >= 512(warps/stages and increased minimum segment count).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
op_tests/triton_tests/attention/test_unified_attention.py:580
- This parametrization doubles the already-large cartesian product of cases (head_size list grows from 2→4 values), which can make this test suite significantly slower and more resource-intensive. A straightforward way to keep coverage for 256/512 while limiting runtime is to couple (head_size, dtype combo) into a single parametrization so large head sizes run BF16-only, while smaller head sizes keep the broader dtype coverage.
@pytest.mark.parametrize("head_size", [64, 128, 256, 512])
@pytest.mark.parametrize("block_size", [16, 64])
@pytest.mark.parametrize("sliding_window", [None, 256])
@pytest.mark.parametrize(
"soft_cap",
[None, 50.0],
)
@pytest.mark.parametrize(
"num_blocks",
[2048, 32768],
)
@pytest.mark.parametrize(
"q_dtype, kv_dtype, out_dtype, use_q_descale, use_kv_descale, use_out_scale",
[
(torch.bfloat16, torch.bfloat16, torch.bfloat16, False, False, False),
(torch.bfloat16, e4m3_dtype, torch.bfloat16, False, True, False),
(e4m3_dtype, e4m3_dtype, torch.bfloat16, True, True, False),
(torch.float16, torch.float16, torch.float16, False, False, False),
],
)
|
@ROCm/team_aiter may I ask you to take a look at these changes please? Thank you in advance |
|
@azaidy @carlushuang @valarLip could you please take a look? |
|
Thanks for the contribution! We'll take a look but likely won't be able to merge until after the 22nd. |
a-sidorova
left a comment
There was a problem hiding this comment.
@cagrikymk Thank you for the review!
@jayfurmanek got it, thank you for the notification. Looking forward to merge it :)
3cab9b8 to
d45d0aa
Compare
|
There are two failed jobs. I believe that this is sporadic since the tests (gemm and PA) are not related to the changes in this PR. |
I triggered the CIs after merging main. I think the PR is in a good shape now and it is not affecting any existing heuristics, as far as I can see. We can merge it after 22nd. |
|
@k50112113 take a look this one? |
|
@cagrikymk hola! Let me know please if we can merge this PR now |
39b94ca to
36742b1
Compare
|
please make sure all ci test pass.... |
36742b1 to
17e6782
Compare
The upstream refactor (ROCm#5088) replaced select_2d_config with per-arch JSON config tables, but the D-axis GEQ entries (D_GEQ_512, D_GEQ_256) shadow the Q_GEQ_256 entry (BLOCK_M=128) for models with head_size >= 256 — so large prefills still run at BLOCK_M=16 (e.g. Gemma-4, head_dim 256/512). Add head_size-aware composite keys so large prefill gets BLOCK_M=128 while keeping the head-size-specific num_warps/num_stages/TILE_SIZE from ROCm#4044: D_GEQ_512.Q_GEQ_256: BLOCK_M=128, warps=4, stages=2, TILE=16 D_GEQ_256.Q_GEQ_256: BLOCK_M=128, warps=2, stages=2, TILE=32 The launch wrapper already raises BLOCK_M to next_pow2(nqpkv) after the lookup, so these entries are safe for any GQA ratio (Copilot's note on the previous Python-based fix is moot in table form). Measured on gfx942 (MI325X) with Gemma-4-31B FP8-block, 32k in / 1k out: 5.9% e2e latency improvement (17.13s -> 16.12s warm avg), 7.8% mean ITL improvement (16.8ms -> 15.5ms/tok). Related: ROCm#4044 (the original Python-tier fix this replaces), ROCm#5027 (fused QK-norm RoPE with head_dim 512 + weightless V-norm, complementary Gemma-4 path). Signed-off-by: Mustafa Yildirim <[email protected]>
Motivation
On AMD CDNA4 (MI355X / gfx950), the vLLM Triton unified-attention backend was
significantly faster than AITER's
ROCM_AITER_UNIFIED_ATTNforgoogle/gemma-4-31B-it,and AITER's full-attention decode crashed outright with
OutOfResources. Gemma-4interleaves two large-head attention layers that fall outside the range the AITER configs
were tuned for (
head_dim ≤ 128):head_dimblock_sizenqpkv=8)nqpkv=2)This PR retunes
select_2d_config/select_3d_configfor these shapes. Changes areconfig-level only (
BLOCK_M,TILE_SIZE,num_warps,num_stages,MIN_SEGMENTS) —no compute-kernel logic is touched, so outputs stay bit-comparable. Result: AITER goes
from 2.3–5.6× slower (and crashing on decode due to reaching LDS limits) to parity-or-faster than the Triton
reference on every prefill/decode case, with full-attention decode fixed.
Technical Details
aiter/ops/triton/attention/unified_attention.pyselect_2d_config— head-size-aware prefill/mixed config. Replaces thesingle
num_stages=1, num_warps=2default with tiered configs:head_size >= 512:TILE_SIZE=16,num_stages=2,num_warps=4,BLOCK_M=max(16, next_pow2(nqpkv)). The narrow 16-wide KV tile fits LDS soit can double-buffer (hiding load latency) and keeps per-CU LDS/register
pressure low, raising occupancy so many memory-bound rows run concurrently
instead of a few stragglers. Turns the 5.3x mixed regression into a win and
is also faster on pure prefill.
head_size >= 256:TILE_SIZE=32,num_stages=2,num_warps=2(tunes thesliding path).
BLOCK_M=128, stages=1, warps=4formax_seqlen_q>=256, elsestages=1, warps=2).select_2d_config— decode config for large heads. Forhead_size >= 512pure-decode routed to 2D, usenum_stages=1, num_warps=4instead of the default
3, 2(deep pipelining over-pressures registers/LDS atthis head size).
select_3d_config— large-head decode tuning. Addsarch = get_arch();for
head_size >= 512(non-RDNA):attn_warps=4, attn_stages=1, and raiseMIN_SEGMENTStomin(16, MAX_SEGMENTS)(then round to a power of two). Thisgives the split-KV decode path enough KV parallelism/occupancy for the
512-wide head.
use_2d_kernel— route large-head prefill/mixed to 2D. Adds an earlyreturn Trueforhead_size >= 512 and not RDNA and not all_decode, sofull-attention prefill/mixed steps always use the (now well-tuned) 2D kernel
instead of the decode-oriented 3D path. Pure decode still uses the existing
heuristic (3D by default; 2D for short context or high program counts).
op_tests/triton_tests/attention/test_unified_attention.pyhead_size ∈ {64,128}to{64,128,256,512}, covering the newly-tunedlarge-head paths.
Test Plan
op_tests/triton_tests/attention/test_unified_attention.pyTest Result
Added tests for
head_dim256 and 512Benchmark Result
Validated on model
google/gemma-4-31B-itusing vLLM v0.22, table below contains total token throughputs (t/s)OutOfResourcesOutOfResourcesOutOfResourcesOutOfResourcesOutOfResourcesThis optimization allows user to use AITER's Unified attention kernel for Gemma-4-31b model and increases total token throughput in x1.05-x1.17 times in comparison with using triton unified attention from vLLM.
Submission Checklist