






"""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__   :






    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      ,):
          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   ):
          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   ):
          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=_lowerCAmelCase      ,initializer_range=self.initializer_range      ,encoder_stride=self.encoder_stride      ,)






    def 							_UpperCamelCase				(					self      ,A      ,A      ,A   ):
          UpperCAmelCase							     =			ViTModel(config=_lowerCAmelCase   )
          model.to(_lowerCAmelCase   )
          model.eval()
          UpperCAmelCase							     =			model(_lowerCAmelCase   )
          self.parent.assertEqual(result.last_hidden_state.shape      ,(self.batch_size, self.seq_length, self.hidden_size)   )






    def 							_UpperCamelCase				(					self      ,A      ,A      ,A   ):
          UpperCAmelCase							     =			ViTForMaskedImageModeling(config=_lowerCAmelCase   )
          model.to(_lowerCAmelCase   )
          model.eval()
          UpperCAmelCase							     =			model(_lowerCAmelCase   )
          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(_lowerCAmelCase   )
          model.to(_lowerCAmelCase   )
          model.eval()

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






    def 							_UpperCamelCase				(					self      ,A      ,A      ,A   ):
          UpperCAmelCase							     =			self.type_sequence_label_size
          UpperCAmelCase							     =			ViTForImageClassification(_lowerCAmelCase   )
          model.to(_lowerCAmelCase   )
          model.eval()
          UpperCAmelCase							     =			model(_lowerCAmelCase      ,labels=_lowerCAmelCase   )
          self.parent.assertEqual(result.logits.shape      ,(self.batch_size, self.type_sequence_label_size)   )

          # test greyscale images
          UpperCAmelCase							     =			1
          UpperCAmelCase							     =			ViTForImageClassification(_lowerCAmelCase   )
          model.to(_lowerCAmelCase   )
          model.eval()

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






    def 							_UpperCamelCase				(					self   ):
          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__   (			snake_case       ,	snake_case       ,	unittest.TestCase					):
    SCREAMING_SNAKE_CASE						=     (
        (
            ViTModel,
            ViTForImageClassification,
            ViTForMaskedImageModeling,
        )
        if is_torch_available()
        else ()
    )
    SCREAMING_SNAKE_CASE						=     (
        {"feature-extraction": ViTModel, "image-classification": ViTForImageClassification}
        if is_torch_available()
        else {}
    )
    SCREAMING_SNAKE_CASE						=     True

    SCREAMING_SNAKE_CASE						=     False
    SCREAMING_SNAKE_CASE						=     False
    SCREAMING_SNAKE_CASE						=     False






    def 							_UpperCamelCase				(					self   ):
          UpperCAmelCase							     =			ViTModelTester(self   )
          UpperCAmelCase							     =			ConfigTester(self      ,config_class=_lowerCAmelCase      ,has_text_modality=_lowerCAmelCase      ,hidden_size=37   )






    def 							_UpperCamelCase				(					self   ):
          self.config_tester.run_common_tests()






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






    def 							_UpperCamelCase				(					self   ):
          UpperCAmelCase     ,   UpperCAmelCase							     =			self.model_tester.prepare_config_and_inputs_for_common()

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






    def 							_UpperCamelCase				(					self   ):
          UpperCAmelCase     ,   UpperCAmelCase							     =			self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                UpperCAmelCase							     =			model_class(_lowerCAmelCase   )
                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]      ,_lowerCAmelCase   )






    def 							_UpperCamelCase				(					self   ):
          UpperCAmelCase							     =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*_lowerCAmelCase   )






    def 							_UpperCamelCase				(					self   ):
          UpperCAmelCase							     =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_image_modeling(*_lowerCAmelCase   )






    def 							_UpperCamelCase				(					self   ):
          UpperCAmelCase							     =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase   )






    @slow
    def 							_UpperCamelCase				(					self   ):
          for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                UpperCAmelCase							     =			ViTModel.from_pretrained(_lowerCAmelCase   )
                self.assertIsNotNone(_lowerCAmelCase   )







def _a					(						):







      """simple docstring"""






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







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






    @cached_property
    def 							_UpperCamelCase				(					self   ):
          return ViTImageProcessor.from_pretrained("""google/vit-base-patch16-224"""   ) if is_vision_available() else None






    @slow
    def 							_UpperCamelCase				(					self   ):
          UpperCAmelCase							     =			ViTForImageClassification.from_pretrained("""google/vit-base-patch16-224"""   ).to(_lowerCAmelCase   )

          UpperCAmelCase							     =			self.default_image_processor
          UpperCAmelCase							     =			prepare_img()
          UpperCAmelCase							     =			image_processor(images=_lowerCAmelCase      ,return_tensors="""pt"""   ).to(_lowerCAmelCase   )

          # forward pass
          with torch.no_grad():
                UpperCAmelCase							     =			model(**_lowerCAmelCase   )

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

          UpperCAmelCase							     =			torch.tensor([-0.2744, 0.8215, -0.0836]   ).to(_lowerCAmelCase   )

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






    @slow
    def 							_UpperCamelCase				(					self   ):
          # 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(_lowerCAmelCase   )

          UpperCAmelCase							     =			ViTImageProcessor.from_pretrained("""facebook/dino-vits8"""      ,size=480   )
          UpperCAmelCase							     =			prepare_img()
          UpperCAmelCase							     =			image_processor(images=_lowerCAmelCase      ,return_tensors="""pt"""   )
          UpperCAmelCase							     =			inputs.pixel_values.to(_lowerCAmelCase   )

          # forward pass
          with torch.no_grad():
                UpperCAmelCase							     =			model(_lowerCAmelCase      ,interpolate_pos_encoding=_lowerCAmelCase   )

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

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

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






    @slow
    @require_accelerate
    @require_torch_gpu
    def 							_UpperCamelCase				(					self   ):
          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=_lowerCAmelCase      ,return_tensors="""pt"""   )
          UpperCAmelCase							     =			inputs.pixel_values.to(_lowerCAmelCase   )

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










'''simple docstring'''


def 						__a(SCREAMING_SNAKE_CASE_	:       int		,							SCREAMING_SNAKE_CASE_	:       int     ):



    '''simple docstring'''





    return numa ^ numa < 0


if __name__ == "__main__":
       import doctest

       doctest.testmod()


