code: def decode(self, t, data, scores):
      # Add predicted scores to top_k_scores
      scores = torch.nn.functional.log_softmax(scores, dim=1)
      scores = self.top_k_scores.expand_as(scores) + scores
  
      # Find next top k scores and words. We flatten the scores tensor here
      # and get the top_k_scores and their indices top_k_words
      top_k_scores, top_k_words = [], []
      ex_start = 0
      for decode_size in self._decode_sizes:
          ex_end = ex_start + decode_size
          if t == 0:
              top_k_score, top_k_word = scores[ex_start].topk(
                  decode_size, 0, True, True
              )
          else:
              top_k_score, top_k_word = (
                  scores[ex_start:ex_end]
                  .view(-1)
                  .topk(decode_size, 0, True, True)
              )
          top_k_scores.extend(top_k_score)
          top_k_words.append(top_k_word)
          ex_start = ex_end
      self.top_k_scores = torch.stack(top_k_scores)
      # Convert to vocab indices. top_k_words contain indices from a flattened
      # k x vocab_size tensor. To get prev_word_indices we divide top_k_words
      # by vocab_size to determine which index in the beam among k generated
      # the next top_k_word. To get next_word_indices we take top_k_words
      # modulo vocab_size index. For example :
      # vocab_size : 9491
      # top_k_words : [610, 7, 19592, 9529, 292]
      # prev_word_ind : [0, 0, 2, 1, 0]
      # next_word_ind : [610, 7, 610, 38, 292]
      # further, shift the prev_word_ind by ex_start to find corresponding example
      # within a batch.
  
      ex_start = 0
      prev_word_inds, next_word_inds = [], []
      for ex_idx, decode_size in enumerate(self._decode_sizes):
          prev_word_inds.extend(
              (top_k_words[ex_idx] // self._vocab_size) + ex_start
          )
          next_word_inds.extend(top_k_words[ex_idx] % self._vocab_size)
          ex_start += decode_size
      prev_word_inds = torch.stack(prev_word_inds)
      next_word_inds = torch.stack(next_word_inds)
  
      # Add new words to sequences
      self.seqs = self.add_next_word(
          self.seqs, prev_word_inds, next_word_inds
      )
      # Find completed sequences
      complete_inds, incomplete_inds = self.find_complete_inds(
          next_word_inds
      )
  
      # Add to completed sequences and Reduce beam length
      ex_start = 0
      for ex_idx, decode_size in enumerate(self._decode_sizes):
          for beam_idx in range(ex_start, ex_start + decode_size):
              if beam_idx in complete_inds:
                  top_k_score = self.top_k_scores[beam_idx]
                  self._complete_seqs[ex_idx].append(
                      self.seqs[beam_idx].tolist()
                  )
                  self._complete_seqs_scores[ex_idx].append(top_k_score)
                  self._decode_sizes[ex_idx] -= 1
          ex_start += decode_size
  
      # Proceed with incomplete sequences
      if sum(self._decode_sizes) == 0:
          return True, data, 0
      self.seqs = self.seqs[incomplete_inds]
      self.top_k_scores = self.top_k_scores[incomplete_inds].unsqueeze(
          1
      )
  
      # TODO: Make the data update generic for any type of model
      # This is specific to BUTD model only.
      image_feature_0 = self.sample_list.image_feature_0
      self.sample_list.image_feature_0 = image_feature_0[
          incomplete_inds
      ]
      data = self.update_data(
          data, prev_word_inds, next_word_inds, incomplete_inds
      )
  
      next_beam_length = len(prev_word_inds[incomplete_inds])
  
      return False, data, next_beam_length
  
imports: import os
  import re
  from collections import Counter
  from itertools import chain
  import torch
  from mmf.common.registry import registry
  from mmf.utils.file_io import PathManager
  from mmf.utils.general import get_absolute_path
label_desc: Use image node_id 3 for calling the BeamSearch obj's underlying member method code with example usage: obj.decode(t, data, scores) and returns: False,  data,  next_beam_length, True,  data,  int