Skip to content

[Triton/Gluon] [GFX950] Productize gluon mha kernel from GFX950-gluon-tutorials - #5153

Merged
leonling-ll merged 2 commits into
ROCm:mainfrom
leonling-ll:liyang/gfx950_mha
Oct 29, 2026
Merged

leonling-ll merged 2 commits into
ROCm:mainfrom
leonling-ll:liyang/gfx950_mha

Conversation

@leonling-ll

@leonling-ll leonling-ll commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

This PR aims to productize the gluon fmha_v3 from gfx950-gluon-tutorials/kernels/attention/fmha_v3.py

The main changes include:

  • Double-buffer and Async_copy on K/V with padded shared layout.
  • Introduce rotated four-stage warp pipeline(_attn_fwd_pipelined) as the Fast Path. Keep and optimize the origin one-tile loop(_attn_fwd_inner) as a fall-back.
  • Adopt split head dims: a padded head dim (e.g. 96 → 64 + 32) runs as two power-of-two MFMA slices instead of one padded tile.
  • Mha config tuning: 256×64 with 8 waves by default, 128×64 with 4 waves for causal below 4096 keys and causal sliding windows
  • The performance change is listed in the table below
mode shape non-causal main non-causal latest Δ causal main causal latest Δ
MHA d=128 B2 S8192 H16/16 724.1 914.6 +26.3% 610.5 822.5 +34.7%
GQA d=128 B2 S8192 H16/2 736.1 940.5 +27.8% 634.4 847.8 +33.7%
MQA d=128 B2 S8192 H16/1 736.1 945.0 +28.4% 639.3 859.5 +34.4%
MHA d=64 B4 S4096 H16/16 563.3 720.7 +27.9% 481.8 595.2 +23.5%
padded d=40 B4 S4096 H16/1 298.0 491.5 +64.9% 218.2 411.4 +88.5%
padded d=96 B2 S4096 H16/16 532.9 823.5 +54.5% 388.9 691.5 +77.8%
PE d=128 dv=64 B2 S4096 H16/16 552.7 837.6 +51.5% 420.8 694.8 +65.1%
PE d=192 dv=128 B2 S4096 H16/16 648.0 831.3 +28.3% 509.2 685.9 +34.7%
SWA left=1024 d=128 B2 S8192 H16/16 635.9 864.0 +35.9% 455.7 545.3 +19.7%
sink d=128 B2 S8192 H16/16 724.1 919.3 +27.0% 608.4 823.4 +35.3%
varlen d=128 B2 S8192 H16/16 722.0 924.7 +28.1% 607.2 815.7 +34.3%
fp8 d=128 B2 S8192 H16/16 797.4 1357.5 +70.2% 669.0 1297.2 +93.9%

cc: @Dewei-Wang-sh

@github-actions

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:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
multigpu Aiter multi-GPU tests on the 8-GPU runner
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 5153 --add-label <label>

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to opt this PR out.

@leonling-ll
leonling-ll force-pushed the liyang/gfx950_mha branch 2 times, most recently from a0f2ec4 to 7a545a5 Compare September 1, 2026 16:26
@leonling-ll
leonling-ll marked this pull request as ready for review September 1, 2026 16:26
@leonling-ll
leonling-ll requested review from a team and a lite review from Copilot September 1, 2026 16:27
@github-actions github-actions Bot changed the title [Gluon] Add gluon mha for gfx950 [Triton/Gluon] Add gluon mha for gfx950 Sep 1, 2026

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

Adds a new gfx950-specific Gluon-based MHA forward kernel and wires it into the existing Triton MHA dispatch so eligible forward-only (no-grad) calls can run on the Gluon implementation for improved throughput.

Changes:

  • Route aiter/ops/triton/attention/mha.py forward to a new Gluon MHA kernel when gradients aren’t being built and unsupported features are absent.
  • Introduce a new Gluon kernel implementation for gfx950 dense MHA forward (mha_fwd.py) including host-side eligibility checks and launch code.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
aiter/ops/triton/attention/mha.py Adds a Gluon forward eligibility gate and dispatches forward-only calls to the gfx950 Gluon kernel.
aiter/ops/triton/_gluon_kernels/gfx950/attention/mha_fwd.py New gfx950 Gluon dense MHA forward kernel + host predicate/launcher.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread aiter/ops/triton/attention/mha.py Resolved
Comment thread aiter/ops/triton/attention/mha.py Resolved
Copilot AI review requested due to automatic review settings September 1, 2026 17:03

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 3 comments.

Suppressed comments (2)

Previously missed (1) — in code that hasn't changed since the last review.

aiter/ops/triton/_gluon_kernels/gfx950/attention/mha_fwd.py:614

  • This comment says dead rows write +inf to LSE, but the value actually written is DEAD_ROW_LSE (passed from the host, and currently set to 0.0 in attention/mha.py, matching the Triton MHA kernel’s behavior). Please update the comment to reflect the actual semantics so future call sites don’t rely on the wrong contract.

This issue also appears on line 1237 of the same file.

    if n_blocks <= 0:
        # Every row of this Q block is fully masked: write zeros to O and +inf to LSE.
        offs_m_z = start_m * BLOCK_M + gl.arange(0, BLOCK_M, layout=offs_m_layout)

aiter/ops/triton/_gluon_kernels/gfx950/attention/mha_fwd.py:1240

  • dead_row_lse defaults to -inf, but the main wrapper (attention/mha.py) passes 0.0 to match the existing Triton MHA behavior for fully-masked rows. Consider defaulting this to 0.0 (and documenting it) so direct callers of mha_fwd_gluon() don’t accidentally get behavior that diverges from the rest of the attention stack.
    layout: str = "bshd",
    out: torch.Tensor | None = None,
    dead_row_lse: float = float("-inf"),
) -> tuple[torch.Tensor, torch.Tensor]:

Comment thread aiter/ops/triton/attention/mha.py Resolved
Comment thread aiter/ops/triton/attention/mha.py Resolved
@zufayu
zufayu requested review from a team and Dewei-Wang-sh September 2, 2026 01:22
Copilot AI review requested due to automatic review settings September 2, 2026 02:00

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.

🔵 Needs a closer look

It introduces a large new architecture-specific GPU kernel and a new dispatch path that needs stronger robustness (import-guarding) and dedicated test coverage/hardware validation before approval.

Review details

Suppressed comments (1)

aiter/ops/triton/attention/mha.py:428

  • This PR adds a new launch path to a new Gluon MHA kernel (mha_fwd_gluon), but there are no accompanying test updates to exercise the new selection logic / kernel correctness. The existing MHA tests under op_tests/triton_tests/attention/ don’t reference the Gluon path, so regressions in _use_gluon_fwd gating or the Gluon kernel would go unnoticed.
        # The Gluon kernel is forward-only: it produces no residuals for this
        # module's backward, so it is taken only when no gradient is being built.
        # `is_grad` already answers that question exactly.
        if not is_grad and _use_gluon_fwd(
  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Lite

Comment thread aiter/ops/triton/attention/mha.py Resolved
Copilot AI review requested due to automatic review settings September 2, 2026 02:08

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.

🟡 Changes recommended

The fallback stub for is_mha_gluon_avail will raise at runtime when the Gluon import fails, and the new dispatch path needs targeted unit coverage to prevent silent regressions.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details

Suppressed comments (2)

aiter/ops/triton/attention/mha.py:447

  • A new forward-only Gluon execution path is added here, but the existing MHA tests do not exercise it (no tests reference is_mha_gluon_avail/mha_fwd_gluon). Please add a unit test that runs on gfx950 (skipping otherwise) to validate correctness for at least one causal and one non-causal shape and to ensure this branch is actually taken.
        # The Gluon kernel is forward-only: it produces no residuals for this
        # module's backward, so it is taken only when no gradient is being built.
        # `is_grad` already answers that question exactly.
        if not is_grad and _use_gluon_fwd(
            q,
            k,
            v,
            causal,
            dropout_p=dropout_p,
            window_size=window_size,
            bias=bias,
            alibi_slopes=alibi_slopes,
            sink=sink,
            config=config,
        ):
            out_padded, softmax_lse = mha_fwd_gluon(
                q, k, v, softmax_scale, causal, dead_row_lse=0.0
            )

aiter/ops/triton/_gluon_kernels/gfx950/attention/mha_fwd.py:614

  • This comment says the kernel writes "+inf" to LSE for fully-masked rows, but the code actually stores DEAD_ROW_LSE (a constexpr parameter, defaulting to -inf and passed as 0.0 from the wrapper). The comment should match the implemented behavior to avoid confusion when debugging numerics.
    if n_blocks <= 0:
        # Every row of this Q block is fully masked: write zeros to O and DEAD_ROW_LSE to LSE.
        offs_m_z = start_m * BLOCK_M + gl.arange(0, BLOCK_M, layout=offs_m_layout)
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Lite

Comment thread aiter/ops/triton/attention/mha.py Resolved
Copilot AI review requested due to automatic review settings September 2, 2026 02:14

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.

🔵 Needs a closer look

The Gluon import fallback currently introduces a runtime TypeError when the import fails (stub signature mismatch), and the new kernel path also lacks a dedicated unit test to validate selection/accuracy on gfx950.

Review details

Suppressed comments (2)

aiter/ops/triton/attention/mha.py:18

  • Fallback stub for is_mha_gluon_avail has an incompatible signature: _use_gluon_fwd calls it with positional args (q, k, v, causal), but the stub only accepts **kwargs, so if the import fails this will raise TypeError and break MHA forward. Also, the bare except will silently swallow real bugs in the gluon module; this should only fall back on ImportError/ModuleNotFoundError.
except:  # noqa: E722
    is_mha_gluon_avail = lambda **kwargs: False
    mha_fwd_gluon = None

aiter/ops/triton/attention/mha.py:437

  • This PR adds a new forward-only Gluon MHA fast-path, but there is no unit test that exercises the Gluon selection/accuracy on gfx950 (existing tests like op_tests/triton_tests/attention/test_mha.py cover the default path, but do not assert the gluon path is correct/selected). Per the Triton ops review rules, new kernels should ship with a matching unit test under op_tests/triton_tests/attention/.
        if not is_grad and _use_gluon_fwd(
            q,
            k,
            v,
            causal,
  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

Dewei-Wang-sh
Dewei-Wang-sh previously approved these changes Sep 2, 2026
@leonling-ll
leonling-ll marked this pull request as ready for review October 15, 2026 14:05
Copilot AI balanced review requested due to automatic review settings October 15, 2026 14:05
@github-actions github-actions Bot changed the title [Triton/Gluon][GFX950] Productize gluon mha kernel from GFX950-gluon-tutorials [Triton/Gluon] [GFX950] Productize gluon mha kernel from GFX950-gluon-tutorials Oct 15, 2026

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.

Comment thread aiter/ops/triton/attention/mha.py Resolved
@zufayu
zufayu requested review from a team and azaidy October 16, 2026 01:11
@azaidy azaidy assigned lburzawa and lucas-santos-amd and unassigned azaidy Oct 19, 2026
Copilot AI balanced review requested due to automatic review settings October 22, 2026 15:18

This comment was marked as duplicate.

@lburzawa

Copy link
Copy Markdown
Contributor

The new kernel is pretty large and a bit hard to read. Is it possible to try to simplify somewhat, maybe even split into multiple kernels? For example it seems that _attn_fwd_inner and _attn_fwd_pipelined are quite different kernel strategies that could be split into different kernels with some common utils/functions and chosen by the python wrapper instead of doing it inside the kernel itself. Just an idea, other ways to more it less dense/more readable would work too.

leonling-ll and others added 2 commits October 29, 2026 01:23
Rework the gfx950 Gluon MHA forward kernel (_attn_fwd):

- K/V (and the PE slice) are staged global->LDS by async copy into
  row-staggered padded shared layouts, double buffered, so no tile passes
  through VGPRs.  The host checks the copy's alignment needs
  (kv_async_copy_ok) and copies K/V into contiguous buffers when a view
  fails them.
- Full key blocks run a rotated four-cluster warp pipeline
  (_attn_fwd_pipelined): the two waves of a SIMD run one cluster apart, so
  the softmax issues in the shadow of the other wave's MFMAs.  Masked,
  windowed and short ranges keep the one-tile loop (_attn_fwd_inner), and a
  sliding window's interior blocks take the pipeline too.
- A padded head dim that splits into power-of-two slices (96 -> 64 + 32,
  ...) is issued as two MFMA chains instead of one padded tile
  (_head_slices).
- fp8 scales P statically onto e4m3's range instead of quantizing it with a
  per-tile amax, and applies descale_v once in the epilogue.
- bf16 folds the softmax scale into Q once (SCALE_ON_Q); fp16 does not,
  since q * scale can overflow it.  The host requires sm_scale >= 0, and a
  zero fp8 scale is clamped so masked scores stay -inf.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Pick the tile per shape instead of one per dtype (BLOCK_M = 32 * num_warps):

- bf16/fp16 default: 256 x 64 with 8 waves.  Two waves per SIMD let the
  pipeline overlap the softmax with MFMA.
- Causal below causal_wide_min_seqlen_k (4096) keys, and a causal sliding
  window: 128 x 64 with 4 waves, which wastes less on the masked diagonal.
- V head dim > 128: 128 x 32; the default tile spills.
- PE: 256 x 64 / 8 waves for a V head <= 64, else 128 x 32.
- fp8 with a 64-wide head: 128 x 128, to amortise the softmax.

The tiles and the causal cutoff live in mha.json.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 29, 2026 01:24

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.

🟡 Changes recommended

Pre-scaling Q in BF16 can alter attention weights through rounding or overflow even when FP32 scores remain finite, and regression tests for these cases are missing.

7 open issues
1 resolved since last review

Review effort: Balanced

Comment thread aiter/ops/triton/attention/mha.py Resolved
Comment thread aiter/ops/triton/attention/mha.py Resolved
Comment thread aiter/ops/triton/attention/mha.py Resolved
@leonling-ll

Copy link
Copy Markdown
Contributor Author

The new kernel is pretty large and a bit hard to read. Is it possible to try to simplify somewhat, maybe even split into multiple kernels? For example it seems that _attn_fwd_inner and _attn_fwd_pipelined are quite different kernel strategies that could be split into different kernels with some common utils/functions and chosen by the python wrapper instead of doing it inside the kernel itself. Just an idea, other ways to more it less dense/more readable would work too.

Hi @lburzawa , thanks for your comment.

I have refactored the kernel file to reduce the line number from 3077 to 1997. Which include 1) merge fp8 kernel function into _attn_fwd to share more common func; 2) leverage @aggregate to reduce duplicated params calling; 3) restructure the code with proper comment to improve the readability.

_attn_fwd_pipelined is the key changes of this production, it include rotated four-stage warp pipeline which is introduced in ROCm/gfx950-gluon-tutorials - fmha_v3.py.
_attn_fwd_inner is a optimized fall-back if _attn_fwd_pipelined does not apply.
They both contribute to the K-loop part inside the kernel. With above lines reducing, they are much shorter than before, so I did not split them to separated kernels or files. Please help take a look.

@leonling-ll
leonling-ll merged commit 3628e56 into ROCm:main Oct 29, 2026
70 checks passed
@leonling-ll
leonling-ll deleted the liyang/gfx950_mha branch October 29, 2026 07:18
solinzby1 pushed a commit that referenced this pull request Nov 2, 2026
…-tutorials (#5153)

This PR aims to productize the gluon fmha_v3 from [gfx950-gluon-tutorials/kernels/attention/fmha_v3.py](https://github.com/ROCm/gfx950-gluon-tutorials/blob/main/kernels/attention/fmha_v3.py)

The main changes include:
- Double-buffer and Async_copy on K/V with padded shared layout.
- Introduce rotated four-stage warp pipeline(_attn_fwd_pipelined) as the Fast Path. Keep and optimize the origin one-tile loop(_attn_fwd_inner) as a fall-back. 
- Adopt split head dims: a padded head dim (e.g. 96 → 64 + 32) runs as two power-of-two MFMA slices instead of one padded tile.
- Mha config tuning: 256×64 with 8 waves by default, 128×64 with 4 waves for causal below 4096 keys and causal sliding windows
shantipriya-amd pushed a commit to shantipriya-amd/aiter that referenced this pull request Nov 18, 2026
…-tutorials (ROCm#5153)

This PR aims to productize the gluon fmha_v3 from [gfx950-gluon-tutorials/kernels/attention/fmha_v3.py](https://github.com/ROCm/gfx950-gluon-tutorials/blob/main/kernels/attention/fmha_v3.py)

The main changes include:
- Double-buffer and Async_copy on K/V with padded shared layout.
- Introduce rotated four-stage warp pipeline(_attn_fwd_pipelined) as the Fast Path. Keep and optimize the origin one-tile loop(_attn_fwd_inner) as a fall-back. 
- Adopt split head dims: a padded head dim (e.g. 96 → 64 + 32) runs as two power-of-two MFMA slices instead of one padded tile.
- Mha config tuning: 256×64 with 8 waves by default, 128×64 with 4 waves for causal below 4096 keys and causal sliding windows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants