





'''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			:   Union[str, Any] , lowerCamelCase			:   int , lowerCamelCase			:   List[Any]=1_3 , lowerCamelCase			:   Optional[Any]=3_0 , lowerCamelCase			:   int=2 , lowerCamelCase			:   Dict=3 , lowerCamelCase			:   int=True , lowerCamelCase			:   int=True , lowerCamelCase			:   Optional[Any]=3_2 , lowerCamelCase			:   Union[str, Any]=2 , lowerCamelCase			:   List[Any]=4 , lowerCamelCase			:   Dict=3_7 , lowerCamelCase			:   Union[str, Any]="gelu" , lowerCamelCase			:   List[str]=0.1 , lowerCamelCase			:   Optional[int]=0.1 , lowerCamelCase			:   Optional[int]=1_0 , lowerCamelCase			:   Any=0.02 , lowerCamelCase			:   str=3 , lowerCamelCase			:   Dict=None , ):




				'''simple docstring'''
				a__     = parent
				a__     = batch_size
				a__     = image_size
				a__     = patch_size
				a__     = num_channels
				a__     = is_training
				a__     = use_labels
				a__     = hidden_size
				a__     = num_hidden_layers
				a__     = num_attention_heads
				a__     = intermediate_size
				a__     = hidden_act
				a__     = hidden_dropout_prob
				a__     = attention_probs_dropout_prob
				a__     = type_sequence_label_size
				a__     = initializer_range
				a__     = scope

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







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




				'''simple docstring'''
				a__     = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

				a__     = None
				if self.use_labels:
							a__     = ids_tensor([self.batch_size] , self.type_sequence_label_size		)

				a__     = self.get_config()

				return config, pixel_values, labels







	def 						__a   (				self			:   List[str]		):




				'''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 						__a   (				self			:   List[str] , lowerCamelCase			:   Any , lowerCamelCase			:   Optional[int] , lowerCamelCase			:   Optional[int]		):




				'''simple docstring'''
				a__     = TFViTModel(config=lowerCamelCase		)
				a__     = 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.
				a__     = self.image_size // 2
				a__     = pixel_values[:, :, :image_size, :image_size]
				a__     = model(lowerCamelCase , interpolate_pos_encoding=lowerCamelCase , training=lowerCamelCase		)
				a__     = (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] , lowerCamelCase			:   List[str] , lowerCamelCase			:   List[str] , lowerCamelCase			:   Union[str, Any]		):




				'''simple docstring'''
				a__     = self.type_sequence_label_size
				a__     = TFViTForImageClassification(lowerCamelCase		)
				a__     = 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.
				a__     = self.image_size // 2
				a__     = pixel_values[:, :, :image_size, :image_size]
				a__     = 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
				a__     = 1
				a__     = TFViTForImageClassification(lowerCamelCase		)
				a__     = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
				a__     = model(lowerCamelCase		)
				self.parent.assertEqual(result.logits.shape , (self.batch_size, self.type_sequence_label_size)		)







	def 						__a   (				self			:   Optional[int]		):




				'''simple docstring'''
				a__     = self.prepare_config_and_inputs()
				a__   ,				a__   ,				a__     = config_and_inputs
				a__     = {"pixel_values": pixel_values}
				return config, inputs_dict






@require_tf
class       UpperCamelCase__  (		__lowerCAmelCase	,__lowerCAmelCase	,unittest.TestCase     ):
	lowerCAmelCase__					:			Any								=						(TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
	lowerCAmelCase__					:			List[Any]								=						(
	    {"feature-extraction": TFViTModel, "image-classification": TFViTForImageClassification}
	    if is_tf_available()
	    else {}
	)

	lowerCAmelCase__					:			Optional[int]								=						False
	lowerCAmelCase__					:			int								=						False
	lowerCAmelCase__					:			List[str]								=						False







	def 						__a   (				self			:   Dict		):




				'''simple docstring'''
				a__     = TFViTModelTester(self		)
				a__     = ConfigTester(self , config_class=lowerCamelCase , has_text_modality=lowerCamelCase , hidden_size=3_7		)







	def 						__a   (				self			:   int		):




				'''simple docstring'''
				self.config_tester.run_common_tests()







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




				'''simple docstring'''
				pass







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




				'''simple docstring'''
				pass







	def 						__a   (				self			:   Optional[Any]		):




				'''simple docstring'''
				a__   ,				a__     = self.model_tester.prepare_config_and_inputs_for_common()

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







	def 						__a   (				self			:   Dict		):




				'''simple docstring'''
				a__   ,				a__     = self.model_tester.prepare_config_and_inputs_for_common()

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

							a__     = ["pixel_values"]
							self.assertListEqual(arg_names[:1] , lowerCamelCase		)







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




				'''simple docstring'''
				a__     = self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*lowerCamelCase		)







	def 						__a   (				self			:   str		):




				'''simple docstring'''
				a__     = self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*lowerCamelCase		)







	@slow
	def 						__a   (				self			:   str		):




				'''simple docstring'''
				a__     = TFViTModel.from_pretrained("google/vit-base-patch16-224"		)
				self.assertIsNotNone(lowerCamelCase		)







def    _lowerCamelCase  ()   ->    List[str]:
			a__     = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"     )
			return image






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







	@cached_property
	def 						__a   (				self			:   Dict		):




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







	@slow
	def 						__a   (				self			:   int		):




				'''simple docstring'''
				a__     = TFViTForImageClassification.from_pretrained("google/vit-base-patch16-224"		)

				a__     = self.default_image_processor
				a__     = prepare_img()
				a__     = image_processor(images=lowerCamelCase , return_tensors="tf"		)

				# forward pass
				a__     = model(**lowerCamelCase		)

				# verify the logits
				a__     = tf.TensorShape((1, 1_0_0_0)		)
				self.assertEqual(outputs.logits.shape , lowerCamelCase		)

				a__     = tf.constant([-0.2744, 0.8215, -0.0836]		)

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










'''simple docstring'''


class       UpperCamelCase__  :







	def __init__(				self			:   str , lowerCamelCase			:   int , lowerCamelCase			:   List[str]=None , lowerCamelCase			:   int=None		):




				'''simple docstring'''
				a__     = data
				a__     = previous
				a__     = next_node







	def __str__(				self			:   List[Any]		):




				'''simple docstring'''
				return F'''{self.data}'''







	def 						__a   (				self			:   List[Any]		):




				'''simple docstring'''
				return self.data







	def 						__a   (				self			:   Tuple		):




				'''simple docstring'''
				return self.next







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




				'''simple docstring'''
				return self.previous






class       UpperCamelCase__  :







	def __init__(				self			:   int , lowerCamelCase			:   Tuple		):




				'''simple docstring'''
				a__     = head







	def __iter__(				self			:   Any		):




				'''simple docstring'''
				return self







	def 						__a   (				self			:   List[Any]		):




				'''simple docstring'''
				if not self.current:
							raise StopIteration
				else:
							a__     = self.current.get_data()
							a__     = self.current.get_next()
							return value






class       UpperCamelCase__  :







	def __init__(				self			:   Tuple		):




				'''simple docstring'''
				a__     = None  # First node in list
				a__     = None  # Last node in list







	def __str__(				self			:   Any		):




				'''simple docstring'''
				a__     = self.head
				a__     = []
				while current is not None:
							nodes.append(current.get_data()		)
							a__     = current.get_next()
				return " ".join(str(lowerCamelCase		) for node in nodes		)







	def __contains__(				self			:   Optional[int] , lowerCamelCase			:   int		):




				'''simple docstring'''
				a__     = self.head
				while current:
							if current.get_data() == value:
										return True
							a__     = current.get_next()
				return False







	def __iter__(				self			:   int		):




				'''simple docstring'''
				return LinkedListIterator(self.head		)







	def 						__a   (				self			:   str		):




				'''simple docstring'''
				if self.head:
							return self.head.get_data()
				return None







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




				'''simple docstring'''
				if self.tail:
							return self.tail.get_data()
				return None







	def 						__a   (				self			:   List[Any] , lowerCamelCase			:   Node		):




				'''simple docstring'''
				if self.head is None:
							a__     = node
							a__     = node
				else:
							self.insert_before_node(self.head , lowerCamelCase		)







	def 						__a   (				self			:   List[str] , lowerCamelCase			:   Node		):




				'''simple docstring'''
				if self.head is None:
							self.set_head(lowerCamelCase		)
				else:
							self.insert_after_node(self.tail , lowerCamelCase		)







	def 						__a   (				self			:   List[str] , lowerCamelCase			:   int		):




				'''simple docstring'''
				a__     = Node(lowerCamelCase		)
				if self.head is None:
							self.set_head(lowerCamelCase		)
				else:
							self.set_tail(lowerCamelCase		)







	def 						__a   (				self			:   Any , lowerCamelCase			:   Node , lowerCamelCase			:   Node		):




				'''simple docstring'''
				a__     = node
				a__     = node.previous

				if node.get_previous() is None:
							a__     = node_to_insert
				else:
							a__     = node_to_insert

				a__     = node_to_insert







	def 						__a   (				self			:   Tuple , lowerCamelCase			:   Node , lowerCamelCase			:   Node		):




				'''simple docstring'''
				a__     = node
				a__     = node.next

				if node.get_next() is None:
							a__     = node_to_insert
				else:
							a__     = node_to_insert

				a__     = node_to_insert







	def 						__a   (				self			:   Union[str, Any] , lowerCamelCase			:   int , lowerCamelCase			:   int		):




				'''simple docstring'''
				a__     = 1
				a__     = Node(lowerCamelCase		)
				a__     = self.head
				while node:
							if current_position == position:
										self.insert_before_node(lowerCamelCase , lowerCamelCase		)
										return
							current_position += 1
							a__     = node.next
				self.insert_after_node(self.tail , lowerCamelCase		)







	def 						__a   (				self			:   List[Any] , lowerCamelCase			:   int		):




				'''simple docstring'''
				a__     = self.head
				while node:
							if node.get_data() == item:
										return node
							a__     = node.get_next()
				raise Exception("Node not found"		)







	def 						__a   (				self			:   List[Any] , lowerCamelCase			:   List[str]		):




				'''simple docstring'''
				if (node := self.get_node(lowerCamelCase		)) is not None:
							if node == self.head:
										a__     = self.head.get_next()

							if node == self.tail:
										a__     = self.tail.get_previous()

							self.remove_node_pointers(lowerCamelCase		)







	@staticmethod
	def 						__a   (				lowerCamelCase			:   Node		):




				'''simple docstring'''
				if node.get_next():
							a__     = node.previous

				if node.get_previous():
							a__     = node.next

				a__     = None
				a__     = None







	def 						__a   (				self			:   List[str]		):




				'''simple docstring'''
				return self.head is None







def    _lowerCamelCase  ()   ->    None:
			pass


if __name__ == "__main__":
		import doctest

		doctest.testmod()



