Skip to content

Wire MoE and MXFP8Linear training paths to CuTeDSL by default - #4705

Open
alexsamardzic wants to merge 8 commits into
gh/alexsamardzic/13/headfrom
gh/alexsamardzic/14/head
Open

alexsamardzic wants to merge 8 commits into
gh/alexsamardzic/13/headfrom
gh/alexsamardzic/14/head

Conversation

@alexsamardzic

@alexsamardzic alexsamardzic commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

(Replaces #4620 due to incorrect ghstack base branch targeting.)

This PR wires the MXFP8 MoE grouped-mm and MXFP8Linear training paths to use the CuTeDSL backend by default, while keeping the legacy CUDA/Triton backend selectable for validation and benchmarking. It adds end-to-end correctness tests for grouped-mm, padded grouped-mm, prequantized MXTensor grouped-mm, and MXFP8Linear fwd/bwd, plus benchmark support for comparing CuTeDSL against the legacy backend. This prepares the next PR to delete torchao._C_mxfp8 and the now-obsolete legacy CUDA/Triton paths.


To run tests:

pytest -q \
    test/prototype/moe_training/test_mxfp8_grouped_mm.py::test_mxfp8_grouped_gemm_with_dq_fwd_bwd \
    test/prototype/moe_training/test_mxfp8_grouped_mm.py::test_mxfp8_grouped_gemm_padded_fwd_bwd \
    test/prototype/moe_training/test_mxfp8_grouped_mm.py::test_mxfp8_grouped_gemm_from_qdata_and_scales_forward \
    test/prototype/moe_training/test_mxfp8_linear.py::test_mxfp8_linear_fwd_bwd_sqnr

To run MoE grouped-mm fwd/bwd recipe benchmark:

python benchmarks/prototype/moe_training/mxfp8/roofline_unified.py \
    --backend=both --K=4096 --N=4096 --G=8 --breakdown_M=16384 \
    --outfile_speedup=roofline_speedup_K4096_N4096.csv \
    --outfile_quant_2d=roofline_quant_2d_K4096_N4096.csv \
    --outfile_quant_3d=roofline_quant_3d_K4096_N4096.csv \
    --plot_file=roofline_K4096_N4096.png

Results of benchmark above:
roofline_K4096_N4096


To run MXFP8Linear fwd/bwd recipe benchmark:

python benchmarks/prototype/moe_training/mxfp8/bench_mxfp8_linear.py \
    4096x4096x2048 \
    --cuda-graph-bench --graph-iters 100

Would be good to run on multi-GPU machine:

torchrun --nproc-per-node=2 --local-ranks-filter=0 \
    benchmarks/prototype/moe_training/mxfp8/bench_ep_pipeline.py --backend=legacy

torchrun --nproc-per-node=2 --local-ranks-filter=0 \
    benchmarks/prototype/moe_training/mxfp8/bench_ep_pipeline.py --backend=cutedsl

[ghstack-poisoned]
@alexsamardzic

alexsamardzic commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor Author

@pytorch-bot

pytorch-bot Bot commented Aug 5, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/4705

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 71bdb6c with merge base 39d5874 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 5, 2026
@alexsamardzic alexsamardzic added the module: training quantize_ api training flow label Aug 5, 2026
@alexsamardzic
alexsamardzic changed the base branch from gh/alexsamardzic/13/head to main August 5, 2026 19:07
@alexsamardzic
alexsamardzic changed the base branch from main to gh/alexsamardzic/13/head August 6, 2026 07:43
@@ -0,0 +1,212 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.

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.

split between test and benchmarks

reason="MXFP8 grouped GEMM requires SM100",
)
@pytest.mark.parametrize("backend", ("legacy", "cutedsl"))
def test_mxfp8_grouped_gemm_backend_fwd_bwd(backend):

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.

is there an existing test we can extend?

reason="MXFP8 grouped GEMM requires SM100",
)
@pytest.mark.parametrize("backend", ("legacy", "cutedsl"))
def test_mxfp8_grouped_gemm_padded_backend_fwd_bwd(backend):

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.

is there an existing test we can extend

reason="MXFP8 grouped GEMM requires SM100",
)
@pytest.mark.parametrize("backend", ("legacy", "cutedsl"))
def test_mxfp8_grouped_gemm_mxtensor_backend_forward(backend):

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.

and here

_MXFP8_GROUPED_MM_BACKEND = "cutedsl"


def set_mxfp8_grouped_mm_backend(backend: str) -> 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.

let's remove the global, instead a user should be able to select this when they create a config. You can use kernel_preference or create a new similar flag.

elem_dtype=torch.float8_e4m3fn,
hp_dtype=grad_output.dtype,
kernel_preference=KernelPreference.AUTO,
cast_kernel_choice=MXFP8Dim1CastKernelChoice.CUDA,

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.

can we wire it with things like MXFP8Dim1CastKernelChoice.CUTEDSL, etc instead of creating a new backend concept

from torchao.prototype.mx_formats.utils import _to_mxfp8_dim1_kernel_wrapper
from torchao.quantization.quantize_.common.kernel_preference import KernelPreference

_MXFP8_LINEAR_BACKEND = "cutedsl"

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.

remove the globals

block_size: int,
scale_calculation_mode: ScaleCalculationMode,
kernel_preference: KernelPreference,
mxfp8_dim0_cast_kernel_choice: MXFP8Dim0CastKernelChoice,

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.

i think we want MXFP8Dim0CastKernelChoice.CUTEDSL

@vkuzo

vkuzo commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

@claude review

@claude

claude Bot commented Aug 13, 2026 •

Copy link
Copy Markdown

Claude finished @vkuzo's task in 4m 38s —— View job


Claude PR Review

  • Read the PR diff
  • Review mxfp8_grouped_mm.py
  • Review mxfp8_linear.py
  • Review tests
  • Review benchmarks & validation script
  • Post review feedback

I reviewed the diff against origin/gh/alexsamardzic/13/head. Overall the wiring is clean and the added correctness tests (fwd/bwd SQNR, padded, prequantized MXTensor) are a good safety net for the upcoming legacy deletion. My feedback largely reinforces @vkuzo's inline comments and adds a few correctness concerns.

1. The backend global should go away (agrees with @vkuzo)

Both mxfp8_grouped_mm.py:56 (_MXFP8_GROUPED_MM_BACKEND) and mxfp8_linear.py:28 (_MXFP8_LINEAR_BACKEND) introduce process-wide mutable state with set_*_backend() setters. Beyond the API-design objection, this has concrete downsides:

  • Not thread-safe / not composable — two models (or two threads) can't use different backends, and torch.compile graphs capture whatever the global was at trace time.
  • Leaky test pattern — every test does set_backend(...) / try … finally: set_backend("cutedsl"). The finally hardcodes "cutedsl" rather than restoring the prior value, so if the module default ever changes the resets become silently wrong.

As @vkuzo notes, the cleaner path is to thread the choice through the config/call as MXFP8Dim1CastKernelChoice.CUTEDSL (and a CUTEDSL member on MXFP8Dim0CastKernelChoice, which today only has TRITON/TORCH — see config.py:16). mxfp8_linear.py already accepts an optional backend= param on MXFP8Linear.__init__ and _to_mxfp8_then_scaled_mm, so most of the plumbing is in place — the global is the only thing that needs removing.

2. "backend" is a leaky abstraction over the kernel choices (agrees with @vkuzo)

The "cutedsl" backend isn't purely CuTeDSL. In _compute_fwd_sm100 (mxfp8_grouped_mm.py:623) the cutedsl branch calls mxfp8_quantize_cuda_3d(...) for the weight quant. So a single string flag actually selects a mix of cutedsl + cuda kernels, which is exactly why per-cast MXFP8Dim{0,1}CastKernelChoice enums are a better fit than a monolithic backend concept — they let each cast be chosen independently and self-document what's actually running.

3. Correctness: scale_calculation_mode other than FLOOR/RCEIL silently breaks under cutedsl

In mxfp8_linear.py:50-55, the cutedsl dim0 path forwards scale_calculation_mode.value straight to mxfp8_quantize_2d_1x32_cutedsl, whose docstring says it only supports "floor"/"rceil" (quant.py:1506). The legacy MXTensor.to_mx path supports CEIL/EVEN too. Since this PR flips the default to cutedsl, a caller using ScaleCalculationMode.CEIL/EVEN will now hit the kernel with an unsupported mode instead of the emulated/triton path. The default is RCEIL so it's not hit today, but there's no assertion guarding it. Worth either asserting the mode is supported or falling back. Fix this →

4. Manual MXTensor(...) construction with positional args

mxfp8_linear.py:56-65 builds an MXTensor with 8 positional args ending in None, True (act_quant_kwargs=None, is_swizzled_scales=True). This is hard to read and fragile if the __new__ signature (mx_tensor.py:521) changes. Please use keyword args, and add a one-line comment explaining that the cutedsl kernel already emits swizzled (blocked tcgen05) scales — otherwise is_swizzled_scales=True on the forward torch.mm path looks surprising and it's not obvious the mm dispatch handles a swizzled dim0 operand here. (The new SQNR tests should cover this, but a comment would save the next reader.)

5. hasattr(weight_t, "_data") pattern

mxfp8_grouped_mm.py:624 and :745 use weight_t._data if hasattr(weight_t, "_data") else weight_t. MXTensor exposes .qdata, not ._data, so this is targeting a different wrapper subclass. The hasattr guard on a private attr with no comment is easy to misread — a short comment naming which tensor type carries _data (and why it's not qdata) would help.

6. Tests — extend existing rather than add near-duplicates (agrees with @vkuzo)

The three new tests (test_mxfp8_grouped_gemm_backend_fwd_bwd, ..._padded_backend_fwd_bwd, ..._mxtensor_backend_forward) largely duplicate the shape setup and SQNR-threshold boilerplate of the existing test_mxfp8_grouped_gemm* tests, differing mainly by an extra @pytest.mark.parametrize("backend", …). Where an existing test already exercises the same path, adding a backend parametrization to it (and dropping the global via #1) would avoid the copy-paste and the repeated try/finally.

7. scripts/validate_mxfp8_linear_cutedsl.py placement (agrees with @vkuzo)

As @vkuzo says, this file is doing two jobs: the SQNR comparison (--bench-only unset) is a test, the --cuda-graph-bench timing is a benchmark. Consider splitting the correctness assertions into test/ and the timing harness into benchmarks/prototype/moe_training/mxfp8/, consistent with the rest of the repo layout.

Minor

  • .value.lower() (grouped_mm) vs .value (linear) for scale_calculation_mode — the enum values are already lowercase, so .lower() is redundant; pick one for consistency.
  • The 2 new H100 CI failures are worth a look before merge — the SM100-guarded tests should skip on H100 (SM90), so a generic exec_script failed exit code 1 there smells like an import-time error in the new modules rather than a test assertion.

Nothing here is blocking beyond #1/#2 which @vkuzo already raised; #3 is the correctness item I'd most want addressed.
· branch gh/alexsamardzic/14/head

[ghstack-poisoned]
@alexsamardzic
alexsamardzic requested a review from drisspg as a code owner August 14, 2026 21:30
alexsamardzic added a commit that referenced this pull request Aug 14, 2026
ghstack-source-id: 4de8753
ghstack-comment-id: 5195370413
Pull-Request: #4705
@alexsamardzic

Copy link
Copy Markdown
Contributor Author

All comments addressed. Two of the three near-duplicate tests merged into existing ones; the padded-groups test stayed separate since it's the only one exercising pad_token_groups_for_grouped_mm=True.

[ghstack-poisoned]
alexsamardzic added a commit that referenced this pull request Aug 15, 2026
ghstack-source-id: 92257fb
ghstack-comment-id: 5195370413
Pull-Request: #4705
[ghstack-poisoned]
alexsamardzic added a commit that referenced this pull request Sep 14, 2026
ghstack-source-id: 191248a
ghstack-comment-id: 5195370413
Pull-Request: #4705
[ghstack-poisoned]
alexsamardzic added a commit that referenced this pull request Sep 14, 2026
ghstack-source-id: ba72a27
ghstack-comment-id: 5195370413
Pull-Request: #4705
[ghstack-poisoned]
alexsamardzic added a commit that referenced this pull request Oct 2, 2026
ghstack-source-id: b965209
ghstack-comment-id: 5195370413
Pull-Request: #4705
[ghstack-poisoned]
alexsamardzic added a commit that referenced this pull request Oct 12, 2026
ghstack-source-id: fa2b856
ghstack-comment-id: 5195370413
Pull-Request: #4705
[ghstack-poisoned]
alexsamardzic added a commit that referenced this pull request Nov 5, 2026
ghstack-source-id: d86fa49
ghstack-comment-id: 5195370413
Pull-Request: #4705
alexsamardzic added a commit that referenced this pull request Nov 5, 2026
ghstack-source-id: d86fa49
ghstack-comment-id: 5195370413
Pull-Request: #4705

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. module: training quantize_ api training flow

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants