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



		def __init__(			self    ,				A_    ,				A_=13    ,				A_=30    ,				A_=2    ,				A_=3    ,				A_=True    ,				A_=True    ,				A_=32    ,				A_=5    ,				A_=4    ,				A_=37    ,				A_="gelu"    ,				A_=0.1    ,				A_=0.1    ,				A_=10    ,				A_=0.02    ,				A_=None    ,				A_=2    ,				)  -> Tuple:
				"""simple docstring"""
				UpperCamelCase          =					parent
				UpperCamelCase          =					batch_size
				UpperCamelCase          =					image_size
				UpperCamelCase          =					patch_size
				UpperCamelCase          =					num_channels
				UpperCamelCase          =					is_training
				UpperCamelCase          =					use_labels
				UpperCamelCase          =					hidden_size
				UpperCamelCase          =					num_hidden_layers
				UpperCamelCase          =					num_attention_heads
				UpperCamelCase          =					intermediate_size
				UpperCamelCase          =					hidden_act
				UpperCamelCase          =					hidden_dropout_prob
				UpperCamelCase          =					attention_probs_dropout_prob
				UpperCamelCase          =					type_sequence_label_size
				UpperCamelCase          =					initializer_range
				UpperCamelCase          =					scope
				UpperCamelCase          =					encoder_stride

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



		def __UpperCamelCase			(			self	)  -> Union[str, Any]:
				"""simple docstring"""
				UpperCamelCase          =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

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

				UpperCamelCase          =					self.get_config()

				return config, pixel_values, labels



		def __UpperCamelCase			(			self	)  -> Optional[Any]:
				"""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=lowercase__    ,				initializer_range=self.initializer_range    ,				encoder_stride=self.encoder_stride    ,				)



		def __UpperCamelCase			(			self    ,				A_    ,				A_    ,				A_	)  -> List[Any]:
				"""simple docstring"""
				UpperCamelCase          =					ViTModel(config=lowercase__	)
				model.to(lowercase__	)
				model.eval()
				UpperCamelCase          =					model(lowercase__	)
				self.parent.assertEqual(result.last_hidden_state.shape    ,				(self.batch_size, self.seq_length, self.hidden_size)	)



		def __UpperCamelCase			(			self    ,				A_    ,				A_    ,				A_	)  -> Any:
				"""simple docstring"""
				UpperCamelCase          =					ViTForMaskedImageModeling(config=lowercase__	)
				model.to(lowercase__	)
				model.eval()
				UpperCamelCase          =					model(lowercase__	)
				self.parent.assertEqual(
				    result.reconstruction.shape    ,				(self.batch_size, self.num_channels, self.image_size, self.image_size)	)

				# test greyscale images
				UpperCamelCase          =					1
				UpperCamelCase          =					ViTForMaskedImageModeling(lowercase__	)
				model.to(lowercase__	)
				model.eval()

				UpperCamelCase          =					floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
				UpperCamelCase          =					model(lowercase__	)
				self.parent.assertEqual(result.reconstruction.shape    ,				(self.batch_size, 1, self.image_size, self.image_size)	)



		def __UpperCamelCase			(			self    ,				A_    ,				A_    ,				A_	)  -> List[str]:
				"""simple docstring"""
				UpperCamelCase          =					self.type_sequence_label_size
				UpperCamelCase          =					ViTForImageClassification(lowercase__	)
				model.to(lowercase__	)
				model.eval()
				UpperCamelCase          =					model(lowercase__    ,				labels=lowercase__	)
				self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.type_sequence_label_size)	)

				# test greyscale images
				UpperCamelCase          =					1
				UpperCamelCase          =					ViTForImageClassification(lowercase__	)
				model.to(lowercase__	)
				model.eval()

				UpperCamelCase          =					floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
				UpperCamelCase          =					model(lowercase__	)
				self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.type_sequence_label_size)	)



		def __UpperCamelCase			(			self	)  -> Optional[int]:
				"""simple docstring"""
				UpperCamelCase          =					self.prepare_config_and_inputs()
				(
				    (
				    UpperCamelCase     
				)     ,  (
				    UpperCamelCase     
				)     ,  (
				    UpperCamelCase     
				)     ,       
				)          =					config_and_inputs
				UpperCamelCase          =					{'pixel_values': pixel_values}
				return config, inputs_dict






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

		__lowercase	:	Any     					=    False
		__lowercase	:	Union[str, Any]     					=    False
		__lowercase	:	List[str]     					=    False



		def __UpperCamelCase			(			self	)  -> Dict:
				"""simple docstring"""
				UpperCamelCase          =					ViTModelTester(self	)
				UpperCamelCase          =					ConfigTester(self    ,				config_class=lowercase__    ,				has_text_modality=lowercase__    ,				hidden_size=37	)



		def __UpperCamelCase			(			self	)  -> List[Any]:
				"""simple docstring"""
				self.config_tester.run_common_tests()



		@unittest.skip(reason='ViT does not use inputs_embeds'	)
		def __UpperCamelCase			(			self	)  -> Optional[int]:
				"""simple docstring"""
				pass



		def __UpperCamelCase			(			self	)  -> Any:
				"""simple docstring"""
				UpperCamelCase     ,  UpperCamelCase          =					self.model_tester.prepare_config_and_inputs_for_common()

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



		def __UpperCamelCase			(			self	)  -> Any:
				"""simple docstring"""
				UpperCamelCase     ,  UpperCamelCase          =					self.model_tester.prepare_config_and_inputs_for_common()

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

						UpperCamelCase          =					['pixel_values']
						self.assertListEqual(arg_names[:1]    ,				lowercase__	)



		def __UpperCamelCase			(			self	)  -> Union[str, Any]:
				"""simple docstring"""
				UpperCamelCase          =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*lowercase__	)



		def __UpperCamelCase			(			self	)  -> Optional[int]:
				"""simple docstring"""
				UpperCamelCase          =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_masked_image_modeling(*lowercase__	)



		def __UpperCamelCase			(			self	)  -> int:
				"""simple docstring"""
				UpperCamelCase          =					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*lowercase__	)



		@slow
		def __UpperCamelCase			(			self	)  -> Union[str, Any]:
				"""simple docstring"""
				for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						UpperCamelCase          =					ViTModel.from_pretrained(lowercase__	)
						self.assertIsNotNone(lowercase__	)






def 				A	( )       ->  Tuple:
		'''simple docstring'''

		UpperCamelCase          =					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'			)
		return image


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



		@cached_property
		def __UpperCamelCase			(			self	)  -> List[str]:
				"""simple docstring"""
				return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224'	) if is_vision_available() else None



		@slow
		def __UpperCamelCase			(			self	)  -> Tuple:
				"""simple docstring"""
				UpperCamelCase          =					ViTForImageClassification.from_pretrained('google/vit-base-patch16-224'	).to(lowercase__	)

				UpperCamelCase          =					self.default_image_processor
				UpperCamelCase          =					prepare_img()
				UpperCamelCase          =					image_processor(images=lowercase__    ,				return_tensors='pt'	).to(lowercase__	)

				# forward pass
				with torch.no_grad():
						UpperCamelCase          =					model(**lowercase__	)

				# verify the logits
				UpperCamelCase          =					torch.Size((1, 1_000)	)
				self.assertEqual(outputs.logits.shape    ,				lowercase__	)

				UpperCamelCase          =					torch.tensor([-0.2744, 0.8215, -0.0836]	).to(lowercase__	)

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



		@slow
		def __UpperCamelCase			(			self	)  -> List[Any]:
				"""simple docstring"""
				# 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.
				UpperCamelCase          =					ViTModel.from_pretrained('facebook/dino-vits8'	).to(lowercase__	)

				UpperCamelCase          =					ViTImageProcessor.from_pretrained('facebook/dino-vits8'    ,				size=480	)
				UpperCamelCase          =					prepare_img()
				UpperCamelCase          =					image_processor(images=lowercase__    ,				return_tensors='pt'	)
				UpperCamelCase          =					inputs.pixel_values.to(lowercase__	)

				# forward pass
				with torch.no_grad():
						UpperCamelCase          =					model(lowercase__    ,				interpolate_pos_encoding=lowercase__	)

				# verify the logits
				UpperCamelCase          =					torch.Size((1, 3_601, 384)	)
				self.assertEqual(outputs.last_hidden_state.shape    ,				lowercase__	)

				UpperCamelCase          =					torch.tensor(
				    [[4.2340, 4.3906, -6.6692], [4.5463, 1.8928, -6.7257], [4.4429, 0.8496, -5.8585]]	).to(lowercase__	)

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



		@slow
		@require_accelerate
		@require_torch_gpu
		def __UpperCamelCase			(			self	)  -> List[Any]:
				"""simple docstring"""
				UpperCamelCase          =					ViTModel.from_pretrained('facebook/dino-vits8'    ,				torch_dtype=torch.floataa    ,				device_map='auto'	)
				UpperCamelCase          =					self.default_image_processor

				UpperCamelCase          =					prepare_img()
				UpperCamelCase          =					image_processor(images=lowercase__    ,				return_tensors='pt'	)
				UpperCamelCase          =					inputs.pixel_values.to(lowercase__	)

				# forward pass to make sure inference works in fp16
				with torch.no_grad():
						UpperCamelCase          =					model(lowercase__	)


import logging
import os

from .state import PartialState


class     lowercase (		logging.LoggerAdapter       ):



		@staticmethod
		def __UpperCamelCase			(			A_	)  -> Optional[Any]:
				"""simple docstring"""
				UpperCamelCase          =					PartialState()
				return not main_process_only or (main_process_only and state.is_main_process)



		def __UpperCamelCase			(			self    ,				A_    ,				A_    ,				*A_    ,				**A_	)  -> Union[str, Any]:
				"""simple docstring"""
				if PartialState._shared_state == {}:
						raise RuntimeError(
						    'You must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility.'	)
				UpperCamelCase          =					kwargs.pop('main_process_only'    ,				A_	)
				UpperCamelCase          =					kwargs.pop('in_order'    ,				A_	)

				if self.isEnabledFor(A_	):
						if self._should_log(A_	):
								UpperCamelCase     ,  UpperCamelCase          =					self.process(A_    ,				A_	)
								self.logger.log(A_    ,				A_    ,				*A_    ,				**A_	)

						elif in_order:
								UpperCamelCase          =					PartialState()
								for i in range(state.num_processes	):
										if i == state.process_index:
												UpperCamelCase     ,  UpperCamelCase          =					self.process(A_    ,				A_	)
												self.logger.log(A_    ,				A_    ,				*A_    ,				**A_	)
										state.wait_for_everyone()






def 				A	( lowercase   ,	lowercase = None			)       ->  Dict:
		'''simple docstring'''

		if log_level is None:
				UpperCamelCase          =					os.environ.get('ACCELERATE_LOG_LEVEL'   ,	lowercase			)
		UpperCamelCase          =					logging.getLogger(lowercase			)
		if log_level is not None:
				logger.setLevel(log_level.upper()			)
				logger.root.setLevel(log_level.upper()			)
		return MultiProcessAdapter(lowercase   ,	{}			)
