How do I list all currently available GPUs with pytorch?

Question:

I know I can access the current GPU using torch.cuda.current_device(), but how can I get a list of all the currently available GPUs?

Asked By: vvvvv

||

Answers:

You can list all the available GPUs by doing:

>>> import torch
>>> available_gpus = [torch.cuda.device(i) for i in range(torch.cuda.device_count())]
>>> available_gpus
[<torch.cuda.device object at 0x7f2585882b50>]
Answered By: vvvvv

Check how many GPUs are available with PyTorch

import torch

num_of_gpus = torch.cuda.device_count()
print(num_of_gpus)

In case you want to use the first GPU from it.

device = 'cuda:0' if cuda.is_available() else 'cpu'

Replace 0 in the above command with another number If you want to use another GPU.

Answered By: Shaida Muhammad

Extending the previous replies with device properties

$ python3 -c "import torch; print([(i, torch.cuda.get_device_properties(i)) for i in range(torch.cuda.device_count())])"
[(0, _CudaDeviceProperties(name='NVIDIA GeForce RTX 3060', major=8, minor=6, total_memory=12044MB, multi_processor_count=28))]
Answered By: dmtgu

I know this answer is kind of late. I thought the author of the question asked what devices are actually available to Pytorch not:

  • how many are available (obtainable with device_count()) OR
  • the device manager handle (obtainable with torch.cuda.device(i)) which is what some of the other answers give.

If you want to know what the actual GPU name is (E.g.: NVIDIA 2070 GTI etc.) try the following instead:

import torch
for i in range(torch.cuda.device_count()):
   print(torch.cuda.get_device_properties(i).name)

Note the use of get_device_properties(i) function. This returns a object that looks like this:

_CudaDeviceProperties(name='NVIDIA GeForce RTX 2070', major=8, minor=6, total_memory=12044MB, multi_processor_count=28))

This object contains a property called name. You may optionally drill down directly to the name property to get the human-readable name associated with the GPU in question.

Answered By: Thornhale
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.