RuntimeError: CUDA out of memory. Tried to allocate 30.00 MiB

问题

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

这个错误是由于CUDA内存不足导致的。根据引用\[1\]和引用\[2\]的信息,你的GPU总容量为4.00 GiB或10.76 GiB,但已经分配了2.34 GiB或1.82 GiB的内存,剩余的内存不足以分配14.00 MiB的内存。这可能是由于你的模型或数据的规模过大,导致内存不足。你可以尝试减小batch size或者使用更小的模型来减少内存的使用。另外,你还可以尝试设置max_split_size_mb参数来避免内存碎片化。关于内存管理和PYTORCH_CUDA_ALLOC_CONF的更多信息,请参考PyTorch的文档。 此外,根据引用\[3\]的信息,你还可以通过手动杀死占用GPU内存的进程来释放内存。你可以使用kill命令加上进程的PID来终止该进程,例如kill -9 31272。 综上所述,你可以通过减小batch size、使用更小的模型、设置max_split_size_mb参数或手动杀死占用内存的进程来解决CUDA内存不足的问题。 #### 引用[.reference_title] - *1* [已解决yolov5报错RuntimeError: CUDA out of memory. Tried to allocate 14.00 MiB](https://blog.csdn.net/Code_and516/article/details/129798540)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [解决RuntimeError: CUDA out of memory. Tried to allocate 14.00 MiB](https://blog.csdn.net/qq_43733107/article/details/126876755)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值