


"""simple docstring"""







import warnings
from typing import Any, Dict, 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 ...utils import PaddingStrategy, TensorType, logging


__lowercase    =				logging.get_logger(__name__)






class 						_A  (  a_  ):







    """simple docstring"""




    UpperCAmelCase	:							List[str]     =	["input_values", "attention_mask"]






    def __init__( self    :   Dict    ,			__UpperCAmelCase    :   str = 1    ,			__UpperCAmelCase    :   Any = 16000    ,			__UpperCAmelCase    :   List[Any] = 0.0    ,			__UpperCAmelCase    :   int = False    ,			__UpperCAmelCase    :   Optional[int] = 80    ,			__UpperCAmelCase    :   Union[str, Any] = 16    ,			__UpperCAmelCase    :   int = 64    ,			__UpperCAmelCase    :   Any = "hann_window"    ,			__UpperCAmelCase    :   Dict = 1.0    ,			__UpperCAmelCase    :   Any = 80    ,			__UpperCAmelCase    :   Tuple = 7600    ,			__UpperCAmelCase    :   Tuple = 1e-10    ,			__UpperCAmelCase    :   int = 2    ,			__UpperCAmelCase    :   List[Any] = True    ,			**__UpperCAmelCase    :   str    ,			):
      super().__init__(feature_size=__UpperCAmelCase    ,			sampling_rate=__UpperCAmelCase    ,			padding_value=__UpperCAmelCase    ,			**__UpperCAmelCase)
      a						:		List[str]											=      do_normalize
      a						:		Optional[int]											=      return_attention_mask

      a						:		str											=      num_mel_bins
      a						:		List[str]											=      hop_length
      a						:		int											=      win_length
      a						:		Tuple											=      win_function
      a						:		Dict											=      frame_signal_scale
      a						:		Tuple											=      fmin
      a						:		Tuple											=      fmax
      a						:		Tuple											=      mel_floor
      a						:		Optional[int]											=      reduction_factor

      a						:		List[Any]											=      win_length * sampling_rate // 1000
      a						:		Tuple											=      hop_length * sampling_rate // 1000
      a						:		int											=      optimal_fft_length(self.sample_size)
      a						:		str											=      (self.n_fft // 2) + 1

      a						:		Optional[int]											=      window_function(window_length=self.sample_size    ,			name=self.win_function    ,			periodic=__UpperCAmelCase)

      a						:		Union[str, Any]											=      mel_filter_bank(
          num_frequency_bins=self.n_freqs    ,			num_mel_filters=self.num_mel_bins    ,			min_frequency=self.fmin    ,			max_frequency=self.fmax    ,			sampling_rate=self.sampling_rate    ,			norm="slaney"    ,			mel_scale="slaney"    ,			)

      if frame_signal_scale != 1.0:
        warnings.warn(
            "The argument `frame_signal_scale` is deprecated and will be removed in version 4.30.0 of Transformers"    ,			__UpperCAmelCase    ,			)
      if reduction_factor != 2.0:
        warnings.warn(
            "The argument `reduction_factor` is deprecated and will be removed in version 4.30.0 of Transformers"    ,			__UpperCAmelCase    ,			)






    @staticmethod
    # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm
    def      __snake_case							( __UpperCAmelCase    :   Optional[Any]    ,			__UpperCAmelCase    :   List[Any]    ,			__UpperCAmelCase    :   int = 0.0):







      if attention_mask is not None:
        a						:		Union[str, Any]											=      np.array(__UpperCAmelCase    ,			np.intaa)
        a						:		List[str]											=      []

        for vector, length in zip(__UpperCAmelCase    ,			attention_mask.sum(-1)):
          a						:		Optional[Any]											=      (vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1e-7)
          if length < normed_slice.shape[0]:
            a						:		Any											=      padding_value

          normed_input_values.append(__UpperCAmelCase)
      else:
        a						:		List[str]											=      [(x - x.mean()) / np.sqrt(x.var() + 1e-7) for x in input_values]

      return normed_input_values






    def      __snake_case							( self    :   List[str]    ,			__UpperCAmelCase    :   Tuple    ,			):
      a						:		Dict											=      spectrogram(
          __UpperCAmelCase    ,			window=self.window    ,			frame_length=self.sample_size    ,			hop_length=self.sample_stride    ,			fft_length=self.n_fft    ,			mel_filters=self.mel_filters    ,			mel_floor=self.mel_floor    ,			log_mel="log10"    ,			)
      return log_mel_spec.T






    def __call__( self    :   List[Any]    ,			__UpperCAmelCase    :   str = None    ,			__UpperCAmelCase    :   Tuple = None    ,			__UpperCAmelCase    :   Union[str, Any] = False    ,			__UpperCAmelCase    :   Dict = None    ,			__UpperCAmelCase    :   Optional[int] = False    ,			__UpperCAmelCase    :   Any = None    ,			__UpperCAmelCase    :   Optional[Any] = None    ,			__UpperCAmelCase    :   str = None    ,			__UpperCAmelCase    :   List[Any] = None    ,			**__UpperCAmelCase    :   List[str]    ,			):







      if audio is None and audio_target is None:
        raise ValueError("You must provide either `audio` or `audio_target` values.")

      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 audio 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.")

      if audio is not None:
        a						:		List[str]											=      self._process_audio(
            __UpperCAmelCase    ,			__UpperCAmelCase    ,			__UpperCAmelCase    ,			__UpperCAmelCase    ,			__UpperCAmelCase    ,			__UpperCAmelCase    ,			__UpperCAmelCase    ,			__UpperCAmelCase    ,			**__UpperCAmelCase    ,			)
      else:
        a						:		str											=      None

      if audio_target is not None:
        a						:		Dict											=      self._process_audio(
            __UpperCAmelCase    ,			__UpperCAmelCase    ,			__UpperCAmelCase    ,			__UpperCAmelCase    ,			__UpperCAmelCase    ,			__UpperCAmelCase    ,			__UpperCAmelCase    ,			__UpperCAmelCase    ,			**__UpperCAmelCase    ,			)

        if inputs is None:
          return inputs_target
        else:
          a						:		Tuple											=      inputs_target["input_values"]
          a						:		Any											=      inputs_target.get("attention_mask")
          if decoder_attention_mask is not None:
            a						:		Dict											=      decoder_attention_mask

      return inputs






    def      __snake_case							( self    :   Optional[Any]    ,			__UpperCAmelCase    :   int    ,			__UpperCAmelCase    :   Any = False    ,			__UpperCAmelCase    :   int = False    ,			__UpperCAmelCase    :   Union[str, Any] = None    ,			__UpperCAmelCase    :   str = False    ,			__UpperCAmelCase    :   Optional[Any] = None    ,			__UpperCAmelCase    :   Any = None    ,			__UpperCAmelCase    :   Union[str, Any] = None    ,			**__UpperCAmelCase    :   Optional[int]    ,			):
      a						:		str											=      isinstance(__UpperCAmelCase    ,			np.ndarray) and len(speech.shape) > 1
      if is_batched_numpy and len(speech.shape) > 2:
        raise ValueError(f'''Only mono-channel audio is supported for input to {self}''')
      a						:		List[Any]											=      is_batched_numpy or (
          isinstance(__UpperCAmelCase    ,			(list, tuple)) and (isinstance(speech[0]    ,			(np.ndarray, tuple, list)))
      )

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

      # always return batch
      if not is_batched:
        a						:		Any											=      [speech]

      # needed to make pad() work on spectrogram inputs
      a						:		Tuple											=      self.feature_size

      # convert into correct format for padding
      if is_target:
        a						:		Any											=      [self._extract_mel_features(__UpperCAmelCase) for waveform in speech]
        a						:		Any											=      BatchFeature({"input_values": features})
        a						:		Any											=      self.num_mel_bins
      else:
        a						:		Dict											=      BatchFeature({"input_values": speech})

      a						:		Optional[Any]											=      self.pad(
          __UpperCAmelCase    ,			padding=__UpperCAmelCase    ,			max_length=__UpperCAmelCase    ,			truncation=__UpperCAmelCase    ,			pad_to_multiple_of=__UpperCAmelCase    ,			return_attention_mask=__UpperCAmelCase    ,			**__UpperCAmelCase    ,			)

      a						:		Optional[Any]											=      feature_size_hack

      # convert input values to correct format
      a						:		Any											=      padded_inputs["input_values"]
      if not isinstance(input_values[0]    ,			np.ndarray):
        a						:		Optional[Any]											=      [np.asarray(__UpperCAmelCase    ,			dtype=np.floataa) for array in input_values]
      elif (
          not isinstance(__UpperCAmelCase    ,			np.ndarray)
          and isinstance(input_values[0]    ,			np.ndarray)
          and input_values[0].dtype is np.dtype(np.floataa)
      ):
        a						:		List[Any]											=      [array.astype(np.floataa) for array in input_values]
      elif isinstance(__UpperCAmelCase    ,			np.ndarray) and input_values.dtype is np.dtype(np.floataa):
        a						:		Optional[Any]											=      input_values.astype(np.floataa)

      # convert attention_mask to correct format
      a						:		Union[str, Any]											=      padded_inputs.get("attention_mask")
      if attention_mask is not None:
        a						:		Any											=      [np.asarray(__UpperCAmelCase    ,			dtype=np.intaa) for array in attention_mask]

      # zero-mean and unit-variance normalization
      if not is_target and self.do_normalize:
        a						:		int											=      (
            attention_mask
            if self._get_padding_strategies(__UpperCAmelCase    ,			max_length=__UpperCAmelCase) is not PaddingStrategy.DO_NOT_PAD
            else None
        )
        a						:		Dict											=      self.zero_mean_unit_var_norm(
            padded_inputs["input_values"]    ,			attention_mask=__UpperCAmelCase    ,			padding_value=self.padding_value)

      if return_tensors is not None:
        a						:		Union[str, Any]											=      padded_inputs.convert_to_tensors(__UpperCAmelCase)

      return padded_inputs






    def      __snake_case							( self    :   Union[str, Any]):
      a						:		Dict											=      super().to_dict()

      # Don't serialize these as they are derived from the other properties.
      a						:		Tuple											=      ["window", "mel_filters", "sample_size", "sample_stride", "n_fft", "n_freqs"]
      for name in names:
        if name in output:
          del output[name]

      return output










"""simple docstring"""







from collections import OrderedDict
from typing import TYPE_CHECKING, Any, List, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import TensorType, logging


if TYPE_CHECKING:
 from ...onnx.config import PatchingSpec
 from ...tokenization_utils_base import PreTrainedTokenizerBase


__lowercase    =				logging.get_logger(__name__)

__lowercase    =				{
    """allenai/longformer-base-4096""": """https://huggingface.co/allenai/longformer-base-4096/resolve/main/config.json""",
    """allenai/longformer-large-4096""": """https://huggingface.co/allenai/longformer-large-4096/resolve/main/config.json""",
    """allenai/longformer-large-4096-finetuned-triviaqa""": (
        """https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/config.json"""
    ),
    """allenai/longformer-base-4096-extra.pos.embd.only""": (
        """https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/config.json"""
    ),
    """allenai/longformer-large-4096-extra.pos.embd.only""": (
        """https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/config.json"""
    ),
}






class 						_A  (  _a  ):







    """simple docstring"""




    UpperCAmelCase	:							Optional[Any]     =	"""longformer"""






    def __init__( self    :   List[Any]    ,			__UpperCAmelCase    :   Union[List[int], int] = 512    ,			__UpperCAmelCase    :   int = 2    ,			__UpperCAmelCase    :   int = 1    ,			__UpperCAmelCase    :   int = 0    ,			__UpperCAmelCase    :   int = 2    ,			__UpperCAmelCase    :   int = 30522    ,			__UpperCAmelCase    :   int = 768    ,			__UpperCAmelCase    :   int = 12    ,			__UpperCAmelCase    :   int = 12    ,			__UpperCAmelCase    :   int = 3072    ,			__UpperCAmelCase    :   str = "gelu"    ,			__UpperCAmelCase    :   float = 0.1    ,			__UpperCAmelCase    :   float = 0.1    ,			__UpperCAmelCase    :   int = 512    ,			__UpperCAmelCase    :   int = 2    ,			__UpperCAmelCase    :   float = 0.02    ,			__UpperCAmelCase    :   float = 1e-12    ,			__UpperCAmelCase    :   bool = False    ,			**__UpperCAmelCase    :   int    ,			):
      super().__init__(pad_token_id=__UpperCAmelCase    ,			**__UpperCAmelCase)

      a						:		int											=      attention_window
      a						:		List[str]											=      sep_token_id
      a						:		List[Any]											=      bos_token_id
      a						:		int											=      eos_token_id
      a						:		Union[str, Any]											=      vocab_size
      a						:		Dict											=      hidden_size
      a						:		int											=      num_hidden_layers
      a						:		int											=      num_attention_heads
      a						:		Optional[int]											=      hidden_act
      a						:		Union[str, Any]											=      intermediate_size
      a						:		Tuple											=      hidden_dropout_prob
      a						:		Union[str, Any]											=      attention_probs_dropout_prob
      a						:		str											=      max_position_embeddings
      a						:		Dict											=      type_vocab_size
      a						:		Optional[Any]											=      initializer_range
      a						:		List[Any]											=      layer_norm_eps
      a						:		List[str]											=      onnx_export






class 						_A  (  _a  ):







    """simple docstring"""




    def __init__( self    :   Tuple    ,			__UpperCAmelCase    :   "PretrainedConfig"    ,			__UpperCAmelCase    :   str = "default"    ,			__UpperCAmelCase    :   "List[PatchingSpec]" = None):
      super().__init__(__UpperCAmelCase    ,			__UpperCAmelCase    ,			__UpperCAmelCase)
      a						:		Optional[Any]											=      True






    @property
    def      __snake_case							( self    :   int):
      if self.task == "multiple-choice":
        a						:		List[Any]											=      {0: "batch", 1: "choice", 2: "sequence"}
      else:
        a						:		int											=      {0: "batch", 1: "sequence"}
      return OrderedDict(
          [
              ("input_ids", dynamic_axis),
              ("attention_mask", dynamic_axis),
              ("global_attention_mask", dynamic_axis),
          ])






    @property
    def      __snake_case							( self    :   Union[str, Any]):
      a						:		str											=      super().outputs
      if self.task == "default":
        a						:		Tuple											=      {0: "batch"}
      return outputs






    @property
    def      __snake_case							( self    :   Optional[int]):
      return 1e-4






    @property
    def      __snake_case							( self    :   str):
      # needs to be >= 14 to support tril operator
      return max(super().default_onnx_opset    ,			14)






    def      __snake_case							( self    :   List[str]    ,			__UpperCAmelCase    :   "PreTrainedTokenizerBase"    ,			__UpperCAmelCase    :   int = -1    ,			__UpperCAmelCase    :   int = -1    ,			__UpperCAmelCase    :   bool = False    ,			__UpperCAmelCase    :   Optional[TensorType] = None    ,			):
      a						:		str											=      super().generate_dummy_inputs(
          preprocessor=__UpperCAmelCase    ,			batch_size=__UpperCAmelCase    ,			seq_length=__UpperCAmelCase    ,			is_pair=__UpperCAmelCase    ,			framework=__UpperCAmelCase)
      import torch

      # for some reason, replacing this code by inputs["global_attention_mask"] = torch.randint(2, inputs["input_ids"].shape, dtype=torch.int64)
      # makes the export fail randomly
      a						:		Union[str, Any]											=      torch.zeros_like(inputs["input_ids"])
      # make every second token global
      a						:		Dict											=      1

      return inputs






