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     a_			:






 '''simple docstring'''






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







        '''simple docstring'''



        lowerCAmelCase__					     = parent
        lowerCAmelCase__					     = batch_size
        lowerCAmelCase__					     = image_size
        lowerCAmelCase__					     = patch_size
        lowerCAmelCase__					     = num_channels
        lowerCAmelCase__					     = is_training
        lowerCAmelCase__					     = use_labels
        lowerCAmelCase__					     = hidden_size
        lowerCAmelCase__					     = num_hidden_layers
        lowerCAmelCase__					     = num_attention_heads
        lowerCAmelCase__					     = intermediate_size
        lowerCAmelCase__					     = hidden_act
        lowerCAmelCase__					     = hidden_dropout_prob
        lowerCAmelCase__					     = attention_probs_dropout_prob
        lowerCAmelCase__					     = type_sequence_label_size
        lowerCAmelCase__					     = initializer_range
        lowerCAmelCase__					     = scope
        lowerCAmelCase__					     = encoder_stride

        # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
        lowerCAmelCase__					     = (image_size // patch_size) ** 2
        lowerCAmelCase__					     = num_patches + 2




 def 		__snake_case						(      self :  List[str]):







        '''simple docstring'''



        lowerCAmelCase__					     = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

        lowerCAmelCase__					     = self.get_config()

        return config, pixel_values, labels




 def 		__snake_case						(      self :  List[Any]):







        '''simple docstring'''



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




 def 		__snake_case						(      self :  List[Any]      ,   lowercase__ :  Optional[Any]      ,   lowercase__ :  Union[str, Any]      ,   lowercase__ :  List[str]):







        '''simple docstring'''



        lowerCAmelCase__					     = DeiTModel(config=lowercase__)
        model.to(lowercase__)
        model.eval()
        lowerCAmelCase__					     = model(lowercase__)
        self.parent.assertEqual(result.last_hidden_state.shape      ,   (self.batch_size, self.seq_length, self.hidden_size))




 def 		__snake_case						(      self :  str      ,   lowercase__ :  Any      ,   lowercase__ :  Tuple      ,   lowercase__ :  List[Any]):







        '''simple docstring'''



        lowerCAmelCase__					     = DeiTForMaskedImageModeling(config=lowercase__)
        model.to(lowercase__)
        model.eval()
        lowerCAmelCase__					     = model(lowercase__)
        self.parent.assertEqual(
            result.reconstruction.shape      ,   (self.batch_size, self.num_channels, self.image_size, self.image_size))

        # test greyscale images
        lowerCAmelCase__					     = 1
        lowerCAmelCase__					     = DeiTForMaskedImageModeling(lowercase__)
        model.to(lowercase__)
        model.eval()

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




 def 		__snake_case						(      self :  Any      ,   lowercase__ :  Optional[int]      ,   lowercase__ :  Optional[Any]      ,   lowercase__ :  Union[str, Any]):







        '''simple docstring'''



        lowerCAmelCase__					     = self.type_sequence_label_size
        lowerCAmelCase__					     = DeiTForImageClassification(lowercase__)
        model.to(lowercase__)
        model.eval()
        lowerCAmelCase__					     = model(lowercase__      ,   labels=lowercase__)
        self.parent.assertEqual(result.logits.shape      ,   (self.batch_size, self.type_sequence_label_size))

        # test greyscale images
        lowerCAmelCase__					     = 1
        lowerCAmelCase__					     = DeiTForImageClassification(lowercase__)
        model.to(lowercase__)
        model.eval()

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




 def 		__snake_case						(      self :  List[str]):







        '''simple docstring'''



        lowerCAmelCase__					     = self.prepare_config_and_inputs()
        (
            (
            lowerCAmelCase__
        )     , (
            lowerCAmelCase__
        )     , (
            lowerCAmelCase__
        )     , 
        )					     = config_and_inputs
        lowerCAmelCase__					     = {'pixel_values': pixel_values}
        return config, inputs_dict






@require_torch
class     a_			(						SCREAMING_SNAKE_CASE	,					SCREAMING_SNAKE_CASE	,					unittest.TestCase						):






 '''simple docstring'''






 UpperCAmelCase_     			= (
     (
         DeiTModel,
         DeiTForImageClassification,
         DeiTForImageClassificationWithTeacher,
         DeiTForMaskedImageModeling,
     )
     if is_torch_available()
     else ()
 )
 UpperCAmelCase_     			= (
     {
         'feature-extraction': DeiTModel,
         'image-classification': (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
     }
     if is_torch_available()
     else {}
 )

 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= False




 def 		__snake_case						(      self :  Any):







        '''simple docstring'''



        lowerCAmelCase__					     = DeiTModelTester(self)
        lowerCAmelCase__					     = ConfigTester(self      ,   config_class=lowercase__      ,   has_text_modality=lowercase__      ,   hidden_size=37)




 def 		__snake_case						(      self :  int):







        '''simple docstring'''



        self.config_tester.run_common_tests()




 @unittest.skip(reason='DeiT does not use inputs_embeds')
 def 		__snake_case						(      self :  str):







        '''simple docstring'''



        pass




 def 		__snake_case						(      self :  Dict):







        '''simple docstring'''



        lowerCAmelCase__     , lowerCAmelCase__					     = self.model_tester.prepare_config_and_inputs_for_common()

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




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







        '''simple docstring'''



        lowerCAmelCase__     , lowerCAmelCase__					     = self.model_tester.prepare_config_and_inputs_for_common()

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

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




 def 		__snake_case						(      self :  List[str]):







        '''simple docstring'''



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




 def 		__snake_case						(      self :  List[Any]):







        '''simple docstring'''



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




 def 		__snake_case						(      self :  Dict):







        '''simple docstring'''



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




 def 		__snake_case						(      self :  Tuple      ,   lowercase__ :  List[str]      ,   lowercase__ :  List[str]      ,   lowercase__ :  int=False):







        '''simple docstring'''



        lowerCAmelCase__					     = super()._prepare_for_class(lowercase__      ,   lowercase__      ,   return_labels=lowercase__)

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

        return inputs_dict




 def 		__snake_case						(      self :  Optional[Any]):







        '''simple docstring'''



        if not self.model_tester.is_training:
               return

        lowerCAmelCase__     , lowerCAmelCase__					     = self.model_tester.prepare_config_and_inputs_for_common()
        lowerCAmelCase__					     = True

        for model_class in self.all_model_classes:
               # DeiTForImageClassificationWithTeacher supports inference-only
               if (
                   model_class in get_values(lowercase__)
                   or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
               ):
                      continue
               lowerCAmelCase__					     = model_class(lowercase__)
               model.to(lowercase__)
               model.train()
               lowerCAmelCase__					     = self._prepare_for_class(lowercase__      ,   lowercase__      ,   return_labels=lowercase__)
               lowerCAmelCase__					     = model(**lowercase__).loss
               loss.backward()




 def 		__snake_case						(      self :  Any):







        '''simple docstring'''



        lowerCAmelCase__     , lowerCAmelCase__					     = self.model_tester.prepare_config_and_inputs_for_common()
        if not self.model_tester.is_training:
               return

        lowerCAmelCase__					     = False
        lowerCAmelCase__					     = True

        for model_class in self.all_model_classes:
               if model_class in get_values(lowercase__) or not model_class.supports_gradient_checkpointing:
                      continue
               # DeiTForImageClassificationWithTeacher supports inference-only
               if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
                      continue
               lowerCAmelCase__					     = model_class(lowercase__)
               model.gradient_checkpointing_enable()
               model.to(lowercase__)
               model.train()
               lowerCAmelCase__					     = self._prepare_for_class(lowercase__      ,   lowercase__      ,   return_labels=lowercase__)
               lowerCAmelCase__					     = model(**lowercase__).loss
               loss.backward()




 def 		__snake_case						(      self :  str):







        '''simple docstring'''



        lowerCAmelCase__     , lowerCAmelCase__					     = self.model_tester.prepare_config_and_inputs_for_common()

        lowerCAmelCase__					     = [
            {'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(lowercase__),
                       *get_values(lowercase__),
                   ]
                   or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
               ):
                      continue

               for problem_type in problem_types:
                      with self.subTest(msg=F"""Testing {model_class} with {problem_type['title']}"""):
                             lowerCAmelCase__					     = problem_type['title']
                             lowerCAmelCase__					     = problem_type['num_labels']

                             lowerCAmelCase__					     = model_class(lowercase__)
                             model.to(lowercase__)
                             model.train()

                             lowerCAmelCase__					     = self._prepare_for_class(lowercase__      ,   lowercase__      ,   return_labels=lowercase__)

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

                             lowerCAmelCase__					     = 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=lowercase__) as warning_list:
                                    lowerCAmelCase__					     = model(**lowercase__).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 :  Dict):







        '''simple docstring'''



        for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowerCAmelCase__					     = DeiTModel.from_pretrained(lowercase__)
               self.assertIsNotNone(lowercase__)






def 				__lowerCamelCase		(						):
       lowerCAmelCase__					     = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'     )
       return image



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






 '''simple docstring'''






 @cached_property
 def 		__snake_case						(      self :  Optional[int]):







        '''simple docstring'''



        return (
            DeiTImageProcessor.from_pretrained('facebook/deit-base-distilled-patch16-224')
            if is_vision_available()
            else None
        )




 @slow
 def 		__snake_case						(      self :  str):







        '''simple docstring'''



        lowerCAmelCase__					     = DeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224').to(
            lowercase__)

        lowerCAmelCase__					     = self.default_image_processor
        lowerCAmelCase__					     = prepare_img()
        lowerCAmelCase__					     = image_processor(images=lowercase__      ,   return_tensors='pt').to(lowercase__)

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

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

        lowerCAmelCase__					     = torch.tensor([-1.0_266, 0.1_912, -1.2_861]).to(lowercase__)

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




 @slow
 @require_accelerate
 @require_torch_gpu
 def 		__snake_case						(      self :  Optional[int]):







        '''simple docstring'''



        lowerCAmelCase__					     = DeiTModel.from_pretrained(
            'facebook/deit-base-distilled-patch16-224'      ,   torch_dtype=torch.floataa      ,   device_map='auto')
        lowerCAmelCase__					     = self.default_image_processor

        lowerCAmelCase__					     = prepare_img()
        lowerCAmelCase__					     = image_processor(images=lowercase__      ,   return_tensors='pt')
        lowerCAmelCase__					     = inputs.pixel_values.to(lowercase__)

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

from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import convert_to_rgb, normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
    import PIL


lowerCAmelCase__     						=			logging.get_logger(__name__)
class     a_			(						SCREAMING_SNAKE_CASE						):






 '''simple docstring'''






 UpperCAmelCase_     			= ['pixel_values']




 def __init__(      self :  Tuple      ,   lowercase__ :  bool = True      ,   lowercase__ :  Dict[str, int] = None      ,   lowercase__ :  PILImageResampling = PILImageResampling.BICUBIC      ,   lowercase__ :  bool = True      ,   lowercase__ :  Union[int, float] = 1 / 255      ,   lowercase__ :  bool = True      ,   lowercase__ :  Optional[Union[float, List[float]]] = None      ,   lowercase__ :  Optional[Union[float, List[float]]] = None      ,   lowercase__ :  bool = True      ,   **lowercase__ :  List[Any]      ,   ):







        '''simple docstring'''



        super().__init__(**lowercase__)
        lowerCAmelCase__					     = size if size is not None else {'height': 384, 'width': 384}
        lowerCAmelCase__					     = get_size_dict(lowercase__      ,   default_to_square=lowercase__)

        lowerCAmelCase__					     = do_resize
        lowerCAmelCase__					     = size
        lowerCAmelCase__					     = resample
        lowerCAmelCase__					     = do_rescale
        lowerCAmelCase__					     = rescale_factor
        lowerCAmelCase__					     = do_normalize
        lowerCAmelCase__					     = image_mean if image_mean is not None else OPENAI_CLIP_MEAN
        lowerCAmelCase__					     = image_std if image_std is not None else OPENAI_CLIP_STD
        lowerCAmelCase__					     = do_convert_rgb




 def 		__snake_case						(      self :  List[str]      ,   lowercase__ :  np.ndarray      ,   lowercase__ :  Dict[str, int]      ,   lowercase__ :  PILImageResampling = PILImageResampling.BICUBIC      ,   lowercase__ :  Optional[Union[str, ChannelDimension]] = None      ,   **lowercase__ :  Dict      ,   ):







        '''simple docstring'''



        lowerCAmelCase__					     = get_size_dict(lowercase__      ,   default_to_square=lowercase__)
        if "height" not in size or "width" not in size:
               raise ValueError(F"""The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}""")
        lowerCAmelCase__					     = (size['height'], size['width'])
        return resize(lowercase__      ,   size=lowercase__      ,   resample=lowercase__      ,   data_format=lowercase__      ,   **lowercase__)




 def 		__snake_case						(      self :  List[str]      ,   lowercase__ :  np.ndarray      ,   lowercase__ :  Union[int, float]      ,   lowercase__ :  Optional[Union[str, ChannelDimension]] = None      ,   **lowercase__ :  Union[str, Any]      ,   ):







        '''simple docstring'''



        return rescale(lowercase__      ,   scale=lowercase__      ,   data_format=lowercase__      ,   **lowercase__)




 def 		__snake_case						(      self :  Optional[Any]      ,   lowercase__ :  np.ndarray      ,   lowercase__ :  Union[float, List[float]]      ,   lowercase__ :  Union[float, List[float]]      ,   lowercase__ :  Optional[Union[str, ChannelDimension]] = None      ,   **lowercase__ :  Any      ,   ):







        '''simple docstring'''



        return normalize(lowercase__      ,   mean=lowercase__      ,   std=lowercase__      ,   data_format=lowercase__      ,   **lowercase__)




 def 		__snake_case						(      self :  Any      ,   lowercase__ :  ImageInput      ,   lowercase__ :  Optional[bool] = None      ,   lowercase__ :  Optional[Dict[str, int]] = None      ,   lowercase__ :  PILImageResampling = None      ,   lowercase__ :  Optional[bool] = None      ,   lowercase__ :  Optional[float] = None      ,   lowercase__ :  Optional[bool] = None      ,   lowercase__ :  Optional[Union[float, List[float]]] = None      ,   lowercase__ :  Optional[Union[float, List[float]]] = None      ,   lowercase__ :  Optional[Union[str, TensorType]] = None      ,   lowercase__ :  bool = None      ,   lowercase__ :  ChannelDimension = ChannelDimension.FIRST      ,   **lowercase__ :  Dict      ,   ):







        '''simple docstring'''



        lowerCAmelCase__					     = do_resize if do_resize is not None else self.do_resize
        lowerCAmelCase__					     = resample if resample is not None else self.resample
        lowerCAmelCase__					     = do_rescale if do_rescale is not None else self.do_rescale
        lowerCAmelCase__					     = rescale_factor if rescale_factor is not None else self.rescale_factor
        lowerCAmelCase__					     = do_normalize if do_normalize is not None else self.do_normalize
        lowerCAmelCase__					     = image_mean if image_mean is not None else self.image_mean
        lowerCAmelCase__					     = image_std if image_std is not None else self.image_std
        lowerCAmelCase__					     = do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

        lowerCAmelCase__					     = size if size is not None else self.size
        lowerCAmelCase__					     = get_size_dict(lowercase__      ,   default_to_square=lowercase__)

        lowerCAmelCase__					     = make_list_of_images(lowercase__)

        if not valid_images(lowercase__):
               raise ValueError(
                   'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '
                   'torch.Tensor, tf.Tensor or jax.ndarray.')

        if do_resize and size is None or resample is None:
               raise ValueError('Size and resample must be specified if do_resize is True.')

        if do_rescale and rescale_factor is None:
               raise ValueError('Rescale factor must be specified if do_rescale is True.')

        if do_normalize and (image_mean is None or image_std is None):
               raise ValueError('Image mean and std must be specified if do_normalize is True.')

        # PIL RGBA images are converted to RGB
        if do_convert_rgb:
               lowerCAmelCase__					     = [convert_to_rgb(lowercase__) for image in images]

        # All transformations expect numpy arrays.
        lowerCAmelCase__					     = [to_numpy_array(lowercase__) for image in images]

        if do_resize:
               lowerCAmelCase__					     = [self.resize(image=lowercase__      ,   size=lowercase__      ,   resample=lowercase__) for image in images]

        if do_rescale:
               lowerCAmelCase__					     = [self.rescale(image=lowercase__      ,   scale=lowercase__) for image in images]

        if do_normalize:
               lowerCAmelCase__					     = [self.normalize(image=lowercase__      ,   mean=lowercase__      ,   std=lowercase__) for image in images]

        lowerCAmelCase__					     = [to_channel_dimension_format(lowercase__      ,   lowercase__) for image in images]

        lowerCAmelCase__					     = BatchFeature(data={'pixel_values': images}      ,   tensor_type=lowercase__)

        return encoded_outputs
