class_docstr: Beam Search implementation ported from ESPNet implementation -
  https://github.com/espnet/espnet/blob/master/espnet/nets/beam_search_transducer.py
  
  Sequence level beam decoding or batched-beam decoding, performed auto-repressively
  depending on the search type chosen.
  
  Args:
      decoder_model: rnnt_utils.AbstractRNNTDecoder implementation.
      joint_model: rnnt_utils.AbstractRNNTJoint implementation.
  
      beam_size: number of beams for beam search. Must be a positive integer >= 1.
          If beam size is 1, defaults to stateful greedy search.
          This greedy search might result in slightly different results than
          the greedy results obtained by GreedyRNNTInfer due to implementation differences.
  
          For accurate greedy results, please use GreedyRNNTInfer or GreedyBatchedRNNTInfer.
  
      search_type: str representing the type of beam search to perform.
          Must be one of ['beam', 'tsd', 'alsd']. 'nsc' is currently not supported.
  
          Algoritm used:
          `beam` - basic beam search strategy. Larger beams generally result in better decoding,
              however the time required for the search also grows steadily.
  
          `tsd` - time synchronous decoding. Please refer to the paper:
              [Alignment-Length Synchronous Decoding for RNN Transducer](https://ieeexplore.ieee.org/document/9053040)
              for details on the algorithm implemented.
  
              Time synchronous decoding (TSD) execution time grows by the factor T * max_symmetric_expansions.
              For longer sequences, T is greater, and can therefore take a long time for beams to obtain
              good results. This also requires greater memory to execute.
  
          `alsd` - alignment-length synchronous decoding. Please refer to the paper:
              [Alignment-Length Synchronous Decoding for RNN Transducer](https://ieeexplore.ieee.org/document/9053040)
              for details on the algorithm implemented.
  
              Alignment-length synchronous decoding (ALSD) execution time is faster than TSD, with growth
              factor of T + U_max, where U_max is the maximum target length expected during execution.
  
              Generally, T + U_max < T * max_symmetric_expansions. However, ALSD beams are non-unique,
              therefore it is required to use larger beam sizes to achieve the same (or close to the same)
              decoding accuracy as TSD.
  
              For a given decoding accuracy, it is possible to attain faster decoding via ALSD than TSD.
  
          `maes` = modified adaptive expansion searcn. Please refer to the paper:
              [Accelerating RNN Transducer Inference via Adaptive Expansion Search](https://ieeexplore.ieee.org/document/9250505)
  
              Modified Adaptive Synchronous Decoding (mAES) execution time is adaptive w.r.t the
              number of expansions (for tokens) required per timestep. The number of expansions can usually
              be constrained to 1 or 2, and in most cases 2 is sufficient.
  
              This beam search technique can possibly obtain superior WER while sacrificing some evaluation time.
  
      score_norm: bool, whether to normalize the scores of the log probabilities.
  
      return_best_hypothesis: bool, decides whether to return a single hypothesis (the best out of N),
          or return all N hypothesis (sorted with best score first). The container class changes based
          this flag -
          When set to True (default), returns a single Hypothesis.
          When set to False, returns a NBestHypotheses container, which contains a list of Hypothesis.
  
      # The following arguments are specific to the chosen `search_type`
  
      tsd_max_sym_exp_per_step: Used for `search_type=tsd`. The maximum symmetric expansions allowed
          per timestep during beam search. Larger values should be used to attempt decoding of longer
          sequences, but this in turn increases execution time and memory usage.
  
      alsd_max_target_len: Used for `search_type=alsd`. The maximum expected target sequence length
          during beam search. Larger values allow decoding of longer sequences at the expense of
          execution time and memory.
  
      # The following two flags are placeholders and unused until `nsc` implementation is stabilized.
      nsc_max_timesteps_expansion: Unused int.
  
      nsc_prefix_alpha: Unused int.
  
      # mAES flags
      maes_num_steps: Number of adaptive steps to take. From the paper, 2 steps is generally sufficient. int > 1.
  
      maes_prefix_alpha: Maximum prefix length in prefix search. Must be an integer, and is advised to keep this as 1
          in order to reduce expensive beam search cost later. int >= 0.
  
      maes_expansion_beta: Maximum number of prefix expansions allowed, in addition to the beam size.
          Effectively, the number of hypothesis = beam_size + maes_expansion_beta. Must be an int >= 0,
          and affects the speed of inference since large values will perform large beam search in the next step.
  
      maes_expansion_gamma: Float pruning threshold used in the prune-by-value step when computing the expansions.
          The default (2.3) is selected from the paper. It performs a comparison (max_log_prob - gamma <= log_prob[v])
          where v is all vocabulary indices in the Vocab set and max_log_prob is the "most" likely token to be
          predicted. Gamma therefore provides a margin of additional tokens which can be potential candidates for
          expansion apart from the "most likely" candidate.
          Lower values will reduce the number of expansions (by increasing pruning-by-value, thereby improving speed
          but hurting accuracy). Higher values will increase the number of expansions (by reducing pruning-by-value,
          thereby reducing speed but potentially improving accuracy). This is a hyper parameter to be experimentally
          tuned on a validation set.
  
      softmax_temperature: Scales the logits of the joint prior to computing log_softmax.
  
      preserve_alignments: Bool flag which preserves the history of alignments generated during
          beam decoding (sample). When set to true, the Hypothesis will contain
          the non-null value for `alignments` in it. Here, `alignments` is a List of List of Tensor (of length V + 1).
  
          The length of the list corresponds to the Acoustic Length (T).
          Each value in the list (Ti) is a torch.Tensor (U), representing 1 or more targets from a vocabulary.
          U is the number of target tokens for the current timestep Ti.
  
          NOTE: `preserve_alignments` is an invalid argument for any `search_type`
          other than basic beam search.
  
      ngram_lm_model: str
          The path to the N-gram LM
      ngram_lm_alpha: float
          Alpha weight of N-gram LM
      tokens_type: str
          Tokenization type ['subword', 'char']
docstr: Score computation for kenlm ngram language model.
docstr_tok: ["Score","computation","for","kenlm","ngram","language","model","."]
code: def compute_ngram_score(
      self, current_lm_state: "kenlm.State", label: int
  ) -> Tuple[float, "kenlm.State"]:
      """
      Score computation for kenlm ngram language model.
      """
  
      if self.token_offset:
          label = chr(label + self.token_offset)
      else:
          label = str(label)
      next_state = kenlm.State()
      lm_score = self.ngram_lm.BaseScore(
          current_lm_state, label, next_state
      )
      lm_score *= 1.0 / np.log10(np.e)
  
      return lm_score, next_state
  
imports: import copy
  from dataclasses import dataclass
  from typing import Any, Dict, List, Optional, Tuple, Union
  import numpy
  import torch
  from tqdm import tqdm
  from nemo.collections.asr.modules import rnnt_abstract
  from nemo.collections.asr.parts.utils.rnnt_utils import HATJointOutput, Hypothesis, NBestHypotheses, is_prefix, select_k_expansions
  from nemo.core.classes import Typing, typecheck
  from nemo.core.neural_types import AcousticEncodedRepresentation, HypothesisType, LengthsType, NeuralType
  from nemo.utils import logging
label_desc: Use image node_id 14 for calling the BeamRNNTInfer obj's underlying member method code with example usage: obj.compute_ngram_score(current_lm_state, label) and returns: lm_score,  next_state