pytorch runtime_error ai_generated true

RuntimeError: MPS backend out of memory (MPS backend does not support float64, falling back to float32)

ID: pytorch/mps-scalar-type-mismatch

Also available as: JSON · Markdown · 中文
82%Fix Rate
88%Confidence
1Evidence
2023-09-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
torch>=2.0.0 active
macOS>=13.0 active
MPS>=1.0 active

Root Cause

MPS (Metal Performance Shaders) backend on Apple Silicon does not support float64 (double precision) tensors, causing fallback or crash when operations require double precision.

generic

中文

Apple Silicon上的MPS(Metal Performance Shaders)后端不支持float64(双精度)张量,当操作需要双精度时会导致回退或崩溃。

Official Documentation

https://pytorch.org/docs/stable/notes/mps.html

Workarounds

  1. 95% success Explicitly cast tensors to float32 before MPS operations: tensor = tensor.float()
    Explicitly cast tensors to float32 before MPS operations: tensor = tensor.float()
  2. 80% success Use CPU or CUDA backend for parts of model requiring float64, then move back: model.to('cpu').double()
    Use CPU or CUDA backend for parts of model requiring float64, then move back: model.to('cpu').double()
  3. 90% success Disable MPS fallback: torch.set_default_dtype(torch.float32) and ensure all model parameters are float32
    Disable MPS fallback: torch.set_default_dtype(torch.float32) and ensure all model parameters are float32

中文步骤

  1. Explicitly cast tensors to float32 before MPS operations: tensor = tensor.float()
  2. Use CPU or CUDA backend for parts of model requiring float64, then move back: model.to('cpu').double()
  3. Disable MPS fallback: torch.set_default_dtype(torch.float32) and ensure all model parameters are float32

Dead Ends

Common approaches that don't work:

  1. 95% fail

    This only affects memory allocation limits, not type support; float64 operations still fail.

  2. 99% fail

    MPS hardware/driver inherently lacks float64 support; no software update can add it.