torch.backends.cudnn.deterministic=True和torch.set_deterministic(True)之间有什么区别?

8

我的网络中包括'torch.nn.MaxPool3d',根据PyTorch文档(版本1.7 - https://pytorch.org/docs/stable/generated/torch.set_deterministic.html#torch.set_deterministic),当cudnn确定性标志位开启时,该函数会抛出一个RuntimeError。然而,当我在代码开头插入' torch.backends.cudnn.deterministic = True'代码后,就没有RuntimeError了。为什么那个代码不会抛出一个RuntimeError呢?我想知道那段代码是否保证了我的训练过程的确定性计算。

1个回答

13

torch.backends.cudnn.deterministic=True 仅适用于 CUDA 卷积操作,其他操作不受影响。因此,它不能保证您的训练过程是确定性的,因为您还使用了 torch.nn.MaxPool3d,其反向函数在 CUDA 中是不确定的。

另一方面,torch.set_deterministic() 影响此处列出的所有通常不确定性的操作(请注意,1.8 中已将 set_deterministic 重命名为 use_deterministic_algorithms):https://pytorch.org/docs/stable/generated/torch.use_deterministic_algorithms.html?highlight=use_deterministic#torch.use_deterministic_algorithms

正如文档所述,其中一些列出的操作没有确定性实现。因此,如果设置了 torch.use_deterministic_algorithms(True),它们将抛出错误。

如果需要使用像 torch.nn.MaxPool3d 这样的不确定性操作,则目前无法使您的训练过程是确定性的,除非您自己编写自定义的确定性实现。或者您可以打开 GitHub 问题请求确定性实现:https://github.com/pytorch/pytorch/issues

此外,您可能还想查看此页面:https://pytorch.org/docs/stable/notes/randomness.html


1
正如文档所述,一些列出的操作没有确定性实现。因此,如果设置了torch.use_deterministic_algorithms(True),它们将抛出错误。谢谢,我曾经对看到的错误感到困惑。 - Charlie Parker
1
如果您正在查找文档中答案的第一行:https://pytorch.org/docs/stable/backends.html#torch.backends.cudnn.deterministic,那么这是一个布尔值,如果为True,则会导致cuDNN仅使用确定性卷积算法。 - user1881282

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接