



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


_A				:    List[str]     						=logging.get_logger(__name__)



class 		lowerCamelCase__      (						A     ):




					'''simple docstring'''



					A_						    =						["""input_features""", """attention_mask"""]




					def __init__(  self				:	str     ,  UpperCamelCase_				:	Any=80     ,  UpperCamelCase_				:	Optional[int]=1_6000     ,  UpperCamelCase_				:	List[str]=80     ,  UpperCamelCase_				:	int=0.0     ,  UpperCamelCase_				:	Dict=True     ,  UpperCamelCase_				:	Union[str, Any]=True     ,  UpperCamelCase_				:	Tuple=True     ,  **UpperCamelCase_				:	Any     ,  )  ->       Dict:



						'''simple docstring'''





						super().__init__(feature_size=UpperCamelCase_     ,  sampling_rate=UpperCamelCase_     ,  padding_value=UpperCamelCase_     ,  **UpperCamelCase_					)
						_lowercase					:       List[str]       =		num_mel_bins
						_lowercase					:       Dict       =		do_ceptral_normalize
						_lowercase					:       str       =		normalize_means
						_lowercase					:       List[Any]       =		normalize_vars
						_lowercase					:       Tuple       =		True




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



						'''simple docstring'''





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




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



						'''simple docstring'''





						if normalize_means:
							_lowercase					:       Dict       =		x[:input_length].mean(axis=0					)
							_lowercase					:       str       =		np.subtract(UpperCamelCase_     ,  UpperCamelCase_					)
						if normalize_vars:
							_lowercase					:       int       =		x[:input_length].std(axis=0					)
							_lowercase					:       Tuple       =		np.divide(UpperCamelCase_     ,  UpperCamelCase_					)

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

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

						return x




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



						'''simple docstring'''





						_lowercase					:       Tuple       =		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				:	Dict     ,  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_				:	Dict     ,  )  ->       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.'					)

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

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

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

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

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

						_lowercase					:       Optional[int]       =		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
						_lowercase					:       Dict       =		padded_inputs.get('input_features'					)
						if isinstance(input_features[0]     ,  UpperCamelCase_					):
							_lowercase					:       Optional[Any]       =		[np.asarray(UpperCamelCase_     ,  dtype=np.floataa					) for feature in input_features]

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

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

						if return_tensors is not None:
							_lowercase					:       Optional[int]       =		padded_inputs.convert_to_tensors(UpperCamelCase_					)

						return padded_inputs





'''simple docstring'''

from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tokenizers_available,
    is_torch_available,
)


_A				:    Union[str, Any]     						={'''configuration_reformer''': ['''REFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ReformerConfig''']}

try:
		if not is_sentencepiece_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_A				:    Dict     						=['''ReformerTokenizer''']

try:
		if not is_tokenizers_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_A				:    Union[str, Any]     						=['''ReformerTokenizerFast''']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_A				:    str     						=[
		    '''REFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''ReformerAttention''',
		    '''ReformerForMaskedLM''',
		    '''ReformerForQuestionAnswering''',
		    '''ReformerForSequenceClassification''',
		    '''ReformerLayer''',
		    '''ReformerModel''',
		    '''ReformerModelWithLMHead''',
		    '''ReformerPreTrainedModel''',
		]


if TYPE_CHECKING:
		from .configuration_reformer import REFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, ReformerConfig

		try:
				if not is_sentencepiece_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_reformer import ReformerTokenizer

		try:
				if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_reformer_fast import ReformerTokenizerFast

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_reformer import (
				    REFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
				    ReformerAttention,
				    ReformerForMaskedLM,
				    ReformerForQuestionAnswering,
				    ReformerForSequenceClassification,
				    ReformerLayer,
				    ReformerModel,
				    ReformerModelWithLMHead,
				    ReformerPreTrainedModel,
				)

else:
		import sys

		_A				:    Union[str, Any]     						=_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
