[英]How do I list all currently available GPUs with pytorch?
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?我知道我可以使用torch.cuda.current_device()
访问当前的 GPU,但我如何才能获得所有当前可用 GPU 的列表?
You can list all the available GPUs by doing:您可以通过执行以下操作列出所有可用的 GPU:
>>> import torch
>>> available_gpus = [torch.cuda.device(i) for i in range(torch.cuda.device_count())]
>>> available_gpus
[<torch.cuda.device object at 0x7f2585882b50>]
Check how many GPUs are available with PyTorch检查 PyTorch 有多少 GPU 可用
import torch
num_of_gpus = torch.cuda.device_count()
print(num_of_gpus)
In case you want to use the first GPU from it.如果您想从中使用第一个 GPU。
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.如果要使用另一个 GPU,则将上述命令中的0替换为另一个数字。
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))]
I know this answer is kind of late.我知道这个答案有点晚了。 I thought the author of the question asked what devices are actually available to Pytorch not:我以为问题的作者问的是 Pytorch 实际可用的设备不是:
If you want to know what the actual GPU name is (Eg: NVIDIA 2070 GTI etc.) try the following instead:如果您想知道实际的 GPU 名称是什么(例如:NVIDIA 2070 GTI 等),请尝试以下操作:
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:请注意“get_device_properties(i)”function 的使用。这会返回一个 object,如下所示:
_CudaDeviceProperties(name='NVIDIA GeForce RTX 2070', major=8, minor=6, total_memory=12044MB, multi_processor_count=28))
This object contains a property called "name".这个 object 包含一个名为“name”的属性。 You may optionally drill down directly to the name property to get the human-readable name associated with the GPU in question.您可以选择直接向下钻取到名称属性,以获取与所讨论的 GPU 关联的人类可读名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.