class_docstr: This method should have a common interface such that it can be called after
  the encoder as well as after the decoder
class_docstr_tok: ["This","method","should","have","a","common","interface","such","that","it","can","be","called","after","the","encoder","as","well","as","after","the","decoder"]
code: def beam_search_aggregate_topk(
      self,
      log_probs_per_model: List[torch.Tensor],
      attn_weights_per_model: List[torch.Tensor],
      prev_scores: torch.Tensor,
      beam_size: int,
      record_attention: bool,
  ):
      average_log_probs = torch.mean(
          torch.cat(log_probs_per_model, dim=1), dim=1, keepdim=True
      )
  
      best_scores_k_by_k, best_tokens_k_by_k = torch.topk(
          average_log_probs.squeeze(1), k=beam_size
      )
  
      prev_scores_k_by_k = prev_scores.view(-1, 1).expand(-1, beam_size)
      total_scores_k_by_k = best_scores_k_by_k + prev_scores_k_by_k
  
      # flatten to take top k over all (beam x beam) hypos
      total_scores_flat = total_scores_k_by_k.view(-1)
      best_tokens_flat = best_tokens_k_by_k.view(-1)
  
      best_scores, best_indices = torch.topk(
          total_scores_flat, k=beam_size
      )
  
      best_tokens = best_tokens_flat.index_select(
          dim=0, index=best_indices
      ).view(-1)
  
      # integer division to determine which input produced each successor
      prev_hypos = best_indices // beam_size
  
      if record_attention:
          average_attn_weights = torch.mean(
              torch.cat(attn_weights_per_model, dim=1),
              dim=1,
              keepdim=True,
          )
          attention_weights = average_attn_weights.index_select(
              dim=0, index=prev_hypos
          )
          attention_weights = attention_weights.squeeze_(1)
      else:
          attention_weights = torch.zeros(
              beam_size, attn_weights_per_model[0].size(2)
          )
  
      return (best_scores, best_tokens, prev_hypos, attention_weights)
  
file_path: pytext/pytext/torchscript/seq2seq/decoder.py
imports: from typing import Dict, List, Tuple
  import torch
  import torch.jit
  import torch.jit.quantized
  from torch import nn, Tensor
label_desc: Use image node_id 4 for calling the DecoderBatchedStepEnsemble obj's underlying member method code with example usage: obj.beam_search_aggregate_topk(log_probs_per_model, attn_weights_per_model, prev_scores, beam_size, record_attention) and returns: best_scores,  best_tokens,  prev_hypos,  attention_weights