How to use map_location='cpu' due to "RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False"
I was trying to download the following model at https://pytorch.org/hub/nvidia_deeplearningexamples_tacotron2/

import torch
tacotron2 = torch.hub.load('nvidia/DeepLearningExamples:torchhub', 'nvidia_tacotron2')


I received:

&gt;&gt;&gt; import torch
&gt;&gt;&gt; tacotron2 = torch.hub.load('nvidia/DeepLearningExamples:torchhub', 'nvidia_tacotron2')
Using cache found in .cache\torch\hub\nvidia_DeepLearningExamples_torchhub
...
  File "Anaconda3\envs\env3_pytorch\lib\site-packages\torch\serialization.py", line 79, in validate_cuda_device
    raise RuntimeError('Attempting to deserialize object on a CUDA '
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='cpu' to map your storages to the CPU.


So I used the following with map_location='cpu', but still get the same error.

&gt;&gt;&gt; tacotron2 = torch.hub.load('nvidia/DeepLearningExamples:torchhub', 'nvidia_tacotron2', map_location='cpu')


torch.version.cuda shows a version but torch.cuda.is_available() is false.

&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.version.cuda
'9.0'
&gt;&gt;&gt; torch.cuda.is_available()
False



How to get around this error related to map_location as I used exactly the command error message asked to use?
How can I use cuda in loading the model?


torch.hub.load does not specifically support map_location, it only forwards the extra arguments to the loading of the model, so it's implementation dependent whether that would be support.

In this case it is not supported, the loading is implemented in NVIDIA/DeepLearningExamples:torchhub - hubconf.py and it does not pass any map_location to torch.load when the checkpoint is loaded.

That means you need to load the checkpoint manually and apply it to the model. Thankfully, the model can be loaded with the same configuration without loading the checkpoint, by setting pretrained=False and the checkpoint can be loaded separately with torch.hub.load_state_dict_from_url, which supports map_location. There is only a small change that is required to the state dict, because it was trained with DistributedDataParallel, which wraps the module, such that every layer in the model became module.layer. In the state dict that module. prefix needs to be stripped.

tacotron2 = torch.hub.load('nvidia/DeepLearningExamples:torchhub', 'nvidia_tacotron2', pretrained=False)

checkpoint = torch.hub.load_state_dict_from_url('https://api.ngc.nvidia.com/v2/models/nvidia/tacotron2pyt_fp32/versions/1/files/nvidia_tacotron2pyt_fp32_20190306.pth', map_location="cpu")

# Unwrap the DistributedDataParallel module
# module.layer -&gt; layer
state_dict = {key.replace("module.", ""): value for key, value in checkpoint["state_dict"].items()}

# Apply the state dict to the model
tacotron2.load_state_dict(state_dict)

