问题
GPU跑模型报错
RuntimeError: CUDA out of memory. Tried to allocate 38.00 MiB (GPU 0; 10.76 GiB total capacity; 9.71 GiB already allocated; 5.56 MiB free; 9.82 GiB reserved in total by PyTorch)
分析
应该有三个原因
-
GPU还有其他进程占用显存,导致本进程无法分配到足够的显存
-
缓存过多,使用torch.cuda.empty_cache()清理缓存
-
卡不行,换块显存更大的卡吧
解决
解决方法总述
1.最直接的解决办法是减小batch_size(常用),或者减小模型参数和输入大小(一般很少这样做);
2.如果测试过程遇到这种情况,加上
with torch.no_grad():
内存就不会分配参数梯度的空间
3.如果在训练过程遇到这种情况,可以尝试在训练前先释放CUDA内存
nvidia-smi查看GPU使用率,如果使用率不高,就使用torch.cuda.empty_cache()释放内存
官网对于torch.cuda.empty_cache()的说明:
Releases all unoccupied cached memory currently held by the caching allocator so that those can be used in other GPU application and visible in nvidia-smi.
释放当前由缓存分配器保留的所有未占用的缓存内存,以便这些内存可在其他GPU应用程序中使用,并在nvidia-smi中可见 。该语句是释放没有被使用的缓存,所以可以放心大胆地使用.
版权声明:本文为CSDN博主「Jumi爱笑笑」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_39326879/article/details/106137586
方式二:测试代码不进行梯度传播
将测试代码放到以下的位置
with torch.no_grad():
(your test code)
测试时报错 RuntimeError: CUDA out of memory._python_齐天大胜-CSDN博客
https://blog.csdn.net/qq_34475584/article/details/103616674
方式3:缓存过多,使用torch.cuda.empty_cache()清理缓存
Pytorch—训练与测试时爆显存(out of memory)的一个解决方案(torch.cuda.empty_cache())_python_zxyhhjs2017的博客-CSDN博客
https://blog.csdn.net/zxyhhjs2017/article/details/92795831
- 参考文献
2020-03-19 RuntimeError: CUDA out of memory. Tried to allocate 38.00 MiB (GPU 0; 10.76 GiB total … – Python量化投资
https://www.lizenghai.com/archives/56659.html
如何解决RuntimeError: CUDA error: out of memory?_python_weixin_43509263的博客-CSDN博客
https://blog.csdn.net/weixin_43509263/article/details/103841657