Repository navigation
Conversation
kulinseth
commented
Jul 11, 2022
- Add test for NLL 1d
- Fix forward NLL for 1D case
- Handle NLL backward for 1d
🔗 Helpful links
❌ 1 New FailuresAs of commit 3580c64 (more details on the Dr. CI page): Expand to see more
🕵️ 1 new failure recognized by patternsThe following CI failures do not appear to be due to upstream breakages
|
|
@pytorchbot merge |
|
@pytorchbot successfully started a merge job. Check the current status here |
|
Merge failed due to Refusing to merge as mandatory check(s) pull failed for rule MPS |
|
@pytorchbot rebase |
|
@pytorchbot successfully started a rebase job. Check the current status here |
* Add test for NLL 1d * Fix forward NLL for 1D case * Handle NLL backward for 1d * Fix handling of 0D target; remove newlines
|
Successfully rebased |
|
The failure is not related to this: |
|
@pytorchbot merge -f |
|
@pytorchbot successfully started a merge job. Check the current status here |
|
Hey @kulinseth. |
Summary: * Add test for NLL 1d * Fix forward NLL for 1D case * Handle NLL backward for 1d Pull Request resolved: #81290 Approved by: https://github.com/razarmehr Test Plan: contbuild & OSS CI, see https://hud.pytorch.org/commit/pytorch/pytorch/ae83e44c5fe5c58d9077f7ac82724bf766a75b57 Reviewed By: DanilBaibak Differential Revision: D37813284 Pulled By: DanilBaibak fbshipit-source-id: cb111ed1d141f29bf9079eeb1c9b1c9d44ad2400
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 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 (#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: ```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 (#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 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 #195389 Fixes #195390 Fixes #195391 Fixes #195392 🤖 Generated with [Claude Code](https://claude.com/claude-code) Pull Request resolved: #195411 Approved by: https://github.com/Isalia20, https://github.com/Skylion007 Co-authored-by: Claude Opus 4.8 <[email protected]>
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]>
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]>