


from __future__ import annotations

import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
						import tensorflow as tf

						from transformers import TFViTForImageClassification, TFViTModel


if is_vision_available():
						from PIL import Image

						from transformers import ViTImageProcessor
class 			_lowercase :






				'''simple docstring'''



				def __init__(			self     :Optional[int]     ,     lowerCAmelCase__     :Union[str, Any]     ,     lowerCAmelCase__     :Dict=13     ,     lowerCAmelCase__     :List[str]=30     ,     lowerCAmelCase__     :Any=2     ,     lowerCAmelCase__     :List[Any]=3     ,     lowerCAmelCase__     :List[str]=True     ,     lowerCAmelCase__     :Optional[int]=True     ,     lowerCAmelCase__     :Dict=32     ,     lowerCAmelCase__     :Dict=2     ,     lowerCAmelCase__     :Any=4     ,     lowerCAmelCase__     :Any=37     ,     lowerCAmelCase__     :Optional[int]="gelu"     ,     lowerCAmelCase__     :Optional[Any]=0.1     ,     lowerCAmelCase__     :str=0.1     ,     lowerCAmelCase__     :str=10     ,     lowerCAmelCase__     :Tuple=0.02     ,     lowerCAmelCase__     :Tuple=3     ,     lowerCAmelCase__     :Optional[Any]=None     ,     )						->     Optional[int]:
										__SCREAMING_SNAKE_CASE		:	Optional[int]						     =						parent
										__SCREAMING_SNAKE_CASE		:	Optional[Any]						     =						batch_size
										__SCREAMING_SNAKE_CASE		:	int						     =						image_size
										__SCREAMING_SNAKE_CASE		:	Union[str, Any]						     =						patch_size
										__SCREAMING_SNAKE_CASE		:	Optional[Any]						     =						num_channels
										__SCREAMING_SNAKE_CASE		:	Union[str, Any]						     =						is_training
										__SCREAMING_SNAKE_CASE		:	int						     =						use_labels
										__SCREAMING_SNAKE_CASE		:	str						     =						hidden_size
										__SCREAMING_SNAKE_CASE		:	List[Any]						     =						num_hidden_layers
										__SCREAMING_SNAKE_CASE		:	Union[str, Any]						     =						num_attention_heads
										__SCREAMING_SNAKE_CASE		:	str						     =						intermediate_size
										__SCREAMING_SNAKE_CASE		:	int						     =						hidden_act
										__SCREAMING_SNAKE_CASE		:	Optional[int]						     =						hidden_dropout_prob
										__SCREAMING_SNAKE_CASE		:	List[str]						     =						attention_probs_dropout_prob
										__SCREAMING_SNAKE_CASE		:	Union[str, Any]						     =						type_sequence_label_size
										__SCREAMING_SNAKE_CASE		:	List[Any]						     =						initializer_range
										__SCREAMING_SNAKE_CASE		:	Any						     =						scope

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


				def 						__magic_name__(			self     :Any      )						->     List[Any]:
										__SCREAMING_SNAKE_CASE		:	Dict						     =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

										__SCREAMING_SNAKE_CASE		:	Tuple						     =						None
										if self.use_labels:
																__SCREAMING_SNAKE_CASE		:	int						     =						ids_tensor([self.batch_size]     ,     self.type_sequence_label_size      )

										__SCREAMING_SNAKE_CASE		:	Union[str, Any]						     =						self.get_config()

										return config, pixel_values, labels


				def 						__magic_name__(			self     :Optional[int]      )						->     int:
										return 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=__a     ,     initializer_range=self.initializer_range     ,     )


				def 						__magic_name__(			self     :Tuple     ,     lowerCAmelCase__     :int     ,     lowerCAmelCase__     :int     ,     lowerCAmelCase__     :Union[str, Any]      )						->     Tuple:
										__SCREAMING_SNAKE_CASE		:	Union[str, Any]						     =						TFViTModel(config=__a      )
										__SCREAMING_SNAKE_CASE		:	Optional[int]						     =						model(__a     ,     training=__a      )
										self.parent.assertEqual(result.last_hidden_state.shape     ,     (self.batch_size, self.seq_length, self.hidden_size)      )

										# Test with an image with different size than the one specified in config.
										__SCREAMING_SNAKE_CASE		:	Optional[int]						     =						self.image_size // 2
										__SCREAMING_SNAKE_CASE		:	int						     =						pixel_values[:, :, :image_size, :image_size]
										__SCREAMING_SNAKE_CASE		:	Tuple						     =						model(__a     ,     interpolate_pos_encoding=__a     ,     training=__a      )
										__SCREAMING_SNAKE_CASE		:	List[Any]						     =						(image_size // self.patch_size) ** 2 + 1
										self.parent.assertEqual(result.last_hidden_state.shape     ,     (self.batch_size, seq_length, self.hidden_size)      )


				def 						__magic_name__(			self     :Tuple     ,     lowerCAmelCase__     :Any     ,     lowerCAmelCase__     :Optional[Any]     ,     lowerCAmelCase__     :Tuple      )						->     Tuple:
										__SCREAMING_SNAKE_CASE		:	List[Any]						     =						self.type_sequence_label_size
										__SCREAMING_SNAKE_CASE		:	Tuple						     =						TFViTForImageClassification(__a      )
										__SCREAMING_SNAKE_CASE		:	Optional[Any]						     =						model(__a     ,     labels=__a     ,     training=__a      )
										self.parent.assertEqual(result.logits.shape     ,     (self.batch_size, self.type_sequence_label_size)      )

										# Test with an image with different size than the one specified in config.
										__SCREAMING_SNAKE_CASE		:	str						     =						self.image_size // 2
										__SCREAMING_SNAKE_CASE		:	Optional[int]						     =						pixel_values[:, :, :image_size, :image_size]
										__SCREAMING_SNAKE_CASE		:	Any						     =						model(__a     ,     interpolate_pos_encoding=__a     ,     training=__a      )
										self.parent.assertEqual(result.logits.shape     ,     (self.batch_size, self.type_sequence_label_size)      )

										# test greyscale images
										__SCREAMING_SNAKE_CASE		:	Optional[int]						     =						1
										__SCREAMING_SNAKE_CASE		:	Optional[Any]						     =						TFViTForImageClassification(__a      )
										__SCREAMING_SNAKE_CASE		:	Optional[int]						     =						floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
										__SCREAMING_SNAKE_CASE		:	Optional[Any]						     =						model(__a      )
										self.parent.assertEqual(result.logits.shape     ,     (self.batch_size, self.type_sequence_label_size)      )



				def 						__magic_name__(			self     :Any      )						->     Tuple:
										__SCREAMING_SNAKE_CASE		:	Any						     =						self.prepare_config_and_inputs()
										__SCREAMING_SNAKE_CASE     ,     __SCREAMING_SNAKE_CASE     ,     __SCREAMING_SNAKE_CASE		:	Dict						     =						config_and_inputs
										__SCREAMING_SNAKE_CASE		:	str						     =						{'''pixel_values''': pixel_values}
										return config, inputs_dict




@require_tf
class 			_lowercase (     UpperCAmelCase_		,     UpperCAmelCase_		,     unittest.TestCase			):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__							:     Tuple         =						(TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
				SCREAMING_SNAKE_CASE__							:     int         =						(
				    {'''feature-extraction''': TFViTModel, '''image-classification''': TFViTForImageClassification}
				    if is_tf_available()
				    else {}
				)

				SCREAMING_SNAKE_CASE__							:     Union[str, Any]         =						False
				SCREAMING_SNAKE_CASE__							:     str         =						False
				SCREAMING_SNAKE_CASE__							:     str         =						False


				def 						__magic_name__(			self     :Optional[int]      )						->     Dict:
										__SCREAMING_SNAKE_CASE		:	str						     =						TFViTModelTester(self      )
										__SCREAMING_SNAKE_CASE		:	Optional[int]						     =						ConfigTester(self     ,     config_class=__a     ,     has_text_modality=__a     ,     hidden_size=37      )


				def 						__magic_name__(			self     :Dict      )						->     Dict:
										self.config_tester.run_common_tests()


				@unittest.skip(reason='''ViT does not use inputs_embeds'''      )
				def 						__magic_name__(			self     :List[Any]      )						->     Tuple:
										pass


				@unittest.skip(reason='''ViT does not use inputs_embeds'''      )
				def 						__magic_name__(			self     :Any      )						->     Union[str, Any]:
										pass


				def 						__magic_name__(			self     :List[str]      )						->     Optional[Any]:
										__SCREAMING_SNAKE_CASE     ,     __SCREAMING_SNAKE_CASE		:	Tuple						     =						self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																__SCREAMING_SNAKE_CASE		:	Dict						     =						model_class(__a      )
																self.assertIsInstance(model.get_input_embeddings()     ,     (tf.keras.layers.Layer)      )
																__SCREAMING_SNAKE_CASE		:	Optional[Any]						     =						model.get_output_embeddings()
																self.assertTrue(x is None or isinstance(__a     ,     tf.keras.layers.Layer      )      )


				def 						__magic_name__(			self     :Dict      )						->     List[Any]:
										__SCREAMING_SNAKE_CASE     ,     __SCREAMING_SNAKE_CASE		:	int						     =						self.model_tester.prepare_config_and_inputs_for_common()

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

																__SCREAMING_SNAKE_CASE		:	Union[str, Any]						     =						['''pixel_values''']
																self.assertListEqual(arg_names[:1]     ,     __a      )


				def 						__magic_name__(			self     :Optional[Any]      )						->     Any:
										__SCREAMING_SNAKE_CASE		:	Tuple						     =						self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*__a      )


				def 						__magic_name__(			self     :Tuple      )						->     List[str]:
										__SCREAMING_SNAKE_CASE		:	Optional[int]						     =						self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*__a      )



				@slow
				def 						__magic_name__(			self     :Tuple      )						->     List[str]:
										__SCREAMING_SNAKE_CASE		:	int						     =						TFViTModel.from_pretrained('''google/vit-base-patch16-224'''      )
										self.assertIsNotNone(__a      )




def 			_UpperCamelCase				( ):
						__SCREAMING_SNAKE_CASE		:	int						     =						Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
						return image
@require_tf
@require_vision
class 			_lowercase (     unittest.TestCase			):






				'''simple docstring'''



				@cached_property
				def 						__magic_name__(			self     :List[str]      )						->     Optional[int]:
										return ViTImageProcessor.from_pretrained('''google/vit-base-patch16-224'''      ) if is_vision_available() else None



				@slow
				def 						__magic_name__(			self     :Dict      )						->     int:
										__SCREAMING_SNAKE_CASE		:	Optional[int]						     =						TFViTForImageClassification.from_pretrained('''google/vit-base-patch16-224'''      )

										__SCREAMING_SNAKE_CASE		:	List[Any]						     =						self.default_image_processor
										__SCREAMING_SNAKE_CASE		:	Tuple						     =						prepare_img()
										__SCREAMING_SNAKE_CASE		:	Dict						     =						image_processor(images=__a     ,     return_tensors='''tf'''      )

										# forward pass
										__SCREAMING_SNAKE_CASE		:	str						     =						model(**__a      )

										# verify the logits
										__SCREAMING_SNAKE_CASE		:	Tuple						     =						tf.TensorShape((1, 1_000)      )
										self.assertEqual(outputs.logits.shape     ,     __a      )

										__SCREAMING_SNAKE_CASE		:	List[str]						     =						tf.constant([-0.2744, 0.8215, -0.0836]      )

										tf.debugging.assert_near(outputs.logits[0, :3]     ,     __a     ,     atol=1E-4      )





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_torch_available,
)


a_    :  Tuple						  =		{'configuration_unispeech': ['UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP', 'UniSpeechConfig']}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     a_    :  Union[str, Any]						  =		[
         'UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST',
         'UniSpeechForCTC',
         'UniSpeechForPreTraining',
         'UniSpeechForSequenceClassification',
         'UniSpeechModel',
         'UniSpeechPreTrainedModel',
     ]

if TYPE_CHECKING:
     from .configuration_unispeech import UNISPEECH_PRETRAINED_CONFIG_ARCHIVE_MAP, UniSpeechConfig

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_unispeech import (
              UNISPEECH_PRETRAINED_MODEL_ARCHIVE_LIST,
              UniSpeechForCTC,
              UniSpeechForPreTraining,
              UniSpeechForSequenceClassification,
              UniSpeechModel,
              UniSpeechPreTrainedModel,
          )

else:
     import sys

     a_    :  Optional[Any]						  =		_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)