

import itertools
import os
import random
import tempfile
import unittest

import numpy as np

from transformers import TvltFeatureExtractor, is_datasets_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_torch_available():
   import torch

if is_datasets_available():
   from datasets import load_dataset

__UpperCAmelCase								=			random.Random()





def      snake_case_				(__A					:						Dict ,       __A					:						Dict=1.0 ,       __A					:						Optional[int]=None ,       __A					:						str=None					)      ->     int:
   if rng is None:
      __lowerCAmelCase  :      Union[str, Any]    =						global_rng

   __lowerCAmelCase  :      List[Any]    =						[]
   for batch_idx in range(shape[0]					):
      values.append([]					)
      for _ in range(shape[1]					):
         values[-1].append(rng.random() * scale					)

   return values



class SCREAMING_SNAKE_CASE     (  unittest.TestCase			):
  """simple docstring"""






  def __init__(      self			:   Optional[int]	,							lowerCAmelCase			:   Any	,							lowerCAmelCase			:   Any=7	,							lowerCAmelCase			:   str=4_00	,							lowerCAmelCase			:   Tuple=20_00	,							lowerCAmelCase			:   List[str]=20_48	,							lowerCAmelCase			:   int=1_28	,							lowerCAmelCase			:   Union[str, Any]=1	,							lowerCAmelCase			:   Union[str, Any]=5_12	,							lowerCAmelCase			:   Optional[int]=30	,							lowerCAmelCase			:   int=4_41_00	,							)							->     str:






     """simple docstring"""







     __lowerCAmelCase  :      Optional[Any]    =						parent
     __lowerCAmelCase  :      Any    =						batch_size
     __lowerCAmelCase  :      Union[str, Any]    =						min_seq_length
     __lowerCAmelCase  :      Any    =						max_seq_length
     __lowerCAmelCase  :      int    =						(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
     __lowerCAmelCase  :      int    =						spectrogram_length
     __lowerCAmelCase  :      int    =						feature_size
     __lowerCAmelCase  :      List[Any]    =						num_audio_channels
     __lowerCAmelCase  :      int    =						hop_length
     __lowerCAmelCase  :      List[str]    =						chunk_length
     __lowerCAmelCase  :      Optional[Any]    =						sampling_rate







  def 	SCREAMING_SNAKE_CASE   (      self			:   Union[str, Any]   )							->     Dict:






     """simple docstring"""







     return {
         "spectrogram_length": self.spectrogram_length,
         "feature_size": self.feature_size,
         "num_audio_channels": self.num_audio_channels,
         "hop_length": self.hop_length,
         "chunk_length": self.chunk_length,
         "sampling_rate": self.sampling_rate,
     }







  def 	SCREAMING_SNAKE_CASE   (      self			:   Any	,							lowerCAmelCase			:   Optional[Any]=False	,							lowerCAmelCase			:   List[str]=False   )							->     str:






     """simple docstring"""







     def _flatten(lowerCAmelCase			:   Union[str, Any]   ):
        return list(itertools.chain(*_A   )   )

     if equal_length:
        __lowerCAmelCase  :      Dict    =						[floats_list((self.max_seq_length, self.feature_size)   ) for _ in range(self.batch_size   )]
     else:
        # make sure that inputs increase in size
        __lowerCAmelCase  :      int    =						[
            floats_list((x, self.feature_size)   )
            for x in range(self.min_seq_length	,							self.max_seq_length	,							self.seq_length_diff   )
        ]
     if numpify:
        __lowerCAmelCase  :      Union[str, Any]    =						[np.asarray(_A   ) for x in speech_inputs]
     return speech_inputs






@require_torch
@require_torchaudio
class SCREAMING_SNAKE_CASE     (  snake_case_       ,							unittest.TestCase			):
  """simple docstring"""






  lowerCamelCase   :      List[str]										=TvltFeatureExtractor







  def 	SCREAMING_SNAKE_CASE   (      self			:   int   )							->     Dict:






     """simple docstring"""







     __lowerCAmelCase  :      List[str]    =						TvltFeatureExtractionTester(self   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[Any]   )							->     List[Any]:






     """simple docstring"""







     __lowerCAmelCase  :      Dict    =						self.feature_extraction_class(**self.feat_extract_dict   )
     self.assertTrue(hasattr(_A	,							"""spectrogram_length"""   )   )
     self.assertTrue(hasattr(_A	,							"""feature_size"""   )   )
     self.assertTrue(hasattr(_A	,							"""num_audio_channels"""   )   )
     self.assertTrue(hasattr(_A	,							"""hop_length"""   )   )
     self.assertTrue(hasattr(_A	,							"""chunk_length"""   )   )
     self.assertTrue(hasattr(_A	,							"""sampling_rate"""   )   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   str   )							->     Optional[int]:






     """simple docstring"""







     __lowerCAmelCase  :      int    =						self.feature_extraction_class(**self.feat_extract_dict   )

     with tempfile.TemporaryDirectory() as tmpdirname:
        __lowerCAmelCase  :      List[Any]    =						feat_extract_first.save_pretrained(_A   )[0]
        check_json_file_has_correct_format(_A   )
        __lowerCAmelCase  :      List[str]    =						self.feature_extraction_class.from_pretrained(_A   )

     __lowerCAmelCase  :      Tuple    =						feat_extract_first.to_dict()
     __lowerCAmelCase  :      Dict    =						feat_extract_second.to_dict()
     __lowerCAmelCase  :      Tuple    =						dict_first.pop("""mel_filters"""   )
     __lowerCAmelCase  :      Union[str, Any]    =						dict_second.pop("""mel_filters"""   )
     self.assertTrue(np.allclose(_A	,							_A   )   )
     self.assertEqual(_A	,							_A   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   Union[str, Any]   )							->     Tuple:






     """simple docstring"""







     __lowerCAmelCase  :      Any    =						self.feature_extraction_class(**self.feat_extract_dict   )

     with tempfile.TemporaryDirectory() as tmpdirname:
        __lowerCAmelCase  :      Union[str, Any]    =						os.path.join(_A	,							"""feat_extract.json"""   )
        feat_extract_first.to_json_file(_A   )
        __lowerCAmelCase  :      Dict    =						self.feature_extraction_class.from_json_file(_A   )

     __lowerCAmelCase  :      Tuple    =						feat_extract_first.to_dict()
     __lowerCAmelCase  :      str    =						feat_extract_second.to_dict()
     __lowerCAmelCase  :      List[Any]    =						dict_first.pop("""mel_filters"""   )
     __lowerCAmelCase  :      Any    =						dict_second.pop("""mel_filters"""   )
     self.assertTrue(np.allclose(_A	,							_A   )   )
     self.assertEqual(_A	,							_A   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   Any   )							->     Optional[int]:






     """simple docstring"""







     __lowerCAmelCase  :      int    =						self.feature_extraction_class(**self.feat_extract_dict   )

     # create three inputs of length 800, 1000, and 1200
     __lowerCAmelCase  :      List[str]    =						[floats_list((1, x)   )[0] for x in range(8_00	,							14_00	,							2_00   )]
     __lowerCAmelCase  :      Tuple    =						[np.asarray(_A   ) for speech_input in speech_inputs]

     # Test not batched input
     __lowerCAmelCase  :      List[Any]    =						feature_extractor(np_speech_inputs[0]	,							return_tensors="""np"""	,							sampling_rate=4_41_00   ).audio_values

     self.assertTrue(encoded_audios.ndim == 4   )
     self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size   )
     self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length   )
     self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels   )

     # Test batched
     __lowerCAmelCase  :      List[Any]    =						feature_extractor(_A	,							return_tensors="""np"""	,							sampling_rate=4_41_00   ).audio_values

     self.assertTrue(encoded_audios.ndim == 4   )
     self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size   )
     self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length   )
     self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels   )

     # Test audio masking
     __lowerCAmelCase  :      Optional[Any]    =						feature_extractor(
         _A	,							return_tensors="""np"""	,							sampling_rate=4_41_00	,							mask_audio=_A   ).audio_values

     self.assertTrue(encoded_audios.ndim == 4   )
     self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size   )
     self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length   )
     self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels   )

     # Test 2-D numpy arrays are batched.
     __lowerCAmelCase  :      Any    =						[floats_list((1, x)   )[0] for x in (8_00, 8_00, 8_00)]
     __lowerCAmelCase  :      List[Any]    =						np.asarray(_A   )
     __lowerCAmelCase  :      List[Any]    =						feature_extractor(_A	,							return_tensors="""np"""	,							sampling_rate=4_41_00   ).audio_values
     self.assertTrue(encoded_audios.ndim == 4   )
     self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size   )
     self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length   )
     self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   int	,							lowerCAmelCase			:   Dict   )							->     Tuple:






     """simple docstring"""







     __lowerCAmelCase  :      Optional[Any]    =						load_dataset("""hf-internal-testing/librispeech_asr_dummy"""	,							"""clean"""	,							split="""validation"""   )
     # automatic decoding with librispeech
     __lowerCAmelCase  :      Optional[Any]    =						ds.sort("""id"""   ).select(range(_A   )   )[:num_samples]['audio']

     return [x["array"] for x in speech_samples]







  def 	SCREAMING_SNAKE_CASE   (      self			:   Any   )							->     Optional[int]:






     """simple docstring"""







     __lowerCAmelCase  :      Dict    =						self._load_datasamples(1   )
     __lowerCAmelCase  :      Tuple    =						TvltFeatureExtractor()
     __lowerCAmelCase  :      Optional[Any]    =						feature_extractor(_A	,							return_tensors="""pt"""   ).audio_values

     self.assertEquals(audio_values.shape	,							(1, 1, 1_92, 1_28)   )

     __lowerCAmelCase  :      Any    =						torch.tensor([[-0.3032, -0.2708], [-0.4434, -0.4007]]   )
     self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2]	,							_A	,							atol=1e-4   )   )





import unittest

import numpy as np

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

from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
   from transformers.models.bert.modeling_flax_bert import (
       FlaxBertForMaskedLM,
       FlaxBertForMultipleChoice,
       FlaxBertForNextSentencePrediction,
       FlaxBertForPreTraining,
       FlaxBertForQuestionAnswering,
       FlaxBertForSequenceClassification,
       FlaxBertForTokenClassification,
       FlaxBertModel,
   )



class SCREAMING_SNAKE_CASE     (  unittest.TestCase			):
  """simple docstring"""






  def __init__(      self			:   Tuple	,							lowerCAmelCase			:   Tuple	,							lowerCAmelCase			:   str=13	,							lowerCAmelCase			:   Optional[Any]=7	,							lowerCAmelCase			:   Optional[Any]=True	,							lowerCAmelCase			:   Any=True	,							lowerCAmelCase			:   str=True	,							lowerCAmelCase			:   Optional[Any]=True	,							lowerCAmelCase			:   Dict=99	,							lowerCAmelCase			:   Any=32	,							lowerCAmelCase			:   int=5	,							lowerCAmelCase			:   Tuple=4	,							lowerCAmelCase			:   Optional[Any]=37	,							lowerCAmelCase			:   str="gelu"	,							lowerCAmelCase			:   str=0.1	,							lowerCAmelCase			:   str=0.1	,							lowerCAmelCase			:   Any=5_12	,							lowerCAmelCase			:   Optional[Any]=16	,							lowerCAmelCase			:   Dict=2	,							lowerCAmelCase			:   Tuple=0.02	,							lowerCAmelCase			:   Optional[int]=4	,							)							->     List[Any]:






     """simple docstring"""







     __lowerCAmelCase  :      int    =						parent
     __lowerCAmelCase  :      Dict    =						batch_size
     __lowerCAmelCase  :      Dict    =						seq_length
     __lowerCAmelCase  :      Union[str, Any]    =						is_training
     __lowerCAmelCase  :      List[Any]    =						use_attention_mask
     __lowerCAmelCase  :      List[Any]    =						use_token_type_ids
     __lowerCAmelCase  :      Optional[int]    =						use_labels
     __lowerCAmelCase  :      str    =						vocab_size
     __lowerCAmelCase  :      Any    =						hidden_size
     __lowerCAmelCase  :      Optional[int]    =						num_hidden_layers
     __lowerCAmelCase  :      Optional[int]    =						num_attention_heads
     __lowerCAmelCase  :      Dict    =						intermediate_size
     __lowerCAmelCase  :      Tuple    =						hidden_act
     __lowerCAmelCase  :      Dict    =						hidden_dropout_prob
     __lowerCAmelCase  :      Any    =						attention_probs_dropout_prob
     __lowerCAmelCase  :      Union[str, Any]    =						max_position_embeddings
     __lowerCAmelCase  :      int    =						type_vocab_size
     __lowerCAmelCase  :      Tuple    =						type_sequence_label_size
     __lowerCAmelCase  :      int    =						initializer_range
     __lowerCAmelCase  :      Optional[int]    =						num_choices







  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[int]   )							->     Dict:






     """simple docstring"""







     __lowerCAmelCase  :      Optional[Any]    =						ids_tensor([self.batch_size, self.seq_length]	,							self.vocab_size   )

     __lowerCAmelCase  :      Dict    =						None
     if self.use_attention_mask:
        __lowerCAmelCase  :      Optional[int]    =						random_attention_mask([self.batch_size, self.seq_length]   )

     __lowerCAmelCase  :      Union[str, Any]    =						None
     if self.use_token_type_ids:
        __lowerCAmelCase  :      str    =						ids_tensor([self.batch_size, self.seq_length]	,							self.type_vocab_size   )

     __lowerCAmelCase  :      List[str]    =						BertConfig(
         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=lowerCAmelCase	,							initializer_range=self.initializer_range	,							)

     return config, input_ids, token_type_ids, attention_mask







  def 	SCREAMING_SNAKE_CASE   (      self			:   List[Any]   )							->     Optional[Any]:






     """simple docstring"""







     __lowerCAmelCase  :      Any    =						self.prepare_config_and_inputs()
     __lowerCAmelCase   ,__lowerCAmelCase   ,__lowerCAmelCase   ,__lowerCAmelCase  :      List[str]    =						config_and_inputs
     __lowerCAmelCase  :      Any    =						{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask}
     return config, inputs_dict







  def 	SCREAMING_SNAKE_CASE   (      self			:   Union[str, Any]   )							->     Tuple:






     """simple docstring"""







     __lowerCAmelCase  :      List[str]    =						self.prepare_config_and_inputs()
     __lowerCAmelCase   ,__lowerCAmelCase   ,__lowerCAmelCase   ,__lowerCAmelCase  :      Dict    =						config_and_inputs

     __lowerCAmelCase  :      Any    =						True
     __lowerCAmelCase  :      str    =						floats_tensor([self.batch_size, self.seq_length, self.hidden_size]   )
     __lowerCAmelCase  :      int    =						ids_tensor([self.batch_size, self.seq_length]	,							vocab_size=2   )

     return (
         config,
         input_ids,
         attention_mask,
         encoder_hidden_states,
         encoder_attention_mask,
     )






@require_flax
class SCREAMING_SNAKE_CASE     (  a_       ,							unittest.TestCase			):
  """simple docstring"""






  lowerCamelCase   :      int										=True

  lowerCamelCase   :      Any										=(
      (
          FlaxBertModel,
          FlaxBertForPreTraining,
          FlaxBertForMaskedLM,
          FlaxBertForMultipleChoice,
          FlaxBertForQuestionAnswering,
          FlaxBertForNextSentencePrediction,
          FlaxBertForSequenceClassification,
          FlaxBertForTokenClassification,
          FlaxBertForQuestionAnswering,
      )
      if is_flax_available()
      else ()
  )







  def 	SCREAMING_SNAKE_CASE   (      self			:   str   )							->     Union[str, Any]:






     """simple docstring"""







     __lowerCAmelCase  :      Union[str, Any]    =						FlaxBertModelTester(self   )







  @slow
  def 	SCREAMING_SNAKE_CASE   (      self			:   Dict   )							->     Dict:






     """simple docstring"""







     __lowerCAmelCase  :      int    =						FlaxBertModel.from_pretrained("""bert-base-cased"""   )
     __lowerCAmelCase  :      str    =						model(np.ones((1, 1)   )   )
     self.assertIsNotNone(lowerCAmelCase   )


