MPS_ERROR_UNSUPPORTED_DTYPE pytorch type_error ai_generated true

RuntimeError: MPS 后端不支持 float64,回退到 float32。请将张量显式转换为 float32。

RuntimeError: MPS backend does not support float64. Falling back to float32. Please cast your tensors to float32 explicitly.

ID: pytorch/mps-float64-cast-fallback

其他格式: JSON · Markdown 中文 · English
90%修复率
88%置信度
1证据数
2023-05-01首次发现

版本兼容性

版本状态引入弃用备注
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

根因分析

Apple Silicon 上的 Metal Performance Shaders (MPS) 后端不支持 float64(双精度)张量;任何涉及 float64 张量的操作会触发回退到 CPU 或导致错误,引起性能下降或崩溃。

English

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

官方文档

https://pytorch.org/docs/stable/notes/mps.html#un-supported-features

解决方案

  1. Cast all tensors to float32 explicitly before moving to MPS device: tensor = tensor.float().to('mps'). This ensures compatibility with MPS backend.
  2. 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.
  3. 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')

无效尝试

常见但无效的做法:

  1. Set environment variable PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.0 to disable MPS fallback 95% 失败

    This flag controls memory watermark, not dtype support. The float64 issue remains and will cause errors.

  2. Use torch.set_default_dtype(torch.float64) to force double precision 90% 失败

    This makes the problem worse by creating more float64 tensors, increasing fallback frequency and memory usage.

  3. Install PyTorch nightly build for better MPS support 85% 失败

    Nightly builds may have improvements but still do not support float64 on MPS as of current versions.