Skip to content

Restore ScalelessRMSNorm hand-rolled forward for eager numerical parity#19654

Open
billmguo wants to merge 1 commit into
pytorch:mainfrom
billmguo:export-D105593738
Open

Restore ScalelessRMSNorm hand-rolled forward for eager numerical parity#19654
billmguo wants to merge 1 commit into
pytorch:mainfrom
billmguo:export-D105593738

Conversation

@billmguo
Copy link
Copy Markdown
Contributor

Summary:
D104258950 changed ScalelessRMSNorm from a hand-rolled fp32 decomposition to a torch.nn.RMSNorm subclass so that QNN and other backends see a proper RMSNorm op for lowering. However, removing the custom forward meant eager execution now uses torch.nn.RMSNorm's fused CUDA kernel, which has different internal precision handling than the hand-rolled x.float() * rsqrt(mean(x^2) + eps) decomposition used by the rlformers reference model.

This caused both test_llm_backbone_correctness_cuda and test_llm_backbone_correctness_decode to fail:

  • fp32 case: SNR dropped from inf to 67-85 dB (same decoded text, different logits)
  • quantized case: SNR dropped to 1-35 dB with negative per-step values and divergent decoded text, because the precision difference was amplified by quantization noise

The fix restores the original hand-rolled forward override on ScalelessRMSNorm while keeping torch.nn.RMSNorm as the base class. A torch.compiler.is_compiling() guard ensures that during torch.export (for QNN, XNNPACK, or any backend), the fused torch.nn.RMSNorm op is used instead — preserving the export-path fix from D104258950.

Differential Revision: D105593738

Summary:
D104258950 changed `ScalelessRMSNorm` from a hand-rolled fp32 decomposition to a `torch.nn.RMSNorm` subclass so that QNN and other backends see a proper RMSNorm op for lowering. However, removing the custom `forward` meant eager execution now uses `torch.nn.RMSNorm`'s fused CUDA kernel, which has different internal precision handling than the hand-rolled `x.float() * rsqrt(mean(x^2) + eps)` decomposition used by the rlformers reference model.

This caused both `test_llm_backbone_correctness_cuda` and `test_llm_backbone_correctness_decode` to fail:
- **fp32 case**: SNR dropped from `inf` to 67-85 dB (same decoded text, different logits)
- **quantized case**: SNR dropped to 1-35 dB with negative per-step values and divergent decoded text, because the precision difference was amplified by quantization noise

The fix restores the original hand-rolled `forward` override on `ScalelessRMSNorm` while keeping `torch.nn.RMSNorm` as the base class. A `torch.compiler.is_compiling()` guard ensures that during `torch.export` (for QNN, XNNPACK, or any backend), the fused `torch.nn.RMSNorm` op is used instead — preserving the export-path fix from D104258950.

Differential Revision: D105593738
@billmguo billmguo requested a review from lucylq as a code owner May 18, 2026 21:04
@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented May 18, 2026

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/19654

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 Unclassified Failures

As of commit c93f866 with merge base 7c495fa (image):

NEW FAILURE - The following job has failed:

UNCLASSIFIED FAILURES - DrCI could not classify the following jobs because the workflow did not run on the merge base. The failures may be pre-existing on trunk or introduced by this PR:

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

@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 18, 2026
@meta-codesync
Copy link
Copy Markdown
Contributor

meta-codesync Bot commented May 18, 2026

@billmguo has exported this pull request. If you are a Meta employee, you can view the originating Diff in D105593738.

@github-actions
Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

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. fb-exported meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant