class_docstr: This class implements the CTC prefix score of Algorithm 2 in
  reference: https://www.merl.com/publications/docs/TR2017-190.pdf.
  Official implementation: https://github.com/espnet/espnet/blob/master/espnet/nets/ctc_prefix_score.py
  
  Arguments
  ---------
  x : torch.Tensor
      The encoder states.
  enc_lens : torch.Tensor
      The actual length of each enc_states sequence.
  batch_size : int
      The size of the batch.
  beam_size : int
      The width of beam.
  blank_index : int
      The index of the blank token.
  eos_index : int
      The index of the end-of-sequence (eos) token.
  ctc_window_size: int
      Compute the ctc scores over the time frames using windowing based on attention peaks.
      If 0, no windowing applied.
class_docstr_tok: ["This","class","implements","the","CTC","prefix","score","of","Algorithm","2","in","reference",":","https",":","\/\/www.merl.com\/publications\/docs\/TR2017-190.pdf",".","Official","implementation",":","https",":","\/\/github.com\/espnet\/espnet\/blob\/master\/espnet\/nets\/ctc_prefix_score.py","Arguments","--","--","--","--","-","x",":","torch.Tensor","The","encoder","states",".","enc_lens",":","torch.Tensor","The","actual","length","of","each","enc_states","sequence",".","batch_size",":","int","The","size","of","the","batch",".","beam_size",":","int","The","width","of","beam",".","blank_index",":","int","The","index","of","the","blank","token",".","eos_index",":","int","The","index","of","the","end-of-sequence","(","eos",")","token",".","ctc_window_size",":","int","Compute","the","ctc","scores","over","the","time","frames","using","windowing","based","on","attention","peaks",".","If","0",",","no","windowing","applied","."]
docstr: This method permutes the CTC model memory
  to synchronize the memory index with the current output.
  
  Arguments
  ---------
  memory : No limit
      The memory variable to be permuted.
  index : torch.Tensor
      The index of the previous path.
  
  Return
  ------
  The variable of the memory being permuted.
docstr_tok: ["This","method","permutes","the","CTC","model","memory","to","synchronize","the","memory","index","with","the","current","output",".","Arguments","--","--","--","--","-","memory",":","No","limit","The","memory","variable","to","be","permuted",".","index",":","torch.Tensor","The","index","of","the","previous","path",".","Return","--","--","--","The","variable","of","the","memory","being","permuted","."]
code: def permute_mem(self, memory, index):
      """This method permutes the CTC model memory
      to synchronize the memory index with the current output.
  
      Arguments
      ---------
      memory : No limit
          The memory variable to be permuted.
      index : torch.Tensor
          The index of the previous path.
  
      Return
      ------
      The variable of the memory being permuted.
  
      """
  
      r, psi, scoring_table = memory
  
      beam_size = index.size(1)
      n_bh = self.batch_size * beam_size
  
      # The first index of each batch.
      beam_offset = self.batch_index * beam_size
      # The index of top-K vocab came from in (t-1) timesteps at batch * beam * vocab dimension.
      cand_index = (
          index
          + beam_offset.unsqueeze(1).expand_as(index) * self.vocab_size
      ).view(n_bh)
      # synchronize forward prob
      psi = torch.index_select(psi.view(-1), dim=0, index=cand_index)
      psi = (
          psi.view(-1, 1)
          .repeat(1, self.vocab_size)
          .view(n_bh, self.vocab_size)
      )
      # The index of top-K vocab came from in (t-1) timesteps at batch * beam dimension.
      hyp_index = (
          torch.div(index, self.vocab_size, rounding_mode="floor")
          + beam_offset.unsqueeze(1).expand_as(index)
      ).view(n_bh)
      # synchronize ctc states
      if scoring_table is not None:
          selected_vocab = (index % self.vocab_size).view(-1)
          score_index = scoring_table[hyp_index, selected_vocab]
          score_index[score_index == -1] = 0
          cand_index = score_index + hyp_index * self.num_candidates
  
      r = torch.index_select(
          r.view(-1, 2, n_bh * self.num_candidates),
          dim=-1,
          index=cand_index,
      )
      r = r.view(-1, 2, n_bh)
  
      return r, psi
  
imports: from itertools import groupby
  from speechbrain.dataio.dataio import length_to_mask
  import math
  import dataclasses
  import numpy
  import heapq
  import logging
  import torch
  import warnings
  from typing import Dict, List, Optional, Union, Any, Tuple
label_desc: Use image node_id 3 for calling the CTCPrefixScore obj's underlying member method code with example usage: obj.permute_mem(memory, index) and returns: r,  psi