class_docstr: Beam search implementation for Transducer.
class_docstr_tok: ["Beam","search","implementation","for","Transducer","."]
docstr: Alignment-length synchronous beam search implementation.
  
  Based on https://ieeexplore.ieee.org/document/9053040
  
  Args:
      h: Encoder output sequences. (T, D)
  
  Returns:
      nbest_hyps: N-best hypothesis.
docstr_tok: ["Alignment-length","synchronous","beam","search","implementation",".","Based","on","https",":","\/\/ieeexplore.ieee.org\/document\/9053040","Args",":","h",":","Encoder","output","sequences",".","(","T",",","D",")","Returns",":","nbest_hyps",":","N-best","hypothesis","."]
code: def align_length_sync_decoding(
      self, enc_out: torch.Tensor
  ) -> List[Hypothesis]:
      """Alignment-length synchronous beam search implementation.
  
      Based on https://ieeexplore.ieee.org/document/9053040
  
      Args:
          h: Encoder output sequences. (T, D)
  
      Returns:
          nbest_hyps: N-best hypothesis.
  
      """
      beam = min(self.beam_size, self.vocab_size)
  
      t_max = int(enc_out.size(0))
      u_max = min(self.u_max, (t_max - 1))
  
      beam_state = self.decoder.init_state(beam)
  
      B = [
          Hypothesis(
              yseq=[self.blank_id],
              score=0.0,
              dec_state=self.decoder.select_state(beam_state, 0),
          )
      ]
      final = []
      cache = {}
  
      if self.use_lm and not self.is_wordlm:
          B[0].lm_state = init_lm_state(self.lm_predictor)
  
      for i in range(t_max + u_max):
          A = []
  
          B_ = []
          B_enc_out = []
          for hyp in B:
              u = len(hyp.yseq) - 1
              t = i - u
  
              if t > (t_max - 1):
                  continue
  
              B_.append(hyp)
              B_enc_out.append((t, enc_out[t]))
  
          if B_:
              (
                  beam_dec_out,
                  beam_state,
                  beam_lm_tokens,
              ) = self.decoder.batch_score(
                  B_,
                  beam_state,
                  cache,
                  self.use_lm,
              )
  
              beam_enc_out = torch.stack([x[1] for x in B_enc_out])
  
              beam_logp = torch.log_softmax(
                  self.joint_network(beam_enc_out, beam_dec_out)
                  / self.softmax_temperature,
                  dim=-1,
              )
              beam_topk = beam_logp[:, 1:].topk(beam, dim=-1)
  
              if self.use_lm:
                  beam_lm_states = create_lm_batch_states(
                      [b.lm_state for b in B_],
                      self.lm_layers,
                      self.is_wordlm,
                  )
  
                  beam_lm_states, beam_lm_scores = self.lm.buff_predict(
                      beam_lm_states, beam_lm_tokens, len(B_)
                  )
  
              for i, hyp in enumerate(B_):
                  new_hyp = Hypothesis(
                      score=(hyp.score + float(beam_logp[i, 0])),
                      yseq=hyp.yseq[:],
                      dec_state=hyp.dec_state,
                      lm_state=hyp.lm_state,
                  )
  
                  A.append(new_hyp)
  
                  if B_enc_out[i][0] == (t_max - 1):
                      final.append(new_hyp)
  
                  for logp, k in zip(
                      beam_topk[0][i], beam_topk[1][i] + 1
                  ):
                      new_hyp = Hypothesis(
                          score=(hyp.score + float(logp)),
                          yseq=(hyp.yseq[:] + [int(k)]),
                          dec_state=self.decoder.select_state(
                              beam_state, i
                          ),
                          lm_state=hyp.lm_state,
                      )
  
                      if self.use_lm:
                          new_hyp.score += (
                              self.lm_weight * beam_lm_scores[i, k]
                          )
  
                          new_hyp.lm_state = select_lm_state(
                              beam_lm_states,
                              i,
                              self.lm_layers,
                              self.is_wordlm,
                          )
  
                      A.append(new_hyp)
  
              B = sorted(A, key=lambda x: x.score, reverse=True)[:beam]
              B = recombine_hyps(B)
  
      if final:
          return self.sort_nbest(final)
      else:
          return B
  
file_path: espnet/espnet/nets/beam_search_transducer.py
imports: import logging
  from typing import List, Union
  import numpy
  import torch
  from espnet.nets.pytorch_backend.transducer.custom_decoder import CustomDecoder
  from espnet.nets.pytorch_backend.transducer.joint_network import JointNetwork
  from espnet.nets.pytorch_backend.transducer.rnn_decoder import RNNDecoder
  from espnet.nets.pytorch_backend.transducer.utils import create_lm_batch_states, init_lm_state, is_prefix, recombine_hyps, select_k_expansions, select_lm_state, subtract
  from espnet.nets.transducer_decoder_interface import ExtendedHypothesis, Hypothesis
label_desc: Use image node_id 8 for calling the BeamSearchTransducer obj's underlying member method code with example usage: obj.align_length_sync_decoding(enc_out) and returns: self, B