【发布时间】:2020-07-03 02:17:28
【问题描述】:
如果我的模型仅包含 nn.Module 层,例如 nn.Linear,则 nn.DataParallel 工作正常。
x = torch.randn(100,10)
class normal_model(torch.nn.Module):
def __init__(self):
super(normal_model, self).__init__()
self.layer = torch.nn.Linear(10,1)
def forward(self, x):
return self.layer(x)
model = normal_model()
model = nn.DataParallel(model.to('cuda:0'))
model(x)
但是,当我的模型包含如下张量操作时
class custom_model(torch.nn.Module):
def __init__(self):
super(custom_model, self).__init__()
self.layer = torch.nn.Linear(10,5)
self.weight = torch.ones(5,1, device='cuda:0')
def forward(self, x):
return self.layer(x) @ self.weight
model = custom_model()
model = torch.nn.DataParallel(model.to('cuda:0'))
model(x)
它给了我以下错误
RuntimeError:在设备 1 上的副本 1 中捕获 RuntimeError。原始 Traceback(最近一次通话最后一次):文件 "/opt/conda/lib/python3.6/site-packages/torch/nn/parallel/parallel_apply.py", 第 60 行,在 _worker 输出=模块(*输入,**kwargs)文件“/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py”, 第 541 行,在 调用 result = self.forward(*input, **kwargs) File "", line 7, in forward return self.layer(x) @self.weight RuntimeError: 参数位于不同的 GPU 上 /pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:277
当我们的模型中有一些张量操作时,如何避免这个错误?
【问题讨论】:
标签: python-3.x pytorch