Repository navigation
Conversation
For 1D input nll_loss has a single loss element and only target[0] is used. The Metal kernel added in #195153 unconditionally dispatched target.numel() threads. When aten.nll_loss_backward is called directly with a 1D input and a multi-element target (as the inductor decomposition test does), the extra threads read the single-element grad_output out of bounds and scatter that garbage into grad_input[target[i]]. In clean memory the OOB read is zero so the result happens to match; when the caching allocator hands back a dirty buffer it is garbage (e.g. FLT_MAX), which is why test_nll_loss_backward_1d_input_mps was flaky. Fix: dispatch a single thread for 1D input, matching the CPU and decomposition semantics. The 2D and 2D-loss paths are unchanged since target.numel() already equals the number of loss elements there. The regression test backs grad_output with a larger sentinel-filled tensor and views out a single element, so the out-of-bounds read is a deterministic non-zero value rather than relying on allocator state; it fails before the fix and passes after. Test Plan: ``` python test/test_nn.py -k test_nll_loss_1d_input_backward python test/inductor/test_torchinductor.py GPUTests.test_nll_loss_backward_1d_input_mps python test/test_mps.py -k nll_loss ``` Fixes #195391 Fixes #195392 Authored with assistance from Claude (AI). Co-Authored-By: Claude Opus 4.8 <[email protected]>
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/195411
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 65e93bf with merge base 74ee2f4 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
malfet
marked this pull request as ready for review
August 31, 2026 05:52
Isalia20
approved these changes
Aug 31, 2026
Isalia20
left a comment
Collaborator
There was a problem hiding this comment.
Does this happen in training? Or if user invokes backward function directly?
Skylion007
approved these changes
Aug 31, 2026
Collaborator
Author
|
@pytorchbot merge |
Collaborator
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 |
This was referenced Aug 31, 2026
orrangetabby17
pushed a commit
to orrangetabby17/pytorch
that referenced
this pull request
Sep 2, 2026
For 1D input `nll_loss` has a single loss element and only `target[0]` is used. The Metal kernel added in pytorch#195153 unconditionally dispatched `target.numel()` threads. When `aten.nll_loss_backward` is called directly with a 1D input and a multi-element target (as the inductor decomposition test does), the extra threads read the single-element `grad_output` out of bounds and scatter that garbage into `grad_input[target[i]]`. In clean memory the out-of-bounds read is zero, so the result happens to match the reference; when the caching allocator hands back a dirty buffer it is garbage (e.g. `FLT_MAX`). That is why `test_nll_loss_backward_1d_input_mps` was flaky (pytorch#195391, pytorch#195392). ## How the previous (MPSGraph) op handled this The MPSGraph implementation that pytorch#195153 replaced reshaped a 1D input `[C]` to `[1, C]` (an explicit batch of one) up front: ```objc auto grad_input = grad_input_arg.dim() == 1 ? grad_input_arg.view({1, grad_input_arg.size(0)}) : grad_input_arg; ``` so the number of output rows was structurally pinned to 1, independent of the target's element count. The Metal migration dropped this normalization and instead derives the iteration count from `target.numel()`, which is where the out-of-bounds read comes from. Note this 1D handling was **not** present in the original MPS op (pytorch#77343) -- it was added later in pytorch#81290 ("[MPS] Handle 1D inputs for NLL") and then lost in the migration. ## Fix Dispatch a single thread for 1D input, matching the CPU and decomposition semantics ("the C++ kernel only uses `target[0]`") and re-instating the old `view({1, C})` invariant as a thread count. The 2D and 2D-loss paths are unchanged since `target.numel()` already equals the number of loss elements there. ## Test Added a device-generic regression test that backs `grad_output` with a larger sentinel-filled tensor and views out a single element, so the out-of-bounds read is a deterministic non-zero value rather than relying on allocator state. It fails before the fix and passes after (`@onlyAccelerator`; the CPU path is correct and used as the reference). Fixes pytorch#195389 Fixes pytorch#195390 Fixes pytorch#195391 Fixes pytorch#195392 🤖 Generated with [Claude Code](https://claude.com/claude-code) Pull Request resolved: pytorch#195411 Approved by: https://github.com/Isalia20, https://github.com/Skylion007 Co-authored-by: Claude Opus 4.8 <[email protected]>
Sanskaaar1
pushed a commit
to Sanskaaar1/pytorch
that referenced
this pull request
Sep 8, 2026
For 1D input `nll_loss` has a single loss element and only `target[0]` is used. The Metal kernel added in pytorch#195153 unconditionally dispatched `target.numel()` threads. When `aten.nll_loss_backward` is called directly with a 1D input and a multi-element target (as the inductor decomposition test does), the extra threads read the single-element `grad_output` out of bounds and scatter that garbage into `grad_input[target[i]]`. In clean memory the out-of-bounds read is zero, so the result happens to match the reference; when the caching allocator hands back a dirty buffer it is garbage (e.g. `FLT_MAX`). That is why `test_nll_loss_backward_1d_input_mps` was flaky (pytorch#195391, pytorch#195392). ## How the previous (MPSGraph) op handled this The MPSGraph implementation that pytorch#195153 replaced reshaped a 1D input `[C]` to `[1, C]` (an explicit batch of one) up front: ```objc auto grad_input = grad_input_arg.dim() == 1 ? grad_input_arg.view({1, grad_input_arg.size(0)}) : grad_input_arg; ``` so the number of output rows was structurally pinned to 1, independent of the target's element count. The Metal migration dropped this normalization and instead derives the iteration count from `target.numel()`, which is where the out-of-bounds read comes from. Note this 1D handling was **not** present in the original MPS op (pytorch#77343) -- it was added later in pytorch#81290 ("[MPS] Handle 1D inputs for NLL") and then lost in the migration. ## Fix Dispatch a single thread for 1D input, matching the CPU and decomposition semantics ("the C++ kernel only uses `target[0]`") and re-instating the old `view({1, C})` invariant as a thread count. The 2D and 2D-loss paths are unchanged since `target.numel()` already equals the number of loss elements there. ## Test Added a device-generic regression test that backs `grad_output` with a larger sentinel-filled tensor and views out a single element, so the out-of-bounds read is a deterministic non-zero value rather than relying on allocator state. It fails before the fix and passes after (`@onlyAccelerator`; the CPU path is correct and used as the reference). Fixes pytorch#195389 Fixes pytorch#195390 Fixes pytorch#195391 Fixes pytorch#195392 🤖 Generated with [Claude Code](https://claude.com/claude-code) Pull Request resolved: pytorch#195411 Approved by: https://github.com/Isalia20, https://github.com/Skylion007 Co-authored-by: Claude Opus 4.8 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
For 1D input
nll_losshas a single loss element and onlytarget[0]is used. The Metal kernel added in #195153 unconditionally dispatchedtarget.numel()threads. Whenaten.nll_loss_backwardis called directly with a 1D input and a multi-element target (as the inductor decomposition test does), the extra threads read the single-elementgrad_outputout of bounds and scatter that garbage intograd_input[target[i]].In clean memory the out-of-bounds read is zero, so the result happens to match the reference; when the caching allocator hands back a dirty buffer it is garbage (e.g.
FLT_MAX). That is whytest_nll_loss_backward_1d_input_mpswas flaky (#195391, #195392).How the previous (MPSGraph) op handled this
The MPSGraph implementation that #195153 replaced reshaped a 1D input
[C]to[1, C](an explicit batch of one) up front:so the number of output rows was structurally pinned to 1, independent of the target's element count. The Metal migration dropped this normalization and instead derives the iteration count from
target.numel(), which is where the out-of-bounds read comes from.Note this 1D handling was not present in the original MPS op (#77343) -- it was added later in #81290 ("[MPS] Handle 1D inputs for NLL") and then lost in the migration.
Fix
Dispatch a single thread for 1D input, matching the CPU and decomposition semantics ("the C++ kernel only uses
target[0]") and re-instating the oldview({1, C})invariant as a thread count. The 2D and 2D-loss paths are unchanged sincetarget.numel()already equals the number of loss elements there.Test
Added a device-generic regression test that backs
grad_outputwith a larger sentinel-filled tensor and views out a single element, so the out-of-bounds read is a deterministic non-zero value rather than relying on allocator state. It fails before the fix and passes after (@onlyAccelerator; the CPU path is correct and used as the reference).Fixes #195389
Fixes #195390
Fixes #195391
Fixes #195392
🤖 Generated with Claude Code