【RuntimeError】Gather got an input of invalid size【DataParallel问题】

错误:

错误发生在DataParallel or data_parallel()时

RuntimeError: Gather got an input of invalid size: got [32, 7, 15013], but expected [32, 6, 15013] (gather at /pytorch/torch/csrc/cuda/comm.cpp:239

 

出错位置:

该模型被放到不同的GPU上,而模型返回的数据在合并gather的时候,出现了维度不同无法gather的问题

output = self.model( args.paras... )

 

参考解决文档:

https://pytorch.org/docs/stable/notes/faq.html

 

出错原因:

错误发生在model代码内使用torch.nn.utils.rnn.pad_packed_sequence() 工具时,在将数据分配到不同的GPU上时,每个GPU的该函数都会自身执行,其会取它所看到的所有sequence的最长长度,来对其他非最长的句子进行padding。这时每个GPU上的数据不同会导致每个函数看到的最长长度不同,导致padding后的长度不同,这样在gather的时候,就会出现维度不匹配的问题。

 

解决方法:

使用pad_packed_sequence()的total_length参数来确保forward()调用相同长度的返回序列

total_length = padded_input.size(1)  # get the max sequence length
output, _ = pad_packed_sequence(packed_output, batch_first=True, total_length=total_length)

 

你可能感兴趣的:(ERROR)