RuntimeError:
CUDA out of
memory. Tried to allocate 20.00 MiB (
GPU 0; 6.00 GiB total capacity; 768.50 MiB already allocated; 16.39 MiB free; 9.50 MiB cached)解决方法
使用
GPU出现上述错误,尝试改小batch_size的大小
主要是记录一下自己平时遇到的问题,和大家分享一下
如有侵犯,请联系我
点个赞支持一下吧
服务器上跑程序,出现这种错误,大概是因为程序太大内存不够,有以下两种解决方式:
1、batch_size设置太大,改小一点
2、cuda节点被占用,可以指定其他节点,一种方式:程序里面写device = torch.device(‘cuda:0’ if torch.cuda.is_available() else ‘cpu’),服务器里面写CUDA_VISIBLE_DEVICES=X;另一种方式:程序里面直接写os.environ[“CUDA_VISIBLE_DEVICES”] = “2,3”
显示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_I
RuntimeError:
CUDA out of
memory. Tried to allocate 20.00 MiB (
GPU 0; 3.94 GiB total capacity; 3.36 GiB already allocated; 13.06 MiB free; 78.58 MiB cached)
减小batch_size, 致敬这位老哥
RuntimeError: CUDA out of memory. Tried to allocate 30.00 MiB (GPU 0; 10.92 GiB total capacity; 9.65 GiB already allocated; 29.00 MiB free; 10.37 GiB reserved in total by PyTorch)
GPU跑模型报错
RuntimeError: CUDA out of memory. Tried to allocate 38.00 MiB (GPU
在加载训练好的模型进行测试时,遇到了RuntimeError: Cuda error: out of memory。
觉得很诧异,明明模型也不是很大,怎么就爆显存了。
后面去pytorch 论坛上找到了答案,原来是加载模型的时候需要先通过torch.load()的map_location参数加载到cpu上,然后才放到gpu上。
def load_model(model, model_save_path, use_state_dict=True):
print(f"[i] load model fro
在工程上同时使用了
Tensorflow框架模型与
pytorch框架模型。全部模型大小加起来1个G左右,显存11G,但是
运行时报错:
CUDA out of
memory
RuntimeError:
CUDA out of
memory. Tried to allocate 20.00 MiB (
GPU 0; 10.91 GiB total capacity; 856.79 MiB already allocated; 17.38 MiB free; 71.21 MiB cached)
RuntimeError:
CUDA out of
memory. Tried to allocate 36.00 MiB (
GPU 0; 2.00 GiB total capacity; 859.04 MiB already allocated; 6.46 MiB free; 904.00 MiB reserved in total by
PyTorch)
解决方案:
win+r,输入cmd,输入nvidia-smi查看
GPU使用情况
可以看到有几个
运行的进程,输入 taskkill -PID
引发pytorch:CUDA out of memory错误的原因有两个:
1.当前要使用的GPU正在被占用,导致显存不足以运行你要运行的模型训练命令不能正常运行
解决方法:
1.换另外的GPU
2.kill 掉占用GPU的另外的程序(慎用!因为另外正在占用GPU的程序可能是别人在运行的程序,如果是自己的不重要的程序则可以kill)
命令行中输入以下命令,可以查看当前正在GPU运行的程序:
nvidia-smi
再根据上面显示的正在运行程序的PID,输入以下查看进程的命令,可以查看到进程的相关信息,包括