Skip to content

[mxfp8 training] add MXFP8Linear - #4374

Merged
danielvegamyhre merged 1 commit into
mainfrom
danielvegamyhre/stack/169
May 9, 2026
Merged

danielvegamyhre merged 1 commit into
mainfrom
danielvegamyhre/stack/169

Conversation

@danielvegamyhre

@danielvegamyhre danielvegamyhre commented May 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • In torchtitan we are refactoring how quantized training is implemented:
    • Previous: initialize model definition with standard nn.Linear / torch._grouped_mm ops. Use torchao model converter to do module swaps or wrap nn.Parameters in tensor subclass with op overrides
    • New: apply quantization to model config first, then construct model based on the config, directly initializing MXFP8Linear / MXFP8Experts rather than doing a model conversion.
  • This requires we implement the building blocks wrapping our "dynamic quant + compute" autograd funcs, to be directly initialized.

Tests

  • Added test_mxfp8_linear.py

Misc changes

  • Migrated mx_formats/mx_linear.py -> moe_training/mxfp8_linear since A) it was only used in moe_training dir, and B) moe_training will soon become just training and be the v2 for the torchao training design - so both linears and grouped gemms will live here. More refactors will be done to materialize this.

Authored using Claude

danielvegamyhre added a commit that referenced this pull request May 9, 2026
stack-info: PR: #4374, branch: danielvegamyhre/stack/169
@pytorch-bot

pytorch-bot Bot commented May 9, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

❌ 1 New Failure, 2 Pending

As of commit dddabf9 with merge base cf1007b (image):

NEW FAILURE - The following job has failed:

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

@danielvegamyhre
danielvegamyhre force-pushed the danielvegamyhre/stack/169 branch from 77a354d to 74a08e8 Compare May 9, 2026 00:03
@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 May 9, 2026
@danielvegamyhre danielvegamyhre added mx module: training quantize_ api training flow labels May 9, 2026
if kernel_preference == KernelPreference.EMULATED:
return
if not is_sm_at_least_100():
pytest.skip("Real MXFP8 kernels require SM100+")

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.

NIT: I think if you do this as a decorator, you get better PyTests, like skip execution, non blocking though

ref, mxfp8 = _build_linear_pair(K, N, bias, kernel_preference, wgrad_with_hp)

x_ref = torch.randn(M, K, dtype=torch.bfloat16, device="cuda", requires_grad=True)
x_mxfp8 = x_ref.clone().detach().requires_grad_(True)

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.

Nit; If you wanted to save one of these activations, you could just use torch.autograd.grad.

Comment on lines +266 to +292
class MXFP8Linear(nn.Linear):
"""
A linear layer that performs dynamic MXFP8 quantization on inputs to GEMMs in both the forward and backward passes.
"""

def __init__(
self,
*args,
kernel_preference: KernelPreference = KernelPreference.AUTO,
wgrad_with_hp: bool = False,
**kwargs,
):
super().__init__(*args, **kwargs)
self.wgrad_with_hp = wgrad_with_hp
self.kernel_preference = kernel_preference

def forward(self, input: torch.Tensor) -> torch.Tensor:
output = _to_mxfp8_then_scaled_mm(
input,
self.weight,
kernel_preference=self.kernel_preference,
scale_calculation_mode=ScaleCalculationMode.RCEIL,
wgrad_with_hp=self.wgrad_with_hp,
)
if self.bias is not None:
output = output + self.bias.to(output.dtype)
return output

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.

Should MXFP8Linear accept an MXFP8TrainingOpConfig?

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.

I am conflicted on this cause MXFP8TrainingOpConfig contains pad_token_grouped_for_grouped_mm, which makes sense for the generic model conversion approach that wraps params in a tensor subclass that overrides both linear and grouped_mm - however, if we are just directly initializing a MXFP8Linear it is weird/confusing to have this unused field.

I would rather just have the few keyword args that the MXFP8Linear needs directly in the constructor, to keep it simple.

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.

are we plannig to support more / different backward options?

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.

None currently on my radar (besides wgrad_with_hp, which is already supported for both mxfp8 linear and grouped_mm)

stack-info: PR: #4374, branch: danielvegamyhre/stack/169
@danielvegamyhre
danielvegamyhre marked this pull request as draft May 9, 2026 01:11
@danielvegamyhre
danielvegamyhre force-pushed the danielvegamyhre/stack/169 branch from 74a08e8 to dddabf9 Compare May 9, 2026 01:11
@danielvegamyhre
danielvegamyhre marked this pull request as ready for review May 9, 2026 01:11
@danielvegamyhre
danielvegamyhre merged commit ac6f641 into main May 9, 2026
22 of 23 checks passed
yichuan-w added a commit to pytorch/torchtitan that referenced this pull request Jun 1, 2026
…#3473)

Migrate MXFP8Linear to use torchao's native MXFP8Linear
(pytorch/ao#4374) instead of Linear + quantize_(), following the same
pattern as Float8Linear.

Merge MXFP8LinearConverter and MXFP8GroupedExpertsConverter into a
single MXFP8Converter that handles both Linear and GroupedExperts in one
pass. For dense models (no GroupedExperts), the MoE traversal is a
no-op.

Fixes #3219
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 mx

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants