Skip to content

Use F.rms_norm in RMSNorm when there is no affine weight - #15458

Open
Shan2L wants to merge 1 commit into
huggingface:mainfrom
Shan2L:users/syliu/rmsnorm-no-affine-fused
Open

Shan2L wants to merge 1 commit into
huggingface:mainfrom
Shan2L:users/syliu/rmsnorm-no-affine-fused

Conversation

@Shan2L

@Shan2L Shan2L commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #15457.

RMSNorm(elementwise_affine=False) ran pow, mean, rsqrt, a multiply and a cast as separate kernels. When there is no weight, forward now calls F.rms_norm, which does the same computation in one kernel. The affine path and the NPU path are unchanged.

On a [1, 8160, 4096] input (960x544x121 LTX-2 video tokens), per call, torch 2.14.0+rocm10.1.0:

GPU dtype before after
Radeon AI PRO R9600D bf16 1.704 ms 0.259 ms
Radeon AI PRO R9600D fp16 1.699 ms 0.258 ms
Radeon AI PRO R9600D fp32 1.135 ms 0.452 ms
Radeon AI PRO R9700 bf16 1.701 ms 0.226 ms
Radeon AI PRO R9700 fp16 1.699 ms 0.228 ms

The output is not bitwise identical: for bf16, 165 of 33,423,360 elements differ, by one bf16 step at most (fp16: 1,207 elements, one fp16 step; fp32: 24.7% of elements, at most 9.5e-7). The error against an fp32 reference on the largest values is unchanged in bf16 and fp16. The output dtype is unchanged.

Tests:

  • New RMSNormTests in tests/models/test_layers_utils.py: the no-affine output matches the explicit fp32 formula in fp32, fp16 and bf16 and keeps the input dtype. It fails if eps is changed in the new call.
  • tests/models/transformers/test_models_transformer_ltx2.py (includes the torch.compile tests) and tests/models/test_layers_utils.py, torch 2.14.0+rocm10.1.0: on main (facc812ea, Radeon AI PRO R9600D) no failure or error; with this change (Radeon AI PRO R9700) the same results plus the new test, also no failure or error.
  • tests/pipelines/ltx2/test_ltx2.py was not run to completion. On main and on this branch the first 71 results are identical, including two errors at the same position, and the file takes more than an hour on this setup, so I stopped both runs.

Self-review notes (diffusers self-review skill)

  • Rubric from .ai/references/review-rules.md, read against the whole diff (two source lines and one test class).
  • Blocking issues: none. utils/check_copies.py passes (no # Copied from copy of RMSNorm exists); ruff 0.17.5 reports no issue on the changed files (the repo pins 0.9.10, which I did not install).
  • Non-blocking, left for review:
    • the output differs from the current implementation by one rounding step on rare elements (numbers above);
    • the affine path is still the multi-kernel version;
    • the NPU branch is unchanged and I did not run on NPU; F.rms_norm is used only where the previous code already took the pure-PyTorch route.
  • Dead code: none.
  • Verdict: READY.

Before submitting

  • Did you use an AI agent (Claude Code, Codex, Cursor, etc.) to help with this PR? If so:
    • Did you read the Coding with AI agents guide?
    • Did you run the self-review skill on the diff?
    • Did you share the final self-review notes in the PR description or a comment?
  • Did you read the contributor guideline?
  • Did you read our philosophy doc? (important for complex PRs)
  • Was this discussed/approved via a GitHub issue or GitHub Discussions? Please add a link to it if that's the case.
  • Did you make sure to update the documentation with your changes? No documentation change is needed.
  • Did you write any new necessary tests?
  • Are you the author (or part of the team we work with) of the model/pipeline this PR relates to?

Who can review?

@yiyixuxu @sayakpaul

Signed-off-by: Sylvan Liu <Sylvan.Liu@amd.com>

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RMSNorm without affine weight runs as five or six kernels; F.rms_norm is 2.5-7.5x faster (not bitwise identical)

1 participant