KEMBAR78
Fix constant_pad_nd_mps bug when pad is empty by can-gaa-hou · Pull Request #161149 · pytorch/pytorch · GitHub
Skip to content

Conversation

@can-gaa-hou
Copy link
Contributor

@can-gaa-hou can-gaa-hou commented Aug 21, 2025

Fixes #161066

There is a size check here, which causes the error.

TORCH_CHECK(
padding_size == 2 || padding_size == 4 || padding_size == 6, "invalid padding argument of size ", padding_size);

If the argument pad is empty, it will return the cloned tensor on CPU.

bool all_pads_non_positive = true;
auto c_input = self;
for (const auto i : c10::irange(l_diff, l_inp)) {
auto pad_idx = 2 * (l_inp - i - 1);
if (pad[pad_idx] < 0) {
c_input = c_input.narrow(i, -pad[pad_idx], c_input.size(i) + pad[pad_idx]);
} else if (pad[pad_idx] != 0) {
all_pads_non_positive = false;
}
if (pad[pad_idx + 1] < 0) {
c_input = c_input.narrow(i, 0, c_input.size(i) + pad[pad_idx + 1]);
} else if (pad[pad_idx + 1] != 0) {
all_pads_non_positive = false;
}
}
// if none of the pads are positive we can optimize and just return the result
// of calling .narrow() on the input
if (all_pads_non_positive) {
return c_input.clone();
}

Therefore, this PR fixes the empty padding argument error by checking the size first and returning a cloned tensor immediately if the padding size is 0.

@pytorch-bot
Copy link

pytorch-bot bot commented Aug 21, 2025

🔗 Helpful Links

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

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

✅ No Failures

As of commit f74b50d with merge base 39862ac (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 21, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: can-gaa-hou / name: can-gaa-hou (f74b50d)

@pytorch-bot pytorch-bot bot added the release notes: mps Release notes category label Aug 21, 2025
@can-gaa-hou can-gaa-hou marked this pull request as draft August 21, 2025 07:52
@can-gaa-hou can-gaa-hou reopened this Aug 21, 2025
@can-gaa-hou can-gaa-hou marked this pull request as ready for review August 21, 2025 08:09
@malfet malfet added the topic: bug fixes topic category label Aug 21, 2025
@malfet
Copy link
Contributor

malfet commented Aug 21, 2025

Thank you for the fix. It would be nice to add a unit test, but could be done in separate PR

@malfet malfet added the ciflow/mps Run MPS tests (subset of trunk) label Aug 21, 2025
@pytorch-bot
Copy link

pytorch-bot bot commented Aug 21, 2025

To add the ciflow label ciflow/mps please first approve the workflows that are awaiting approval (scroll to the bottom of this page).

This helps ensure we don't trigger CI on this PR until it is actually authorized to do so. Please ping one of the reviewers if you do not have access to approve and run workflows.

@pytorch-bot pytorch-bot bot removed the ciflow/mps Run MPS tests (subset of trunk) label Aug 21, 2025
@malfet malfet added the ciflow/mps Run MPS tests (subset of trunk) label Aug 21, 2025
@malfet
Copy link
Contributor

malfet commented Aug 21, 2025

@pytorchbot merge -f "Seems fine to me, will add fix in separate PR"

@pytorchmergebot
Copy link
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

pytorchmergebot pushed a commit that referenced this pull request Aug 23, 2025
Fixes #161066

This PR adds a simple testcase for constant_pad_nd on MPS as mentioned in #161149 (comment)

Pull Request resolved: #161259
Approved by: https://github.com/malfet

Co-authored-by: Nikita Shulga <2453524+malfet@users.noreply.github.com>
markc-614 pushed a commit to markc-614/pytorch that referenced this pull request Sep 17, 2025
Fixes pytorch#161066

There is a size check here, which causes the error.
https://github.com/pytorch/pytorch/blob/8ce81bcee1da294a34af0a90dc16483055e8c5a4/aten/src/ATen/native/mps/operations/Pad.mm#L39-L40

If the argument `pad` is empty, it will return the cloned tensor on CPU.

https://github.com/pytorch/pytorch/blob/8ce81bcee1da294a34af0a90dc16483055e8c5a4/aten/src/ATen/native/PadNd.cpp#L43-L64

Therefore, this PR fixes the empty padding argument error by checking the size first and returning a cloned tensor immediately if the padding size is 0.

Pull Request resolved: pytorch#161149
Approved by: https://github.com/malfet
markc-614 pushed a commit to markc-614/pytorch that referenced this pull request Sep 17, 2025
Fixes pytorch#161066

This PR adds a simple testcase for constant_pad_nd on MPS as mentioned in pytorch#161149 (comment)

Pull Request resolved: pytorch#161259
Approved by: https://github.com/malfet

Co-authored-by: Nikita Shulga <2453524+malfet@users.noreply.github.com>
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) Merged open source release notes: mps Release notes category topic: bug fixes topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MPS constant_pad_nd fails with empty pad list

4 participants