Raise error for 1D (size > 1) -> 0D parameter loads#166335
Raise error for 1D (size > 1) -> 0D parameter loads#166335dsashidh wants to merge 2 commits intopytorch:mainfrom
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/166335
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 8d2fa97 with merge base ed4aa44 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@pytorchbot label "topic: not user facing" |
There was a problem hiding this comment.
@dsashidh It looks like this case was here for backwards compatibility, but from a long time ago.
If there is a decision to no longer support this backward compatibility, the cleaner fix would be to remove this if statement and let if fall back to if not is_param_lazy and input_param.shape != param.shape, where it will be caught in this case and currently has the same body.
There was a problem hiding this comment.
Thanks for the feedback, I commented below.
| @@ -2436,7 +2436,11 @@ def _load_from_state_dict( | |||
| and len(param.shape) == 0 | |||
| and len(input_param.shape) == 1 | |||
There was a problem hiding this comment.
Perhaps add and input_param.shape[0] == 1, so the unexpected case will not fall into this if statement
There was a problem hiding this comment.
Thanks for the feedback, I commented below.
|
Thank you for the feedback! I wrote my test with the expectation that [1] -> scalar should raise an error (strict shape matching), which passes with approach 1 but not approach 2. Since this backward compatibility is from PyTorch 0.3 (2017), I'm inclined toward approach 1 for stricter shape matching. |
|
I think it should be fine to load a 1d tensor of size 1 into a scalar tensor, iiuc that's what adding the check I suggested would do, though do correct me if I'm wrong |
|
@dsashidh Go with what @mikaylagawarecki is suggesting. |
Hi @mikaylagawarecki thanks for clarifying! I've implemented your suggested check (and input_param.shape[0] == 1) and updated my test accordingly. |
6b097c0 to
8d2fa97
Compare
|
Hi @mikaylagawarecki I was seeing a MYPY lintrunner failure unrelated to my changes. I’ve rebased my branch on upstream/viable/strict which should hopefully align it with a clean CI baseline |
|
@pytorchbot merge |
Merge startedYour 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 |
Summary: Fix in one OSS notebook where the state_dict was naively expanded. This [pytorch PR](pytorch/pytorch#166335) caused an error that was previously silently ignored. Differential Revision: D86884189
Summary: Pull Request resolved: #3079 Fix in one OSS notebook where the state_dict was naively expanded. This [pytorch PR](pytorch/pytorch#166335) caused an error that was previously silently ignored. Reviewed By: sdaulton Differential Revision: D86884189 fbshipit-source-id: 2c5ded01b17800a64da53b0722cfcc1ccac5e6eb
Fixes pytorch#165873 # Title Fix load_state_dict: raise error for 1D (size > 1) -> 0D parameter loads ## Summary This PR fixes a bug where loading a 1D tensor (size > 1) into a scalar (0D) parameter would silently take the first element instead of raising an error. The fix preserves backward compatibility for 1D tensors of size 1 while catching genuine shape mismatches. ## Motivation Previously, loading a 1D tensor like torch.randn(32000) into a 0D scalar parameter would silently slice the first element, leading to silent data loss and potential bugs. This change ensures users get a clear error when there's a genuine shape mismatch. ## Behavior change Before: 1D tensor (any length) -> 0D scalar -> silently coerced using input_param[0] After: - 1D tensor (size == 1) -> 0D scalar -> allowed (backward compatibility) - 1D tensor (size > 1) -> 0D scalar -> raises RuntimeError with size mismatch message In torch/nn/modules/module.py, _load_from_state_dict, added input_param.shape[0] == 1 check to the backward compatibility condition to only allow single-element 1D tensors. ## Tests Added test_scalar_param_1d_tensor_raises to verify that loading 1D tensors of size > 1 raises an error, while size 1 loads successfully. Pull Request resolved: pytorch#166335 Approved by: https://github.com/mikaylagawarecki
Fixes #165873
Title
Fix load_state_dict: raise error for 1D (size > 1) -> 0D parameter loads
Summary
This PR fixes a bug where loading a 1D tensor (size > 1) into a scalar (0D) parameter would silently take the first element instead of raising an error. The fix preserves backward compatibility for 1D tensors of size 1 while catching genuine shape mismatches.
Motivation
Previously, loading a 1D tensor like torch.randn(32000) into a 0D scalar parameter would silently slice the first element, leading to silent data loss and potential bugs. This change ensures users get a clear error when there's a genuine shape mismatch.
Behavior change
Before:
1D tensor (any length) -> 0D scalar -> silently coerced using input_param[0]
After:
In torch/nn/modules/module.py, _load_from_state_dict, added input_param.shape[0] == 1 check to the backward compatibility condition to only allow single-element 1D tensors.
Tests
Added test_scalar_param_1d_tensor_raises to verify that loading 1D tensors of size > 1 raises an error, while size 1 loads successfully.