Skip to content

Treat an engaged-but-undefined optional weight as no weight in rms_norm - #195574

Closed
egeboy35 wants to merge 1 commit into
pytorch:mainfrom
egeboy35:fix-rms-norm-undefined-weight
Closed

egeboy35 wants to merge 1 commit into
pytorch:mainfrom
egeboy35:fix-rms-norm-undefined-weight

Conversation

@egeboy35

@egeboy35 egeboy35 commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

aten::rms_norm declares Tensor? weight=None, but rms_norm_composite gates the weight multiply on weight_opt.has_value() alone. A C++ caller forwarding a possibly-undefined Tensor where an optional is expected produces an engaged optional holding an undefined tensor, and that reaches upcasted_result.mul(<undefined>):

Expected a proper Tensor but got None (or an undefined Tensor in C++) for argument #1 'other'

at::layer_norm accepts the same call, because math_native_layer_norm also checks .defined(). That asymmetry is the bug.

The MPS branch of rms_norm_symint had the same gate and would have handed an undefined weight to _fused_rms_norm_mps, so it is fixed here rather than left as a twin of the one being removed. I have no MPS hardware; that half is reasoned from the source and left to CI.

Test

The failure is only reachable from C++ — Python cannot construct an engaged optional holding an undefined tensor — so the regression test is a gtest in undefined_tensor_test.cpp, which aten/tools/run_tests.sh already runs. It went there rather than math_kernel_test.cpp, which is built but appears in no runner list and so would not have gated this PR.

This machine's Device Guard policy refuses to launch freshly linked gtest binaries, so the assertions were exercised through a cpp_extension against a locally built torch_cpu. Same harness before and after:

before:  engaged-but-undefined  : THREW -> Expected a proper Tensor but got None ...
after:   engaged-but-undefined  : ok, numel=6

and the test's own assertion, plus a check that a real weight is still applied:

equal(undef, nullopt)  : true
max abs diff           : 0
weight still applied   : true

Related

#193936 adds torch::nn::RMSNorm and currently works around this by converting an undefined weight to std::nullopt before calling at::rms_norm. If this lands first, that conversion and its comment can be dropped there.

Developed with AI assistance (Claude); I reproduced the failure, wrote the fix and ran the checks above.

`aten::rms_norm` declares `Tensor? weight=None`, but `rms_norm_composite`
gated the weight multiply on `weight_opt.has_value()` alone. A C++ caller
that forwards a possibly-undefined Tensor where an optional is expected
produces an engaged optional holding an undefined tensor, and that reached
`upcasted_result.mul(<undefined>)`, which the autograd layer's `unpack()`
rejects with "Expected a proper Tensor but got None (or an undefined Tensor
in C++) for argument #1 'other'". The same call shape on `at::layer_norm`
works, because `math_native_layer_norm` also checks `.defined()`.

The MPS branch of `rms_norm_symint` had the identical
has_value()-without-defined() gate and would have handed an undefined weight
to `_fused_rms_norm_mps`, so it is fixed here rather than left as a twin of
the bug being removed. I have no MPS hardware, so that half is reasoned from
the source and left to CI.

Test Plan:

The failure is only reachable from C++ - Python cannot construct an engaged
optional holding an undefined tensor - so the regression test is a gtest in
`undefined_tensor_test.cpp`, which `aten/tools/run_tests.sh` already runs.
It went there rather than `math_kernel_test.cpp` because that file is built
but is in no runner list, so it would not have gated this PR.

This machine's Device Guard policy refuses to launch freshly linked gtest
binaries, so the assertions were exercised through a `cpp_extension` against
a locally built `torch_cpu`. Before:

```
nullopt                   : ok, numel=6
engaged-but-undefined     : has_value=1 defined=0
                          : THREW -> Expected a proper Tensor but got None ...
layer_norm same call      : ok, numel=6
```

After:

```
nullopt                   : ok, numel=6
engaged-but-undefined     : has_value=1 defined=0
                          : ok, numel=6
layer_norm same call      : ok, numel=6
```

The test's own assertion, and that a real weight is still applied:

```
equal(undef, nullopt)  : true
max abs diff           : 0
weight still applied   : true
```

Built with:

```
USE_CUDA=0 BUILD_TEST=1 python -m pip install -e . -v --no-build-isolation
```

`clang-format` 19.1.5 is clean on the changed lines.

Developed with AI assistance (Claude).

Co-Authored-By: Claude Opus 5 <[email protected]>
@pytorch-bot

pytorch-bot Bot commented Sep 1, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (2 Unrelated Failures)

As of commit 94e0af8 with merge base 69c9b15 (image):

CRCR (non-blocking) - The following CRCR downstream CI jobs failed but are non-blocking:

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

@egeboy35

egeboy35 commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "release notes: nn" "topic: bug fixes"

@pytorch-bot pytorch-bot Bot added release notes: nn release notes category topic: bug fixes topic category labels Sep 1, 2026
@egeboy35

egeboy35 commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

@Skylion007 this is the .defined() guard I said I would send separately rather than widen #193936.

@albanD you approved both #121364, which introduced the has_value() line being changed, and #153666, which added the is_nested() line next to it - would you or @mikaylagawarecki mind taking a look?

🤖 Drafted with Claude Code; I reproduced the failure and ran the checks in the description.

@egeboy35

egeboy35 commented Sep 5, 2026

Copy link
Copy Markdown
Contributor Author

@malfet @Skylion007 - tagging you because you have both touched layer_norm.cpp recently and this PR has no reviewer assigned, so I don't think it has reached anyone. Happy to be redirected if someone else owns this.

rms_norm_composite gates the weight multiply on weight_opt.has_value() alone. An engaged optional can still hold an undefined tensor - which is exactly what a C++ caller passing a possibly-undefined Tensor for a Tensor? parameter produces - and line 305 then multiplies by it. Two functions below, rms_norm_symint already does the .defined() check, and math_native_layer_norm does too, so this is rms_norm being inconsistent with its neighbours rather than a new rule.

The fix adds the missing .defined(), reuses it for the is_nested line, and does the same for the MPS branch in rms_norm_symint.

This was found while reviewing #193936: the C++ RMSNorm mirror has to convert an undefined weight to std::nullopt before calling at::rms_norm to avoid this. If this lands, that workaround can go, and any other C++ caller of at::rms_norm stops hitting the same trap.

CI is green (156 passed) and there is a regression in undefined_tensor_test.cpp.

🤖 Drafted with Claude Code; I reviewed this before posting.

@drisspg drisspg added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Sep 22, 2026
@drisspg
drisspg requested a review from Skylion007 September 22, 2026 23:35
@egeboy35

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Sep 24, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

egeboy35 added a commit to egeboy35/pytorch that referenced this pull request Sep 25, 2026
pytorch#195574 made at::rms_norm treat an engaged-but-undefined weight as no
weight, so the nullopt conversion and the comment describing the old ATen
behavior are no longer needed. RMSNormNoAffine covers that path.

Test Plan:

On the previous base (7744b78) with pytorch#195574 cherry-picked:

```
build/bin/test_api.exe --gtest_filter=*RMSNorm*:*LayerNorm*
```

9 tests passed, including ModulesTest.RMSNormNoAffine.

Developed with AI assistance (Claude).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged open source release notes: nn release notes category topic: bug fixes topic 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