






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






			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		,					)      ->       List[Any]:
						SCREAMING_SNAKE_CASE						= parent
						SCREAMING_SNAKE_CASE						= batch_size
						SCREAMING_SNAKE_CASE						= image_size
						SCREAMING_SNAKE_CASE						= patch_size
						SCREAMING_SNAKE_CASE						= num_channels
						SCREAMING_SNAKE_CASE						= is_training
						SCREAMING_SNAKE_CASE						= use_labels
						SCREAMING_SNAKE_CASE						= hidden_size
						SCREAMING_SNAKE_CASE						= num_hidden_layers
						SCREAMING_SNAKE_CASE						= num_attention_heads
						SCREAMING_SNAKE_CASE						= intermediate_size
						SCREAMING_SNAKE_CASE						= hidden_act
						SCREAMING_SNAKE_CASE						= hidden_dropout_prob
						SCREAMING_SNAKE_CASE						= attention_probs_dropout_prob
						SCREAMING_SNAKE_CASE						= type_sequence_label_size
						SCREAMING_SNAKE_CASE						= initializer_range
						SCREAMING_SNAKE_CASE						= scope
						SCREAMING_SNAKE_CASE						= encoder_stride

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






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Optional[Any]:
						SCREAMING_SNAKE_CASE						= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

						SCREAMING_SNAKE_CASE						= self.get_config()

						return config, pixel_values, labels






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       int:
						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    SCREAMING_SNAKE_CASE__    (       self		,					a		,					a		,					a)      ->       int:
						SCREAMING_SNAKE_CASE						= ViTModel(config=a)
						model.to(a)
						model.eval()
						SCREAMING_SNAKE_CASE						= model(a)
						self.parent.assertEqual(result.last_hidden_state.shape		,					(self.batch_size, self.seq_length, self.hidden_size))






			def    SCREAMING_SNAKE_CASE__    (       self		,					a		,					a		,					a)      ->       Any:
						SCREAMING_SNAKE_CASE						= ViTForMaskedImageModeling(config=a)
						model.to(a)
						model.eval()
						SCREAMING_SNAKE_CASE						= model(a)
						self.parent.assertEqual(
						    result.reconstruction.shape		,					(self.batch_size, self.num_channels, self.image_size, self.image_size))

						# test greyscale images
						SCREAMING_SNAKE_CASE						= 1
						SCREAMING_SNAKE_CASE						= ViTForMaskedImageModeling(a)
						model.to(a)
						model.eval()

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






			def    SCREAMING_SNAKE_CASE__    (       self		,					a		,					a		,					a)      ->       Union[str, Any]:
						SCREAMING_SNAKE_CASE						= self.type_sequence_label_size
						SCREAMING_SNAKE_CASE						= ViTForImageClassification(a)
						model.to(a)
						model.eval()
						SCREAMING_SNAKE_CASE						= model(a		,					labels=a)
						self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.type_sequence_label_size))

						# test greyscale images
						SCREAMING_SNAKE_CASE						= 1
						SCREAMING_SNAKE_CASE						= ViTForImageClassification(a)
						model.to(a)
						model.eval()

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







			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Optional[int]:
						SCREAMING_SNAKE_CASE						= self.prepare_config_and_inputs()
						(
						    (
						    SCREAMING_SNAKE_CASE
						)				,						(
						    SCREAMING_SNAKE_CASE
						)				,						(
						    SCREAMING_SNAKE_CASE
						)				,						
						)						= config_and_inputs
						SCREAMING_SNAKE_CASE						= {'pixel_values': pixel_values}
						return config, inputs_dict



@require_torch
class 		_snake_case  (  A__   ,    A__   ,    unittest.TestCase					):
			_lowercase		:    Dict			     =							(
			    (
			        ViTModel,
			        ViTForImageClassification,
			        ViTForMaskedImageModeling,
			    )
			    if is_torch_available()
			    else ()
			)
			_lowercase		:    str			     =							(
			    {'''feature-extraction''': ViTModel, '''image-classification''': ViTForImageClassification}
			    if is_torch_available()
			    else {}
			)
			_lowercase		:    Optional[Any]			     =							True

			_lowercase		:    Optional[Any]			     =							False
			_lowercase		:    Tuple			     =							False
			_lowercase		:    str			     =							False






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       str:
						SCREAMING_SNAKE_CASE						= ViTModelTester(self)
						SCREAMING_SNAKE_CASE						= ConfigTester(self		,					config_class=a		,					has_text_modality=a		,					hidden_size=37)






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       List[Any]:
						self.config_tester.run_common_tests()






			@unittest.skip(reason='ViT does not use inputs_embeds')
			def    SCREAMING_SNAKE_CASE__    (       self)      ->       int:
						pass






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Dict:
						SCREAMING_SNAKE_CASE				,						SCREAMING_SNAKE_CASE						= self.model_tester.prepare_config_and_inputs_for_common()

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






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       str:
						SCREAMING_SNAKE_CASE				,						SCREAMING_SNAKE_CASE						= self.model_tester.prepare_config_and_inputs_for_common()

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

									SCREAMING_SNAKE_CASE						= ['pixel_values']
									self.assertListEqual(arg_names[:1]		,					a)






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       int:
						SCREAMING_SNAKE_CASE						= self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*a)






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Dict:
						SCREAMING_SNAKE_CASE						= self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_image_modeling(*a)






			def    SCREAMING_SNAKE_CASE__    (       self)      ->       int:
						SCREAMING_SNAKE_CASE						= self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*a)







			@slow
			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Optional[int]:
						for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									SCREAMING_SNAKE_CASE						= ViTModel.from_pretrained(a)
									self.assertIsNotNone(a)






def 				lowerCamelCase__				():
			SCREAMING_SNAKE_CASE						= Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png')
			return image







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






			@cached_property
			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Dict:
						return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224') if is_vision_available() else None






			@slow
			def    SCREAMING_SNAKE_CASE__    (       self)      ->       Dict:
						SCREAMING_SNAKE_CASE						= ViTForImageClassification.from_pretrained('google/vit-base-patch16-224').to(a)

						SCREAMING_SNAKE_CASE						= self.default_image_processor
						SCREAMING_SNAKE_CASE						= prepare_img()
						SCREAMING_SNAKE_CASE						= image_processor(images=a		,					return_tensors='pt').to(a)

						# forward pass
						with torch.no_grad():
									SCREAMING_SNAKE_CASE						= model(**a)

						# verify the logits
						SCREAMING_SNAKE_CASE						= torch.Size((1, 1000))
						self.assertEqual(outputs.logits.shape		,					a)

						SCREAMING_SNAKE_CASE						= 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    SCREAMING_SNAKE_CASE__    (       self)      ->       Optional[int]:
						# 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.
						SCREAMING_SNAKE_CASE						= ViTModel.from_pretrained('facebook/dino-vits8').to(a)

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

						# forward pass
						with torch.no_grad():
									SCREAMING_SNAKE_CASE						= model(a		,					interpolate_pos_encoding=a)

						# verify the logits
						SCREAMING_SNAKE_CASE						= torch.Size((1, 3601, 384))
						self.assertEqual(outputs.last_hidden_state.shape		,					a)

						SCREAMING_SNAKE_CASE						= 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    SCREAMING_SNAKE_CASE__    (       self)      ->       Tuple:
						SCREAMING_SNAKE_CASE						= ViTModel.from_pretrained('facebook/dino-vits8'		,					torch_dtype=torch.floataa		,					device_map='auto')
						SCREAMING_SNAKE_CASE						= self.default_image_processor

						SCREAMING_SNAKE_CASE						= prepare_img()
						SCREAMING_SNAKE_CASE						= image_processor(images=a		,					return_tensors='pt')
						SCREAMING_SNAKE_CASE						= inputs.pixel_values.to(a)

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












import os
from collections.abc import Iterator






def 				lowerCamelCase__				(_UpperCAmelCase = "."):
			for dir_path, dir_names, filenames in os.walk(_UpperCAmelCase):
						SCREAMING_SNAKE_CASE						= [d for d in dir_names if d != 'scripts' and d[0] not in '._']
						for filename in filenames:
									if filename == "__init__.py":
												continue
									if os.path.splitext(_UpperCAmelCase)[1] in (".py", ".ipynb"):
												yield os.path.join(_UpperCAmelCase							,       _UpperCAmelCase).lstrip('./')






def 				lowerCamelCase__				(_UpperCAmelCase):
			return F'''{i * '  '}*''' if i else "\n##"






def 				lowerCamelCase__				(_UpperCAmelCase							,       _UpperCAmelCase):
			SCREAMING_SNAKE_CASE						= old_path.split(os.sep)
			for i, new_part in enumerate(new_path.split(os.sep)):
						if (i + 1 > len(_UpperCAmelCase) or old_parts[i] != new_part) and new_part:
									print(F'''{md_prefix(_UpperCAmelCase)} {new_part.replace('_'							,       ' ').title()}''')
			return new_path






def 				lowerCamelCase__				(_UpperCAmelCase = "."):
			SCREAMING_SNAKE_CASE						= ''
			for filepath in sorted(good_file_paths(_UpperCAmelCase)):
						SCREAMING_SNAKE_CASE				,						SCREAMING_SNAKE_CASE						= os.path.split(_UpperCAmelCase)
						if filepath != old_path:
									SCREAMING_SNAKE_CASE						= print_path(_UpperCAmelCase							,       _UpperCAmelCase)
						SCREAMING_SNAKE_CASE						= (filepath.count(os.sep) + 1) if filepath else 0
						SCREAMING_SNAKE_CASE						= F'''{filepath}/{filename}'''.replace(' '							,       '%20')
						SCREAMING_SNAKE_CASE						= os.path.splitext(filename.replace('_'							,       ' ').title())[0]
						print(F'''{md_prefix(_UpperCAmelCase)} [{filename}]({url})''')


if __name__ == "__main__":
				print_directory_md('.')




