大概率(5重方法)解决RuntimeError: CUDA out of memory. Tried to allocate ... MiB

这篇具有很好参考价值的文章主要介绍了大概率(5重方法)解决RuntimeError: CUDA out of memory. Tried to allocate ... MiB。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


前些天发现了一个出色的人工智能学习网站。它的内容不仅深入浅出、易于理解,还充满了趣味性和幽默感,我觉得这对于喜欢探索新知识的朋友们来说会是一个不错的资源。
如果你对人工智能感兴趣,不妨 点击查看,看看能否为你的学习之旅增添一些乐趣和启发。

项目场景

跑bert-seq2seq的代码时,出现报错

RuntimeError: CUDA out of memory. Tried to allocate 870.00 MiB (GPU 2; 23.70 GiB total capacity; 19.18 GiB already allocated; 323.81 MiB free; 21.70 GiB reserved in total by PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation.
See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF

运行时错误:CUDA内存不足。试图分配870.00 MiB (GPU 2;23.70 GiB总容量;19.18 GiB已经分配;323.81 MiB空闲;如果分配的内存是>,>分配的内存尝试设置max_split_size_mb以避免碎片。请参阅文档了解内存管理和PYTORCH_CUDA_ALLOC_CONF

大概率(5重方法)解决RuntimeError: CUDA out of memory. Tried to allocate ... MiB

参考:
https://blog.csdn.net/qq_37555071/article/details/108346569
https://blog.csdn.net/xiyou__/article/details/118529350


原因分析&解决方案

① GPU空间没有释放

可能为PyTorch占用的GPU空间没有释放,导致下次运行时,出现CUDA out of memory。
命令行输入 nvidia-smi,显示GPU的使用情况,以及占用GPU的应用程序

nvidia-smi

大概率(5重方法)解决RuntimeError: CUDA out of memory. Tried to allocate ... MiB
此时发现仅GPU:0有4778MIB的占用
有两种解决方案

解决一 换GPU

将代码中的默认0,换成指定2

# device = "cuda" if torch.cuda.is_available() else 'cpu'
device = torch.device('cuda:2')

解决二 杀掉进程

windows系统输入taskkill -PID 进程号 -F 结束占用的进程,比如

taskkill -PID 7072 -F

linux系统输入kill 进程号 结束占用的进程,比如

kill 7072

大概率(5重方法)解决RuntimeError: CUDA out of memory. Tried to allocate ... MiB

然后再次输入 nvidia-smi 查看GPU使用情况


② 更换GPU后仍未解决

法一:调小batch_size

设到4基本上能解决问题,如果还不行,该方法pass。

法二:定时清内存

在报错处、代码关键节点(一个epoch跑完…)插入以下代码(目的是定时清内存):

import torch, gc

gc.collect()
torch.cuda.empty_cache()

法三(常用方法):设置测试&验证不计算参数梯度

在测试阶段和验证阶段前插入代码 with torch.no_grad()(目的是该段程序不计算参数梯度),如下:

def test(model,dataloader):
    model.eval()
    with torch.no_grad(): ###插在此处
        for batch in tqdm(dataloader):
			……

法四(使用的别人的代码时):将"pin_memory": True改为False

如果怎么修改,都会出现题中bug,甚至跑了几轮之后突然出现 cuda out of
memory,查看代码中是否存在一下代码(通常出现在main.py 或者数据加载的py文件中:

kwargs = {'num_workers': 6, 'pin_memory': True} if torch.cuda.is_available() else {}

将"pin_memory": True改为False,具体原因原博:

pin_memory就是锁页内存,创建DataLoader时,设置pin_memory=True,则意味着生成的Tensor数据最开始是属于内存中的锁页内存,这样将内存的Tensor转义到GPU的显存就会更快一些。
主机中的内存,有两种存在方式,一是锁页,二是不锁页,锁页内存存放的内容在任何情况下都不会与主机的虚拟内存进行交换(注:虚拟内存就是硬盘),而不锁页内存在主机内存不足时,数据会存放在虚拟内存中。显卡中的显存全部是锁页内存,当计算机的内存充足的时候,可以设置pin_memory=True。当系统卡住,或者交换内存使用过多的时候,设置pin_memory=False。因为pin_memory与电脑硬件性能有关,pytorch开发者不能确保每一个炼丹玩家都有高端设备,因此pin_memory默认为False。文章来源地址https://www.toymoban.com/news/detail-450055.html

到了这里,关于大概率(5重方法)解决RuntimeError: CUDA out of memory. Tried to allocate ... MiB的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • AI绘画——使用stable-diffusion生成图片时提示RuntimeError: CUDA out of memory处理方法

    RuntimeError: CUDA out of memory. Tried to allocate 1.50 GiB (GPU 0; 8.00 GiB total capacity; 5.62 GiB already allocated; 109.75 MiB free; 5.74 GiB reserved in total by PyTorch) If reserved memory is allocated memory try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF 1、添加参数–n_

    2024年02月02日
    浏览(41)
  • 报错记录torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 90.00 MiB (GPU 0; 7.93 GiB to

    原因,我选的卡号选错了, 确认好两件事: 1、本地文件和远程文件同步好了 2、代码中有没有指定哪块GPU的操作 他这个报错很反直觉的一个地方:如果你指定了2卡,2卡显存满了,他会说0卡显存满了,你去看0发现0根本没人用,这就很容易被绕进去

    2024年02月12日
    浏览(34)
  • RuntimeError: CUDA out of memory See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF

    报错: If reserved memory is allocated memory try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF 当reserved memory is allocated memory,进行如下设置,可解决此bug: 代码如下:

    2024年02月11日
    浏览(40)
  • 【已解决】探究CUDA out of memory背后原因,如何释放GPU显存?

    研究过深度学习的同学,一定对类似下面这个CUDA显存溢出错误不陌生 RuntimeError: CUDA out of memory. Tried to allocate 916.00 MiB (GPU 0; 6.00 GiB total capacity; 4.47 GiB already allocated; 186.44 MiB free; 4.47 GiB reserved in total by PyTorch) 本文探究CUDA的内存管理机制,并总结该问题的解决办法 在实验开始前

    2023年04月20日
    浏览(87)
  • Ubuntu下跑Aplaca报错:torch.cuda.0utofMemoryError: CUDA out of memory.解决办法(查看CUDA占用情况&清除GPU缓存)

    错误提示: torch.cuda.0utofMemoryError: CUDA out of memory.Tried to allocate 2.00 MiB (PU 0; 23.69 GiB total capacity; 237 BiB already allocated; 18.38 MiB fre; 2.50 GiB reserved in total by PyTorch) If reserved memory is allocated memory try setting max_split_size_mb to avoid fragmentation.See documentation for Memory Management and PYTORCH_CUDA_ALLOC_

    2024年02月11日
    浏览(37)
  • CUDA报错:Out of Memory

    如果报错里提示Pytorch reserved的内存远大于Already allocated的内存,那么就是因为分配显存时单位过大,导致出现大量内存碎片无法继续分配(与操作系统内存管理同理)。 我们可以限制一次分配的最大单位来解决这个问题。 随后代码便可正常运行了。

    2024年02月15日
    浏览(48)
  • torch.cuda.OutOfMemoryError: CUDA out of memory.

    训练清华ChatGLM-6B时报错, 原因是显存不够 torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 96.00 MiB (GPU 0; 23.70 GiB total capacity; 4.37 GiB already allocated; 64.81 MiB free; 4.37 GiB reserved in total by PyTorch) If reserved memory is allocated memory try setting max_split_size_mb to avoid fragmentation.  See documentatio

    2024年02月06日
    浏览(34)
  • 【CUDA OUT OF MEMORY】【Pytorch】计算图与CUDA OOM

    在实践过程中多次碰到了CUDA OOM的问题,有时候这个问题是很好解决的,有时候DEBUG一整天还是头皮发麻。 最近实践对由于计算图积累导致CUDA OOM有一点新的看法,写下来记录一下。 包括对计算图的一些看法和一个由于计算图引发错误的简化实例记录。 本人能力有限,认识片

    2024年02月09日
    浏览(27)
  • Pycharm报错torch.cuda.OutOfMemoryError: CUDA out of memory.

    报错 做深度学习相关的实验,可以看到我的显卡内存很小(哭了,不过我有时候是在别的电脑上做的,那个电脑比这个好用),网上搜到的说的 max_split_size_mb:128 这个方法我贴到我代码上之后没有效果。 因为我在这个电脑上做的是主实验后面的一些对比实验,也就是代码中很

    2024年02月05日
    浏览(33)
  • 部署stable diffusion 错误torch.cuda.OutOfMemoryError: CUDA out of memory.

    以来安装完毕,开始执行web_ui.bat 错误截图:  猜测原因:GPU用错了 webUI.py加一行代码 在此启动web_ui.bat,成功打开网页页面

    2024年02月11日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包