class_docstr: Beam search implementation.
docstr: Initialize beam search.
  
  Args:
      scorers (dict[str, ScorerInterface]): Dict of decoder modules
          e.g., Decoder, CTCPrefixScorer, LM
          The scorer will be ignored if it is `None`
      weights (dict[str, float]): Dict of weights for each scorers
          The scorer will be ignored if its weight is 0
      beam_size (int): The number of hypotheses kept during search
      vocab_size (int): The number of vocabulary
      sos (int): Start of sequence id
      eos (int): End of sequence id
      token_list (list[str]): List of tokens for debug log
      pre_beam_score_key (str): key of scores to perform pre-beam search
      pre_beam_ratio (float): beam size in the pre-beam search
          will be `int(pre_beam_ratio * beam_size)`
      return_hs (bool): Whether to return hidden intermediates
      normalize_length (bool): If true, select the best ended hypotheses
          based on length-normalized scores rather than the accumulated scores
docstr_tok: ["Initialize","beam","search",".","Args",":","scorers","(","dict","[","str",",","ScorerInterface","]",")",":","Dict","of","decoder","modules","e.g.",",","Decoder",",","CTCPrefixScorer",",","LM","The","scorer","will","be","ignored","if","it","is","`","None","`","weights","(","dict","[","str",",","float","]",")",":","Dict","of","weights","for","each","scorers","The","scorer","will","be","ignored","if","its","weight","is","0","beam_size","(","int",")",":","The","number","of","hypotheses","kept","during","search","vocab_size","(","int",")",":","The","number","of","vocabulary","sos","(","int",")",":","Start","of","sequence","id","eos","(","int",")",":","End","of","sequence","id","token_list","(","list","[","str","]",")",":","List","of","tokens","for","debug","log","pre_beam_score_key","(","str",")",":","key","of","scores","to","perform","pre-beam","search","pre_beam_ratio","(","float",")",":","beam","size","in","the","pre-beam","search","will","be","`","int","(","pre_beam_ratio","*","beam_size",")","`","return_hs","(","bool",")",":","Whether","to","return","hidden","intermediates","normalize_length","(","bool",")",":","If","true",",","select","the","best","ended","hypotheses","based","on","length-normalized","scores","rather","than","the","accumulated","scores"]
code: def __init__(
      self,
      scorers: Dict[str, ScorerInterface],
      weights: Dict[str, float],
      beam_size: int,
      vocab_size: int,
      sos: int,
      eos: int,
      token_list: List[str] = None,
      pre_beam_ratio: float = 1.5,
      pre_beam_score_key: str = None,
      return_hs: bool = False,
      hyp_primer: List[int] = None,
      normalize_length: bool = False,
  ):
      """Initialize beam search.
  
      Args:
          scorers (dict[str, ScorerInterface]): Dict of decoder modules
              e.g., Decoder, CTCPrefixScorer, LM
              The scorer will be ignored if it is `None`
          weights (dict[str, float]): Dict of weights for each scorers
              The scorer will be ignored if its weight is 0
          beam_size (int): The number of hypotheses kept during search
          vocab_size (int): The number of vocabulary
          sos (int): Start of sequence id
          eos (int): End of sequence id
          token_list (list[str]): List of tokens for debug log
          pre_beam_score_key (str): key of scores to perform pre-beam search
          pre_beam_ratio (float): beam size in the pre-beam search
              will be `int(pre_beam_ratio * beam_size)`
          return_hs (bool): Whether to return hidden intermediates
          normalize_length (bool): If true, select the best ended hypotheses
              based on length-normalized scores rather than the accumulated scores
  
      """
      super().__init__()
      # set scorers
      self.weights = weights
      self.scorers = dict()
      self.full_scorers = dict()
      self.part_scorers = dict()
      # this module dict is required for recursive cast
      # `self.to(device, dtype)` in `recog.py`
      self.nn_dict = torch.nn.ModuleDict()
      for k, v in scorers.items():
          w = weights.get(k, 0)
          if w == 0 or v is None:
              continue
          assert isinstance(
              v, ScorerInterface
          ), f"{k} ({type(v)}) does not implement ScorerInterface"
          self.scorers[k] = v
          if isinstance(v, PartialScorerInterface):
              self.part_scorers[k] = v
          else:
              self.full_scorers[k] = v
          if isinstance(v, torch.nn.Module):
              self.nn_dict[k] = v
  
      # set configurations
      self.sos = sos
      self.eos = eos
  
      # added for OpenAI Whisper decoding
      self.hyp_primer = hyp_primer
  
      self.token_list = token_list
      self.pre_beam_size = int(pre_beam_ratio * beam_size)
      self.beam_size = beam_size
      self.n_vocab = vocab_size
      if (
          pre_beam_score_key is not None
          and pre_beam_score_key != "full"
          and pre_beam_score_key not in self.full_scorers
      ):
          raise KeyError(
              f"{pre_beam_score_key} is not found in {self.full_scorers}"
          )
      self.pre_beam_score_key = pre_beam_score_key
      self.do_pre_beam = (
          self.pre_beam_score_key is not None
          and self.pre_beam_size < self.n_vocab
          and len(self.part_scorers) > 0
      )
      self.return_hs = return_hs
      self.normalize_length = normalize_length
  
imports: import logging
  from itertools import chain
  from typing import Any, Dict, List, NamedTuple, Tuple, Union
  import torch
  from espnet.nets.e2e_asr_common import end_detect
  from espnet.nets.scorer_interface import PartialScorerInterface, ScorerInterface
label_desc: Use image node_id 1 to create a new BeamSearch object from inherited base classes: torch.nn with example: obj = BeamSearch(scorers, weights, beam_size, vocab_size, sos, eos, token_list, pre_beam_ratio, pre_beam_score_key, return_hs, hyp_primer, normalize_length)