

"""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 TensorType, logging


_SCREAMING_SNAKE_CASE       :  Union[str, Any]        = logging.get_logger(__name__)

class     __a   (      snake_case__    ):

			"""simple docstring"""

			SCREAMING_SNAKE_CASE_						     =						['input_values', 'attention_mask']






			def __init__(							self     :					List[str]						,  lowercase_     :					Tuple=1						,  lowercase_     :					Optional[int]=1_6000						,  lowercase_     :					Any=128						,  lowercase_     :					Tuple=1024						,  lowercase_     :					str=0.0						,  lowercase_     :					Union[str, Any]=True						,  lowercase_     :					str=-4.2_6_7_7_3_9_3						,  lowercase_     :					List[str]=4.5_6_8_9_9_7_4						,  lowercase_     :					Optional[Any]=False						,  **lowercase_     :					Optional[Any]						,  ):
								super().__init__(feature_size=lowercase_						,  sampling_rate=lowercase_						,  padding_value=lowercase_						,  **lowercase_						)
								UpperCamelCase__		:    Optional[int]         =num_mel_bins
								UpperCamelCase__		:    Tuple         =max_length
								UpperCamelCase__		:    Tuple         =do_normalize
								UpperCamelCase__		:    str         =mean
								UpperCamelCase__		:    int         =std
								UpperCamelCase__		:    str         =return_attention_mask






			def    _lowerCAmelCase			(							self     :					int						,  lowercase_     :					np.ndarray						,  lowercase_     :					int						,  ):
								UpperCamelCase__		:    Tuple         =torch.from_numpy(lowercase_						).unsqueeze(0						)
								UpperCamelCase__		:    Dict         =ta_kaldi.fbank(
								    lowercase_						,  htk_compat=lowercase_						,  sample_frequency=self.sampling_rate						,  use_energy=lowercase_						,  window_type='''hanning'''						,  num_mel_bins=self.num_mel_bins						,  dither=0.0						,  frame_shift=10						,  )

								UpperCamelCase__		:    List[Any]         =fbank.shape[0]
								UpperCamelCase__		:    Tuple         =max_length - n_frames

								# pad or truncate, depending on difference
								if difference > 0:
													UpperCamelCase__		:    Any         =torch.nn.ZeroPadad((0, 0, 0, difference)						)
													UpperCamelCase__		:    List[str]         =pad_module(lowercase_						)
								elif difference < 0:
													UpperCamelCase__		:    str         =fbank[0:max_length, :]

								UpperCamelCase__		:    str         =fbank.numpy()

								return fbank






			def    _lowerCAmelCase			(							self     :					Dict						,  lowercase_     :					np.ndarray						):
								return (input_values - (self.mean)) / (self.std * 2)






			def __call__(							self     :					Dict						,  lowercase_     :					Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]						,  lowercase_     :					Optional[int] = None						,  lowercase_     :					Optional[Union[str, TensorType]] = None						,  **lowercase_     :					List[Any]						,  ):

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

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

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

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

								# extract fbank features and pad/truncate to max_length
								UpperCamelCase__		:    Union[str, Any]         =[self._extract_fbank_features(lowercase_						,  max_length=self.max_length						) for waveform in raw_speech]

								# convert into BatchFeature
								UpperCamelCase__		:    List[Any]         =BatchFeature({'''input_values''': features}						)

								# make sure list is in array format
								UpperCamelCase__		:    Dict         =padded_inputs.get('''input_values'''						)
								if isinstance(input_values[0]						,  lowercase_						):
													UpperCamelCase__		:    Dict         =[np.asarray(lowercase_						,  dtype=np.floataa						) for feature in input_values]

								# normalization
								if self.do_normalize:
													UpperCamelCase__		:    int         =[self.normalize(lowercase_						) for feature in input_values]

								if return_tensors is not None:
													UpperCamelCase__		:    Optional[Any]         =padded_inputs.convert_to_tensors(lowercase_						)

								return padded_inputs





"""simple docstring"""





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_torch_available


_SCREAMING_SNAKE_CASE       :  Union[str, Any]        = {
    """configuration_xlm_prophetnet""": ["""XLM_PROPHETNET_PRETRAINED_CONFIG_ARCHIVE_MAP""", """XLMProphetNetConfig"""],
}

try:
				if not is_sentencepiece_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				_SCREAMING_SNAKE_CASE       :  Any        = ["""XLMProphetNetTokenizer"""]

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				_SCREAMING_SNAKE_CASE       :  Optional[Any]        = [
				    """XLM_PROPHETNET_PRETRAINED_MODEL_ARCHIVE_LIST""",
				    """XLMProphetNetDecoder""",
				    """XLMProphetNetEncoder""",
				    """XLMProphetNetForCausalLM""",
				    """XLMProphetNetForConditionalGeneration""",
				    """XLMProphetNetModel""",
				    """XLMProphetNetPreTrainedModel""",
				]


if TYPE_CHECKING:
				from .configuration_xlm_prophetnet import XLM_PROPHETNET_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMProphetNetConfig

				try:
								if not is_sentencepiece_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_xlm_prophetnet import XLMProphetNetTokenizer

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_xlm_prophetnet import (
								    XLM_PROPHETNET_PRETRAINED_MODEL_ARCHIVE_LIST,
								    XLMProphetNetDecoder,
								    XLMProphetNetEncoder,
								    XLMProphetNetForCausalLM,
								    XLMProphetNetForConditionalGeneration,
								    XLMProphetNetModel,
								    XLMProphetNetPreTrainedModel,
								)

else:
				import sys

				_SCREAMING_SNAKE_CASE       :  Dict        = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


