fix: ensure dtype consistency in grouped_mm under autocast#43833
Open
nulone wants to merge 2 commits intohuggingface:mainfrom
Open
fix: ensure dtype consistency in grouped_mm under autocast#43833nulone wants to merge 2 commits intohuggingface:mainfrom
nulone wants to merge 2 commits intohuggingface:mainfrom
Conversation
torch._grouped_mm is not registered for autocast, causing dtype mismatch when LayerNorm outputs float32 but weights are bfloat16. Fixes huggingface#43828
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
ArthurZucker
reviewed
Feb 9, 2026
Collaborator
ArthurZucker
left a comment
There was a problem hiding this comment.
Hey! does not look too bad, just unsure, should we cast back post op?
Author
|
No need — the result is already cast back to So the dtype flow is: input (float32) → cast to weight.dtype (bf16) → _grouped_mm → cast back to hidden_states.dtype (float32) at the end. |
This was referenced Apr 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #43828
What does this PR do?
torch._grouped_mmis not registered for autocast. Undertorch.autocast, LayerNorm outputs float32 while model weights stay bfloat16, causing RuntimeError: "expected mat1 and mat2 to have same dtype".Fix
Cast input to
weight.dtypebefore calling_grouped_mminsrc/transformers/integrations/moe.py.Impact
Affects all MoE models using
grouped_mmunder autocast (Mixtral, Qwen3 MoE, DeepSeek, PhiMoE, etc.)Before submitting
Note: No local GPU access — relying on CI for verification.