RuntimeError: Attempted to set the storage of a tensor on device "cuda:0" to a storage on different device "cpu"

Question:

Earlier I have configured the following project

https://github.com/zllrunning/face-makeup.PyTorch

using Pytorch with CUDA=10.2, Now Pytorch with CUDA=10.2 support is not available for Windows.
So, when I am configuring the same project using Pytorch with CUDA=11.3, then I am getting the following error:

RuntimeError: Attempted to set the storage of a tensor on device "cuda:0" to a storage on different device "cpu".  This is no longer allowed; the devices must match.

Please help me in solving this problem.

Asked By: Mayank Tiwari

||

Answers:

I solved this by adding map_location=lambda storage, loc: storage.cuda() in the model_zoo.load_url method. I think in torch 1.12 they have changed the default location from GPU to CPU (which does not make any sense).

Edit:
In the file resnet.py, under function def init_weight(self):, the following line

state_dict = modelzoo.load_url(resnet18_url)

is changed with

state_dict = modelzoo.load_url(resnet18_url, map_location=lambda storage, loc: storage.cuda())

Answered By: Vinayak Kumar

not specifically related to resnet , but below code should work for majority of issues related to " dont know how to restore data location of torch.storage._untype storage ( tagged with gpu)

import torch  
import whisper 
devices = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") 
model = whisper.load_model("medium" , device =devices)
Answered By: Mahendra
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.