Skip to content

fix(triton): scope AMD gfx950 compiler knobs to GEMM kernel launches#405

Merged
RuibinCheung merged 4 commits into
mainfrom
dev/kyle/triton_amd_knobs_scope
Jul 6, 2026
Merged

fix(triton): scope AMD gfx950 compiler knobs to GEMM kernel launches#405
RuibinCheung merged 4 commits into
mainfrom
dev/kyle/triton_amd_knobs_scope

Conversation

@kyle-256

@kyle-256 kyle-256 commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

Description

The AMD Triton knobs (use_async_copy / scalarize_packed_fops /
use_block_pingpong) were set process-globally once (guarded by a
_KNOBS_SET flag) 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_knobs decorator that snapshots the knob state on
entry 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

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

  • primus_turbo/triton/utils/triton_knobs_helper.py: drop the one-shot
    _KNOBS_SET global guard (knobs are now re-applied per call and restored by
    the surrounding scope); add scoped_amd_triton_knobs() context manager that
    snapshots/restores the AMD knobs (with triton.knobs.amd and env-var
    fallback), and a scoped_amd_knobs decorator wrapping it.
  • Decorate all 16 GEMM / grouped-GEMM entry points with @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)
  • The existing gfx942 layout-dependent _set_amd_knobs(...) calls are kept
    verbatim; they are now scoped (restored on function exit) too.

Checklist:

  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Copilot AI review requested due to automatic review settings 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
kyle-256 force-pushed the dev/kyle/triton_amd_knobs_scope branch from fe13b0e to 2d0df3e Compare July 3, 2026 03:44

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

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_knobs decorator for snapshot/restore semantics.
  • Wraps GEMM and grouped-GEMM public entry points with @scoped_amd_knobs so 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.

Comment thread primus_turbo/triton/utils/triton_knobs_helper.py Outdated
Copilot AI review requested due to automatic review settings July 3, 2026 03:45

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 6 out of 6 changed files in this pull request and generated 2 comments.

Comment thread primus_turbo/triton/utils/triton_knobs_helper.py Outdated
Comment thread primus_turbo/triton/utils/triton_knobs_helper.py
@kyle-256 kyle-256 added the ci:gpu label Jul 3, 2026
wenxie-amd
wenxie-amd previously approved these changes Jul 3, 2026
Comment thread primus_turbo/triton/gemm/gemm_fp8_kernel.py Outdated
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.
Copilot AI review requested due to automatic review settings July 6, 2026 03:11

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

Comment thread primus_turbo/triton/utils/triton_knobs_helper.py
Comment thread primus_turbo/triton/gemm/gemm_fp8_kernel.py Outdated
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
kyle-256 force-pushed the dev/kyle/triton_amd_knobs_scope branch from 8ebb316 to d0cd72b Compare July 6, 2026 03:44
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.
RuibinCheung
RuibinCheung previously approved these changes Jul 6, 2026

@RuibinCheung RuibinCheung left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

Copilot AI review requested due to automatic review settings July 6, 2026 04:49

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 6 out of 6 changed files in this pull request and generated 2 comments.

Comment thread primus_turbo/triton/utils/triton_knobs_helper.py
Comment thread primus_turbo/triton/gemm/gemm_fp8_kernel.py

@RuibinCheung RuibinCheung left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@RuibinCheung
RuibinCheung merged commit 066f0e3 into main Jul 6, 2026
5 checks passed
@RuibinCheung
RuibinCheung deleted the dev/kyle/triton_amd_knobs_scope branch July 6, 2026 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants