fix: #1432 TE RMS Norm numerical Instability#1681
Open
JiwaniZakir wants to merge 1 commit intoNVIDIA-NeMo:mainfrom
Open
fix: #1432 TE RMS Norm numerical Instability#1681JiwaniZakir wants to merge 1 commit intoNVIDIA-NeMo:mainfrom
JiwaniZakir wants to merge 1 commit intoNVIDIA-NeMo:mainfrom
Conversation
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Contributor
|
Hi @JiwaniZakir thanks for making this, do you have a wandb that you can share showing the improved stability? Since this change is at a module level, it will affect all models using it, so i want to make sure we have a good understanding/review of the change. Thank you. |
Contributor
|
/claude review |
Comment on lines
+230
to
+247
| """The TE RMSNorm patch must upcast bf16 inputs to fp32 before the kernel call.""" | ||
| captured = {} | ||
|
|
||
| def mock_original_forward(self_inner, x): | ||
| captured["dtype"] = x.dtype | ||
| return x # pass-through | ||
|
|
||
| instance = MagicMock() | ||
| x_bf16 = torch.randn(2, 4, dtype=torch.bfloat16) | ||
|
|
||
| # Replicate the patched forward logic from _make_lazy_te_patcher | ||
| input_dtype = x_bf16.dtype | ||
| if input_dtype != torch.float32: | ||
| result = mock_original_forward(instance, x_bf16.float()) | ||
| else: | ||
| result = mock_original_forward(instance, x_bf16) | ||
|
|
||
| assert captured["dtype"] == torch.float32, "TE RMSNorm should receive fp32 inputs after upcast patch" |
Contributor
There was a problem hiding this comment.
This test re-implements the patch logic inline rather than invoking the actual _patched_rmsnorm_forward. If the real patch logic drifts, this test will still pass.
More concretely, it also doesn't verify the .to(input_dtype) cast-back — the result variable is never checked for dtype. Consider adding:
assert result.dtype == torch.bfloat16, "Result should be cast back to the original input dtype"
Contributor
|
Hi, @JiwaniZakir Thank you for the PR! As for the next step, could you help us with
Let us know what you think. |
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.
Closes #1432
What does this PR do ?
Fix numerical instability in TE RMSNorm by upcasting non-fp32 inputs to float32 before the kernel call, then casting outputs back to the original dtype.
Changelog
nemo_automodel/components/models/common/utils.py: In_make_lazy_te_patcher,_patched_rmsnorm_forwardnow capturesinput_dtypeand, when the input is not alreadyfloat32, calls_original_rmsnorm_forwardwithx.float()and casts the result back toinput_dtypebefore returning.tests/unit_tests/models/common/test_model_common_utils.py: AddedTestFloat32RMSNormtest class with four tests covering:Float32RMSNormweight dtype preservation, output dtype consistency through the fp32 computation path, numerical accuracy comparison between fp32-upcast and native bf16 norm against a float64 reference, and verification that the TE RMSNorm patch passes fp32 tensors to the underlying kernel.Before your PR is "Ready for review"
Pre checks:
Additional Information
This PR was created with AI assistance (Claude). The changes were reviewed by quality gates and a critic model before submission.