



import inspect
import unittest
import warnings

from transformers import DeiTConfig
from transformers.models.auto import get_values
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 (
     MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
     MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
     MODEL_MAPPING,
     DeiTForImageClassification,
     DeiTForImageClassificationWithTeacher,
     DeiTForMaskedImageModeling,
     DeiTModel,
 )
 from transformers.models.deit.modeling_deit import DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import DeiTImageProcessor






class      UpperCAmelCase_  :



      def __init__(					self					:     int					,     A					:     int					,     A					:     str=1_3					,     A					:     Optional[int]=3_0					,     A					:     int=2					,     A					:     Optional[Any]=3					,     A					:     List[str]=True					,     A					:     Tuple=True					,     A					:     Optional[int]=3_2					,     A					:     Union[str, Any]=5					,     A					:     Tuple=4					,     A					:     List[Any]=3_7					,     A					:     int="gelu"					,     A					:     Optional[Any]=0.1					,     A					:     Any=0.1					,     A					:     Optional[Any]=1_0					,     A					:     List[Any]=0.02					,     A					:     str=3					,     A					:     Union[str, Any]=None					,     A					:     int=2					,     ):
           _UpperCAmelCase      :     List[Any]        =   parent
           _UpperCAmelCase      :     List[Any]        =   batch_size
           _UpperCAmelCase      :     Optional[Any]        =   image_size
           _UpperCAmelCase      :     List[Any]        =   patch_size
           _UpperCAmelCase      :     Dict        =   num_channels
           _UpperCAmelCase      :     int        =   is_training
           _UpperCAmelCase      :     str        =   use_labels
           _UpperCAmelCase      :     List[str]        =   hidden_size
           _UpperCAmelCase      :     Tuple        =   num_hidden_layers
           _UpperCAmelCase      :     List[str]        =   num_attention_heads
           _UpperCAmelCase      :     Any        =   intermediate_size
           _UpperCAmelCase      :     Optional[int]        =   hidden_act
           _UpperCAmelCase      :     str        =   hidden_dropout_prob
           _UpperCAmelCase      :     List[Any]        =   attention_probs_dropout_prob
           _UpperCAmelCase      :     List[str]        =   type_sequence_label_size
           _UpperCAmelCase      :     List[Any]        =   initializer_range
           _UpperCAmelCase      :     List[Any]        =   scope
           _UpperCAmelCase      :     Union[str, Any]        =   encoder_stride

           # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
           _UpperCAmelCase      :     Union[str, Any]        =   (image_size // patch_size) ** 2
           _UpperCAmelCase      :     Optional[int]        =   num_patches + 2



      def 						snake_case_ (					self					:     Dict						):
           _UpperCAmelCase      :     List[Any]        =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

           _UpperCAmelCase      :     List[str]        =   None
           if self.use_labels:
                _UpperCAmelCase      :     Dict        =   ids_tensor([self.batch_size]					,     self.type_sequence_label_size						)

           _UpperCAmelCase      :     Tuple        =   self.get_config()

           return config, pixel_values, labels



      def 						snake_case_ (					self					:     Any						):
           return DeiTConfig(
               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=snake_case__					,     initializer_range=self.initializer_range					,     encoder_stride=self.encoder_stride					,     )



      def 						snake_case_ (					self					:     List[str]					,     A					:     Union[str, Any]					,     A					:     List[Any]					,     A					:     str						):
           _UpperCAmelCase      :     Union[str, Any]        =   DeiTModel(config=snake_case__						)
           model.to(snake_case__						)
           model.eval()
           _UpperCAmelCase      :     Any        =   model(snake_case__						)
           self.parent.assertEqual(result.last_hidden_state.shape					,     (self.batch_size, self.seq_length, self.hidden_size)						)



      def 						snake_case_ (					self					:     List[str]					,     A					:     Any					,     A					:     int					,     A					:     Union[str, Any]						):
           _UpperCAmelCase      :     int        =   DeiTForMaskedImageModeling(config=snake_case__						)
           model.to(snake_case__						)
           model.eval()
           _UpperCAmelCase      :     Optional[int]        =   model(snake_case__						)
           self.parent.assertEqual(
               result.reconstruction.shape					,     (self.batch_size, self.num_channels, self.image_size, self.image_size)						)

           # test greyscale images
           _UpperCAmelCase      :     Any        =   1
           _UpperCAmelCase      :     Optional[int]        =   DeiTForMaskedImageModeling(snake_case__						)
           model.to(snake_case__						)
           model.eval()

           _UpperCAmelCase      :     Any        =   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
           _UpperCAmelCase      :     List[Any]        =   model(snake_case__						)
           self.parent.assertEqual(result.reconstruction.shape					,     (self.batch_size, 1, self.image_size, self.image_size)						)



      def 						snake_case_ (					self					:     Union[str, Any]					,     A					:     int					,     A					:     Union[str, Any]					,     A					:     Dict						):
           _UpperCAmelCase      :     Optional[int]        =   self.type_sequence_label_size
           _UpperCAmelCase      :     List[str]        =   DeiTForImageClassification(snake_case__						)
           model.to(snake_case__						)
           model.eval()
           _UpperCAmelCase      :     Any        =   model(snake_case__					,     labels=snake_case__						)
           self.parent.assertEqual(result.logits.shape					,     (self.batch_size, self.type_sequence_label_size)						)

           # test greyscale images
           _UpperCAmelCase      :     str        =   1
           _UpperCAmelCase      :     Any        =   DeiTForImageClassification(snake_case__						)
           model.to(snake_case__						)
           model.eval()

           _UpperCAmelCase      :     Tuple        =   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
           _UpperCAmelCase      :     Optional[int]        =   model(snake_case__					,     labels=snake_case__						)
           self.parent.assertEqual(result.logits.shape					,     (self.batch_size, self.type_sequence_label_size)						)



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







@require_torch
class      UpperCAmelCase_  (    SCREAMING_SNAKE_CASE_	,      SCREAMING_SNAKE_CASE_	,      unittest.TestCase						):
      __SCREAMING_SNAKE_CASE						:       str								=			(
          (
              DeiTModel,
              DeiTForImageClassification,
              DeiTForImageClassificationWithTeacher,
              DeiTForMaskedImageModeling,
          )
          if is_torch_available()
          else ()
      )
      __SCREAMING_SNAKE_CASE						:       Any								=			(
          {
              'feature-extraction': DeiTModel,
              'image-classification': (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
          }
          if is_torch_available()
          else {}
      )

      __SCREAMING_SNAKE_CASE						:       Optional[Any]								=			False
      __SCREAMING_SNAKE_CASE						:       str								=			False
      __SCREAMING_SNAKE_CASE						:       Optional[int]								=			False



      def 						snake_case_ (					self					:     int						):
           _UpperCAmelCase      :     Dict        =   DeiTModelTester(self						)
           _UpperCAmelCase      :     str        =   ConfigTester(self					,     config_class=snake_case__					,     has_text_modality=snake_case__					,     hidden_size=3_7						)



      def 						snake_case_ (					self					:     Optional[Any]						):
           self.config_tester.run_common_tests()



      @unittest.skip(reason="DeiT does not use inputs_embeds"						)
      def 						snake_case_ (					self					:     Optional[int]						):
           pass



      def 						snake_case_ (					self					:     Dict						):
           _UpperCAmelCase      :     Dict        =   self.model_tester.prepare_config_and_inputs_for_common()

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



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

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

                _UpperCAmelCase      :     Any        =   ['pixel_values']
                self.assertListEqual(arg_names[:1]					,     snake_case__						)



      def 						snake_case_ (					self					:     Any						):
           _UpperCAmelCase      :     Optional[Any]        =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*snake_case__						)



      def 						snake_case_ (					self					:     List[Any]						):
           _UpperCAmelCase      :     Tuple        =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_image_modeling(*snake_case__						)



      def 						snake_case_ (					self					:     Union[str, Any]						):
           _UpperCAmelCase      :     List[Any]        =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*snake_case__						)



      def 						snake_case_ (					self					:     Dict					,     A					:     Dict					,     A					:     List[Any]					,     A					:     int=False						):
           _UpperCAmelCase      :     int        =   super()._prepare_for_class(snake_case__					,     snake_case__					,     return_labels=snake_case__						)

           if return_labels:
                if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
                     del inputs_dict["labels"]

           return inputs_dict



      def 						snake_case_ (					self					:     str						):

           if not self.model_tester.is_training:
                return

           _UpperCAmelCase      :     List[Any]        =   self.model_tester.prepare_config_and_inputs_for_common()
           _UpperCAmelCase      :     List[Any]        =   True

           for model_class in self.all_model_classes:
                # DeiTForImageClassificationWithTeacher supports inference-only
                if (
                    model_class in get_values(snake_case__						)
                    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
                ):
                     continue
                _UpperCAmelCase      :     Tuple        =   model_class(snake_case__						)
                model.to(snake_case__						)
                model.train()
                _UpperCAmelCase      :     Optional[int]        =   self._prepare_for_class(snake_case__					,     snake_case__					,     return_labels=snake_case__						)
                _UpperCAmelCase      :     str        =   model(**snake_case__						).loss
                loss.backward()



      def 						snake_case_ (					self					:     int						):
           _UpperCAmelCase      :     List[str]        =   self.model_tester.prepare_config_and_inputs_for_common()
           if not self.model_tester.is_training:
                return

           _UpperCAmelCase      :     str        =   False
           _UpperCAmelCase      :     str        =   True

           for model_class in self.all_model_classes:
                if model_class in get_values(snake_case__						) or not model_class.supports_gradient_checkpointing:
                     continue
                # DeiTForImageClassificationWithTeacher supports inference-only
                if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
                     continue
                _UpperCAmelCase      :     Tuple        =   model_class(snake_case__						)
                model.gradient_checkpointing_enable()
                model.to(snake_case__						)
                model.train()
                _UpperCAmelCase      :     Any        =   self._prepare_for_class(snake_case__					,     snake_case__					,     return_labels=snake_case__						)
                _UpperCAmelCase      :     Optional[int]        =   model(**snake_case__						).loss
                loss.backward()



      def 						snake_case_ (					self					:     List[Any]						):
           _UpperCAmelCase      :     Tuple        =   self.model_tester.prepare_config_and_inputs_for_common()

           _UpperCAmelCase      :     int        =   [
               {'title': 'multi_label_classification', 'num_labels': 2, 'dtype': torch.float},
               {'title': 'single_label_classification', 'num_labels': 1, 'dtype': torch.long},
               {'title': 'regression', 'num_labels': 1, 'dtype': torch.float},
           ]

           for model_class in self.all_model_classes:
                if (
                    model_class
                    not in [
                        *get_values(snake_case__						),
                        *get_values(snake_case__						),
                    ]
                    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
                ):
                     continue

                for problem_type in problem_types:
                     with self.subTest(msg=f'Testing {model_class} with {problem_type["title"]}'						):
                          _UpperCAmelCase      :     Optional[Any]        =   problem_type['title']
                          _UpperCAmelCase      :     List[str]        =   problem_type['num_labels']

                          _UpperCAmelCase      :     Union[str, Any]        =   model_class(snake_case__						)
                          model.to(snake_case__						)
                          model.train()

                          _UpperCAmelCase      :     List[Any]        =   self._prepare_for_class(snake_case__					,     snake_case__					,     return_labels=snake_case__						)

                          if problem_type["num_labels"] > 1:
                               _UpperCAmelCase      :     Optional[Any]        =   inputs['labels'].unsqueeze(1						).repeat(1					,     problem_type["num_labels"]						)

                          _UpperCAmelCase      :     Union[str, Any]        =   inputs['labels'].to(problem_type["dtype"]						)

                          # This tests that we do not trigger the warning form PyTorch "Using a target size that is different
                          # to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
                          # they have the same size." which is a symptom something in wrong for the regression problem.
                          # See https://github.com/huggingface/transformers/issues/11780
                          with warnings.catch_warnings(record=snake_case__						) as warning_list:
                               _UpperCAmelCase      :     Any        =   model(**snake_case__						).loss
                          for w in warning_list:
                               if "Using a target size that is different to the input size" in str(w.message						):
                                    raise ValueError(
                                        f'Something is going wrong in the regression problem: intercepted {w.message}'						)

                          loss.backward()



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

           for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                _UpperCAmelCase      :     str        =   DeiTModel.from_pretrained(snake_case__						)
                self.assertIsNotNone(snake_case__						)







def 						__snake_case						(				)  ->       List[str]:





     '''simple docstring'''

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






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



      @cached_property
      def 						snake_case_ (					self					:     Union[str, Any]						):
           return (
               DeiTImageProcessor.from_pretrained("facebook/deit-base-distilled-patch16-224"						)
               if is_vision_available()
               else None
           )



      @slow
      def 						snake_case_ (					self					:     Dict						):
           _UpperCAmelCase      :     Optional[int]        =   DeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224"						).to(
               snake_case__						)

           _UpperCAmelCase      :     int        =   self.default_image_processor
           _UpperCAmelCase      :     Optional[int]        =   prepare_img()
           _UpperCAmelCase      :     List[Any]        =   image_processor(images=snake_case__					,     return_tensors="pt"						).to(snake_case__						)

           # forward pass
           with torch.no_grad():
                _UpperCAmelCase      :     int        =   model(**snake_case__						)

           # verify the logits
           _UpperCAmelCase      :     str        =   torch.Size((1, 1_0_0_0)						)
           self.assertEqual(outputs.logits.shape					,     snake_case__						)

           _UpperCAmelCase      :     Optional[int]        =   torch.tensor([-1.0_266, 0.1_912, -1.2_861]						).to(snake_case__						)

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



      @slow
      @require_accelerate
      @require_torch_gpu
      def 						snake_case_ (					self					:     Any						):
           _UpperCAmelCase      :     Tuple        =   DeiTModel.from_pretrained(
               "facebook/deit-base-distilled-patch16-224"					,     torch_dtype=torch.floataa					,     device_map="auto"						)
           _UpperCAmelCase      :     Any        =   self.default_image_processor

           _UpperCAmelCase      :     Optional[int]        =   prepare_img()
           _UpperCAmelCase      :     List[str]        =   image_processor(images=snake_case__					,     return_tensors="pt"						)
           _UpperCAmelCase      :     Tuple        =   inputs.pixel_values.to(snake_case__						)

           # forward pass to make sure inference works in fp16
           with torch.no_grad():
                _UpperCAmelCase      :     Union[str, Any]        =   model(snake_case__						)





"""simple docstring"""




import importlib
import inspect
import os
import re


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_config_docstrings.py
_lowerCAmelCase    : Dict							=							"src/transformers"


# This is to make sure the transformers module imported is the one in the repo.
_lowerCAmelCase    : List[Any]							=							importlib.util.spec_from_file_location(
    "transformers",
    os.path.join(PATH_TO_TRANSFORMERS, "__init__.py"),
    submodule_search_locations=[PATH_TO_TRANSFORMERS],
)
_lowerCAmelCase    : Tuple							=							spec.loader.load_module()

_lowerCAmelCase    : Tuple							=							transformers.models.auto.configuration_auto.CONFIG_MAPPING

# Regex pattern used to find the checkpoint mentioned in the docstring of `config_class`.
# For example, `[bert-base-uncased](https://huggingface.co/bert-base-uncased)`
_lowerCAmelCase    : Optional[int]							=							re.compile("\[(.+?)\]\((https://huggingface\.co/.+?)\)")


_lowerCAmelCase    : Optional[int]							=							{
    "CLIPConfigMixin",
    "DecisionTransformerConfigMixin",
    "EncoderDecoderConfigMixin",
    "RagConfigMixin",
    "SpeechEncoderDecoderConfigMixin",
    "VisionEncoderDecoderConfigMixin",
    "VisionTextDualEncoderConfigMixin",
}

def 						__snake_case						(				)  ->       Dict:





     '''simple docstring'''

     _UpperCAmelCase      :     List[str]        =   []

     for config_class in list(CONFIG_MAPPING.values()    ):
          _UpperCAmelCase      :     Union[str, Any]        =   False

          # source code of `config_class`
          _UpperCAmelCase      :     Optional[int]        =   inspect.getsource(SCREAMING_SNAKE_CASE__    )
          _UpperCAmelCase      :     List[Any]        =   _re_checkpoint.findall(SCREAMING_SNAKE_CASE__    )

          for checkpoint in checkpoints:
               # Each `checkpoint` is a tuple of a checkpoint name and a checkpoint link.
               # For example, `('bert-base-uncased', 'https://huggingface.co/bert-base-uncased')`
               _UpperCAmelCase    ,	_UpperCAmelCase      :     List[Any]        =   checkpoint

               # verify the checkpoint name corresponds to the checkpoint link
               _UpperCAmelCase      :     Optional[Any]        =   f'https://huggingface.co/{ckpt_name}'
               if ckpt_link == ckpt_link_from_name:
                    _UpperCAmelCase      :     Optional[Any]        =   True
                    break

          _UpperCAmelCase      :     int        =   config_class.__name__
          if not checkpoint_found and name not in CONFIG_CLASSES_TO_IGNORE_FOR_DOCSTRING_CHECKPOINT_CHECK:
               configs_without_checkpoint.append(SCREAMING_SNAKE_CASE__    )

     if len(SCREAMING_SNAKE_CASE__    ) > 0:
          _UpperCAmelCase      :     List[str]        =   "\n".join(sorted(SCREAMING_SNAKE_CASE__    )    )
          raise ValueError(f'The following configurations don\'t contain any valid checkpoint:\n{message}'    )


if __name__ == "__main__":
 check_config_docstrings_have_checkpoints()
