笔者跑神经网络的时候遇到显存溢出问题。
系统:Ubuntu 16.04
CUDA:10.0.130
CUDNN:7.6.4.38
Python:3.6.12
PYTORCH: 1.2
TORCHVISION: 0.4
卷积神经网络的代码用CPU跑没问题,但是用CUDA+CUDNN跑报错:
RuntimeError: cuDNN error: CUDNN_STATUS_EXECUTION_FAILED
载入参数前,加上语句:
torch.backends.cudnn.enabled = False
禁用CUDNN跑,报错:
RuntimeError: CUDA error: CUBLAS_STATUS_EXECUTION_FAILED when calling `cublasSgemm( handle, opa, opb, m, n, k, &alpha, a, lda, b, ldb, &beta, c, ldc)`
试着换CUDA和CUDNN版本:
CUDA: 11.1.105
CUDNN
Python: 3.6.6
PYTORCH: 1.8
TORCHVISION: 0.9
BATCHSIZE:64
CUDA+CUDNN跑报错:
RuntimeError: CUDA out of memory. Tried to allocate 392.00 MiB (GPU 0; 7.79 GiB total capacity; 5.32 GiB already allocated; 307.25 MiB free; 5.33 GiB reserved in total by PyTorch)
屏蔽CUDNN也报错:
RuntimeError: CUDA out of memory. Tried to allocate 56.00 MiB (GPU 0; 7.79 GiB total capacity; 5.46 GiB already allocated; 70.81 MiB free; 5.48 GiB reserved in total by PyTorch)
在网上搜,说在报错语句前后加清理缓存的语句:
if hasattr(torch.cuda, 'empty_cache'):
torch.cuda.empty_cache()
还是没解决。
这个问题是显存不足,除了换显卡之外,修改模型大小可能是比较好的解决方法。
最后,BATCHSIZE改为8,运行成功。