Skip to main content

Fixed: RuntimeError: invalid low watermark ratio 1.4

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

I set PYTORCH_MPS_HIGH_WATERMARK_RATIO to lower PyTorch's GPU memory limit on a Mac, and the script stopped with RuntimeError: invalid low watermark ratio 1.4. PyTorch has a second, low watermark that keeps its default of 1.4 and may not be above the high one. Setting the low one too fixed it.

What I Was Doing​

I wanted PyTorch 2.15.0 to stop a script that takes too much GPU memory on my Apple M1 Pro, and to set that limit from the terminal without changing the script. PyTorch calls the Mac's GPU MPS, and the variable for its hard memory limit is PYTORCH_MPS_HIGH_WATERMARK_RATIO. I set it to 0.5: half of the GPU memory macOS recommends for one process.

The script makes eight images of random numbers on the GPU and prints the memory they use. The project is a uv project with PyTorch and NumPy in it (How to Install PyTorch with uv).

low_watermark.py
import torch

images = torch.rand(8, 3, 512, 512, device="mps")
print(f"8 images use {torch.mps.current_allocated_memory() / 1024**2:.0f} MiB")
Terminal
PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.5 uv run python low_watermark.py

Writing NAME=value in front of a command sets that environment variable for this one command. MPS exists only on Macs, so the commands here are for the macOS terminal.

What I Expected​

Expected output
8 images use 24 MiB

What I Got​

Output
Traceback (most recent call last):
File "/Users/you/source/mps-memory/low_watermark.py", line 3, in <module>
images = torch.rand(8, 3, 512, 512, device="mps")
RuntimeError: invalid low watermark ratio 1.4

The terminal after the command with PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.5: a traceback at the line that makes the images on the GPU, ending in RuntimeError: invalid low watermark ratio 1.4.

The message names a low watermark and the number 1.4. I had set a high watermark, and I had typed 0.5.

What I Checked​

Without the variable, the script runs. uv run python low_watermark.py printed 8 images use 24 MiB. So the variable causes the error, not the script.

The documentation. PyTorch's MPS environment variables page lists two ratios:

  • PYTORCH_MPS_HIGH_WATERMARK_RATIO: "High watermark ratio for MPS allocator. Default is 1.7."
  • PYTORCH_MPS_LOW_WATERMARK_RATIO: "Low watermark ratio for MPS allocator. Default is 1.4 (unified) or 1.0 (discrete)."

Its note says the low ratio takes a "Value between 0 to m_high_watermark_ratio". So the low ratio may not be above the high one, and 1.4 is the low ratio's default on Apple silicon.

More values. I ran the same script with other settings:

High ratioLow ratioResult
1.39not setRuntimeError: invalid low watermark ratio 1.4
1.4not set8 images use 24 MiB
2.0not set8 images use 24 MiB
2.1not setRuntimeError: invalid high watermark ratio 2.1
0.0not set8 images use 24 MiB
1.01.2RuntimeError: invalid low watermark ratio 1.2
0.50.58 images use 24 MiB
0.50.08 images use 24 MiB

With the low ratio not set, every high ratio below 1.4 that I tried failed: 1.39 here, 0.5 above, and 0.1 as well. Only 0.0 ran. The number in the message is the low ratio that was refused.

Why It Happens​

PyTorch keeps two marks for the GPU memory of a process on a Mac:

  • The high watermark is the hard limit. The docs call it "a hard limit for the total allowed allocations". A request that would go past it fails with an out-of-memory error.
  • The low watermark is a soft one. The docs describe it as "a soft limit to attempt limiting memory allocations up to the lower watermark level". Past it, PyTorch tries to hold the memory down, for example by giving back memory it keeps for reuse.

When the script first uses the GPU, PyTorch reads both variables and checks that the low ratio isn't above the high one. A variable you don't set keeps its default. With my high ratio of 0.5 and the default low ratio of 1.4, the check failed, and the message names the value that failed it: 1.4. That's also why the traceback points at the line that makes the first tensor on the GPU, and not at the start of the script. The check is in PyTorch's MPSAllocator.mm.

A high ratio of 0.0 is the exception, because it means no limit at all. The docs: "0.0 : disables high watermark limit (may cause system failure if system-wide OOM occurs)".

The Fix​

Set the low ratio as well, at or below the high one:

Terminal
PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.5 PYTORCH_MPS_LOW_WATERMARK_RATIO=0.4 uv run python low_watermark.py

After the Fix​

Output
8 images use 24 MiB

The terminal after the command with both ratios set, 0.5 and 0.4: 8 images use 24 MiB, with no error.

The script runs, and the limit holds. With both ratios at 0.1, a script that asked for 1.50 GiB stopped with max allowed: 1.18 GiB, a tenth of the 11.84 GiB that macOS recommends on this Mac. That stop is the error the limit is there to raise: Fixed: RuntimeError: MPS backend out of memory.

Common Mistakes​

MistakeWhat happensDo this instead
Setting only the high ratio, below 1.4Even 1.39 raised RuntimeError: invalid low watermark ratio 1.4Set PYTORCH_MPS_LOW_WATERMARK_RATIO too, at or below the high ratio
A low ratio above the high oneHigh 1.0 with low 1.2 raised RuntimeError: invalid low watermark ratio 1.2Keep the low ratio at or below the high one
A high ratio above 22.1 raised RuntimeError: invalid high watermark ratio 2.1. 2.0 ranStay between 0 and 2

Troubleshooting​

SymptomLikely causeFix
invalid low watermark ratio 1.4, and you never set a low ratioOnly the high ratio is set, below 1.4Set both variables
invalid low watermark ratio with a number you typedYour low ratio is above your high ratioLower the low ratio
invalid high watermark ratio with a number you typedThe high ratio is above 2Use a value from 0 to 2

FAQ​

Can I set the limit without any variable?​

Yes, in the code. With torch.mps.set_per_process_memory_fraction(0.5) as the first line after the import, and no variable set, the script printed 8 images use 24 MiB. The docs say: "The allowed value equals the fraction multiplied by recommended maximum device memory".

How do I set both for a whole terminal session?​

Use export. After export PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.5 and export PYTORCH_MPS_LOW_WATERMARK_RATIO=0.4, two runs of uv run python low_watermark.py in the same terminal both printed 8 images use 24 MiB.

What does a low ratio of 0.0 do?​

The script ran with a high ratio of 0.5 and a low ratio of 0.0. The docs say that for the low ratio "setting 0.0 disables adaptive commit and garbage collection", which are the two ways PyTorch holds memory down before the hard limit.

Is the number always 1.4?​

On Apple silicon, yes: 1.4 is the default there. The docs give 1.0 as the default for Macs with a separate graphics card, so the message there should name 1.0. I could only run this on Apple silicon.

Conclusion​

RuntimeError: invalid low watermark ratio 1.4 appears when PYTORCH_MPS_HIGH_WATERMARK_RATIO is set below the low watermark's default of 1.4. Setting PYTORCH_MPS_LOW_WATERMARK_RATIO at or below the high ratio fixed it, and the script printed 8 images use 24 MiB.

Additional Resources​