class_docstr: This class calculates Viterbi alignments in the forward method.
  
  It also records alignments and creates batches of them for use
  in Viterbi training.
  
  Arguments
  ---------
  states_per_phoneme : int
      Number of hidden states to use per phoneme.
  output_folder : str
      It is the folder that the alignments will be stored in when
      saved to disk. Not yet implemented.
  neg_inf : float
      The float used to represent a negative infinite log probability.
      Using `-float("Inf")` tends to give numerical instability.
      A number more negative than -1e5 also sometimes gave errors when
      the `genbmm` library was used (currently not in use). (default: -1e5)
  batch_reduction : string
      One of "none", "sum" or "mean".
      What kind of batch-level reduction to apply to the loss calculated
      in the forward method.
  input_len_norm : bool
      Whether to normalize the loss in the forward method by the length of
      the inputs.
  target_len_norm : bool
      Whether to normalize the loss in the forward method by the length of
      the targets.
  lexicon_path : string
      The location of the lexicon.
  
  Example
  -------
  >>> log_posteriors = torch.tensor([[[ -1., -10., -10.],
  ...                                 [-10.,  -1., -10.],
  ...                                 [-10., -10.,  -1.]],
  ...
  ...                                [[ -1., -10., -10.],
  ...                                 [-10.,  -1., -10.],
  ...                                 [-10., -10., -10.]]])
  >>> lens = torch.tensor([1., 0.66])
  >>> phns = torch.tensor([[0, 1, 2],
  ...                      [0, 1, 0]])
  >>> phn_lens = torch.tensor([1., 0.66])
  >>> aligner = HMMAligner()
  >>> forward_scores = aligner(
  ...        log_posteriors, lens, phns, phn_lens, 'forward'
  ... )
  >>> forward_scores.shape
  torch.Size([2])
  >>> viterbi_scores, alignments = aligner(
  ...        log_posteriors, lens, phns, phn_lens, 'viterbi'
  ... )
  >>> alignments
  [[0, 1, 2], [0, 1]]
  >>> viterbi_scores.shape
  torch.Size([2])
docstr: Prepares relevant (log) probability tensors and does dynamic
  programming: either the forward or the Viterbi algorithm. Applies
  reduction as specified during object initialization.
  
  Arguments
  ---------
  emission_pred : torch.Tensor (batch, time, phoneme in vocabulary)
      Posterior probabilities from our acoustic model.
  lens : torch.Tensor (batch)
      The relative duration of each utterance sound file.
  phns : torch.Tensor (batch, phoneme in phn sequence)
      The phonemes that are known/thought to be in each utterance
  phn_lens : torch.Tensor (batch)
      The relative length of each phoneme sequence in the batch.
  dp_algorithm : string
      Either "forward" or "viterbi".
  prob_matrices : dict
      (Optional) Must contain keys 'trans_prob', 'pi_prob' and 'final_states'.
      Used to override the default forward and viterbi operations which
      force traversal over all of the states in the `phns` sequence.
  
  Returns
  -------
  tensor
  
      (1) if dp_algorithm == "forward".
  
          ``forward_scores`` : torch.Tensor (batch, or scalar)
  
          The (log) likelihood of each utterance in the batch, with reduction
          applied if specified. (OR)
  
      (2) if dp_algorithm == "viterbi".
  
          ``viterbi_scores`` : torch.Tensor (batch, or scalar)
  
          The (log) likelihood of the Viterbi path for each utterance, with
          reduction applied if specified.
  
          ``alignments`` : list of lists of int
  
          Viterbi alignments for the files in the batch.
docstr_tok: ["Prepares","relevant","(","log",")","probability","tensors","and","does","dynamic","programming",":","either","the","forward","or","the","Viterbi","algorithm",".","Applies","reduction","as","specified","during","object","initialization",".","Arguments","--","--","--","--","-","emission_pred",":","torch.Tensor","(","batch",",","time",",","phoneme","in","vocabulary",")","Posterior","probabilities","from","our","acoustic","model",".","lens",":","torch.Tensor","(","batch",")","The","relative","duration","of","each","utterance","sound","file",".","phns",":","torch.Tensor","(","batch",",","phoneme","in","phn","sequence",")","The","phonemes","that","are","known\/thought","to","be","in","each","utterance","phn_lens",":","torch.Tensor","(","batch",")","The","relative","length","of","each","phoneme","sequence","in","the","batch",".","dp_algorithm",":","string","Either","``","forward","''","or","``","viterbi","''",".","prob_matrices",":","dict","(","Optional",")","Must","contain","keys","'trans_prob","'",",","'pi_prob","'","and","'final_states","'",".","Used","to","override","the","default","forward","and","viterbi","operations","which","force","traversal","over","all","of","the","states","in","the","`","phns","`","sequence",".","Returns","--","--","--","-","tensor","(","1",")","if","dp_algorithm","==","``","forward","''",".","``","forward_scores","``",":","torch.Tensor","(","batch",",","or","scalar",")","The","(","log",")","likelihood","of","each","utterance","in","the","batch",",","with","reduction","applied","if","specified",".","(","OR",")","(","2",")","if","dp_algorithm","==","``","viterbi","''",".","``","viterbi_scores","``",":","torch.Tensor","(","batch",",","or","scalar",")","The","(","log",")","likelihood","of","the","Viterbi","path","for","each","utterance",",","with","reduction","applied","if","specified",".","``","alignments","``",":","list","of","lists","of","int","Viterbi","alignments","for","the","files","in","the","batch","."]
code: def forward(
      self,
      emission_pred,
      lens,
      phns,
      phn_lens,
      dp_algorithm,
      prob_matrices=None,
  ):
      """Prepares relevant (log) probability tensors and does dynamic
      programming: either the forward or the Viterbi algorithm. Applies
      reduction as specified during object initialization.
  
      Arguments
      ---------
      emission_pred : torch.Tensor (batch, time, phoneme in vocabulary)
          Posterior probabilities from our acoustic model.
      lens : torch.Tensor (batch)
          The relative duration of each utterance sound file.
      phns : torch.Tensor (batch, phoneme in phn sequence)
          The phonemes that are known/thought to be in each utterance
      phn_lens : torch.Tensor (batch)
          The relative length of each phoneme sequence in the batch.
      dp_algorithm : string
          Either "forward" or "viterbi".
      prob_matrices : dict
          (Optional) Must contain keys 'trans_prob', 'pi_prob' and 'final_states'.
          Used to override the default forward and viterbi operations which
          force traversal over all of the states in the `phns` sequence.
  
      Returns
      -------
      tensor
  
          (1) if dp_algorithm == "forward".
  
              ``forward_scores`` : torch.Tensor (batch, or scalar)
  
              The (log) likelihood of each utterance in the batch, with reduction
              applied if specified. (OR)
  
          (2) if dp_algorithm == "viterbi".
  
              ``viterbi_scores`` : torch.Tensor (batch, or scalar)
  
              The (log) likelihood of the Viterbi path for each utterance, with
              reduction applied if specified.
  
              ``alignments`` : list of lists of int
  
              Viterbi alignments for the files in the batch.
      """
  
      lens_abs = torch.round(emission_pred.shape[1] * lens).long()
      phn_lens_abs = torch.round(phns.shape[1] * phn_lens).long()
      phns = phns.long()
  
      if prob_matrices is None:
          pi_prob = self._make_pi_prob(phn_lens_abs)
          trans_prob = self._make_trans_prob(phn_lens_abs)
          final_states = None
      else:
          if (
              ("pi_prob" in prob_matrices)
              and ("trans_prob" in prob_matrices)
              and ("final_states" in prob_matrices)
          ):
              pi_prob = prob_matrices["pi_prob"]
              trans_prob = prob_matrices["trans_prob"]
              final_states = prob_matrices["final_states"]
          else:
              ValueError(
                  """`prob_matrices` must contain the keys
                  `pi_prob`, `trans_prob` and `final_states`"""
              )
  
      emiss_pred_useful = self._make_emiss_pred_useful(
          emission_pred, lens_abs, phn_lens_abs, phns
      )
  
      if dp_algorithm == "forward":
          # do forward training
          forward_scores = self._dp_forward(
              pi_prob,
              trans_prob,
              emiss_pred_useful,
              lens_abs,
              phn_lens_abs,
              phns,
          )
  
          forward_scores = self._loss_reduction(
              forward_scores, lens_abs, phn_lens_abs
          )
  
          return forward_scores
  
      elif dp_algorithm == "viterbi":
          alignments, _, viterbi_scores = self._dp_viterbi(
              pi_prob,
              trans_prob,
              emiss_pred_useful,
              lens_abs,
              phn_lens_abs,
              phns,
              final_states,
          )
  
          viterbi_scores = self._loss_reduction(
              viterbi_scores, lens_abs, phn_lens_abs
          )
  
          return viterbi_scores, alignments
  
      else:
          raise ValueError(
              "dp_algorithm input must be either 'forward' or 'viterbi'"
          )
  
imports: import torch
  import random
  from speechbrain.utils.checkpoints import register_checkpoint_hooks
  from speechbrain.utils.checkpoints import mark_as_saver
  from speechbrain.utils.checkpoints import mark_as_loader
  from speechbrain.utils.data_utils import undo_padding
label_desc: Use image node_id 10 for calling the HMMAligner obj's underlying member method code with example usage: obj.forward(emission_pred, lens, phns, phn_lens, dp_algorithm, prob_matrices) and returns: forward_scores, viterbi_scores,  alignments