1_torch.device
参考网址: https://ptorch.com/news/187.html
torch.device
代表将torch.Tensor
分配到的设备的对象。
torch.device
包含一个设备类型('cpu'
或'cuda'
设备类型)和可选的设备的序号。
如果设备序号不存在,则为当前设备;
例如,torch.Tensor
用设备构建'cuda'
的结果等同于'cuda:X'
,其中X
是torch.cuda.current_device()
的结果。
torch.Tensor
的设备可以通过Tensor.device
访问属性。
构造torch.device
可以通过字符串/字符串和设备编号。
>>> torch.device('cuda:0')
device(type='cuda', index=0)
>>> torch.device('cpu')
device(type='cpu')
>>> torch.device('cuda') # current cuda device
device(type='cuda')
在我的Siammask项目里面
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
2_torch.backend.cidnn.benchmark
参考网址: https://ptorch.com/news/187.html
torch.backend.cudnn.benchmark
大部分情况下,设置这个 flag 可以让内置的 cuDNN 的 auto-tuner 自动寻找最适合当前配置的高效算法,来达到优化运行效率的问题。
一般来讲,应该遵循以下准则:
- 如果网络的输入数据维度或类型上变化不大,设置 torch.backends.cudnn.benchmark = true 可以增加运行效率;
- 如果网络的输入数据在每次 iteration 都变化的话,会导致 cnDNN 每次都会去寻找一遍最优配置,这样反而会降低运行效率。