





"""simple docstring"""


from typing import List, Optional, Union

import numpy as np

from ....audio_utils import mel_filter_bank, optimal_fft_length, spectrogram, window_function
from ....feature_extraction_sequence_utils import SequenceFeatureExtractor
from ....feature_extraction_utils import BatchFeature
from ....file_utils import PaddingStrategy, TensorType
from ....utils import logging


__magic_name__       :					Tuple						=			logging.get_logger(__name__)






class 							lowercase__			(   __lowerCAmelCase	):

			"""simple docstring"""
			__lowerCAmelCase     :				Tuple										=  ['''input_features''', '''attention_mask''']







			def __init__(      self						,   _A=8_0						,   _A=1_6_0_0_0						,   _A=0.0						,   _A=1_0						,   _A=2_5						,   _A="hamming_window"						,   _A=3_2_7_6_8.0						,   _A=0.97						,   _A=1.0						,   _A=True						,   _A=True						,   _A=False						,   **_A						,   ):






									'''simple docstring'''







									super().__init__(feature_size=_UpperCAmelCase						,   sampling_rate=_UpperCAmelCase						,   padding_value=_UpperCAmelCase						,   **_UpperCAmelCase    )

									UpperCamelCase				:		List[str]				   =			feature_size
									UpperCamelCase				:		str				   =			sampling_rate
									UpperCamelCase				:		List[str]				   =			padding_value
									UpperCamelCase				:		Dict				   =			hop_length
									UpperCamelCase				:		Union[str, Any]				   =			win_length
									UpperCamelCase				:		Union[str, Any]				   =			frame_signal_scale
									UpperCamelCase				:		Optional[Any]				   =			preemphasis_coeff
									UpperCamelCase				:		Optional[int]				   =			mel_floor
									UpperCamelCase				:		Union[str, Any]				   =			normalize_means
									UpperCamelCase				:		Optional[int]				   =			normalize_vars
									UpperCamelCase				:		str				   =			win_function
									UpperCamelCase				:		List[str]				   =			return_attention_mask

									UpperCamelCase				:		Union[str, Any]				   =			win_length * sampling_rate // 1_0_0_0
									UpperCamelCase				:		Optional[int]				   =			hop_length * sampling_rate // 1_0_0_0

									UpperCamelCase				:		Dict				   =			optimal_fft_length(self.sample_size    )
									UpperCamelCase				:		int				   =			(self.n_fft // 2) + 1







			def   _a      (      self						,   _A    ):






									'''simple docstring'''







									if self.win_function == "hamming_window":
															UpperCamelCase				:		Optional[int]				   =			window_function(window_length=self.sample_size						,   name=self.win_function						,   periodic=_UpperCAmelCase    )
									else:
															UpperCamelCase				:		Optional[Any]				   =			window_function(window_length=self.sample_size						,   name=self.win_function    )

									UpperCamelCase				:		Optional[Any]				   =			mel_filter_bank(
									    num_frequency_bins=self.n_freqs						,   num_mel_filters=self.feature_size						,   min_frequency=0.0						,   max_frequency=self.sampling_rate / 2.0						,   sampling_rate=self.sampling_rate						,   )

									UpperCamelCase				:		List[str]				   =			spectrogram(
									    one_waveform * self.frame_signal_scale						,   window=_UpperCAmelCase						,   frame_length=self.sample_size						,   hop_length=self.sample_stride						,   fft_length=self.n_fft						,   center=_UpperCAmelCase						,   preemphasis=self.preemphasis_coeff						,   mel_filters=_UpperCAmelCase						,   mel_floor=self.mel_floor						,   log_mel="""log"""						,   )
									return msfc_features.T







			def   _a      (      self						,   _A						,   _A						,   _A    ):






									'''simple docstring'''







									if self.normalize_means:
															UpperCamelCase				:		Optional[int]				   =			x[:input_length].mean(axis=0    )
															UpperCamelCase				:		Dict				   =			np.subtract(_UpperCAmelCase						,   _UpperCAmelCase    )
									if self.normalize_vars:
															UpperCamelCase				:		List[Any]				   =			x[:input_length].std(axis=0    )
															UpperCamelCase				:		Dict				   =			np.divide(_UpperCAmelCase						,   _UpperCAmelCase    )

									if input_length < x.shape[0]:
															UpperCamelCase				:		Union[str, Any]				   =			padding_value

									# make sure array is in float32
									UpperCamelCase				:		Any				   =			x.astype(np.floataa    )

									return x







			def   _a      (      self						,   _A						,   _A = None    ):






									'''simple docstring'''







									UpperCamelCase				:		Optional[Any]				   =			attention_mask.sum(-1    ) if attention_mask is not None else [x.shape[0] for x in input_features]
									return [self._normalize_one(_UpperCAmelCase						,   _UpperCAmelCase						,   self.padding_value    ) for x, n in zip(_UpperCAmelCase						,   _UpperCAmelCase    )]







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






									'''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."""    )

									UpperCamelCase				:		Tuple				   =			isinstance(_UpperCAmelCase						,   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}"""    )
									UpperCamelCase				:		List[str]				   =			is_batched_numpy or (
									    isinstance(_UpperCAmelCase						,   (list, tuple)    ) and (isinstance(raw_speech[0]						,   (np.ndarray, tuple, list)    ))
									)

									if is_batched:
															UpperCamelCase				:		Optional[int]				   =			[np.asarray(_UpperCAmelCase						,   dtype=np.floataa    ) for speech in raw_speech]
									elif not is_batched and not isinstance(_UpperCAmelCase						,   np.ndarray    ):
															UpperCamelCase				:		List[str]				   =			np.asarray(_UpperCAmelCase						,   dtype=np.floataa    )
									elif isinstance(_UpperCAmelCase						,   np.ndarray    ) and raw_speech.dtype is np.dtype(np.floataa    ):
															UpperCamelCase				:		Union[str, Any]				   =			raw_speech.astype(np.floataa    )

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

									# extract fbank features
									UpperCamelCase				:		Tuple				   =			[self._extract_mfsc_features(_UpperCAmelCase    ) for one_waveform in raw_speech]

									# convert into correct format for padding
									UpperCamelCase				:		Tuple				   =			BatchFeature({"""input_features""": features}    )

									UpperCamelCase				:		Optional[Any]				   =			self.pad(
									    _UpperCAmelCase						,   padding=_UpperCAmelCase						,   max_length=_UpperCAmelCase						,   truncation=_UpperCAmelCase						,   pad_to_multiple_of=_UpperCAmelCase						,   return_attention_mask=_UpperCAmelCase						,   **_UpperCAmelCase						,   )
									# make sure list is in array format
									UpperCamelCase				:		List[str]				   =			padded_inputs.get("""input_features"""    )
									if isinstance(input_features[0]						,   _UpperCAmelCase    ):
															UpperCamelCase				:		Any				   =			[np.asarray(_UpperCAmelCase						,   dtype=np.floataa    ) for feature in input_features]

									UpperCamelCase				:		Dict				   =			padded_inputs.get("""attention_mask"""    )
									if attention_mask is not None:
															UpperCamelCase				:		Dict				   =			[np.asarray(_UpperCAmelCase						,   dtype=np.intaa    ) for array in attention_mask]

									if self.normalize_means or self.normalize_vars:
															UpperCamelCase				:		List[Any]				   =			(
															    np.array(_UpperCAmelCase						,   dtype=np.intaa    )
															    if self._get_padding_strategies(_UpperCAmelCase						,   max_length=_UpperCAmelCase    ) is not PaddingStrategy.DO_NOT_PAD
															    and padding
															    else None
															)
															UpperCamelCase				:		Dict				   =			self.normalize(
															    padded_inputs["""input_features"""]						,   attention_mask=_UpperCAmelCase    )

									if return_tensors is not None:
															UpperCamelCase				:		Any				   =			padded_inputs.convert_to_tensors(_UpperCAmelCase    )

									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


a							      =     logging.get_logger(__name__)

a							      =     {
    '''facebook/xmod-base''': '''https://huggingface.co/facebook/xmod-base/resolve/main/config.json''',
    '''facebook/xmod-large-prenorm''': '''https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json''',
    '''facebook/xmod-base-13-125k''': '''https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json''',
    '''facebook/xmod-base-30-125k''': '''https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json''',
    '''facebook/xmod-base-30-195k''': '''https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json''',
    '''facebook/xmod-base-60-125k''': '''https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json''',
    '''facebook/xmod-base-60-265k''': '''https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json''',
    '''facebook/xmod-base-75-125k''': '''https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json''',
    '''facebook/xmod-base-75-269k''': '''https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json''',
}





class 							lowercase_			(	__lowerCAmelCase				):



 '''simple docstring'''
 UpperCAmelCase       :	Optional[Any]     =      '''xmod'''

 def __init__(					self			:			str       ,				_UpperCAmelCase			:			Optional[Any]=30_522       ,				_UpperCAmelCase			:			Any=768       ,				_UpperCAmelCase			:			int=12       ,				_UpperCAmelCase			:			List[str]=12       ,				_UpperCAmelCase			:			Dict=3_072       ,				_UpperCAmelCase			:			Union[str, Any]="gelu"       ,				_UpperCAmelCase			:			Optional[int]=0.1       ,				_UpperCAmelCase			:			Tuple=0.1       ,				_UpperCAmelCase			:			List[str]=512       ,				_UpperCAmelCase			:			Dict=2       ,				_UpperCAmelCase			:			Optional[int]=0.02       ,				_UpperCAmelCase			:			Any=1E-1_2       ,				_UpperCAmelCase			:			Tuple=1       ,				_UpperCAmelCase			:			int=0       ,				_UpperCAmelCase			:			List[Any]=2       ,				_UpperCAmelCase			:			List[str]="absolute"       ,				_UpperCAmelCase			:			Tuple=True       ,				_UpperCAmelCase			:			Tuple=None       ,				_UpperCAmelCase			:			int=False       ,				_UpperCAmelCase			:			Union[str, Any]=2       ,				_UpperCAmelCase			:			List[Any]=False       ,				_UpperCAmelCase			:			Dict=True       ,				_UpperCAmelCase			:			List[str]=True       ,				_UpperCAmelCase			:			Tuple=("en_XX",)       ,				_UpperCAmelCase			:			List[str]=None       ,				**_UpperCAmelCase			:			Optional[Any]       ,				):
       super().__init__(pad_token_id=_UpperCAmelCase       ,				bos_token_id=_UpperCAmelCase       ,				eos_token_id=_UpperCAmelCase       ,				**_UpperCAmelCase						)

       _A     =  vocab_size
       _A     =  hidden_size
       _A     =  num_hidden_layers
       _A     =  num_attention_heads
       _A     =  hidden_act
       _A     =  intermediate_size
       _A     =  hidden_dropout_prob
       _A     =  attention_probs_dropout_prob
       _A     =  max_position_embeddings
       _A     =  type_vocab_size
       _A     =  initializer_range
       _A     =  layer_norm_eps
       _A     =  position_embedding_type
       _A     =  use_cache
       _A     =  classifier_dropout
       _A     =  pre_norm
       _A     =  adapter_reduction_factor
       _A     =  adapter_layer_norm
       _A     =  adapter_reuse_layer_norm
       _A     =  ln_before_adapter
       _A     =  list(_UpperCAmelCase						)
       _A     =  default_language





class 							lowercase_			(	__lowerCAmelCase				):



 '''simple docstring'''

 @property
 def      lowerCAmelCase_				(					self			:			Dict						):
       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),
           ]						)

