


import math
from collections import OrderedDict

import torch
from packaging import version
from torch import Tensor, nn

from .utils import logging


A__     				=	logging.get_logger(__name__)





class  __lowerCAmelCase			(					nn.Module	):




				def __init__(							self		):



									"""simple docstring"""





									super().__init__()
									if version.parse(torch.__version__		) < version.parse("""1.12.0"""		):
														raise ImportError(
														    F'You are using torch=={torch.__version__}, but torch>=1.12.0 is required to use '
														    """PytorchGELUTanh. Please upgrade torch."""		)




				def 							snake_case       (							self						,    _snake_case		):



									"""simple docstring"""





									return nn.functional.gelu(_snake_case						,    approximate="""tanh"""		)





class  __lowerCAmelCase			(					nn.Module	):




				def 							snake_case       (							self						,    _snake_case		):



									"""simple docstring"""





									return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi		) * (input + 0.04_4715 * torch.pow(_snake_case						,    3.0		))		))





class  __lowerCAmelCase			(					nn.Module	):




				def __init__(							self						,    _snake_case = False		):



									"""simple docstring"""





									super().__init__()
									if use_gelu_python:
														_lowerCAmelCase      =   self._gelu_python
									else:
														_lowerCAmelCase      =   nn.functional.gelu




				def 							snake_case       (							self						,    _snake_case		):



									"""simple docstring"""





									return input * 0.5 * (1.0 + torch.erf(input / math.sqrt(2.0		)		))




				def 							snake_case       (							self						,    _snake_case		):



									"""simple docstring"""





									return self.act(_snake_case		)





class  __lowerCAmelCase			(					nn.Module	):




				def 							snake_case       (							self						,    _snake_case		):



									"""simple docstring"""





									return 0.5 * input * (1.0 + torch.tanh(input * 0.79_7884_5608 * (1.0 + 0.04_4715 * input * input)		))





class  __lowerCAmelCase			(					nn.Module	):




				def 							snake_case       (							self						,    _snake_case		):



									"""simple docstring"""





									return input * torch.sigmoid(1.702 * input		)





class  __lowerCAmelCase			(					nn.Module	):




				def __init__(							self						,    _snake_case						,    _snake_case		):



									"""simple docstring"""





									if min > max:
														raise ValueError(F'min should be < max (got min: {min}, max: {max})'		)

									super().__init__()
									_lowerCAmelCase      =   min
									_lowerCAmelCase      =   max




				def 							snake_case       (							self						,    _snake_case		):



									"""simple docstring"""





									return torch.clip(gelu(_snake_case		)						,    self.min						,    self.max		)





class  __lowerCAmelCase			(					nn.Module	):




				def __init__(							self		):



									"""simple docstring"""





									super().__init__()
									_lowerCAmelCase      =   math.sqrt(2 / math.pi		)




				def 							snake_case       (							self						,    _snake_case		):



									"""simple docstring"""





									return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.04_4715 * torch.pow(_snake_case						,    3		))		))





class  __lowerCAmelCase			(					nn.Module	):




				def 							snake_case       (							self						,    _snake_case		):



									"""simple docstring"""





									return nn.functional.silu(_snake_case		)





class  __lowerCAmelCase			(					nn.Module	):




				def __init__(							self		):



									"""simple docstring"""





									super().__init__()
									if version.parse(torch.__version__		) < version.parse("""1.9.0"""		):
														_lowerCAmelCase      =   self._mish_python
									else:
														_lowerCAmelCase      =   nn.functional.mish




				def 							snake_case       (							self						,    _snake_case		):



									"""simple docstring"""





									return input * torch.tanh(nn.functional.softplus(_snake_case		)		)




				def 							snake_case       (							self						,    _snake_case		):



									"""simple docstring"""





									return self.act(_snake_case		)





class  __lowerCAmelCase			(					nn.Module	):




				def 							snake_case       (							self						,    _snake_case		):



									"""simple docstring"""





									return input





class  __lowerCAmelCase			(					nn.Module	):




				def 							snake_case       (							self						,    _snake_case						,    _snake_case=0.70_7107						,    _snake_case=0.28_2095		):



									"""simple docstring"""





									_lowerCAmelCase      =   (input - mu).div(sigma * math.sqrt(2.0		)		)
									return 0.5 * (1.0 + torch.erf(_snake_case		))





class  __lowerCAmelCase			(					nn.Module	):




				def 							snake_case       (							self						,    _snake_case		):



									"""simple docstring"""





									_lowerCAmelCase      =   nn.functional.relu(_snake_case		)
									_lowerCAmelCase      =   torch.square(_snake_case		)
									return squared





class  __lowerCAmelCase			(					lowerCamelCase__	):




				def __getitem__(							self						,    _snake_case		):



									"""simple docstring"""





									_lowerCAmelCase      =   super().__getitem__(_snake_case		)
									_lowerCAmelCase     ,     _lowerCAmelCase      =   content if isinstance(_snake_case						,    _snake_case		) else (content, {})
									return cls(**_snake_case		)

A__     				=	{
    """gelu""": GELUActivation,
    """gelu_10""": (ClippedGELUActivation, {"""min""": -10, """max""": 10}),
    """gelu_fast""": FastGELUActivation,
    """gelu_new""": NewGELUActivation,
    """gelu_python""": (GELUActivation, {"""use_gelu_python""": True}),
    """gelu_pytorch_tanh""": PytorchGELUTanh,
    """gelu_accurate""": AccurateGELUActivation,
    """laplace""": LaplaceActivation,
    """linear""": LinearActivation,
    """mish""": MishActivation,
    """quick_gelu""": QuickGELUActivation,
    """relu""": nn.ReLU,
    """relu2""": ReLUSquaredActivation,
    """relu6""": nn.ReLUa,
    """sigmoid""": nn.Sigmoid,
    """silu""": SiLUActivation,
    """swish""": SiLUActivation,
    """tanh""": nn.Tanh,
}
A__     				=	ClassInstantier(ACTaCLS)


def 		_UpperCAmelCase    (				snake_case      ):





					"""simple docstring"""

					if activation_string in ACTaFN:
										return ACTaFN[activation_string]
					else:
										raise KeyError(F'function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()      )}'      )


# For backwards compatibility with: from activations import gelu_python
A__     				=	get_activation("""gelu_python""")
A__     				=	get_activation("""gelu_new""")
A__     				=	get_activation("""gelu""")
A__     				=	get_activation("""gelu_fast""")
A__     				=	get_activation("""quick_gelu""")
A__     				=	get_activation("""silu""")
A__     				=	get_activation("""mish""")
A__     				=	get_activation("""linear""")








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


A__     				=	logging.get_logger(__name__)





class  __lowerCAmelCase			(					lowerCamelCase__	):
				__lowerCamelCase			=    ['''input_values''', '''attention_mask''']




				def __init__(							self						,    _snake_case=1						,    _snake_case=16000						,    _snake_case=128						,    _snake_case=1024						,    _snake_case=0.0						,    _snake_case=True						,    _snake_case=-4.267_7393						,    _snake_case=4.568_9974						,    _snake_case=False						,    **_snake_case						,    ):



									"""simple docstring"""





									super().__init__(feature_size=_snake_case						,    sampling_rate=_snake_case						,    padding_value=_snake_case						,    **_snake_case		)
									_lowerCAmelCase      =   num_mel_bins
									_lowerCAmelCase      =   max_length
									_lowerCAmelCase      =   do_normalize
									_lowerCAmelCase      =   mean
									_lowerCAmelCase      =   std
									_lowerCAmelCase      =   return_attention_mask




				def 							snake_case       (							self						,    _snake_case						,    _snake_case						,    ):



									"""simple docstring"""





									_lowerCAmelCase      =   torch.from_numpy(_snake_case		).unsqueeze(0		)
									_lowerCAmelCase      =   ta_kaldi.fbank(
									    _snake_case						,    htk_compat=_snake_case						,    sample_frequency=self.sampling_rate						,    use_energy=_snake_case						,    window_type="""hanning"""						,    num_mel_bins=self.num_mel_bins						,    dither=0.0						,    frame_shift=10						,    )

									_lowerCAmelCase      =   fbank.shape[0]
									_lowerCAmelCase      =   max_length - n_frames

									# pad or truncate, depending on difference
									if difference > 0:
														_lowerCAmelCase      =   torch.nn.ZeroPadad((0, 0, 0, difference)		)
														_lowerCAmelCase      =   pad_module(_snake_case		)
									elif difference < 0:
														_lowerCAmelCase      =   fbank[0:max_length, :]

									_lowerCAmelCase      =   fbank.numpy()

									return fbank




				def 							snake_case       (							self						,    _snake_case		):



									"""simple docstring"""





									return (input_values - (self.mean)) / (self.std * 2)




				def __call__(							self						,    _snake_case						,    _snake_case = None						,    _snake_case = None						,    **_snake_case						,    ):



									"""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(_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(_snake_case						,    (list, tuple)		) and (isinstance(raw_speech[0]						,    (np.ndarray, tuple, list)		))
									)

									if is_batched:
														_lowerCAmelCase      =   [np.asarray(_snake_case						,    dtype=np.floataa		) for speech in raw_speech]
									elif not is_batched and not isinstance(_snake_case						,    np.ndarray		):
														_lowerCAmelCase      =   np.asarray(_snake_case						,    dtype=np.floataa		)
									elif isinstance(_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 and pad/truncate to max_length
									_lowerCAmelCase      =   [self._extract_fbank_features(_snake_case						,    max_length=self.max_length		) for waveform in raw_speech]

									# convert into BatchFeature
									_lowerCAmelCase      =   BatchFeature({"""input_values""": features}		)

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

									# normalization
									if self.do_normalize:
														_lowerCAmelCase      =   [self.normalize(_snake_case		) for feature in input_values]

									if return_tensors is not None:
														_lowerCAmelCase      =   padded_inputs.convert_to_tensors(_snake_case		)

									return padded_inputs



