class_docstr: Decodes the output of Beam Search to get the top hypotheses
class_docstr_tok: ["Decodes","the","output","of","Beam","Search","to","get","the","top","hypotheses"]
docstr: Return all end states and hypothesis scores for those end states.
docstr_tok: ["Return","all","end","states","and","hypothesis","scores","for","those","end","states","."]
code: def _get_all_end_states(
      self,
      beam_tokens: Tensor,
      beam_scores: Tensor,
      beam_prev_indices: Tensor,
      num_steps: int,
  ) -> Tensor:
      """
      Return all end states and hypothesis scores for those end states.
      """
      min_score = float("inf")
      min_index = -1
      end_states = torch.jit.annotate(List[Tensor], [])
      prev_hypo_is_finished = torch.zeros(self.beam_size).byte()
  
      position = 1
      while bool(position <= num_steps):
          hypo_is_finished = torch.zeros(self.beam_size).byte()
  
          for hyp_index in range(self.beam_size):
              prev_pos = beam_prev_indices[position][hyp_index]
              hypo_is_finished[hyp_index] = prev_hypo_is_finished[
                  prev_pos
              ]
  
              # If hypothesis was completed in the previous index,
              # then just continue
              if bool(hypo_is_finished[hyp_index] == 0):
                  # If the present token is EOS or we have reached max_length
                  # then hypothesis is complete
                  if bool(
                      beam_tokens[position][hyp_index]
                      == self.eos_token_id
                  ) or bool(position == num_steps):
                      if bool(self.stop_at_eos):
                          hypo_is_finished[hyp_index] = 1
  
                      hypo_score = float(
                          beam_scores[position][hyp_index]
                      )
                      if bool(self.length_penalty != 0):
                          hypo_score = hypo_score / float(
                              position
                          ) ** float(self.length_penalty)
  
                      (
                          end_states,
                          min_score,
                          min_index,
                      ) = self._add_to_end_states(
                          end_states,
                          min_score,
                          torch.tensor(
                              [
                                  hypo_score,
                                  float(position),
                                  float(hyp_index),
                              ]
                          ),
                          min_index,
                      )
  
          prev_hypo_is_finished = hypo_is_finished
          position = position + 1
  
      end_states = torch.stack(end_states)
  
      _, sorted_end_state_indices = end_states[:, 0].sort(
          dim=0, descending=True
      )
      end_states = end_states[sorted_end_state_indices, :]
      return end_states
  
imports: import math
  from typing import List, Tuple
  import torch
  from fairseq import search, utils
  from fairseq.models import FairseqIncrementalDecoder
  from pytorch_translate import utils
  from torch import Tensor
label_desc: Use image node_id 5 for calling the BeamDecode obj's underlying member method code with example usage: obj._get_all_end_states(beam_tokens, beam_scores, beam_prev_indices, num_steps) and returns: end_states