


from math import ceil
from typing import List, Optional, Union

import numpy as np

from ...audio_utils import mel_filter_bank, spectrogram, window_function
from ...feature_extraction_sequence_utils import BatchFeature, SequenceFeatureExtractor
from ...utils import TensorType, logging


_snake_case  :	Dict			      =       logging.get_logger(__name__)

class        a	(_lowerCAmelCase   ):




							"""simple docstring"""


							__UpperCAmelCase					:							Optional[Any]  						=						["audio_values", "audio_mask"]

							def __init__(							self :						Union[str, Any]					,			lowerCamelCase :						Optional[Any]=2048					,			lowerCamelCase :						str=1					,			lowerCamelCase :						Dict=[16, 16]					,			lowerCamelCase :						Dict=128					,			lowerCamelCase :						Dict=44100					,			lowerCamelCase :						Optional[int]=86					,			lowerCamelCase :						int=2048					,			lowerCamelCase :						str=0.0					,			**lowerCamelCase :						Any					,			)			->					Dict:
														super().__init__(
														    feature_size=lowerCamelCase					,			sampling_rate=lowerCamelCase					,			padding_value=lowerCamelCase					,			**lowerCamelCase					,			)

														__snake_case	:    List[str]											=     spectrogram_length
														__snake_case	:    Union[str, Any]											=     num_channels
														__snake_case	:    Optional[Any]											=     patch_size
														__snake_case	:    List[str]											=     feature_size // self.patch_size[1]
														__snake_case	:    Union[str, Any]											=     n_fft
														__snake_case	:    Optional[int]											=     sampling_rate // hop_length_to_sampling_rate
														__snake_case	:    int											=     sampling_rate
														__snake_case	:    Optional[Any]											=     padding_value
														__snake_case	:    Union[str, Any]											=     mel_filter_bank(
														    num_frequency_bins=1 + n_fft // 2					,			num_mel_filters=lowerCamelCase					,			min_frequency=0.0					,			max_frequency=2_20_50.0					,			sampling_rate=lowerCamelCase					,			norm="slaney"					,			mel_scale="slaney"					,			).T

							def 					__snake_case      (							self :						Union[str, Any]					,			lowerCamelCase :						np.array       )			->					np.ndarray:
														__snake_case	:    Any											=     spectrogram(
														    lowerCamelCase					,			window_function(self.n_fft					,			"hann"       )					,			frame_length=self.n_fft					,			hop_length=self.hop_length					,			power=2.0					,			mel_filters=self.mel_filters.T					,			log_mel="dB"					,			db_range=80.0					,			)
														__snake_case	:    Dict											=     log_spec[:, :-1]
														__snake_case	:    Any											=     log_spec - 20.0
														__snake_case	:    List[str]											=     np.clip(log_spec / 40.0					,			-2.0					,			0.0       ) + 1.0
														return log_spec






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

														if sampling_rate is not None:
																					if sampling_rate != self.sampling_rate:
																												raise ValueError(
																												    "This feature extractor is set to support sampling rate"
																												    F' of {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled'
																												    F' with {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	:    int											=     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}'       )
														__snake_case	:    Optional[Any]											=     is_batched_numpy or (
														    isinstance(lowerCamelCase					,			(list, tuple)       ) and (isinstance(raw_speech[0]					,			(np.ndarray, tuple, list)       ))
														)
														if is_batched:
																					__snake_case	:    str											=     [np.asarray([speech]					,			dtype=np.floataa       ).T for speech in raw_speech]
														elif not is_batched and not isinstance(lowerCamelCase					,			np.ndarray       ):
																					__snake_case	:    Optional[Any]											=     np.asarray(lowerCamelCase					,			dtype=np.floataa       )
														elif isinstance(lowerCamelCase					,			np.ndarray       ) and raw_speech.dtype is np.dtype(np.floataa       ):
																					__snake_case	:    List[str]											=     raw_speech.astype(np.floataa       )
														# always return batch
														if not is_batched:
																					__snake_case	:    List[str]											=     [np.asarray([raw_speech]       ).T]

														# Convert audio signals to log mel spectrograms, truncate by time axis
														__snake_case	:    List[Any]											=     [
														    self._np_extract_fbank_features(waveform.squeeze()       ).T[: self.spectrogram_length] for waveform in raw_speech
														]
														if isinstance(audio_features[0]					,			lowerCamelCase       ):
																					__snake_case	:    Optional[Any]											=     [np.asarray(lowerCamelCase					,			dtype=np.floataa       ) for feature in audio_features]

														# Create audio attention mask
														__snake_case	:    Optional[int]											=     max(
														    [ceil(feature.shape[0] / self.patch_size[0]       ) * self.freq_len for feature in audio_features]       )  # The maximum number of audio patches in a batch
														if return_attention_mask:
																					__snake_case	:    Any											=     [
																					    (ceil(feature.shape[0] / self.patch_size[0]       ) * self.freq_len) * [1]
																					    + (max_patch_len - ceil(feature.shape[0] / self.patch_size[0]       ) * self.freq_len) * [0]
																					    for feature in audio_features
																					]
																					__snake_case	:    str											=     np.array(lowerCamelCase       ).astype(np.floataa       )

														# convert into correct format for padding
														__snake_case	:    Dict											=     max_patch_len // self.freq_len * self.patch_size[0]  # The maximum audio size in a batch
														__snake_case	:    int											=     np.ones([len(lowerCamelCase       ), 1, max_time_len, self.feature_size]       ).astype(np.floataa       )
														__snake_case	:    Any											=     padded_audio_features * self.padding_value
														for i in range(len(lowerCamelCase       )       ):
																					__snake_case	:    str											=     audio_features[i]
																					__snake_case	:    Any											=     feature

														# return as BatchFeature
														if return_attention_mask:
																					__snake_case	:    Optional[Any]											=     {"audio_values": padded_audio_features, "audio_mask": audio_mask}
														else:
																					__snake_case	:    Tuple											=     {"audio_values": padded_audio_features}

														__snake_case	:    int											=     BatchFeature(data=lowerCamelCase					,			tensor_type=lowerCamelCase       )
														return encoded_inputs





import warnings

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


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

_snake_case  :	str			      =       {
    "xlnet-base-cased": "https://huggingface.co/xlnet-base-cased/resolve/main/config.json",
    "xlnet-large-cased": "https://huggingface.co/xlnet-large-cased/resolve/main/config.json",
}

class        a	(_lowerCAmelCase   ):




							"""simple docstring"""


							__UpperCAmelCase					:							List[Any]  						=						"xlnet"
							__UpperCAmelCase					:							Dict  						=						["mems"]
							__UpperCAmelCase					:							Tuple  						=						{
							    "n_token": "vocab_size",  # Backward compatibility
							    "hidden_size": "d_model",
							    "num_attention_heads": "n_head",
							    "num_hidden_layers": "n_layer",
							}

							def __init__(							self :						List[Any]					,			lowerCamelCase :						Optional[int]=32000					,			lowerCamelCase :						str=1024					,			lowerCamelCase :						int=24					,			lowerCamelCase :						List[Any]=16					,			lowerCamelCase :						Any=4096					,			lowerCamelCase :						Optional[Any]="gelu"					,			lowerCamelCase :						Optional[Any]=True					,			lowerCamelCase :						Optional[int]="bi"					,			lowerCamelCase :						Union[str, Any]=0.02					,			lowerCamelCase :						Union[str, Any]=1E-12					,			lowerCamelCase :						Dict=0.1					,			lowerCamelCase :						int=512					,			lowerCamelCase :						str=None					,			lowerCamelCase :						Dict=True					,			lowerCamelCase :						str=False					,			lowerCamelCase :						Any=False					,			lowerCamelCase :						Optional[Any]=-1					,			lowerCamelCase :						List[Any]=False					,			lowerCamelCase :						Optional[int]="last"					,			lowerCamelCase :						Dict=True					,			lowerCamelCase :						Union[str, Any]="tanh"					,			lowerCamelCase :						int=0.1					,			lowerCamelCase :						Any=5					,			lowerCamelCase :						List[str]=5					,			lowerCamelCase :						List[Any]=5					,			lowerCamelCase :						Tuple=1					,			lowerCamelCase :						Dict=2					,			**lowerCamelCase :						List[str]					,			)			->					List[Any]:
														__snake_case	:    Optional[Any]											=     vocab_size
														__snake_case	:    Any											=     d_model
														__snake_case	:    int											=     n_layer
														__snake_case	:    Optional[int]											=     n_head
														if d_model % n_head != 0:
																					raise ValueError(F'\'d_model % n_head\' ({d_model % n_head}) should be equal to 0'       )
														if "d_head" in kwargs:
																					if kwargs["d_head"] != d_model // n_head:
																												raise ValueError(
																												    F'`d_head` ({kwargs["d_head"]}) should be equal to `d_model // n_head` ({d_model // n_head})'       )
														__snake_case	:    Any											=     d_model // n_head
														__snake_case	:    Tuple											=     ff_activation
														__snake_case	:    int											=     d_inner
														__snake_case	:    Optional[Any]											=     untie_r
														__snake_case	:    Optional[Any]											=     attn_type

														__snake_case	:    List[str]											=     initializer_range
														__snake_case	:    Tuple											=     layer_norm_eps

														__snake_case	:    str											=     dropout
														__snake_case	:    Any											=     mem_len
														__snake_case	:    Tuple											=     reuse_len
														__snake_case	:    Union[str, Any]											=     bi_data
														__snake_case	:    List[Any]											=     clamp_len
														__snake_case	:    Optional[int]											=     same_length

														__snake_case	:    List[str]											=     summary_type
														__snake_case	:    Any											=     summary_use_proj
														__snake_case	:    Any											=     summary_activation
														__snake_case	:    Optional[int]											=     summary_last_dropout
														__snake_case	:    Dict											=     start_n_top
														__snake_case	:    Optional[Any]											=     end_n_top

														__snake_case	:    List[str]											=     bos_token_id
														__snake_case	:    Any											=     pad_token_id
														__snake_case	:    Optional[Any]											=     eos_token_id

														if "use_cache" in kwargs:
																					warnings.warn(
																					    "The `use_cache` argument is deprecated and will be removed in a future version, use `use_mems_eval`"
																					    " instead."					,			lowerCamelCase					,			)
																					__snake_case	:    Any											=     kwargs["use_cache"]

														__snake_case	:    int											=     use_mems_eval
														__snake_case	:    Optional[int]											=     use_mems_train
														super().__init__(pad_token_id=lowerCamelCase					,			bos_token_id=lowerCamelCase					,			eos_token_id=lowerCamelCase					,			**lowerCamelCase       )

							@property
							def 					__snake_case      (							self :						Any       )			->					Optional[int]:
														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 					__snake_case      (							self :						Union[str, Any]					,			lowerCamelCase :						Any       )			->					Optional[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.'       )

