GPU编程

本文介绍了如何通过shell命令`watch -n 1 nvidia-smi`监控GPU使用情况,以及在Python中利用PyTorch检查GPU可用性。同时,讲解了如何指定使用特定GPU进行计算,并提供了将数据移到GPU上的两种方法。还提到了加速技巧和模型在GPU与CPU之间的转换加载方法,包括多GPU环境的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 查看服务器上gpu使用情况(shell)

 watch -n 1 nvidia-smi

  • 查看当前环境是否有gpu(python)

torch.cuda.is_available()

  • 指定使用某一块gpu(单GPU)

os.environ["CUDA_VISIBLE_DEVICES"] = "3"

  • 将数据放到gpu上计算

方法一:

a_cuda = a.cuda()

方法二:

self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

self.D.to(self.device)# self.device ==‘cpu’ or ‘cuda’

  • 加速

torch.backends.cudnn.benchmark = True # to speed up

  • GPU、CPU模型的转换加载

CPU->CPU,GPU->GPU

torch.load('gen_500000.pkl')

GPU->CPU

torch.load('gen_500000.pkl', map_location=lambda storage, loc: storage)

CPU->GPU1

torch.load('gen_500000.pkl', map_location=lambda storage, loc: storage.cuda(1))

参考链接:https://blog.youkuaiyun.com/dcrmg/article/details/79503978

  • 多GPU

https://blog.youkuaiyun.com/qq_19598705/article/details/80396325

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值