On a cpu device, how to load checkpoint saved on gpu device

Ja-Keoung_Koo: Loading this checkpoint on my cpu device gives an error: <SCODE>    raise AssertionError("Torch not compiled with CUDA enabled")
AssertionError: Torch not compiled with CUDA enabled```<ECODE>

apaszke: <SCODE>torch.load('my_file.pt', map_location=lambda storage, location: 'cpu')
<ECODE> While this will only map storages from GPU0: <SCODE>torch.load('my_file.pt', map_location={'cuda:0': 'cpu'})
<ECODE>

mromaniuk: I’m trying to load a GPU-trained model onto a CPU with the code you suggested: <SCODE>torch.load('my_file.pt', map_location=lambda storage, location: 'cpu')
<ECODE> … and I get this error: <SCODE>Traceback (most recent call last):
  File "net_predict.py", line 146, in <module>
    net = torch.load(f_net, map_location=(lambda storage, location: 'cpu'))
  File "/home/[...]/anaconda2/lib/python2.7/site-packages/torch/serialization.py", line 248, in load
    return _load(f, map_location, pickle_module)
  File "/home/[...]/anaconda2/lib/python2.7/site-packages/torch/serialization.py", line 340, in _load
    tensor = tensor_type._new_with_metadata_file(f, storage)
AttributeError: type object 'str' has no attribute '_new_with_metadata_file'
<ECODE> (I replaced my username with […]) Any idea what I’m doing wrong?

apaszke: I’m sorry, my bad. This should work: <SCODE>torch.load('my_file.pt', map_location=lambda storage, loc: storage)
<ECODE>

mromaniuk: Out of curiosity: could you explain what this does? I’m not sure how it knows to remap storage to CPU, since the lambda returns the storage it got as an argument.

apaszke: 

cyyyyc123: <SCODE>import torch

encoder = torch.load('encoder.pt', map_location=lambda storage, loc: storage)
decoder = torch.load('decoder.pt', map_location=lambda storage, loc: storage)

encoder.cpu()
decoder.cpu()
<ECODE> How can I load a GPU-trained model on a CPU device (without any GPUs) correctly? Thank you for your great work!

apaszke: Hey, no problem! I only have a couple more questions:

cyyyyc123: Good morning! Thank you very much!

cyyyyc123: May this is useful to provide some information for solving the problem. Thank you!

cyyyyc123: 

gaoking132: Sorry to reopen the thread. After running the code: I met the same problem as Yangyu met before. The error message shows: I just updated my pytorch to the latest version in the master branch. The version number is 0.1.11+761eef1. Any idea why?

denis_64: Hello, I tried to load a snapshot from gpu-training to run it on CPU-mode, but faced with the same problem, that described above. Of course, tried to use given advice, but there is no effect. <SCODE>torch.load('./snapshots/cpu_final_snapshot.pth', map_location=lambda storage, loc: storage)
<ECODE> I have the following traceback: <SCODE>Traceback (most recent call last):
  File "predict.py", line 39, in <module>
    params = torch.load('./snapshots/cpu_final_snapshot.pth', map_location=lambda storage, loc: storage)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/torch/serialization.py", line 222, in load
    return _load(f, map_location, pickle_module)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/torch/serialization.py", line 370, in _load
    result = unpickler.load()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/torch/cuda/__init__.py", line 279, in __new__
    _lazy_init()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/torch/cuda/__init__.py", line 96, in _lazy_init
    _check_driver()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/torch/cuda/__init__.py", line 63, in _check_driver
    raise AssertionError("Torch not compiled with CUDA enabled")
<ECODE> <SCODE>torch.__version__
'0.1.10_1'
<ECODE> Would be appreciated any help.

gaoking132: It seems that I found the problem that causes the error of “invalid combination of arguments”. Yesterday I used the model trained on 0.1.9 version of pytorch, and loaded it to cpu using the latest version of 0.1.11. The error appeared. Today I retrained the model using the latest version of 0.1.11 and loaded also using the latest version. Everything works. So I guess that there are inconsistencies between different versions of pytorch models.

Eugenio_Culurciello: 

hazelnutsgz: Is anything wrong with the new version of PyTorch?

colllin: 

Zayd: torch.load(‘save/best_BiLSTMCRF_pos_2019-01-10 12-42-50’,map_location=‘cpu’)

Willem: Sorry for reviving this post. I have a closely related question. I want to do the exact same thing, but using the C++ front-end. I.e. I want to save a model, trained using the C++ front-end on GPU, and then load in using the C++ front-end on a CPU device. It is possible? The documentation on torch::load does not give the map_location? Thanks for any help.

Towsif_Ahamed: torch.load(WEIGHTS_FILE, map_location=torch.device(‘cpu’) )