# 运行时错误：MPS后端不可用。请安装支持MPS的PyTorch或使用CPU/CUDA。

- **ID:** `pytorch/mps-backend-unavailable`
- **领域:** pytorch
- **类别:** config_error
- **错误码:** `MPS_NOT_AVAILABLE`
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

PyTorch编译时未启用MPS（Metal Performance Shaders）支持，或系统缺少兼容的Apple GPU/macOS版本。

## 版本兼容性

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

## 解决方案

1. ```
   安装支持MPS的PyTorch：pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/mps。然后验证：import torch; print(torch.backends.mps.is_available())
   ```
2. ```
   回退到CPU：device = torch.device('cuda' if torch.cuda.is_available() else ('mps' if torch.backends.mps.is_available() else 'cpu'))
   ```

## 无效尝试

- **conda install pytorch -c pytorch** — Installing PyTorch from conda default channel often ships CPU-only build; user must explicitly select MPS variant. (70% 失败率)
- **export PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.0** — Setting environment variable PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.0 does not enable MPS; it only controls memory threshold. (90% 失败率)
