"""simple docstring"""







import math

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


SCREAMING_SNAKE_CASE									=						logging.get_logger(__name__)

SCREAMING_SNAKE_CASE									=						{
    'facebook/data2vec-base-960h': 'https://huggingface.co/facebook/data2vec-audio-base-960h/resolve/main/config.json',
    # See all Data2VecAudio models at https://huggingface.co/models?filter=data2vec-audio
}



class  _lowerCamelCase			(__lowerCamelCase     ):
				_snake_case     =  "data2vec-audio"







				def __init__( self    :       List[str]			,					lowerCamelCase_    :       Dict=3_2			,					lowerCamelCase_    :       Union[str, Any]=7_6_8			,					lowerCamelCase_    :       Tuple=1_2			,					lowerCamelCase_    :       List[Any]=1_2			,					lowerCamelCase_    :       List[Any]=3_0_7_2			,					lowerCamelCase_    :       Optional[int]="gelu"			,					lowerCamelCase_    :       Optional[Any]=0.1			,					lowerCamelCase_    :       str=0.1			,					lowerCamelCase_    :       Any=0.1			,					lowerCamelCase_    :       Optional[int]=0.0			,					lowerCamelCase_    :       Union[str, Any]=0.1			,					lowerCamelCase_    :       Optional[Any]=0.1			,					lowerCamelCase_    :       Optional[Any]=0.02			,					lowerCamelCase_    :       List[Any]=1E-5			,					lowerCamelCase_    :       List[str]="gelu"			,					lowerCamelCase_    :       int=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2)			,					lowerCamelCase_    :       List[Any]=(5, 2, 2, 2, 2, 2, 2)			,					lowerCamelCase_    :       Tuple=(1_0, 3, 3, 3, 3, 2, 2)			,					lowerCamelCase_    :       Any=False			,					lowerCamelCase_    :       List[Any]=1_6			,					lowerCamelCase_    :       List[str]=1_9			,					lowerCamelCase_    :       List[Any]=5			,					lowerCamelCase_    :       Tuple=0.05			,					lowerCamelCase_    :       Any=1_0			,					lowerCamelCase_    :       Any=2			,					lowerCamelCase_    :       Optional[int]=0.0			,					lowerCamelCase_    :       int=1_0			,					lowerCamelCase_    :       List[Any]=0			,					lowerCamelCase_    :       List[str]="sum"			,					lowerCamelCase_    :       str=False			,					lowerCamelCase_    :       List[Any]=False			,					lowerCamelCase_    :       List[str]=2_5_6			,					lowerCamelCase_    :       Dict=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0)			,					lowerCamelCase_    :       str=(5, 3, 3, 1, 1)			,					lowerCamelCase_    :       Union[str, Any]=(1, 2, 3, 1, 1)			,					lowerCamelCase_    :       int=5_1_2			,					lowerCamelCase_    :       List[Any]=0			,					lowerCamelCase_    :       Tuple=1			,					lowerCamelCase_    :       Union[str, Any]=2			,					lowerCamelCase_    :       Optional[Any]=False			,					lowerCamelCase_    :       List[str]=3			,					lowerCamelCase_    :       Any=2			,					lowerCamelCase_    :       List[Any]=3			,					lowerCamelCase_    :       Tuple=None			,					**lowerCamelCase_    :       Tuple			,					):

					"""simple docstring"""






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

					# ctc loss
					_lowercase :    Tuple							=							ctc_loss_reduction
					_lowercase :    List[Any]							=							ctc_zero_infinity

					# adapter
					_lowercase :    Dict							=							add_adapter
					_lowercase :    Optional[Any]							=							adapter_kernel_size
					_lowercase :    List[Any]							=							adapter_stride
					_lowercase :    Dict							=							num_adapter_layers
					_lowercase :    Tuple							=							output_hidden_size or hidden_size

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

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







				@property
				def 							__UpperCAmelCase					( self    :       Any						):

					"""simple docstring"""






					return math.prod(self.conv_stride						)




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


SCREAMING_SNAKE_CASE									=						logging.get_logger(__name__)



class  _lowerCamelCase			(__lowerCamelCase     ):
				_snake_case     =  ["input_features", "attention_mask"]







				def __init__( self    :       int			,					lowerCamelCase_    :       List[str]=8_0			,					lowerCamelCase_    :       Tuple=1_6_0_0_0			,					lowerCamelCase_    :       Union[str, Any]=0.0			,					lowerCamelCase_    :       List[Any]=1_0			,					lowerCamelCase_    :       List[str]=2_5			,					lowerCamelCase_    :       List[Any]="hamming_window"			,					lowerCamelCase_    :       Tuple=3_2768.0			,					lowerCamelCase_    :       int=0.97			,					lowerCamelCase_    :       Optional[int]=1.0			,					lowerCamelCase_    :       Optional[Any]=True			,					lowerCamelCase_    :       Union[str, Any]=True			,					lowerCamelCase_    :       Optional[int]=False			,					**lowerCamelCase_    :       Optional[Any]			,					):

					"""simple docstring"""






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

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

					_lowercase :    Optional[Any]							=							win_length * sampling_rate // 1_0_0_0
					_lowercase :    Tuple							=							hop_length * sampling_rate // 1_0_0_0

					_lowercase :    str							=							optimal_fft_length(self.sample_size						)
					_lowercase :    Dict							=							(self.n_fft // 2) + 1







				def 							__UpperCAmelCase					( self    :       List[str]			,					lowerCamelCase_    :       np.array						):

					"""simple docstring"""






					if self.win_function == "hamming_window":
						_lowercase :    List[Any]							=							window_function(window_length=self.sample_size			,					name=self.win_function			,					periodic=lowerCamelCase_						)
					else:
						_lowercase :    Union[str, Any]							=							window_function(window_length=self.sample_size			,					name=self.win_function						)

					_lowercase :    Tuple							=							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			,					)

					_lowercase :    Tuple							=							spectrogram(
					    one_waveform * self.frame_signal_scale			,					window=lowerCamelCase_			,					frame_length=self.sample_size			,					hop_length=self.sample_stride			,					fft_length=self.n_fft			,					center=lowerCamelCase_			,					preemphasis=self.preemphasis_coeff			,					mel_filters=lowerCamelCase_			,					mel_floor=self.mel_floor			,					log_mel='log'			,					)
					return msfc_features.T







				def 							__UpperCAmelCase					( self    :       Optional[Any]			,					lowerCamelCase_    :       Dict			,					lowerCamelCase_    :       Dict			,					lowerCamelCase_    :       Tuple						):

					"""simple docstring"""






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

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

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

					return x







				def 							__UpperCAmelCase					( self    :       List[Any]			,					lowerCamelCase_    :       List[np.ndarray]			,					lowerCamelCase_    :       Optional[np.ndarray] = None						):

					"""simple docstring"""






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







				def __call__( self    :       Dict			,					lowerCamelCase_    :       Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]			,					lowerCamelCase_    :       Union[bool, str, PaddingStrategy] = False			,					lowerCamelCase_    :       Optional[int] = None			,					lowerCamelCase_    :       bool = False			,					lowerCamelCase_    :       Optional[int] = None			,					lowerCamelCase_    :       Optional[bool] = None			,					lowerCamelCase_    :       Optional[Union[str, TensorType]] = None			,					lowerCamelCase_    :       Optional[int] = None			,					**lowerCamelCase_    :       Optional[int]			,					):

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

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

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

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

					# extract fbank features
					_lowercase :    Optional[Any]							=							[self._extract_mfsc_features(lowerCamelCase_						) for one_waveform in raw_speech]

					# convert into correct format for padding
					_lowercase :    Optional[int]							=							BatchFeature({'input_features': features}						)

					_lowercase :    Tuple							=							self.pad(
					    lowerCamelCase_			,					padding=lowerCamelCase_			,					max_length=lowerCamelCase_			,					truncation=lowerCamelCase_			,					pad_to_multiple_of=lowerCamelCase_			,					return_attention_mask=lowerCamelCase_			,					**lowerCamelCase_			,					)
					# make sure list is in array format
					_lowercase :    Dict							=							padded_inputs.get('input_features'						)
					if isinstance(input_features[0]			,					lowerCamelCase_						):
						_lowercase :    List[str]							=							[np.asarray(lowerCamelCase_			,					dtype=np.floataa						) for feature in input_features]

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

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

					if return_tensors is not None:
						_lowercase :    Union[str, Any]							=							padded_inputs.convert_to_tensors(lowerCamelCase_						)

					return padded_inputs



