pytorch config_error ai_generated true

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

Also available as: JSON · Markdown · 中文
90%Fix Rate
87%Confidence
1Evidence
2023-04-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.10 active
1.11 active
1.12 active
1.13 active
2.0 active
2.1 active
2.2 active
2.3 active

Root Cause

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.

generic

中文

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

Official Documentation

https://pytorch.org/docs/stable/generated/torch.load.html

Workarounds

  1. 95% success Use torch.load with map_location=torch.device('cpu') to load the checkpoint onto CPU, then move the model to the desired device afterward.
    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. 90% success Alternatively, use map_location='cpu' directly as a string argument.
    Alternatively, use map_location='cpu' directly as a string argument.

中文步骤

  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.

Dead Ends

Common approaches that don't work:

  1. 80% fail

    This doesn't address the device mismatch; the model still expects CUDA.

  2. 90% fail

    This just changes the file path but doesn't fix the loading device.