如何检查PyTorch是否使用GPU?nvidia-smi命令可以检测GPU活动,但我想直接从Python脚本中检查它。
当前回答
由于这里没有提出,我添加了一个使用torch.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_reserved(0)/1024**3,1), 'GB')
编辑:torch.cuda。Memory_cached已重命名为torch.cuda.memory_reserved。因此,对于旧版本使用memory_cached。
输出:
Using device: cuda
Tesla K80
Memory Usage:
Allocated: 0.3 GB
Cached: 0.6 GB
如上所述,使用设备可以:
将张量移动到相应的设备: torch.rand(10),(设备) 直接在设备上创建一个张量: 火炬。兰特(10,设备=设备)
这使得CPU和GPU之间的切换舒适,而不改变实际的代码。
编辑:
由于有一些问题和困惑的缓存和分配内存,我添加了一些关于它的额外信息:
max_memory_cached(device=None)返回缓存分配器管理的最大GPU内存,单位为字节 鉴于设备。 memory_allocated(device=None)返回给定设备的当前GPU内存使用情况(以字节为单位)。
你可以直接移交一个设备,就像上面提到的那样,或者你可以让它为None,它将使用current_device()。
附加注意:旧的图形卡与Cuda计算能力3.0或更低可能是可见的,但不能被Pytorch使用!感谢hekimgil指出这一点!“发现了GPU0 GeForce GT 750M, cuda能力3.0。PyTorch不再支持这个GPU,因为它太老了。我们支持的cuda最低能力是3.5。”
其他回答
简单地从命令提示符或Linux环境中运行以下命令。
python -c 'import torch; print(torch.cuda.is_available())'
上面应该打印为True
python -c 'import torch; print(torch.rand(2,3).cuda())'
它应该打印以下内容:
tensor([[0.7997, 0.6170, 0.7042], [0.4174, 0.1494, 0.0516]], device='cuda:0')
Query | Command |
---|---|
Does PyTorch see any GPUs? | torch.cuda.is_available() |
Are tensors stored on GPU by default? | torch.rand(10).device |
Set default tensor type to CUDA: | torch.set_default_tensor_type(torch.cuda.FloatTensor) |
Is this tensor a GPU tensor? | my_tensor.is_cuda |
Is this model stored on the GPU? | all(p.is_cuda for p in my_model.parameters()) |
步骤1:导入火炬库
import torch
#步骤2:创建张量
tensor = torch.tensor([5, 6])
#步骤3:查找设备类型
#输出1:在下面的输出中,我们可以得到size(tensor.shape), dimension(tensor.ndim), #和处理张量的设备
tensor, tensor.device, tensor.ndim, tensor.shape
(tensor([5, 6]), device(type='cpu'), 1, torch.Size([2]))
#or
#输出2:在下面,输出我们可以得到的唯一设备类型
tensor.device
device(type='cpu')
#由于我的系统使用cpu处理器“第11代英特尔(R)酷睿(TM) i5-1135G7 @ 2.40GHz 2.42 GHz”
#find,如果张量处理GPU?
print(tensor, torch.cuda.is_available()
# the output will be
tensor([5, 6]) False
#上面的输出是假的,因此它不在gpu上
#快乐编码:)
从实际的角度来看,有一个小题外话:
import torch
dev = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
这个开发人员现在知道是cuda还是cpu。
在使用cuda时,处理模型和张量的方式是不同的。一开始有点奇怪。
import torch
import torch.nn as nn
dev = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
t1 = torch.randn(1,2)
t2 = torch.randn(1,2).to(dev)
print(t1) # tensor([[-0.2678, 1.9252]])
print(t2) # tensor([[ 0.5117, -3.6247]], device='cuda:0')
t1.to(dev)
print(t1) # tensor([[-0.2678, 1.9252]])
print(t1.is_cuda) # False
t1 = t1.to(dev)
print(t1) # tensor([[-0.2678, 1.9252]], device='cuda:0')
print(t1.is_cuda) # True
class M(nn.Module):
def __init__(self):
super().__init__()
self.l1 = nn.Linear(1,2)
def forward(self, x):
x = self.l1(x)
return x
model = M() # not on cuda
model.to(dev) # is on cuda (all parameters)
print(next(model.parameters()).is_cuda) # True
这一切都很棘手,一旦理解它,就可以帮助您快速处理较少的调试。
这是可能的
torch.cuda.is_available()
返回True,但在运行时得到以下错误
>>> torch.rand(10).to(device)
MBT建议:
RuntimeError: CUDA error: no kernel image is available for execution on the device
这个链接解释了
... torch.cuda。Is_available只检查你的驱动程序是否与我们在二进制文件中使用的cuda版本兼容。这意味着CUDA 10.1与您的驱动程序是兼容的。但是当你用CUDA计算时,它找不到你的拱门的代码。
推荐文章
- 如何限制Django模型中数值字段的最大值?
- 访问对象内存地址
- 为什么在Python方法中需要显式地有“self”参数?
- 如何升级Windows 10下的Python安装?
- tqdm在Jupyter笔记本重复打印新的进度条
- 如何在for循环中注释类型?
- 如何替换一个Pandas数据框架的字符串列中的文本?
- 从pandas apply()返回多个列
- 从列中的字符串中删除不需要的部分
- Python只大写第一个字母
- 使用熊猫合并时如何保持索引
- 如何排序两个列表(其中相互引用)在完全相同的方式
- Python中的哈希映射
- 子进程Popen和调用之间的区别是什么(我如何使用它们)?
- 将python 'type'对象转换为字符串