Skip to content

[MPS] Fix unary/binary ops for 2**32+ elem tensors - #155183

Closed
malfet wants to merge 4 commits into
gh/malfet/379/basefrom
gh/malfet/379/head
Closed

malfet wants to merge 4 commits into
gh/malfet/379/basefrom
gh/malfet/379/head

Conversation

@malfet

@malfet malfet commented Jun 5, 2025 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

By using TensorIterator::with_32bit_indexing() primitive

Add bind_tensors helper function that correctly sets up MPS tensors originating from TensorIterator

TODO: Add comments to bind_tensors as well asunit test, based on

python  -c "import torch;print((torch.rand(1, 1024, 1024, dtype=torch.bfloat16, device='mps') + torch.rand(5000, 1, 1, dtype=torch.bfloat16, device='mps')).sin())"

Fixes #154828

[ghstack-poisoned]
@malfet
malfet requested a review from kulinseth as a code owner June 5, 2025 01:21
@pytorch-bot

pytorch-bot Bot commented Jun 5, 2025 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 3 Cancelled Jobs, 2 Pending

As of commit 80f5d62 with merge base 31d12b3 (image):

CANCELLED JOBS - The following jobs were cancelled. Please retry:

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 Jun 5, 2025
@malfet
malfet requested review from Skylion007 and dcci June 5, 2025 01:21
@malfet malfet added the topic: bug fixes topic category label Jun 5, 2025
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
malfet added a commit that referenced this pull request Jun 5, 2025
By using `TensorIterator..with_32bit_indexing()` primitive

Add `bind_tensors` helper function that correctly sets up MPS tensors originating from TensorIterator

TODO: Add unit test, based on
```
python  -c "import torch;print((torch.rand(1, 1024, 1024, dtype=torch.bfloat16, device='mps') + torch.rand(5000, 1, 1, dtype=torch.bfloat16, device='mps')).sin())"
```

ghstack-source-id: dd9ed1b
Pull Request resolved: #155183
@malfet

malfet commented Jun 5, 2025

Copy link
Copy Markdown
Collaborator Author

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

@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

pytorchmergebot pushed a commit that referenced this pull request Oct 23, 2026
)

## Linked issue or supporting maintainer

Fixes #201835

## BC-breaking?

No

## Summary (human written only)

<!-- Point to the issue for relevant design discussion. If not discussed there (rare), add up to one paragraph. Overly verbose descriptions will be considered spam. -->
The test_mps function test_64bits_binops assumes <= 16 GiB of GPU Mem is required, however, this is not the case due to broadcasting where  a + b makes a 9.77 GiB tensor and another .sin() makes a second 9.77 GiB, where the sum becomes 9.77 GiB * 2 > 16 GiB.

Using .sin_() writes the result back to the first tensor, so it only use a total of 9.77 GiB of memory well below 16 GiB.

From the code, it continues the test if GPU Mem > 16 GiB which is not true.
https://github.com/pytorch/pytorch/blob/3c53c6fe501fd43f2ae33490b0aee276c6e0db6e/test/test_mps.py#L10531-L10532

## Agent details (if applicable)

> Prepared with Claude Code (Opus 5.5) at my request; the commands below were run on my M4 Pro.
>
> Test-only change in `test/test_mps.py`. `test_64bit_binops` kept `a + b` and `(a + b).sin()` alive together (2 x 9.77 GiB), above the 17.76 GiB GPU working set of a 24 GB Mac and at 92% of the 21.3 GiB working set of CI's M2 runners, where it was disabled as flaky (#190665). It now computes the sine in place and frees its tensors at the end, like the other `TestLargeTensors` tests.
>
> Tested on an Apple M4 Pro (24 GB, macOS 27.0.1):
>
> ```
> PYTORCH_TEST_WITH_SLOW=1 python test/test_mps.py TestLargeTensors.test_64bit_binops
> PYTORCH_TEST_WITH_SLOW=1 python test/test_mps.py TestLargeTensors
> spin quicklint
> ```
>
> Before: `OutOfMemoryError: MPS command buffer execution failed: Insufficient Memory`. After: OK in 3 of 3 runs; the class passes and 2 to 4 of the 64-bit `cat` tests that follow no longer skip for memory. Coverage check: with `exec_unary_kernel` temporarily changed to dispatch only its first 32-bit chunk, the patched test fails (98.6% of the checked elements wrong), so it still covers #155183.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

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

Co-authored-by: Claude Opus 5.5 <[email protected]>
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 release notes: mps Release notes category topic: bug fixes topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants