MPS_ERROR_UNSUPPORTED_DTYPE
pytorch
type_error
ai_generated
true
RuntimeError: MPS backend does not support float64. Falling back to float32. Please cast your tensors to float32 explicitly.
ID: pytorch/mps-float64-cast-fallback
90%Fix Rate
88%Confidence
1Evidence
2023-05-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| PyTorch 2.0.0 | active | — | — | — |
| PyTorch 2.1.0 | active | — | — | — |
| macOS 13 Ventura | active | — | — | — |
| macOS 14 Sonoma | active | — | — | — |
| Apple M1 | active | — | — | — |
| Apple M2 | active | — | — | — |
| Apple M3 | active | — | — | — |
Root Cause
The Metal Performance Shaders (MPS) backend on Apple Silicon does not support float64 (double precision) tensors; any operation involving float64 tensors triggers a fallback to CPU or an error, causing performance degradation or crashes.
generic中文
Apple Silicon 上的 Metal Performance Shaders (MPS) 后端不支持 float64(双精度)张量;任何涉及 float64 张量的操作会触发回退到 CPU 或导致错误,引起性能下降或崩溃。
Official Documentation
https://pytorch.org/docs/stable/notes/mps.html#un-supported-featuresWorkarounds
-
95% success Cast all tensors to float32 explicitly before moving to MPS device: tensor = tensor.float().to('mps'). This ensures compatibility with MPS backend.
Cast all tensors to float32 explicitly before moving to MPS device: tensor = tensor.float().to('mps'). This ensures compatibility with MPS backend. -
90% success Set the default dtype to float32 at the start of the script: torch.set_default_dtype(torch.float32). This prevents accidental creation of float64 tensors from Python floats.
Set the default dtype to float32 at the start of the script: torch.set_default_dtype(torch.float32). This prevents accidental creation of float64 tensors from Python floats.
-
85% success Use a custom wrapper function that checks dtype and casts if needed: def to_mps(tensor): return tensor.float().to('mps') if tensor.is_floating_point() else tensor.to('mps')
Use a custom wrapper function that checks dtype and casts if needed: def to_mps(tensor): return tensor.float().to('mps') if tensor.is_floating_point() else tensor.to('mps')
中文步骤
Cast all tensors to float32 explicitly before moving to MPS device: tensor = tensor.float().to('mps'). This ensures compatibility with MPS backend.Set the default dtype to float32 at the start of the script: torch.set_default_dtype(torch.float32). This prevents accidental creation of float64 tensors from Python floats.
Use a custom wrapper function that checks dtype and casts if needed: def to_mps(tensor): return tensor.float().to('mps') if tensor.is_floating_point() else tensor.to('mps')
Dead Ends
Common approaches that don't work:
-
Set environment variable PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.0 to disable MPS fallback
95% fail
This flag controls memory watermark, not dtype support. The float64 issue remains and will cause errors.
-
Use torch.set_default_dtype(torch.float64) to force double precision
90% fail
This makes the problem worse by creating more float64 tensors, increasing fallback frequency and memory usage.
-
Install PyTorch nightly build for better MPS support
85% fail
Nightly builds may have improvements but still do not support float64 on MPS as of current versions.