Skip to content

Triton mxfp4 (and a8w4) grouped GEMM kernel - #723

Merged
matthiasdiener merged 16 commits into
devfrom
mdiener/mxfp4-grouped-gemm
Sep 23, 2026
Merged

matthiasdiener merged 16 commits into
devfrom
mdiener/mxfp4-grouped-gemm

Conversation

@matthiasdiener

@matthiasdiener matthiasdiener commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Description

Based on the Primus-Turbo kernels at https://github.com/AMD-AGI/Primus-Turbo/tree/main/primus_turbo/triton/grouped_gemm.

Adds the kernel itself+functional API. Passes the added tests and the Grouped GEMM microbenchmarks.

Followup PRs:

Fixes https://github.com/AMD-ROCm-Internal/frameworks-internal/issues/371.
Performance results: https://github.com/AMD-ROCm-Internal/frameworks-internal/issues/371#issuecomment-5720191794

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

Checklist:

  • I have read and followed the contributing guidelines
  • 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

@matthiasdiener matthiasdiener self-assigned this Aug 31, 2026
@matthiasdiener matthiasdiener added the ci-level 1 CI test level 1 label Aug 31, 2026
@matthiasdiener matthiasdiener changed the title Mdiener/mxfp4 grouped gemm Triton mxfp4 grouped GEMM kernel Aug 31, 2026
@@ -0,0 +1,601 @@
# Copyright (c) 2026, Advanced Micro Devices, Inc. All rights reserved.

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.

Primus kernels carry a 2025 watermark, not 100% sure of this but shouldn't we inherit that if still AMD?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, fixed in 4cd50f0

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

Missing shape and split validation can cause invalid memory access, while valid unaligned inputs currently fail quantization.

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

Pull request overview

Adds gfx950 Triton kernels and functional APIs for grouped MXFP4 forward, input-gradient, and weight-gradient GEMMs.

Changes:

  • Adds persistent grouped MXFP4 Triton kernels.
  • Adds quantization, dispatch, and autograd integration.
  • Adds numerical correctness and accumulation tests.
File summaries
File Description
grouped_gemm_mxfp4.py Implements Triton kernels and launch wrappers.
grouped_gemm_mxfp4_impl.py Adds quantization bridges and grouped operations.
test_grouped_gemm_mxfp4.py Tests forward, backward, padding, and accumulation.
Review details

Suppressed comments (2)

transformer_engine/pytorch/triton_kernels/grouped_gemm_mxfp4_impl.py:228

  • This entry point also needs to reject negative splits and require len(m_splits) == len(weights) and sum(m_splits) == grad_out.shape[0]. Passing unchecked offsets to the raw Triton kernel can cause out-of-bounds offset reads/stores or leave part of the returned gradient uninitialized.
    group_offs = _prefix_offsets(m_splits, grad_out.device)

transformer_engine/pytorch/triton_kernels/grouped_gemm_mxfp4_impl.py:274

  • Before slicing by m_splits, require nonnegative splits and verify that their sum equals both a.shape[0] and grad_out.shape[0]. As written, undersized splits silently drop trailing rows from wgrad, while oversized splits produce truncated groups and invalid quantizer shapes.
    lhs_data, lhs_scale, go_pad = _col_operand_grouped_padded(grad_out, m_splits)  # (N, Mpad/2)
    rhs_data, rhs_scale, _ = _col_operand_grouped_padded(a, m_splits)  # (K, Mpad/2)
  • Files reviewed: 3/3 changed files
  • Comments generated: 7
  • Review effort level: Balanced

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

Comment thread transformer_engine/pytorch/triton_kernels/grouped_gemm_mxfp4.py
Comment thread transformer_engine/pytorch/triton_kernels/grouped_gemm_mxfp4.py Outdated
Comment thread transformer_engine/pytorch/triton_kernels/grouped_gemm_mxfp4.py
Comment thread transformer_engine/pytorch/triton_kernels/grouped_gemm_mxfp4.py Outdated
@matthiasdiener matthiasdiener changed the title Triton mxfp4 grouped GEMM kernel Triton mxfp4 (and a8w4) grouped GEMM kernel Sep 17, 2026
Comment thread transformer_engine/pytorch/triton_kernels/grouped_gemm_mxfp4.py Outdated
Comment thread tests/pytorch/triton_kernels/test_grouped_gemm_mxfp4.py
Comment thread tests/pytorch/test_grouped_gemm_mxfp4.py Outdated
Comment thread tests/pytorch/triton_kernels/test_grouped_gemm_mxfp4.py
Comment thread tests/pytorch/triton_kernels/test_grouped_gemm_mxfp4.py

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

Architecture-specific autotuned GPU kernels and custom autograd require human validation alongside the unresolved correctness guards.

Review details

Suppressed comments (3)

transformer_engine/pytorch/triton_kernels/grouped_gemm_mxfp4.py:1

  • Use a single 2026 start year for this newly added file's AMD copyright, matching the new-file headers in grouped_gemm_mxfp4_impl.py:1 and test_grouped_gemm_mxfp4.py:1.
# Copyright (c) 2025-2026, Advanced Micro Devices, Inc. All rights reserved.

transformer_engine/pytorch/triton_kernels/grouped_gemm_mxfp4.py:1

  • This is a newly added AMD-only file, but its header uses a 2025-2026 range. New AMD-only files in this repository use the current year alone; please change the header to 2026.
# Copyright (c) 2025-2026, Advanced Micro Devices, Inc. All rights reserved.

transformer_engine/pytorch/triton_kernels/grouped_gemm_mxfp4.py:1

  • This newly added file uses a 2025-2026 copyright range, while new AMD files in this PR use the current-year-only header. Please use the required 2026 header for a new file.
# Copyright (c) 2025-2026, Advanced Micro Devices, Inc. All rights reserved.
  • Files reviewed: 4/4 changed files
  • Comments generated: 4
  • Review effort level: Balanced

# Per-group wgrad M padding multiple; must be >= the largest wgrad BLOCK_K the
# kernel may pick. 256 lets the variable-K autotune try BLOCK_K=256 (at the cost
# of more padding for small groups). Default 128 = unchanged behavior.
WGRAD_PAD_MULTIPLE = int(os.environ.get("NVTE_MXFP4_WGRAD_PAD", str(BLOCK_SIZE_K)))
Comment on lines +81 to +86
def _require_gfx950() -> None:
# The kernels use the CDNA4 scaled-FP4 MFMA (tl.dot_scaled e2m1); no other arch has it.
if not _is_gfx950():
raise RuntimeError(
"grouped MXFP4 GEMM requires gfx950 (CDNA4); the current device lacks the"
" scaled-FP4 MFMA used by tl.dot_scaled(..., \"e2m1\", ...)."
Comment on lines +248 to +257
if weight_row is not None:
b_data, b_scale = weight_row
N = b_data.shape[1]
_check_splits(m_splits, a.shape[0], b_data.shape[0])
else:
N = weights[0].shape[0]
_check_splits(m_splits, a.shape[0], len(weights))
b_datas, b_scales = [], []
for w in weights:
d, s = _row_operand(w) # (N, K/2), (N, K/32)
Comment on lines +81 to +83
def _require_gfx950() -> None:
# The kernels use the CDNA4 scaled-FP4 MFMA (tl.dot_scaled e2m1); no other arch has it.
if not _is_gfx950():

@wangye805 wangye805 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

@matthiasdiener
matthiasdiener merged commit 03afd8f into dev Sep 23, 2026
13 of 19 checks passed
@matthiasdiener
matthiasdiener deleted the mdiener/mxfp4-grouped-gemm branch September 23, 2026 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-level 1 CI test level 1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants