Skip to content

[Triton/Gluon] gfx1151 gemm_a16w16: round before the activation to avoid spills - #6676

Open
klin-amd wants to merge 9 commits into
mainfrom
amd/dev/klin/triton-gemm-a16w16-epilogue
Open

klin-amd wants to merge 9 commits into
mainfrom
amd/dev/klin/triton-gemm-a16w16-epilogue

Conversation

@klin-amd

@klin-amd klin-amd commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On gfx1151, gemm_a16w16 now rounds the fp32 accumulator to the 16-bit output type before a fused activation. This removes the epilogue register spills for bf16 and reduces them for fp16.

Motivation

On gfx1151 a fused activation makes gemm_a16w16 much slower: PixArt-XL FF1 (8192 x 4608 x 1152, fp16) gives 21.9 TFLOPS with gelu_tanh and 29.9 without. The cause is register pressure: the any bucket of configs/gfx1151/triton/gemm/gemm_a16w16/DEFAULT.json uses a 128 x 128 tile whose fp32 accumulator needs 128 VGPRs, and the epilogue spills 134 registers.

Changes

  • The kernel casts the accumulator to the output type before the activation, so it stays in packed registers. An unfused GEMM rounds the same way, so fused now matches unfused.
  • A new constexpr ROUND_BEFORE_ACT controls the step. The wrapper sets it for gfx1151. The step also needs a 16-bit output and NUM_KSPLIT == 1.
  • For fp16 the kernel holds the lower bound with tl.where: a cast below -65504 gives -inf, and gelu and silu give a NaN from it. tl.clamp and tl.maximum default to PropagateNan.NONE and would hide a real NaN.
  • Elsewhere the constexpr is False and Triton drops the step: a compile for gfx1100 and gfx1201 gives 20 AMDGCN kernels identical to main. Forced on, gfx1201 is 1.3% to 3.7% slower, so adding an architecture needs a benchmark first.

Performance (if applicable)

Radeon 8060S (gfx1151, 40 CU), ROCm 7.15, torch 2.13, Triton 3.8.0, layout TN. Median of interleaved rounds with alternating order, 3 for rows 1 and 3 and 6 for row 2; brackets give spread.

Hardware Workload/configuration Before After Change
Radeon 8060S (gfx1151, 40 CU) PixArt-XL FF1, 8192 x 4608 x 1152, fp16, gelu_tanh 21.93 TFLOPS (0.3%) 26.05 TFLOPS (0.8%) 1.188x
Radeon 8060S (gfx1151, 40 CU) SD3.5-Large FF1, 2048 x 9728 x 2432, bf16, gelu_tanh 16.18 TFLOPS (17.6%) 18.53 TFLOPS (23.6%) 1.145x
Radeon 8060S (gfx1151, 40 CU) PixArt-XL FF1, fp16, no activation (control) 29.89 TFLOPS (1.7%) 29.71 TFLOPS (1.6%) 0.994x

Controls: base against base gives 0.997x on row 1 and 1.019x on row 2; the no-activation row gives 0.994x, as both trees compile the same kernel. Row 2 is noisy — both sides spread well over the 5% bar, and the instability does not sort by side. Its median was measured twice in separate sessions, giving 1.139x and 1.145x, so the ratio holds where the absolute values move.

Spills

Shape dtype Before After
8192 x 4608 x 1152 fp16 134 112
2048 x 9728 x 2432 fp16 134 112
8192 x 4608 x 1152 bf16 135 0
2048 x 9728 x 2432 bf16 135 0

This sorts by dtype, not shape. The cast alone reaches 0 on fp16 too: removing only the tl.where bound gives 254 VGPRs and 0 spills, against 256 and 112 as shipped. So the fp16 bound costs the whole saving. bf16 keeps the same bound and still reaches 0, unexplained here.

Testing

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

Documentation

  • No documentation change is needed: the public API and numerical contract do not change.

Dependencies

  • No new third-party dependencies added

Breaking Changes

None.

PR Self-Review

Reviewed revision

db57388

Behavior and intent

On gfx1151, with a 16-bit output and NUM_KSPLIT == 1, the activation now gets a value already rounded to the output type instead of the fp32 accumulator. The spills made fusing slower than a separate activation. Other architectures are unchanged.

Validation and unverified scope

This PR does not change a test. A reviewer asked to remove the two op tests of an earlier revision, so I did the checks below by hand on a Radeon 8060S (gfx1151, 40 CU), ROCm 7.15, torch 2.13, Triton 3.8.0. The kernel and wrapper files of this revision are byte-identical to the revision that I tested.

  • pytest op_tests/triton_tests/gemm/basic/test_gemm_a16w16.py -q --tb=line on main and on this PR: 517 failed, 757 passed on main and 517 failed, 769 passed on the PR, 2560 skipped on both. The failed test IDs are the same on both sides. All 517 failures raise Required config file doesn't exist for gfx1151: a missing tuned config, not this PR.
  • The removed tests, run on this kernel: 12 passed. They force the accumulator to -98304 and +98304 for fp16 and bf16 with gelu, gelu_tanh and silu. fp16 below the range gives 0.0, fp16 above gives inf and bf16 keeps 98304.0. A NaN input stays NaN in the output.
  • Spills from the AMDGCN, main against this PR: fp16 gelu_tanh 136 to 40 VGPR spills (plus 13 SGPR spills), bf16 gelu_tanh 136 to 114.
  • With ROUND_BEFORE_ACT=False, the gfx1151 AMDGCN of 8 cases is the same as main, except for debug information.

Caveat: the --shape path of bench_gemm_a16w16.py does not forward --activation, so it times use_activation_0. Both trees were patched identically to forward it, and the kernel name was checked every run. That bug is on main, not fixed here.

Unverified: the gfx1201 figure and the AMDGCN comparison for gfx1100 and gfx1201 were not repeated here. MI250X and MI300X were not used: the step is off there. CI does not run the new branch, because CI has no gfx1151 runner.

PR-specific question

Before this PR the new tests cannot run, because ROUND_BEFORE_ACT does not exist. The canonical test_gemm_a16_w16_activation uses random data and a loose tolerance, so its accumulator never leaves the fp16 range.

Changed op tests

None.

@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 (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 gfx1201 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 6676 --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.

@klin-amd klin-amd changed the title [Triton] gemm_a16w16: round to the output type before the activation (GELU 19-34% faster on gfx1151) [Triton] gemm_a16w16: on gfx1151, round to the output type before the activation (GELU up to 36% faster) Oct 20, 2026
@menglcai-amd menglcai-amd changed the title [Triton] gemm_a16w16: on gfx1151, round to the output type before the activation (GELU up to 36% faster) [Triton/Gluon] gfx1151 gemm_a16w16: round before the activation to avoid spills Oct 22, 2026
@menglcai-amd
menglcai-amd marked this pull request as ready for review October 22, 2026 09:09
@menglcai-amd
menglcai-amd requested a review from a team October 22, 2026 09:09
@Boss2002n
Boss2002n requested a balanced review from Copilot October 22, 2026 09:33

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

Early bf16 rounding can convert finite negative accumulators to infinity and introduce NaN activation outputs.

1 open issue
What changed in this PR

This PR reduces register pressure in the gfx1151 Triton gemm_a16w16 activation epilogue by rounding accumulators to the output type before activation.

Changes:

  • Enable early rounding for gfx1151 with 16-bit output and no split-K reduction.
  • Add a NaN-preserving fp16 lower bound before conversion.
  • Add overflow and NaN regression tests that force the new path.
File Description
op_tests/​triton_tests/​gemm/​basic/​test_gemm_a16w16.py Add forced-path overflow and NaN tests.
aiter/​ops/​triton/​gemm/​basic/​gemm_a16w16.py Enable early rounding on gfx1151.
aiter/​ops/​triton/​_triton_kernels/​gemm/​basic/​gemm_a16w16.py Add conditional rounding and the fp16 lower bound.

Review effort: Balanced

@azaidy
azaidy requested a review from omuhamma October 22, 2026 16:18
Copilot AI balanced review requested due to automatic review settings October 22, 2026 17:09

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 change affects numerical behavior, and independent GPU correctness and performance validation remains unavailable.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Low severity Shorten docstring to meet two-sentence limit

op_tests/​triton_tests/​gemm/​basic/​test_gemm_a16w16.py:191

This three-sentence docstring exceeds the Triton documentation limit of two sentences. Shorten it to state the fp16 overflow cases and the finite bf16 case checked by this test.

1 resolved since last review

Review effort: Balanced

omuhamma
omuhamma previously approved these changes Oct 25, 2026

@omuhamma omuhamma 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.

lgtm

@github-actions

Copy link
Copy Markdown
Contributor

No activity for 15 days, so this is now labelled stale. A push or a comment clears it; keep-open exempts it. Nothing is closed automatically.

@github-actions github-actions Bot added the stale The PR hasn't been updated in 2 weeks label Nov 10, 2026
@menglcai-amd
menglcai-amd force-pushed the amd/dev/klin/triton-gemm-a16w16-epilogue branch from bc0292e to 895e81b Compare November 10, 2026 05:55
@aiter-bot

aiter-bot commented Nov 10, 2026 •

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision f56df5f96efa · mode shadow

Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision.

Questions to answer in the PR description

  1. What behavior does this PR change, and why is the existing behavior insufficient?
  2. What did you validate, and what remains unverified? Include commands, hardware/configuration and results.
  3. What is the most important boundary or failure mode introduced by this change, and what evidence covers it?
  4. For every changed op_tests/ file, what behavior does it cover and why is it not redundant? Write None if no op test changed.

Edit the PR Self-Review section in the PR description. Include the current revision SHA; this comment refreshes automatically.

Missing or invalid answers

  • FAIL — Reviewed revision must contain at least the first 7 characters of the current head SHA

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

@menglcai-amd

Copy link
Copy Markdown
Contributor

Hi @omuhamma — this PR went stale, so I rebased it onto current main. The rebase dismissed your approval — could you please take another look when you get a chance? Thank you.

The only code difference from the version you approved is the conflict fix in the kernel signature: main's #5505 added NUM_XCDS next to this PR's ROUND_BEFORE_ACT, so both now sit together.

The description also carries new measurements taken on this revision, and some of the figures changed.

@github-actions github-actions Bot removed the stale The PR hasn't been updated in 2 weeks label Nov 11, 2026
omuhamma
omuhamma previously approved these changes Nov 17, 2026

@omuhamma omuhamma 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.

approving, but can you trim some comments (gemm_a16w16 L176-180 and some in the test file

@menglcai-amd

Copy link
Copy Markdown
Contributor

Hi @omuhamma, thank you for the review! I trimmed the kernel comment at L176-180 and the test comments in 24c3b64, with no code change.
The push dismissed your approval, so could you please take another look when you have time? Thank you!

klin-amd and others added 3 commits November 22, 2026 16:11
With activation="gelu" or "gelu_tanh", gemm_a16w16 on gfx1151 ran 19-34%
slower than with this change. The kernel now rounds the fp32 accumulator
to the fp16/bf16 output type before it applies the activation. An unfused
GEMM followed by an activation does the same rounding.

The change applies only to 16-bit outputs with NUM_KSPLIT == 1. Other
paths are not changed.

Co-Authored-By: Claude <[email protected]>
Add the ROUND_BEFORE_ACT constexpr. The wrapper sets it only on gfx1151,
the arch where the change was measured. The original activation path was
benchmarked on gfx942 and gfx950 (#736). With ROUND_BEFORE_ACT=False, the
kernel compiles to the same instructions as before on gfx942, gfx950 and
gfx1100.

Co-Authored-By: Claude <[email protected]>
Signed-off-by: menglcai <[email protected]>
@menglcai-amd
menglcai-amd force-pushed the amd/dev/klin/triton-gemm-a16w16-epilogue branch from 24c3b64 to 81fd2aa Compare November 22, 2026 08:17
omuhamma
omuhamma previously approved these changes Dec 1, 2026

@omuhamma omuhamma 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.

lgtm

@azaidy azaidy 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.

Please remove the test changes

Copilot AI balanced review requested due to automatic review settings December 2, 2026 19:22

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@menglcai-amd

Copy link
Copy Markdown
Contributor

Thanks @azaidy, I removed the test changes in db57388. The PR now changes only the kernel and the wrapper.
I verified the fp16 bound and NaN propagation on gfx1151 by hand, and the results are in the description.

@azaidy azaidy 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.

LGTM!

Copilot AI balanced review requested due to automatic review settings December 3, 2026 16:09

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings December 3, 2026 21:01

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings December 4, 2026 04:09

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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.

6 participants