【发布时间】:2021-01-14 06:15:36
【问题描述】:
在一台机器上在多个 GPU 上训练一个神经网络的最佳做法是什么?
nn.DataParallel 的不同选项与使用 .to('cuda:0') and .to('cuda:1') 在不同 GPU 上放置不同层的不同选项让我有些困惑。我在 Pytorch docs 中看到后一种方法的日期是 2017 年。是否有标准,还是取决于偏好或模型类型?
方法一
class ToyModel(nn.Module):
def __init__(self):
super(ToyModel, self).__init__()
self.net1 = torch.nn.Linear(10, 10)
self.relu = torch.nn.ReLU()
self.net2 = torch.nn.Linear(10, 5)
def forward(self, x):
x = self.relu(self.net1(x))
return self.net2(x)
model = ToyModel().to('cuda')
model = nn.DataParallel(model)
方法二
class ToyModel(nn.Module):
def __init__(self):
super(ToyModel, self).__init__()
self.net1 = torch.nn.Linear(10, 10).to('cuda:0')
self.relu = torch.nn.ReLU()
self.net2 = torch.nn.Linear(10, 5).to('cuda:1')
def forward(self, x):
x = self.relu(self.net1(x.to('cuda:0')))
return self.net2(x.to('cuda:1'))
我不确定 Pytorch 是否提供了更多方法来在多个 GPU 上进行训练。 这两种方法似乎都会导致我的系统冻结,具体取决于我使用的模型。在 Jupyter 中,单元格停留在 [*],如果我不重新启动内核,屏幕会冻结,我必须进行硬重置。一些关于多 GPU 的教程导致我的系统像这样挂起和冻结。
【问题讨论】:
标签: parallel-processing neural-network pytorch gpu