Repository: facebookresearch/fairseq

Issue:
generator failed: topk() got an unexpected keyword argument 'out'
I am running generate.py to check the translation performance. But I got an error :
###############################################################################
Traceback (most recent call last):
  File "generate.py", line 140, in <module>
    main()
  File "generate.py", line 92, in main
    for sample_id, src_tokens, target_tokens, hypos in translations:
  File "/home/kimi/Desktop/fairseq-py/fairseq/sequence_generator.py", line 77, in generate_batched_itr
    maxlen=int(maxlen_a*srclen + maxlen_b))
  File "/home/kimi/Desktop/fairseq-py/fairseq/sequence_generator.py", line 92, in generate
    return self._generate(src_tokens, beam_size, maxlen)
  File "/home/kimi/Desktop/fairseq-py/fairseq/sequence_generator.py", line 248, in _generate
    out=(cand_scores, cand_indices))
TypeError: topk() got an unexpected keyword argument 'out'
################################################################################

I set a breakpoint to check this error and find that both cand_scores and cand_indices are empty. I guess it may result in this error. 


Discussion:
What version of PyTorch are you using? The latest update requires >= 0.4.0, i.e., building from source (we will update the README to reflect this).
I am using 0.4.0a0+a011853. And this fairseq-py is not the latest version, I cloned it about 2 or 3 days ago

Resolving patch:
diff --git a/fairseq/sequence_generator.py b/fairseq/sequence_generator.py
--- a/fairseq/sequence_generator.py
+++ b/fairseq/sequence_generator.py
@@ -271,8 +271,8 @@ def get_hypo():
             if step < maxlen:
                 if prefix_tokens is not None and step < prefix_tokens.size(1):
                     probs_slice = probs.view(bsz, -1, probs.size(-1))[:, 0, :]
-                    cand_scores = probs_slice.gather(
-                        dim=1,
+                    cand_scores = torch.gather(
+                        probs_slice, dim=1,
                         index=prefix_tokens[:, step].view(-1, 1).data
                     ).expand(-1, cand_size)
                     cand_indices = prefix_tokens[:, step].view(-1, 1).expand(bsz, cand_size).data
@@ -341,17 +341,17 @@ def get_hypo():
             # get the top beam_size active hypotheses, which are just the hypos
             # with the smallest values in active_mask
             active_hypos, _ignore = buffer('active_hypos'), buffer('_ignore')
-            active_mask.topk(
-                k=beam_size, dim=1, largest=False,
-                out=(_ignore, active_hypos),
+            torch.topk(
+                active_mask, k=beam_size, dim=1, largest=False,
+                out=(_ignore, active_hypos)
             )
             active_bbsz_idx = buffer('active_bbsz_idx')
-            cand_bbsz_idx.gather(
-                dim=1, index=active_hypos,
+            torch.gather(
+                cand_bbsz_idx, dim=1, index=active_hypos,
                 out=active_bbsz_idx,
             )
-            active_scores = cand_scores.gather(
-                dim=1, index=active_hypos,
+            active_scores = torch.gather(
+                cand_scores, dim=1, index=active_hypos,
                 out=scores[:, step].view(bsz, beam_size),
             )
             active_bbsz_idx = active_bbsz_idx.view(-1)
diff --git a/generate.py b/generate.py
--- a/generate.py
+++ b/generate.py
@@ -127,7 +127,10 @@ def main(args):
                             hypo['positional_scores'].tolist(),
                         ))
                     ))
-                    print('A-{}\t{}'.format(sample_id, ' '.join(map(str, alignment))))
+                    print('A-{}\t{}'.format(
+                        sample_id,
+                        ' '.join(map(lambda x: str(utils.item(x)), alignment))
+                    ))
 
                 # Score only the top hypothesis
                 if i == 0:


Test patch: