fix(triton): scope AMD gfx950 compiler knobs to GEMM kernel launches#405
Merged
Conversation
kyle-256
requested review from
RuibinCheung,
wenxie-amd and
xiaobochen-amd
as code owners
July 3, 2026 03:43
The AMD Triton knobs (use_async_copy / scalarize_packed_fops / use_block_pingpong) were set process-globally once and never reverted, so after any turbo GEMM/grouped-GEMM ran they leaked into unrelated Triton kernels: higher shared-memory (LDS) usage that OOMs during training and regresses some kernels. Wrap every GEMM/grouped-GEMM entry point with a scoped_amd_knobs decorator that snapshots the knobs on entry and restores them on exit; the knobs are compile-time only, so they need only be active while turbo's own kernels JIT-compile. Default state is now off; turbo turns them on only while its GEMM kernels compile.
kyle-256
force-pushed
the
dev/kyle/triton_amd_knobs_scope
branch
from
July 3, 2026 03:44
fe13b0e to
2d0df3e
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes AMD Triton compiler knob leakage by scoping gfx950-specific knob toggles to only the GEMM / grouped-GEMM entry points that need them, snapshotting and restoring the previous knob state on exit so unrelated Triton kernels in the same process aren’t affected.
Changes:
- Reworks the gfx950 knob helper to remove the one-shot global guard and adds a
scoped_amd_triton_knobs()context manager plus@scoped_amd_knobsdecorator for snapshot/restore semantics. - Wraps GEMM and grouped-GEMM public entry points with
@scoped_amd_knobsso knob changes only apply during first-time JIT compilation / launch. - Keeps existing layout-dependent
_set_amd_knobs(...)behavior, but now implicitly scoped via the decorator on the entry points.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
primus_turbo/triton/utils/triton_knobs_helper.py |
Introduces scoped snapshot/restore for AMD Triton knobs and removes process-global one-shot knob enabling. |
primus_turbo/triton/gemm/gemm_kernel.py |
Decorates the BF16 GEMM entry point to scope AMD knob changes. |
primus_turbo/triton/gemm/gemm_fp8_kernel.py |
Decorates FP8 GEMM entry points (tensorwise/rowwise/blockwise) to scope AMD knob changes. |
primus_turbo/triton/grouped_gemm/grouped_gemm_kernel.py |
Decorates grouped GEMM entry points (forward + variable-K backward) to scope AMD knob changes. |
primus_turbo/triton/grouped_gemm/grouped_gemm_fp4_kernel.py |
Decorates grouped MXFP4 entry points to scope AMD knob changes. |
primus_turbo/triton/grouped_gemm/grouped_gemm_fp8_kernel.py |
Decorates grouped FP8/MXFP8 entry points to scope AMD knob changes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
wenxie-amd
previously approved these changes
Jul 3, 2026
RuibinCheung
requested changes
Jul 6, 2026
The scoped_amd_triton_knobs context manager now enables the gfx950 compiler knobs itself (guarded by is_gfx950) after snapshotting, so every GEMM / grouped-GEMM entry point no longer needs an explicit set_triton_knobs_gfx950() call. Drop the public helper and the per-entry-point calls; the gfx942 _set_amd_knobs branch is unchanged. Behavior and knob coverage are identical.
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (1)
primus_turbo/triton/grouped_gemm/grouped_gemm_fp8_kernel.py:1699
- Similar to
gemm_fp8_kernel.py, this file removed the previous non-gfx950 knob toggling (_set_amd_knobs) for the blockwise entry points, but the PR description states the gfx942 layout-dependent knob logic was kept. Please either update the PR description to match the new behavior or restore the non-gfx950 knob handling under the new scoped approach if it is still needed for performance/compatibility.
@scoped_amd_knobs
def grouped_gemm_fp8_blockwise_triton_kernel(
a: torch.Tensor,
b: torch.Tensor,
a_scales: torch.Tensor,
Replace the two per-file _set_amd_knobs helpers with a single gfx942_enable parameter on scoped_amd_triton_knobs (and the scoped_amd_knobs decorator). gfx950 keeps its full knob set; gfx942 retains its per-layout policy: NT/NN enable use_async_copy / scalarize_packed_fops, while TN/wgrad disable them to avoid the ~5-8% MI300X regression. The unified dense blockwise kernel selects the policy at call time via the context manager since it dispatches NT/NN/TN internally.
kyle-256
force-pushed
the
dev/kyle/triton_amd_knobs_scope
branch
from
July 6, 2026 03:44
8ebb316 to
d0cd72b
Compare
Set triton.knobs.amd knobs only when the attribute exists, matching the snapshot loop. Prevents an AttributeError (or an un-restored stray knob) on Triton builds that expose knobs.amd but not every knob this scope touches, keeping snapshot/restore symmetric.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The AMD Triton knobs (
use_async_copy/scalarize_packed_fops/use_block_pingpong) were set process-globally once (guarded by a_KNOBS_SETflag) and never reverted. After any turbo GEMM / grouped-GEMM ran,these knobs leaked into unrelated Triton kernels running in the same
process (e.g. the training loop): they raise shared-memory (LDS) usage — fine
for a standalone op, but enough to OOM during training — and regress some other
Triton kernels.
These knobs are compile-time only: they affect codegen and only need to be
active while a kernel is JIT-compiled on its first launch (Triton caches the
compiled binary, so later launches are unaffected). This PR keeps the knob
flipping inside the GEMM / grouped-GEMM entry points but wraps every entry
point with a new
scoped_amd_knobsdecorator that snapshots the knob state onentry and restores it on exit.
Net effect: the default state is now off; turbo turns these knobs on only
while its own GEMM / grouped-GEMM kernels compile, so nothing else in the
process inherits them.
Fixes # (issue)
Type of change
Changes
primus_turbo/triton/utils/triton_knobs_helper.py: drop the one-shot_KNOBS_SETglobal guard (knobs are now re-applied per call and restored bythe surrounding scope); add
scoped_amd_triton_knobs()context manager thatsnapshots/restores the AMD knobs (with
triton.knobs.amdand env-varfallback), and a
scoped_amd_knobsdecorator wrapping it.@scoped_amd_knobs:primus_turbo/triton/gemm/gemm_kernel.py(1)primus_turbo/triton/gemm/gemm_fp8_kernel.py(3)primus_turbo/triton/grouped_gemm/grouped_gemm_kernel.py(2)primus_turbo/triton/grouped_gemm/grouped_gemm_fp4_kernel.py(2)primus_turbo/triton/grouped_gemm/grouped_gemm_fp8_kernel.py(8)_set_amd_knobs(...)calls are keptverbatim; they are now scoped (restored on function exit) too.
Checklist: