





import numpy as np
import torch
from torch.utils.data import Dataset, IterableDataset

from ..utils.generic import ModelOutput






class        snake_case_    (		__lowercase						):






  def __init__(							self			:					List[Any]       ,  _snake_case			:					List[str]       ,  _snake_case			:					str       ,  _snake_case			:					str      )->Optional[Any]:






   '''simple docstring'''

   __lowerCAmelCase					:	Optional[int]      =       dataset
   __lowerCAmelCase					:	Optional[int]      =       process
   __lowerCAmelCase					:	int      =       params






  def __len__(							self			:					Any      )->Any:






   '''simple docstring'''

   return len(self.dataset      )






  def __getitem__(							self			:					Optional[int]       ,  _snake_case			:					int      )->int:






   '''simple docstring'''

   __lowerCAmelCase					:	Tuple      =       self.dataset[i]
   __lowerCAmelCase					:	Any      =       self.process(_snake_case       ,  **self.params      )
   return processed






class        snake_case_    (		__lowercase						):






  def __init__(							self			:					str       ,  _snake_case			:					str       ,  _snake_case			:					Tuple       ,  _snake_case			:					Union[str, Any]       ,  _snake_case			:					str=None      )->Optional[Any]:






   '''simple docstring'''

   __lowerCAmelCase					:	List[Any]      =       loader
   __lowerCAmelCase					:	Tuple      =       infer
   __lowerCAmelCase					:	Dict      =       params
   if loader_batch_size == 1:
    # Let's spare some time by deactivating altogether
    __lowerCAmelCase					:	Union[str, Any]      =       None
   __lowerCAmelCase					:	List[str]      =       loader_batch_size

   # Internal bookkeeping
   __lowerCAmelCase					:	List[str]      =       None
   __lowerCAmelCase					:	Tuple      =       None






  def __len__(							self			:					Optional[Any]      )->int:






   '''simple docstring'''

   return len(self.loader      )






  def __iter__(							self			:					Optional[int]      )->List[Any]:






   '''simple docstring'''

   __lowerCAmelCase					:	Tuple      =       iter(self.loader      )
   return self






  def 				UpperCAmelCase__						(							self			:					Dict      )->int:






   '''simple docstring'''

   if isinstance(self._loader_batch_data       ,  torch.Tensor      ):
    # Batch data is simple tensor, just fetch the slice
    __lowerCAmelCase					:	str      =       self._loader_batch_data[self._loader_batch_index]
   else:
    # Batch data is assumed to be BaseModelOutput (or dict)
    __lowerCAmelCase					:	Any      =       {}
    for k, element in self._loader_batch_data.items():
     if isinstance(_snake_case       ,  _snake_case      ):
      # Convert ModelOutput to tuple first
      __lowerCAmelCase					:	Any      =       element.to_tuple()
      if isinstance(element[0]       ,  torch.Tensor      ):
       __lowerCAmelCase					:	Dict      =       tuple(el[self._loader_batch_index].unsqueeze(0      ) for el in element      )
      elif isinstance(element[0]       ,  np.ndarray      ):
       __lowerCAmelCase					:	List[str]      =       tuple(np.expand_dims(el[self._loader_batch_index]       ,  0      ) for el in element      )
      continue
     if k in {"hidden_states", "past_key_values", "attentions"} and isinstance(_snake_case       ,  _snake_case      ):
      # Those are stored as lists of tensors so need specific unbatching.
      if isinstance(element[0]       ,  torch.Tensor      ):
       __lowerCAmelCase					:	int      =       tuple(el[self._loader_batch_index].unsqueeze(0      ) for el in element      )
      elif isinstance(element[0]       ,  np.ndarray      ):
       __lowerCAmelCase					:	Dict      =       tuple(np.expand_dims(el[self._loader_batch_index]       ,  0      ) for el in element      )
      continue
     if element is None:
      # This can happen for optional data that get passed around
      __lowerCAmelCase					:	List[Any]      =       None
     elif isinstance(element[self._loader_batch_index]       ,  torch.Tensor      ):
      # Take correct batch data, but make it looked like batch_size=1
      # For compatibility with other methods within transformers

      __lowerCAmelCase					:	List[Any]      =       element[self._loader_batch_index].unsqueeze(0      )
     elif isinstance(element[self._loader_batch_index]       ,  np.ndarray      ):
      # Take correct batch data, but make it looked like batch_size=1
      # For compatibility with other methods within transformers
      __lowerCAmelCase					:	List[str]      =       np.expand_dims(element[self._loader_batch_index]       ,  0      )
     else:
      # This is typically a list, so no need to `unsqueeze`.
      __lowerCAmelCase					:	str      =       element[self._loader_batch_index]
            # Recreate the element by reusing the original class to make it look
            # batch_size=1
    __lowerCAmelCase					:	str      =       self._loader_batch_data.__class__(_snake_case      )
   self._loader_batch_index += 1
   return result






  def 				UpperCAmelCase__						(							self			:					str      )->Any:






   '''simple docstring'''

   if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size:
    # We are currently unrolling a batch so we just need to return
    # the current item within a batch
    return self.loader_batch_item()

   # We're out of items within a batch
   __lowerCAmelCase					:	Tuple      =       next(self.iterator      )
   __lowerCAmelCase					:	Optional[Any]      =       self.infer(_snake_case       ,  **self.params      )
   # We now have a batch of "inferred things".
   if self.loader_batch_size is not None:
    # Try to infer the size of the batch
    if isinstance(_snake_case       ,  torch.Tensor      ):
     __lowerCAmelCase					:	Optional[Any]      =       processed
    else:
     __lowerCAmelCase					:	Tuple      =       list(processed.keys()      )[0]
     __lowerCAmelCase					:	int      =       processed[key]
    if isinstance(_snake_case       ,  _snake_case      ):
     __lowerCAmelCase					:	str      =       len(_snake_case      )
    else:
     __lowerCAmelCase					:	int      =       first_tensor.shape[0]
    if 0 < observed_batch_size < self.loader_batch_size:
     # could be last batch so we can't unroll as many
     # elements.
     __lowerCAmelCase					:	List[str]      =       observed_batch_size
    # Setting internal index to unwrap the batch
    __lowerCAmelCase					:	int      =       processed
    __lowerCAmelCase					:	Optional[int]      =       0
    return self.loader_batch_item()
   else:
    # We're not unrolling batches
    return processed






class        snake_case_    (		__lowercase						):






  def __init__(							self			:					List[Any]       ,  _snake_case			:					Tuple       ,  _snake_case			:					List[Any]       ,  _snake_case			:					List[str]       ,  _snake_case			:					List[str]=None      )->str:






   '''simple docstring'''

   super().__init__(_snake_case       ,  _snake_case       ,  _snake_case      )






  def __iter__(							self			:					Union[str, Any]      )->List[Any]:






   '''simple docstring'''

   __lowerCAmelCase					:	Dict      =       iter(self.loader      )
   __lowerCAmelCase					:	Tuple      =       None
   return self






  def 				UpperCAmelCase__						(							self			:					Optional[int]      )->Optional[Any]:






   '''simple docstring'''

   if self.subiterator is None:
    __lowerCAmelCase					:	Optional[Any]      =       self.infer(next(self.iterator      )       ,  **self.params      )
   try:
    # Try to return next item
    __lowerCAmelCase					:	Any      =       next(self.subiterator      )
   except StopIteration:
    # When a preprocess iterator ends, we can start lookig at the next item
    # ChunkIterator will keep feeding until ALL elements of iterator
    # all have created their subiterator and have been iterating against.
    #
    # Another way to look at it, is we're basically flattening lists of lists
    # into a single list, but with generators
    __lowerCAmelCase					:	List[str]      =       self.infer(next(self.iterator      )       ,  **self.params      )
    __lowerCAmelCase					:	str      =       next(self.subiterator      )
   return processed






class        snake_case_    (		__lowercase						):






  def __iter__(							self			:					Union[str, Any]      )->Dict:






   '''simple docstring'''

   __lowerCAmelCase					:	List[str]      =       iter(self.loader      )
   return self






  def 				UpperCAmelCase__						(							self			:					List[str]      )->Any:






   '''simple docstring'''

   __lowerCAmelCase					:	Union[str, Any]      =       False
   __lowerCAmelCase					:	Union[str, Any]      =       []
   if self._loader_batch_index is not None and self._loader_batch_index < self.loader_batch_size:
    while self._loader_batch_index < self.loader_batch_size:
     __lowerCAmelCase					:	Tuple      =       self.loader_batch_item()
     __lowerCAmelCase					:	Optional[Any]      =       item.pop("""is_last"""      )
     accumulator.append(_snake_case      )
     if is_last:
      return accumulator

   while not is_last:
    __lowerCAmelCase					:	str      =       self.infer(next(self.iterator      )       ,  **self.params      )
    if self.loader_batch_size is not None:
     if isinstance(_snake_case       ,  torch.Tensor      ):
      __lowerCAmelCase					:	str      =       processed
     else:
      __lowerCAmelCase					:	Tuple      =       list(processed.keys()      )[0]
      __lowerCAmelCase					:	str      =       processed[key]
     if isinstance(_snake_case       ,  _snake_case      ):
      __lowerCAmelCase					:	int      =       len(_snake_case      )
     else:
      __lowerCAmelCase					:	Any      =       first_tensor.shape[0]
     if 0 < observed_batch_size < self.loader_batch_size:
      # could be last batch so we can't unroll as many
      # elements.
      __lowerCAmelCase					:	int      =       observed_batch_size
     __lowerCAmelCase					:	Tuple      =       processed
     __lowerCAmelCase					:	Optional[Any]      =       0
     while self._loader_batch_index < self.loader_batch_size:
      __lowerCAmelCase					:	Optional[Any]      =       self.loader_batch_item()
      __lowerCAmelCase					:	int      =       item.pop("""is_last"""      )
      accumulator.append(_snake_case      )
      if is_last:
       return accumulator
    else:
     __lowerCAmelCase					:	Any      =       processed
     __lowerCAmelCase					:	Any      =       item.pop("""is_last"""      )
     accumulator.append(_snake_case      )
   return accumulator






class        snake_case_    (		__lowercase						):






  def __init__(							self			:					Tuple       ,  _snake_case			:					Dataset       ,  _snake_case			:					str      )->List[str]:






   '''simple docstring'''

   __lowerCAmelCase					:	Union[str, Any]      =       dataset
   __lowerCAmelCase					:	int      =       key






  def __len__(							self			:					List[str]      )->Optional[Any]:






   '''simple docstring'''

   return len(self.dataset      )






  def __getitem__(							self			:					Tuple       ,  _snake_case			:					List[Any]      )->Union[str, Any]:






   '''simple docstring'''

   return self.dataset[i][self.key]






class        snake_case_    (		__lowercase						):






  def __init__(							self			:					Any       ,  _snake_case			:					Dataset       ,  _snake_case			:					str       ,  _snake_case			:					str      )->Tuple:






   '''simple docstring'''

   __lowerCAmelCase					:	Any      =       dataset
   __lowerCAmelCase					:	int      =       keya
   __lowerCAmelCase					:	List[Any]      =       keya






  def __len__(							self			:					List[Any]      )->Tuple:






   '''simple docstring'''

   return len(self.dataset      )






  def __getitem__(							self			:					Any       ,  _snake_case			:					Union[str, Any]      )->List[str]:






   '''simple docstring'''

   return {"text": self.dataset[i][self.keya], "text_pair": self.dataset[i][self.keya]}






import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_UpperCAmelCase             =							logging.get_logger(__name__)

_UpperCAmelCase             =							{
    'microsoft/unispeech-sat-base-100h-libri-ft': (
        'https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json'
    ),
    # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat
}






class        snake_case_    (		__lowercase						):
  A_      							=   'unispeech-sat'






  def __init__(							self			:					str       ,  _snake_case			:					List[Any]=32       ,  _snake_case			:					Union[str, Any]=768       ,  _snake_case			:					Tuple=12       ,  _snake_case			:					Optional[int]=12       ,  _snake_case			:					Optional[Any]=3072       ,  _snake_case			:					Tuple="gelu"       ,  _snake_case			:					int=0.1       ,  _snake_case			:					List[Any]=0.1       ,  _snake_case			:					Union[str, Any]=0.1       ,  _snake_case			:					str=0.0       ,  _snake_case			:					List[str]=0.0       ,  _snake_case			:					int=0.1       ,  _snake_case			:					Optional[Any]=0.1       ,  _snake_case			:					Optional[Any]=0.02       ,  _snake_case			:					int=1E-5       ,  _snake_case			:					Dict="group"       ,  _snake_case			:					Optional[Any]="gelu"       ,  _snake_case			:					Optional[Any]=(512, 512, 512, 512, 512, 512, 512)       ,  _snake_case			:					int=(5, 2, 2, 2, 2, 2, 2)       ,  _snake_case			:					int=(10, 3, 3, 3, 3, 2, 2)       ,  _snake_case			:					Any=False       ,  _snake_case			:					Optional[Any]=128       ,  _snake_case			:					Tuple=16       ,  _snake_case			:					str=False       ,  _snake_case			:					Dict=True       ,  _snake_case			:					Tuple=0.05       ,  _snake_case			:					str=10       ,  _snake_case			:					Tuple=2       ,  _snake_case			:					List[Any]=0.0       ,  _snake_case			:					str=10       ,  _snake_case			:					Any=0       ,  _snake_case			:					List[Any]=320       ,  _snake_case			:					Union[str, Any]=2       ,  _snake_case			:					Dict=0.1       ,  _snake_case			:					Dict=100       ,  _snake_case			:					Union[str, Any]=256       ,  _snake_case			:					int=256       ,  _snake_case			:					Union[str, Any]=0.1       ,  _snake_case			:					Optional[Any]="mean"       ,  _snake_case			:					int=False       ,  _snake_case			:					str=False       ,  _snake_case			:					str=256       ,  _snake_case			:					List[Any]=(512, 512, 512, 512, 1500)       ,  _snake_case			:					Optional[int]=(5, 3, 3, 1, 1)       ,  _snake_case			:					Tuple=(1, 2, 3, 1, 1)       ,  _snake_case			:					Dict=512       ,  _snake_case			:					Union[str, Any]=0       ,  _snake_case			:					List[str]=1       ,  _snake_case			:					Optional[Any]=2       ,  _snake_case			:					Optional[int]=504       ,  **_snake_case			:					Optional[int]       ,  )->Union[str, Any]:






   '''simple docstring'''

   super().__init__(**_snake_case       ,  pad_token_id=_snake_case       ,  bos_token_id=_snake_case       ,  eos_token_id=_snake_case      )
   __lowerCAmelCase					:	Dict      =       hidden_size
   __lowerCAmelCase					:	List[Any]      =       feat_extract_norm
   __lowerCAmelCase					:	int      =       feat_extract_activation
   __lowerCAmelCase					:	Union[str, Any]      =       list(_snake_case      )
   __lowerCAmelCase					:	str      =       list(_snake_case      )
   __lowerCAmelCase					:	Optional[Any]      =       list(_snake_case      )
   __lowerCAmelCase					:	Optional[int]      =       conv_bias
   __lowerCAmelCase					:	Dict      =       num_conv_pos_embeddings
   __lowerCAmelCase					:	List[Any]      =       num_conv_pos_embedding_groups
   __lowerCAmelCase					:	Tuple      =       len(self.conv_dim      )
   __lowerCAmelCase					:	int      =       num_hidden_layers
   __lowerCAmelCase					:	str      =       intermediate_size
   __lowerCAmelCase					:	str      =       hidden_act
   __lowerCAmelCase					:	Any      =       num_attention_heads
   __lowerCAmelCase					:	Optional[int]      =       hidden_dropout
   __lowerCAmelCase					:	str      =       attention_dropout
   __lowerCAmelCase					:	int      =       activation_dropout
   __lowerCAmelCase					:	Union[str, Any]      =       feat_proj_dropout
   __lowerCAmelCase					:	List[str]      =       final_dropout
   __lowerCAmelCase					:	Dict      =       layerdrop
   __lowerCAmelCase					:	Tuple      =       layer_norm_eps
   __lowerCAmelCase					:	Optional[Any]      =       initializer_range
   __lowerCAmelCase					:	str      =       vocab_size
   __lowerCAmelCase					:	Optional[int]      =       num_clusters
   __lowerCAmelCase					:	List[Any]      =       do_stable_layer_norm
   __lowerCAmelCase					:	Tuple      =       use_weighted_layer_sum

   if (
       (len(self.conv_stride      ) != self.num_feat_extract_layers)
       or (len(self.conv_kernel      ) != self.num_feat_extract_layers)
       or (len(self.conv_dim      ) != self.num_feat_extract_layers)
   ):
    raise ValueError(
        """Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="""
        """ `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="""
        F''' {len(self.conv_dim      )}`, `len(config.conv_stride) = {len(self.conv_stride      )}`,'''
        F''' `len(config.conv_kernel) = {len(self.conv_kernel      )}`.'''      )

   # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
   __lowerCAmelCase					:	Dict      =       apply_spec_augment
   __lowerCAmelCase					:	List[Any]      =       mask_time_prob
   __lowerCAmelCase					:	List[str]      =       mask_time_length
   __lowerCAmelCase					:	Dict      =       mask_time_min_masks
   __lowerCAmelCase					:	Tuple      =       mask_feature_prob
   __lowerCAmelCase					:	List[str]      =       mask_feature_length
   __lowerCAmelCase					:	str      =       mask_feature_min_masks

   # parameters for pretraining with codevector quantized representations
   __lowerCAmelCase					:	Optional[int]      =       num_codevectors_per_group
   __lowerCAmelCase					:	List[Any]      =       num_codevector_groups
   __lowerCAmelCase					:	int      =       contrastive_logits_temperature
   __lowerCAmelCase					:	str      =       feat_quantizer_dropout
   __lowerCAmelCase					:	int      =       num_negatives
   __lowerCAmelCase					:	str      =       codevector_dim
   __lowerCAmelCase					:	Any      =       proj_codevector_dim
   __lowerCAmelCase					:	Any      =       diversity_loss_weight

   # ctc loss
   __lowerCAmelCase					:	Tuple      =       ctc_loss_reduction
   __lowerCAmelCase					:	Any      =       ctc_zero_infinity

   # SequenceClassification-specific parameter. Feel free to ignore for other classes.
   __lowerCAmelCase					:	Any      =       classifier_proj_size

   # XVector-specific parameters. Feel free to ignore for other classes.
   __lowerCAmelCase					:	List[str]      =       list(_snake_case      )
   __lowerCAmelCase					:	List[str]      =       list(_snake_case      )
   __lowerCAmelCase					:	Optional[int]      =       list(_snake_case      )
   __lowerCAmelCase					:	Optional[int]      =       xvector_output_dim






  @property
  def 				UpperCAmelCase__						(							self			:					Optional[Any]      )->Any:






   '''simple docstring'''

   return functools.reduce(operator.mul       ,  self.conv_stride       ,  1      )