





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

a        =						random.Random()

def 						__magic_name__       (							__UpperCAmelCase							,	__UpperCAmelCase=1.0							,	__UpperCAmelCase=None							,	__UpperCAmelCase=None      )						->					Optional[int]:

   '''simple docstring'''







   if rng is None:
      __SCREAMING_SNAKE_CASE      							=       global_rng

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

   return values







class __a (   unittest.TestCase			):





   def __init__(					self				:			List[Any]				,lowerCamelCase				:			Optional[Any]				,lowerCamelCase				:			Optional[Any]=7				,lowerCamelCase				:			Union[str, Any]=400				,lowerCamelCase				:			Any=2000				,lowerCamelCase				:			Optional[Any]=2048				,lowerCamelCase				:			Any=128				,lowerCamelCase				:			List[Any]=1				,lowerCamelCase				:			str=512				,lowerCamelCase				:			Any=30				,lowerCamelCase				:			List[str]=4_4100				,):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       parent
      __SCREAMING_SNAKE_CASE      							=       batch_size
      __SCREAMING_SNAKE_CASE      							=       min_seq_length
      __SCREAMING_SNAKE_CASE      							=       max_seq_length
      __SCREAMING_SNAKE_CASE      							=       (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
      __SCREAMING_SNAKE_CASE      							=       spectrogram_length
      __SCREAMING_SNAKE_CASE      							=       feature_size
      __SCREAMING_SNAKE_CASE      							=       num_audio_channels
      __SCREAMING_SNAKE_CASE      							=       hop_length
      __SCREAMING_SNAKE_CASE      							=       chunk_length
      __SCREAMING_SNAKE_CASE      							=       sampling_rate





   def      UpperCAmelCase__     (					self				:			List[Any]    ):

      '''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      UpperCAmelCase__     (					self				:			str				,lowerCamelCase				:			str=False				,lowerCamelCase				:			Any=False    ):

      '''simple docstring'''

      def _flatten(lowerCamelCase				:			Optional[int]    ):
         return list(itertools.chain(*lowerCamelCase    )    )

      if equal_length:
         __SCREAMING_SNAKE_CASE      							=       [floats_list((self.max_seq_length, self.feature_size)    ) for _ in range(self.batch_size    )]
      else:
         # make sure that inputs increase in size
         __SCREAMING_SNAKE_CASE      							=       [
             floats_list((x, self.feature_size)    )
             for x in range(self.min_seq_length				,self.max_seq_length				,self.seq_length_diff    )
         ]
      if numpify:
         __SCREAMING_SNAKE_CASE      							=       [np.asarray(lowerCamelCase    ) for x in speech_inputs]
      return speech_inputs







@require_torch
@require_torchaudio
class __a (   _snake_case,   unittest.TestCase			):
   __UpperCamelCase   : Any	      =	TvltFeatureExtractor





   def      UpperCAmelCase__     (					self				:			int    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       TvltFeatureExtractionTester(self    )





   def      UpperCAmelCase__     (					self				:			Any    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.feature_extraction_class(**self.feat_extract_dict    )
      self.assertTrue(hasattr(lowerCamelCase				,"""spectrogram_length"""    )    )
      self.assertTrue(hasattr(lowerCamelCase				,"""feature_size"""    )    )
      self.assertTrue(hasattr(lowerCamelCase				,"""num_audio_channels"""    )    )
      self.assertTrue(hasattr(lowerCamelCase				,"""hop_length"""    )    )
      self.assertTrue(hasattr(lowerCamelCase				,"""chunk_length"""    )    )
      self.assertTrue(hasattr(lowerCamelCase				,"""sampling_rate"""    )    )





   def      UpperCAmelCase__     (					self				:			Optional[Any]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.feature_extraction_class(**self.feat_extract_dict    )

      with tempfile.TemporaryDirectory() as tmpdirname:
         __SCREAMING_SNAKE_CASE      							=       feat_extract_first.save_pretrained(lowerCamelCase    )[0]
         check_json_file_has_correct_format(lowerCamelCase    )
         __SCREAMING_SNAKE_CASE      							=       self.feature_extraction_class.from_pretrained(lowerCamelCase    )

      __SCREAMING_SNAKE_CASE      							=       feat_extract_first.to_dict()
      __SCREAMING_SNAKE_CASE      							=       feat_extract_second.to_dict()
      __SCREAMING_SNAKE_CASE      							=       dict_first.pop("""mel_filters"""    )
      __SCREAMING_SNAKE_CASE      							=       dict_second.pop("""mel_filters"""    )
      self.assertTrue(np.allclose(lowerCamelCase				,lowerCamelCase    )    )
      self.assertEqual(lowerCamelCase				,lowerCamelCase    )





   def      UpperCAmelCase__     (					self				:			List[str]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.feature_extraction_class(**self.feat_extract_dict    )

      with tempfile.TemporaryDirectory() as tmpdirname:
         __SCREAMING_SNAKE_CASE      							=       os.path.join(lowerCamelCase				,"""feat_extract.json"""    )
         feat_extract_first.to_json_file(lowerCamelCase    )
         __SCREAMING_SNAKE_CASE      							=       self.feature_extraction_class.from_json_file(lowerCamelCase    )

      __SCREAMING_SNAKE_CASE      							=       feat_extract_first.to_dict()
      __SCREAMING_SNAKE_CASE      							=       feat_extract_second.to_dict()
      __SCREAMING_SNAKE_CASE      							=       dict_first.pop("""mel_filters"""    )
      __SCREAMING_SNAKE_CASE      							=       dict_second.pop("""mel_filters"""    )
      self.assertTrue(np.allclose(lowerCamelCase				,lowerCamelCase    )    )
      self.assertEqual(lowerCamelCase				,lowerCamelCase    )





   def      UpperCAmelCase__     (					self				:			int    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.feature_extraction_class(**self.feat_extract_dict    )

      # create three inputs of length 800, 1000, and 1200
      __SCREAMING_SNAKE_CASE      							=       [floats_list((1, x)    )[0] for x in range(800				,1400				,200    )]
      __SCREAMING_SNAKE_CASE      							=       [np.asarray(lowerCamelCase    ) for speech_input in speech_inputs]

      # Test not batched input
      __SCREAMING_SNAKE_CASE      							=       feature_extractor(np_speech_inputs[0]				,return_tensors="""np"""				,sampling_rate=4_4100    ).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
      __SCREAMING_SNAKE_CASE      							=       feature_extractor(lowerCamelCase				,return_tensors="""np"""				,sampling_rate=4_4100    ).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
      __SCREAMING_SNAKE_CASE      							=       feature_extractor(
          lowerCamelCase				,return_tensors="""np"""				,sampling_rate=4_4100				,mask_audio=lowerCamelCase    ).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.
      __SCREAMING_SNAKE_CASE      							=       [floats_list((1, x)    )[0] for x in (800, 800, 800)]
      __SCREAMING_SNAKE_CASE      							=       np.asarray(lowerCamelCase    )
      __SCREAMING_SNAKE_CASE      							=       feature_extractor(lowerCamelCase				,return_tensors="""np"""				,sampling_rate=4_4100    ).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      UpperCAmelCase__     (					self				:			int				,lowerCamelCase				:			List[str]    ):

      '''simple docstring'''

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

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





   def      UpperCAmelCase__     (					self				:			List[str]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self._load_datasamples(1    )
      __SCREAMING_SNAKE_CASE      							=       TvltFeatureExtractor()
      __SCREAMING_SNAKE_CASE      							=       feature_extractor(lowerCamelCase				,return_tensors="""pt"""    ).audio_values

      self.assertEquals(audio_values.shape				,(1, 1, 192, 128)    )

      __SCREAMING_SNAKE_CASE      							=       torch.tensor([[-0.3_032, -0.2_708], [-0.4_434, -0.4_007]]    )
      self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2]				,lowerCamelCase				,atol=1E-4    )    )












'''simple docstring'''

import inspect
import unittest

import numpy as np

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

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
    import jax

    from transformers.models.vit.modeling_flax_vit import FlaxViTForImageClassification, FlaxViTModel







class __a (   unittest.TestCase			):





   def __init__(					self				:			Optional[int]				,lowerCamelCase				:			str				,lowerCamelCase				:			List[str]=13				,lowerCamelCase				:			Optional[Any]=30				,lowerCamelCase				:			Dict=2				,lowerCamelCase				:			List[Any]=3				,lowerCamelCase				:			List[str]=True				,lowerCamelCase				:			str=True				,lowerCamelCase				:			Optional[int]=32				,lowerCamelCase				:			Dict=5				,lowerCamelCase				:			Optional[int]=4				,lowerCamelCase				:			List[Any]=37				,lowerCamelCase				:			Union[str, Any]="gelu"				,lowerCamelCase				:			List[Any]=0.1				,lowerCamelCase				:			Any=0.1				,lowerCamelCase				:			str=10				,lowerCamelCase				:			Dict=0.02				,):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       parent
      __SCREAMING_SNAKE_CASE      							=       batch_size
      __SCREAMING_SNAKE_CASE      							=       image_size
      __SCREAMING_SNAKE_CASE      							=       patch_size
      __SCREAMING_SNAKE_CASE      							=       num_channels
      __SCREAMING_SNAKE_CASE      							=       is_training
      __SCREAMING_SNAKE_CASE      							=       use_labels
      __SCREAMING_SNAKE_CASE      							=       hidden_size
      __SCREAMING_SNAKE_CASE      							=       num_hidden_layers
      __SCREAMING_SNAKE_CASE      							=       num_attention_heads
      __SCREAMING_SNAKE_CASE      							=       intermediate_size
      __SCREAMING_SNAKE_CASE      							=       hidden_act
      __SCREAMING_SNAKE_CASE      							=       hidden_dropout_prob
      __SCREAMING_SNAKE_CASE      							=       attention_probs_dropout_prob
      __SCREAMING_SNAKE_CASE      							=       type_sequence_label_size
      __SCREAMING_SNAKE_CASE      							=       initializer_range

      # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
      __SCREAMING_SNAKE_CASE      							=       (image_size // patch_size) ** 2
      __SCREAMING_SNAKE_CASE      							=       num_patches + 1





   def      UpperCAmelCase__     (					self				:			List[Any]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

      __SCREAMING_SNAKE_CASE      							=       ViTConfig(
          image_size=self.image_size				,patch_size=self.patch_size				,num_channels=self.num_channels				,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				,is_decoder=lowerCamelCase				,initializer_range=self.initializer_range				,)

      return config, pixel_values





   def      UpperCAmelCase__     (					self				:			Tuple				,lowerCamelCase				:			int				,lowerCamelCase				:			Optional[Any]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       FlaxViTModel(config=lowerCamelCase    )
      __SCREAMING_SNAKE_CASE      							=       model(lowerCamelCase    )
      # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
      __SCREAMING_SNAKE_CASE      							=       (self.image_size, self.image_size)
      __SCREAMING_SNAKE_CASE      							=       (self.patch_size, self.patch_size)
      __SCREAMING_SNAKE_CASE      							=       (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
      self.parent.assertEqual(result.last_hidden_state.shape				,(self.batch_size, num_patches + 1, self.hidden_size)    )





   def      UpperCAmelCase__     (					self				:			Union[str, Any]				,lowerCamelCase				:			Optional[int]				,lowerCamelCase				:			Dict    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.type_sequence_label_size
      __SCREAMING_SNAKE_CASE      							=       FlaxViTForImageClassification(config=lowerCamelCase    )
      __SCREAMING_SNAKE_CASE      							=       model(lowerCamelCase    )
      self.parent.assertEqual(result.logits.shape				,(self.batch_size, self.type_sequence_label_size)    )

      # test greyscale images
      __SCREAMING_SNAKE_CASE      							=       1
      __SCREAMING_SNAKE_CASE      							=       FlaxViTForImageClassification(lowerCamelCase    )

      __SCREAMING_SNAKE_CASE      							=       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
      __SCREAMING_SNAKE_CASE      							=       model(lowerCamelCase    )





   def      UpperCAmelCase__     (					self				:			int    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.prepare_config_and_inputs()
      (
          (
          __SCREAMING_SNAKE_CASE
      )							,     (
          __SCREAMING_SNAKE_CASE
      )							,     
      )      							=       config_and_inputs
      __SCREAMING_SNAKE_CASE      							=       {"""pixel_values""": pixel_values}
      return config, inputs_dict







@require_flax
class __a (   _snake_case,   unittest.TestCase			):
   __UpperCamelCase   : Any	      =	(FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else ()





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

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       FlaxViTModelTester(self    )
      __SCREAMING_SNAKE_CASE      							=       ConfigTester(self				,config_class=lowerCamelCase				,has_text_modality=lowerCamelCase				,hidden_size=37    )





   def      UpperCAmelCase__     (					self				:			Tuple    ):

      '''simple docstring'''

      self.config_tester.run_common_tests()





   def      UpperCAmelCase__     (					self				:			List[str]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*lowerCamelCase    )





   def      UpperCAmelCase__     (					self				:			List[Any]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE      							=       self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*lowerCamelCase    )





   def      UpperCAmelCase__     (					self				:			List[Any]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE							,     __SCREAMING_SNAKE_CASE      							=       self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
         __SCREAMING_SNAKE_CASE      							=       model_class(lowerCamelCase    )
         __SCREAMING_SNAKE_CASE      							=       inspect.signature(model.__call__    )
         # signature.parameters is an OrderedDict => so arg_names order is deterministic
         __SCREAMING_SNAKE_CASE      							=       [*signature.parameters.keys()]

         __SCREAMING_SNAKE_CASE      							=       ["""pixel_values"""]
         self.assertListEqual(arg_names[:1]				,lowerCamelCase    )





   def      UpperCAmelCase__     (					self				:			Optional[int]    ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE							,     __SCREAMING_SNAKE_CASE      							=       self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
         with self.subTest(model_class.__name__    ):
            __SCREAMING_SNAKE_CASE      							=       self._prepare_for_class(lowerCamelCase				,lowerCamelCase    )
            __SCREAMING_SNAKE_CASE      							=       model_class(lowerCamelCase    )

            @jax.jit
            def model_jitted(lowerCamelCase				:			int				,**lowerCamelCase				:			Union[str, Any]    ):
               return model(pixel_values=lowerCamelCase				,**lowerCamelCase    )

            with self.subTest("""JIT Enabled"""    ):
               __SCREAMING_SNAKE_CASE      							=       model_jitted(**lowerCamelCase    ).to_tuple()

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

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





   @slow
   def      UpperCAmelCase__     (					self				:			Optional[Any]    ):

      '''simple docstring'''

      for model_class_name in self.all_model_classes:
         __SCREAMING_SNAKE_CASE      							=       model_class_name.from_pretrained("""google/vit-base-patch16-224"""    )
         __SCREAMING_SNAKE_CASE      							=       model(np.ones((1, 3, 224, 224)    )    )
         self.assertIsNotNone(lowerCamelCase    )





