# RuntimeError：MPS后端内存不足（MPS后端不支持float64，回退到float32）

- **ID:** `pytorch/mps-scalar-type-mismatch`
- **领域:** pytorch
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 82%

## 根因

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

## 版本兼容性

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

## 解决方案

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
   ```

## 无效尝试

- **** — This only affects memory allocation limits, not type support; float64 operations still fail. (95% 失败率)
- **** — MPS hardware/driver inherently lacks float64 support; no software update can add it. (99% 失败率)
