





'''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 			lowerCamelCase					:


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





											'''simple docstring'''





											_snake_case:   Optional[Any]  					=		parent
											_snake_case:   Any  					=		batch_size
											_snake_case:   List[str]  					=		image_size
											_snake_case:   Dict  					=		patch_size
											_snake_case:   Union[str, Any]  					=		num_channels
											_snake_case:   Tuple  					=		is_training
											_snake_case:   Dict  					=		use_labels
											_snake_case:   Union[str, Any]  					=		hidden_size
											_snake_case:   Optional[int]  					=		num_hidden_layers
											_snake_case:   Optional[int]  					=		num_attention_heads
											_snake_case:   Optional[int]  					=		intermediate_size
											_snake_case:   Dict  					=		hidden_act
											_snake_case:   List[Any]  					=		hidden_dropout_prob
											_snake_case:   Any  					=		attention_probs_dropout_prob
											_snake_case:   Tuple  					=		type_sequence_label_size
											_snake_case:   Optional[Any]  					=		initializer_range
											_snake_case:   Union[str, Any]  					=		scope

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


					def    SCREAMING_SNAKE_CASE_		(					self	:  Tuple       ):





											'''simple docstring'''





											_snake_case:   Union[str, Any]  					=		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

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

											_snake_case:   str  					=		self.get_config()

											return config, pixel_values, labels


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





											'''simple docstring'''





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


					def    SCREAMING_SNAKE_CASE_		(					self	:  int       ,     __snake_case	:  Union[str, Any]       ,     __snake_case	:  Any       ,     __snake_case	:  Any       ):





											'''simple docstring'''





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


					def    SCREAMING_SNAKE_CASE_		(					self	:  int       ,     __snake_case	:  str       ,     __snake_case	:  int       ,     __snake_case	:  Optional[int]       ):





											'''simple docstring'''





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

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




					def    SCREAMING_SNAKE_CASE_		(					self	:  Optional[int]       ):





											'''simple docstring'''





											_snake_case:   List[Any]  					=		self.prepare_config_and_inputs()
											_snake_case:   List[str]  					=		config_and_inputs
											_snake_case:   int  					=		{'pixel_values': pixel_values}
											return config, inputs_dict







@require_tf
class 			lowerCamelCase					(						__UpperCAmelCase     ,							__UpperCAmelCase     ,							unittest.TestCase  ):
					_SCREAMING_SNAKE_CASE							   =    (TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
					_SCREAMING_SNAKE_CASE							   =    (
					    {"feature-extraction": TFViTModel, "image-classification": TFViTForImageClassification}
					    if is_tf_available()
					    else {}
					)

					_SCREAMING_SNAKE_CASE							   =    False
					_SCREAMING_SNAKE_CASE							   =    False
					_SCREAMING_SNAKE_CASE							   =    False


					def    SCREAMING_SNAKE_CASE_		(					self	:  Tuple       ):





											'''simple docstring'''





											_snake_case:   Tuple  					=		TFViTModelTester(self       )
											_snake_case:   Dict  					=		ConfigTester(self       ,     config_class=__snake_case       ,     has_text_modality=__snake_case       ,     hidden_size=37       )


					def    SCREAMING_SNAKE_CASE_		(					self	:  Optional[Any]       ):





											'''simple docstring'''





											self.config_tester.run_common_tests()


					@unittest.skip(reason='ViT does not use inputs_embeds'       )
					def    SCREAMING_SNAKE_CASE_		(					self	:  int       ):





											'''simple docstring'''





											pass


					@unittest.skip(reason='ViT does not use inputs_embeds'       )
					def    SCREAMING_SNAKE_CASE_		(					self	:  Tuple       ):





											'''simple docstring'''





											pass


					def    SCREAMING_SNAKE_CASE_		(					self	:  Tuple       ):





											'''simple docstring'''





											_snake_case:   Any  					=		self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																	_snake_case:   List[str]  					=		model_class(__snake_case       )
																	self.assertIsInstance(model.get_input_embeddings()       ,     (tf.keras.layers.Layer)       )
																	_snake_case:   Tuple  					=		model.get_output_embeddings()
																	self.assertTrue(x is None or isinstance(__snake_case       ,     tf.keras.layers.Layer       )       )


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





											'''simple docstring'''





											_snake_case:   List[str]  					=		self.model_tester.prepare_config_and_inputs_for_common()

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

																	_snake_case:   Any  					=		['pixel_values']
																	self.assertListEqual(arg_names[:1]       ,     __snake_case       )


					def    SCREAMING_SNAKE_CASE_		(					self	:  int       ):





											'''simple docstring'''





											_snake_case:   List[str]  					=		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__snake_case       )


					def    SCREAMING_SNAKE_CASE_		(					self	:  Optional[int]       ):





											'''simple docstring'''





											_snake_case:   Optional[int]  					=		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*__snake_case       )




					@slow
					def    SCREAMING_SNAKE_CASE_		(					self	:  Dict       ):





											'''simple docstring'''





											_snake_case:   Dict  					=		TFViTModel.from_pretrained('google/vit-base-patch16-224'       )
											self.assertIsNotNone(__snake_case       )





def       lowercase_      (		):
						_snake_case:   Union[str, Any]  					=		Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'						)
						return image







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


					@cached_property
					def    SCREAMING_SNAKE_CASE_		(					self	:  str       ):





											'''simple docstring'''





											return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224'       ) if is_vision_available() else None




					@slow
					def    SCREAMING_SNAKE_CASE_		(					self	:  int       ):





											'''simple docstring'''





											_snake_case:   List[str]  					=		TFViTForImageClassification.from_pretrained('google/vit-base-patch16-224'       )

											_snake_case:   str  					=		self.default_image_processor
											_snake_case:   Optional[Any]  					=		prepare_img()
											_snake_case:   Any  					=		image_processor(images=__snake_case       ,     return_tensors='tf'       )

											# forward pass
											_snake_case:   Optional[Any]  					=		model(**__snake_case       )

											# verify the logits
											_snake_case:   str  					=		tf.TensorShape((1, 10_00)       )
											self.assertEqual(outputs.logits.shape       ,     __snake_case       )

											_snake_case:   Dict  					=		tf.constant([-0.2_744, 0.8_215, -0.0_836]       )

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












'''simple docstring'''






def       lowercase_      (		lowercase__ = 1				,  lowercase__ = 1000						)   ->int:
						_snake_case:   int  					=		1
						_snake_case:   Union[str, Any]  					=		0

						for divide_by_number in range(lowercase__				,  digit + 1						):
												_snake_case:   list[int]  					=		[]
												_snake_case:   int  					=		numerator
												for _ in range(1				,  digit + 1						):
																		if now_divide in has_been_divided:
																								if longest_list_length < len(lowercase__						):
																														_snake_case:   Union[str, Any]  					=		len(lowercase__						)
																														_snake_case:   Tuple  					=		divide_by_number
																		else:
																								has_been_divided.append(lowercase__						)
																								_snake_case:   Union[str, Any]  					=		now_divide * 10 % divide_by_number

						return the_digit


# Tests
if __name__ == "__main__":
		import doctest

		doctest.testmod()





