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"]
code: def forward(
      self,
      beam_tokens: Tensor,
      beam_scores: Tensor,
      token_weights: Tensor,
      beam_prev_indices: Tensor,
      num_steps: int,
  ) -> List[Tuple[Tensor, float, List[float], Tensor, Tensor]]:
      self._check_dimensions(
          beam_tokens,
          beam_scores,
          token_weights,
          beam_prev_indices,
          num_steps,
      )
  
      end_states = self._get_all_end_states(
          beam_tokens, beam_scores, beam_prev_indices, num_steps
      )
  
      # outputs is list of the following for each hypothesis:
      # Tuple[Hypothesis, Hypothesis score, Token level scores, Attention Weights, Best indices]
      outputs = torch.jit.annotate(
          List[Tuple[Tensor, float, List[float], Tensor, Tensor]], []
      )
  
      for state_idx in range(len(end_states)):
          state = end_states[state_idx]
          hypothesis_score = float(state[0])
          beam_indices = self._get_output_steps_to_beam_indices(
              state, beam_prev_indices
          )
          beam_output = torch.jit.annotate(List[Tensor], [])
          token_level_scores = torch.jit.annotate(List[float], [])
          position = int(state[1])
          hyp_index = int(state[2])
  
          # best_indices represents the ending position of one hypothesis,
          # the first index corresponds num_step, the second corresponds beam_index
          best_indices = torch.tensor([position, hyp_index])
          back_alignment_weights = []
  
          assert position + 1 == len(beam_indices)
          pos = 1
          prev_beam_index = -1
          while pos < len(beam_indices):
              beam_index = beam_indices[pos]
              beam_output.append(beam_tokens[pos][beam_index])
              if pos == 1:
                  # beam_scores[0][:] are all 0s
                  token_level_scores.append(
                      float(beam_scores[pos][beam_index])
                  )
              else:
                  token_level_scores.append(
                      float(beam_scores[pos][beam_index])
                      - float(beam_scores[pos - 1][prev_beam_index])
                  )
              back_alignment_weights.append(
                  token_weights[pos][beam_index].detach()
              )
              prev_beam_index = beam_index
              pos += 1
          outputs.append(
              (
                  torch.stack(beam_output),
                  hypothesis_score,
                  token_level_scores,
                  torch.stack(back_alignment_weights, dim=1),
                  best_indices,
              )
          )
  
      return outputs
  
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 2 for calling the BeamDecode obj's underlying member method code with example usage: obj.forward(beam_tokens, beam_scores, token_weights, beam_prev_indices, num_steps) and returns: outputs