Repository navigation
[mxfp8 training] add MXFP8Linear - #4374
Conversation
stack-info: PR: #4374, branch: danielvegamyhre/stack/169
🔗 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 SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: ❌ 1 New Failure, 2 PendingAs of commit dddabf9 with merge base cf1007b ( NEW FAILURE - The following job has failed:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
77a354d to
74a08e8
Compare
| if kernel_preference == KernelPreference.EMULATED: | ||
| return | ||
| if not is_sm_at_least_100(): | ||
| pytest.skip("Real MXFP8 kernels require SM100+") |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Nit; If you wanted to save one of these activations, you could just use torch.autograd.grad.
| 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 |
There was a problem hiding this comment.
Should MXFP8Linear accept an MXFP8TrainingOpConfig?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
are we plannig to support more / different backward options?
There was a problem hiding this comment.
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
74a08e8 to
dddabf9
Compare
…#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
Summary
Tests
test_mxfp8_linear.pyMisc changes
mx_formats/mx_linear.py->moe_training/mxfp8_linearsince A) it was only used in moe_training dir, and B)moe_trainingwill soon become justtrainingand 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