




"""simple docstring"""






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





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




           """simple docstring"""







           _lowerCAmelCase:Any									=  parent
           _lowerCAmelCase:Optional[int]									=  batch_size
           _lowerCAmelCase:str									=  image_size
           _lowerCAmelCase:Any									=  patch_size
           _lowerCAmelCase:Tuple									=  num_channels
           _lowerCAmelCase:Dict									=  is_training
           _lowerCAmelCase:int									=  use_labels
           _lowerCAmelCase:Optional[Any]									=  hidden_size
           _lowerCAmelCase:List[Any]									=  num_hidden_layers
           _lowerCAmelCase:Tuple									=  num_attention_heads
           _lowerCAmelCase:int									=  intermediate_size
           _lowerCAmelCase:Tuple									=  hidden_act
           _lowerCAmelCase:Dict									=  hidden_dropout_prob
           _lowerCAmelCase:Optional[Any]									=  attention_probs_dropout_prob
           _lowerCAmelCase:List[Any]									=  type_sequence_label_size
           _lowerCAmelCase:Dict									=  initializer_range
           _lowerCAmelCase:Tuple									=  scope
           _lowerCAmelCase: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)
           _lowerCAmelCase:int									=  (image_size // patch_size) ** 2
           _lowerCAmelCase:Optional[Any]									=  num_patches + 2





      def       __UpperCamelCase     (							self      :	Dict)						->       Any:




           """simple docstring"""







           _lowerCAmelCase:int									=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

           _lowerCAmelCase:str									=  None
           if self.use_labels:
                _lowerCAmelCase:Any									=  ids_tensor([self.batch_size]     ,self.type_sequence_label_size)

           _lowerCAmelCase:Union[str, Any]									=  self.get_config()

           return config, pixel_values, labels





      def       __UpperCamelCase     (							self      :	List[Any])						->       Optional[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=a__     ,initializer_range=self.initializer_range     ,encoder_stride=self.encoder_stride     ,)





      def       __UpperCamelCase     (							self      :	Union[str, Any]     ,a__      :	Any     ,a__      :	Tuple     ,a__      :	Optional[int])						->       Optional[int]:




           """simple docstring"""







           _lowerCAmelCase:Union[str, Any]									=  DeiTModel(config=a__)
           model.to(a__)
           model.eval()
           _lowerCAmelCase:List[Any]									=  model(a__)
           self.parent.assertEqual(result.last_hidden_state.shape     ,(self.batch_size, self.seq_length, self.hidden_size))





      def       __UpperCamelCase     (							self      :	Tuple     ,a__      :	Optional[Any]     ,a__      :	int     ,a__      :	str)						->       Tuple:




           """simple docstring"""







           _lowerCAmelCase:str									=  DeiTForMaskedImageModeling(config=a__)
           model.to(a__)
           model.eval()
           _lowerCAmelCase:int									=  model(a__)
           self.parent.assertEqual(
               result.reconstruction.shape     ,(self.batch_size, self.num_channels, self.image_size, self.image_size))

           # test greyscale images
           _lowerCAmelCase:Dict									=  1
           _lowerCAmelCase:Optional[Any]									=  DeiTForMaskedImageModeling(a__)
           model.to(a__)
           model.eval()

           _lowerCAmelCase:Optional[Any]									=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
           _lowerCAmelCase:Tuple									=  model(a__)
           self.parent.assertEqual(result.reconstruction.shape     ,(self.batch_size, 1, self.image_size, self.image_size))





      def       __UpperCamelCase     (							self      :	Optional[int]     ,a__      :	Any     ,a__      :	Any     ,a__      :	str)						->       Dict:




           """simple docstring"""







           _lowerCAmelCase:Any									=  self.type_sequence_label_size
           _lowerCAmelCase:List[str]									=  DeiTForImageClassification(a__)
           model.to(a__)
           model.eval()
           _lowerCAmelCase:Optional[int]									=  model(a__     ,labels=a__)
           self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.type_sequence_label_size))

           # test greyscale images
           _lowerCAmelCase:Any									=  1
           _lowerCAmelCase:Tuple									=  DeiTForImageClassification(a__)
           model.to(a__)
           model.eval()

           _lowerCAmelCase:Tuple									=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
           _lowerCAmelCase:Dict									=  model(a__     ,labels=a__)
           self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.type_sequence_label_size))







      def       __UpperCamelCase     (							self      :	Union[str, Any])						->       int:




           """simple docstring"""







           _lowerCAmelCase:Any									=  self.prepare_config_and_inputs()
           (
               _lowerCAmelCase   
           ):Union[str, Any]									=  config_and_inputs
           _lowerCAmelCase:str									=  {'''pixel_values''': pixel_values}
           return config, inputs_dict




@require_torch
class   a__			(       UpperCamelCase_					,	UpperCamelCase_					,	unittest.TestCase  ):
      snake_case__             = (
          (
              DeiTModel,
              DeiTForImageClassification,
              DeiTForImageClassificationWithTeacher,
              DeiTForMaskedImageModeling,
          )
          if is_torch_available()
          else ()
      )
      snake_case__             = (
          {
              '''feature-extraction''': DeiTModel,
              '''image-classification''': (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
          }
          if is_torch_available()
          else {}
      )

      snake_case__             = False
      snake_case__             = False
      snake_case__             = False





      def       __UpperCamelCase     (							self      :	Tuple)						->       Optional[int]:




           """simple docstring"""







           _lowerCAmelCase:Optional[Any]									=  DeiTModelTester(self)
           _lowerCAmelCase:Optional[Any]									=  ConfigTester(self     ,config_class=a__     ,has_text_modality=a__     ,hidden_size=37)





      def       __UpperCamelCase     (							self      :	Dict)						->       Any:




           """simple docstring"""







           self.config_tester.run_common_tests()





      @unittest.skip(reason='''DeiT does not use inputs_embeds''')
      def       __UpperCamelCase     (							self      :	int)						->       Any:




           """simple docstring"""







           pass





      def       __UpperCamelCase     (							self      :	str)						->       List[Any]:




           """simple docstring"""







           _lowerCAmelCase:Tuple									=  self.model_tester.prepare_config_and_inputs_for_common()

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





      def       __UpperCamelCase     (							self      :	Optional[Any])						->       int:




           """simple docstring"""







           _lowerCAmelCase:Any									=  self.model_tester.prepare_config_and_inputs_for_common()

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

                _lowerCAmelCase:List[Any]									=  ['''pixel_values''']
                self.assertListEqual(arg_names[:1]     ,a__)





      def       __UpperCamelCase     (							self      :	Optional[int])						->       Tuple:




           """simple docstring"""







           _lowerCAmelCase:Union[str, Any]									=  self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*a__)





      def       __UpperCamelCase     (							self      :	str)						->       Union[str, Any]:




           """simple docstring"""







           _lowerCAmelCase:Dict									=  self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_image_modeling(*a__)





      def       __UpperCamelCase     (							self      :	List[Any])						->       Dict:




           """simple docstring"""







           _lowerCAmelCase:Any									=  self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*a__)





      def       __UpperCamelCase     (							self      :	Any     ,a__      :	Union[str, Any]     ,a__      :	Optional[Any]     ,a__      :	int=False)						->       List[str]:




           """simple docstring"""







           _lowerCAmelCase:List[str]									=  super()._prepare_for_class(a__     ,a__     ,return_labels=a__)

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

           return inputs_dict





      def       __UpperCamelCase     (							self      :	Dict)						->       Any:




           """simple docstring"""







           if not self.model_tester.is_training:
                return

           _lowerCAmelCase:int									=  self.model_tester.prepare_config_and_inputs_for_common()
           _lowerCAmelCase:Tuple									=  True

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





      def       __UpperCamelCase     (							self      :	Any)						->       List[str]:




           """simple docstring"""







           _lowerCAmelCase:Union[str, Any]									=  self.model_tester.prepare_config_and_inputs_for_common()
           if not self.model_tester.is_training:
                return

           _lowerCAmelCase:Dict									=  False
           _lowerCAmelCase:Any									=  True

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





      def       __UpperCamelCase     (							self      :	List[Any])						->       str:




           """simple docstring"""







           _lowerCAmelCase:Optional[int]									=  self.model_tester.prepare_config_and_inputs_for_common()

           _lowerCAmelCase:Any									=  [
               {'''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(a__),
                        *get_values(a__),
                    ]
                    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:Tuple									=  problem_type['''title''']
                          _lowerCAmelCase:int									=  problem_type['''num_labels''']

                          _lowerCAmelCase:str									=  model_class(a__)
                          model.to(a__)
                          model.train()

                          _lowerCAmelCase:List[Any]									=  self._prepare_for_class(a__     ,a__     ,return_labels=a__)

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

                          _lowerCAmelCase:Tuple									=  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=a__) as warning_list:
                               _lowerCAmelCase:List[Any]									=  model(**a__).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       __UpperCamelCase     (							self      :	Union[str, Any])						->       int:




           """simple docstring"""







           for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                _lowerCAmelCase:Union[str, Any]									=  DeiTModel.from_pretrained(a__)
                self.assertIsNotNone(a__)





def 							UpperCAmelCase		(		):
     _lowerCAmelCase:Optional[Any]									=  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''    )
     return image





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





      @cached_property
      def       __UpperCamelCase     (							self      :	Dict)						->       Optional[Any]:




           """simple docstring"""







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





      @slow
      def       __UpperCamelCase     (							self      :	int)						->       int:




           """simple docstring"""







           _lowerCAmelCase:Tuple									=  DeiTForImageClassificationWithTeacher.from_pretrained('''facebook/deit-base-distilled-patch16-224''').to(
               a__)

           _lowerCAmelCase:Optional[Any]									=  self.default_image_processor
           _lowerCAmelCase:Union[str, Any]									=  prepare_img()
           _lowerCAmelCase:Optional[int]									=  image_processor(images=a__     ,return_tensors='''pt''').to(a__)

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

           # verify the logits
           _lowerCAmelCase:Optional[Any]									=  torch.Size((1, 1000))
           self.assertEqual(outputs.logits.shape     ,a__)

           _lowerCAmelCase:List[str]									=  torch.tensor([-1.0266, 0.1912, -1.2861]).to(a__)

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







      @slow
      @require_accelerate
      @require_torch_gpu
      def       __UpperCamelCase     (							self      :	str)						->       List[Any]:




           """simple docstring"""







           _lowerCAmelCase:int									=  DeiTModel.from_pretrained(
               '''facebook/deit-base-distilled-patch16-224'''     ,torch_dtype=torch.floataa     ,device_map='''auto''')
           _lowerCAmelCase:Union[str, Any]									=  self.default_image_processor

           _lowerCAmelCase:Any									=  prepare_img()
           _lowerCAmelCase:Optional[Any]									=  image_processor(images=a__     ,return_tensors='''pt''')
           _lowerCAmelCase:List[str]									=  inputs.pixel_values.to(a__)

           # forward pass to make sure inference works in fp16
           with torch.no_grad():
                _lowerCAmelCase:Optional[int]									=  model(a__)












"""simple docstring"""






from typing import List, Union

import numpy as np

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
				from PIL import Image

				from ..image_utils import load_image

if is_torch_available():
				import torch

				from ..models.auto.modeling_auto import MODEL_FOR_DEPTH_ESTIMATION_MAPPING

UpperCamelCase__						=			logging.get_logger(__name__)




@add_end_docstrings(UpperCamelCase_  )
class   a__			(       UpperCamelCase_  ):





						def __init__(							self      :	int     ,*a__      :	Optional[Any]     ,**a__      :	Union[str, Any])						->       Tuple:




											"""simple docstring"""







											super().__init__(*a__     ,**a__)
											requires_backends(self     ,'''vision''')
											self.check_model_type(a__)





						def __call__(							self      :	str     ,a__      :	Union[str, List[str], "Image.Image", List["Image.Image"]]     ,**a__      :	List[str])						->       Optional[int]:




											"""simple docstring"""







											return super().__call__(a__     ,**a__)





						def       __UpperCamelCase     (							self      :	Union[str, Any]     ,**a__      :	List[Any])						->       Any:




											"""simple docstring"""







											return {}, {}, {}





						def       __UpperCamelCase     (							self      :	Tuple     ,a__      :	Optional[int])						->       Optional[Any]:




											"""simple docstring"""







											_lowerCAmelCase:List[str]									=  load_image(a__)
											_lowerCAmelCase:int									=  image.size
											_lowerCAmelCase:int									=  self.image_processor(images=a__     ,return_tensors=self.framework)
											return model_inputs





						def       __UpperCamelCase     (							self      :	Dict     ,a__      :	List[str])						->       Union[str, Any]:




											"""simple docstring"""







											_lowerCAmelCase:Any									=  self.model(**a__)
											return model_outputs







						def       __UpperCamelCase     (							self      :	List[Any]     ,a__      :	Dict)						->       Any:




											"""simple docstring"""







											_lowerCAmelCase:Optional[int]									=  model_outputs.predicted_depth
											_lowerCAmelCase:Union[str, Any]									=  torch.nn.functional.interpolate(
											    predicted_depth.unsqueeze(1)     ,size=self.image_size[::-1]     ,mode='''bicubic'''     ,align_corners=a__)
											_lowerCAmelCase:List[str]									=  prediction.squeeze().cpu().numpy()
											_lowerCAmelCase:Any									=  (output * 255 / np.max(a__)).astype('''uint8''')
											_lowerCAmelCase:Dict									=  Image.fromarray(a__)
											_lowerCAmelCase:Tuple									=  {}
											_lowerCAmelCase:Optional[int]									=  predicted_depth
											_lowerCAmelCase:str									=  depth
											return output_dict






