pytorch
config_error
ai_generated
true
RuntimeError: 尝试在 CUDA 设备上反序列化对象,但 torch.cuda.is_available() 为 False。如果你在仅 CPU 的机器上运行,请使用 torch.load 并设置 map_location=torch.device('cpu') 将存储映射到 CPU。
RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map the storages to the CPU.
ID: pytorch/model-save-load-device-mismatch
90%修复率
87%置信度
1证据数
2023-04-12首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.10 | active | — | — | — |
| 1.11 | active | — | — | — |
| 1.12 | active | — | — | — |
| 1.13 | active | — | — | — |
| 2.0 | active | — | — | — |
| 2.1 | active | — | — | — |
| 2.2 | active | — | — | — |
| 2.3 | active | — | — | — |
根因分析
将在 GPU 机器上保存的模型检查点加载到仅 CPU 的机器上,而未指定 map_location,导致 PyTorch 尝试在非 CUDA 系统上加载 CUDA 张量。
English
Loading a model checkpoint that was saved on a GPU machine onto a CPU-only machine without specifying map_location, causing PyTorch to try to load CUDA tensors on a non-CUDA system.
官方文档
https://pytorch.org/docs/stable/generated/torch.load.html解决方案
-
Use torch.load with map_location=torch.device('cpu') to load the checkpoint onto CPU, then move the model to the desired device afterward. -
Alternatively, use map_location='cpu' directly as a string argument.
无效尝试
常见但无效的做法:
-
80% 失败
This doesn't address the device mismatch; the model still expects CUDA.
-
90% 失败
This just changes the file path but doesn't fix the loading device.