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: Run beam search.
  
  Args:
      encoder_input: 2-tuple (tokens, length) of int tensors containing
          the source tokens and the source sentence lengths.
      beam_size: beam size (if None, use self.beam_size)
      maxlen: Maximum target sentence length (if None, use self.maxlen)
      prefix_tokens: None or [bsz, prefix_length] int tensor with
          translation prefixes. All generated translations will be
          constrained to these prefixes.
      extra_info: If true, output additional information like alignment,
          attentions, and positional scores.
  
  Returns:
      A list of lists, containing the n-best translations for each
      batch entry. The translations are represented as dictionary with
      keys tokens, score, attention, alignment, positional_scores.
docstr_tok: ["Run","beam","search",".","Args",":","encoder_input",":","2-tuple","(","tokens",",","length",")","of","int","tensors","containing","the","source","tokens","and","the","source","sentence","lengths",".","beam_size",":","beam","size","(","if","None",",","use","self.beam_size",")","maxlen",":","Maximum","target","sentence","length","(","if","None",",","use","self.maxlen",")","prefix_tokens",":","None","or","[","bsz",",","prefix_length","]","int","tensor","with","translation","prefixes",".","All","generated","translations","will","be","constrained","to","these","prefixes",".","extra_info",":","If","true",",","output","additional","information","like","alignment",",","attentions",",","and","positional","scores",".","Returns",":","A","list","of","lists",",","containing","the","n-best","translations","for","each","batch","entry",".","The","translations","are","represented","as","dictionary","with","keys","tokens",",","score",",","attention",",","alignment",",","positional_scores","."]
code: def _generate(
      self,
      encoder_input,
      beam_size=None,
      maxlen=None,
      prefix_tokens=None,
      extra_info=True,
  ):
      """Run beam search.
  
      Args:
          encoder_input: 2-tuple (tokens, length) of int tensors containing
              the source tokens and the source sentence lengths.
          beam_size: beam size (if None, use self.beam_size)
          maxlen: Maximum target sentence length (if None, use self.maxlen)
          prefix_tokens: None or [bsz, prefix_length] int tensor with
              translation prefixes. All generated translations will be
              constrained to these prefixes.
          extra_info: If true, output additional information like alignment,
              attentions, and positional scores.
  
      Returns:
          A list of lists, containing the n-best translations for each
          batch entry. The translations are represented as dictionary with
          keys tokens, score, attention, alignment, positional_scores.
      """
      src_tokens = encoder_input[0]
      bsz, srclen = src_tokens.size()
      maxlen = (
          min(maxlen, self.maxlen)
          if maxlen is not None
          else self.maxlen
      )
      beam_size = beam_size if beam_size is not None else self.beam_size
  
      # Encode
      encoder_outs, incremental_states = self._encode(
          encoder_input, beam_size
      )
  
      hypo_tokens = src_tokens.new(bsz * beam_size, maxlen + 2).fill_(
          self.pad
      )
      hypo_tokens_buf = hypo_tokens.clone()
      hypo_tokens[:, 0] = self.eos
      cand_scores = src_tokens.new(bsz, beam_size).float().fill_(0)
      attn_list = []
      cand_scores_list = []
      cand_beams_list = []
      new_order = None
      # For example, new_order_offsets for bsz=3, beam_size=5 is
      # [0, 0, 0, 0, 0, 5, 5, 5, 5, 5, 10, 10, 10, 10, 10]
      new_order_offsets = (
          torch.arange(
              0, bsz * beam_size, step=beam_size, dtype=torch.long
          )
          .view(bsz, 1)
          .repeat(1, beam_size)
          .view(-1)
          .cuda()
      )
      for step in range(maxlen + 1):  # one extra step for EOS marker
          num_finished = 0
          if step > 0:
              eos_indices = torch.nonzero(
                  hypo_tokens[:, step] == self.eos
              )
              num_finished = eos_indices.size(0)
              if num_finished == bsz * beam_size:
                  break
          self.reorder_states(new_order, incremental_states)
          (
              word_scores,
              avg_attn,
              possible_translation_tokens,
          ) = self._decode(
              hypo_tokens[:, : step + 1],
              encoder_outs,
              incremental_states,
          )
          attn_list.append(avg_attn.view(bsz, beam_size, -1))
          self.add_rewards(
              word_scores, step, possible_translation_tokens
          )
          if step == 0:  # Use only first beam in first time step
              word_scores.view(bsz, beam_size, -1)[:, 1:, :] = -math.inf
          elif num_finished > 0:  # make sure EOS is continued with EOS
              word_scores[eos_indices] = -math.inf
              word_scores[eos_indices, self.eos] = 0.0
          if prefix_tokens is not None and step < prefix_tokens.size(1):
              self.constrain_tokens(word_scores, prefix_tokens[step])
          if step >= maxlen:  # Force EOS on all hypos
              self.constrain_tokens(
                  word_scores,
                  hypo_tokens.new_full((bsz * beam_size,), self.eos),
              )
          word_scores.add_(cand_scores.view(-1, 1))
          (
              cand_scores,
              cand_indices,
              cand_beams,
          ) = self.select_next_words(
              word_scores, bsz, beam_size, possible_translation_tokens
          )
          new_order = cand_beams.view(-1) + new_order_offsets
          torch.index_select(
              hypo_tokens, dim=0, index=new_order, out=hypo_tokens_buf
          )
          hypo_tokens_buf[:, step + 1] = cand_indices.view(-1)
          cand_scores_list.append(cand_scores)
          cand_beams_list.append(cand_beams)
          hypo_tokens, hypo_tokens_buf = hypo_tokens_buf, hypo_tokens
      build_hypo_fn = (
          self.build_hypos if extra_info else self.build_hypos_fast
      )
      return build_hypo_fn(
          hypo_tokens.view(bsz, beam_size, -1),
          cand_scores_list,
          attn_list,
          cand_beams_list,
      )
  
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 1 for calling the CompetingCompletedSequenceGenerator obj's underlying member method code with example usage: obj._generate(encoder_input, beam_size, maxlen, prefix_tokens, extra_info) and returns: build_hypo_fn