



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



   def __init__(       self    :	Optional[Any]							,       __lowerCamelCase    :	str							,       __lowerCamelCase    :	Any=1_3							,       __lowerCamelCase    :	int=3_0							,       __lowerCamelCase    :	Any=2							,       __lowerCamelCase    :	Any=3							,       __lowerCamelCase    :	List[str]=True							,       __lowerCamelCase    :	List[Any]=True							,       __lowerCamelCase    :	Tuple=3_2							,       __lowerCamelCase    :	Optional[Any]=5							,       __lowerCamelCase    :	Optional[int]=4							,       __lowerCamelCase    :	Optional[Any]=3_7							,       __lowerCamelCase    :	Optional[Any]="gelu"							,       __lowerCamelCase    :	Dict=0.1							,       __lowerCamelCase    :	List[Any]=0.1							,       __lowerCamelCase    :	Tuple=1_0							,       __lowerCamelCase    :	List[str]=0.0_2							,       __lowerCamelCase    :	Optional[Any]=None							,       __lowerCamelCase    :	List[str]=2							,       ):



     """simple docstring"""
     _snake_case   	=      parent
     _snake_case   	=      batch_size
     _snake_case   	=      image_size
     _snake_case   	=      patch_size
     _snake_case   	=      num_channels
     _snake_case   	=      is_training
     _snake_case   	=      use_labels
     _snake_case   	=      hidden_size
     _snake_case   	=      num_hidden_layers
     _snake_case   	=      num_attention_heads
     _snake_case   	=      intermediate_size
     _snake_case   	=      hidden_act
     _snake_case   	=      hidden_dropout_prob
     _snake_case   	=      attention_probs_dropout_prob
     _snake_case   	=      type_sequence_label_size
     _snake_case   	=      initializer_range
     _snake_case   	=      scope
     _snake_case   	=      encoder_stride

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



   def    __UpperCAmelCase    (       self    :	Union[str, Any]				):



     """simple docstring"""
     _snake_case   	=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

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

     _snake_case   	=      self.get_config()

     return config, pixel_values, labels



   def    __UpperCAmelCase    (       self    :	Tuple				):



     """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=__lowerCamelCase							,       initializer_range=self.initializer_range							,       encoder_stride=self.encoder_stride							,       )



   def    __UpperCAmelCase    (       self    :	List[Any]							,       __lowerCamelCase    :	Optional[int]							,       __lowerCamelCase    :	Tuple							,       __lowerCamelCase    :	Tuple				):



     """simple docstring"""
     _snake_case   	=      ViTModel(config=__lowerCamelCase				)
     model.to(__lowerCamelCase				)
     model.eval()
     _snake_case   	=      model(__lowerCamelCase				)
     self.parent.assertEqual(result.last_hidden_state.shape							,       (self.batch_size, self.seq_length, self.hidden_size)				)



   def    __UpperCAmelCase    (       self    :	Any							,       __lowerCamelCase    :	Any							,       __lowerCamelCase    :	Tuple							,       __lowerCamelCase    :	str				):



     """simple docstring"""
     _snake_case   	=      ViTForMaskedImageModeling(config=__lowerCamelCase				)
     model.to(__lowerCamelCase				)
     model.eval()
     _snake_case   	=      model(__lowerCamelCase				)
     self.parent.assertEqual(
         result.reconstruction.shape							,       (self.batch_size, self.num_channels, self.image_size, self.image_size)				)

     # test greyscale images
     _snake_case   	=      1
     _snake_case   	=      ViTForMaskedImageModeling(__lowerCamelCase				)
     model.to(__lowerCamelCase				)
     model.eval()

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



   def    __UpperCAmelCase    (       self    :	Dict							,       __lowerCamelCase    :	Optional[Any]							,       __lowerCamelCase    :	Dict							,       __lowerCamelCase    :	Tuple				):



     """simple docstring"""
     _snake_case   	=      self.type_sequence_label_size
     _snake_case   	=      ViTForImageClassification(__lowerCamelCase				)
     model.to(__lowerCamelCase				)
     model.eval()
     _snake_case   	=      model(__lowerCamelCase							,       labels=__lowerCamelCase				)
     self.parent.assertEqual(result.logits.shape							,       (self.batch_size, self.type_sequence_label_size)				)

     # test greyscale images
     _snake_case   	=      1
     _snake_case   	=      ViTForImageClassification(__lowerCamelCase				)
     model.to(__lowerCamelCase				)
     model.eval()

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





   def    __UpperCAmelCase    (       self    :	Dict				):



     """simple docstring"""
     _snake_case   	=      self.prepare_config_and_inputs()
     (
         (
         _snake_case
     )	,     (
         _snake_case
     )	,     (
         _snake_case
     )	,     
     )   	=      config_and_inputs
     _snake_case   	=      {'''pixel_values''': pixel_values}
     return config, inputs_dict



@require_torch
class     UpperCAmelCase    (	__SCREAMING_SNAKE_CASE,__SCREAMING_SNAKE_CASE,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__		:	Optional[int]           =  True

   A__		:	str           =  False
   A__		:	Any           =  False
   A__		:	List[Any]           =  False



   def    __UpperCAmelCase    (       self    :	List[str]				):



     """simple docstring"""
     _snake_case   	=      ViTModelTester(self				)
     _snake_case   	=      ConfigTester(self							,       config_class=__lowerCamelCase							,       has_text_modality=__lowerCamelCase							,       hidden_size=3_7				)



   def    __UpperCAmelCase    (       self    :	Optional[Any]				):



     """simple docstring"""
     self.config_tester.run_common_tests()



   @unittest.skip(reason='''ViT does not use inputs_embeds'''				)
   def    __UpperCAmelCase    (       self    :	Union[str, Any]				):



     """simple docstring"""
     pass



   def    __UpperCAmelCase    (       self    :	Dict				):



     """simple docstring"""
     _snake_case	,     _snake_case   	=      self.model_tester.prepare_config_and_inputs_for_common()

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



   def    __UpperCAmelCase    (       self    :	Optional[int]				):



     """simple docstring"""
     _snake_case	,     _snake_case   	=      self.model_tester.prepare_config_and_inputs_for_common()

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

       _snake_case   	=      ['''pixel_values''']
       self.assertListEqual(arg_names[:1]							,       __lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	str				):



     """simple docstring"""
     _snake_case   	=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*__lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	Optional[int]				):



     """simple docstring"""
     _snake_case   	=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_image_modeling(*__lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	str				):



     """simple docstring"""
     _snake_case   	=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase				)





   @slow
   def    __UpperCAmelCase    (       self    :	Optional[int]				):



     """simple docstring"""
     for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       _snake_case   	=      ViTModel.from_pretrained(__lowerCamelCase				)
       self.assertIsNotNone(__lowerCamelCase				)




def 			snake_case		(		)						->				Any:
  _snake_case   	=      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
  return image







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



   @cached_property
   def    __UpperCAmelCase    (       self    :	Union[str, Any]				):



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



   @slow
   def    __UpperCAmelCase    (       self    :	Optional[Any]				):



     """simple docstring"""
     _snake_case   	=      ViTForImageClassification.from_pretrained('''google/vit-base-patch16-224'''				).to(__lowerCamelCase				)

     _snake_case   	=      self.default_image_processor
     _snake_case   	=      prepare_img()
     _snake_case   	=      image_processor(images=__lowerCamelCase							,       return_tensors='''pt'''				).to(__lowerCamelCase				)

     # forward pass
     with torch.no_grad():
       _snake_case   	=      model(**__lowerCamelCase				)

     # verify the logits
     _snake_case   	=      torch.Size((1, 1_0_0_0)				)
     self.assertEqual(outputs.logits.shape							,       __lowerCamelCase				)

     _snake_case   	=      torch.tensor([-0.2_7_4_4, 0.8_2_1_5, -0.0_8_3_6]				).to(__lowerCamelCase				)

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



   @slow
   def    __UpperCAmelCase    (       self    :	Union[str, 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.
     _snake_case   	=      ViTModel.from_pretrained('''facebook/dino-vits8'''				).to(__lowerCamelCase				)

     _snake_case   	=      ViTImageProcessor.from_pretrained('''facebook/dino-vits8'''							,       size=4_8_0				)
     _snake_case   	=      prepare_img()
     _snake_case   	=      image_processor(images=__lowerCamelCase							,       return_tensors='''pt'''				)
     _snake_case   	=      inputs.pixel_values.to(__lowerCamelCase				)

     # forward pass
     with torch.no_grad():
       _snake_case   	=      model(__lowerCamelCase							,       interpolate_pos_encoding=__lowerCamelCase				)

     # verify the logits
     _snake_case   	=      torch.Size((1, 3_6_0_1, 3_8_4)				)
     self.assertEqual(outputs.last_hidden_state.shape							,       __lowerCamelCase				)

     _snake_case   	=      torch.tensor(
         [[4.2_3_4_0, 4.3_9_0_6, -6.6_6_9_2], [4.5_4_6_3, 1.8_9_2_8, -6.7_2_5_7], [4.4_4_2_9, 0.8_4_9_6, -5.8_5_8_5]]				).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    :	Union[str, Any]				):



     """simple docstring"""
     _snake_case   	=      ViTModel.from_pretrained('''facebook/dino-vits8'''							,       torch_dtype=torch.floataa							,       device_map='''auto'''				)
     _snake_case   	=      self.default_image_processor

     _snake_case   	=      prepare_img()
     _snake_case   	=      image_processor(images=__lowerCamelCase							,       return_tensors='''pt'''				)
     _snake_case   	=      inputs.pixel_values.to(__lowerCamelCase				)

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












'''simple docstring'''


from __future__ import annotations


def 						__a(SCREAMING_SNAKE_CASE_	:       list     ):



    '''simple docstring'''





    if not nums:
        raise ValueError("List is empty"     )
    return sum(SCREAMING_SNAKE_CASE_     ) / len(SCREAMING_SNAKE_CASE_     )


if __name__ == "__main__":
       import doctest

       doctest.testmod()


