RuntimeError: CUDA out of memory
报错内容:
RuntimeError: CUDA out of memory. Tried to allocate 16.00 MiB (GPU 0; 2.00 GiB total capacity; 1.34 GiB already allocated; 14.76 MiB free; 1.38 GiB reserved in total by PyTorch)
查资料的过程发现另一种报错:
RuntimeError: CUDA out of memory. Tried to allocate 12.50 MiB (GPU 0; 10.92 GiB total capacity; 8.57 MiB already allocated; 9.28 GiB free; 4.68 MiB cached)
注意到括号内最后一部分的报错是不一样的。
解决
网络上提供的解决方法主要有以下几种:
(1) 减小batch size
最简单有效的办法:重新启动内核并找到最佳batch size之后减少batch size。
(2) 清除缓存
即加上:
torch.cuda.empty_cache()
或者手动清除未使用的变量:
import torch,gc
del variables
gc.collect()
torch.cuda.empty_cache()
但使用该方法错误可能依旧存在,因为Pytorch实际上并不清除内存,而是清除对变量占用的内存的引用。
(3) 不使用梯度
即(在错误代码前)加上:
with torch.no_grad():outputs = net(images) #该行为报错代码
在test过程中是要如此处理的,否则会使显存加倍导致out of memory (OOM)错误。但若是在训练过程中采用此策略,可能出现效果差甚至更严重的问题。
(4) 杀死其他占用GPU的进程
(Windows系统)打开cmd,输入命令查看:
nvidia-smi
可以看到gpu被占用情况和占用的进程(以及其进程号PID),接下来杀死最后一个进程:
taskkill /PID 12132 -t -f
其中12132是要杀死的进程号。再次查看可以看到:
(5) 正确使用GPU
可能最开始使用了:
os.environ["CUDA_VISIBLE_DEVICES"] = "0, 2, 3"
但打开任务管理器查看性能却有:
查看性能,发现NVIDIA的只有GPU1,所以改成如下:
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
若想直接改到CPU上运行,则可以修改如下:
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
(6) 换显存
即购买或租用高性能显卡。
(7) Gradient accumulation + Automatic Mixed Precision
该方法请参考:RuntimeError:CUDA Error;CUDA out of memory.
(8) 其他
通过以下命令进一步查看使用的gpu的内存分配情况:
torch.cuda.memory_summary(device=None, abbreviated=False)
通过可读的内存分配summary可以查看报错原因。
另外,迭代地传递数据或者改变网络层的大小也是可以尝试的方法。
总结
我报的是第一类错误,最后使用方法(4)解决了。大家可以根据自己的情况用以上的一种或者多种方法尝试解决。
参考:
- RuntimeError: CUDA out of memory. Tried to allocate 82.00 MiB
- https://zhuanlan.zhihu.com/p/373446225
- 【E-02】内存不足RuntimeError: CUDA out of memory. Tried to allocate 16.00 MiB (GPU 0; 2.00 GiB total capacity; 1.34 GiB already allocated; 14.76 MiB free; 1.38 GiB reserved in total by PyTorch)
RuntimeError: CUDA out of memory相关推荐
- pytorch遇见RuntimeError: CUDA out of memory的解决
RuntimeError: CUDA out of memory 1.查看是否其他程序占用显存 遇到此类错误后,对于py格式的文件来说,程序会进行终止,也就是当前程序占用的显存将会被释放.此时可用 w ...
- RuntimeError: CUDA out of memory. Tried to allocate 132.00 MiB (GPU 2; 3.95 GiB total capacity; 3.41
pytorch报错:RuntimeError: CUDA out of memory. Tried to allocate 132.00 MiB (GPU 2; 3.95 GiB total capa ...
- pytorch出现RuntimeError: CUDA out of memory.
无论batch-size设置多小也是会出现这个问题的,我的原因是我将pytorch升级到了1.0.1,然后出现了这个问题 RuntimeError: CUDA out of memory. Tried ...
- RuntimeError: CUDA out of memory. Tried to allocate 1.54 GiB...
报错 F:\Anaconda3\python.exe "E:/Program Files/PyCharm 2019.2/***/xxx.py" F:\Anaconda3\lib\s ...
- 显存不足问题(OOM、RuntimeError: CUDA out of memory)
显存不足问题(OOM.RuntimeError: CUDA out of memory). 记录一次pytorch yolov3爆显存(windows使用任务管理器查看显存为4G). 2G显存:SSD ...
- 报错`RuntimeError: CUDA out of memory. Tried to allocate 256.00 MiB (GPU 0; 9.78 GiB total capaci
报错RuntimeError: CUDA out of memory. Tried to allocate 256.00 MiB (GPU 0; 9.78 GiB total capacity; 8. ...
- RuntimeError: CUDA out of memory. Tried to allocate 170.00 MiB (GPU 0; 3.82 GiB total capacity; 1.94
错误: RuntimeError: CUDA out of memory. Tried to allocate 170.00 MiB (GPU 0; 3.82 GiB total capacity; ...
- 问题:RuntimeError: CUDA out of memory.
问题描述 出现RuntimeError: CUDA out of memory. Tried to allocate 1.26 GiB (GPU 0; 6.00 GiB total capacity; ...
- pytorch出现RuntimeError: CUDA out of memory的问题解决方法
pytorch出现RuntimeError: CUDA out of memory解决方法 将batch-size设置小些试试,如果不行,继续第二步 多线程num_works设置较小,最差设为0
最新文章
- python 画折线图怎么控制x轴间隔_如何在折线图的分类轴(xaxis)上指定标签之间的间隔?...
- ways of make process to background job
- 在Ubuntu中永久添加DNS
- JQuery 实现遮罩层
- oracle12c双机热备方案,Oracle 共享磁盘阵列 双机热备 实战 配置教程 配置手册
- java多线程安全解决方案_java常用知识:多线程安全问题的解决方法-Lock锁
- 分布式应用框架 Dapr
- 通过Katas进行Java教程:网球比赛(轻松)
- [Swift]LeetCode1043. 分隔数组以得到最大和 | Partition Array for Maximum Sum
- 构建插件式的应用程序框架(一)----开篇
- cordova APP 检查更新
- 循环 计算数值的整数次幂
- 字节跳动年底再招 10000 人,前端工程师非常紧缺!
- 【语音隐写】基于matlab GUI LSB语音信号数字水印嵌入提取(带面板)【含Matlab源码 1676期】
- 远控免杀从入门到实践
- Andorid11 暗码启动应用(一)
- 转:隐马尔可夫模型(HMM)攻略
- java 图片标注_怎么用java做一个图像细胞的标注测试的工具?,实现图片放大,框图,定位坐标?...
- 【复习总结】玫瑰人生 La Vie en Rose
- ZZNU-正约数之和(DP)
热门文章
- redhat KVM 桥接网络br0启动失败问题
- AppBar的WTL实现
- 【图像配准】基于粒子群改进的sift图像配准matlab源码
- 阿里AI天池大赛-天猫复购预测-基于XGBoost模型预测
- 大学生生涯规划1000字计算机专业,我的职业生涯规划计算机专业1000字
- 使用echarts制作泡泡效果
- 吴恩达 tensorflow2.0 实践系列课程(3):NLP
- Qt connect有第5个参数你知道吗?
- 运行Powershell脚本提示“因为在此系统上禁止运行脚本”解决办法
- 自学JAVA的我提交给2020的问卷答案,JAVA核心知识点分享这一年来的努力并没有白费!