





'''simple docstring'''






import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import (
    require_accelerate,
    require_torch,
    require_torch_gpu,
    require_vision,
    slow,
    torch_device,
)
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch
	from torch import nn

	from transformers import ViTForImageClassification, ViTForMaskedImageModeling, ViTModel
	from transformers.models.vit.modeling_vit import VIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
	from PIL import Image

	from transformers import ViTImageProcessor




class  _A :

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


					"""simple docstring"""

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

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

		def 	lowercase__     (   self					:   Any			)					->    List[Any]:


					"""simple docstring"""

					__snake_case					:      List[Any]							      =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

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

					__snake_case					:      List[str]							      =     self.get_config()

					return config, pixel_values, labels

		def 	lowercase__     (   self					:   str			)					->    Tuple:


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

		def 	lowercase__     (   self					:   int  ,      __magic_name__					:   Any  ,      __magic_name__					:   Dict  ,      __magic_name__					:   str			)					->    str:


					"""simple docstring"""

					__snake_case					:      Optional[Any]							      =     ViTModel(config=__magic_name__			)
					model.to(__magic_name__			)
					model.eval()
					__snake_case					:      Dict							      =     model(__magic_name__			)
					self.parent.assertEqual(result.last_hidden_state.shape  ,      (self.batch_size, self.seq_length, self.hidden_size)			)

		def 	lowercase__     (   self					:   Any  ,      __magic_name__					:   str  ,      __magic_name__					:   Dict  ,      __magic_name__					:   List[str]			)					->    Tuple:


					"""simple docstring"""

					__snake_case					:      Optional[Any]							      =     ViTForMaskedImageModeling(config=__magic_name__			)
					model.to(__magic_name__			)
					model.eval()
					__snake_case					:      List[str]							      =     model(__magic_name__			)
					self.parent.assertEqual(
					    result.reconstruction.shape  ,      (self.batch_size, self.num_channels, self.image_size, self.image_size)			)

					# test greyscale images
					__snake_case					:      int							      =     1
					__snake_case					:      List[Any]							      =     ViTForMaskedImageModeling(__magic_name__			)
					model.to(__magic_name__			)
					model.eval()

					__snake_case					:      Optional[Any]							      =     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]			)
					__snake_case					:      Union[str, Any]							      =     model(__magic_name__			)
					self.parent.assertEqual(result.reconstruction.shape  ,      (self.batch_size, 1, self.image_size, self.image_size)			)

		def 	lowercase__     (   self					:   int  ,      __magic_name__					:   int  ,      __magic_name__					:   Any  ,      __magic_name__					:   Union[str, Any]			)					->    Optional[int]:


					"""simple docstring"""

					__snake_case					:      List[Any]							      =     self.type_sequence_label_size
					__snake_case					:      Optional[int]							      =     ViTForImageClassification(__magic_name__			)
					model.to(__magic_name__			)
					model.eval()
					__snake_case					:      List[str]							      =     model(__magic_name__  ,      labels=__magic_name__			)
					self.parent.assertEqual(result.logits.shape  ,      (self.batch_size, self.type_sequence_label_size)			)

					# test greyscale images
					__snake_case					:      int							      =     1
					__snake_case					:      List[str]							      =     ViTForImageClassification(__magic_name__			)
					model.to(__magic_name__			)
					model.eval()

					__snake_case					:      Optional[Any]							      =     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]			)
					__snake_case					:      Union[str, Any]							      =     model(__magic_name__			)
					self.parent.assertEqual(result.logits.shape  ,      (self.batch_size, self.type_sequence_label_size)			)

		def 	lowercase__     (   self					:   int			)					->    Union[str, Any]:


					"""simple docstring"""

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





@require_torch
class  _A (					__lowercase						,					__lowercase						,					unittest.TestCase		):
		lowercase__:   Optional[int]				     =   (
		    (
		        ViTModel,
		        ViTForImageClassification,
		        ViTForMaskedImageModeling,
		    )
		    if is_torch_available()
		    else ()
		)
		lowercase__:   Tuple				     =   (
		    {'''feature-extraction''': ViTModel, '''image-classification''': ViTForImageClassification}
		    if is_torch_available()
		    else {}
		)
		lowercase__:   Tuple				     =   True

		lowercase__:   str				     =   False
		lowercase__:   Optional[Any]				     =   False
		lowercase__:   Optional[Any]				     =   False

		def 	lowercase__     (   self					:   Optional[int]			)					->    str:


					"""simple docstring"""

					__snake_case					:      Any							      =     ViTModelTester(self			)
					__snake_case					:      Optional[Any]							      =     ConfigTester(self  ,      config_class=__magic_name__  ,      has_text_modality=__magic_name__  ,      hidden_size=37			)

		def 	lowercase__     (   self					:   List[Any]			)					->    Any:


					"""simple docstring"""

					self.config_tester.run_common_tests()

		@unittest.skip(reason="""ViT does not use inputs_embeds"""			)
		def 	lowercase__     (   self					:   Optional[Any]			)					->    Dict:


					"""simple docstring"""

					pass

		def 	lowercase__     (   self					:   Optional[int]			)					->    str:


					"""simple docstring"""

					__snake_case      ,     __snake_case					:      Dict							      =     self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
								__snake_case					:      int							      =     model_class(__magic_name__			)
								self.assertIsInstance(model.get_input_embeddings()  ,      (nn.Module)			)
								__snake_case					:      Any							      =     model.get_output_embeddings()
								self.assertTrue(x is None or isinstance(__magic_name__  ,      nn.Linear			)			)

		def 	lowercase__     (   self					:   Dict			)					->    List[Any]:


					"""simple docstring"""

					__snake_case      ,     __snake_case					:      int							      =     self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
								__snake_case					:      Optional[Any]							      =     model_class(__magic_name__			)
								__snake_case					:      Optional[Any]							      =     inspect.signature(model.forward			)
								# signature.parameters is an OrderedDict => so arg_names order is deterministic
								__snake_case					:      List[str]							      =     [*signature.parameters.keys()]

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

		def 	lowercase__     (   self					:   Dict			)					->    Any:


					"""simple docstring"""

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

		def 	lowercase__     (   self					:   int			)					->    Optional[int]:


					"""simple docstring"""

					__snake_case					:      Tuple							      =     self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_image_modeling(*__magic_name__			)

		def 	lowercase__     (   self					:   Optional[int]			)					->    Optional[int]:


					"""simple docstring"""

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

		@slow
		def 	lowercase__     (   self					:   int			)					->    int:


					"""simple docstring"""

					for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								__snake_case					:      str							      =     ViTModel.from_pretrained(__magic_name__			)
								self.assertIsNotNone(__magic_name__			)







def 					_a						(       ) ->				Optional[int]:



			"""simple docstring"""


			__snake_case					:      int							      =     Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" )
			return image




@require_torch
@require_vision
class  _A (					unittest.TestCase		):

		@cached_property
		def 	lowercase__     (   self					:   Optional[int]			)					->    str:


					"""simple docstring"""

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

		@slow
		def 	lowercase__     (   self					:   Optional[Any]			)					->    str:


					"""simple docstring"""

					__snake_case					:      Any							      =     ViTForImageClassification.from_pretrained("""google/vit-base-patch16-224"""			).to(__magic_name__			)

					__snake_case					:      List[str]							      =     self.default_image_processor
					__snake_case					:      Any							      =     prepare_img()
					__snake_case					:      Optional[Any]							      =     image_processor(images=__magic_name__  ,      return_tensors="""pt"""			).to(__magic_name__			)

					# forward pass
					with torch.no_grad():
								__snake_case					:      Optional[Any]							      =     model(**__magic_name__			)

					# verify the logits
					__snake_case					:      List[str]							      =     torch.Size((1, 10_00)			)
					self.assertEqual(outputs.logits.shape  ,      __magic_name__			)

					__snake_case					:      int							      =     torch.tensor([-0.2744, 0.8215, -0.0836]			).to(__magic_name__			)

					self.assertTrue(torch.allclose(outputs.logits[0, :3]  ,      __magic_name__  ,      atol=1E-4			)			)

		@slow
		def 	lowercase__     (   self					:   str			)					->    Tuple:


					"""simple docstring"""

					__snake_case					:      Tuple							      =     ViTModel.from_pretrained("""facebook/dino-vits8"""			).to(__magic_name__			)

					__snake_case					:      List[str]							      =     ViTImageProcessor.from_pretrained("""facebook/dino-vits8"""  ,      size=4_80			)
					__snake_case					:      Dict							      =     prepare_img()
					__snake_case					:      Tuple							      =     image_processor(images=__magic_name__  ,      return_tensors="""pt"""			)
					__snake_case					:      Optional[int]							      =     inputs.pixel_values.to(__magic_name__			)

					# forward pass
					with torch.no_grad():
								__snake_case					:      List[str]							      =     model(__magic_name__  ,      interpolate_pos_encoding=__magic_name__			)

					# verify the logits
					__snake_case					:      Any							      =     torch.Size((1, 36_01, 3_84)			)
					self.assertEqual(outputs.last_hidden_state.shape  ,      __magic_name__			)

					__snake_case					:      Union[str, Any]							      =     torch.tensor(
					    [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]]			).to(__magic_name__			)

					self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3]  ,      __magic_name__  ,      atol=1E-4			)			)

		@slow
		@require_accelerate
		@require_torch_gpu
		def 	lowercase__     (   self					:   Optional[Any]			)					->    str:


					"""simple docstring"""

					__snake_case					:      int							      =     ViTModel.from_pretrained("""facebook/dino-vits8"""  ,      torch_dtype=torch.floataa  ,      device_map="""auto"""			)
					__snake_case					:      str							      =     self.default_image_processor

					__snake_case					:      Optional[Any]							      =     prepare_img()
					__snake_case					:      Dict							      =     image_processor(images=__magic_name__  ,      return_tensors="""pt"""			)
					__snake_case					:      Dict							      =     inputs.pixel_values.to(__magic_name__			)

					# forward pass to make sure inference works in fp16
					with torch.no_grad():
								__snake_case					:      List[Any]							      =     model(__magic_name__			)









'''simple docstring'''






def 					_a						(       _lowerCamelCase ) ->				int:



			"""simple docstring"""


			if not isinstance(_lowerCamelCase    ,	_lowerCamelCase ):
						raise TypeError("""only integers accepted as input""" )
			else:
						__snake_case					:      List[Any]							      =     str(abs(_lowerCamelCase ) )
						__snake_case					:      Union[str, Any]							      =     [list(_lowerCamelCase ) for char in range(len(_lowerCamelCase ) )]
						for index in range(len(_lowerCamelCase ) ):
									num_transpositions[index].pop(_lowerCamelCase )
						return max(
						    int("""""".join(list(_lowerCamelCase ) ) ) for transposition in num_transpositions )


if __name__ == "__main__":
	__import__("doctest").testmod()


