





'''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     __lowerCAmelCase					:






       """simple docstring"""





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

            '''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 snake_case__						(							self       :		Optional[int]					)   ->					Optional[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 snake_case__						(							self       :		List[Any]					)   ->					List[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 snake_case__						(							self       :		Dict	,			lowerCAmelCase__       :		Tuple	,			lowerCAmelCase__       :		List[Any]	,			lowerCAmelCase__       :		Dict					)   ->					Tuple:

            '''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 snake_case__						(							self       :		Union[str, Any]	,			lowerCAmelCase__       :		str	,			lowerCAmelCase__       :		Union[str, Any]	,			lowerCAmelCase__       :		Optional[Any]					)   ->					Union[str, 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 snake_case__						(							self       :		Tuple	,			lowerCAmelCase__       :		str	,			lowerCAmelCase__       :		List[Any]	,			lowerCAmelCase__       :		List[Any]					)   ->					Dict:

            '''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 snake_case__						(							self       :		Optional[Any]					)   ->					Tuple:

            '''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     __lowerCAmelCase					(   lowerCamelCase__						,						lowerCamelCase__						,						unittest.TestCase			):






       """simple docstring"""
       _snake_case     :					Tuple						    =   (
           (
               ViTModel,
               ViTForImageClassification,
               ViTForMaskedImageModeling,
           )
           if is_torch_available()
           else ()
       )
       _snake_case     :					int						    =   (
           {'feature-extraction': ViTModel, 'image-classification': ViTForImageClassification}
           if is_torch_available()
           else {}
       )
       _snake_case     :					Optional[int]						    =   True

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





       def snake_case__						(							self       :		Tuple					)   ->					str:

            '''simple docstring'''




            _UpperCamelCase       =	ViTModelTester(self					)
            _UpperCamelCase       =	ConfigTester(self	,			config_class=lowercase__	,			has_text_modality=lowercase__	,			hidden_size=37					)





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

            '''simple docstring'''




            self.config_tester.run_common_tests()





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

            '''simple docstring'''




            pass





       def snake_case__						(							self       :		int					)   ->					List[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 snake_case__						(							self       :		Optional[Any]					)   ->					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 snake_case__						(							self       :		int					)   ->					str:

            '''simple docstring'''




            _UpperCamelCase       =	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*lowercase__					)





       def snake_case__						(							self       :		Optional[int]					)   ->					Tuple:

            '''simple docstring'''




            _UpperCamelCase       =	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_masked_image_modeling(*lowercase__					)





       def snake_case__						(							self       :		List[Any]					)   ->					Union[str, Any]:

            '''simple docstring'''




            _UpperCamelCase       =	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*lowercase__					)







       @slow
       def snake_case__						(							self       :		str					)   ->					Optional[int]:

            '''simple docstring'''




            for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 _UpperCamelCase       =	ViTModel.from_pretrained(lowercase__					)
                 self.assertIsNotNone(lowercase__					)







def  a__    (    )		->		Any:





     """simple docstring"""

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




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






       """simple docstring"""





       @cached_property
       def snake_case__						(							self       :		Optional[Any]					)   ->					Union[str, Any]:

            '''simple docstring'''




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





       @slow
       def snake_case__						(							self       :		str					)   ->					Dict:

            '''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, 1000)					)
            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 snake_case__						(							self       :		Dict					)   ->					Any:

            '''simple docstring'''




            _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, 3601, 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 snake_case__						(							self       :		Union[str, Any]					)   ->					List[str]:

            '''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__					)










'''simple docstring'''

import unittest

from transformers.utils.backbone_utils import (
    BackboneMixin,
    get_aligned_output_features_output_indices,
    verify_out_features_out_indices,
)
class     __lowerCAmelCase					(   unittest.TestCase			):






       """simple docstring"""





       def snake_case__						(							self       :		Optional[Any]					)   ->					List[str]:

            '''simple docstring'''




            _UpperCamelCase       =	['''a''', '''b''', '''c''']

            # Defaults to last layer if both are None
            _UpperCamelCase   ,    _UpperCamelCase       =	get_aligned_output_features_output_indices(lowerCAmelCase__	,			lowerCAmelCase__	,			lowerCAmelCase__					)
            self.assertEqual(lowerCAmelCase__	,			['''c''']					)
            self.assertEqual(lowerCAmelCase__	,			[2]					)

            # Out indices set to match out features
            _UpperCamelCase   ,    _UpperCamelCase       =	get_aligned_output_features_output_indices(['''a''', '''c''']	,			lowerCAmelCase__	,			lowerCAmelCase__					)
            self.assertEqual(lowerCAmelCase__	,			['''a''', '''c''']					)
            self.assertEqual(lowerCAmelCase__	,			[0, 2]					)

            # Out features set to match out indices
            _UpperCamelCase   ,    _UpperCamelCase       =	get_aligned_output_features_output_indices(lowerCAmelCase__	,			[0, 2]	,			lowerCAmelCase__					)
            self.assertEqual(lowerCAmelCase__	,			['''a''', '''c''']					)
            self.assertEqual(lowerCAmelCase__	,			[0, 2]					)

            # Out features selected from negative indices
            _UpperCamelCase   ,    _UpperCamelCase       =	get_aligned_output_features_output_indices(lowerCAmelCase__	,			[-3, -1]	,			lowerCAmelCase__					)
            self.assertEqual(lowerCAmelCase__	,			['''a''', '''c''']					)
            self.assertEqual(lowerCAmelCase__	,			[-3, -1]					)





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

            '''simple docstring'''




            with self.assertRaises(lowerCAmelCase__					):
                 verify_out_features_out_indices(['''a''', '''b''']	,			(0, 1)	,			lowerCAmelCase__					)

            # Out features must be a list
            with self.assertRaises(lowerCAmelCase__					):
                 verify_out_features_out_indices(('''a''', '''b''')	,			(0, 1)	,			['''a''', '''b''']					)

            # Out features must be a subset of stage names
            with self.assertRaises(lowerCAmelCase__					):
                 verify_out_features_out_indices(['''a''', '''b''']	,			(0, 1)	,			['''a''']					)

            # Out indices must be a list or tuple
            with self.assertRaises(lowerCAmelCase__					):
                 verify_out_features_out_indices(lowerCAmelCase__	,			0	,			['''a''', '''b''']					)

            # Out indices must be a subset of stage names
            with self.assertRaises(lowerCAmelCase__					):
                 verify_out_features_out_indices(lowerCAmelCase__	,			(0, 1)	,			['''a''']					)

            # Out features and out indices must be the same length
            with self.assertRaises(lowerCAmelCase__					):
                 verify_out_features_out_indices(['''a''', '''b''']	,			(0,)	,			['''a''', '''b''', '''c''']					)

            # Out features should match out indices
            with self.assertRaises(lowerCAmelCase__					):
                 verify_out_features_out_indices(['''a''', '''b''']	,			(0, 2)	,			['''a''', '''b''', '''c''']					)

            # Out features and out indices should be in order
            with self.assertRaises(lowerCAmelCase__					):
                 verify_out_features_out_indices(['''b''', '''a''']	,			(0, 1)	,			['''a''', '''b''']					)

            # Check passes with valid inputs
            verify_out_features_out_indices(['''a''', '''b''', '''d''']	,			(0, 1, -1)	,			['''a''', '''b''', '''c''', '''d''']					)







       def snake_case__						(							self       :		Any					)   ->					str:

            '''simple docstring'''




            _UpperCamelCase       =	BackboneMixin()

            _UpperCamelCase       =	['''a''', '''b''', '''c''']
            _UpperCamelCase       =	['''a''', '''c''']
            _UpperCamelCase       =	[0, 2]

            # Check that the output features and indices are set correctly
            self.assertEqual(backbone.out_features	,			['''a''', '''c''']					)
            self.assertEqual(backbone.out_indices	,			[0, 2]					)

            # Check out features and indices are updated correctly
            _UpperCamelCase       =	['''a''', '''b''']
            self.assertEqual(backbone.out_features	,			['''a''', '''b''']					)
            self.assertEqual(backbone.out_indices	,			[0, 1]					)

            _UpperCamelCase       =	[-3, -1]
            self.assertEqual(backbone.out_features	,			['''a''', '''c''']					)
            self.assertEqual(backbone.out_indices	,			[-3, -1]					)



