Fix issue with memory manager on MPS devices

This commit is contained in:
Jaret Burkett committed 2026-10-23 14:37:50 -06:00
1 parent ea3afb405f
commit 609f3e5fa5
2 files changed
+17 -10

No files matched your search

@@ -31,7 +31,11 @@ from torch.utils.checkpoint import checkpoint
def rope(pos: Tensor, dim: int, theta: float = 1e4, ntk: float = 1.0) -> Tensor:
scale = torch.arange(0, dim, 2, dtype=torch.float64, device=pos.device) / dim
# MPS has no float64
dt = torch.float32 if pos.device.type == "mps" else torch.float64
if dt is torch.float32:
pos = pos.to(dt)
scale = torch.arange(0, dim, 2, dtype=dt, device=pos.device) / dim
omega = 1.0 / ((theta * ntk) ** scale)
out = torch.einsum("...n,d->...nd", pos, omega)
out = torch.stack(
+12 -9
View File
@@ -457,15 +457,18 @@ class MemoryManager:
gc.collect()
torch.cuda.empty_cache()
# torch's pinned-host cache; private API, name varies by torch version
for fn_name in ("_accelerator_emptyHostCache", "_host_emptyCache"):
fn = getattr(torch._C, fn_name, None)
if fn is not None:
try:
fn()
break
except Exception:
pass
# torch's pinned-host cache; private API, name varies by torch version.
# Skipped on MPS: it segfaults there (pytorch/pytorch#197561), not catchable
on_mps = getattr(torch.backends, "mps", None) is not None and torch.backends.mps.is_available()
if not on_mps:
for fn_name in ("_accelerator_emptyHostCache", "_host_emptyCache"):
fn = getattr(torch._C, fn_name, None)
if fn is not None:
try:
fn()
break
except Exception:
pass
# glibc keeps freed arenas mapped; CDLL(None) resolves malloc_trim in
# the running process where glibc is present and fails cleanly on