Fixed: RuntimeError: MPS backend out of memory
Training on a Mac's GPU stopped with RuntimeError: MPS backend out of memory. PyTorch had been asked for more GPU memory than the process was allowed to take. A smaller batch fixed it, and the numbers in the message show how much smaller it has to be.
What I Was Doing
I was timing training steps of a small language model with PyTorch 2.15.0 on an Apple M1 Pro. The model ran on the Mac's GPU, which PyTorch calls MPS. I had set a memory limit for the process on purpose, so that a run that's too big would fail at once instead of slowing down the whole Mac. A batch of 32 ran. A batch of 64 stopped with the error.
The smallest script that shows the same thing makes one batch of random images on the GPU. It first lowers the limit to a tenth of the memory macOS recommends, so the error shows on any Mac without filling its memory. The project is a uv project with PyTorch and NumPy in it (How to Install PyTorch with uv).
import torch
# Let this process use a tenth of the GPU memory that macOS recommends.
torch.mps.set_per_process_memory_fraction(0.1)
batch_size = 512
images = torch.rand(batch_size, 3, 512, 512, device="mps")
print(f"{batch_size} images use {torch.mps.current_allocated_memory() / 1024**3:.2f} GiB")
torch.rand(batch_size, 3, 512, 512, device="mps")makes 512 images of 512 by 512 pixels, with 3 colour values per pixel, filled with random numbers. It makes them directly on the GPU.torch.mps.current_allocated_memory()returns the GPU memory that tensors use, in bytes. Dividing by1024**3turns bytes into GiB.
uv run python batch_memory.py
What I Expected
One line that says how much memory the batch uses:
512 images use 1.50 GiB
What I Got
Traceback (most recent call last):
File "/Users/you/source/mps-memory/batch_memory.py", line 7, in <module>
images = torch.rand(batch_size, 3, 512, 512, device="mps")
RuntimeError: MPS backend out of memory (MPS allocated: 0 bytes, other allocations: 384.00 KiB, max allowed: 1.18 GiB). Tried to allocate 1.50 GiB on shared pool. Use PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.0 to disable upper limit for memory allocations (may cause system failure).

The message carries four numbers:
| Part of the message | Here | What it is |
|---|---|---|
MPS allocated | 0 bytes | GPU memory that PyTorch already holds. Nothing yet, because this was the first tensor |
other allocations | 384.00 KiB | GPU memory the process holds outside PyTorch's own memory pools |
max allowed | 1.18 GiB | The limit for this process |
Tried to allocate | 1.50 GiB | The request that didn't fit |
Here the request alone was bigger than the limit. In my timing run the same message read MPS allocated: 4.08 GiB, max allowed: 4.14 GiB and Tried to allocate 72.00 MiB: the memory was nearly full, and a small request was one too many. That's the usual picture in the middle of training.
What I Checked
A second script prints where the limit comes from and what the batch needs:
import torch
GIB = 1024**3
recommended = torch.mps.recommended_max_memory()
print(f"recommended maximum: {recommended / GIB:.2f} GiB")
print(f"a tenth of it: {0.1 * recommended / GIB:.2f} GiB")
print(f"512 images need: {512 * 3 * 512 * 512 * 4 / GIB:.2f} GiB")
print(f"256 images need: {256 * 3 * 512 * 512 * 4 / GIB:.2f} GiB")
uv run python memory_numbers.py
recommended maximum: 11.84 GiB
a tenth of it: 1.18 GiB
512 images need: 1.50 GiB
256 images need: 0.75 GiB

- The recommended maximum is a number PyTorch reads from macOS. The docs say
torch.mps.recommended_max_memory()"Returns recommended max Working set size for GPU memory in bytes." On this Mac it's 11.84 GiB. - The limit is that number times the fraction my script set: a tenth, 1.18 GiB. That's the
max allowedin the message. - The need is plain arithmetic. Each image holds 3 × 512 × 512 numbers, and each number takes 4 bytes. 512 images need 1.50 GiB, which is the
Tried to allocatein the message. 256 images need 0.75 GiB.
Without a limit of your own, PyTorch still has one. Its MPS environment variables page says of PYTORCH_MPS_HIGH_WATERMARK_RATIO: "High watermark ratio for MPS allocator. Default is 1.7." It adds that the "high watermark ratio is a hard limit for the total allowed allocations", and that a ratio of 1.0 means the recommended maximum. So the default limit on this Mac is 1.7 × 11.84 GiB, about 20 GiB. The Mac has 16 GB of memory in all, so a run can slow the whole Mac down long before PyTorch stops it. That's why I set a lower limit.
Why It Happens
On a Mac with Apple silicon, the GPU shares the computer's memory with everything else that runs. PyTorch therefore keeps a hard limit for each process. When a request would take the process past that limit, PyTorch raises MPS backend out of memory instead of asking macOS for more. The docs of torch.mps.set_per_process_memory_fraction say it in one sentence: "If trying to allocate more than the allowed value in a process, it will raise an out of memory error in allocator."
Two things push a process to the limit:
- One step needs too much at once. The batch, the inputs or the model are too big. This was my case.
- Memory grows from step to step. The script keeps tensors it no longer needs. That case has its own section below.
The Fix
Make the batch smaller. The memory a batch needs grows in step with its size, so half the batch needs half the memory. In batch_memory.py, change one line:
batch_size = 256
After the Fix
uv run python batch_memory.py
256 images use 0.75 GiB

The batch fits under the 1.18 GiB limit and the script runs to its end. In my timing run, the process with the batch of 32 held 3.11 GiB of GPU memory, under the 4.14 GiB limit that the batch of 64 had hit.
When Memory Grows With Every Step
A batch that fits can still end in the same error many steps later, when the script keeps every tensor it makes. This script puts six batches of 256 images into a list, with the limit at three tenths of the recommended memory:
import torch
torch.mps.set_per_process_memory_fraction(0.3)
batches = []
for number in range(1, 7):
batches.append(torch.rand(256, 3, 512, 512, device="mps"))
print(f"batch {number}: {torch.mps.current_allocated_memory() / 1024**3:.2f} GiB in use")
uv run python keep_batches.py
batch 1: 0.75 GiB in use
batch 2: 1.50 GiB in use
batch 3: 2.25 GiB in use
batch 4: 3.00 GiB in use
Traceback (most recent call last):
File "/Users/you/source/mps-memory/keep_batches.py", line 7, in <module>
batches.append(torch.rand(256, 3, 512, 512, device="mps"))
~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: MPS backend out of memory (MPS allocated: 3.00 GiB, other allocations: 400.00 KiB, max allowed: 3.55 GiB). Tried to allocate 768.00 MiB on shared pool. Use PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.0 to disable upper limit for memory allocations (may cause system failure).

Every batch stays alive in the list, so the memory in use grows by 0.75 GiB per batch. The fifth batch would take it past 3.55 GiB.
The fix is to keep only what's needed. This version gives each new batch the same name, so Python frees the old batch when the next one is made:
import torch
torch.mps.set_per_process_memory_fraction(0.3)
for number in range(1, 7):
batch = torch.rand(256, 3, 512, 512, device="mps")
print(f"batch {number}: {torch.mps.current_allocated_memory() / 1024**3:.2f} GiB in use")
uv run python drop_batches.py
batch 1: 0.75 GiB in use
batch 2: 0.75 GiB in use
batch 3: 0.75 GiB in use
batch 4: 0.75 GiB in use
batch 5: 0.75 GiB in use
batch 6: 0.75 GiB in use

All six batches ran, and the memory stayed at 0.75 GiB.
In a training loop, the usual way to keep tensors by accident is to collect each step's loss in a list or a running total. PyTorch's own Frequently Asked Questions put it first in their list of things to check: "Don't accumulate history across your training loop." Collect plain Python numbers instead (How to Get a Python Number from a PyTorch Tensor with item()).
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Removing the limit as the first step | With no limit, a run that needs more memory than the Mac has isn't stopped any more. The docs warn that a ratio of 0.0 "may cause system failure if system-wide OOM occurs" | Make the batch smaller first |
| Setting the high watermark ratio to 0.0 for a script that sets its own limit | batch_memory.py with 512 images still stopped with max allowed: 1.18 GiB: the limit set in the code won | Change the limit in the code, or take that line out |
Expecting del to hand memory back to macOS | After del images, the tensors used 0.00 GiB, but the process still held 0.75 GiB of GPU memory | Call torch.mps.empty_cache() after it. Then the process held 0.00 GiB |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
The error on the first big tensor, with MPS allocated: 0 bytes | One request is larger than the limit | A smaller batch or smaller inputs |
The error after many steps, with MPS allocated close to max allowed | Tensors from earlier steps are still alive | Keep only what you need, as in drop_batches.py |
RuntimeError: invalid low watermark ratio 1.4 after setting the high watermark ratio | The ratio is below the low watermark's default | Fixed: RuntimeError: invalid low watermark ratio 1.4 |
FAQ
Should I set PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.0, as the message says?
Only when you know the run fits into the Mac's memory. The variable removes the limit. I took the limit out of the script and set it from the terminal instead, at a tenth: the 512 images failed as before. With PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.0 in front of the command, that script printed 512 images use 1.50 GiB. That was safe because 1.50 GiB is small. For a run that needs more than the Mac has, the limit is what stops it in time.
Why does the message say that more is allocated than my tensors use?
PyTorch takes GPU memory from macOS in chunks and counts whole chunks. When I kept batches of 64 images, 0.19 GiB each, the tensors used 3.00 GiB after 16 batches, and the message of the 17th said MPS allocated: 4.00 GiB. PyTorch's source says allocations of that size "may be packed in 1 GiB heaps", and five of those batches fit into one, so 16 batches took four of them. An open report on PyTorch's tracker, issue #205359, describes how such a chunk can end a run early.
Does torch.mps.empty_cache() fix it?
It didn't change anything in my test. I made four batches of 256 images, 3.00 GiB, and deleted them. The process still held the 3.00 GiB, because PyTorch keeps freed memory for reuse. A new batch of 1,000 images, 2.94 GiB, then fit under the 3.55 GiB limit, with and without torch.mps.empty_cache() in between: PyTorch reused its own kept memory. What the call does is give that memory back to macOS. The docs say it "Releases all unoccupied cached memory currently held by the caching allocator so that those can be used in other GPU applications." PyTorch's tracker has an open report where it did help, issue #105839, so it's worth one try.
Can I catch the error and try a smaller batch?
Yes. I put the line that makes the batch in a try block inside a loop, and halved batch_size in except RuntimeError. The script printed 512 images didn't fit, trying 256, then 256 images use 0.75 GiB.
Does a smaller batch make training slower?
Not in my timing run. A model with 5.9 million parameters processed 38,572 tokens per second with a batch of 16, 38,281 with 32 and 36,055 with 64. The GPU was busy in every case, so the batch size was a choice about memory, not about speed.
Conclusion
RuntimeError: MPS backend out of memory means a request would have taken the process past PyTorch's GPU memory limit. The message names the limit (max allowed) and the request (Tried to allocate). Halving the batch from 512 to 256 images brought the need from 1.50 GiB to 0.75 GiB, under the 1.18 GiB limit, and the script ran.
Additional Resources
- MPS Environment Variables —
PYTORCH_MPS_HIGH_WATERMARK_RATIO, its default and what 0.0 does. - torch.mps.set_per_process_memory_fraction — setting the limit in code.
- torch.mps.empty_cache — giving kept memory back to macOS.
- Frequently Asked Questions — PyTorch's own list of things to check when a model runs out of memory.
- MPSAllocator.h — the chunk sizes PyTorch uses for GPU memory on a Mac.
