


'''simple docstring'''






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 						__UpperCamelCase	:





							def __init__(  self		:Tuple  ,_UpperCamelCase		:Optional[Any]  ,_UpperCamelCase		:str=1_3  ,_UpperCamelCase		:int=3_0  ,_UpperCamelCase		:Any=2  ,_UpperCamelCase		:Any=3  ,_UpperCamelCase		:List[str]=True  ,_UpperCamelCase		:List[Any]=True  ,_UpperCamelCase		:Dict=3_2  ,_UpperCamelCase		:Tuple=2  ,_UpperCamelCase		:Any=4  ,_UpperCamelCase		:Any=3_7  ,_UpperCamelCase		:List[str]="gelu"  ,_UpperCamelCase		:str=0.1  ,_UpperCamelCase		:int=0.1  ,_UpperCamelCase		:Dict=1_0  ,_UpperCamelCase		:Optional[int]=0.02  ,_UpperCamelCase		:List[str]=3  ,_UpperCamelCase		:int=None  ,):
									snake_case_		:							Dict			       =						parent
									snake_case_		:							List[str]			       =						batch_size
									snake_case_		:							Any			       =						image_size
									snake_case_		:							Any			       =						patch_size
									snake_case_		:							str			       =						num_channels
									snake_case_		:							Dict			       =						is_training
									snake_case_		:							List[Any]			       =						use_labels
									snake_case_		:							List[Any]			       =						hidden_size
									snake_case_		:							List[Any]			       =						num_hidden_layers
									snake_case_		:							List[Any]			       =						num_attention_heads
									snake_case_		:							str			       =						intermediate_size
									snake_case_		:							Union[str, Any]			       =						hidden_act
									snake_case_		:							Tuple			       =						hidden_dropout_prob
									snake_case_		:							Optional[Any]			       =						attention_probs_dropout_prob
									snake_case_		:							Union[str, Any]			       =						type_sequence_label_size
									snake_case_		:							Dict			       =						initializer_range
									snake_case_		:							List[Any]			       =						scope

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





							def 		a__   (  self		:List[str]			):
									snake_case_		:							str			       =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

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

									snake_case_		:							Tuple			       =						self.get_config()

									return config, pixel_values, labels





							def 		a__   (  self		:str			):
									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=_UpperCamelCase  ,initializer_range=self.initializer_range  ,)





							def 		a__   (  self		:Any  ,_UpperCamelCase		:Optional[Any]  ,_UpperCamelCase		:Dict  ,_UpperCamelCase		:Tuple			):
									snake_case_		:							Optional[int]			       =						TFViTModel(config=_UpperCamelCase			)
									snake_case_		:							Union[str, Any]			       =						model(_UpperCamelCase  ,training=_UpperCamelCase			)
									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.
									snake_case_		:							Dict			       =						self.image_size // 2
									snake_case_		:							Any			       =						pixel_values[:, :, :image_size, :image_size]
									snake_case_		:							List[str]			       =						model(_UpperCamelCase  ,interpolate_pos_encoding=_UpperCamelCase  ,training=_UpperCamelCase			)
									snake_case_		:							Tuple			       =						(image_size // self.patch_size) ** 2 + 1
									self.parent.assertEqual(result.last_hidden_state.shape  ,(self.batch_size, seq_length, self.hidden_size)			)





							def 		a__   (  self		:Union[str, Any]  ,_UpperCamelCase		:List[str]  ,_UpperCamelCase		:Optional[int]  ,_UpperCamelCase		:Tuple			):
									snake_case_		:							Optional[Any]			       =						self.type_sequence_label_size
									snake_case_		:							List[str]			       =						TFViTForImageClassification(_UpperCamelCase			)
									snake_case_		:							Tuple			       =						model(_UpperCamelCase  ,labels=_UpperCamelCase  ,training=_UpperCamelCase			)
									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.
									snake_case_		:							Optional[int]			       =						self.image_size // 2
									snake_case_		:							List[Any]			       =						pixel_values[:, :, :image_size, :image_size]
									snake_case_		:							List[Any]			       =						model(_UpperCamelCase  ,interpolate_pos_encoding=_UpperCamelCase  ,training=_UpperCamelCase			)
									self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.type_sequence_label_size)			)

									# test greyscale images
									snake_case_		:							Optional[Any]			       =						1
									snake_case_		:							Dict			       =						TFViTForImageClassification(_UpperCamelCase			)
									snake_case_		:							Optional[Any]			       =						floats_tensor([self.batch_size, 1, self.image_size, self.image_size]			)
									snake_case_		:							List[Any]			       =						model(_UpperCamelCase			)
									self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.type_sequence_label_size)			)





							def 		a__   (  self		:Optional[int]			):
									snake_case_		:							int			       =						self.prepare_config_and_inputs()
									snake_case_		:							Optional[Any]			       =						config_and_inputs
									snake_case_		:							List[Any]			       =						{"""pixel_values""": pixel_values}
									return config, inputs_dict





@require_tf
class 						__UpperCamelCase	(						lowercase__						,  lowercase__						,  unittest.TestCase							):
							lowercase			:		Dict   		=  (TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
							lowercase			:		Union[str, Any]   		=  (
							    {'feature-extraction': TFViTModel, 'image-classification': TFViTForImageClassification}
							    if is_tf_available()
							    else {}
							)

							lowercase			:		Optional[int]   		=  False
							lowercase			:		Tuple   		=  False
							lowercase			:		Union[str, Any]   		=  False





							def 		a__   (  self		:List[Any]			):
									snake_case_		:							Union[str, Any]			       =						TFViTModelTester(self			)
									snake_case_		:							List[Any]			       =						ConfigTester(self  ,config_class=_UpperCamelCase  ,has_text_modality=_UpperCamelCase  ,hidden_size=3_7			)





							def 		a__   (  self		:Union[str, Any]			):
									self.config_tester.run_common_tests()





							@unittest.skip(reason="""ViT does not use inputs_embeds"""			)
							def 		a__   (  self		:List[str]			):
									pass





							@unittest.skip(reason="""ViT does not use inputs_embeds"""			)
							def 		a__   (  self		:str			):
									pass





							def 		a__   (  self		:Union[str, Any]			):
									snake_case_		:							Union[str, Any]			       =						self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
											snake_case_		:							int			       =						model_class(_UpperCamelCase			)
											self.assertIsInstance(model.get_input_embeddings()  ,(tf.keras.layers.Layer)			)
											snake_case_		:							Union[str, Any]			       =						model.get_output_embeddings()
											self.assertTrue(x is None or isinstance(_UpperCamelCase  ,tf.keras.layers.Layer			)			)





							def 		a__   (  self		:List[Any]			):
									snake_case_		:							List[Any]			       =						self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
											snake_case_		:							Union[str, Any]			       =						model_class(_UpperCamelCase			)
											snake_case_		:							str			       =						inspect.signature(model.call			)
											# signature.parameters is an OrderedDict => so arg_names order is deterministic
											snake_case_		:							List[str]			       =						[*signature.parameters.keys()]

											snake_case_		:							List[str]			       =						["""pixel_values"""]
											self.assertListEqual(arg_names[:1]  ,_UpperCamelCase			)





							def 		a__   (  self		:Tuple			):
									snake_case_		:							Optional[int]			       =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_UpperCamelCase			)





							def 		a__   (  self		:int			):
									snake_case_		:							Dict			       =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase			)





							@slow
							def 		a__   (  self		:Union[str, Any]			):
									snake_case_		:							Tuple			       =						TFViTModel.from_pretrained("""google/vit-base-patch16-224"""			)
									self.assertIsNotNone(_UpperCamelCase			)





def      UpperCAmelCase      (		):







		'''simple docstring'''
		snake_case_		:							Dict			       =						Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" )
		return image






@require_tf
@require_vision
class 						__UpperCamelCase	(						unittest.TestCase							):





							@cached_property
							def 		a__   (  self		:Optional[Any]			):
									return ViTImageProcessor.from_pretrained("""google/vit-base-patch16-224"""			) if is_vision_available() else None





							@slow
							def 		a__   (  self		:Tuple			):
									snake_case_		:							Dict			       =						TFViTForImageClassification.from_pretrained("""google/vit-base-patch16-224"""			)

									snake_case_		:							Optional[Any]			       =						self.default_image_processor
									snake_case_		:							Union[str, Any]			       =						prepare_img()
									snake_case_		:							int			       =						image_processor(images=_UpperCamelCase  ,return_tensors="""tf"""			)

									# forward pass
									snake_case_		:							str			       =						model(**_UpperCamelCase			)

									# verify the logits
									snake_case_		:							Union[str, Any]			       =						tf.TensorShape((1, 1_0_0_0)			)
									self.assertEqual(outputs.logits.shape  ,_UpperCamelCase			)

									snake_case_		:							str			       =						tf.constant([-0.27_44, 0.82_15, -0.08_36]			)

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



'''simple docstring'''






from abc import ABC, abstractmethod
from argparse import ArgumentParser
class 						__UpperCamelCase	(						lowercase__							):





							@staticmethod
							@abstractmethod
							def 		a__   (  _UpperCamelCase		:ArgumentParser			):
									raise NotImplementedError()





							@abstractmethod
							def 		a__   (  self		:Optional[Any]			):
									raise NotImplementedError()