title: how to get alignment or attention information for translations produced by a torch hub model?
body: <p>The torch hub provides pretrained models, such as: <a href="https://pytorch.org/hub/pytorch_fairseq_translation/" rel="nofollow noreferrer">https://pytorch.org/hub/pytorch_fairseq_translation/</a></p>
  <p>These models can be used in python, or interactively with a CLI.
  With the CLI it is possible to get alignments, with the <code>--print-alignment</code> flag. <a href="https://fairseq.readthedocs.io/en/latest/getting_started.html" rel="nofollow noreferrer">The following code</a> works in a terminal, after installing <a href="https://github.com/pytorch/fairseq" rel="nofollow noreferrer">fairseq</a> (and pytorch)</p>
  <pre><code>curl https://dl.fbaipublicfiles.com/fairseq/models/wmt14.v2.en-fr.fconv-py.tar.bz2 | tar xvjf -
  MODEL_DIR=wmt14.en-fr.fconv-py
  fairseq-interactive \
      --path $MODEL_DIR/model.pt $MODEL_DIR \
      --beam 5 --source-lang en --target-lang fr \
      --tokenizer moses \
      --bpe subword_nmt --bpe-codes $MODEL_DIR/bpecodes \ 
      --print-alignment
  </code></pre>
  <p>In python it is possible to specify the keyword args <code>verbose</code> and <code>print_alignment</code>:</p>
  <pre><code>import torch
  
  en2fr = torch.hub.load('pytorch/fairseq', 'transformer.wmt14.en-fr', tokenizer='moses', bpe='subword_nmt')
  
  fr = en2fr.translate('Hello world!', beam=5, verbose=True, print_alignment=True)
  </code></pre>
  <p>However, this will only output the alignment as a logging message. And for fairseq 0.9 it seems to be broken and results in an error message (<a href="https://github.com/pytorch/fairseq/issues/1880" rel="nofollow noreferrer">issue</a>).</p>
  <p>Is there a way to access alignment information (or possibly even the full attention matrix) from python code?</p>