pytorch runtime_error ai_generated true

RuntimeError:MPS后端内存不足(MPS后端不支持float64,回退到float32)

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

ID: pytorch/mps-scalar-type-mismatch

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

版本兼容性

版本状态引入弃用备注
torch>=2.0.0 active
macOS>=13.0 active
MPS>=1.0 active

根因分析

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

English

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

官方文档

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

解决方案

  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

无效尝试

常见但无效的做法:

  1. 95% 失败

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

  2. 99% 失败

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