torch.backends.cudnn.benchmark的用法

首先,要明白backends是什么,Pytorch的backends是其调用的底层库。torch的backends都有:

cuda
cudnn
mkl
mkldnn
openmp

代码torch.backends.cudnn.benchmark主要针对Pytorch的cudnn底层库进行设置,输入为布尔值True或者False:

  1. 设置为True,会使得cuDNN来衡量自己库里面的多个卷积算法的速度,然后选择其中最快的那个卷积算法。
    我们看官方文档描述:
    请添加图片描述
    所以,当这个参数设置为True时,启动算法的前期会比较慢,但算法跑起来以后会非常快
    根据官网论坛上一位海外网友的说法:如果输入大小是固定的,采用cudnn.benchmark是可以加速推理和训练的。除了启动时会花额外的时间配置。但对于输入大小会变动的,每变一次就会配置一次,反而会影响速度。
    英文原回答如下:

It enables benchmark mode in cudnn.
benchmark mode is good whenever your input sizes for your network do not vary. This way, cudnn will look for the optimal set of algorithms for that particular configuration (which takes some time). This usually leads to faster runtime.
But if your input sizes changes at each iteration, then cudnn will benchmark every time a new size appears, possibly leading to worse runtime performances.

  1. 设置为False
    则会关闭这一功能。

参考链接1:https://pytorch.org/docs/stable/backends.html
参考链接2:https://discuss.pytorch.org/t/what-does-torch-backends-cudnn-benchmark-do/5936

来源:木盏

物联沃分享整理
物联沃-IOTWORD物联网 » torch.backends.cudnn.benchmark的用法

发表评论