fix(model): Ensure LayerNorm Parameters Match Computation Dtype During FP16 Execution#2790
Open
subramanya-dev wants to merge 1 commit into
Open
Conversation
Author
|
Hi @jongwook and @hintz-openai, Could you please review this PR when convenient? I believe it fixes a mixed-precision issue in Thanks |
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.
Summary
LayerNorm.forward()performs normalization infloat32for numerical stability and then casts the output back to the original input dtype. To avoid dtype mismatch errors when a model has been converted withmodel.half(), LayerNorm parameters should be explicitly cast to the computation dtype used by the normalization operation.Motivation
When models are converted to FP16 using:
the LayerNorm weights and biases are also converted to
float16.If LayerNorm internally performs computation using:
while leaving parameters in FP16, some PyTorch versions may raise dtype mismatch errors because the input tensor and normalization parameters no longer share the same dtype.
Other custom modules in the codebase already handle mixed-precision execution by ensuring operands participate in computation using compatible dtypes. Applying the same pattern to LayerNorm improves consistency and robustness.
Proposed Implementation
Use
torch.nn.functional.layer_norm()and explicitly cast the LayerNorm parameters to the computation dtype:Benefits
Compatibility
This change is backward compatible because it preserves the existing API and output dtype while only affecting the internal computation dtype handling.
Expected Outcome
LayerNorm remains numerically stable under mixed-precision execution and avoids failures that can occur when model parameters have been converted to FP16.