





"""simple docstring"""
from typing import List, Optional, Union

import numpy as np
import torch
import torchaudio.compliance.kaldi as ta_kaldi

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


__UpperCamelCase     :						Any						       =  logging.get_logger(__name__)







class       a							(   a__  ):
 snake_case__             =       ['''input_features''', '''attention_mask''']



 def __init__(     self				,       _snake_case=80				,       _snake_case=1_60_00				,       _snake_case=80				,       _snake_case=0.0				,       _snake_case=True				,       _snake_case=True				,       _snake_case=True				,       **_snake_case				,       ):







  """simple docstring"""






  super().__init__(feature_size=_snake_case				,       sampling_rate=_snake_case				,       padding_value=_snake_case				,       **_snake_case     )
  lowerCAmelCase    				= num_mel_bins
  lowerCAmelCase    				= do_ceptral_normalize
  lowerCAmelCase    				= normalize_means
  lowerCAmelCase    				= normalize_vars
  lowerCAmelCase    				= True



 def    UpperCamelCase__  (     self				,       _snake_case				,       ):







  """simple docstring"""






  lowerCAmelCase    				= waveform * (2**15)  # Kaldi compliance: 16-bit signed integers
  lowerCAmelCase    				= torch.from_numpy(_snake_case     ).unsqueeze(0     )
  lowerCAmelCase    				= ta_kaldi.fbank(_snake_case				,       num_mel_bins=self.num_mel_bins				,       sample_frequency=self.sampling_rate     )
  return features.numpy()



 @staticmethod
 def    UpperCamelCase__  (     _snake_case				,       _snake_case				,       _snake_case = True				,       _snake_case = True				,       _snake_case = 0.0				,       ):







  """simple docstring"""






  if normalize_means:
   lowerCAmelCase    				= x[:input_length].mean(axis=0     )
   lowerCAmelCase    				= np.subtract(_snake_case				,       _snake_case     )
  if normalize_vars:
   lowerCAmelCase    				= x[:input_length].std(axis=0     )
   lowerCAmelCase    				= np.divide(_snake_case				,       _snake_case     )

  if input_length < x.shape[0]:
   lowerCAmelCase    				= padding_value

  # make sure array is in float32
  lowerCAmelCase    				= x.astype(np.floataa     )

  return x



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case = None     ):







  """simple docstring"""






  lowerCAmelCase    				= attention_mask.sum(-1     ) if attention_mask is not None else [x.shape[0] for x in input_features]
  return [
      self.utterance_cmvn(_snake_case				,       _snake_case				,       self.normalize_means				,       self.normalize_vars				,       self.padding_value     )
      for x, n in zip(_snake_case				,       _snake_case     )
  ]



 def __call__(     self				,       _snake_case				,       _snake_case = False				,       _snake_case = None				,       _snake_case = False				,       _snake_case = None				,       _snake_case = None				,       _snake_case = None				,       _snake_case = None				,       **_snake_case				,       ):







  """simple docstring"""






  if sampling_rate is not None:
   if sampling_rate != self.sampling_rate:
    raise ValueError(
        F'The model corresponding to this feature extractor: {self} was trained using a sampling rate of'
        F' {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with'
        F' {self.sampling_rate} and not {sampling_rate}.'     )
  else:
   logger.warning(
       'It is strongly recommended to pass the `sampling_rate` argument to this function. '
       'Failing to do so can result in silent errors that might be hard to debug.'     )

  lowerCAmelCase    				= isinstance(_snake_case				,       np.ndarray     ) and len(raw_speech.shape     ) > 1
  if is_batched_numpy and len(raw_speech.shape     ) > 2:
   raise ValueError(F'Only mono-channel audio is supported for input to {self}'     )
  lowerCAmelCase    				= is_batched_numpy or (
      isinstance(_snake_case				,       (list, tuple)     ) and (isinstance(raw_speech[0]				,       (np.ndarray, tuple, list)     ))
  )

  if is_batched:
   lowerCAmelCase    				= [np.asarray(_snake_case				,       dtype=np.floataa     ) for speech in raw_speech]
  elif not is_batched and not isinstance(_snake_case				,       np.ndarray     ):
   lowerCAmelCase    				= np.asarray(_snake_case				,       dtype=np.floataa     )
  elif isinstance(_snake_case				,       np.ndarray     ) and raw_speech.dtype is np.dtype(np.floataa     ):
   lowerCAmelCase    				= raw_speech.astype(np.floataa     )

  # always return batch
  if not is_batched:
   lowerCAmelCase    				= [raw_speech]

  # extract fbank features
  lowerCAmelCase    				= [self._extract_fbank_features(_snake_case     ) for waveform in raw_speech]

  # convert into correct format for padding
  lowerCAmelCase    				= BatchFeature({'input_features': features}     )

  lowerCAmelCase    				= self.pad(
      _snake_case				,       padding=_snake_case				,       max_length=_snake_case				,       truncation=_snake_case				,       pad_to_multiple_of=_snake_case				,       return_attention_mask=_snake_case				,       **_snake_case				,       )

  # make sure list is in array format
  lowerCAmelCase    				= padded_inputs.get('input_features'     )
  if isinstance(input_features[0]				,       _snake_case     ):
   lowerCAmelCase    				= [np.asarray(_snake_case				,       dtype=np.floataa     ) for feature in input_features]

  lowerCAmelCase    				= padded_inputs.get('attention_mask'     )
  if attention_mask is not None:
   lowerCAmelCase    				= [np.asarray(_snake_case				,       dtype=np.intaa     ) for array in attention_mask]

  # Utterance-level cepstral mean and variance normalization
  if self.do_ceptral_normalize:
   lowerCAmelCase    				= (
       np.array(_snake_case				,       dtype=np.intaa     )
       if self._get_padding_strategies(_snake_case				,       max_length=_snake_case     ) is not PaddingStrategy.DO_NOT_PAD
       else None
   )
   lowerCAmelCase    				= self.normalize(
       padded_inputs['input_features']				,       attention_mask=_snake_case     )

  if return_tensors is not None:
   lowerCAmelCase    				= padded_inputs.convert_to_tensors(_snake_case     )

  return padded_inputs












'''simple docstring'''

from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__lowerCAmelCase				:      Optional[Any]						      =logging.get_logger(__name__)

__lowerCAmelCase				:      str						      ={
    "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/config.json",
    "distilbert-base-uncased-distilled-squad": (
        "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json"
    ),
    "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/config.json",
    "distilbert-base-cased-distilled-squad": (
        "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json"
    ),
    "distilbert-base-german-cased": "https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json",
    "distilbert-base-multilingual-cased": (
        "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json"
    ),
    "distilbert-base-uncased-finetuned-sst-2-english": (
        "https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json"
    ),
}






class   UpperCAmelCase      (			UpperCamelCase__							):
 __lowercase  						=							"""distilbert"""
 __lowercase  						=							{
     """hidden_size""": """dim""",
     """num_attention_heads""": """n_heads""",
     """num_hidden_layers""": """n_layers""",
 }
 def __init__(							self :Tuple		,			lowercase_ :List[str]=3_05_22		,			lowercase_ :Tuple=5_12		,			lowercase_ :List[str]=False		,			lowercase_ :int=6		,			lowercase_ :Optional[int]=12		,			lowercase_ :Optional[int]=7_68		,			lowercase_ :str=4 * 7_68		,			lowercase_ :List[Any]=0.1		,			lowercase_ :str=0.1		,			lowercase_ :Optional[int]="gelu"		,			lowercase_ :Optional[Any]=0.0_2		,			lowercase_ :Optional[Any]=0.1		,			lowercase_ :List[Any]=0.2		,			lowercase_ :str=0		,			**lowercase_ :Union[str, Any]		,			)->				str:
     A__							=     vocab_size
     A__							=     max_position_embeddings
     A__							=     sinusoidal_pos_embds
     A__							=     n_layers
     A__							=     n_heads
     A__							=     dim
     A__							=     hidden_dim
     A__							=     dropout
     A__							=     attention_dropout
     A__							=     activation
     A__							=     initializer_range
     A__							=     qa_dropout
     A__							=     seq_classif_dropout
     super().__init__(**lowercase_		,			pad_token_id=lowercase_				)






class   UpperCAmelCase      (			UpperCamelCase__							):
 @property
 def 	UpperCAmelCase_				(							self :List[str]				)->				Mapping[str, Mapping[int, str]]:
     if self.task == "multiple-choice":
         A__							=     {0: "batch", 1: "choice", 2: "sequence"}
     else:
         A__							=     {0: "batch", 1: "sequence"}
     return OrderedDict(
         [
             ("input_ids", dynamic_axis),
             ("attention_mask", dynamic_axis),
         ]				)



