KEMBAR78
NLLLoss: validate target is 0D when input is 1D by mansiag05 · Pull Request #161412 · pytorch/pytorch · GitHub
Skip to content

Conversation

@mansiag05
Copy link
Contributor

@mansiag05 mansiag05 commented Aug 25, 2025

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

@pytorch-bot pytorch-bot bot added the release notes: nn release notes category label Aug 25, 2025
@pytorch-bot
Copy link

pytorch-bot bot commented Aug 25, 2025

🔗 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 Failures

As of commit 1398d84 with merge base c321111 (image):
💚 Looks good so far! There are no failures yet. 💚

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

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Aug 25, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

Skylion007
Skylion007 previously approved these changes Aug 25, 2025
Copy link
Collaborator

@ngimel ngimel left a 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

@malfet malfet dismissed Skylion007’s stale review August 25, 2025 22:25

Let's think it's over

@mansiag05
Copy link
Contributor Author

mansiag05 commented Aug 26, 2025

Hello all, thanks for the reviews and clarifications.
I wanted to follow up with an example to clarify the edge case we are discussing.

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
  • Conceptually, nll_loss expects input.shape = [N, C], target.shape = [N].
  • When input is 1D ([C]), PyTorch internally treats it as a batch of size 1 ([1, C]). This means the target should be scalar (or [1]) to match the single batch sample.

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)
  • Here PyTorch currently does not raise an error, even though logically this is invalid: a batch of size 1 cannot have 10 labels.
  • This seems to be a quirk in the current shape-checking logic: the implementation treats the 1D input as batch size 1 internally but does not fully enforce that the target must be scalar. So it accidentally accepts [10] as a target, even though it doesn’t align with the “batch size =1” interpretation.

Next Step

Given this, what would be the recommended approach for the PR?

  1. Update the documentation to clearly explain that for 1D input, only scalar targets are valid?
  2. Or additionally introduce a deprecation warning / stricter error check for the invalid 1D input + 1D target case?

Happy to adjust the PR based on guidance.

@soulitzer soulitzer added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Aug 26, 2025
@ngimel
Copy link
Collaborator

ngimel commented Aug 26, 2025

neither. We could make the 1d-1d shape check correct, but we won't limit target to 1d, and we won'd deprecate.

@mansiag05
Copy link
Contributor Author

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.

@ngimel
Copy link
Collaborator

ngimel commented Sep 2, 2025

Right, that's why we need better shape checks (to make sure target has 1 element in this case)

@mansiag05
Copy link
Contributor Author

I've updated the shape check. Could you please review.

@mansiag05
Copy link
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Sep 5, 2025
@pytorchmergebot
Copy link
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

@pytorchmergebot
Copy link
Collaborator

@ngimel
Copy link
Collaborator

ngimel commented Sep 5, 2025

@pytorchbot rebase -b main

@pytorchmergebot
Copy link
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/main. Check the current status here

@pytorchmergebot
Copy link
Collaborator

Successfully rebased fix-issue-157420 onto refs/remotes/origin/main, please pull locally before adding more changes (for example, via git checkout fix-issue-157420 && git pull --rebase)

@pytorch-bot pytorch-bot bot removed the ciflow/trunk Trigger trunk jobs on your pull request label Sep 5, 2025
@mansiag05
Copy link
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Sep 6, 2025
@pytorchmergebot
Copy link
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

daisyden pushed a commit to daisyden/pytorch that referenced this pull request Sep 8, 2025
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
@t-vi
Copy link
Collaborator

t-vi commented Sep 9, 2025

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.

markc-614 pushed a commit to markc-614/pytorch that referenced this pull request Sep 17, 2025
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
mansiag05 added a commit to mansiag05/pytorch that referenced this pull request Sep 22, 2025
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
cleonard530 pushed a commit to cleonard530/pytorch that referenced this pull request Sep 22, 2025
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
dsashidh pushed a commit to dsashidh/pytorch that referenced this pull request Sep 26, 2025
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
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 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.

nll_loss gives result when both input and target are 1D tensor

7 participants