Repository navigation
Conversation
🔗 Helpful links
❌ 9 New Failures, 57 PendingAs of commit c0fddd7 (more details on the Dr. CI page): Expand to see more
🕵️ 9 new failures recognized by patternsThe following CI failures do not appear to be due to upstream breakages:
|
7f670dd to
20d5bec
Compare
20d5bec to
d858ffe
Compare
albanD
left a comment
There was a problem hiding this comment.
Did docs and test except test_mps.
9138073 to
d071e8d
Compare
|
@albanD has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
1 similar comment
|
@albanD has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
albanD
left a comment
There was a problem hiding this comment.
test_mps.py will re-quire a post-merge cleanup as well
4777cf3 to
fee8ec9
Compare
|
@pytorchbot merge this please CI failures are due to bad handling of the label which makes the pytorch version |
|
Hey @kulinseth. |
Summary: Add PyTorch operations to MPS backend. - #77394 Pull Request resolved: #77343 Approved by: https://github.com/albanD Test Plan: contbuild & OSS CI, see https://hud.pytorch.org/commit/pytorch/pytorch/e011a8e18bf469a6a612fd1e7647159c353730a9 Reviewed By: gchanan Differential Revision: D36374948 fbshipit-source-id: 59f5ae225f07e16bf07083e2a73ae5de1e419b7c
|
hey @kulinseth, any thoughts on this issue. Am I missing something here? |
`mps_linear_backward` passed `grad_output.suggest_memory_format()` to `at::empty()` when allocating the weight and bias gradient tensors. When `grad_output` happened to have NHWC-like strides (e.g. from a permute chain), `suggest_memory_format()` returned `ChannelsLast`, which requires rank 4 — but the weight gradient is 2D and the bias gradient is 1D, triggering "required rank 4 tensor to use channels_last format". Replace the verbose `at::empty(size, scalar_type, nullopt, kMPS, nullopt, suggest_memory_format())` calls with `at::empty(size, grad_output.options())` which carries dtype and device without propagating an incorrect memory format. The bug has existed since the original MPS backend introduction in #77343. Fixes #178222 ghstack-source-id: c8e9c2a Pull-Request: #178278
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]>
Add PyTorch operations to MPS backend.