Skip to content

[triton] Optimized Unified Attention for Gemma-4-31b - #4044

Merged
cagrikymk merged 5 commits into
ROCm:mainfrom
a-sidorova:asidorov/feature/unified_attn_d512
Jul 30, 2026
Merged

cagrikymk merged 5 commits into
ROCm:mainfrom
a-sidorova:asidorov/feature/unified_attn_d512

Conversation

@a-sidorova

@a-sidorova a-sidorova commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

On AMD CDNA4 (MI355X / gfx950), the vLLM Triton unified-attention backend was
significantly faster than AITER's ROCM_AITER_UNIFIED_ATTN for google/gemma-4-31B-it,
and AITER's full-attention decode crashed outright with OutOfResources. Gemma-4
interleaves two large-head attention layers that fall outside the range the AITER configs
were tuned for (head_dim ≤ 128):

Layer head_dim GQA (q/kv) block_size Window
full (global) 512 32 / 4 (nqpkv=8) 64 global
sliding 256 32 / 16 (nqpkv=2) 32 1024

This PR retunes select_2d_config / select_3d_config for these shapes. Changes are
config-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.py

  1. select_2d_config — head-size-aware prefill/mixed config. Replaces the
    single num_stages=1, num_warps=2 default 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 so
      it 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 the
      sliding path).
    • otherwise: unchanged (BLOCK_M=128, stages=1, warps=4 for
      max_seqlen_q>=256, else stages=1, warps=2).
  2. select_2d_config — decode config for large heads. For
    head_size >= 512 pure-decode routed to 2D, use num_stages=1, num_warps=4
    instead of the default 3, 2 (deep pipelining over-pressures registers/LDS at
    this head size).

  3. select_3d_config — large-head decode tuning. Adds arch = get_arch();
    for head_size >= 512 (non-RDNA): attn_warps=4, attn_stages=1, and raise
    MIN_SEGMENTS to min(16, MAX_SEGMENTS) (then round to a power of two). This
    gives the split-KV decode path enough KV parallelism/occupancy for the
    512-wide head.

  4. use_2d_kernel — route large-head prefill/mixed to 2D. Adds an early
    return True for head_size >= 512 and not RDNA and not all_decode, so
    full-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.py

  1. Test coverage. Extends the 3D-attention parametrization from
    head_size ∈ {64,128} to {64,128,256,512}, covering the newly-tuned
    large-head paths.

Test Plan

  • op_tests/triton_tests/attention/test_unified_attention.py

Test Result

Added tests for head_dim 256 and 512

Benchmark Result

Validated on model google/gemma-4-31B-it using vLLM v0.22, table below contains total token throughputs (t/s)

Configuration TRITON_ATTN AITER_UNIFIED_ATTN main branch AITER_UNIFIED_ATTN this branch
tp=1, 1k->1k, conc=8 929.61 OutOfResources 1071.4
tp=1, 1k->1k, conc=16 1596.86 OutOfResources 1750.79
tp=1, 8k->1k, conc=16 4358.84 OutOfResources 5315.78
tp=4, 8k->1k, conc=16 9005.72 OutOfResources 9416.41
tp=4, 8k->1k, conc=32 13219.82 OutOfResources 14201.55

This 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.

SAFETENSORS_FAST_GPU=1 \
HIP_FORCE_DEV_KERNARG=1 \
TORCH_BLAS_PREFER_HIPBLASLT=1 \
NCCL_MIN_NCHANNELS=112 \
VLLM_ROCM_USE_AITER_RMSNORM=0 \
VLLM_ROCM_USE_AITER=1 \
VLLM_ROCM_USE_AITER_UNIFIED_ATTENTION=1 \
vllm serve google/gemma-4-31B-it \
  --dtype bfloat16 \
  --tensor-parallel-size X \
  --max-model-len 32768 \
  --max-num-seqs 2048 \
  --gpu-memory-utilization 0.90 \
  --block-size 32 \
  --enable-auto-tool-choice \
  --tool-call-parser gemma4 \
  --port 1616 \
  --host 0.0.0.0 \
  --no-enable-prefix-caching \
  --attention-backend ROCM_AITER_UNIFIED_ATTN

vllm bench serve \
  --backend openai-chat \
  --model google/gemma-4-31B-it \
  --base-url http://0.0.0.0:1616 \
  --endpoint /v1/chat/completions \
  --dataset-name random \
  --random-input-len X \
  --random-output-len 1024 \
  --num-prompts X * 10 \
  --max-concurrency X

Submission Checklist

@a-sidorova
a-sidorova requested review from a team and Copilot July 1, 2026 14:22
@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 4044 --add-label <label>

@a-sidorova
a-sidorova force-pushed the asidorov/feature/unified_attn_d512 branch from 44efba5 to ab0f4b3 Compare July 1, 2026 14:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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_config prefill and decode heuristics for head_size >= 256/512 on non-RDNA targets (warps/stages/tile sizing).
  • Adjust select_3d_config split-KV heuristics for head_size >= 512 (warps/stages and increased minimum segment count).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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),
    ],
)

@a-sidorova
a-sidorova marked this pull request as draft July 2, 2026 10:08
@a-sidorova
a-sidorova marked this pull request as ready for review July 2, 2026 12:05
@a-sidorova

Copy link
Copy Markdown
Contributor Author

@ROCm/team_aiter may I ask you to take a look at these changes please? Thank you in advance

@andrei-kochin
andrei-kochin requested a review from k50112113 July 6, 2026 12:20
@wenjiwan
wenjiwan requested a review from vgokhale July 8, 2026 06:22
@andrei-kochin

Copy link
Copy Markdown
Contributor

@azaidy @carlushuang @valarLip could you please take a look?

@cagrikymk
cagrikymk self-requested a review July 12, 2026 19:06
@jayfurmanek

Copy link
Copy Markdown
Contributor

Thanks for the contribution! We'll take a look but likely won't be able to merge until after the 22nd.

@a-sidorova a-sidorova 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.

@cagrikymk Thank you for the review!

@jayfurmanek got it, thank you for the notification. Looking forward to merge it :)

@a-sidorova
a-sidorova requested a review from cagrikymk July 14, 2026 06:35
@a-sidorova
a-sidorova force-pushed the asidorov/feature/unified_attn_d512 branch from 3cab9b8 to d45d0aa Compare July 14, 2026 06:56
@a-sidorova

Copy link
Copy Markdown
Contributor Author

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.
However, I don't have permissions to relaunch tests

@cagrikymk

Copy link
Copy Markdown
Contributor

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. However, I don't have permissions to relaunch tests

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.

@valarLip

Copy link
Copy Markdown
Collaborator

@k50112113 take a look this one?

@zufayu
zufayu removed the request for review from valarLip July 21, 2026 02:40
@a-sidorova

Copy link
Copy Markdown
Contributor Author

@cagrikymk hola! Let me know please if we can merge this PR now

@a-sidorova
a-sidorova force-pushed the asidorov/feature/unified_attn_d512 branch from 39b94ca to 36742b1 Compare July 24, 2026 07:27
@a-sidorova
a-sidorova requested a review from cagrikymk July 27, 2026 06:43
@valarLip

Copy link
Copy Markdown
Collaborator

please make sure all ci test pass....

@a-sidorova
a-sidorova force-pushed the asidorov/feature/unified_attn_d512 branch from 36742b1 to 17e6782 Compare July 30, 2026 05:52
@cagrikymk
cagrikymk merged commit aa6ee61 into ROCm:main Jul 30, 2026
56 checks passed
mustafayildirim added a commit to mustafayildirim/aiter that referenced this pull request Sep 8, 2026
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]>
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.

6 participants