class_docstr: This class implements a model-specific automatic speech recognizer using the end-to-end speech recognizer in
  Espresso.
  
  | Paper link: https://arxiv.org/abs/1909.08723
class_docstr_tok: ["This","class","implements","a","model-specific","automatic","speech","recognizer","using","the","end-to-end","speech","recognizer","in","Espresso",".","|","Paper","link",":","https",":","\/\/arxiv.org\/abs\/1909.08723"]
docstr: Transform the user input space into the model input space.
  
  :param x: Samples of shape (nb_samples, seq_length). Note that, it is allowable that sequences in the batch
            could have different lengths. A possible example of `x` could be:
            `x = np.ndarray([[0.1, 0.2, 0.1, 0.4], [0.3, 0.1]])`.
  :param y: Target values of shape (nb_samples). Each sample in `y` is a string and it may possess different
            lengths. A possible example of `y` could be: `y = np.array(['SIXTY ONE', 'HELLO'])`.
  :param compute_gradient: Indicate whether to compute gradients for the input `x`.
  :return: A tuple of a dictionary of batch and a list representing the original order of the batch
docstr_tok: ["Transform","the","user","input","space","into","the","model","input","space",".",":","param","x",":","Samples","of","shape","(","nb_samples",",","seq_length",")",".","Note","that",",","it","is","allowable","that","sequences","in","the","batch","could","have","different","lengths",".","A","possible","example","of","`","x","`","could","be",":","`","x","=","np.ndarray","(","[","[","0.1",",","0.2",",","0.1",",","0.4","]",",","[","0.3",",","0.1","]","]",")","`",".",":","param","y",":","Target","values","of","shape","(","nb_samples",")",".","Each","sample","in","`","y","`","is","a","string","and","it","may","possess","different","lengths",".","A","possible","example","of","`","y","`","could","be",":","`","y","=","np.array","(","[","'SIXTY","ONE","'",",","'HELLO","'","]",")","`",".",":","param","compute_gradient",":","Indicate","whether","to","compute","gradients","for","the","input","`","x","`",".",":","return",":","A","tuple","of","a","dictionary","of","batch","and","a","list","representing","the","original","order","of","the","batch"]
code: def _transform_model_input(
      self,
      x: Union[np.ndarray, "torch.Tensor"],
      y: Optional[np.ndarray] = None,
      compute_gradient: bool = False,
  ) -> Tuple[Dict, List]:
      """
      Transform the user input space into the model input space.
  
      :param x: Samples of shape (nb_samples, seq_length). Note that, it is allowable that sequences in the batch
                could have different lengths. A possible example of `x` could be:
                `x = np.ndarray([[0.1, 0.2, 0.1, 0.4], [0.3, 0.1]])`.
      :param y: Target values of shape (nb_samples). Each sample in `y` is a string and it may possess different
                lengths. A possible example of `y` could be: `y = np.array(['SIXTY ONE', 'HELLO'])`.
      :param compute_gradient: Indicate whether to compute gradients for the input `x`.
      :return: A tuple of a dictionary of batch and a list representing the original order of the batch
      """
      import torch
      from fairseq.data import data_utils
  
      def _collate_fn(batch: List) -> dict:
          """
          Collate function that transforms a list of numpy array or torch tensor representing a batch into a
          dictionary that Espresso takes as input.
          """
          # sort by seq length in descending order
          batch = sorted(
              batch, key=lambda t: t[0].size(0), reverse=True
          )
          batch_size = len(batch)
          max_seqlength = batch[0][0].size(0)
          src_frames = torch.zeros(batch_size, max_seqlength, 1)
          src_lengths = torch.zeros(batch_size, dtype=torch.long)
  
          for i, (sample, _) in enumerate(batch):
              seq_length = sample.size(0)
              src_frames[i, :seq_length, :] = sample.unsqueeze(1)
              src_lengths[i] = seq_length
  
          if compute_gradient:
              src_frames = torch.tensor(src_frames, requires_grad=True)
              src_frames.requires_grad = True
  
          # for input feeding in training
          if batch[0][1] is not None:
              pad_idx = self.dictionary.pad()
              eos_idx = self.dictionary.eos()
              target = data_utils.collate_tokens(
                  [s[1] for s in batch],
                  pad_idx,
                  eos_idx,
                  False,
                  False,
                  pad_to_length=None,
                  pad_to_multiple=1,
              )
              prev_output_tokens = data_utils.collate_tokens(
                  [s[1] for s in batch],
                  pad_idx,
                  eos_idx,
                  False,
                  True,
                  pad_to_length=None,
                  pad_to_multiple=1,
              )
              target = target.long().to(self._device)
              prev_output_tokens = prev_output_tokens.long().to(
                  self._device
              )
              ntokens = sum(
                  s[1].ne(pad_idx).int().sum().item() for s in batch
              )
  
          else:
              target = None
              prev_output_tokens = None
              ntokens = None
  
          batch_dict = {
              "ntokens": ntokens,
              "net_input": {
                  "src_tokens": src_frames.to(self._device),
                  "src_lengths": src_lengths.to(self._device),
                  "prev_output_tokens": prev_output_tokens,
              },
              "target": target,
          }
  
          return batch_dict
  
      # We must process each sequence separately due to the diversity of their length
      batch = []
      for i, _ in enumerate(x):
          # First process the target
          if y is None:
              target = None
          else:
              eap = self.spp.EncodeAsPieces(y[i])
              sp_string = " ".join(eap)
              target = self.dictionary.encode_line(
                  sp_string, add_if_not_exist=False
              )  # target is a long tensor
  
          # Push the sequence to device
          if isinstance(x, np.ndarray):
              x[i] = x[i].astype(config.ART_NUMPY_DTYPE)
              x[i] = torch.tensor(x[i]).to(self._device)
  
          # Set gradient computation permission
          if compute_gradient:
              x[i].requires_grad = True
  
          # Re-scale the input audio to the magnitude used to train Espresso model
          x[i] = x[i] * 32767
  
          # Form the batch
          batch.append((x[i], target))
  
      # We must keep the order of the batch for later use as the following function will change its order
      batch_idx = sorted(
          range(len(batch)),
          key=lambda i: batch[i][0].size(0),
          reverse=True,
      )
  
      # The collate function is important to convert input into model space
      batch_dict = _collate_fn(batch)
  
      # return inputs, targets, input_percentages, target_sizes, batch_idx
      return batch_dict, batch_idx
  
imports: import ast
  from argparse import Namespace
  import logging
  from typing import Dict, List, Optional, Tuple, TYPE_CHECKING, Union
  import numpy
  from art import config
  from art.estimators.pytorch import PyTorchEstimator
  from art.estimators.speech_recognition.speech_recognizer import SpeechRecognizerMixin, PytorchSpeechRecognizerMixin
  from art.utils import get_file
label_desc: Use image node_id 5 for calling the PyTorchEspresso obj's underlying member method code with example usage: obj._transform_model_input(x, y, compute_gradient) and returns: batch_dict,  batch_idx, batch_dict