

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


A_	= logging.get_logger(__name__)


class  UpperCAmelCase (							UpperCAmelCase__    ):

		'''simple docstring'''






		SCREAMING_SNAKE_CASE_           =					['input_features', 'attention_mask']
		def __init__(					self   ,			SCREAMING_SNAKE_CASE_=80   ,			SCREAMING_SNAKE_CASE_=16000   ,			SCREAMING_SNAKE_CASE_=0.0   ,			SCREAMING_SNAKE_CASE_=10   ,			SCREAMING_SNAKE_CASE_=25   ,			SCREAMING_SNAKE_CASE_="hamming_window"   ,			SCREAMING_SNAKE_CASE_=32_768.0   ,			SCREAMING_SNAKE_CASE_=0.97   ,			SCREAMING_SNAKE_CASE_=1.0   ,			SCREAMING_SNAKE_CASE_=True   ,			SCREAMING_SNAKE_CASE_=True   ,			SCREAMING_SNAKE_CASE_=False   ,			**SCREAMING_SNAKE_CASE_   ,			)  ->     int:






							'''simple docstring'''



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

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

							lowerCamelCase_  				=      win_length * sampling_rate // 1000
							lowerCamelCase_  				=      hop_length * sampling_rate // 1000

							lowerCamelCase_  				=      optimal_fft_length(self.sample_size		)
							lowerCamelCase_  				=      (self.n_fft // 2) + 1
		def 			UpperCamelCase(					self   ,			SCREAMING_SNAKE_CASE_		)  ->     np.ndarray:






							'''simple docstring'''



							if self.win_function == "hamming_window":
												lowerCamelCase_  				=      window_function(window_length=self.sample_size   ,			name=self.win_function   ,			periodic=SCREAMING_SNAKE_CASE_		)
							else:
												lowerCamelCase_  				=      window_function(window_length=self.sample_size   ,			name=self.win_function		)

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

							lowerCamelCase_  				=      spectrogram(
							    one_waveform * self.frame_signal_scale   ,			window=SCREAMING_SNAKE_CASE_   ,			frame_length=self.sample_size   ,			hop_length=self.sample_stride   ,			fft_length=self.n_fft   ,			center=SCREAMING_SNAKE_CASE_   ,			preemphasis=self.preemphasis_coeff   ,			mel_filters=SCREAMING_SNAKE_CASE_   ,			mel_floor=self.mel_floor   ,			log_mel='log'   ,			)
							return msfc_features.T
		def 			UpperCamelCase(					self   ,			SCREAMING_SNAKE_CASE_   ,			SCREAMING_SNAKE_CASE_   ,			SCREAMING_SNAKE_CASE_		)  ->     Dict:






							'''simple docstring'''



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

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

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

							return x
		def 			UpperCamelCase(					self   ,			SCREAMING_SNAKE_CASE_   ,			SCREAMING_SNAKE_CASE_ = None		)  ->     List[np.ndarray]:






							'''simple docstring'''



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






		def __call__(					self   ,			SCREAMING_SNAKE_CASE_   ,			SCREAMING_SNAKE_CASE_ = False   ,			SCREAMING_SNAKE_CASE_ = None   ,			SCREAMING_SNAKE_CASE_ = False   ,			SCREAMING_SNAKE_CASE_ = None   ,			SCREAMING_SNAKE_CASE_ = None   ,			SCREAMING_SNAKE_CASE_ = None   ,			SCREAMING_SNAKE_CASE_ = None   ,			**SCREAMING_SNAKE_CASE_   ,			)  ->     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.'		)

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

							if is_batched:
												lowerCamelCase_  				=      [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		):
												lowerCamelCase_  				=      np.asarray(SCREAMING_SNAKE_CASE_   ,			dtype=np.floataa		)
							elif isinstance(SCREAMING_SNAKE_CASE_   ,			np.ndarray		) and raw_speech.dtype is np.dtype(np.floataa		):
												lowerCamelCase_  				=      raw_speech.astype(np.floataa		)

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

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

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

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

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

							if self.normalize_means or self.normalize_vars:
												lowerCamelCase_  				=      (
												    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
												    and padding
												    else None
												)
												lowerCamelCase_  				=      self.normalize(
												    padded_inputs['input_features']   ,			attention_mask=SCREAMING_SNAKE_CASE_		)

							if return_tensors is not None:
												lowerCamelCase_  				=      padded_inputs.convert_to_tensors(SCREAMING_SNAKE_CASE_		)

							return padded_inputs











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


lowercase    				=   logging.get_logger(__name__)

lowercase    				=   {
    "transfo-xl-wt103": "https://huggingface.co/transfo-xl-wt103/resolve/main/config.json",
}




class 					UpperCamelCase_ (     snake_case_							):



	'''simple docstring'''




	lowerCAmelCase						=					'''transfo-xl'''
	lowerCAmelCase						=					['''mems''']
	lowerCAmelCase						=					{
	    '''n_token''': '''vocab_size''',
	    '''hidden_size''': '''d_model''',
	    '''num_attention_heads''': '''n_head''',
	    '''num_hidden_layers''': '''n_layer''',
	}




	def __init__(			self			,     a=26_77_35			,     a=[2_00_00, 4_00_00, 20_00_00]			,     a=10_24			,     a=10_24			,     a=16			,     a=64			,     a=40_96			,     a=4			,     a=False			,     a=18			,     a=16_00			,     a=10_00			,     a=True			,     a=True			,     a=0			,     a=-1			,     a=True			,     a=0.1			,     a=0.0			,     a=True			,     a="normal"			,     a=0.01			,     a=0.01			,     a=0.02			,     a=1E-5			,     a=0			,     **a			,     )			->       Union[str, Any]:
								snake_case_           =				vocab_size
								snake_case_           =				[]
								self.cutoffs.extend(a   )
								if proj_share_all_but_first:
															snake_case_           =				[False] + [True] * len(self.cutoffs   )
								else:
															snake_case_           =				[False] + [False] * len(self.cutoffs   )
								snake_case_           =				d_model
								snake_case_           =				d_embed
								snake_case_           =				d_head
								snake_case_           =				d_inner
								snake_case_           =				div_val
								snake_case_           =				pre_lnorm
								snake_case_           =				n_layer
								snake_case_           =				n_head
								snake_case_           =				mem_len
								snake_case_           =				same_length
								snake_case_           =				attn_type
								snake_case_           =				clamp_len
								snake_case_           =				sample_softmax
								snake_case_           =				adaptive
								snake_case_           =				dropout
								snake_case_           =				dropatt
								snake_case_           =				untie_r
								snake_case_           =				init
								snake_case_           =				init_range
								snake_case_           =				proj_init_std
								snake_case_           =				init_std
								snake_case_           =				layer_norm_epsilon
								super().__init__(eos_token_id=a			,     **a   )




	@property
	def 				_UpperCamelCase     (			self   )			->       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			,     a   )			->       List[str]:
								# 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.'''   )





