一般来说,题目所示这种问题都是由batch_size的设置引起的,修改batch_size之后就能有效解决。但是,我今天遇到一种情况,这个问题不是由batch_size引起的,而是由torch.backends.cudnn.enabled = False这个设置引起的。
torch.backends.cudnn.enabled = False这个设置主要用于实验过程的可复现,具体的实验过程可复现代码设置如下:
import numpy as np
import torch
from torch.backends import cudnn
import random
import os
print(torch.__version__)
def set_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # if you are using multi-GPU.
np.random.seed(seed) # Numpy module.
random.seed(seed) # Python random module.
os.environ['PYTHONHASHSEED'] = str(seed)
torch.use_deterministic_algorithms(True)
torch.backends.cudnn.enabled = False # ----》引起问题的代码行
torch.backends.cudnn.benchmark = False #禁用benchmark,保证可复现
#torch.backends.cudnn.benchmark = True #恢复b

本文探讨了一个关于PyTorch中torch.backends.cudnn.enabled=False设置导致CUDA内存溢出的问题。作者发现即使batch_size较小,特定操作也会触发内存问题,并通过版本切换和代码注释验证了问题来源。文章建议理解cuDNN的非确定性和如何平衡可复现性和效率。
最低0.47元/天 解锁文章
1424

被折叠的 条评论
为什么被折叠?



