Cuda.is_available() is True on CPU?

Hi everyone,

I tried to use such a function to determine cuda availability

def get_torch_device():
    if torch.cuda.is_available():
        return torch.device("cuda:0")
    return torch.device("cpu")

and it returns ‘cuda:0’ on a device without Nvidia GPU at all.
Have anyone faced the same problem? Probably anyone has another solution?


That’s strange, as the script works even if I run it on a machine with a masked GPU:

> device(type='cpu')

EDIT: Wait a moment. I’ve run the script on the wrong machine.

What is torch.cuda.is_available() returning on your machine?

It returns True. So I manually set device='cpu' everytime I run code on my laptop without discrete graphics card

What does torch.cuda.device_count() return?

The script indeed works, i.e. masking the GPU returns ‘cpu’, while the plain call returns ‘cuda:0’ on my machine.

I’m sorry for a delay with a feedback. It seems that it was an issue with my env setup. I installed pytorch for GPU on a laptop without GPU. Updated to version 0.4.1 for CPU and everything works just fine.

pip3 install
pip3 install torchvision