Skip to content

extend MXFP4 recipe to GroupedLinear and a8w4 (Triton grouped GEMM kernels) - #745

Open
matthiasdiener wants to merge 31 commits into
devfrom
mdiener/mxfp4-a8w4-recipe
Open

matthiasdiener wants to merge 31 commits into
devfrom
mdiener/mxfp4-a8w4-recipe

Conversation

@matthiasdiener

@matthiasdiener matthiasdiener commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Description

Followup of #723.

Extends the MXFP4 recipe to route te.GroupedLinear through the grouped GEMM MXFP4/a8w4 Triton kernels (#723) on gfx950, and adds a8w4 support (fwd-only) for K3.

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

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 Sep 28, 2026
@matthiasdiener matthiasdiener added the ci-level 1 CI test level 1 label Sep 28, 2026
@matthiasdiener
matthiasdiener force-pushed the mdiener/mxfp4-a8w4-recipe branch 2 times, most recently from 66400e7 to d640739 Compare September 28, 2026 18:33
@matthiasdiener
matthiasdiener force-pushed the mdiener/mxfp4-a8w4-recipe branch from d640739 to e3027dc Compare September 28, 2026 18:36
@matthiasdiener
matthiasdiener force-pushed the mdiener/mxfp4-a8w4-recipe branch from e3027dc to f4c9b08 Compare September 28, 2026 18:50
@matthiasdiener matthiasdiener changed the title add a8w4/mxfp4 reference recipe extend MXFP4 recipe to GroupedLinear + a8w4 (Triton grouped GEMM kernels) Sep 28, 2026
@matthiasdiener matthiasdiener changed the title extend MXFP4 recipe to GroupedLinear + a8w4 (Triton grouped GEMM kernels) extend MXFP4 recipe to GroupedLinear and a8w4 (Triton grouped GEMM kernels) Sep 28, 2026
@matthiasdiener
matthiasdiener requested a balanced review from Copilot September 28, 2026 18:58

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 review overview

🟡 Changes recommended

The fast-path dispatch breaks supported buffer and gradient-handling modes and lacks sufficient layout and shape validation.

Review effort: Balanced
Findings: 2 High severity · 2 Low severity

Open (4)
What changed in this PR

Extends ROCm gfx950 MXFP4 support to GroupedLinear, including a8w4 forward inference and a4w4 training paths.

Changes:

  • Adds prequantized grouped Triton GEMM dispatch.
  • Adds MXFP8 E4M3 reference quantization and recipe factories.
  • Adds Linear and GroupedLinear coverage for a8w4/a4w4.
File Description
grouped_gemm_mxfp4_impl.py Adds prequantized grouped GEMM entry point.
grouped_linear.py Integrates MXFP4/a8w4 fast paths and backward dispatch.
quantization_mxfp4_grouped.py Defines reference quantizers and recipe factories.
test_a8w4_recipe.py Tests a8w4 Linear forward and backward.
test_a8w4_grouped_fast.py Tests grouped kernels and GroupedLinear integration.

💡 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/module/grouped_linear.py Outdated
Comment thread tests/pytorch/mxfp4/test_a8w4_recipe.py Outdated
Comment thread transformer_engine/pytorch/module/grouped_linear.py Outdated

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 encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

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 review overview

🟡 Changes recommended

The optimized path mishandles supported output, gradient, cache, empty-input, and quantization-parity cases.

Review effort: Balanced
Findings: 1 High severity · 2 Medium severity

Open (3)
Previously missed (2)

In code that hasn't changed since last review

Medium severity Honor preallocated out and dgrad_out buffers

transformer_engine/​pytorch/​module/​grouped_linear.py:1216

The optimized path returns before handling the public out and dgrad_out arguments, so callers that supply preallocated buffers no longer receive aliased output or dgrad as documented. Either plumb both buffers through the custom kernels or fall back whenever either is provided.

Medium severity Support fused and delayed wgrad modes in the optimized path

transformer_engine/​pytorch/​module/​grouped_linear.py:1216

This early path ignores fuse_wgrad_accumulation and delayed wgrad (wgrad_store), although both are supported GroupedLinear modes. Its backward returns ordinary parameter gradients immediately, so callers requesting fused accumulation will not get main_grad updates and delayed-wgrad callers will not have work queued. Gate this route for those modes or implement the same handling as _backward_blockwise_fp8_triton.

Comment thread transformer_engine/pytorch/triton_kernels/cast_transpose.py
Comment thread transformer_engine/pytorch/module/grouped_linear.py Outdated
Comment thread transformer_engine/pytorch/triton_kernels/cast_transpose.py Outdated

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 review overview

🟡 Changes recommended

Scale-layout ambiguity, skipped validation, and unsupported GroupedLinear options can produce incorrect behavior.

Review effort: Balanced
Findings: 1 High severity · 2 Medium severity

Open (3)
Resolved since last review (3)

Comment thread transformer_engine/pytorch/module/grouped_linear.py
Comment thread transformer_engine/pytorch/triton_kernels/grouped_gemm_mxfp4_impl.py Outdated
Comment thread transformer_engine/pytorch/triton_kernels/grouped_gemm_mxfp4_impl.py Outdated

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 review overview

🟡 Changes recommended

Cache namespace collisions, device-insensitive architecture gating, and fused-quantization underflow can cause failures or incorrect results.

Review effort: Balanced
Findings: 1 High severity

Open (1)
Resolved since last review (1)
Previously missed (1)

In code that hasn't changed since last review

Medium severity Clamp tiny block scales to preserve nonzero values

transformer_engine/​pytorch/​triton_kernels/​cast_transpose.py:909

The reused cast kernel does not match the documented reference for every nonzero block. When amax / 448 underflows into FP32's subnormal range, float_to_e8m0_triton emits scale byte 0 and exp2f_rcp_triton treats that byte as an inverse scale of 1, so tiny BF16 values quantize to zero. The torch reference clamps the exponent to -127 and rescales first (for example, amax=2^-126 becomes E4M3 value 2), preserving the value. This makes the new fused path numerically differ for nonzero low-magnitude blocks; use a downcast path that applies the clamped -127 inverse scale for this case.

Comment thread transformer_engine/pytorch/module/grouped_linear.py Outdated
@matthiasdiener
matthiasdiener marked this pull request as ready for review September 29, 2026 22:57
Comment thread transformer_engine/pytorch/module/grouped_linear.py Outdated
Comment thread transformer_engine/pytorch/module/grouped_linear.py Outdated
# Conflicts:
#	transformer_engine/pytorch/module/grouped_linear.py
@Micky774

Copy link
Copy Markdown
Contributor

There are a couple of tests that fail to collect -- also the new test files still need to be added to the CI script right?

@matthiasdiener

matthiasdiener commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor Author

There are a couple of tests that fail to collect

Thanks, this is fixed in c4c0e64

-- also the new test files still need to be added to the CI script right?

The CI test script already runs all test files in the mxfp4/ folder, so it should be fine:

run_default_fa 1 mxfp4/

@Micky774 Micky774 added the P2 Priority 2: normal label Oct 16, 2026

@contextlib.contextmanager
def _amd_compiler_knobs(*, is_tn: bool):
def _amd_compiler_knobs(*, is_tn: bool, overrides: dict | None = None):

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.

Since we now have the fp8 blocked as well as mxfp4 implementations sharing this func, can we move it into a shared utility file like common.py?

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.

Thanks, done in b4e9e19

Comment thread transformer_engine/pytorch/module/grouped_linear.py
Comment thread transformer_engine/pytorch/triton_kernels/grouped_gemm_mxfp4.py Outdated
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 P2 Priority 2: normal

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants