Pytorch下GPU加速

1.单GPU加速

1.1 确保服务器的GPU可用

 import GPU
 torch.cuda.is_available()     #返回True,GPU能够使用
 torch.cuda.device_count()   #能够使用的GPU数量

1.2 查看GPU信息,在命令行输入nvidia-smi

在这里插入图片描述
动态查看命令,0.5s更新一次:watch -n 0.5 nvidia-smi

1.3 转移数据与模型

把张量(数据)或模型从内存转移至GPU,一般使用.to(device)或.cuda()。

device = torch.device("cuda:0"if torch.cuda.is_available() else "cpu")
#或device1=torch.device("cuda:1")        #可指定要使用那块GPU,也可以不指定
for batch_idx,(img,label) in enumerate(train_loader):
    img = img.to(device)
    label = img.to(device)

对于模型一样,也是.to(device)或.cuda()将网络放在GPU显存中
model =Net()
model.to(device1) #使用序号为1的GPU

1.4 GPU与CPU存储的数据对比

>>> import torch
>>> import numpy as np
>>> #利用numpy创建
... a = np.array([1, 2])
>>> a_torch = torch.from_numpy(a)
>>> print(type(a), type(a_torch))
<class 'numpy.ndarray'> <class 'torch.Tensor'>
>>> a_torch.to("cuda")
tensor([1, 2], device='cuda:0')
>>> a_torch.to("cpu")
tensor([1, 2])

2 多GPU加速

单机多GPU主要使用DataParallel函数,多主机多GPU一般使用DistributeParallel

#模型
model = torch.nn.DataParallel(model)    #默认使用所有的GPU
#如果想指定使用的GPU,比如有编号01234个GPU,只使用部分
device_ids =[0,1,2,3]
#对数据
input_data = input_data.to(device=device_ids[0])
#对模型
model= torch.nn.DataParallel(model)

3 GPU 使用注意事项

  1. GPU的数量尽量为偶数,奇数可能会出现异常中断的情况
  2. GPU很快,但数据量较小时,不如CPU在这里插入代码片
  3. 如果内存不够大,使用多个GPU训练时,构造Dataloader设置pin_memory=False

4 运行时异常问题解决

4.1 CUDNN_STATUS_EXECUTION_FAILED

因为内置的 cuDNN 的 auto-tuner 会自动寻找最适合当前配置的高效算法,来达到优化运行效率的问题,每个批次会导致cuDNN每次都会去寻找一遍最优配置,这样反而会降低运行效率

#在主函数开发头加入
from torch.backends import cudnn
cudnn.benchmark = False
torch.backends.cudnn.enabled = False

5 NVIDIA-SMI failed 解决

 NVIDIA-SMI has failed because it couldn’t communicate with the NVIDIA driver. Ma
ke sure that the latest NVIDIA driver is installed and running

是因为服务重启后linux 内核更新,与原来cuda的driver版本不匹配原因
遇到这个问题解决办法:
方法一:
cd /usr/src 查看驱动版本号(我的是440.44)
在这里插入图片描述
sudo apt-get install dkms
sudo dkms install -m nvidia -v 440.44
无需重启即可成功看到输入nvidia-smi后熟悉的界面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值