Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix MPS fused RMSNorm: do the weight multiply in fp32 to match CPU/CUDA#187779

Closed
Incheonkirin wants to merge 2 commits into
pytorch:mainfrom
Incheonkirin:fix-mps-rmsnorm-fp32-weight-multiply
Closed

Fix MPS fused RMSNorm: do the weight multiply in fp32 to match CPU/CUDA#187779
Incheonkirin wants to merge 2 commits into
pytorch:mainfrom
Incheonkirin:fix-mps-rmsnorm-fp32-weight-multiply

Conversation

@Incheonkirin

Copy link
Copy Markdown

What does this PR do?

The MPS fused RMSNorm kernel casts the normalized value to the half dtype before multiplying by the weight (out = w * static_cast<T>(x * inv)), so its output diverges from PyTorch's CPU composite and CUDA fused/reference behavior, which keep the x * inv * weight product in fp32 and cast once at the end (the CPU side was aligned in #147203). This affects half/bfloat16 inference on MPS whenever RMSNorm/rms_norm dispatches to the fused affine kernel (weight present, matching dtype, no grad).

Fix: do the weight multiply in fp32 in the same order as the reference ((x * inv) * weight) and cast once. Adds an MPS regression test that runs the fused kernel in fp16/bf16 and in fp32 with the same inputs (identical reduction) and asserts they agree, so only the weight-multiply precision is checked (N = 4095, 4097, covering both shader paths).

Reproduced on Apple Silicon; the kernel recompile (a full source build) is left to CI.

The MPS fused RMSNorm kernel cast the normalized value to the half dtype before
the weight multiply, diverging from the CPU composite and CUDA fused/reference
paths which keep x*inv*weight in fp32 and cast once (CPU aligned in #147203).
Do the weight multiply in fp32 in the reference order ((x*inv)*weight) and cast
once. Adds an MPS regression test (fp16/bf16, N=4095/4097).
@Incheonkirin Incheonkirin requested a review from malfet as a code owner June 21, 2026 01:35
@pytorch-bot

pytorch-bot Bot commented Jun 21, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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:

✅ No Failures

As of commit 3b06ac9 with merge base fd03d74 (image):
💚 Looks good so far! There are no failures yet. 💚

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

@pytorch-bot pytorch-bot Bot added ciflow/mps Run MPS tests (subset of trunk) release notes: mps Release notes category labels Jun 21, 2026
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jun 21, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: Incheonkirin / name: jeongmingi (c797e89)

@pytorch-bot

pytorch-bot Bot commented Jun 21, 2026

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@jhavukainen jhavukainen left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think the idea is correct, we shouldn't truncate mid computation and aligning with the other backend implementations makes sense here.

Left a few comments on how this might be tidied up a bit.

Comment thread aten/src/ATen/native/mps/kernels/RMSNorm.metal
Comment thread aten/src/ATen/native/mps/kernels/RMSNorm.metal
Comment thread aten/src/ATen/native/mps/kernels/RMSNorm.metal Outdated
Comment thread aten/src/ATen/native/mps/kernels/RMSNorm.metal Outdated
Comment thread aten/src/ATen/native/mps/kernels/RMSNorm.metal Outdated
Comment thread aten/src/ATen/native/mps/kernels/RMSNorm.metal Outdated
Per review: the four write sites repeated the same upcast, so move it into
one inline helper and use opmath_t<T> instead of hardcoding float (needs
c10/metal/utils.h for opmath_t). No behavior change.
@zou3519 zou3519 added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Jun 23, 2026

@jhavukainen jhavukainen left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good to me after the cleanup.

@Incheonkirin

Copy link
Copy Markdown
Author

I accidentally closed this PR while changing the visibility of my local upstream mirror. The fix is still absent from main, and the existing MPS review and CI were green. I have restored the source repository and branch, but GitHub no longer lets me reopen the PR from my account after the fork relationship was detached (the API still reports the patch as mergeable).\n\n@jhavukainen, could you please reopen it? Sorry for the noise.

@Incheonkirin

Copy link
Copy Markdown
Author

Replacement PR is now #189617 from a proper fork; no reopen is needed. It preserves the reviewed patch and is rebased onto current main.

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

Labels

ciflow/mps Run MPS tests (subset of trunk) open source release notes: mps Release notes category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants