-
Notifications
You must be signed in to change notification settings - Fork 25.7k
NLLLoss: validate target is 0D when input is 1D #161412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/161412
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 1398d84 with merge base c321111 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't submit BC-breaking changes. When you think documentation contradicts current behavior, changes should be made to documentation, not behavior. In this case, there is logic in implementation that sets batch_size to 1 if input is 1d, and there was a specific check in the file you were changing https://github.com/pytorch/pytorch/pull/161412/files#diff-0b104a4612220d611b2c5d76272c28ddb21efbd8f9b9ae29d7f5b6c9e2a9be1fR53 to make sure inputs are valid
|
Hello all, thanks for the reviews and clarifications. Examples: Valid case: input = torch.randn(10) # [C] -> Internally [1,C]
target = torch.tensor(3) # scalar, class 3 is the correct label.
F.nll_loss(input, target) # works
Invalid case: # 1D input and 1D target of same length
input1 = torch.randn(10) # shape [10] -> internally [1,10]
target1 = torch.randint(0, 10, (10,), dtype=torch.long) # shape [10]
F.nll_loss(input1, target1)
Next Step Given this, what would be the recommended approach for the PR?
Happy to adjust the PR based on guidance. |
|
neither. We could make the 1d-1d shape check correct, but we won't limit target to 1d, and we won'd deprecate. |
|
I've updated the check as per the comment. Also, one more observation, when both the input and target tensors are 1D, the NLL loss function only uses the value at index 0 of the target tensor. The remaining values in the target tensor are ignored. |
|
Right, that's why we need better shape checks (to make sure target has 1 element in this case) |
|
I've updated the shape check. Could you please review. |
|
@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 |
|
@pytorchbot rebase -b main |
|
@pytorchbot started a rebase job onto refs/remotes/origin/main. Check the current status here |
Updating the UT assert message with ValueError instead of RuntimeError.
|
Successfully rebased |
3534048 to
1398d84
Compare
|
@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 |
Add a shape check in nll_loss_forward to error out when both input and target are 1D. Added a unit test to cover the incompatible 1D/1D case. Fixes pytorch#157420 Pull Request resolved: pytorch#161412 Approved by: https://github.com/ngimel
|
PyTorch used to allow 1d size 0 target with 1d size 0 input, is it intentional that this PR blocks this? It's a corner case, just wondering. I guess part of it is that there is no 0 element 0 dim tensor, but that's just how dimensions work. |
Add a shape check in nll_loss_forward to error out when both input and target are 1D. Added a unit test to cover the incompatible 1D/1D case. Fixes pytorch#157420 Pull Request resolved: pytorch#161412 Approved by: https://github.com/ngimel
Add a shape check in nll_loss_forward to error out when both input and target are 1D. Added a unit test to cover the incompatible 1D/1D case. Fixes pytorch#157420 Pull Request resolved: pytorch#161412 Approved by: https://github.com/ngimel
Add a shape check in nll_loss_forward to error out when both input and target are 1D. Added a unit test to cover the incompatible 1D/1D case. Fixes pytorch#157420 Pull Request resolved: pytorch#161412 Approved by: https://github.com/ngimel
Add a shape check in nll_loss_forward to error out when both input and target are 1D. Added a unit test to cover the incompatible 1D/1D case. Fixes pytorch#157420 Pull Request resolved: pytorch#161412 Approved by: https://github.com/ngimel
Add a shape check in nll_loss_forward to error out when both input and target are 1D. Added a unit test to cover the incompatible 1D/1D case.
Fixes #157420
cc @albanD @ngimel @peterbell10 @cyyever @kurtamohler