我想知道pytorch
是否正在使用我的GPU。如果在此过程中GPU中有任何活动,可以使用nvidia-smi
进行检测,但我想要用python
脚本编写的内容。
有办法吗?
这将是有效的:
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
。
在您开始运行训练循环后,如果您想从终端手动观察它是否正在使用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/
由于这里没有提出,我正在添加一个使用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
可以:
device
:
torch.rand(10).to(device)
device
上直接创建张量:
torch.rand(10, device=device)
这使得CPU和GPU之间的切换更加舒适,而无需更改实际代码。
由于对缓存和分配的内存存在一些疑问和困惑,我正在添加一些关于它的其他信息:
torch.cuda.max_memory_cached(device=None)
返回给定设备的缓存分配器管理的最大GPU内存(以字节为单位)。torch.cuda.memory_allocated(device=None)
返回给定设备的当前GPU内存使用量(以字节为单位)。
您可以直接交出邮件中上面指定的device
,也可以将其保留为None,它将使用current_device()
。
要检查是否有可用的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
在GPU上创建一个张量,如下所示:
$ python
>>> import torch
>>> print(torch.rand(3,3).cuda())
不要退出,打开另一个终端并检查python进程是否正在使用GPU:
$ nvidia-smi