

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

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





    def       snake_case_				(      self):
         lowercase__    :   Union[str, Any]      =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

         lowercase__    :   Dict      =       None
         if self.use_labels:
              lowercase__    :   Tuple      =       ids_tensor([self.batch_size]						,   self.type_sequence_label_size)

         lowercase__    :   Optional[int]      =       self.get_config()

         return config, pixel_values, labels





    def       snake_case_				(      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=a						,   initializer_range=self.initializer_range						,   encoder_stride=self.encoder_stride						,   )





    def       snake_case_				(      self						,   a						,   a						,   a):
         lowercase__    :   List[Any]      =       ViTModel(config=a)
         model.to(a)
         model.eval()
         lowercase__    :   int      =       model(a)
         self.parent.assertEqual(result.last_hidden_state.shape						,   (self.batch_size, self.seq_length, self.hidden_size))





    def       snake_case_				(      self						,   a						,   a						,   a):
         lowercase__    :   Dict      =       ViTForMaskedImageModeling(config=a)
         model.to(a)
         model.eval()
         lowercase__    :   Any      =       model(a)
         self.parent.assertEqual(
             result.reconstruction.shape						,   (self.batch_size, self.num_channels, self.image_size, self.image_size))

         # test greyscale images
         lowercase__    :   str      =       1
         lowercase__    :   Dict      =       ViTForMaskedImageModeling(a)
         model.to(a)
         model.eval()

         lowercase__    :   Any      =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
         lowercase__    :   Optional[int]      =       model(a)
         self.parent.assertEqual(result.reconstruction.shape						,   (self.batch_size, 1, self.image_size, self.image_size))





    def       snake_case_				(      self						,   a						,   a						,   a):
         lowercase__    :   Tuple      =       self.type_sequence_label_size
         lowercase__    :   Optional[int]      =       ViTForImageClassification(a)
         model.to(a)
         model.eval()
         lowercase__    :   Optional[Any]      =       model(a						,   labels=a)
         self.parent.assertEqual(result.logits.shape						,   (self.batch_size, self.type_sequence_label_size))

         # test greyscale images
         lowercase__    :   Optional[int]      =       1
         lowercase__    :   int      =       ViTForImageClassification(a)
         model.to(a)
         model.eval()

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





    def       snake_case_				(      self):
         lowercase__    :   Union[str, Any]      =       self.prepare_config_and_inputs()
         (
             (
             lowercase__
         )      , (
             lowercase__
         )      , (
             lowercase__
         )      , 
         )    :   int      =       config_and_inputs
         lowercase__    :   List[Any]      =       {'pixel_values': pixel_values}
         return config, inputs_dict



@require_torch
class       SCREAMING_SNAKE_CASE__					(__snake_case		,       __snake_case		,       unittest.TestCase     ):
    __lowerCamelCase						:       Optional[Any]														=     (
        (
            ViTModel,
            ViTForImageClassification,
            ViTForMaskedImageModeling,
        )
        if is_torch_available()
        else ()
    )
    __lowerCamelCase						:       Dict														=     (
        {"""feature-extraction""": ViTModel, """image-classification""": ViTForImageClassification}
        if is_torch_available()
        else {}
    )
    __lowerCamelCase						:       Any														=     True

    __lowerCamelCase						:       Union[str, Any]														=     False
    __lowerCamelCase						:       List[str]														=     False
    __lowerCamelCase						:       Optional[Any]														=     False





    def       snake_case_				(      self):
         lowercase__    :   Tuple      =       ViTModelTester(self)
         lowercase__    :   Union[str, Any]      =       ConfigTester(self						,   config_class=a						,   has_text_modality=a						,   hidden_size=37)





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





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





    def       snake_case_				(      self):
         lowercase__      , lowercase__    :   Union[str, Any]      =       self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
              lowercase__    :   List[Any]      =       model_class(a)
              self.assertIsInstance(model.get_input_embeddings()						,   (nn.Module))
              lowercase__    :   Union[str, Any]      =       model.get_output_embeddings()
              self.assertTrue(x is None or isinstance(a						,   nn.Linear))





    def       snake_case_				(      self):
         lowercase__      , lowercase__    :   Optional[int]      =       self.model_tester.prepare_config_and_inputs_for_common()

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

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





    def       snake_case_				(      self):
         lowercase__    :   str      =       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*a)





    def       snake_case_				(      self):
         lowercase__    :   Dict      =       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_image_modeling(*a)





    def       snake_case_				(      self):
         lowercase__    :   str      =       self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*a)





    @slow
    def       snake_case_				(      self):
         for model_name in VIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              lowercase__    :   Optional[int]      =       ViTModel.from_pretrained(a)
              self.assertIsNotNone(a)



def 	snake_case__			(    ):



     '''simple docstring'''





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


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





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





    @slow
    def       snake_case_				(      self):
         lowercase__    :   Optional[int]      =       ViTForImageClassification.from_pretrained('google/vit-base-patch16-224').to(a)

         lowercase__    :   Tuple      =       self.default_image_processor
         lowercase__    :   List[str]      =       prepare_img()
         lowercase__    :   Dict      =       image_processor(images=a						,   return_tensors='pt').to(a)

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

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

         lowercase__    :   Any      =       torch.tensor([-0.2_744, 0.8_215, -0.0_836]).to(a)

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





    @slow
    def       snake_case_				(      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.
         lowercase__    :   Tuple      =       ViTModel.from_pretrained('facebook/dino-vits8').to(a)

         lowercase__    :   List[str]      =       ViTImageProcessor.from_pretrained('facebook/dino-vits8'						,   size=480)
         lowercase__    :   Any      =       prepare_img()
         lowercase__    :   List[Any]      =       image_processor(images=a						,   return_tensors='pt')
         lowercase__    :   Optional[Any]      =       inputs.pixel_values.to(a)

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

         # verify the logits
         lowercase__    :   List[str]      =       torch.Size((1, 3601, 384))
         self.assertEqual(outputs.last_hidden_state.shape						,   a)

         lowercase__    :   Tuple      =       torch.tensor(
             [[4.2_340, 4.3_906, -6.6_692], [4.5_463, 1.8_928, -6.7_257], [4.4_429, 0.8_496, -5.8_585]]).to(a)

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





    @slow
    @require_accelerate
    @require_torch_gpu
    def       snake_case_				(      self):
         lowercase__    :   str      =       ViTModel.from_pretrained('facebook/dino-vits8'						,   torch_dtype=torch.floataa						,   device_map='auto')
         lowercase__    :   Optional[int]      =       self.default_image_processor

         lowercase__    :   int      =       prepare_img()
         lowercase__    :   Optional[Any]      =       image_processor(images=a						,   return_tensors='pt')
         lowercase__    :   str      =       inputs.pixel_values.to(a)

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




from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


snake_case_			  = logging.get_logger(__name__)

snake_case_			  = {
    '''distilbert-base-uncased''': '''https://huggingface.co/distilbert-base-uncased/resolve/main/config.json''',
    '''distilbert-base-uncased-distilled-squad''': (
        '''https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json'''
    ),
    '''distilbert-base-cased''': '''https://huggingface.co/distilbert-base-cased/resolve/main/config.json''',
    '''distilbert-base-cased-distilled-squad''': (
        '''https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json'''
    ),
    '''distilbert-base-german-cased''': '''https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json''',
    '''distilbert-base-multilingual-cased''': (
        '''https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json'''
    ),
    '''distilbert-base-uncased-finetuned-sst-2-english''': (
        '''https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json'''
    ),
}


class       SCREAMING_SNAKE_CASE__					(__snake_case     ):
    __lowerCamelCase						:       Any														=     """distilbert"""
    __lowerCamelCase						:       List[Any]														=     {
        """hidden_size""": """dim""",
        """num_attention_heads""": """n_heads""",
        """num_hidden_layers""": """n_layers""",
    }





    def __init__(      self						,   a=3_0522						,   a=512						,   a=False						,   a=6						,   a=12						,   a=768						,   a=4 * 768						,   a=0.1						,   a=0.1						,   a="gelu"						,   a=0.02						,   a=0.1						,   a=0.2						,   a=0						,   **a						,   ):
         lowercase__    :   int      =       vocab_size
         lowercase__    :   int      =       max_position_embeddings
         lowercase__    :   List[str]      =       sinusoidal_pos_embds
         lowercase__    :   Tuple      =       n_layers
         lowercase__    :   Optional[int]      =       n_heads
         lowercase__    :   List[Any]      =       dim
         lowercase__    :   Any      =       hidden_dim
         lowercase__    :   Union[str, Any]      =       dropout
         lowercase__    :   Optional[int]      =       attention_dropout
         lowercase__    :   List[Any]      =       activation
         lowercase__    :   Union[str, Any]      =       initializer_range
         lowercase__    :   Dict      =       qa_dropout
         lowercase__    :   Tuple      =       seq_classif_dropout
         super().__init__(**a						,   pad_token_id=a)



class       SCREAMING_SNAKE_CASE__					(__snake_case     ):





    @property
    def       snake_case_				(      self):
         if self.task == "multiple-choice":
              lowercase__    :   List[str]      =       {0: 'batch', 1: 'choice', 2: 'sequence'}
         else:
              lowercase__    :   Tuple      =       {0: 'batch', 1: 'sequence'}
         return OrderedDict(
             [
                 ('input_ids', dynamic_axis),
                 ('attention_mask', dynamic_axis),
             ])

