Skip to content
Advertisement

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

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.

Advertisement

Answer

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())

Advertisement