【问题标题】:Mixed Precision(Pytorch Autocast) Slows Down the Code混合精度(Pytorch Autocast)减慢代码速度
【发布时间】:2021-08-11 08:21:26
【问题描述】:

我有 RTX 3070。不知何故,使用自动投射会减慢我的代码。

torch.version.cuda 打印 11.1,torch.backends.cudnn.version() 打印 8005,我的 PyTorch 版本是 1.9.0。我正在使用带有内核 5.11.0-25-generic 的 Ubuntu 20.04。

这是我一直在使用的代码:

torch.cuda.synchronize()
start = torch.cuda.Event(enable_timing=True)
end = torch.cuda.Event(enable_timing=True)
start.record()
for epoch in range(10):

    running_loss = 0.0
    for i, data in enumerate(trainloader, 0):

        inputs, labels = data

      
        optimizer.zero_grad()
        
        
        with torch.cuda.amp.autocast(): 
             outputs = net(inputs)
             oss = criterion(outputs, labels) 

         scaler.scale(loss).backward() 
         scaler.step(optimizer)
         scaler.update() 

end.record()

torch.cuda.synchronize()

print(start.elapsed_time(end))

没有 torch.cuda.amp.autocast(),1 个 epoch 需要 22 秒,而使用 autocast() 1 个 epoch 需要 30 秒。

【问题讨论】:

    标签: pytorch


    【解决方案1】:

    事实证明,我的模型不够大,无法使用混合精度。当我增加卷积层的输入/输出通道时,它终于按预期工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-26
      • 2018-11-30
      • 1970-01-01
      • 2021-09-06
      • 1970-01-01
      • 2012-05-16
      • 2019-01-15
      • 2022-01-26
      相关资源
      最近更新 更多