【Pytorch】RuntimeError: CUDA out of memory 问题解决

情况一:
显示free的内存足够,但是仍然报CUDA out of memory错误。
如(仅举例):RuntimeError: CUDA out of memory. Tried to allocate 26.00 MiB (GPU 0; 10.73 GiB total capacity; 9.55 GiB already allocated; 199 MiB free; 19.44 MiB cached)

情况二:
报错
RuntimeError: cuDNN error: CUDNN_STATUS_INTERNAL_ERROR
You can try to repro this exception using the following code snippet. If that doesn’t trigger the error, please include your original repro script when reporting this issue.

前两种情况在修改数据加载函数(根据代码有所不同)中的num_workers 参数后解决。
num_workers参数是加载数据的线程数目,有可能电脑的线程数不满足所配置的参数。我的电脑是8线程,参数调为8就不合适。
解决办法出处:https://www.cxymm.net/article/yapifeitu/109004902

查看自己电脑线程数的方法:
1.WIN+R
2.输入wmic回车
3.输入cpu get numberOfLogicalProcessors回车

情况三:
GPU内存确实不足:
RuntimeError: CUDA out of memory. Tried to allocate 26.00 MiB (GPU 0; 10.73 GiB total capacity; 9.55 GiB already allocated; 28.31 MiB free; 19.44 MiB cached)

解决办法可以采用:
1.减小batch_size
2.运行torch.cuda.empty_cache()函数
3.释放GPU内存,kill掉没用的进程
4.更改使用的显卡。这里大多指在服务器上运行,没有制定显卡,都默认使用第一张,或者多人使用,产生的不足。参考:https://blog.csdn.net/weixin_43525427/article/details/104865262

来源:柠檬啵

物联沃分享整理
物联沃-IOTWORD物联网 » 【Pytorch】RuntimeError: CUDA out of memory 问题解决

发表评论