



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







					def __init__(       self      :     List[Any]			,		_A      :     Tuple			,		_A      :     List[Any]=13			,		_A      :     Union[str, Any]=30			,		_A      :     Any=2			,		_A      :     str=3			,		_A      :     Union[str, Any]=True			,		_A      :     Optional[Any]=True			,		_A      :     Tuple=32			,		_A      :     Optional[int]=5			,		_A      :     Union[str, Any]=4			,		_A      :     str=37			,		_A      :     Optional[Any]="gelu"			,		_A      :     Dict=0.1			,		_A      :     List[str]=0.1			,		_A      :     Optional[int]=10			,		_A      :     Optional[int]=0.02			,		_A      :     int=None			,		_A      :     int=2			,		):
											A__	:							int						    = parent
											A__	:							Dict						    = batch_size
											A__	:							Optional[int]						    = image_size
											A__	:							Union[str, Any]						    = patch_size
											A__	:							List[str]						    = num_channels
											A__	:							Dict						    = is_training
											A__	:							Tuple						    = use_labels
											A__	:							int						    = hidden_size
											A__	:							Dict						    = num_hidden_layers
											A__	:							List[str]						    = num_attention_heads
											A__	:							Dict						    = intermediate_size
											A__	:							int						    = hidden_act
											A__	:							List[str]						    = hidden_dropout_prob
											A__	:							List[str]						    = attention_probs_dropout_prob
											A__	:							Any						    = type_sequence_label_size
											A__	:							List[Any]						    = initializer_range
											A__	:							Optional[int]						    = scope
											A__	:							List[str]						    = encoder_stride

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







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

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

											A__	:							Tuple						    = self.get_config()

											return config, pixel_values, labels







					def  _lowercase			(       self      :     str):
											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			,		encoder_stride=self.encoder_stride			,		)







					def  _lowercase			(       self      :     str			,		_A      :     Optional[Any]			,		_A      :     str			,		_A      :     List[Any]):
											A__	:							Dict						    = ViTModel(config=_A)
											model.to(_A)
											model.eval()
											A__	:							List[Any]						    = model(_A)
											self.parent.assertEqual(result.last_hidden_state.shape			,		(self.batch_size, self.seq_length, self.hidden_size))







					def  _lowercase			(       self      :     Any			,		_A      :     Optional[int]			,		_A      :     Dict			,		_A      :     Union[str, Any]):
											A__	:							int						    = ViTForMaskedImageModeling(config=_A)
											model.to(_A)
											model.eval()
											A__	:							Union[str, Any]						    = model(_A)
											self.parent.assertEqual(
											    result.reconstruction.shape			,		(self.batch_size, self.num_channels, self.image_size, self.image_size))

											# test greyscale images
											A__	:							Optional[int]						    = 1
											A__	:							int						    = ViTForMaskedImageModeling(_A)
											model.to(_A)
											model.eval()

											A__	:							Union[str, Any]						    = floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
											A__	:							List[str]						    = model(_A)
											self.parent.assertEqual(result.reconstruction.shape			,		(self.batch_size, 1, self.image_size, self.image_size))







					def  _lowercase			(       self      :     List[str]			,		_A      :     Tuple			,		_A      :     Optional[Any]			,		_A      :     int):
											A__	:							List[Any]						    = self.type_sequence_label_size
											A__	:							List[Any]						    = ViTForImageClassification(_A)
											model.to(_A)
											model.eval()
											A__	:							List[str]						    = model(_A			,		labels=_A)
											self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.type_sequence_label_size))

											# test greyscale images
											A__	:							List[str]						    = 1
											A__	:							Optional[int]						    = ViTForImageClassification(_A)
											model.to(_A)
											model.eval()

											A__	:							List[Any]						    = floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
											A__	:							Tuple						    = model(_A)
											self.parent.assertEqual(result.logits.shape			,		(self.batch_size, self.type_sequence_label_size))







					def  _lowercase			(       self      :     int):
											A__	:							int						    = self.prepare_config_and_inputs()
											(
											    (
											    A__
											)    ,       (
											    A__
											)    ,       (
											    A__
											)    ,       
											)	:							Optional[Any]						    = config_and_inputs
											A__	:							Union[str, Any]						    = {"pixel_values": pixel_values}
											return config, inputs_dict




@require_torch
class 							lowerCAmelCase__  (	UpperCamelCase					,							UpperCamelCase					,							unittest.TestCase						):
					__A     :    Optional[int]           =						(
					    (
					        ViTModel,
					        ViTForImageClassification,
					        ViTForMaskedImageModeling,
					    )
					    if is_torch_available()
					    else ()
					)
					__A     :    Dict           =						(
					    {'feature-extraction': ViTModel, 'image-classification': ViTForImageClassification}
					    if is_torch_available()
					    else {}
					)
					__A     :    int           =						True

					__A     :    Tuple           =						False
					__A     :    Optional[int]           =						False
					__A     :    Optional[int]           =						False







					def  _lowercase			(       self      :     Any):
											A__	:							str						    = ViTModelTester(self)
											A__	:							int						    = ConfigTester(self			,		config_class=_A			,		has_text_modality=_A			,		hidden_size=37)







					def  _lowercase			(       self      :     List[Any]):
											self.config_tester.run_common_tests()







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







					def  _lowercase			(       self      :     Dict):
											A__    ,       A__	:							Any						    = self.model_tester.prepare_config_and_inputs_for_common()

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







					def  _lowercase			(       self      :     List[str]):
											A__    ,       A__	:							List[str]						    = self.model_tester.prepare_config_and_inputs_for_common()

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

																	A__	:							Union[str, Any]						    = ["pixel_values"]
																	self.assertListEqual(arg_names[:1]			,		_A)







					def  _lowercase			(       self      :     Union[str, Any]):
											A__	:							Optional[Any]						    = self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_A)







					def  _lowercase			(       self      :     List[Any]):
											A__	:							Optional[Any]						    = self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_image_modeling(*_A)







					def  _lowercase			(       self      :     int):
											A__	:							Optional[int]						    = self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*_A)







					@slow
					def  _lowercase			(       self      :     str):
											for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	A__	:							str						    = ViTModel.from_pretrained(_A)
																	self.assertIsNotNone(_A)




def 	snake_case__       (					)		->		Dict:







						"""simple docstring"""

						A__	:							List[Any]						    = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"       )
						return image




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







					@cached_property
					def  _lowercase			(       self      :     Union[str, Any]):
											return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224") if is_vision_available() else None







					@slow
					def  _lowercase			(       self      :     Optional[Any]):
											A__	:							List[Any]						    = ViTForImageClassification.from_pretrained("google/vit-base-patch16-224").to(_A)

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

											# forward pass
											with torch.no_grad():
																	A__	:							Tuple						    = model(**_A)

											# verify the logits
											A__	:							Tuple						    = torch.Size((1, 1000))
											self.assertEqual(outputs.logits.shape			,		_A)

											A__	:							Optional[int]						    = torch.tensor([-0.27_44, 0.82_15, -0.08_36]).to(_A)

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







					@slow
					def  _lowercase			(       self      :     Union[str, Any]):
											# ViT models have an `interpolate_pos_encoding` argument in their forward method,
											# allowing to interpolate the pre-trained position embeddings in order to use
											# the model on higher resolutions. The DINO model by Facebook AI leverages this
											# to visualize self-attention on higher resolution images.
											A__	:							List[str]						    = ViTModel.from_pretrained("facebook/dino-vits8").to(_A)

											A__	:							int						    = ViTImageProcessor.from_pretrained("facebook/dino-vits8"			,		size=480)
											A__	:							Tuple						    = prepare_img()
											A__	:							List[Any]						    = image_processor(images=_A			,		return_tensors="pt")
											A__	:							int						    = inputs.pixel_values.to(_A)

											# forward pass
											with torch.no_grad():
																	A__	:							Optional[int]						    = model(_A			,		interpolate_pos_encoding=_A)

											# verify the logits
											A__	:							Optional[Any]						    = torch.Size((1, 3601, 384))
											self.assertEqual(outputs.last_hidden_state.shape			,		_A)

											A__	:							List[Any]						    = torch.tensor(
											    [[4.23_40, 4.39_06, -6.66_92], [4.54_63, 1.89_28, -6.72_57], [4.44_29, 0.84_96, -5.85_85]]).to(_A)

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







					@slow
					@require_accelerate
					@require_torch_gpu
					def  _lowercase			(       self      :     str):
											A__	:							int						    = ViTModel.from_pretrained("facebook/dino-vits8"			,		torch_dtype=torch.floataa			,		device_map="auto")
											A__	:							Optional[int]						    = self.default_image_processor

											A__	:							Optional[int]						    = prepare_img()
											A__	:							Dict						    = image_processor(images=_A			,		return_tensors="pt")
											A__	:							Dict						    = inputs.pixel_values.to(_A)

											# forward pass to make sure inference works in fp16
											with torch.no_grad():
																	A__	:							Union[str, Any]						    = model(_A)




from argparse import ArgumentParser

from .add_new_model import AddNewModelCommand
from .add_new_model_like import AddNewModelLikeCommand
from .convert import ConvertCommand
from .download import DownloadCommand
from .env import EnvironmentCommand
from .lfs import LfsCommands
from .pt_to_tf import PTtoTFCommand
from .run import RunCommand
from .serving import ServeCommand
from .user import UserCommands
def 	snake_case__       (					)		->		List[Any]:







						"""simple docstring"""

						A__	:							List[Any]						    = ArgumentParser("Transformers CLI tool"							,     usage="transformers-cli <command> [<args>]"       )
						A__	:							int						    = parser.add_subparsers(help="transformers-cli command helpers"       )

						# Register commands
						ConvertCommand.register_subcommand(__lowercase       )
						DownloadCommand.register_subcommand(__lowercase       )
						EnvironmentCommand.register_subcommand(__lowercase       )
						RunCommand.register_subcommand(__lowercase       )
						ServeCommand.register_subcommand(__lowercase       )
						UserCommands.register_subcommand(__lowercase       )
						AddNewModelCommand.register_subcommand(__lowercase       )
						AddNewModelLikeCommand.register_subcommand(__lowercase       )
						LfsCommands.register_subcommand(__lowercase       )
						PTtoTFCommand.register_subcommand(__lowercase       )

						# Let's go
						A__	:							int						    = parser.parse_args()

						if not hasattr(__lowercase							,     "func"       ):
												parser.print_help()
												exit(1       )

						# Run
						A__	:							Any						    = args.func(__lowercase       )
						service.run()


if __name__ == "__main__":
		main()