Torch.cuda.device_count() with no CUDA compiled / available

Would it return 0? Or would it throw? Docs don’t clarify this.

A question is whether I should check torch.cuda.is_available() prior to querying torch.cuda.device_count()

Sorry for the lazy question :frowning: , I don’t have a CPU-only install of pytorch on hands for now to check

On MacOSX (without CUDA support) it is

>>> import torch
>>> torch.cuda.is_available()
False
>>> torch.cuda.device_count()
0

either you can check it with CUDA_VISIBLE_DEVICES="":

CUDA_VISIBLE_DEVICES="" python -c "import torch; print(torch.cuda.is_available() + torch.cuda.device_count())"
0
1 Like

If PyTorch is compiled without CUDA at all (pytorch-cpu), would it also return 0?

Yes, I would say so

>>> import torch
>>> torch.__version__
'1.5.0'
>>> torch.cuda.is_available()
False
>>> torch.cuda.device_count()
0
>>> 
# conda list | grep torch
cpuonly                   1.0                           0    pytorch-nightly
pytorch                   1.5.0               py3.6_cpu_0  [cpuonly]  pytorch
1 Like