Skip to content

[MPS] Fix linear backward crash with channels_last grad - #178278

Closed
malfet wants to merge 1 commit into
gh/malfet/778/basefrom
gh/malfet/778/head
Closed

malfet wants to merge 1 commit into
gh/malfet/778/basefrom
gh/malfet/778/head

Conversation

@malfet

@malfet malfet commented Mar 24, 2026 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

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". This logic existed there since the inception of MPS backend in https://github.com/pytorch/pytorch/77343

Replace verbose at::empty(size, scalar_type, nullopt, kMPS, nullopt, suggest_memory_format()) with more compact at::empty(size, grad_output.options())

Fixes #178222

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Mar 24, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/178278

Note: Links to docs will display an error until the docs builds have been completed.

⏳ No Failures, 36 Pending

As of commit 61eb42c with merge base 60cc45c (image):
💚 Looks good so far! There are no failures yet. 💚

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

@pytorch-bot pytorch-bot Bot added ciflow/mps Run MPS tests (subset of trunk) release notes: mps Release notes category labels Mar 24, 2026
malfet added a commit that referenced this pull request Mar 24, 2026
`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
@malfet malfet added the topic: bug fixes topic category label Mar 24, 2026

Tensor output = at::empty(
input_size, grad_output.scalar_type(), std::nullopt, kMPS, std::nullopt, grad_output.suggest_memory_format());
Tensor output = at::empty(input_size, grad_output.options());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this copy the layout by default when passing options or just makes it contiguous?

@Skylion007

Copy link
Copy Markdown
Collaborator

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Mar 24, 2026
@pytorchmergebot

Copy link
Copy Markdown
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

@malfet

malfet commented Mar 24, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge -f "Lint + MPS are green"

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

The merge job was canceled or timed out. This most often happen if two merge requests were issued for the same PR, or if merge job was waiting for more than 6 hours for tests to finish. In later case, please do not hesitate to reissue the merge command
For more information see pytorch-bot wiki.

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

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

Copilot AI pushed a commit that referenced this pull request Mar 27, 2026
`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". This logic existed there since the inception of MPS backend in https://github.com/pytorch/pytorch/77343

Replace verbose `at::empty(size, scalar_type, nullopt, kMPS, nullopt,
suggest_memory_format())` with more compact `at::empty(size, grad_output.options())`

Fixes #178222
Pull Request resolved: #178278
Approved by: https://github.com/Skylion007

Co-authored-by: Xia-Weiwen <[email protected]>
AaronWang04 pushed a commit to AaronWang04/pytorch that referenced this pull request Mar 31, 2026
`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". This logic existed there since the inception of MPS backend in https://github.com/pytorch/pytorch/77343

Replace verbose `at::empty(size, scalar_type, nullopt, kMPS, nullopt,
suggest_memory_format())` with more compact `at::empty(size, grad_output.options())`

Fixes pytorch#178222
Pull Request resolved: pytorch#178278
Approved by: https://github.com/Skylion007
xuhancn pushed a commit to xuhancn/pytorch that referenced this pull request Apr 2, 2026
`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". This logic existed there since the inception of MPS backend in https://github.com/pytorch/pytorch/77343

Replace verbose `at::empty(size, scalar_type, nullopt, kMPS, nullopt,
suggest_memory_format())` with more compact `at::empty(size, grad_output.options())`

Fixes pytorch#178222
Pull Request resolved: pytorch#178278
Approved by: https://github.com/Skylion007
nklshy-aws pushed a commit to nklshy-aws/pytorch that referenced this pull request Apr 7, 2026
`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". This logic existed there since the inception of MPS backend in https://github.com/pytorch/pytorch/77343

Replace verbose `at::empty(size, scalar_type, nullopt, kMPS, nullopt,
suggest_memory_format())` with more compact `at::empty(size, grad_output.options())`

Fixes pytorch#178222
Pull Request resolved: pytorch#178278
Approved by: https://github.com/Skylion007
@github-actions
github-actions Bot deleted the gh/malfet/778/head branch April 24, 2026 02:25
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
`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". This logic existed there since the inception of MPS backend in https://github.com/pytorch/pytorch/77343

Replace verbose `at::empty(size, scalar_type, nullopt, kMPS, nullopt,
suggest_memory_format())` with more compact `at::empty(size, grad_output.options())`

Fixes pytorch#178222
Pull Request resolved: pytorch#178278
Approved by: https://github.com/Skylion007
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/mps Run MPS tests (subset of trunk) ciflow/trunk Trigger trunk jobs on your pull request Merged release notes: mps Release notes category topic: bug fixes topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants