关于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是为了保持结果的可复现性,我感觉这很扯就是,波动理应当极小极小(当然这是我目前的偏见)。

相关推荐
喵手21 分钟前
Python爬虫实战:网抑云音乐热门歌单爬虫实战 - 从入门到数据分析的完整指南!
爬虫·python·爬虫实战·网易云·零基础python爬虫教学·音乐热门采集·热门歌单采集
skywalk81631 小时前
LTX-2 是一个基于 Transformer 的视频生成模型,能够根据文本描述生成高质量视频
python·深度学习·transformer
不懒不懒1 小时前
【Python办公自动化进阶指南:系统交互与网页操作实战】
开发语言·python·交互
会周易的程序员1 小时前
cNetgate插件架构设计详解 动态库 脚本二开lua, python, javascript
javascript·c++·python·物联网·lua·iot
小雨中_2 小时前
3.7 GSPO:Group Sequence Policy Optimization(组序列策略优化)
人工智能·python·深度学习·机器学习·自然语言处理
zchxzl3 小时前
亲测2026京津冀专业广告展会
大数据·人工智能·python
~央千澈~3 小时前
抖音弹幕游戏开发之第19集:课程总结与答疑·优雅草云桧·卓伊凡
python·pygame
小雨中_4 小时前
3.5 ReMax:用 Greedy 作为基线的 REINFORCE + RLOO
人工智能·python·深度学习·机器学习·自然语言处理
overmind5 小时前
oeasy Python 116 用列表乱序shuffle来洗牌抓拍玩升级拖拉机
服务器·windows·python
A懿轩A5 小时前
【Java 基础编程】Java 枚举与注解从零到一:Enum 用法 + 常用注解 + 自定义注解实战
java·开发语言·python