关于torch.backends.deterministic和torch.backends.cudnn.benchmark

TLDR:这是个关于torch.backends.cudnn设置的问题,不同组合的torch.backends.deterministic和torch.backends.cudnn.benchmark会产生不一样的结果,其中最快的组合(deterministic = False ,benchmark = True)比最慢的组合(deterministic = True ,benchmark = False)大约快了30倍。

现在先记录下方便以后有想法有能力了再总结回顾。

在跑BEAT的时候,有一处代码很好玩,other_tools.set_random_seed()

我加了点注释的代码如下:

python 复制代码
def set_random_seed(args):
    os.environ['PYTHONHASHSEED'] = str(args.random_seed)
    random.seed(args.random_seed)
    np.random.seed(args.random_seed)
    torch.manual_seed(args.random_seed)
    torch.cuda.manual_seed_all(args.random_seed)
    torch.cuda.manual_seed(args.random_seed)
    ## pay attention ,the below is training speed difference ,in camn:
    ## if set deterministic = True  ,benchmark = True ,it will cost almost 50-60 seconds for 10its
    ## if set deterministic = False ,benchmark = True ,it will cost almost 1-2 seconds for 10its
    ## if set deterministic = True  ,benchmark = False,it will cost 58 seconds for 10its
    ## if set deterministic = False  ,benchmark = False,it will cost almost 4-5 seconds for 10its
    torch.backends.cudnn.deterministic = args.deterministic #default: False
    torch.backends.cudnn.benchmark = args.benchmark         #default: False
    torch.backends.cudnn.enabled = args.cudnn_enabled       #default: True

只不过很有点意外的是当deterministic = True ,benchmark = True的时候居然这么慢,我起初以为设置好了benchmark=True后torch框架会自动选个最快的卷积算法,后续deterministic = True让这个卷积算法每次返回都是这个固定最快的。

上面是我以为的,下面根据结果(在注释的代码中)来分析

deterministic = True ,benchmark = True的情况,的确还是会选下卷积算法,比如把benchmark在比如设置为False的时候每次运行时间都是固定的,设置为True的时候还是会有点时间上的小波动,可见的确是选了下卷积的算法造成了结果的差异。当然具体怎么选的我暂且就不知道了,当然,选取最快的情况deterministic = False ,benchmark = True会有什么意向不到的结果我暂且也不清楚,网上很多说选取deterministic = True ,benchmark = False是为了保持结果的可复现性,我感觉这很扯就是,波动理应当极小极小(当然这是我目前的偏见)。

相关推荐
蓝婷儿2 小时前
Python 机器学习核心入门与实战进阶 Day 1 - 分类 vs 回归
python·机器学习·分类
Devil枫3 小时前
Kotlin扩展函数与属性
开发语言·python·kotlin
程序员阿超的博客4 小时前
Python 数据分析与机器学习入门 (八):用 Scikit-Learn 跑通第一个机器学习模型
python·机器学习·数据分析·scikit-learn·入门教程·python教程
xingshanchang5 小时前
PyTorch 不支持旧GPU的异常状态与解决方案:CUDNN_STATUS_NOT_SUPPORTED_ARCH_MISMATCH
人工智能·pytorch·python
费弗里8 小时前
Python全栈应用开发利器Dash 3.x新版本介绍(1)
python·dash
李少兄9 天前
解决OSS存储桶未创建导致的XML错误
xml·开发语言·python
就叫飞六吧9 天前
基于keepalived、vip实现高可用nginx (centos)
python·nginx·centos
Vertira9 天前
PyTorch中的permute, transpose, view, reshape和flatten函数详解(已解决)
人工智能·pytorch·python
学Linux的语莫9 天前
python基础语法
开发语言·python
匿名的魔术师9 天前
实验问题记录:PyTorch Tensor 也会出现 a = b 赋值后,修改 a 会影响 b 的情况
人工智能·pytorch·python