Repository: OpenNMT/OpenNMT-py

Issue:
Pytorch 1.2 Mask Inversion
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.
```


Discussion:
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.

Resolving patch:
diff --git a/onmt/decoders/transformer.py b/onmt/decoders/transformer.py
--- a/onmt/decoders/transformer.py
+++ b/onmt/decoders/transformer.py
@@ -67,6 +67,11 @@ def forward(self, inputs, memory_bank, src_pad_mask, tgt_pad_mask,
                 device=tgt_pad_mask.device,
                 dtype=torch.uint8)
             future_mask = future_mask.triu_(1).view(1, tgt_len, tgt_len)
+            # BoolTensor was introduced in pytorch 1.2
+            try:
+                future_mask = future_mask.bool()
+            except AttributeError:
+                pass
             dec_mask = torch.gt(tgt_pad_mask + future_mask, 0)
 
         input_norm = self.layer_norm_1(inputs)
diff --git a/onmt/modules/global_attention.py b/onmt/modules/global_attention.py
--- a/onmt/modules/global_attention.py
+++ b/onmt/modules/global_attention.py
@@ -180,7 +180,7 @@ def forward(self, source, memory_bank, memory_lengths=None, coverage=None):
         if memory_lengths is not None:
             mask = sequence_mask(memory_lengths, max_len=align.size(-1))
             mask = mask.unsqueeze(1)  # Make it broadcastable.
-            align.masked_fill_(1 - mask, -float('inf'))
+            align.masked_fill_(~mask, -float('inf'))
 
         # Softmax or sparsemax to normalize attention weights
         if self.attn_func == "softmax":
diff --git a/onmt/translate/beam_search.py b/onmt/translate/beam_search.py
--- a/onmt/translate/beam_search.py
+++ b/onmt/translate/beam_search.py
@@ -73,6 +73,11 @@ def __init__(self, beam_size, batch_size, pad, bos, eos, n_best, mb_device,
 
         # beam state
         self.top_beam_finished = torch.zeros([batch_size], dtype=torch.uint8)
+        # BoolTensor was introduced in pytorch 1.2
+        try:
+            self.top_beam_finished = self.top_beam_finished.bool()
+        except AttributeError:
+            pass
         self.best_scores = torch.full([batch_size], -1e10, dtype=torch.float,
                                       device=mb_device)
 


Test patch: