class_docstr: Beam search implementation for Transducer.
  
  Args:
      decoder: Decoder module.
      joint_network: Joint network module.
      beam_size: Size of the beam.
      lm: LM module.
      lm_weight: LM weight for soft fusion.
      search_type: Search algorithm to use during inference.
      max_sym_exp: Number of maximum symbol expansions at each time step. (TSD)
      u_max: Maximum expected target sequence length. (ALSD)
      nstep: Number of maximum expansion steps at each time step. (mAES)
      expansion_gamma: Allowed logp difference for prune-by-value method. (mAES)
      expansion_beta:
           Number of additional candidates for expanded hypotheses selection. (mAES)
      score_norm: Normalize final scores by length.
      nbest: Number of final hypothesis.
      streaming: Whether to perform chunk-by-chunk beam search.
class_docstr_tok: ["Beam","search","implementation","for","Transducer",".","Args",":","decoder",":","Decoder","module",".","joint_network",":","Joint","network","module",".","beam_size",":","Size","of","the","beam",".","lm",":","LM","module",".","lm_weight",":","LM","weight","for","soft","fusion",".","search_type",":","Search","algorithm","to","use","during","inference",".","max_sym_exp",":","Number","of","maximum","symbol","expansions","at","each","time","step",".","(","TSD",")","u_max",":","Maximum","expected","target","sequence","length",".","(","ALSD",")","nstep",":","Number","of","maximum","expansion","steps","at","each","time","step",".","(","mAES",")","expansion_gamma",":","Allowed","logp","difference","for","prune-by-value","method",".","(","mAES",")","expansion_beta",":","Number","of","additional","candidates","for","expanded","hypotheses","selection",".","(","mAES",")","score_norm",":","Normalize","final","scores","by","length",".","nbest",":","Number","of","final","hypothesis",".","streaming",":","Whether","to","perform","chunk-by-chunk","beam","search","."]
docstr: Modified version of Adaptive Expansion Search (mAES).
  
  Based on AES (https://ieeexplore.ieee.org/document/9250505) and
           NSC (https://arxiv.org/abs/2201.05420).
  
  Args:
      enc_out: Encoder output sequence. (T, D_enc)
  
  Returns:
      nbest_hyps: N-best hypothesis.
docstr_tok: ["Modified","version","of","Adaptive","Expansion","Search","(","mAES",")",".","Based","on","AES","(","https",":","\/\/ieeexplore.ieee.org\/document\/9250505",")","and","NSC","(","https",":","\/\/arxiv.org\/abs\/2201.05420",")",".","Args",":","enc_out",":","Encoder","output","sequence",".","(","T",",","D_enc",")","Returns",":","nbest_hyps",":","N-best","hypothesis","."]
code: def modified_adaptive_expansion_search(
      self,
      enc_out: torch.Tensor,
  ) -> List[ExtendedHypothesis]:
      """Modified version of Adaptive Expansion Search (mAES).
  
      Based on AES (https://ieeexplore.ieee.org/document/9250505) and
               NSC (https://arxiv.org/abs/2201.05420).
  
      Args:
          enc_out: Encoder output sequence. (T, D_enc)
  
      Returns:
          nbest_hyps: N-best hypothesis.
  
      """
      if self.search_cache is not None:
          kept_hyps = self.search_cache
      else:
          init_tokens = [
              ExtendedHypothesis(
                  yseq=[0],
                  score=0.0,
                  dec_state=self.decoder.init_state(1),
              )
          ]
  
          beam_dec_out, beam_state = self.decoder.batch_score(
              init_tokens,
          )
  
          if self.use_lm:
              beam_lm_scores, beam_lm_states = self.lm.batch_score(
                  self.create_lm_batch_inputs(
                      [h.yseq for h in init_tokens]
                  ),
                  [h.lm_state for h in init_tokens],
                  None,
              )
  
              lm_state = beam_lm_states[0]
              lm_score = beam_lm_scores[0]
          else:
              lm_state = None
              lm_score = None
  
          kept_hyps = [
              ExtendedHypothesis(
                  yseq=[0],
                  score=0.0,
                  dec_state=self.decoder.select_state(beam_state, 0),
                  dec_out=beam_dec_out[0],
                  lm_state=lm_state,
                  lm_score=lm_score,
              )
          ]
  
      for enc_out_t in enc_out:
          hyps = kept_hyps
          kept_hyps = []
  
          beam_enc_out = enc_out_t.unsqueeze(0)
  
          list_b = []
          for n in range(self.nstep):
              beam_dec_out = torch.stack([h.dec_out for h in hyps])
  
              beam_logp, beam_idx = torch.log_softmax(
                  self.joint_network(beam_enc_out, beam_dec_out),
                  dim=-1,
              ).topk(self.max_candidates, dim=-1)
  
              k_expansions = self.select_k_expansions(
                  hyps, beam_idx, beam_logp
              )
  
              list_exp = []
              for i, hyp in enumerate(hyps):
                  for k, new_score in k_expansions[i]:
                      new_hyp = ExtendedHypothesis(
                          yseq=hyp.yseq[:],
                          score=new_score,
                          dec_out=hyp.dec_out,
                          dec_state=hyp.dec_state,
                          lm_state=hyp.lm_state,
                          lm_score=hyp.lm_score,
                      )
  
                      if k == 0:
                          list_b.append(new_hyp)
                      else:
                          new_hyp.yseq.append(int(k))
  
                          if self.use_lm:
                              new_hyp.score += self.lm_weight * float(
                                  hyp.lm_score[k]
                              )
  
                          list_exp.append(new_hyp)
  
              if not list_exp:
                  kept_hyps = sorted(
                      self.recombine_hyps(list_b),
                      key=lambda x: x.score,
                      reverse=True,
                  )[: self.beam_size]
  
                  break
              else:
                  beam_dec_out, beam_state = self.decoder.batch_score(
                      list_exp,
                  )
  
                  if self.use_lm:
                      (
                          beam_lm_scores,
                          beam_lm_states,
                      ) = self.lm.batch_score(
                          self.create_lm_batch_inputs(
                              [h.yseq for h in list_exp]
                          ),
                          [h.lm_state for h in list_exp],
                          None,
                      )
  
                  if n < (self.nstep - 1):
                      for i, hyp in enumerate(list_exp):
                          hyp.dec_out = beam_dec_out[i]
                          hyp.dec_state = self.decoder.select_state(
                              beam_state, i
                          )
  
                          if self.use_lm:
                              hyp.lm_state = beam_lm_states[i]
                              hyp.lm_score = beam_lm_scores[i]
  
                      hyps = list_exp[:]
                  else:
                      beam_logp = torch.log_softmax(
                          self.joint_network(
                              beam_enc_out, beam_dec_out
                          ),
                          dim=-1,
                      )
  
                      for i, hyp in enumerate(list_exp):
                          hyp.score += float(beam_logp[i, 0])
  
                          hyp.dec_out = beam_dec_out[i]
                          hyp.dec_state = self.decoder.select_state(
                              beam_state, i
                          )
  
                          if self.use_lm:
                              hyp.lm_state = beam_lm_states[i]
                              hyp.lm_score = beam_lm_scores[i]
  
                      kept_hyps = sorted(
                          self.recombine_hyps(list_b + list_exp),
                          key=lambda x: x.score,
                          reverse=True,
                      )[: self.beam_size]
  
      return kept_hyps
  
imports: from dataclasses import dataclass
  from typing import Any, Dict, List, Optional, Tuple, Union
  import numpy
  import torch
  from espnet2.asr_transducer.decoder.abs_decoder import AbsDecoder
  from espnet2.asr_transducer.joint_network import JointNetwork
label_desc: Use image node_id 11 for calling the BeamSearchTransducer obj's underlying member method code with example usage: obj.modified_adaptive_expansion_search(enc_out) and returns: kept_hyps