Skip to content

[Triton/Gluon] fused_indexed_gated_residual: make column blocks the fastest grid index - #8924

Open
Shan2L wants to merge 1 commit into
ROCm:mainfrom
Shan2L:triton/gated-residual-column-block-grid
Open

Shan2L wants to merge 1 commit into
ROCm:mainfrom
Shan2L:triton/gated-residual-column-block-grid

Conversation

@Shan2L

@Shan2L Shan2L commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

fused_indexed_gated_residual (Triton, merged in #6416) now launches a 1-D grid in which the column block varies fastest. Before, row blocks varied fastest. On gfx1201 this raises the kernel from 138–575 GB/s to 584–597 GB/s at M = 16384, depending on the row width. Outputs are bit-identical, and the kernel's math, tiles and config are unchanged.

Motivation

Found while comparing this kernel with the HIP version of the op in the same op test (#6417, review comment #6417 (comment)). The Triton kernel was much slower than HIP at some row widths, for example 5.8 ms against 1.3 ms at 16384 x 8192.

The grid was (cdiv(M, BLOCK_M), cdiv(N, BLOCK_N)) with the row block in program_id(0). Programs that run together therefore covered one 256-column stripe of 16 x (number of resident programs) rows, instead of walking along rows. On gfx1201 this costs bandwidth at every row width and collapses it when the row pitch is a large power of two (N = 8192: 138 GB/s; N = 4096: 275 GB/s).

Swapping the grid order is the whole fix. In the same comparison, a variant with the same kernel body and only the grid axes swapped ran at 572–594 GB/s on every case, while row-wise tiles (1 x 1024) and narrower row blocks (4 x 512) recovered only part of it. The ISA confirms that the slow N = 8192 case is not an instruction-width problem: its loads and stores are 128-bit, exactly as at N = 5376.

Changes

  • _fused_indexed_gated_residual_kernel: derive the row block and column block from a 1-D program_id(0), column block fastest.
  • fused_indexed_gated_residual: launch cdiv(M, BLOCK_M) * cdiv(N, BLOCK_N) programs on one grid axis. A 1-D grid also stays clear of the 65535 limit on grid axes 1 and 2.

Performance (if applicable)

Radeon AI PRO R9700 (gfx1201), bf16, G = 9, run-of-index indices, round_intermediate=True. Median of 5 rounds of 30 calls (torch.cuda.Event), mean of two processes each for before and after, run in the order before, after, after, before. Effective bandwidth counts 3 x M x N x 2 bytes.

Hardware Workload/configuration Before After Change
R9700 (gfx1201) M = 16384, N = 5376 (MiniMax-H3 hidden) 919.8 us 888.1 us 0.966x time (575 -> 595 GB/s)
R9700 (gfx1201) M = 113102, N = 5376 6270.2 us 6060.4 us 0.967x
R9700 (gfx1201) M = 16384, N = 5376, rows padded to N+8 1136.6 us 905.1 us 0.796x (465 -> 584 GB/s)
R9700 (gfx1201) M = 16384, N = 5376, rows padded to N+16 1032.7 us 900.5 us 0.872x
R9700 (gfx1201) M = 16384, N = 8192 5831.0 us 1350.8 us 0.232x (138 -> 596 GB/s)
R9700 (gfx1201) M = 16384, N = 8192, rows padded to N+16 1526.7 us 1368.4 us 0.896x
R9700 (gfx1201) M = 16384, N = 4096 1461.8 us 679.8 us 0.465x
R9700 (gfx1201) M = 16384, N = 3072 934.8 us 505.8 us 0.541x
R9700 (gfx1201) M = 16384, N = 1536 470.4 us 255.9 us 0.544x
R9700 (gfx1201) M = 16384, N = 320 30.4 us 31.1 us 1.025x (cache-resident)
R9700 (gfx1201) M = 1024, N = 1536 to 8192 31.0–54.2 us 30.9–33.0 us 0.61x–1.00x

27 cases were measured at M = 1024, 16384 and 113102. The only regression is the cache-resident M = 16384, N = 320 case (+2.5%, 0.7 us); at M = 1024 and M = 113102 the same N is 0.99x and 0.95x.

Testing

  • Unit tests added or updated
  • Performance benchmarks run
  • Tested on MI250X
  • Tested on MI300X

No test is added: the existing op_tests/triton_tests/fusions/test_fused_indexed_gated_residual.py already covers tails along both M and N, single rows and narrow N, and strided rows. It passes unchanged (34 passed on gfx1201). Every timed case above was also checked bit-identical to the eager expression before and after the change.

Documentation

  • Docstrings updated
  • User guide updated
  • Performance guide updated
  • No documentation change is needed, with the reason stated below

The interface and results are unchanged.

Dependencies

  • No new third-party dependencies added
  • New dependencies are listed and justified below

Breaking Changes

None.

AI assistance: this change was developed with the help of an AI coding assistant (GitHub Copilot). All tests and timings above were run on the listed hardware.

PR Self-Review

Reviewed revision

a72fabe

Behavior and intent

Only the launch order of the programs changes: the same [BLOCK_M, BLOCK_N] tiles are computed with the same instructions, now with column blocks varying fastest across the grid. Before, the order spread concurrent programs over one column stripe of many rows, which lost bandwidth at every row width and up to 4.9x time at power-of-two row pitches on gfx1201 (M = 113102, N = 8192: 45.0 ms before, 9.2 ms after). Results are bit-identical.

Validation and unverified scope

  • gfx1201 (Radeon AI PRO R9700), image rocm/pytorch:rocm10.1.0_ubuntu24.04_py3.12_pytorch_release_2.14.0 (torch 2.14.0, Triton 3.8.0), AITER_TRITON_ONLY=1:
    • python3 -m pytest -q op_tests/triton_tests/fusions/test_fused_indexed_gated_residual.py: 34 passed at a72fabe (on main 3486e43).
    • Timing: the table above, before (main 3486e43) and after (a72fabe), each case checked with torch.equal against residual + gate[indices] * x.
  • Unverified: MI hardware (none available to me). The tile config is unchanged, and on MI the old order may already have been fine; CI covers gfx942/gfx950 correctness.

PR-specific question

The automated self-review posted no question for this path, so here is the invariant I checked. Every call of the Triton fused_indexed_gated_residual on every architecture takes the changed launch. The grid is cdiv(M, BLOCK_M) * cdiv(N, BLOCK_N) programs on axis 0, and program pid computes tile (pid // col_blocks, pid % col_blocks). So each (row block, column block) pair is computed exactly once, with the same masks as before. Axis 0 allows up to 2^31 - 1 programs, so the 1-D grid has no new size limit; the old grid's axis 1 was capped at 65535 column blocks. The 34 existing tests compare against the eager reference with torch.equal.

Changed op tests

None.

…astest grid index

The kernel launched a (row blocks, column blocks) grid, so programs that
run together covered one 256-column stripe of many rows. On gfx1201 that
loses bandwidth at every row pitch and collapses it at large power-of-two
pitches: N = 8192 ran at 138 GB/s. A 1-D grid with the column block
varying fastest lets concurrent programs stream along rows: 584-597 GB/s
for every shape measured at M = 16384. Outputs are unchanged.

Signed-off-by: Sylvan Liu <[email protected]>
@Shan2L
Shan2L requested a review from a team November 30, 2026 08:34
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

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

Extended tests (opt-in via labels):

Label Tests
ci:mi300x Run Aiter standard tests on MI300X in PRs; main branch always runs them
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 (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:windows Windows JIT op smoke test on the Radeon R9700 ROCm runner
ci:multi-gpu 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: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
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 8924 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

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 stop the title rewrites; labels stay in sync either way.

Reviews:
A reviewer is usually assigned within one working day after the PR is ready for review (see Code Review Process). If nobody has responded for about a week, mention the area contact listed in CONTACTS.md. Want to help review? See REVIEWERS.md.

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 launch-order change affects all supported GPUs, but timing evidence covers only gfx1201, leaving gfx942/gfx950 performance for a maintainer to confirm before a final decision.

No open issues 🎉

What changed in this PR

This PR changes the launch order of AITER’s Triton fused_indexed_gated_residual kernel to improve memory access on gfx1201. The public API, computation, tile sizes, and tuning configuration remain unchanged.

Changes:

  • Replace the two-dimensional launch grid with a one-dimensional grid.
  • Derive tile coordinates so column blocks vary fastest, avoiding the secondary grid-axis size limit.
File Description
aiter/​ops/​triton/​fusions/​fused_indexed_gated_residual.py Launch all row and column tiles on one grid axis.
aiter/​ops/​triton/​_triton_kernels/​fusions/​fused_indexed_gated_residual.py Map each program ID to tile coordinates with column blocks varying fastest.

Review effort: Balanced

@aiter-bot

aiter-bot commented Nov 30, 2026 •

Copy link
Copy Markdown

NOT READY FOR HUMAN REVIEW

PR Self-Review

Revision a72fabea0721 · mode shadow

Author self-review accepted. Readiness report:

Author answers

  • PASS — current-SHA answers are present and structurally specific
Show the author's self-review answers

Behavior and intent

Only the launch order of the programs changes: the same [BLOCK_M, BLOCK_N] tiles are computed with the same instructions, now with column blocks varying fastest across the grid. Before, the order spread concurrent programs over one column stripe of many rows, which lost bandwidth at every row width and up to 4.9x time at power-of-two row pitches on gfx1201 (M = 113102, N = 8192: 45.0 ms before, 9.2 ms after). Results are bit-identical.

Validation and unverified scope

- gfx1201 (Radeon AI PRO R9700), image `rocm/pytorch:rocm10.1.0_ubuntu24.04_py3.12_pytorch_release_2.14.0` (torch 2.14.0, Triton 3.8.0), `AITER_TRITON_ONLY=1`:
  - `python3 -m pytest -q op_tests/triton_tests/fusions/test_fused_indexed_gated_residual.py`: 34 passed at a72fabea (on main 3486e43b).
  - Timing: the table above, before (main 3486e43b) and after (a72fabea), each case checked with `torch.equal` against `residual + gate[indices] * x`.
- Unverified: MI hardware (none available to me). The tile config is unchanged, and on MI the old order may already have been fine; CI covers gfx942/gfx950 correctness.

PR-specific question

The automated self-review posted no question for this path, so here is the invariant I checked. Every call of the Triton `fused_indexed_gated_residual` on every architecture takes the changed launch. The grid is `cdiv(M, BLOCK_M) * cdiv(N, BLOCK_N)` programs on axis 0, and program `pid` computes tile `(pid // col_blocks, pid % col_blocks)`. So each `(row block, column block)` pair is computed exactly once, with the same masks as before. Axis 0 allows up to 2^31 - 1 programs, so the 1-D grid has no new size limit; the old grid's axis 1 was capped at 65535 column blocks. The 34 existing tests compare against the eager reference with `torch.equal`.

Changed op tests

None.

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • PASS — no active Copilot finding lacks a substantive author reply

Code and op-test hygiene

  • PASS — no committed artifact or conflict marker detected

Required CI

  • PASS — Black: passed
  • PASS — Ruff: passed
  • PASS — Repository dependency: passed
  • FAIL — Aiter Test Gate: failed
  • Required CI failed; fix it before requesting human review.

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

@Shan2L

Shan2L commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

The failed Aiter Test Gate is infrastructure, not this change. 7 of the 8 standard-test shards stopped before running any test, at docker run:

Unable to find image 'rocm/pytorch:rocm7.2.4_ubuntu24.04_py3.12_pytorch_release_2.10.0' locally
docker: Error response from daemon: toomanyrequests: You have reached your unauthenticated pull rate limit.

I can't re-run jobs. Could a maintainer re-run the failed ones?

On the Copilot note about gfx942/gfx950: I have no MI hardware, so I could not time it there. Only the program-to-tile mapping changes; tile sizes, configs and the per-tile instructions stay the same. On MI the old order may already have been fine. If someone can run op_tests/op_benchmarks/triton/bench_fused_indexed_gated_residual.py -N 5376 --provider fused --metric time (and -N 4096) on main and on this branch, that would settle it. I'm happy to restrict the new order to gfx12 if MI turns out slower.

@zufayu
zufayu requested review from a team and Boss2002n December 1, 2026 04:37

This branch has not been deployed

No deployments
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.

4 participants