



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


			'''simple docstring'''

			def __init__(							self     : Union[str, Any]							,     lowerCAmelCase__     : Tuple							,     lowerCAmelCase__     : Optional[Any]=1_3							,     lowerCAmelCase__     : Dict=3_0							,     lowerCAmelCase__     : str=2							,     lowerCAmelCase__     : Optional[int]=3							,     lowerCAmelCase__     : Optional[Any]=True							,     lowerCAmelCase__     : List[str]=True							,     lowerCAmelCase__     : str=3_2							,     lowerCAmelCase__     : List[Any]=2							,     lowerCAmelCase__     : Any=4							,     lowerCAmelCase__     : int=3_7							,     lowerCAmelCase__     : Any="gelu"							,     lowerCAmelCase__     : Dict=0.1							,     lowerCAmelCase__     : Optional[int]=0.1							,     lowerCAmelCase__     : List[Any]=1_0							,     lowerCAmelCase__     : List[str]=0.02							,     lowerCAmelCase__     : List[str]=3							,     lowerCAmelCase__     : Dict=None							,     ):





					"""simple docstring"""






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

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

			def UpperCamelCase__					(							self     : Dict						):





					"""simple docstring"""






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

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

					__SCREAMING_SNAKE_CASE       :		Tuple           =		self.get_config()

					return config, pixel_values, labels

			def UpperCamelCase__					(							self     : Dict						):





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

			def UpperCamelCase__					(							self     : Optional[int]							,     lowerCAmelCase__     : Any							,     lowerCAmelCase__     : Any							,     lowerCAmelCase__     : Optional[int]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		TFViTModel(config=lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE       :		Any           =		model(lowerCAmelCase__							,     training=lowerCAmelCase__						)
					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       :		List[str]           =		self.image_size // 2
					__SCREAMING_SNAKE_CASE       :		Any           =		pixel_values[:, :, :image_size, :image_size]
					__SCREAMING_SNAKE_CASE       :		List[Any]           =		model(lowerCAmelCase__							,     interpolate_pos_encoding=lowerCAmelCase__							,     training=lowerCAmelCase__						)
					__SCREAMING_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 UpperCamelCase__					(							self     : int							,     lowerCAmelCase__     : Dict							,     lowerCAmelCase__     : int							,     lowerCAmelCase__     : Any						):





					"""simple docstring"""






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

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



			def UpperCamelCase__					(							self     : int						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		List[str]           =		self.prepare_config_and_inputs()
					__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       :		Tuple           =		config_and_inputs
					__SCREAMING_SNAKE_CASE       :		Any           =		{"""pixel_values""": pixel_values}
					return config, inputs_dict






@require_tf
class 						_UpperCamelCase  (							lowerCamelCase__    ,					lowerCamelCase__    ,					unittest.TestCase    ):


			'''simple docstring'''

			_A      :		str			    =		(TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
			_A      :		Optional[int]			    =		(
			    {'''feature-extraction''': TFViTModel, '''image-classification''': TFViTForImageClassification}
			    if is_tf_available()
			    else {}
			)

			_A      :		Optional[int]			    =		False
			_A      :		Union[str, Any]			    =		False
			_A      :		List[str]			    =		False

			def UpperCamelCase__					(							self     : Dict						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Optional[int]           =		TFViTModelTester(self						)
					__SCREAMING_SNAKE_CASE       :		Tuple           =		ConfigTester(self							,     config_class=lowerCAmelCase__							,     has_text_modality=lowerCAmelCase__							,     hidden_size=3_7						)

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





					"""simple docstring"""






					self.config_tester.run_common_tests()

			@unittest.skip(reason="""ViT does not use inputs_embeds"""						)
			def UpperCamelCase__					(							self     : Any						):





					"""simple docstring"""






					pass

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





					"""simple docstring"""






					pass

			def UpperCamelCase__					(							self     : Tuple						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       :		str           =		self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
							__SCREAMING_SNAKE_CASE       :		Dict           =		model_class(lowerCAmelCase__						)
							self.assertIsInstance(model.get_input_embeddings()							,     (tf.keras.layers.Layer)						)
							__SCREAMING_SNAKE_CASE       :		str           =		model.get_output_embeddings()
							self.assertTrue(x is None or isinstance(lowerCAmelCase__							,     tf.keras.layers.Layer						)						)

			def UpperCamelCase__					(							self     : Dict						):





					"""simple docstring"""






					__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       :		int           =		model_class(lowerCAmelCase__						)
							__SCREAMING_SNAKE_CASE       :		Any           =		inspect.signature(model.call						)
							# signature.parameters is an OrderedDict => so arg_names order is deterministic
							__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		[*signature.parameters.keys()]

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

			def UpperCamelCase__					(							self     : Dict						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		str           =		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*lowerCAmelCase__						)

			def UpperCamelCase__					(							self     : Optional[int]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		List[Any]           =		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__						)



			@slow
			def UpperCamelCase__					(							self     : Union[str, Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		TFViTModel.from_pretrained("""google/vit-base-patch16-224"""						)
					self.assertIsNotNone(lowerCAmelCase__						)


def 				lowerCAmelCase_   (	):
		__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""					)
		return image






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


			'''simple docstring'''

			@cached_property
			def UpperCamelCase__					(							self     : Tuple						):





					"""simple docstring"""






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



			@slow
			def UpperCamelCase__					(							self     : List[Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		TFViTForImageClassification.from_pretrained("""google/vit-base-patch16-224"""						)

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

					# forward pass
					__SCREAMING_SNAKE_CASE       :		Any           =		model(**lowerCAmelCase__						)

					# verify the logits
					__SCREAMING_SNAKE_CASE       :		str           =		tf.TensorShape((1, 1_0_0_0)						)
					self.assertEqual(outputs.logits.shape							,     lowerCAmelCase__						)

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

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




'''simple docstring'''




from math import ceil, sqrt


def 				lowerCAmelCase_   (	_lowerCamelCase:   int = 1_00_00_00					):
		__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		0

		for outer_width in range(3       ,      (limit // 4) + 2					):
				if outer_width**2 > limit:
						__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		max(ceil(sqrt(outer_width**2 - limit					)					)       ,      1					)
				else:
						__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		1
				if (outer_width - hole_width_lower_bound) % 2:
						hole_width_lower_bound += 1

				answer += (outer_width - hole_width_lower_bound - 2) // 2 + 1

		return answer


if __name__ == "__main__":
						print(f"{solution() = }")