Skip to content

[MPS] Halve test_64bit_binops peak memory so it fits 24 GB Macs - #201836

Closed
lsyurea wants to merge 1 commit into
pytorch:mainfrom
lsyurea:mps/test-64bit-binops-memory
Closed

lsyurea wants to merge 1 commit into
pytorch:mainfrom
lsyurea:mps/test-64bit-binops-memory

Conversation

@lsyurea

@lsyurea lsyurea commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Linked issue or supporting maintainer

Fixes #201835

BC-breaking?

No

Summary (human written only)

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.

pytorch/test/test_mps.py

Lines 10531 to 10532 in 3c53c6f

if torch.mps.recommended_max_memory() < 16_000_000_000:
raise unittest.SkipTest("Needs at least 16Gb of RAM")

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

test_64bit_binops checks unary and binary ops on a 5000x1024x1024 fp16
tensor (9.77 GiB) to cover the 32-bit split added in pytorch#155183. Writing
`(a + b).sin()` keeps the sum and its sine alive together, a ~21 GB peak,
while the guard only requires recommended_max_memory() >= 16 GB. On a
24 GB Mac (17.76 GiB working set) the test fails every time with
kIOGPUCommandBufferCallbackErrorOutOfMemory. On CI's macos-m2-15 runners
it used 92% of their 21.3 GiB working set and was disabled as flaky in
pytorch#190665 after returning wrong values and running out of memory on reruns.

Compute the sine in place so only one 10 GB tensor is alive, and free the
tensors at the end like the other TestLargeTensors tests. sin_ goes through
the same split in exec_unary_kernel as sin. Raising the guard instead would
skip the test on 24 GB Macs and leave it at the edge on the CI runners.

Test Plan:

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, test_64bit_binops raised OutOfMemoryError. After, it passes in 3
of 3 runs, the class passes, and 2 to 4 of the 64-bit cat tests that run
after it no longer skip for insufficient memory. With exec_unary_kernel
temporarily changed to dispatch only its first 32-bit chunk, the patched
test fails with 98.6% of the checked elements wrong, so it still covers
the 64-bit unary path.

This change was authored with the help of an AI assistant (Claude Code).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@pytorch-bot

pytorch-bot Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

PR Status: Approved 🚀

Please fix all CI failures and trigger merge by commenting "@pytorchbot merge".

🔗 Helpful Links

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

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

✅ No Failures

As of commit 776d6a3 with merge base 3c53c6f (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) topic: not user facing topic category labels Oct 23, 2026
@pytorch-bot

pytorch-bot Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@lsyurea

lsyurea commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "topic: not user facing"

@linux-foundation-easycla

linux-foundation-easycla Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

One or more co-authors of this pull request were not found. You must specify co-authors in commit message trailer via:

Co-authored-by: name <email>

Supported Co-authored-by: formats include:

  1. Anything <[email protected]> - it will locate your GitHub user by id part.
  2. Anything <[email protected]> - it will locate your GitHub user by login part.
  3. Anything <public-email> - it will locate your GitHub user by public-email part. Note that this email must be made public on Github.
  4. Anything <other-email> - it will locate your GitHub user by other-email part but only if that email was used before for any other CLA as a main commit author.
  5. login <any-valid-email> - it will locate your GitHub user by login part, note that login part must be at least 3 characters long.

Alternatively, if the co-author should not be included, remove the Co-authored-by: line from the commit message.

Please update your commit message(s) by doing git commit --amend and then git push [--force] and then request re-running CLA check via commenting on this pull request:

/easycla

@lsyurea
lsyurea marked this pull request as ready for review October 23, 2026 10:00
@github-actions github-actions Bot added bot-triaged This is a label only to be used by the auto triage bot missing actionable issue A PR without a linked actionable issue or maintainer sponsor triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Oct 23, 2026
@Isalia20

Copy link
Copy Markdown
Collaborator

Please follow contributing guidelines

@lsyurea
lsyurea marked this pull request as draft October 23, 2026 15:24
@lsyurea

lsyurea commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

alright, I have changed it back to draft and I'll wait for #201835 to be marked actionable :)

Comment thread test/test_mps.py
rc_slice_cpu = (a.cpu() + b.cpu()[slice_idx:]).sin()
self.assertEqual(rc_slice, rc_slice_cpu)
del a, b, rc, rc_slice
gc.collect()

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.

Why do you need this one? I.e. there are no reference cycles and no backward, so del should free them right away

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thats true, no harm using it, but there is no need to add this. The main issue is the torch.mps.recommended_max_memory() < 16_000_000_000 which was initially not entirely correct.

@lsyurea
lsyurea marked this pull request as ready for review October 23, 2026 15:56
@lsyurea

lsyurea commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Oct 23, 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

@lsyurea
lsyurea deleted the mps/test-64bit-binops-memory branch October 30, 2026 10:19

This branch was previously deployed

1 inactive deployment
bedrock — 776d6a3b Deployed Oct 23, 2026 by lsyurea via analyze #13236
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot-triaged This is a label only to be used by the auto triage bot ciflow/mps Run MPS tests (subset of trunk) ciflow/trunk Trigger trunk jobs on your pull request Merged missing actionable issue A PR without a linked actionable issue or maintainer sponsor open source topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MPS] test_64bit_binops needs ~21 GB of GPU memory but only skips below 16 GB,

5 participants