class_docstr: Beam search which keeps completed hypotheses in the beam.
  
  This is an alternative beam search implementation which is more similar to
  some of the beam search implementations in the literature (cf. Nematus,
  Blocks, tensor2tensor, TF NMT tutorial...). This implementation keeps
  completed hypotheses as active hypotheses in the next time step. Thus, they
  will keep competing against longer hypotheses. Beam search terminates if all
  active hypotheses are completed (i.e. end with EOS).
  
  Experiments have shown that this implementation and the main implementation
  in `beam_search.py` produce almost the same translations. We're leaving this
  under research/ because experimentation did not show a noticeable difference
  in translation quality from the existing implementation.
class_docstr_tok: ["Beam","search","which","keeps","completed","hypotheses","in","the","beam",".","This","is","an","alternative","beam","search","implementation","which","is","more","similar","to","some","of","the","beam","search","implementations","in","the","literature","(","cf",".","Nematus",",","Blocks",",","tensor2tensor",",","TF","NMT","tutorial","...",")",".","This","implementation","keeps","completed","hypotheses","as","active","hypotheses","in","the","next","time","step",".","Thus",",","they","will","keep","competing","against","longer","hypotheses",".","Beam","search","terminates","if","all","active","hypotheses","are","completed","(","i.e",".","end","with","EOS",")",".","Experiments","have","shown","that","this","implementation","and","the","main","implementation","in","`","beam_search.py","`","produce","almost","the","same","translations",".","We","'re","leaving","this","under","research\/","because","experimentation","did","not","show","a","noticeable","difference","in","translation","quality","from","the","existing","implementation","."]
docstr: Modifies word_scores such that hypos are expanded with the tokens
  in `permitted_tokens`.
  
  Args:
      word_scores: [bsz*beam_size, vocab_size] float tensor with
          cumulative word scores (logprobs plus hypo scores plus rewards)
      permitted_tokens: [bsz*beam_size] int tensor of tokens. We set all
          entries in `word_scores` to -inf except the permitted tokens.
docstr_tok: ["Modifies","word_scores","such","that","hypos","are","expanded","with","the","tokens","in","`","permitted_tokens","`",".","Args",":","word_scores",":","[","bsz","*","beam_size",",","vocab_size","]","float","tensor","with","cumulative","word","scores","(","logprobs","plus","hypo","scores","plus","rewards",")","permitted_tokens",":","[","bsz","*","beam_size","]","int","tensor","of","tokens",".","We","set","all","entries","in","`","word_scores","`","to","-inf","except","the","permitted","tokens","."]
code: def constrain_tokens(self, word_scores, permitted_tokens):
      """Modifies word_scores such that hypos are expanded with the tokens
      in `permitted_tokens`.
  
      Args:
          word_scores: [bsz*beam_size, vocab_size] float tensor with
              cumulative word scores (logprobs plus hypo scores plus rewards)
          permitted_tokens: [bsz*beam_size] int tensor of tokens. We set all
              entries in `word_scores` to -inf except the permitted tokens.
      """
      permitted_tokens = permitted_tokens.unsqueeze(1)
      permitted_scores = torch.gather(
          word_scores, dim=1, index=permitted_tokens
      )
      word_scores[:, :] = -math.inf
      word_scores.scatter_(
          dim=1, index=permitted_tokens, src=permitted_scores
      )
  
file_path: translate/pytorch_translate/research/beam_search/competing_completed.py
imports: import math
  import torch
  from fairseq.models import FairseqIncrementalDecoder
  from pytorch_translate.beam_decode import SequenceGenerator
label_desc: Use image node_id 6 for calling the CompetingCompletedSequenceGenerator obj's underlying member method code with example usage: obj.constrain_tokens(word_scores, permitted_tokens) without return types