



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


__snake_case     :str			       =logging.get_logger(__name__)



class        lowerCAmelCase__					(  _lowerCamelCase       ):
				A_   :     int								=     ['input_features', 'attention_mask']


				def __init__(							self      :  Tuple						, __UpperCamelCase      :  List[Any]=80						, __UpperCamelCase      :  int=16_000						, __UpperCamelCase      :  Tuple=0.0						, __UpperCamelCase      :  Optional[Any]=10						, __UpperCamelCase      :  int=25						, __UpperCamelCase      :  str="hamming_window"						, __UpperCamelCase      :  List[str]=3_2_7_6_8.0						, __UpperCamelCase      :  Optional[int]=0.9_7						, __UpperCamelCase      :  List[Any]=1.0						, __UpperCamelCase      :  int=True						, __UpperCamelCase      :  str=True						, __UpperCamelCase      :  Optional[Any]=False						, **__UpperCamelCase      :  Union[str, Any]						, )		->     Tuple:
											super().__init__(feature_size=__UpperCamelCase						, sampling_rate=__UpperCamelCase						, padding_value=__UpperCamelCase						, **__UpperCamelCase       )

											A      						=    feature_size
											A      						=    sampling_rate
											A      						=    padding_value
											A      						=    hop_length
											A      						=    win_length
											A      						=    frame_signal_scale
											A      						=    preemphasis_coeff
											A      						=    mel_floor
											A      						=    normalize_means
											A      						=    normalize_vars
											A      						=    win_function
											A      						=    return_attention_mask

											A      						=    win_length * sampling_rate // 1_000
											A      						=    hop_length * sampling_rate // 1_000

											A      						=    optimal_fft_length(self.sample_size       )
											A      						=    (self.n_fft // 2) + 1


				def __UpperCamelCase						(							self      :  Tuple						, __UpperCamelCase      :  np.array       )		->     np.ndarray:
											if self.win_function == "hamming_window":
																		A      						=    window_function(window_length=self.sample_size						, name=self.win_function						, periodic=__UpperCamelCase       )
											else:
																		A      						=    window_function(window_length=self.sample_size						, name=self.win_function       )

											A      						=    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						, )

											A      						=    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 __UpperCamelCase						(							self      :  List[str]						, __UpperCamelCase      :  Optional[Any]						, __UpperCamelCase      :  str						, __UpperCamelCase      :  Tuple       )		->     Optional[int]:
											# make sure we normalize float32 arrays
											if self.normalize_means:
																		A      						=    x[:input_length].mean(axis=0       )
																		A      						=    np.subtract(__UpperCamelCase						, __UpperCamelCase       )
											if self.normalize_vars:
																		A      						=    x[:input_length].std(axis=0       )
																		A      						=    np.divide(__UpperCamelCase						, __UpperCamelCase       )

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

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

											return x


				def __UpperCamelCase						(							self      :  Optional[Any]						, __UpperCamelCase      :  List[np.ndarray]						, __UpperCamelCase      :  Optional[np.ndarray] = None       )		->     List[np.ndarray]:
											A      						=    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      :  Union[str, Any]						, __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[bool] = None						, __UpperCamelCase      :  Optional[Union[str, TensorType]] = None						, __UpperCamelCase      :  Optional[int] = None						, **__UpperCamelCase      :  List[str]						, )		->     BatchFeature:

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

											A      						=    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}'''       )
											A      						=    is_batched_numpy or (
											    isinstance(__UpperCamelCase						, (list, tuple)       ) and (isinstance(raw_speech[0]						, (np.ndarray, tuple, list)       ))
											)

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

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

											# extract fbank features
											A      						=    [self._extract_mfsc_features(__UpperCamelCase       ) for one_waveform in raw_speech]

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

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

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

											if self.normalize_means or self.normalize_vars:
																		A      						=    (
																		    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
																		)
																		A      						=    self.normalize(
																		    padded_inputs['input_features']						, attention_mask=__UpperCamelCase       )

											if return_tensors is not None:
																		A      						=    padded_inputs.convert_to_tensors(__UpperCamelCase       )

											return padded_inputs




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


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

__snake_case     :Tuple			       ={
    'transfo-xl-wt103': 'https://huggingface.co/transfo-xl-wt103/resolve/main/config.json',
}



class        lowerCAmelCase__					(  _lowerCamelCase       ):
				A_   :     Any								=     'transfo-xl'
				A_   :     Dict								=     ['mems']
				A_   :     int								=     {
				    'n_token': 'vocab_size',
				    'hidden_size': 'd_model',
				    'num_attention_heads': 'n_head',
				    'num_hidden_layers': 'n_layer',
				}


				def __init__(							self      :  Optional[Any]						, __UpperCamelCase      :  str=267_735						, __UpperCamelCase      :  int=[20_000, 40_000, 200_000]						, __UpperCamelCase      :  List[str]=1_024						, __UpperCamelCase      :  Union[str, Any]=1_024						, __UpperCamelCase      :  Optional[Any]=16						, __UpperCamelCase      :  Dict=64						, __UpperCamelCase      :  Optional[Any]=4_096						, __UpperCamelCase      :  int=4						, __UpperCamelCase      :  List[str]=False						, __UpperCamelCase      :  Optional[int]=18						, __UpperCamelCase      :  int=1_600						, __UpperCamelCase      :  Dict=1_000						, __UpperCamelCase      :  Optional[Any]=True						, __UpperCamelCase      :  Optional[Any]=True						, __UpperCamelCase      :  Optional[Any]=0						, __UpperCamelCase      :  Any=-1						, __UpperCamelCase      :  Any=True						, __UpperCamelCase      :  List[Any]=0.1						, __UpperCamelCase      :  str=0.0						, __UpperCamelCase      :  Dict=True						, __UpperCamelCase      :  int="normal"						, __UpperCamelCase      :  List[Any]=0.0_1						, __UpperCamelCase      :  str=0.0_1						, __UpperCamelCase      :  Any=0.0_2						, __UpperCamelCase      :  str=1e-5						, __UpperCamelCase      :  Optional[int]=0						, **__UpperCamelCase      :  Any						, )		->     int:
											A      						=    vocab_size
											A      						=    []
											self.cutoffs.extend(__UpperCamelCase       )
											if proj_share_all_but_first:
																		A      						=    [False] + [True] * len(self.cutoffs       )
											else:
																		A      						=    [False] + [False] * len(self.cutoffs       )
											A      						=    d_model
											A      						=    d_embed
											A      						=    d_head
											A      						=    d_inner
											A      						=    div_val
											A      						=    pre_lnorm
											A      						=    n_layer
											A      						=    n_head
											A      						=    mem_len
											A      						=    same_length
											A      						=    attn_type
											A      						=    clamp_len
											A      						=    sample_softmax
											A      						=    adaptive
											A      						=    dropout
											A      						=    dropatt
											A      						=    untie_r
											A      						=    init
											A      						=    init_range
											A      						=    proj_init_std
											A      						=    init_std
											A      						=    layer_norm_epsilon
											super().__init__(eos_token_id=__UpperCamelCase						, **__UpperCamelCase       )


				@property
				def __UpperCamelCase						(							self      :  Any       )		->     Optional[int]:
											# Message copied from Transformer-XL documentation
											logger.info(f'''The model {self.model_type} is one of the few models that has no sequence length limit.'''       )
											return -1


				@max_position_embeddings.setter
				def __UpperCamelCase						(							self      :  List[Any]						, __UpperCamelCase      :  List[Any]       )		->     List[Any]:
											# Message copied from Transformer-XL documentation
											raise NotImplementedError(
											    f'''The model {self.model_type} is one of the few models that has no sequence length limit.'''       )