






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


__lowerCamelCase   :     Dict 		= logging.get_logger(__name__)



class 				SCREAMING_SNAKE_CASE__		(							UpperCamelCase_		):





					"""simple docstring"""






					a_      =						['input_features', 'attention_mask']



					def __init__(   self	:    Optional[Any]				,						__A	:    int=8_0				,						__A	:    List[Any]=1_6_0_0_0				,						__A	:    Union[str, Any]=8_0				,						__A	:    Optional[Any]=0.0				,						__A	:    str=True				,						__A	:    Union[str, Any]=True				,						__A	:    Tuple=True				,						**__A	:    Optional[int]				,						):
						super().__init__(feature_size=_SCREAMING_SNAKE_CASE				,						sampling_rate=_SCREAMING_SNAKE_CASE				,						padding_value=_SCREAMING_SNAKE_CASE				,						**_SCREAMING_SNAKE_CASE			)
						snake_case__					: Dict  =    num_mel_bins
						snake_case__					: str  =    do_ceptral_normalize
						snake_case__					: int  =    normalize_means
						snake_case__					: Optional[Any]  =    normalize_vars
						snake_case__					: List[Any]  =    True



					def 			_lowercase	(   self	:    List[str]				,						__A	:    int				,						):
						snake_case__					: Optional[Any]  =    waveform * (2**1_5)  # Kaldi compliance: 16-bit signed integers
						snake_case__					: str  =    torch.from_numpy(_SCREAMING_SNAKE_CASE			).unsqueeze(0			)
						snake_case__					: List[Any]  =    ta_kaldi.fbank(_SCREAMING_SNAKE_CASE				,						num_mel_bins=self.num_mel_bins				,						sample_frequency=self.sampling_rate			)
						return features.numpy()



					@staticmethod
					def 			_lowercase	(   __A	:    Tuple				,						__A	:    Any				,						__A	:    Tuple = True				,						__A	:    Tuple = True				,						__A	:    Union[str, Any] = 0.0				,						):





						if normalize_means:
							snake_case__					: str  =    x[:input_length].mean(axis=0			)
							snake_case__					: Optional[Any]  =    np.subtract(_SCREAMING_SNAKE_CASE				,						_SCREAMING_SNAKE_CASE			)
						if normalize_vars:
							snake_case__					: Optional[int]  =    x[:input_length].std(axis=0			)
							snake_case__					: Optional[int]  =    np.divide(_SCREAMING_SNAKE_CASE				,						_SCREAMING_SNAKE_CASE			)

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

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

						return x



					def 			_lowercase	(   self	:    Optional[int]				,						__A	:    List[Any]				,						__A	:    Optional[Any] = None			):
						snake_case__					: Optional[Any]  =    attention_mask.sum(-1			) if attention_mask is not None else [x.shape[0] for x in input_features]
						return [
						    self.utterance_cmvn(_SCREAMING_SNAKE_CASE				,						_SCREAMING_SNAKE_CASE				,						self.normalize_means				,						self.normalize_vars				,						self.padding_value			)
						    for x, n in zip(_SCREAMING_SNAKE_CASE				,						_SCREAMING_SNAKE_CASE			)
						]






					def __call__(   self	:    Dict				,						__A	:    int				,						__A	:    Any = False				,						__A	:    Any = None				,						__A	:    int = False				,						__A	:    Dict = None				,						__A	:    int = None				,						__A	:    Dict = None				,						__A	:    Union[str, Any] = None				,						**__A	:    List[str]				,						):





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

						snake_case__					: Union[str, Any]  =    isinstance(_SCREAMING_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}'''			)
						snake_case__					: Optional[int]  =    is_batched_numpy or (
						    isinstance(_SCREAMING_SNAKE_CASE				,						(list, tuple)			) and (isinstance(raw_speech[0]				,						(np.ndarray, tuple, list)			))
						)

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

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

						# extract fbank features
						snake_case__					: Dict  =    [self._extract_fbank_features(_SCREAMING_SNAKE_CASE			) for waveform in raw_speech]

						# convert into correct format for padding
						snake_case__					: List[str]  =    BatchFeature({"input_features": features}			)

						snake_case__					: Optional[int]  =    self.pad(
						    _SCREAMING_SNAKE_CASE				,						padding=_SCREAMING_SNAKE_CASE				,						max_length=_SCREAMING_SNAKE_CASE				,						truncation=_SCREAMING_SNAKE_CASE				,						pad_to_multiple_of=_SCREAMING_SNAKE_CASE				,						return_attention_mask=_SCREAMING_SNAKE_CASE				,						**_SCREAMING_SNAKE_CASE				,						)

						# make sure list is in array format
						snake_case__					: Any  =    padded_inputs.get("input_features"			)
						if isinstance(input_features[0]				,						_SCREAMING_SNAKE_CASE			):
							snake_case__					: Union[str, Any]  =    [np.asarray(_SCREAMING_SNAKE_CASE				,						dtype=np.floataa			) for feature in input_features]

						snake_case__					: List[str]  =    padded_inputs.get("attention_mask"			)
						if attention_mask is not None:
							snake_case__					: List[str]  =    [np.asarray(_SCREAMING_SNAKE_CASE				,						dtype=np.intaa			) for array in attention_mask]

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

						if return_tensors is not None:
							snake_case__					: int  =    padded_inputs.convert_to_tensors(_SCREAMING_SNAKE_CASE			)

						return padded_inputs







from collections import OrderedDict
from typing import Mapping

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


lowerCAmelCase__    :Any       =				logging.get_logger(__name__)

lowerCAmelCase__    :Union[str, Any]       =				{
    '''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 						__a (							UpperCAmelCase     ):
		_a      :    Any           =      'xmod'
		def __init__(     self     ,       _SCREAMING_SNAKE_CASE=30522     ,       _SCREAMING_SNAKE_CASE=768     ,       _SCREAMING_SNAKE_CASE=12     ,       _SCREAMING_SNAKE_CASE=12     ,       _SCREAMING_SNAKE_CASE=3072     ,       _SCREAMING_SNAKE_CASE="gelu"     ,       _SCREAMING_SNAKE_CASE=0.1     ,       _SCREAMING_SNAKE_CASE=0.1     ,       _SCREAMING_SNAKE_CASE=512     ,       _SCREAMING_SNAKE_CASE=2     ,       _SCREAMING_SNAKE_CASE=0.02     ,       _SCREAMING_SNAKE_CASE=1e-1_2     ,       _SCREAMING_SNAKE_CASE=1     ,       _SCREAMING_SNAKE_CASE=0     ,       _SCREAMING_SNAKE_CASE=2     ,       _SCREAMING_SNAKE_CASE="absolute"     ,       _SCREAMING_SNAKE_CASE=True     ,       _SCREAMING_SNAKE_CASE=None     ,       _SCREAMING_SNAKE_CASE=False     ,       _SCREAMING_SNAKE_CASE=2     ,       _SCREAMING_SNAKE_CASE=False     ,       _SCREAMING_SNAKE_CASE=True     ,       _SCREAMING_SNAKE_CASE=True     ,       _SCREAMING_SNAKE_CASE=("en_XX",)     ,       _SCREAMING_SNAKE_CASE=None     ,       **_SCREAMING_SNAKE_CASE     ,       )					->			Dict:




									"""simple docstring"""





									super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE     ,       bos_token_id=_SCREAMING_SNAKE_CASE     ,       eos_token_id=_SCREAMING_SNAKE_CASE     ,       **_SCREAMING_SNAKE_CASE		)

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



class 						__a (							UpperCAmelCase     ):
		@property
		def       UpperCAmelCase__							(     self		)					->			Mapping[str, Mapping[int, str]]:




									"""simple docstring"""





									if self.task == "multiple-choice":
																_UpperCAmelCase											=     {0: 'batch', 1: 'choice', 2: 'sequence'}
									else:
																_UpperCAmelCase											=     {0: 'batch', 1: 'sequence'}
									return OrderedDict(
									    [
									        ('input_ids', dynamic_axis),
									        ('attention_mask', dynamic_axis),
									    ]		)
