# RuntimeError: 尝试在 CUDA 设备上反序列化对象，但 torch.cuda.is_available() 为 False。如果你在仅 CPU 的机器上运行，请使用 torch.load 并设置 map_location=torch.device('cpu') 将存储映射到 CPU。

- **ID:** `pytorch/model-save-load-device-mismatch`
- **领域:** pytorch
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

将在 GPU 机器上保存的模型检查点加载到仅 CPU 的机器上，而未指定 map_location，导致 PyTorch 尝试在非 CUDA 系统上加载 CUDA 张量。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.10 | active | — | — |
| 1.11 | active | — | — |
| 1.12 | active | — | — |
| 1.13 | active | — | — |
| 2.0 | active | — | — |
| 2.1 | active | — | — |
| 2.2 | active | — | — |
| 2.3 | active | — | — |

## 解决方案

1. ```
   Use torch.load with map_location=torch.device('cpu') to load the checkpoint onto CPU, then move the model to the desired device afterward.
   ```
2. ```
   Alternatively, use map_location='cpu' directly as a string argument.
   ```

## 无效尝试

- **** — This doesn't address the device mismatch; the model still expects CUDA. (80% 失败率)
- **** — This just changes the file path but doesn't fix the loading device. (90% 失败率)
