Skip to content

[PyTorch] Compile normalization operations - #3920

Open
pggPL wants to merge 7 commits into
NVIDIA:mainfrom
pggPL:ops_norms_compile
Open

pggPL wants to merge 7 commits into
NVIDIA:mainfrom
pggPL:ops_norms_compile

Conversation

@pggPL

@pggPL pggPL commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Add torch.compile support for normalization operations (LayerNorm, RMSNorm, L2Normalization) in te.ops.Sequential, enabling full-graph compilation of normalized MLPs and standalone normalization stacks (including quantized outputs, autocast, dynamic shapes, and multi-dimensional normalized_shape).

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

  • Implement custom-op forward/backward pack, compute, fake, and context setup hooks for te.ops.LayerNorm, te.ops.RMSNorm, and te.ops.L2Normalization, requiring contiguous forward inputs under torch.compile and reporting CPU offloading as unsupported.
  • Add an explicit inherit_compile_ops class keyword argument to FusibleOperation.__init_subclass__ and use it in te.LayerNorm and te.RMSNorm wrappers so they share their parent operation's custom ops.
  • Flatten saved inputs to (-1, inner_dim) in BackwardAddRMSNorm so multi-dimensional normalized_shape works in the fused backward path, and extend test_backward_add_rmsnorm with a 2D normalized_shape.
  • Add torch.compile tests in tests/pytorch/test_torch_compile.py covering normalized MLPs (with FP8/MXFP8/NVFP4 recipes), LayerNorm/RMSNorm/L2Normalization variants (dtypes, strided rejection, FP8 input, inference), autocast (zero_centered_gamma, te.LayerNorm/te.RMSNorm), dynamic shapes, and multi-dimensional normalized_shape (unquantized and FP8).

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

pggPL and others added 6 commits November 23, 2026 13:15
Register LayerNorm, RMSNorm and L2Normalization forward and backward as
torch.compile custom ops with shape-only fake implementations. A same-named
subclass, such as the module-level RMSNorm wrapper, shares its parent's
custom ops instead of registering duplicates.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
…ompile tests

Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
@pggPL
pggPL marked this pull request as ready for review November 27, 2026 10:25
@pggPL
pggPL requested a review from timmoon10 as a code owner November 27, 2026 10:25
@pggPL

pggPL commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch L0

@greptile-apps

greptile-apps Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 4/5 · The PR still needs a fix because eager normalization backward can empty the caller's input.RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The changes reach PyTorch normalization stacks and models compiled through `te.ops.Sequential`.

The PR still needs a fix because eager normalization backward can empty the caller's input.

Findings

  1. P1 Backward empties the input ▶

Summary

Adds torch.compile support for LayerNorm, RMSNorm, and L2Normalization.

  • Compiled models now run three normalization operations through custom operations.
  • Fused RMSNorm backward handles multi-dimensional normalized weights.

Reviews (2) · Last reviewed commit: "Use explicit inherit_compile_ops and mir..." · Reviewed by Greptile

Comment on lines +340 to +341
if not in_custom_op:
clear_tensor_data(args.input_, args.means, args.rstdevs)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Backward empties the input

When a contiguous input already has the compute dtype, LayerNorm now saves the original tensor. Eager backward passes that same object to clear_tensor_data, which replaces its .data with an empty tensor. Previously, it cleared a separate flattened view, leaving the caller's tensor intact. This can destroy a retained input or break another backward branch that needs it. RMSNorm and its fused backward path have the same problem. Keep a separate saved view, or avoid clearing the original input.

Knowledge Base Used: PyTorch tensor and operator abstractions

Comment thread tests/pytorch/test_torch_compile.py Resolved
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
@pggPL
pggPL requested a review from ksivaman as a code owner November 27, 2026 11:42
@pggPL

pggPL commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator Author

/te-ci pytorch L0

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant