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

- **ID:** `pytorch/mps-float64-fallback`
- **领域:** pytorch
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

Apple MPS（Metal Performance Shaders）后端不支持 float64（双精度）张量，导致操作失败或静默回退到 float32，可能造成精度损失或类型不匹配。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| torch>=1.12.0 | active | — | — |
| macOS>=12.3 | active | — | — |
| MPS>=1.0 | active | — | — |

## 解决方案

1. ```
   Explicitly cast all input tensors to float32 before moving to MPS: tensor = tensor.float().to('mps'). For models, use model = model.float().to('mps').
   ```
2. ```
   Override the default dtype at the start of your script: torch.set_default_dtype(torch.float32). Also ensure all data loaders yield float32 tensors by adding a transform: transforms.ConvertImageDtype(torch.float32).
   ```

## 无效尝试

- **** — Tensors already created as float64 remain float64; only newly created tensors are affected. (60% 失败率)
- **** — Float16 may underflow or overflow in training, leading to NaN loss or convergence issues. (70% 失败率)
