Repository: facebookresearch/fairseq

Issue:
Latest Version AssertionError
I just cloned the lasted version (which claimed has fixed the round bug reported before). But I meet a new error:
File "/home/kimi/Desktop/fairseq-py/fairseq/utils.py", line 292, in convert_padding_direction
    assert not isinstance(src_tokens, Variable)
AssertionError



Discussion:
Are you training an LSTM model? Can you paste the entire log and the command you're running, please
Yes. I am using LSTM (lstm_luong_wmt_en_de). And my PyTorch is 0.4.0.  (I use the same command in the previous versions (cloned 3 or 4 days ago)
**The command I use is:** 
CUDA_VISIBLE_DEVICES=0 python train.py data-bin/wmt18.tokenized.de-en --source-lang de --target-lang en --optimizer adam --lr 1e-3 --clip-norm 5.0 --arch lstm_luong_wmt_en_de --save-dir checkpoints/blstm

**The log is:**
| [de] dictionary: 8791 types
| [en] dictionary: 6655 types
| data-bin/wmt18.tokenized.de-en train 259668 examples
| data-bin/wmt18.tokenized.de-en valid 11813 examples
| model lstm_luong_wmt_en_de, criterion CrossEntropyCriterion
| num. model params: 93171655
| training on 1 GPUs
| max tokens per GPU = 6000 and max sentences per GPU = None
| epoch 001:   0%|                                     | 0/1694 [00:00<?, ?it/s]Traceback (most recent call last):
  File "train.py", line 29, in <module>
    main(args)
  File "train.py", line 23, in main
    singleprocess_main(args)
  File "/home/kimi/Desktop/fairseq-py/singleprocess_train.py", line 79, in main
    train(args, trainer, dataset, epoch, batch_offset)
  File "/home/kimi/Desktop/fairseq-py/singleprocess_train.py", line 138, in train
    log_output = trainer.train_step(sample)
  File "/home/kimi/Desktop/fairseq-py/fairseq/trainer.py", line 94, in train_step
    loss, sample_sizes, logging_outputs, ooms_fwd = self._forward(sample)
  File "/home/kimi/Desktop/fairseq-py/fairseq/trainer.py", line 142, in _forward
    loss, sample_size, logging_output_ = self.criterion(self.model, sample)
  File "/home/kimi/anaconda2/envs/py36/lib/python3.6/site-packages/torch/nn/modules/module.py", line 357, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/kimi/Desktop/fairseq-py/fairseq/criterions/cross_entropy.py", line 28, in forward
    net_output = model(**sample['net_input'])
  File "/home/kimi/anaconda2/envs/py36/lib/python3.6/site-packages/torch/nn/modules/module.py", line 357, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/kimi/Desktop/fairseq-py/fairseq/models/fairseq_model.py", line 43, in forward
    encoder_out = self.encoder(src_tokens, src_lengths)
  File "/home/kimi/anaconda2/envs/py36/lib/python3.6/site-packages/torch/nn/modules/module.py", line 357, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/kimi/Desktop/fairseq-py/fairseq/models/lstm.py", line 103, in forward
    left_to_right=True,
  File "/home/kimi/Desktop/fairseq-py/fairseq/utils.py", line 292, in convert_padding_direction
    assert not isinstance(src_tokens, Variable)
AssertionError



Yep, there was a change in behavior in the latest PyTorch:

```
>>> x = torch.autograd.Variable(torch.rand(5))
>>> isinstance(x, torch.autograd.Variable)
True
>>> isinstance(x.data, torch.autograd.Variable)
True
```

I'll submit a fix shortly.

Resolving patch:
diff --git a/fairseq/models/lstm.py b/fairseq/models/lstm.py
--- a/fairseq/models/lstm.py
+++ b/fairseq/models/lstm.py
@@ -96,9 +96,9 @@ def __init__(self, dictionary, embed_dim=512, num_layers=1, dropout_in=0.1,
     def forward(self, src_tokens, src_lengths):
         if LanguagePairDataset.LEFT_PAD_SOURCE:
             # convert left-padding to right-padding
-            src_tokens.data = utils.convert_padding_direction(
-                src_tokens.data,
-                src_lengths.data,
+            src_tokens = utils.convert_padding_direction(
+                src_tokens,
+                src_lengths,
                 self.padding_idx,
                 left_to_right=True,
             )
diff --git a/fairseq/utils.py b/fairseq/utils.py
--- a/fairseq/utils.py
+++ b/fairseq/utils.py
@@ -289,8 +289,6 @@ def convert_padding_direction(
     right_to_left=False,
     left_to_right=False,
 ):
-    assert not isinstance(src_tokens, Variable)
-    assert not isinstance(src_lengths, Variable)
     assert right_to_left ^ left_to_right
     pad_mask = src_tokens.eq(padding_idx)
     if pad_mask.max() == 0:


Test patch:
diff --git a/tests/test_utils.py b/tests/test_utils.py
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -61,7 +61,7 @@ def test_make_variable(self):
 
     def assertAlmostEqual(self, t1, t2):
         self.assertEqual(t1.size(), t2.size(), "size mismatch")
-        self.assertLess((t1 - t2).abs().max(), 1e-4)
+        self.assertLess(utils.item((t1 - t2).abs().max()), 1e-4)
 
 
 if __name__ == '__main__':