



"""simple docstring"""
import unittest

from transformers import BigBirdConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
       import jax

       from transformers.models.big_bird.modeling_flax_big_bird import (
           FlaxBigBirdForCausalLM,
           FlaxBigBirdForMaskedLM,
           FlaxBigBirdForMultipleChoice,
           FlaxBigBirdForPreTraining,
           FlaxBigBirdForQuestionAnswering,
           FlaxBigBirdForSequenceClassification,
           FlaxBigBirdForTokenClassification,
           FlaxBigBirdModel,
       )


class  SCREAMING_SNAKE_CASE__			(				unittest.TestCase ):

    def __init__(					self      :					Tuple			,	lowerCAmelCase_      :					List[str]			,	lowerCAmelCase_      :					Union[str, Any]=2			,	lowerCAmelCase_      :					int=5_6			,	lowerCAmelCase_      :					Any=True			,	lowerCAmelCase_      :					Union[str, Any]=True			,	lowerCAmelCase_      :					Optional[int]=True			,	lowerCAmelCase_      :					Any=True			,	lowerCAmelCase_      :					Tuple=9_9			,	lowerCAmelCase_      :					Any=3_2			,	lowerCAmelCase_      :					List[Any]=2			,	lowerCAmelCase_      :					Optional[Any]=2			,	lowerCAmelCase_      :					Any=7			,	lowerCAmelCase_      :					Union[str, Any]="gelu_new"			,	lowerCAmelCase_      :					List[Any]=0.1			,	lowerCAmelCase_      :					Optional[Any]=0.1			,	lowerCAmelCase_      :					Optional[int]=5_1_2			,	lowerCAmelCase_      :					Optional[int]=1_6			,	lowerCAmelCase_      :					int=2			,	lowerCAmelCase_      :					List[Any]=0.02			,	lowerCAmelCase_      :					int=4			,	lowerCAmelCase_      :					Optional[Any]="block_sparse"			,	lowerCAmelCase_      :					Union[str, Any]=True			,	lowerCAmelCase_      :					List[Any]=False			,	lowerCAmelCase_      :					Optional[Any]=2			,	lowerCAmelCase_      :					Optional[Any]=3			,	):







      """simple docstring"""







      lowercase_             =			parent
      lowercase_             =			batch_size
      lowercase_             =			seq_length
      lowercase_             =			is_training
      lowercase_             =			use_attention_mask
      lowercase_             =			use_token_type_ids
      lowercase_             =			use_labels
      lowercase_             =			vocab_size
      lowercase_             =			hidden_size
      lowercase_             =			num_hidden_layers
      lowercase_             =			num_attention_heads
      lowercase_             =			intermediate_size
      lowercase_             =			hidden_act
      lowercase_             =			hidden_dropout_prob
      lowercase_             =			attention_probs_dropout_prob
      lowercase_             =			max_position_embeddings
      lowercase_             =			type_vocab_size
      lowercase_             =			type_sequence_label_size
      lowercase_             =			initializer_range
      lowercase_             =			num_choices

      lowercase_             =			rescale_embeddings
      lowercase_             =			attention_type
      lowercase_             =			use_bias
      lowercase_             =			block_size
      lowercase_             =			num_random_blocks

    def        _UpperCAmelCase						(					self      :					Optional[Any]):







      """simple docstring"""







      lowercase_             =			ids_tensor([self.batch_size, self.seq_length]			,	self.vocab_size)

      lowercase_             =			None
      if self.use_attention_mask:
        lowercase_             =			random_attention_mask([self.batch_size, self.seq_length])

      lowercase_             =			None
      if self.use_token_type_ids:
        lowercase_             =			ids_tensor([self.batch_size, self.seq_length]			,	self.type_vocab_size)

      lowercase_             =			BigBirdConfig(
          vocab_size=self.vocab_size			,	hidden_size=self.hidden_size			,	num_hidden_layers=self.num_hidden_layers			,	num_attention_heads=self.num_attention_heads			,	intermediate_size=self.intermediate_size			,	hidden_act=self.hidden_act			,	hidden_dropout_prob=self.hidden_dropout_prob			,	attention_probs_dropout_prob=self.attention_probs_dropout_prob			,	max_position_embeddings=self.max_position_embeddings			,	type_vocab_size=self.type_vocab_size			,	is_decoder=snake_case__			,	initializer_range=self.initializer_range			,	attention_type=self.attention_type			,	block_size=self.block_size			,	num_random_blocks=self.num_random_blocks			,	use_bias=self.use_bias			,	rescale_embeddings=self.rescale_embeddings			,	)

      return config, input_ids, token_type_ids, attention_mask




    def        _UpperCAmelCase						(					self      :					Tuple):







      """simple docstring"""







      lowercase_             =			self.prepare_config_and_inputs()
      lowercase_             =			config_and_inputs
      lowercase_             =			{
          "input_ids": input_ids,
          "token_type_ids": token_type_ids,
          "attention_mask": attention_mask,
      }
      return config, inputs_dict







@require_flax
class  SCREAMING_SNAKE_CASE__			(				__UpperCAmelCase   ,	unittest.TestCase ):
    lowercase__  			= (
        (
            FlaxBigBirdForCausalLM,
            FlaxBigBirdModel,
            FlaxBigBirdForPreTraining,
            FlaxBigBirdForMaskedLM,
            FlaxBigBirdForMultipleChoice,
            FlaxBigBirdForQuestionAnswering,
            FlaxBigBirdForSequenceClassification,
            FlaxBigBirdForTokenClassification,
        )
        if is_flax_available()
        else ()
    )

    lowercase__  			= False
    lowercase__  			= False

    def        _UpperCAmelCase						(					self      :					Optional[Any]):







      """simple docstring"""







      lowercase_             =			FlaxBigBirdModelTester(self)

    @slow
    # copied from `test_modeling_flax_common` because it takes much longer than other models
    def        _UpperCAmelCase						(					self      :					List[Any]):







      """simple docstring"""







      super().test_from_pretrained_save_pretrained()

    @slow
    # copied from `test_modeling_flax_common` because it takes much longer than other models
    def        _UpperCAmelCase						(					self      :					Tuple):







      """simple docstring"""







      super().test_from_pretrained_with_no_automatic_init()

    @slow
    # copied from `test_modeling_flax_common` because it takes much longer than other models
    def        _UpperCAmelCase						(					self      :					Tuple):







      """simple docstring"""







      super().test_no_automatic_init()

    @slow
    # copied from `test_modeling_flax_common` because it takes much longer than other models
    def        _UpperCAmelCase						(					self      :					Dict):







      """simple docstring"""







      super().test_hidden_states_output()

    @slow
    def        _UpperCAmelCase						(					self      :					List[Any]):







      """simple docstring"""







      for model_class_name in self.all_model_classes:
        lowercase_             =			model_class_name.from_pretrained("""google/bigbird-roberta-base""")
        self.assertIsNotNone(snake_case__)

    def        _UpperCAmelCase						(					self      :					Union[str, Any]):







      """simple docstring"""







      if self.test_attn_probs:
        super().test_attention_outputs()

    @slow
    # copied from `test_modeling_flax_common` because it takes much longer than other models
    def        _UpperCAmelCase						(					self      :					int):







      """simple docstring"""







      lowercase_             =			self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
        with self.subTest(model_class.__name__):
          lowercase_             =			self._prepare_for_class(snake_case__			,	snake_case__)
          lowercase_             =			model_class(snake_case__)

          @jax.jit
          def model_jitted(lowerCAmelCase_      :					int			,	lowerCAmelCase_      :					Optional[int]=None			,	**lowerCAmelCase_      :					Optional[Any]):
            return model(input_ids=snake_case__			,	attention_mask=snake_case__			,	**snake_case__)

          with self.subTest("""JIT Enabled"""):
            lowercase_             =			model_jitted(**snake_case__).to_tuple()

          with self.subTest("""JIT Disabled"""):
            with jax.disable_jit():
              lowercase_             =			model_jitted(**snake_case__).to_tuple()

          self.assertEqual(len(snake_case__)			,	len(snake_case__))
          for jitted_output, output in zip(snake_case__			,	snake_case__):
            self.assertEqual(jitted_output.shape			,	output.shape)




    def        _UpperCAmelCase						(					self      :					List[str]			,	lowerCAmelCase_      :					int			,	lowerCAmelCase_      :					Dict			,	lowerCAmelCase_      :					Any			,	lowerCAmelCase_      :					Union[str, Any]=1E-5			,	lowerCAmelCase_      :					Any="outputs"			,	lowerCAmelCase_      :					Optional[Any]=None):







      """simple docstring"""







      if name.startswith("""outputs.attentions"""):
        return
      else:
        super().check_pt_flax_outputs(snake_case__			,	snake_case__			,	snake_case__			,	snake_case__			,	snake_case__			,	snake_case__)



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


SCREAMING_SNAKE_CASE     =       logging.get_logger(__name__)






class 							UpperCAmelCase_							(    A				):

 '''simple docstring'''
 lowercase_					:    int				=							["input_values", "attention_mask"]







 def __init__(					self		:  Any			,				snake_case__		:  int = 1			,				snake_case__		:  int = 1_60_00			,				snake_case__		:  float = 0.0			,				snake_case__		:  bool = False			,				snake_case__		:  int = 80			,				snake_case__		:  int = 16			,				snake_case__		:  int = 64			,				snake_case__		:  str = "hann_window"			,				snake_case__		:  float = 1.0			,				snake_case__		:  float = 80			,				snake_case__		:  float = 76_00			,				snake_case__		:  float = 1e-10			,				snake_case__		:  int = 2			,				snake_case__		:  bool = True			,				**snake_case__		:  List[Any]			,				):



   '''simple docstring'''




   super().__init__(feature_size=snake_case__			,				sampling_rate=snake_case__			,				padding_value=snake_case__			,				**snake_case__ )
   UpperCAmelCase__      :					int				       =							do_normalize
   UpperCAmelCase__      :					Tuple				       =							return_attention_mask

   UpperCAmelCase__      :					Union[str, Any]				       =							num_mel_bins
   UpperCAmelCase__      :					List[str]				       =							hop_length
   UpperCAmelCase__      :					List[str]				       =							win_length
   UpperCAmelCase__      :					Union[str, Any]				       =							win_function
   UpperCAmelCase__      :					str				       =							frame_signal_scale
   UpperCAmelCase__      :					int				       =							fmin
   UpperCAmelCase__      :					Union[str, Any]				       =							fmax
   UpperCAmelCase__      :					Optional[Any]				       =							mel_floor
   UpperCAmelCase__      :					List[str]				       =							reduction_factor

   UpperCAmelCase__      :					str				       =							win_length * sampling_rate // 10_00
   UpperCAmelCase__      :					Union[str, Any]				       =							hop_length * sampling_rate // 10_00
   UpperCAmelCase__      :					Optional[Any]				       =							optimal_fft_length(self.sample_size )
   UpperCAmelCase__      :					str				       =							(self.n_fft // 2) + 1

   UpperCAmelCase__      :					Dict				       =							window_function(window_length=self.sample_size			,				name=self.win_function			,				periodic=snake_case__ )

   UpperCAmelCase__      :					List[str]				       =							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"			,				snake_case__			,				)
   if reduction_factor != 2.0:
     warnings.warn(
         "The argument `reduction_factor` is deprecated and will be removed in version 4.30.0 of Transformers"			,				snake_case__			,				)







 @staticmethod
 # Copied from transformers.models.wav2vec2.feature_extraction_wav2vec2.Wav2Vec2FeatureExtractor.zero_mean_unit_var_norm
 def 							UpperCamelCase  (					snake_case__		:  List[np.ndarray]			,				snake_case__		:  List[np.ndarray]			,				snake_case__		:  float = 0.0 ):



   '''simple docstring'''




   if attention_mask is not None:
     UpperCAmelCase__      :					Tuple				       =							np.array(snake_case__			,				np.intaa )
     UpperCAmelCase__      :					Tuple				       =							[]

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

       normed_input_values.append(snake_case__ )
   else:
     UpperCAmelCase__      :					Optional[int]				       =							[(x - x.mean()) / np.sqrt(x.var() + 1e-7 ) for x in input_values]

   return normed_input_values







 def 							UpperCamelCase  (					self		:  Tuple			,				snake_case__		:  np.ndarray			,				):



   '''simple docstring'''




   UpperCAmelCase__      :					Optional[int]				       =							spectrogram(
       snake_case__			,				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]			,				snake_case__		:  Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None			,				snake_case__		:  Optional[Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]] = None			,				snake_case__		:  Union[bool, str, PaddingStrategy] = False			,				snake_case__		:  Optional[int] = None			,				snake_case__		:  bool = False			,				snake_case__		:  Optional[int] = None			,				snake_case__		:  Optional[bool] = None			,				snake_case__		:  Optional[Union[str, TensorType]] = None			,				snake_case__		:  Optional[int] = None			,				**snake_case__		:  List[Any]			,				):



   '''simple docstring'''




   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:
     UpperCAmelCase__      :					Union[str, Any]				       =							self._process_audio(
         snake_case__			,				snake_case__			,				snake_case__			,				snake_case__			,				snake_case__			,				snake_case__			,				snake_case__			,				snake_case__			,				**snake_case__			,				)
   else:
     UpperCAmelCase__      :					str				       =							None

   if audio_target is not None:
     UpperCAmelCase__      :					str				       =							self._process_audio(
         snake_case__			,				snake_case__			,				snake_case__			,				snake_case__			,				snake_case__			,				snake_case__			,				snake_case__			,				snake_case__			,				**snake_case__			,				)

     if inputs is None:
       return inputs_target
     else:
       UpperCAmelCase__      :					Union[str, Any]				       =							inputs_target["input_values"]
       UpperCAmelCase__      :					Any				       =							inputs_target.get("attention_mask" )
       if decoder_attention_mask is not None:
         UpperCAmelCase__      :					Dict				       =							decoder_attention_mask

   return inputs







 def 							UpperCamelCase  (					self		:  List[Any]			,				snake_case__		:  Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]			,				snake_case__		:  bool = False			,				snake_case__		:  Union[bool, str, PaddingStrategy] = False			,				snake_case__		:  Optional[int] = None			,				snake_case__		:  bool = False			,				snake_case__		:  Optional[int] = None			,				snake_case__		:  Optional[bool] = None			,				snake_case__		:  Optional[Union[str, TensorType]] = None			,				**snake_case__		:  int			,				):



   '''simple docstring'''




   UpperCAmelCase__      :					Optional[int]				       =							isinstance(snake_case__			,				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}""" )
   UpperCAmelCase__      :					Union[str, Any]				       =							is_batched_numpy or (
       isinstance(snake_case__			,				(list, tuple) ) and (isinstance(speech[0]			,				(np.ndarray, tuple, list) ))
   )

   if is_batched:
     UpperCAmelCase__      :					List[Any]				       =							[np.asarray(snake_case__			,				dtype=np.floataa ) for speech in speech]
   elif not is_batched and not isinstance(snake_case__			,				np.ndarray ):
     UpperCAmelCase__      :					int				       =							np.asarray(snake_case__			,				dtype=np.floataa )
   elif isinstance(snake_case__			,				np.ndarray ) and speech.dtype is np.dtype(np.floataa ):
     UpperCAmelCase__      :					List[Any]				       =							speech.astype(np.floataa )

   # always return batch
   if not is_batched:
     UpperCAmelCase__      :					str				       =							[speech]

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

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

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

   UpperCAmelCase__      :					List[str]				       =							feature_size_hack

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

   # convert attention_mask to correct format
   UpperCAmelCase__      :					Dict				       =							padded_inputs.get("attention_mask" )
   if attention_mask is not None:
     UpperCAmelCase__      :					int				       =							[np.asarray(snake_case__			,				dtype=np.intaa ) for array in attention_mask]

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

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

   return padded_inputs







 def 							UpperCamelCase  (					self		:  List[Any] ):



   '''simple docstring'''




   UpperCAmelCase__      :					Union[str, Any]				       =							super().to_dict()

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

   return output



