





import json
import pathlib
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision, slow
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image

     from transformers import DeformableDetrImageProcessor







class 				__lowerCAmelCase       (    unittest.TestCase):







      def __init__(    self		,      lowerCAmelCase__		,      lowerCAmelCase__=7		,      lowerCAmelCase__=3		,      lowerCAmelCase__=3_0		,      lowerCAmelCase__=4_0_0		,      lowerCAmelCase__=True		,      lowerCAmelCase__=None		,      lowerCAmelCase__=True		,      lowerCAmelCase__=[0.5, 0.5, 0.5]		,      lowerCAmelCase__=[0.5, 0.5, 0.5]		,      lowerCAmelCase__=True		,      lowerCAmelCase__=1 / 2_5_5		,      lowerCAmelCase__=True		,      )   ->						str:



        '''simple docstring'''




        a__	:					str										=size if size is not None else {"shortest_edge": 1_8, "longest_edge": 1_3_3_3}
        a__	:					Tuple										=parent
        a__	:					Any										=batch_size
        a__	:					int										=num_channels
        a__	:					List[str]										=min_resolution
        a__	:					Tuple										=max_resolution
        a__	:					Tuple										=do_resize
        a__	:					Dict										=size
        a__	:					List[str]										=do_normalize
        a__	:					Optional[Any]										=image_mean
        a__	:					Tuple										=image_std
        a__	:					Dict										=do_rescale
        a__	:					List[Any]										=rescale_factor
        a__	:					Optional[Any]										=do_pad







      def    _lowercase  (    self							)   ->						Any:



        '''simple docstring'''




        return {
            "do_resize": self.do_resize,
            "size": self.size,
            "do_normalize": self.do_normalize,
            "image_mean": self.image_mean,
            "image_std": self.image_std,
            "do_rescale": self.do_rescale,
            "rescale_factor": self.rescale_factor,
            "do_pad": self.do_pad,
        }







      def    _lowercase  (    self		,      lowerCAmelCase__		,      lowerCAmelCase__=False							)   ->						Any:



        '''simple docstring'''




        if not batched:
          a__	:					Tuple										=image_inputs[0]
          if isinstance(lowerCAmelCase__		,      Image.Image							):
            a__					,							a__	:					Union[str, Any]										=image.size
          else:
            a__					,							a__	:					Optional[int]										=image.shape[1], image.shape[2]
          if w < h:
            a__	:					Optional[int]										=int(self.size["shortest_edge"] * h / w							)
            a__	:					List[Any]										=self.size["shortest_edge"]
          elif w > h:
            a__	:					Dict										=self.size["shortest_edge"]
            a__	:					List[Any]										=int(self.size["shortest_edge"] * w / h							)
          else:
            a__	:					List[Any]										=self.size["shortest_edge"]
            a__	:					int										=self.size["shortest_edge"]

        else:
          a__	:					List[Any]										=[]
          for image in image_inputs:
            a__					,							a__	:					Optional[int]										=self.get_expected_values([image]							)
            expected_values.append((expected_height, expected_width)							)
          a__	:					Any										=max(lowerCAmelCase__		,      key=lambda lowerCAmelCase__							: item[0]							)[0]
          a__	:					Union[str, Any]										=max(lowerCAmelCase__		,      key=lambda lowerCAmelCase__							: item[1]							)[1]

        return expected_height, expected_width







@require_torch
@require_vision
class 				__lowerCAmelCase       (    UpperCamelCase__						,				unittest.TestCase):
      _lowercase				:					Tuple						=			DeformableDetrImageProcessor if is_vision_available() else None







      def    _lowercase  (    self							)   ->						Tuple:



        '''simple docstring'''




        a__	:					List[str]										=DeformableDetrImageProcessingTester(self							)







      @property
      def    _lowercase  (    self							)   ->						Dict:



        '''simple docstring'''




        return self.image_processor_tester.prepare_image_processor_dict()







      def    _lowercase  (    self							)   ->						Tuple:



        '''simple docstring'''




        a__	:					Optional[Any]										=self.image_processing_class(**self.image_processor_dict							)
        self.assertTrue(hasattr(lowerCAmelCase__		,      "image_mean"							)							)
        self.assertTrue(hasattr(lowerCAmelCase__		,      "image_std"							)							)
        self.assertTrue(hasattr(lowerCAmelCase__		,      "do_normalize"							)							)
        self.assertTrue(hasattr(lowerCAmelCase__		,      "do_resize"							)							)
        self.assertTrue(hasattr(lowerCAmelCase__		,      "do_rescale"							)							)
        self.assertTrue(hasattr(lowerCAmelCase__		,      "do_pad"							)							)
        self.assertTrue(hasattr(lowerCAmelCase__		,      "size"							)							)







      def    _lowercase  (    self							)   ->						str:



        '''simple docstring'''




        a__	:					Optional[int]										=self.image_processing_class.from_dict(self.image_processor_dict							)
        self.assertEqual(image_processor.size		,      {"shortest_edge": 1_8, "longest_edge": 1_3_3_3}							)
        self.assertEqual(image_processor.do_pad		,      lowerCAmelCase__							)

        a__	:					List[str]										=self.image_processing_class.from_dict(
            self.image_processor_dict		,      size=4_2		,      max_size=8_4		,      pad_and_return_pixel_mask=lowerCAmelCase__							)
        self.assertEqual(image_processor.size		,      {"shortest_edge": 4_2, "longest_edge": 8_4}							)
        self.assertEqual(image_processor.do_pad		,      lowerCAmelCase__							)







      def    _lowercase  (    self							)   ->						Dict:



        '''simple docstring'''




        pass







      def    _lowercase  (    self							)   ->						Tuple:



        '''simple docstring'''




        a__	:					Optional[int]										=self.image_processing_class(**self.image_processor_dict							)
        # create random PIL images
        a__	:					List[Any]										=prepare_image_inputs(self.image_processor_tester		,      equal_resolution=lowerCAmelCase__							)
        for image in image_inputs:
          self.assertIsInstance(lowerCAmelCase__		,      Image.Image							)

        # Test not batched input
        a__	:					List[Any]										=image_processing(image_inputs[0]		,      return_tensors="pt"							).pixel_values

        a__					,							a__	:					List[Any]										=self.image_processor_tester.get_expected_values(lowerCAmelCase__							)

        self.assertEqual(
            encoded_images.shape		,      (1, self.image_processor_tester.num_channels, expected_height, expected_width)		,      )

        # Test batched
        a__					,							a__	:					List[str]										=self.image_processor_tester.get_expected_values(lowerCAmelCase__		,      batched=lowerCAmelCase__							)

        a__	:					Optional[int]										=image_processing(lowerCAmelCase__		,      return_tensors="pt"							).pixel_values
        self.assertEqual(
            encoded_images.shape		,      (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )		,      )







      def    _lowercase  (    self							)   ->						Optional[Any]:



        '''simple docstring'''




        a__	:					List[Any]										=self.image_processing_class(**self.image_processor_dict							)
        # create random numpy tensors
        a__	:					int										=prepare_image_inputs(self.image_processor_tester		,      equal_resolution=lowerCAmelCase__		,      numpify=lowerCAmelCase__							)
        for image in image_inputs:
          self.assertIsInstance(lowerCAmelCase__		,      np.ndarray							)

        # Test not batched input
        a__	:					Union[str, Any]										=image_processing(image_inputs[0]		,      return_tensors="pt"							).pixel_values

        a__					,							a__	:					Dict										=self.image_processor_tester.get_expected_values(lowerCAmelCase__							)

        self.assertEqual(
            encoded_images.shape		,      (1, self.image_processor_tester.num_channels, expected_height, expected_width)		,      )

        # Test batched
        a__	:					Union[str, Any]										=image_processing(lowerCAmelCase__		,      return_tensors="pt"							).pixel_values

        a__					,							a__	:					List[str]										=self.image_processor_tester.get_expected_values(lowerCAmelCase__		,      batched=lowerCAmelCase__							)

        self.assertEqual(
            encoded_images.shape		,      (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )		,      )







      def    _lowercase  (    self							)   ->						int:



        '''simple docstring'''




        a__	:					List[str]										=self.image_processing_class(**self.image_processor_dict							)
        # create random PyTorch tensors
        a__	:					Union[str, Any]										=prepare_image_inputs(self.image_processor_tester		,      equal_resolution=lowerCAmelCase__		,      torchify=lowerCAmelCase__							)
        for image in image_inputs:
          self.assertIsInstance(lowerCAmelCase__		,      torch.Tensor							)

        # Test not batched input
        a__	:					int										=image_processing(image_inputs[0]		,      return_tensors="pt"							).pixel_values

        a__					,							a__	:					List[Any]										=self.image_processor_tester.get_expected_values(lowerCAmelCase__							)

        self.assertEqual(
            encoded_images.shape		,      (1, self.image_processor_tester.num_channels, expected_height, expected_width)		,      )

        # Test batched
        a__	:					Union[str, Any]										=image_processing(lowerCAmelCase__		,      return_tensors="pt"							).pixel_values

        a__					,							a__	:					Tuple										=self.image_processor_tester.get_expected_values(lowerCAmelCase__		,      batched=lowerCAmelCase__							)

        self.assertEqual(
            encoded_images.shape		,      (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                expected_height,
                expected_width,
            )		,      )







      @slow
      def    _lowercase  (    self							)   ->						Optional[int]:



        '''simple docstring'''




        a__	:					int										=Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
        with open("./tests/fixtures/tests_samples/COCO/coco_annotations.txt"		,      "r"							) as f:
          a__	:					Optional[int]										=json.loads(f.read()							)

        a__	:					Any										={"image_id": 3_9_7_6_9, "annotations": target}

        # encode them
        a__	:					Union[str, Any]										=DeformableDetrImageProcessor()
        a__	:					Tuple										=image_processing(images=lowerCAmelCase__		,      annotations=lowerCAmelCase__		,      return_tensors="pt"							)

        # verify pixel values
        a__	:					Tuple										=torch.Size([1, 3, 8_0_0, 1_0_6_6]							)
        self.assertEqual(encoding["pixel_values"].shape		,      lowerCAmelCase__							)

        a__	:					Any										=torch.tensor([0.27_96, 0.31_38, 0.34_81]							)
        self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3]		,      lowerCAmelCase__		,      atol=1E-4							)							)

        # verify area
        a__	:					List[Any]										=torch.tensor([58_87.96_00, 1_12_50.20_61, 48_93_53.84_38, 83_71_22.75_00, 14_79_67.51_56, 16_57_32.34_38]							)
        self.assertTrue(torch.allclose(encoding["labels"][0]["area"]		,      lowerCAmelCase__							)							)
        # verify boxes
        a__	:					Any										=torch.Size([6, 4]							)
        self.assertEqual(encoding["labels"][0]["boxes"].shape		,      lowerCAmelCase__							)
        a__	:					Optional[Any]										=torch.tensor([0.55_03, 0.27_65, 0.06_04, 0.22_15]							)
        self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0]		,      lowerCAmelCase__		,      atol=1E-3							)							)
        # verify image_id
        a__	:					int										=torch.tensor([3_9_7_6_9]							)
        self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"]		,      lowerCAmelCase__							)							)
        # verify is_crowd
        a__	:					str										=torch.tensor([0, 0, 0, 0, 0, 0]							)
        self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"]		,      lowerCAmelCase__							)							)
        # verify class_labels
        a__	:					List[str]										=torch.tensor([7_5, 7_5, 6_3, 6_5, 1_7, 1_7]							)
        self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"]		,      lowerCAmelCase__							)							)
        # verify orig_size
        a__	:					Optional[int]										=torch.tensor([4_8_0, 6_4_0]							)
        self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"]		,      lowerCAmelCase__							)							)
        # verify size
        a__	:					int										=torch.tensor([8_0_0, 1_0_6_6]							)
        self.assertTrue(torch.allclose(encoding["labels"][0]["size"]		,      lowerCAmelCase__							)							)







      @slow
      def    _lowercase  (    self							)   ->						List[str]:



        '''simple docstring'''




        a__	:					List[str]										=Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
        with open("./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt"		,      "r"							) as f:
          a__	:					int										=json.loads(f.read()							)

        a__	:					List[str]										={"file_name": "000000039769.png", "image_id": 3_9_7_6_9, "segments_info": target}

        a__	:					List[Any]										=pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic"							)

        # encode them
        a__	:					Any										=DeformableDetrImageProcessor(format="coco_panoptic"							)
        a__	:					Optional[Any]										=image_processing(images=lowerCAmelCase__		,      annotations=lowerCAmelCase__		,      masks_path=lowerCAmelCase__		,      return_tensors="pt"							)

        # verify pixel values
        a__	:					Union[str, Any]										=torch.Size([1, 3, 8_0_0, 1_0_6_6]							)
        self.assertEqual(encoding["pixel_values"].shape		,      lowerCAmelCase__							)

        a__	:					Union[str, Any]										=torch.tensor([0.27_96, 0.31_38, 0.34_81]							)
        self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3]		,      lowerCAmelCase__		,      atol=1E-4							)							)

        # verify area
        a__	:					Dict										=torch.tensor([14_79_79.68_75, 16_55_27.04_69, 48_46_38.59_38, 1_12_92.93_75, 58_79.65_62, 76_34.11_47]							)
        self.assertTrue(torch.allclose(encoding["labels"][0]["area"]		,      lowerCAmelCase__							)							)
        # verify boxes
        a__	:					Optional[int]										=torch.Size([6, 4]							)
        self.assertEqual(encoding["labels"][0]["boxes"].shape		,      lowerCAmelCase__							)
        a__	:					Any										=torch.tensor([0.26_25, 0.54_37, 0.46_88, 0.86_25]							)
        self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0]		,      lowerCAmelCase__		,      atol=1E-3							)							)
        # verify image_id
        a__	:					int										=torch.tensor([3_9_7_6_9]							)
        self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"]		,      lowerCAmelCase__							)							)
        # verify is_crowd
        a__	:					List[str]										=torch.tensor([0, 0, 0, 0, 0, 0]							)
        self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"]		,      lowerCAmelCase__							)							)
        # verify class_labels
        a__	:					Optional[Any]										=torch.tensor([1_7, 1_7, 6_3, 7_5, 7_5, 9_3]							)
        self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"]		,      lowerCAmelCase__							)							)
        # verify masks
        a__	:					int										=8_2_2_8_7_3
        self.assertEqual(encoding["labels"][0]["masks"].sum().item()		,      lowerCAmelCase__							)
        # verify orig_size
        a__	:					List[str]										=torch.tensor([4_8_0, 6_4_0]							)
        self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"]		,      lowerCAmelCase__							)							)
        # verify size
        a__	:					int										=torch.tensor([8_0_0, 1_0_6_6]							)
        self.assertTrue(torch.allclose(encoding["labels"][0]["size"]		,      lowerCAmelCase__							)							)












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		,			_A		,			_A=1_3		,			_A=3_0		,			_A=2		,			_A=3		,			_A=True		,			_A=True		,			_A=3_2		,			_A=5		,			_A=4		,			_A=3_7		,			_A="gelu"		,			_A=0.1		,			_A=0.1		,			_A=1_0		,			_A=0.02		,			_A=3		,			_A=None		,			_A=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 				__SCREAMING_SNAKE_CASE(   self			):




											"""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 				__SCREAMING_SNAKE_CASE(   self			):




											"""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 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A		,			_A			):




											"""simple docstring"""



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






						def 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A		,			_A			):




											"""simple docstring"""



											__lowerCAmelCase				   =       DeiTForMaskedImageModeling(config=_A			)
											model.to(_A			)
											model.eval()
											__lowerCAmelCase				   =       model(_A			)
											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(_A			)
											model.to(_A			)
											model.eval()

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






						def 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A		,			_A			):




											"""simple docstring"""



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

											# test greyscale images
											__lowerCAmelCase				   =       1
											__lowerCAmelCase				   =       DeiTForImageClassification(_A			)
											model.to(_A			)
											model.eval()

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






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""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__    (					snake_case__ ,				snake_case__ ,				unittest.TestCase      ):
						_a							:    Optional[Any]					=    (
						    (
						        DeiTModel,
						        DeiTForImageClassification,
						        DeiTForImageClassificationWithTeacher,
						        DeiTForMaskedImageModeling,
						    )
						    if is_torch_available()
						    else ()
						)
						_a							:    int					=    (
						    {
						        """feature-extraction""": DeiTModel,
						        """image-classification""": (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
						    }
						    if is_torch_available()
						    else {}
						)

						_a							:    Optional[Any]					=    False
						_a							:    Tuple					=    False
						_a							:    Tuple					=    False






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase				   =       DeiTModelTester(self			)
											__lowerCAmelCase				   =       ConfigTester(self		,			config_class=_A		,			has_text_modality=_A		,			hidden_size=3_7			)






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											self.config_tester.run_common_tests()






						@unittest.skip(reason="DeiT does not use inputs_embeds"			)
						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											pass






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase   ,							__lowerCAmelCase				   =       self.model_tester.prepare_config_and_inputs_for_common()

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






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase   ,							__lowerCAmelCase				   =       self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																__lowerCAmelCase				   =       model_class(_A			)
																__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]		,			_A			)






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase				   =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_A			)






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase				   =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_image_modeling(*_A			)






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase				   =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*_A			)






						def 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A		,			_A=False			):




											"""simple docstring"""



											__lowerCAmelCase				   =       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 				__SCREAMING_SNAKE_CASE(   self			):




											"""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(_A			)
																    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
																):
																					continue
																__lowerCAmelCase				   =       model_class(_A			)
																model.to(_A			)
																model.train()
																__lowerCAmelCase				   =       self._prepare_for_class(_A		,			_A		,			return_labels=_A			)
																__lowerCAmelCase				   =       model(**_A			).loss
																loss.backward()






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""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(_A			) or not model_class.supports_gradient_checkpointing:
																					continue
																# DeiTForImageClassificationWithTeacher supports inference-only
																if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
																					continue
																__lowerCAmelCase				   =       model_class(_A			)
																model.gradient_checkpointing_enable()
																model.to(_A			)
																model.train()
																__lowerCAmelCase				   =       self._prepare_for_class(_A		,			_A		,			return_labels=_A			)
																__lowerCAmelCase				   =       model(**_A			).loss
																loss.backward()






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""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(_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				   =       problem_type["title"]
																										__lowerCAmelCase				   =       problem_type["num_labels"]

																										__lowerCAmelCase				   =       model_class(_A			)
																										model.to(_A			)
																										model.train()

																										__lowerCAmelCase				   =       self._prepare_for_class(_A		,			_A		,			return_labels=_A			)

																										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=_A			) as warning_list:
																															__lowerCAmelCase				   =       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 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



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







def 	_a      (						):
					__lowerCAmelCase				   =       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"     )
					return image


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






						@cached_property
						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



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






						@slow
						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase				   =       DeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224"			).to(
											    _A			)

											__lowerCAmelCase				   =       self.default_image_processor
											__lowerCAmelCase				   =       prepare_img()
											__lowerCAmelCase				   =       image_processor(images=_A		,			return_tensors="pt"			).to(_A			)

											# forward pass
											with torch.no_grad():
																__lowerCAmelCase				   =       model(**_A			)

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

											__lowerCAmelCase				   =       torch.tensor([-1.02_66, 0.19_12, -1.28_61]			).to(_A			)

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






						@slow
						@require_accelerate
						@require_torch_gpu
						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""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=_A		,			return_tensors="pt"			)
											__lowerCAmelCase				   =       inputs.pixel_values.to(_A			)

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




