Skip to content

Add _MXFP8GroupedMM variant with bf16 backward - #4649

Draft
andrewor14 wants to merge 1 commit into
mainfrom
mxfp8_fwd_bf16_bwd
Draft

andrewor14 wants to merge 1 commit into
mainfrom
mxfp8_fwd_bf16_bwd

Conversation

@andrewor14

Copy link
Copy Markdown
Contributor

Summary:

Add _MXFP8GroupedMMFwdBF16Bwd, which does real MXFP8 forward but BF16 backward backward (straight-through), for QAT or RL trainer where a low-precision backward isn't needed. Selectable via a new bf16_bwd flag on _to_mxfp8_then_scaled_grouped_mm and MXFP8TrainingOpConfig. Also add KernelPreference.TRITON, which runs pure triton kernels for the forward quantization to work around an mxfp8 cutedsl issue (see #4647).

Example usage:

from torchao.prototype.moe_training.config import MXFP8TrainingOpConfig
from torchao.prototype.mx_formats.config import ScaleCalculationMode
from torchao.quantization import quantize_
from torchao.quantization.quantize_.common import KernelPreference
from torchtitan.models.common.moe import GroupedExperts

config = MXFP8TrainingOpConfig(
    kernel_preference=KernelPreference.TRITON,
    scale_calculation_mode=ScaleCalculationMode.RCEIL,
    bf16_bwd=True,
)
quantize_(model, config=config, filter_fn=lambda m, fqn: isinstance(m, GroupedExperts))

Test Plan:

pytest test/prototype/moe_training/test_mxfp8_grouped_mm.py -k bf16_bwd_forward_matches_default
pytest test/prototype/moe_training/test_mxfp8_grouped_mm.py -k bf16_bwd_triton_matches_emulated

Real e2e RL tests TBD.

@pytorch-bot

pytorch-bot Bot commented Jul 29, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit 344526e with merge base f10e69b (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 Jul 29, 2026
@andrewor14 andrewor14 added the module: training quantize_ api training flow label Jul 29, 2026
@vkuzo

vkuzo commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

why do we need cutedsl 4.6.0? can we use 4.5.2?

@andrewor14

Copy link
Copy Markdown
Contributor Author

why do we need cutedsl 4.6.0? can we use 4.5.2?

It's because vllm nightly (since 7/16/26) has 4.6.0 has a hard dependency, and I feel it's unlikely they'll downgrade. torchtitan RL is expected to work with vllm nightly

out_dtype: Optional[torch.dtype] = torch.bfloat16

# Whether to compute the gradient of the weights in high precision (True) or use MXFP8 (False).
wgrad_with_hp: bool = False

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.

@andrewor14 , i think instead of bf16_bwd you can set this flag to true and also add dgrad_with_hp to get the same effect but matching the existing code better.

@andrewor14

Copy link
Copy Markdown
Contributor Author

Going to close this to reopen with ghstack

@andrewor14 andrewor14 closed this Aug 3, 2026
@andrewor14 andrewor14 reopened this Aug 11, 2026
@andrewor14
andrewor14 marked this pull request as draft August 11, 2026 22:20
**Summary:**

Add `_MXFP8GroupedMMFwdBF16Bwd`, which does real MXFP8 forward but
BF16 backward backward (straight-through), for QAT or RL trainer
where a low-precision backward isn't needed. Selectable via a new
`bf16_bwd` flag on `_to_mxfp8_then_scaled_grouped_mm`
and `MXFP8TrainingOpConfig`. Also add KernelPreference.TRITON,
which runs pure triton kernels for the forward quantization to
work around an mxfp8 cutedsl issue (see #4647).

Example usage:

```
from torchao.prototype.moe_training.config import MXFP8TrainingOpConfig
from torchao.prototype.mx_formats.config import ScaleCalculationMode
from torchao.quantization import quantize_
from torchao.quantization.quantize_.common import KernelPreference
from torchtitan.models.common.moe import GroupedExperts

config = MXFP8TrainingOpConfig(
    kernel_preference=KernelPreference.TRITON,
    scale_calculation_mode=ScaleCalculationMode.RCEIL,
    bf16_bwd=True,
)
quantize_(model, config=config, filter_fn=lambda m, fqn: isinstance(m, GroupedExperts))
```

**Test Plan:**

```
pytest test/prototype/moe_training/test_mxfp8_grouped_mm.py -k bf16_bwd_forward_matches_default
pytest test/prototype/moe_training/test_mxfp8_grouped_mm.py -k bf16_bwd_triton_matches_emulated
```

Real e2e RL tests TBD.
@andrewor14
andrewor14 force-pushed the mxfp8_fwd_bf16_bwd branch 2 times, most recently from 0814732 to 344526e Compare August 17, 2026 19:43
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