这是一个运行基于字符的语言生成的RNN模型:
class RNN(nn.Module):
def __init__(self, input_size, hidden_size, output_size, n_layers):
super(RNN, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.n_layers = n_layers
self.encoder = nn.Embedding(input_size, hidden_size)
self.GRU = nn.GRU(hidden_size, hidden_size, n_layers, batch_first=True)
self.decoder = nn.Linear(hidden_size, output_size)
def forward(self, input, batch_size):
self.init_hidden(batch_size)
input = self.encoder(input)
output, self.hidden = self.GRU(input, self.hidden)
output = self.decoder(output.view(batch_size, self.hidden_size))
return output
def init_hidden(self, batch_size):
self.hidden = Variable(torch.randn(self.n_layers, batch_size, self.hidden_size).cuda())
我使用DataParallel实例化模型,在我的4个GPU中分割输入批量:
net = torch.nn.DataParallel(RNN(n_chars, hidden_size, n_chars, n_layers)).cuda()
这里是full code。
不幸的是,DataParallel要求输入将batch_size作为第一维,但GRU函数需要隐藏张量将batch_size作为第二维:
output, self.hidden = self.GRU(input, self.hidden)
代码将引发以下错误(请注意打印输出显示编码器在4个GPU上正确执行):
...
forward function: encoding input of shape: (16L, 1L)
forward function: encoding input of shape: (16L, 1L)
forward function: encoding input of shape: (16L,
forward function: encoding input of shape:
forward function: GRU processing input of shape:
1L)
( (16L, 16L1L, 1L), 100L)
forward function: GRU processing input of shape:
(16L, 1L,
forward function: GRU processing input of shape:100L)
(16L
forward function: GRU processing input of shape:, 1L, 100L) (
16L, 1L, 100L)
Traceback (most recent call last):
File "gru2.py", line 166, in <module>
output = net(c, batch_size)
File "/root/miniconda2/lib/python2.7/site-packages/torch/nn/modules/module.py", line 206, in __call__
result = self.forward(*input, **kwargs)
File "/root/miniconda2/lib/python2.7/site-packages/torch/nn/parallel/data_parallel.py", line 61, in forward
outputs = self.parallel_apply(replicas, inputs, kwargs)
File "/root/miniconda2/lib/python2.7/site-packages/torch/nn/parallel/data_parallel.py", line 71, in parallel_apply
return parallel_apply(replicas, inputs, kwargs)
File "/root/miniconda2/lib/python2.7/site-packages/torch/nn/parallel/parallel_apply.py", line 45, in parallel_apply
raise output
RuntimeError: Expected hidden size (2, 16L, 100), got (2L, 64L, 100L)
此处模型有2层,batch_size = 64,hidden_size = 100.
如何在前向功能中并行化GRU操作?
答案 0 :(得分:3)
您只需设置参数dim = 1,例如
net = torch.nn.DataParallel(RNN(n_chars, hidden_size, n_chars, n_layers), dim=1).cuda()
答案 1 :(得分:1)
PyTorch 1.5已完全解决了RNN培训和DataParallel的问题。 似乎已经无缝地完成了。 无需再进行gerrymandering。 我今天在一个涉及语音mfcc的双向GRUS的项目中证实了这一点。
class PEncoder(nn.Module):
def __init__(self, args, encoder):
super(PEncoder, self).__init__()
self.gpu_ids = args.gpu_ids
self.model = encoder
def forward(self, input):
if len(self.gpu_ids) > 1:
return nn.parallel.data_parallel(self.model, (input), self.gpu_ids)
else:
return self.model(input)
就是这么简单。 这确实将您的模型包装到另一个模型中,并有效地产生了稍微不同的计算图。因此,如果您拥有较早训练的模型,则可能必须以特殊方式加载它们并为此并行包装创建一些设置器。尝试一下,您会看到的。 (我尚未确认这方面。)