KEMBAR78
[MPS] Add mps keys to `indices` and `values` ops by Isalia20 · Pull Request #160223 · pytorch/pytorch · GitHub
Skip to content

Conversation

@Isalia20
Copy link
Collaborator

@Isalia20 Isalia20 commented Aug 8, 2025

@pytorch-bot
Copy link

pytorch-bot bot commented Aug 8, 2025

🔗 Helpful Links

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

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

✅ No Failures

As of commit 7be6762 with merge base 2247aa6 (image):
💚 Looks good so far! There are no failures yet. 💚

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

@Isalia20 Isalia20 added module: mps Related to Apple Metal Performance Shaders framework ciflow/mps Run MPS tests (subset of trunk) module: sparse Related to torch.sparse labels Aug 8, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Aug 8, 2025

Attention! native_functions.yaml was changed

If you are adding a new function or defaulted argument to native_functions.yaml, you cannot use it from pre-existing Python frontend code until our FC window passes (two weeks). Split your PR into two PRs, one which adds the new C++ functionality, and one that makes use of it from Python, and land them two weeks apart. See https://github.com/pytorch/pytorch/wiki/PyTorch's-Python-Frontend-Backward-and-Forward-Compatibility-Policy#forwards-compatibility-fc for more info.


Caused by:

Copy link
Collaborator

@pearu pearu left a comment

Choose a reason for hiding this comment

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

LGTM. Could you also add tests for indices/values? Say, by extending TestSparseMPS.test_sparse_coo_tensor_with_dims_and_tensors with

        self.assertEqual(t.indices().cpu(), indices.cpu())
        self.assertEqual(t.values().cpu(), values.cpu())

@Isalia20
Copy link
Collaborator Author

Isalia20 commented Aug 9, 2025

I don't think that test is needed. indices() and values() do not come from a custom implementation and I would basically be comparing two same things to each other, just being dispatched from different devices.

@pearu
Copy link
Collaborator

pearu commented Aug 9, 2025

I don't think that test is needed. indices() and values() do not come from a custom implementation

These tests are needed for sanity. Note that indices/values belong to the public API, not _indices/_values.

and I would basically be comparing two same things to each other, ...

Isn't that true for most tests? :)

@pearu
Copy link
Collaborator

pearu commented Aug 9, 2025

..., just being dispatched from different devices.

This is also the reason why these tests are needed as this PR adds a feature to the dispatching system.

@Isalia20
Copy link
Collaborator Author

Isalia20 commented Aug 9, 2025

It's not true for most of the tests. Tests which compare outputs between device and cpu, in my opinion, should be added when you have a different implementation for it. i.e. see implementation for coalesce_sparse_mps. coalesce_sparse_mps and coalesce_sparse_cpu have different underlying implementations/functions they call, thus the need to check for the outputs to match. Whenever you dispatch the function to the same underlying function which is the case for indices and values, test comparing cpu output and mps output isn't needed.

Also one of the reasons tests get added is to make sure that in the future people don't break things, here you can't physically break this implementation since you will be breaking both mps/cpu/cuda implementation if you change the underlying function.

@Isalia20
Copy link
Collaborator Author

Isalia20 commented Aug 9, 2025

@malfet What do you think?

@pearu
Copy link
Collaborator

pearu commented Aug 9, 2025

Also one of the reasons tests get added is to make sure that in the future people don't break things, here you can't physically break this implementation since you will be breaking both mps/cpu/cuda implementation if you change the underlying function.

There exists a number of ways to break a code that relies on the feature that this PR introduces without pytorch CI discovering the breakage. A trivial example is a future PR that removes the dispatch keys introduced here. Or a PR that provides a MPS-specific-but-buggy implementation for indices/values. Etc. While these examples may be artificial (or are they? This is considering that people will use/already use AI to generate PRs) in the sense that the review process should prevent landing such PRs. A failing CI would save everyone's time by making such future PRs unacceptable.

@Isalia20 Isalia20 requested a review from malfet August 10, 2025 12:45
Copy link
Contributor

@malfet malfet left a comment

Choose a reason for hiding this comment

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

It would be nice to have some testing, but adding something to dispatch guess will not hurt, so I guess in followup PRs I expect you'll enable running test_sparse but skip majority of test there for MPS backend

@malfet malfet changed the title [MPS] Sparse enable indices and values [MPS] Add mps keys to indices and values ops Aug 12, 2025
@malfet
Copy link
Contributor

malfet commented Aug 12, 2025

@pytorchbot merge -f "This seems fine"

@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

chuanhaozhuge pushed a commit that referenced this pull request Aug 14, 2025
enable indices and values on sparse mps

Pull Request resolved: #160223
Approved by: https://github.com/malfet
chuanhaozhuge pushed a commit that referenced this pull request Aug 18, 2025
enable indices and values on sparse mps

Pull Request resolved: #160223
Approved by: https://github.com/malfet
can-gaa-hou pushed a commit to can-gaa-hou/pytorch that referenced this pull request Aug 22, 2025
enable indices and values on sparse mps

Pull Request resolved: pytorch#160223
Approved by: https://github.com/malfet
markc-614 pushed a commit to markc-614/pytorch that referenced this pull request Sep 17, 2025
enable indices and values on sparse mps

Pull Request resolved: pytorch#160223
Approved by: https://github.com/malfet
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 module: mps Related to Apple Metal Performance Shaders framework module: sparse Related to torch.sparse open source

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants