如何检查pytorch是否正在使用GPU?

问题描述 投票:58回答:7

我想知道pytorch是否正在使用我的GPU。如果在此过程中GPU中有任何活动,可以使用nvidia-smi进行检测,但我想要用python脚本编写的内容。

有办法吗?

python memory-management gpu nvidia pytorch
7个回答
101
投票

这将是有效的:

In [1]: import torch

In [2]: torch.cuda.current_device()
Out[2]: 0

In [3]: torch.cuda.device(0)
Out[3]: <torch.cuda.device at 0x7efce0b03be0>

In [4]: torch.cuda.device_count()
Out[4]: 1

In [5]: torch.cuda.get_device_name(0)
Out[5]: 'GeForce GTX 950M'

In [6]: torch.cuda.is_available()
Out[6]: True

这告诉我GeForce GTX 950M正在使用GPU PyTorch


30
投票

在您开始运行训练循环后,如果您想从终端手动观察它是否正在使用GPU资源以及程度,那么您可以使用:

$ watch -n 2 nvidia-smi

这将每2秒更新一次使用统计数据,直到您按ctrl + c


此外,您可以通过执行以下操作来检查PyTorch的安装是否正确检测到您的CUDA安装:

In [13]: import  torch

In [14]: torch.cuda.is_available()
Out[14]: True

True状态意味着PyTorch已正确配置并且正在使用GPU,尽管您必须在代码中移动/放置具有必要语句的张量。


如果您想在Python代码中执行此操作,请查看此模块:

https://github.com/jonsafari/nvidia-ml-py或pypi在这里:https://pypi.python.org/pypi/nvidia-ml-py/


21
投票

由于这里没有提出,我正在添加一个使用torch.device的方法,因为这非常方便,也就是在正确的device上初始化张量时。

# setting device on GPU if available, else CPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print('Using device:', device)
print()

#Additional Info when using cuda
if device.type == 'cuda':
    print(torch.cuda.get_device_name(0))
    print('Memory Usage:')
    print('Allocated:', round(torch.cuda.memory_allocated(0)/1024**3,1), 'GB')
    print('Cached:   ', round(torch.cuda.memory_cached(0)/1024**3,1), 'GB')

输出:

Using device: cuda

Tesla K80
Memory Usage:
Allocated: 0.3 GB
Cached:    0.6 GB

如上所述,使用device可以:

  • 将张量移动到相应的devicetorch.rand(10).to(device)
  • 要在device上直接创建张量: torch.rand(10, device=device)

这使得CPU和GPU之间的切换更加舒适,而无需更改实际代码。


编辑:

由于对缓存和分配的内存存在一些疑问和困惑,我正在添加一些关于它的其他信息:

您可以直接交出邮件中上面指定的device,也可以将其保留为None,它将使用current_device()


10
投票

在office站点和get start页面上,检查GPU的PyTorch如下:

import torch
torch.cuda.is_available()

参考:PyTorch|Get Start


3
投票

要检查是否有可用的GPU:

torch.cuda.is_available()

如果上面的函数返回False,你要么没有GPU,要么没有安装Nvidia驱动程序,因此操作系统没有看到GPU,或者GPU正被环境变量CUDA_VISIBLE_DEVICES隐藏。当CUDA_VISIBLE_DEVICES的值为-1时,则隐藏所有设备。您可以使用以下行检查代码中的值:`os.environ ['CUDA_VISIBLE_DEVICES']

如果上面的函数返回True,这并不一定意味着你正在使用GPU。在Pytorch中,您可以在创建设备时为设备分配张量。默认情况下,张量分配给cpu。要检查张量的分配位置,请执行以下操作:

# assuming that 'a' is a tensor created somewhere else
a.device  # returns the device where the tensor is allocated

请注意,您无法对在不同设备中分配的张量进行操作。要了解如何为GPU分配张量,请参见此处:https://pytorch.org/docs/stable/notes/cuda.html


0
投票

在GPU上创建一个张量,如下所示:

$ python
>>> import torch
>>> print(torch.rand(3,3).cuda()) 

不要退出,打开另一个终端并检查python进程是否正在使用GPU:

$ nvidia-smi

0
投票

FWIW:如果你在这里因为你的pytorch总是为false提供torch.cuda.is_available(),这可能是因为你安装了没有GPU支持的pytorch版本。 (例如:您在笔记本电脑中编码然后在服务器上进行测试)。解决方案是使用pytorch downloads页面中的正确命令再次卸载并安装pytorch。另请参阅this pytorch问题。

© www.soinside.com 2019 - 2024. All rights reserved.