【问题标题】:is it possible make cuda deterministic?是否有可能使 cuda 具有确定性?
【发布时间】:2019-03-23 06:57:36
【问题描述】:

我正在尝试通过比较输出来重构应用程序并针对旧版本测试新版本。在使用相同输入的第一个 pytorch conv 层,我在使用 CPU 时得到相同的输出。然而,在 GPU 上使用 cuda,输出张量在某些单元上的小数点后 5 位不同。有没有办法消除这种差异?我已经在设置:

random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True

【问题讨论】:

    标签: python gpu pytorch


    【解决方案1】:

    即使种子设置相同,cpu 和 gpu 也无法产生相同的结果。 参考thisthis

    【讨论】:

      【解决方案2】:

      如果你想在 Pytorch 中得到确定性的结果,请参考这个函数。

      def set_deterministic(seed=42):
          random.seed(seed)
          np.random.seed(seed)
          torch.manual_seed(seed)
          torch.cuda.manual_seed(seed)
          torch.cuda.manual_seed_all(seed) 
      
          torch.backends.cudnn.deterministic = True
          torch.backends.cudnn.benchmark = False
          torch.backends.cudnn.enabled = False
      seed = 42 # any number 
      set_deterministic(seed=seed)
      

      但是,torch.backends.cudnn.enabled = False 选项可能会降低代码的速度,因为它会禁用 cudnn。

      【讨论】:

        猜你喜欢
        • 2012-02-08
        • 1970-01-01
        • 2011-03-04
        • 2021-03-20
        • 2011-07-14
        • 2011-09-12
        • 2019-06-26
        • 1970-01-01
        • 2014-01-30
        相关资源
        最近更新 更多