ruotianluo / self-critical.pytorch

Unofficial pytorch implementation for Self-critical Sequence Training for Image Captioning. and others.
MIT License
995 stars 278 forks source link

error calling `pack_padded_sequence` when using pytorch 1.7 #239

Open entalent opened 3 years ago

entalent commented 3 years ago

Hi, I ran the tools/train.py using python 3.7 and torch 1.7, and the program throws the following exception:

 ......
  File "/home/self-critical.pytorch-master/captioning/models/AttModel.py", line 34, in sort_pack_padded_sequence
    tmp = pack_padded_sequence(input[indices], sorted_lengths, batch_first=True)
  File "/home/library/miniconda3/envs/py3.7/lib/python3.7/site-packages/torch/nn/utils/rnn.py", line 244, in pack_padded_sequence
    _VF._pack_padded_sequence(input, lengths, batch_first)
RuntimeError: 'lengths' argument should be a 1D CPU int64 tensor, but got 1D cuda:0 Long tensor

I searched about this exception and found that it is related to this issue: https://github.com/pytorch/pytorch/issues/43227 It seems that PyTorch 1.7 requires the lengths to be a CPU tensor when calling pack_padded_sequence, and I think that is controversial, too.

Though this is caused by the new version of PyTorch (this weird behavior is not even documented), would you consider adding some workaround to improve the compatibility of the code?

ruotianluo commented 3 years ago

Thanks for the notice. For now you can simply add .cpu() I believe. I will push a fix some time.