Opennmt-py: Pytorch 1.2 Mask Inversion

Created on 10 Aug 2019  路  3Comments  路  Source: OpenNMT/OpenNMT-py

The mask inversion changed in the new pytorch version:

1 - (torch.tensor([1, 2, 3]) < torch.tensor([3, 1, 2]))
RuntimeError: Subtraction, the - operator, with a bool tensor is not supported.
If you are trying to invert a mask, use the ~ or bitwise_not() operator instead.

~(torch.tensor([1, 2, 3]) < torch.tensor([3, 1, 2]))
tensor([False, True, True])

It seems that this change leads to a runtime error:

Traceback (most recent call last):
  File "translate.py", line 48, in <module>
    main(opt)
  File "translate.py", line 32, in main
    attn_debug=opt.attn_debug
  File "/home/kalle/workspaces/openNMT/OpenNMT-py-master/onmt/translate/translator.py", line 330, in translate
    batch, data.src_vocabs, attn_debug
  File "/home/kalle/workspaces/openNMT/OpenNMT-py-master/onmt/translate/translator.py", line 523, in translate_batch
    return_attention=attn_debug or self.replace_unk)
  File "/home/kalle/workspaces/openNMT/OpenNMT-py-master/onmt/translate/translator.py", line 672, in _translate_batch
    batch_offset=beam._batch_offset)
  File "/home/kalle/workspaces/openNMT/OpenNMT-py-master/onmt/translate/translator.py", line 561, in _decode_and_generate
    decoder_in, memory_bank, memory_lengths=memory_lengths, step=step
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 547, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/kalle/workspaces/openNMT/OpenNMT-py-master/onmt/decoders/decoder.py", line 213, in forward
    tgt, memory_bank, memory_lengths=memory_lengths)
  File "/home/kalle/workspaces/openNMT/OpenNMT-py-master/onmt/decoders/decoder.py", line 395, in _run_forward_pass
    memory_lengths=memory_lengths)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 547, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/kalle/workspaces/openNMT/OpenNMT-py-master/onmt/modules/global_attention.py", line 183, in forward
    align.masked_fill_(1 - mask, -float('inf'))
  File "/usr/local/lib/python3.6/dist-packages/torch/tensor.py", line 325, in __rsub__
    return _C._VariableFunctions.rsub(self, other)
RuntimeError: Subtraction, the `-` operator, with a bool tensor is not supported. If you are trying to invert a mask, use the `~` or `bitwise_not()` operator instead.

Most helpful comment

i know the solution.
please visit pytorch 1.2.0 release documents .

Version 1.2:

>>> 1 - (torch.tensor([1, 2, 3]) < torch.tensor([3, 1, 2]))
RuntimeError: Subtraction, the `-` operator, with a bool tensor is not supported.
If you are trying to invert a mask, use the `~` or `bitwise_not()` operator instead.

>>> ~(torch.tensor([1, 2, 3]) < torch.tensor([3, 1, 2]))
tensor([False,  True,  True])

align.masked_fill_(1 - mask, -float('inf'))

simply change it.

align.masked_fill_(~mask, -float('inf'))

All 3 comments

Hi @Bachstelze
Thanks for pointing this one out. There are a few other things that need to be adapted to support pytorch release 1.2. You may remain with version 1.1.0 of pytorch until those are fixed.

i know the solution.
please visit pytorch 1.2.0 release documents .

Version 1.2:

>>> 1 - (torch.tensor([1, 2, 3]) < torch.tensor([3, 1, 2]))
RuntimeError: Subtraction, the `-` operator, with a bool tensor is not supported.
If you are trying to invert a mask, use the `~` or `bitwise_not()` operator instead.

>>> ~(torch.tensor([1, 2, 3]) < torch.tensor([3, 1, 2]))
tensor([False,  True,  True])

align.masked_fill_(1 - mask, -float('inf'))

simply change it.

align.masked_fill_(~mask, -float('inf'))

@MuruganR96 pending PR #1527 is already including this change. Should be merged soon.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mrpega picture mrpega  路  3Comments

mataney picture mataney  路  4Comments

nikhilweee picture nikhilweee  路  3Comments

AyaNsar picture AyaNsar  路  4Comments

kakkartushar1 picture kakkartushar1  路  5Comments