torch.cuda.OutOfMemoryError: CUDA out of memory.

这篇具有很好参考价值的文章主要介绍了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 documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF

尝试将

model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).half().cuda()

改为

model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).half().quantize(4).cuda()

仍然报错

RuntimeError: CUBLAS error: CUBLAS_STATUS_NOT_INITIALIZED

排错流程如下

查看服务器显存占用情况

watch -n 0.1 nvidia-smi

torch.cuda.OutOfMemoryError: CUDA out of memory.

 发现gpu:0显存被PID:19409程序大量占用, 报错应该是默认在gpu:0训练导致显存不足, 接着查看gpu:0上程序所属用户(如果不是师兄的我就kill了)

top

torch.cuda.OutOfMemoryError: CUDA out of memory.

一看是root的, 惹不起还躲不起嘛, 换张卡跑, 顺嘴一提, 权限内的程序可以kill -9 {pid}掉释放显存

kill -9 19409

 发现gpu:1空闲, 指定gpu:1上训练模型, 有多种方法,

(1) 可以在py代码开头(一定要在开头)加

import os
os.environ['CUDA_VISIBLE_DEVICES']='1'

这样即可指定在gpu:1上训练, 实际上是只设置gpu:1可见, 而屏蔽其他gpu卡

(2) 可以在代码运行前shell或bash脚本中加

CUDA_VISIBLE_DEVICES=1 python xxx.py

这样即可指定在gpu:1上训练, 实际上是只设置gpu:1可见, 而屏蔽其他gpu卡

(3)在程序中使用set_device()

import torch
torch.cuda.set_device(id)

设置完成后查看显存占用情况可以看到, gpu:1显存占用马上上升了, 不影响其他gpu卡的显存

 可以看到清华的ChatGLM-6B约占12G显存(其他卡显存增加是写文章的时候其他小伙伴在跑)

torch.cuda.OutOfMemoryError: CUDA out of memory.文章来源地址https://www.toymoban.com/news/detail-457790.html

到了这里,关于torch.cuda.OutOfMemoryError: CUDA out of memory.的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

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

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

相关文章

  • 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)
  • 【CUDA OUT OF MEMORY】【Pytorch】计算图与CUDA OOM

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

    2024年02月09日
    浏览(28)
  • 报错解决:RuntimeError: CUDA out of memory.

    在进行深度学习的模型训练时,经常会遇到显存溢出的报错: RuntimeError: CUDA out of memory. 输出如下图所示: 打开一个终端,输入以下命令查看GPU使用情况: 输出如下图所示: 使用nvidia-htop可以进一步查看更为详细的内容。 nvidia-htop:A tool for enriching the output of nvidia-smi. 可以通

    2024年02月12日
    浏览(42)
  • 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日
    浏览(42)
  • RuntimeError: CUDA out of memory.【多种场景下的解决方案】

    RuntimeError: CUDA out of memory.【多种场景下的解决方案】 🌈 个人主页:高斯小哥 🔥 高质量专栏:【Matplotlib之旅:零基础精通数据可视化】 🏆🏆关注博主,随时获取更多关于深度学习、PyTorch、Python领域的优质内容!🏆🏆   随着深度学习的繁荣发展,GPU已成为推动这一浪

    2024年02月22日
    浏览(39)
  • 【已解决】探究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日
    浏览(89)
  • Stable Diffusion WebUI内存不够爆CUDA Out of memory怎么办?

    在我们运行SD的时候,我们经常会爆CUDA Out of memory。 我们应该怎么办呢? 这是因为我们的显存或者内存不够了。 如果你是用cpu来跑图的则表示内存不够,这个时候就需要换个大点的内存了。 如果你是用gpu来跑图的就说明你显存不够用咯,这时候咋办呢? 下面我将一一述说解

    2024年02月08日
    浏览(109)
  • 【PyTorch】设置CUDA_VISIBLE_DEVICES无效的问题以及多卡使用以及CUDA out of memory问题

    方法1: 理想情况下, 该环境变量应设置在程序的顶部 。如果在设置 torch.backends.cudnn.benchmark 之后调用 CUDA_VISIBLE_DEVICES 变量,则更改 CUDA_VISIBLE_DEVICES 变量将不起作用。 方法2: https://discuss.pytorch.org/t/how-to-change-the-default-device-of-gpu-device-ids-0/1041/29 问题:单机多卡使用时,指定

    2024年04月14日
    浏览(38)
  • RuntimeError:CUDA out of memory.Tried to allocate 20.00MiB.

    这是我遇到的问题,刚开始的时候怎么也解决不了。 然后我去搜了一下解决方法,具体方法如下: 方法一: 仅需减小batchsize 改文件的配置cfg的batchsize=1,一般在cfg文件下的查找batch或batchsize,将batchsize调小后,再次运行,类似于改下面 方法二 :  上述方法还没解决,不改

    2023年04月11日
    浏览(31)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包