

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


						def __init__(							self    ,       A_    ,       A_=13    ,       A_=30    ,       A_=2    ,       A_=3    ,       A_=True    ,       A_=True    ,       A_=32    ,       A_=2    ,       A_=4    ,       A_=37    ,       A_="gelu"    ,       A_=0.1    ,       A_=0.1    ,       A_=10    ,       A_=0.02    ,       A_=3    ,       A_=None    ,       ):







										'''simple docstring'''


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

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


						def 							_UpperCAmelCase					(							self						):







										'''simple docstring'''


										_UpperCAmelCase							:		int					=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

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

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

										return config, pixel_values, labels


						def 							_UpperCAmelCase					(							self						):







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


						def 							_UpperCAmelCase					(							self    ,       A_    ,       A_    ,       A_						):







										'''simple docstring'''


										_UpperCAmelCase							:		Any					=     TFViTModel(config=A_						)
										_UpperCAmelCase							:		Optional[Any]					=     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.
										_UpperCAmelCase							:		Tuple					=     self.image_size // 2
										_UpperCAmelCase							:		List[Any]					=     pixel_values[:, :, :image_size, :image_size]
										_UpperCAmelCase							:		int					=     model(A_    ,       interpolate_pos_encoding=A_    ,       training=A_						)
										_UpperCAmelCase							:		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 							_UpperCAmelCase					(							self    ,       A_    ,       A_    ,       A_						):







										'''simple docstring'''


										_UpperCAmelCase							:		Optional[int]					=     self.type_sequence_label_size
										_UpperCAmelCase							:		Tuple					=     TFViTForImageClassification(A_						)
										_UpperCAmelCase							:		str					=     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.
										_UpperCAmelCase							:		Tuple					=     self.image_size // 2
										_UpperCAmelCase							:		int					=     pixel_values[:, :, :image_size, :image_size]
										_UpperCAmelCase							:		Tuple					=     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
										_UpperCAmelCase							:		Dict					=     1
										_UpperCAmelCase							:		List[str]					=     TFViTForImageClassification(A_						)
										_UpperCAmelCase							:		List[Any]					=     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
										_UpperCAmelCase							:		List[str]					=     model(A_						)
										self.parent.assertEqual(result.logits.shape    ,       (self.batch_size, self.type_sequence_label_size)						)





						def 							_UpperCAmelCase					(							self						):







										'''simple docstring'''


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







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

						_lowercase         =      False
						_lowercase         =      False
						_lowercase         =      False


						def 							_UpperCAmelCase					(							self						):







										'''simple docstring'''


										_UpperCAmelCase							:		str					=     TFViTModelTester(self						)
										_UpperCAmelCase							:		Any					=     ConfigTester(self    ,       config_class=A_    ,       has_text_modality=A_    ,       hidden_size=37						)


						def 							_UpperCAmelCase					(							self						):







										'''simple docstring'''


										self.config_tester.run_common_tests()


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







										'''simple docstring'''


										pass


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







										'''simple docstring'''


										pass


						def 							_UpperCAmelCase					(							self						):







										'''simple docstring'''


										_UpperCAmelCase							:		Optional[int]					=     self.model_tester.prepare_config_and_inputs_for_common()

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


						def 							_UpperCAmelCase					(							self						):







										'''simple docstring'''


										_UpperCAmelCase							:		Any					=     self.model_tester.prepare_config_and_inputs_for_common()

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

														_UpperCAmelCase							:		Optional[int]					=     ["pixel_values"]
														self.assertListEqual(arg_names[:1]    ,       A_						)


						def 							_UpperCAmelCase					(							self						):







										'''simple docstring'''


										_UpperCAmelCase							:		Optional[int]					=     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*A_						)


						def 							_UpperCAmelCase					(							self						):







										'''simple docstring'''


										_UpperCAmelCase							:		Union[str, Any]					=     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*A_						)





						@slow
						def 							_UpperCAmelCase					(							self						):







										'''simple docstring'''


										_UpperCAmelCase							:		List[Any]					=     TFViTModel.from_pretrained("google/vit-base-patch16-224"						)
										self.assertIsNotNone(A_						)







def 				__SCREAMING_SNAKE_CASE				(					) ->							Optional[Any]:
				_UpperCAmelCase							:		Optional[int]					=     Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"			)
				return image





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


						@cached_property
						def 							_UpperCAmelCase					(							self						):







										'''simple docstring'''


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





						@slow
						def 							_UpperCAmelCase					(							self						):







										'''simple docstring'''


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

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

										# forward pass
										_UpperCAmelCase							:		Dict					=     model(**A_						)

										# verify the logits
										_UpperCAmelCase							:		Tuple					=     tf.TensorShape((1, 1000)						)
										self.assertEqual(outputs.logits.shape    ,       A_						)

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

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





from __future__ import annotations

def 			lowerCamelCase__			(       _a):
	if len(_a) == 0:
		return []
	SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE :   Tuple					  =							min(_a), max(_a)
	SCREAMING_SNAKE_CASE :   Dict					  =							int(max_value - min_value) + 1
	SCREAMING_SNAKE_CASE :   list[list]					  =							[[] for _ in range(_a)]

	for i in my_list:
		buckets[int(i - min_value)].append(_a)

	return [v for bucket in buckets for v in sorted(_a)]


if __name__ == "__main__":
			from doctest import testmod

			testmod()
			assert bucket_sort([4, 5, 3, 2, 1]) == [1, 2, 3, 4, 5]
			assert bucket_sort([0, 1, -10, 15, 2, -2]) == [-10, -2, 0, 1, 2, 15]