






'''simple docstring'''



import itertools
import random
import unittest

import numpy as np

from transformers import WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST, WavaVecaConfig, WavaVecaFeatureExtractor
from transformers.testing_utils import require_torch, slow

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


SCREAMING_SNAKE_CASE							=						random.Random()


def 				lowercase_					(     __A			:							List[str]       ,   __A			:							Any=1.0       ,   __A			:							Optional[Any]=None       ,   __A			:							Any=None      )							->   Tuple:



 """simple docstring"""




 if rng is None:
  lowercase       : int						=global_rng

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

 return values
class UpperCAmelCase_       (     unittest.TestCase					):




      """simple docstring"""




      def __init__(			self      :		int  ,   UpperCAmelCase      :		Tuple  ,   UpperCAmelCase      :		int=7  ,   UpperCAmelCase      :		Optional[int]=400  ,   UpperCAmelCase      :		List[str]=2000  ,   UpperCAmelCase      :		List[Any]=1  ,   UpperCAmelCase      :		Any=0.0  ,   UpperCAmelCase      :		Dict=1_6000  ,   UpperCAmelCase      :		Any=True  ,   UpperCAmelCase      :		int=True  ,   )	->	Any:


       '''simple docstring'''

       lowercase       : Tuple						=parent
       lowercase       : List[Any]						=batch_size
       lowercase       : Union[str, Any]						=min_seq_length
       lowercase       : List[str]						=max_seq_length
       lowercase       : List[Any]						=(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
       lowercase       : Dict						=feature_size
       lowercase       : Union[str, Any]						=padding_value
       lowercase       : List[str]						=sampling_rate
       lowercase       : Tuple						=return_attention_mask
       lowercase       : Any						=do_normalize






      def 					A__   (			self      :		Optional[Any]		)	->	Any:


       '''simple docstring'''

       return {
           "feature_size": self.feature_size,
           "padding_value": self.padding_value,
           "sampling_rate": self.sampling_rate,
           "return_attention_mask": self.return_attention_mask,
           "do_normalize": self.do_normalize,
       }






      def 					A__   (			self      :		Any  ,   UpperCAmelCase      :		List[str]=False  ,   UpperCAmelCase      :		Optional[int]=False		)	->	Optional[int]:


       '''simple docstring'''

       def _flatten(UpperCAmelCase      :		List[Any]		):
        return list(itertools.chain(*UpperCAmelCase		)		)

       if equal_length:
        lowercase       : Optional[Any]						=floats_list((self.batch_size, self.max_seq_length)		)
       else:
        # make sure that inputs increase in size
        lowercase       : List[Any]						=[
            _flatten(floats_list((x, self.feature_size)		)		)
            for x in range(self.min_seq_length  ,   self.max_seq_length  ,   self.seq_length_diff		)
        ]

       if numpify:
        lowercase       : Optional[Any]						=[np.asarray(UpperCAmelCase		) for x in speech_inputs]

       return speech_inputs





class UpperCAmelCase_       (     __A					,							unittest.TestCase					):




      """simple docstring"""




      UpperCamelCase_             =      WavaVecaFeatureExtractor






      def 					A__   (			self      :		Tuple		)	->	Optional[int]:


       '''simple docstring'''

       lowercase       : Tuple						=WavaVecaFeatureExtractionTester(self		)






      def 					A__   (			self      :		Union[str, Any]  ,   UpperCAmelCase      :		str		)	->	Optional[Any]:


       '''simple docstring'''

       self.assertTrue(np.all(np.mean(UpperCAmelCase  ,   axis=0		) < 1e-3		)		)
       self.assertTrue(np.all(np.abs(np.var(UpperCAmelCase  ,   axis=0		) - 1		) < 1e-3		)		)






      def 					A__   (			self      :		Dict		)	->	Dict:


       '''simple docstring'''

       lowercase       : Any						=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
       # create three inputs of length 800, 1000, and 1200
       lowercase       : Tuple						=[floats_list((1, x)		)[0] for x in range(800  ,   1400  ,   200		)]
       lowercase       : int						=[np.asarray(UpperCAmelCase		) for speech_input in speech_inputs]

       # Test not batched input
       lowercase       : List[Any]						=feat_extract(speech_inputs[0]  ,   return_tensors='''np'''		).input_values
       lowercase       : Union[str, Any]						=feat_extract(np_speech_inputs[0]  ,   return_tensors='''np'''		).input_values
       self.assertTrue(np.allclose(UpperCAmelCase  ,   UpperCAmelCase  ,   atol=1e-3		)		)

       # Test batched
       lowercase       : Optional[Any]						=feat_extract(UpperCAmelCase  ,   return_tensors='''np'''		).input_values
       lowercase       : List[str]						=feat_extract(UpperCAmelCase  ,   return_tensors='''np'''		).input_values
       for enc_seq_a, enc_seq_a in zip(UpperCAmelCase  ,   UpperCAmelCase		):
        self.assertTrue(np.allclose(UpperCAmelCase  ,   UpperCAmelCase  ,   atol=1e-3		)		)

       # Test 2-D numpy arrays are batched.
       lowercase       : str						=[floats_list((1, x)		)[0] for x in (800, 800, 800)]
       lowercase       : List[str]						=np.asarray(UpperCAmelCase		)
       lowercase       : Union[str, Any]						=feat_extract(UpperCAmelCase  ,   return_tensors='''np'''		).input_values
       lowercase       : List[str]						=feat_extract(UpperCAmelCase  ,   return_tensors='''np'''		).input_values
       for enc_seq_a, enc_seq_a in zip(UpperCAmelCase  ,   UpperCAmelCase		):
        self.assertTrue(np.allclose(UpperCAmelCase  ,   UpperCAmelCase  ,   atol=1e-3		)		)






      def 					A__   (			self      :		Tuple		)	->	Optional[Any]:


       '''simple docstring'''

       lowercase       : Optional[int]						=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
       lowercase       : Optional[int]						=[floats_list((1, x)		)[0] for x in range(800  ,   1400  ,   200		)]

       lowercase       : Tuple						=['''longest''', '''max_length''', '''do_not_pad''']
       lowercase       : Tuple						=[None, 1600, None]
       for max_length, padding in zip(UpperCAmelCase  ,   UpperCAmelCase		):
        lowercase       : Union[str, Any]						=feat_extract(UpperCAmelCase  ,   padding=UpperCAmelCase  ,   max_length=UpperCAmelCase  ,   return_tensors='''np'''		)
        lowercase       : List[Any]						=processed.input_values

        self._check_zero_mean_unit_variance(input_values[0][:800]		)
        self.assertTrue(input_values[0][800:].sum() < 1e-6		)
        self._check_zero_mean_unit_variance(input_values[1][:1000]		)
        self.assertTrue(input_values[0][1000:].sum() < 1e-6		)
        self._check_zero_mean_unit_variance(input_values[2][:1200]		)






      def 					A__   (			self      :		Optional[int]		)	->	int:


       '''simple docstring'''

       lowercase       : List[str]						=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
       lowercase       : str						=range(800  ,   1400  ,   200		)
       lowercase       : int						=[floats_list((1, x)		)[0] for x in lengths]

       lowercase       : Optional[Any]						=['''longest''', '''max_length''', '''do_not_pad''']
       lowercase       : Dict						=[None, 1600, None]

       for max_length, padding in zip(UpperCAmelCase  ,   UpperCAmelCase		):
        lowercase       : List[Any]						=feat_extract(UpperCAmelCase  ,   max_length=UpperCAmelCase  ,   padding=UpperCAmelCase		)
        lowercase       : List[str]						=processed.input_values

        self._check_zero_mean_unit_variance(input_values[0][:800]		)
        self._check_zero_mean_unit_variance(input_values[1][:1000]		)
        self._check_zero_mean_unit_variance(input_values[2][:1200]		)






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


       '''simple docstring'''

       lowercase       : Union[str, Any]						=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
       lowercase       : str						=[floats_list((1, x)		)[0] for x in range(800  ,   1400  ,   200		)]
       lowercase       : Optional[int]						=feat_extract(
           UpperCAmelCase  ,   truncation=UpperCAmelCase  ,   max_length=1000  ,   padding='''max_length'''  ,   return_tensors='''np'''		)
       lowercase       : str						=processed.input_values

       self._check_zero_mean_unit_variance(input_values[0, :800]		)
       self._check_zero_mean_unit_variance(input_values[1]		)
       self._check_zero_mean_unit_variance(input_values[2]		)






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


       '''simple docstring'''

       lowercase       : Dict						=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
       lowercase       : Optional[Any]						=[floats_list((1, x)		)[0] for x in range(800  ,   1400  ,   200		)]
       lowercase       : str						=feat_extract(
           UpperCAmelCase  ,   truncation=UpperCAmelCase  ,   max_length=1000  ,   padding='''longest'''  ,   return_tensors='''np'''		)
       lowercase       : Optional[Any]						=processed.input_values

       self._check_zero_mean_unit_variance(input_values[0, :800]		)
       self._check_zero_mean_unit_variance(input_values[1, :1000]		)
       self._check_zero_mean_unit_variance(input_values[2]		)

       # make sure that if max_length < longest -> then pad to max_length
       self.assertTrue(input_values.shape == (3, 1000)		)

       lowercase       : str						=[floats_list((1, x)		)[0] for x in range(800  ,   1400  ,   200		)]
       lowercase       : Any						=feat_extract(
           UpperCAmelCase  ,   truncation=UpperCAmelCase  ,   max_length=2000  ,   padding='''longest'''  ,   return_tensors='''np'''		)
       lowercase       : List[str]						=processed.input_values

       self._check_zero_mean_unit_variance(input_values[0, :800]		)
       self._check_zero_mean_unit_variance(input_values[1, :1000]		)
       self._check_zero_mean_unit_variance(input_values[2]		)

       # make sure that if max_length > longest -> then pad to longest
       self.assertTrue(input_values.shape == (3, 1200)		)






      @require_torch
      def 					A__   (			self      :		Union[str, Any]		)	->	List[str]:


       '''simple docstring'''

       import torch

       lowercase       : Union[str, Any]						=self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
       lowercase       : Tuple						=np.random.rand(100		).astype(np.floataa		)
       lowercase       : Optional[Any]						=np_speech_inputs.tolist()

       for inputs in [py_speech_inputs, np_speech_inputs]:
        lowercase       : List[str]						=feature_extractor.pad([{'''input_values''': inputs}]  ,   return_tensors='''np'''		)
        self.assertTrue(np_processed.input_values.dtype == np.floataa		)
        lowercase       : List[Any]						=feature_extractor.pad([{'''input_values''': inputs}]  ,   return_tensors='''pt'''		)
        self.assertTrue(pt_processed.input_values.dtype == torch.floataa		)






      @slow
      @require_torch
      def 					A__   (			self      :		str		)	->	List[Any]:


       '''simple docstring'''

       for model_id in WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST:
        lowercase       : int						=WavaVecaConfig.from_pretrained(UpperCAmelCase		)
        lowercase       : Optional[int]						=WavaVecaFeatureExtractor.from_pretrained(UpperCAmelCase		)

        # only "layer" feature extraction norm should make use of
        # attention_mask
        self.assertEqual(feat_extract.return_attention_mask  ,   config.feat_extract_norm == '''layer'''		)







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     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE=13     ,       _SCREAMING_SNAKE_CASE=30     ,       _SCREAMING_SNAKE_CASE=2     ,       _SCREAMING_SNAKE_CASE=3     ,       _SCREAMING_SNAKE_CASE=True     ,       _SCREAMING_SNAKE_CASE=True     ,       _SCREAMING_SNAKE_CASE=32     ,       _SCREAMING_SNAKE_CASE=5     ,       _SCREAMING_SNAKE_CASE=4     ,       _SCREAMING_SNAKE_CASE=37     ,       _SCREAMING_SNAKE_CASE="gelu"     ,       _SCREAMING_SNAKE_CASE=0.1     ,       _SCREAMING_SNAKE_CASE=0.1     ,       _SCREAMING_SNAKE_CASE=10     ,       _SCREAMING_SNAKE_CASE=0.02     ,       )					->			int:




									"""simple docstring"""





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

									# in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
									_UpperCAmelCase											=     (image_size // patch_size) ** 2
									_UpperCAmelCase											=     num_patches + 1
		def       UpperCAmelCase__							(     self		)					->			Optional[int]:




									"""simple docstring"""





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

									_UpperCAmelCase											=     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=_SCREAMING_SNAKE_CASE     ,       initializer_range=self.initializer_range     ,       )

									return config, pixel_values
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)					->			str:




									"""simple docstring"""





									_UpperCAmelCase											=     FlaxViTModel(config=_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     model(_SCREAMING_SNAKE_CASE		)
									# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
									_UpperCAmelCase											=     (self.image_size, self.image_size)
									_UpperCAmelCase											=     (self.patch_size, self.patch_size)
									_UpperCAmelCase											=     (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     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)					->			Any:




									"""simple docstring"""





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

									# test greyscale images
									_UpperCAmelCase											=     1
									_UpperCAmelCase											=     FlaxViTForImageClassification(_SCREAMING_SNAKE_CASE		)

									_UpperCAmelCase											=     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
									_UpperCAmelCase											=     model(_SCREAMING_SNAKE_CASE		)






		def       UpperCAmelCase__							(     self		)					->			str:




									"""simple docstring"""





									_UpperCAmelCase											=     self.prepare_config_and_inputs()
									(
									    (
									    _UpperCAmelCase
									)						,			(
									    _UpperCAmelCase
									)						,			
									)											=     config_and_inputs
									_UpperCAmelCase											=     {'pixel_values': pixel_values}
									return config, inputs_dict



@require_flax
class 						__a (							UpperCAmelCase       , unittest.TestCase     ):
		_a      :    List[str]           =      (FlaxViTModel, FlaxViTForImageClassification) if is_flax_available() else ()
		def       UpperCAmelCase__							(     self		)					->			None:




									"""simple docstring"""





									_UpperCAmelCase											=     FlaxViTModelTester(self		)
									_UpperCAmelCase											=     ConfigTester(self     ,       config_class=_SCREAMING_SNAKE_CASE     ,       has_text_modality=_SCREAMING_SNAKE_CASE     ,       hidden_size=37		)
		def       UpperCAmelCase__							(     self		)					->			Dict:




									"""simple docstring"""





									self.config_tester.run_common_tests()
		def       UpperCAmelCase__							(     self		)					->			Optional[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			List[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			str:




									"""simple docstring"""





									_UpperCAmelCase						,			_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs_for_common()

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

																_UpperCAmelCase											=     ['pixel_values']
																self.assertListEqual(arg_names[:1]     ,       _SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			int:




									"""simple docstring"""





									_UpperCAmelCase						,			_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
																with self.subTest(model_class.__name__		):
																							_UpperCAmelCase											=     self._prepare_for_class(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)
																							_UpperCAmelCase											=     model_class(_SCREAMING_SNAKE_CASE		)

																							@jax.jit
																							def model_jitted(_SCREAMING_SNAKE_CASE     ,       **_SCREAMING_SNAKE_CASE		):
																														return model(pixel_values=_SCREAMING_SNAKE_CASE     ,       **_SCREAMING_SNAKE_CASE		)

																							with self.subTest('JIT Enabled'		):
																														_UpperCAmelCase											=     model_jitted(**_SCREAMING_SNAKE_CASE		).to_tuple()

																							with self.subTest('JIT Disabled'		):
																														with jax.disable_jit():
																																					_UpperCAmelCase											=     model_jitted(**_SCREAMING_SNAKE_CASE		).to_tuple()

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






		@slow
		def       UpperCAmelCase__							(     self		)					->			Dict:




									"""simple docstring"""





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