



'''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


snake_case_			=     logging.get_logger(__name__)


class 	a__     (				_lowercase	):
	__magic_name__		:    str	    =     ["input_features", "attention_mask"]


	def __init__(self : List[Any],					__UpperCAmelCase : List[str]=80,					__UpperCAmelCase : List[Any]=16000,					__UpperCAmelCase : List[str]=80,					__UpperCAmelCase : List[Any]=0.0,					__UpperCAmelCase : Optional[Any]=True,					__UpperCAmelCase : int=True,					__UpperCAmelCase : Tuple=True,					**__UpperCAmelCase : Any,					)     ->			List[str]:

					"""simple docstring"""

					super().__init__(feature_size=__UpperCAmelCase,					sampling_rate=__UpperCAmelCase,					padding_value=__UpperCAmelCase,					**__UpperCAmelCase      )
					SCREAMING_SNAKE_CASE  :						List[str]      					= num_mel_bins
					SCREAMING_SNAKE_CASE  :						Tuple      					= do_ceptral_normalize
					SCREAMING_SNAKE_CASE  :						int      					= normalize_means
					SCREAMING_SNAKE_CASE  :						List[str]      					= normalize_vars
					SCREAMING_SNAKE_CASE  :						List[Any]      					= True


	def 				lowercase__  (self : Dict,					__UpperCAmelCase : np.ndarray,					)     ->			np.ndarray:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Optional[int]      					= waveform * (2**15)  # Kaldi compliance: 16-bit signed integers
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= torch.from_numpy(__UpperCAmelCase      ).unsqueeze(0      )
					SCREAMING_SNAKE_CASE  :						str      					= ta_kaldi.fbank(__UpperCAmelCase,					num_mel_bins=self.num_mel_bins,					sample_frequency=self.sampling_rate      )
					return features.numpy()


	@staticmethod
	def 				lowercase__  (__UpperCAmelCase : np.ndarray,					__UpperCAmelCase : int,					__UpperCAmelCase : Optional[bool] = True,					__UpperCAmelCase : Optional[bool] = True,					__UpperCAmelCase : float = 0.0,					)     ->			np.ndarray:

					"""simple docstring"""

					if normalize_means:
									SCREAMING_SNAKE_CASE  :						Dict      					= x[:input_length].mean(axis=0      )
									SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= np.subtract(__UpperCAmelCase,					__UpperCAmelCase      )
					if normalize_vars:
									SCREAMING_SNAKE_CASE  :						Dict      					= x[:input_length].std(axis=0      )
									SCREAMING_SNAKE_CASE  :						Any      					= np.divide(__UpperCAmelCase,					__UpperCAmelCase      )

					if input_length < x.shape[0]:
									SCREAMING_SNAKE_CASE  :						Optional[int]      					= padding_value

					# make sure array is in float32
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= x.astype(np.floataa      )

					return x


	def 				lowercase__  (self : Optional[int],					__UpperCAmelCase : List[np.ndarray],					__UpperCAmelCase : Optional[np.ndarray] = None      )     ->			List[np.ndarray]:

					"""simple docstring"""

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






	def __call__(self : List[str],					__UpperCAmelCase : Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]],					__UpperCAmelCase : Union[bool, str, PaddingStrategy] = False,					__UpperCAmelCase : Optional[int] = None,					__UpperCAmelCase : bool = False,					__UpperCAmelCase : Optional[int] = None,					__UpperCAmelCase : Optional[Union[str, TensorType]] = None,					__UpperCAmelCase : Optional[int] = None,					__UpperCAmelCase : Optional[bool] = None,					**__UpperCAmelCase : Union[str, Any],					)     ->			BatchFeature:

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

					SCREAMING_SNAKE_CASE  :						int      					= 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}'''      )
					SCREAMING_SNAKE_CASE  :						List[Any]      					= is_batched_numpy or (
					    isinstance(__UpperCAmelCase,					(list, tuple)      ) and (isinstance(raw_speech[0],					(np.ndarray, tuple, list)      ))
					)

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

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

					# extract fbank features
					SCREAMING_SNAKE_CASE  :						str      					= [self._extract_fbank_features(__UpperCAmelCase      ) for waveform in raw_speech]

					# convert into correct format for padding
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= BatchFeature({'''input_features''': features}      )

					SCREAMING_SNAKE_CASE  :						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
					SCREAMING_SNAKE_CASE  :						Tuple      					= padded_inputs.get('''input_features'''      )
					if isinstance(input_features[0],					__UpperCAmelCase      ):
									SCREAMING_SNAKE_CASE  :						Tuple      					= [np.asarray(__UpperCAmelCase,					dtype=np.floataa      ) for feature in input_features]

					SCREAMING_SNAKE_CASE  :						Any      					= padded_inputs.get('''attention_mask'''      )
					if attention_mask is not None:
									SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= [np.asarray(__UpperCAmelCase,					dtype=np.intaa      ) for array in attention_mask]

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

					if return_tensors is not None:
									SCREAMING_SNAKE_CASE  :						Tuple      					= padded_inputs.convert_to_tensors(__UpperCAmelCase      )

					return padded_inputs









'''simple docstring'''

import functools
import operator

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


snake_case_			=     logging.get_logger(__name__)

snake_case_			=     {
    """facebook/wav2vec2-base-960h""": """https://huggingface.co/facebook/wav2vec2-base-960h/resolve/main/config.json""",
    # See all Wav2Vec2 models at https://huggingface.co/models?filter=wav2vec2
}


class 	a__     (				_lowercase	):
	__magic_name__		:    Dict	    =     "wav2vec2"


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

					"""simple docstring"""

					super().__init__(**__UpperCAmelCase,					pad_token_id=__UpperCAmelCase,					bos_token_id=__UpperCAmelCase,					eos_token_id=__UpperCAmelCase      )
					SCREAMING_SNAKE_CASE  :						Any      					= hidden_size
					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= feat_extract_norm
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= feat_extract_activation
					SCREAMING_SNAKE_CASE  :						Dict      					= list(__UpperCAmelCase      )
					SCREAMING_SNAKE_CASE  :						List[str]      					= list(__UpperCAmelCase      )
					SCREAMING_SNAKE_CASE  :						Tuple      					= list(__UpperCAmelCase      )
					SCREAMING_SNAKE_CASE  :						Dict      					= conv_bias
					SCREAMING_SNAKE_CASE  :						Dict      					= num_conv_pos_embeddings
					SCREAMING_SNAKE_CASE  :						List[Any]      					= num_conv_pos_embedding_groups
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= len(self.conv_dim      )
					SCREAMING_SNAKE_CASE  :						Tuple      					= num_hidden_layers
					SCREAMING_SNAKE_CASE  :						str      					= intermediate_size
					SCREAMING_SNAKE_CASE  :						Any      					= hidden_act
					SCREAMING_SNAKE_CASE  :						str      					= num_attention_heads
					SCREAMING_SNAKE_CASE  :						int      					= hidden_dropout
					SCREAMING_SNAKE_CASE  :						Any      					= attention_dropout
					SCREAMING_SNAKE_CASE  :						int      					= activation_dropout
					SCREAMING_SNAKE_CASE  :						int      					= feat_proj_dropout
					SCREAMING_SNAKE_CASE  :						List[str]      					= final_dropout
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= layerdrop
					SCREAMING_SNAKE_CASE  :						Tuple      					= layer_norm_eps
					SCREAMING_SNAKE_CASE  :						Dict      					= initializer_range
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= vocab_size
					SCREAMING_SNAKE_CASE  :						Tuple      					= do_stable_layer_norm
					SCREAMING_SNAKE_CASE  :						Dict      					= 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
					SCREAMING_SNAKE_CASE  :						str      					= apply_spec_augment
					SCREAMING_SNAKE_CASE  :						Dict      					= mask_time_prob
					SCREAMING_SNAKE_CASE  :						Optional[int]      					= mask_time_length
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= mask_time_min_masks
					SCREAMING_SNAKE_CASE  :						Dict      					= mask_feature_prob
					SCREAMING_SNAKE_CASE  :						Tuple      					= mask_feature_length
					SCREAMING_SNAKE_CASE  :						Tuple      					= mask_feature_min_masks

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

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

					# adapter
					SCREAMING_SNAKE_CASE  :						int      					= add_adapter
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= adapter_kernel_size
					SCREAMING_SNAKE_CASE  :						int      					= adapter_stride
					SCREAMING_SNAKE_CASE  :						List[str]      					= num_adapter_layers
					SCREAMING_SNAKE_CASE  :						Tuple      					= output_hidden_size or hidden_size
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= adapter_attn_dim

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

					# XVector-specific parameters. Feel free to ignore for other classes.
					SCREAMING_SNAKE_CASE  :						int      					= list(__UpperCAmelCase      )
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= list(__UpperCAmelCase      )
					SCREAMING_SNAKE_CASE  :						int      					= list(__UpperCAmelCase      )
					SCREAMING_SNAKE_CASE  :						Dict      					= xvector_output_dim






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

					"""simple docstring"""

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




