You can remap the Tensor location at load time using the map_location argument to torch.load. For example this will forcefully remap everything onto CPU:
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'
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.
Sure. map_location can be either a dict where the locations corresponding to keys are remaped to their values. Alternatively, we support passing in a function, that will get a CPU storage and its serialized location, and it should return some storage that will replace the CPU one. If you just want to load everything onto the CPU, you can just return the first arugment, but you could do some more crazy stuff like sending all CUDA tensors to the next GPU, by parsing out the original device from the loc argument.
@apaszke Hi! I am sorry to reopen this thread. I have encountered a problem when I used the above method to load a GPU-trained model on CPU mode. The code fragment is:
The version of my PyTorch is 0.1.9+b46d5e0. I have compiled the PyTorch from source since I want to try to use half tensor with stateless methods. (You have mentioned it in this pull request. Excellent!)
I created the checkpoint about 12 hours before, which also used the 0.1.9+b46d5e0 version of PyTorch.
I met the same problem as Yangyu met before. The error message shows:
TypeError: set_ received an invalid combination of arguments - got (torch.FloatStorage, int, tuple, tuple), but expected one of:
no arguments
(torch.cuda.FloatTensor source)
(torch.cuda.FloatStorage storage)
(torch.cuda.FloatStorage sourceStorage, int storage_offset, int … size)
didn’t match because some of the arguments have invalid types: (!torch.FloatStorage!, int, !tuple!, !tuple!)
(torch.cuda.FloatStorage sourceStorage, int storage_offset, torch.Size size)
(torch.cuda.FloatStorage sourceStorage, int storage_offset, torch.Size size, tuple strides)
I just updated my pytorch to the latest version in the master branch. The version number is 0.1.11+761eef1. Any idea why?
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.
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")
When I use the torch 1.0.0, the given code will produce the result as the following:
torch.load(‘save/best_BiLSTMCRF_pos_2019-01-10 12-42-50’, map_location=lambda storage, location: ‘cpu’)
Traceback (most recent call last):
File “”, line 1, in
File “/home/jiaxin/.local/lib/python3.6/site-packages/torch/serialization.py”, line 367, in load
return _load(f, map_location, pickle_module)
File “/home/jiaxin/.local/lib/python3.6/site-packages/torch/serialization.py”, line 538, in _load
result = unpickler.load()
File “/home/jiaxin/.local/lib/python3.6/site-packages/torch/_utils.py”, line 135, in _rebuild_tensor_v2
tensor = _rebuild_tensor(storage, storage_offset, size, stride)
File “/home/jiaxin/.local/lib/python3.6/site-packages/torch/_utils.py”, line 129, in _rebuild_tensor
module = importlib.import_module(storage.module)
AttributeError: ‘str’ object has no attribute ‘module’
Is anything wrong with the new version of PyTorch?
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.