


"""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  lowerCamelCase__    :
      def __init__(		self						:						Optional[Any]   ,							A_						:						Any   ,							A_						:						List[str]=1_3   ,							A_						:						str=3_0   ,							A_						:						str=2   ,							A_						:						Tuple=3   ,							A_						:						Union[str, Any]=True   ,							A_						:						Any=True   ,							A_						:						Union[str, Any]=3_2   ,							A_						:						List[str]=5   ,							A_						:						Optional[int]=4   ,							A_						:						Dict=3_7   ,							A_						:						List[Any]="gelu"   ,							A_						:						List[Any]=0.1   ,							A_						:						List[str]=0.1   ,							A_						:						Union[str, Any]=1_0   ,							A_						:						Optional[int]=0.02   ,							A_						:						List[str]=3   ,							A_						:						List[str]=None   ,							A_						:						int=2   ,							):






       '''simple docstring'''


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

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






       '''simple docstring'''


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

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

       __lowercase						=						self.get_config()

       return config, pixel_values, labels
      def SCREAMING_SNAKE_CASE_						(		self						:						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 SCREAMING_SNAKE_CASE_						(		self						:						Optional[Any]   ,							A_						:						List[Any]   ,							A_						:						int   ,							A_						:						Any				):






       '''simple docstring'''


       __lowercase						=						DeiTModel(config=A_				)
       model.to(A_				)
       model.eval()
       __lowercase						=						model(A_				)
       self.parent.assertEqual(result.last_hidden_state.shape   ,							(self.batch_size, self.seq_length, self.hidden_size)				)
      def SCREAMING_SNAKE_CASE_						(		self						:						int   ,							A_						:						Any   ,							A_						:						List[Any]   ,							A_						:						Tuple				):






       '''simple docstring'''


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

       # test greyscale images
       __lowercase						=						1
       __lowercase						=						DeiTForMaskedImageModeling(A_				)
       model.to(A_				)
       model.eval()

       __lowercase						=						floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
       __lowercase						=						model(A_				)
       self.parent.assertEqual(result.reconstruction.shape   ,							(self.batch_size, 1, self.image_size, self.image_size)				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Dict   ,							A_						:						Optional[Any]   ,							A_						:						List[str]   ,							A_						:						List[str]				):






       '''simple docstring'''


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

       # test greyscale images
       __lowercase						=						1
       __lowercase						=						DeiTForImageClassification(A_				)
       model.to(A_				)
       model.eval()

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






      def SCREAMING_SNAKE_CASE_						(		self						:						int				):






       '''simple docstring'''


       __lowercase						=						self.prepare_config_and_inputs()
       (
           (
           __lowercase
       )				,		(
           __lowercase
       )				,		(
           __lowercase
       )				,		
       )						=						config_and_inputs
       __lowercase						=						{"""pixel_values""": pixel_values}
       return config, inputs_dict






@require_torch
class  lowerCamelCase__    (		_a      , _a      , unittest.TestCase     ):
      a			:  Tuple           =		(
          (
              DeiTModel,
              DeiTForImageClassification,
              DeiTForImageClassificationWithTeacher,
              DeiTForMaskedImageModeling,
          )
          if is_torch_available()
          else ()
      )
      a			:  Union[str, Any]           =		(
          {
              """feature-extraction""": DeiTModel,
              """image-classification""": (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
          }
          if is_torch_available()
          else {}
      )

      a			:  Optional[Any]           =		False
      a			:  int           =		False
      a			:  int           =		False
      def SCREAMING_SNAKE_CASE_						(		self						:						Optional[int]				):






       '''simple docstring'''


       __lowercase						=						DeiTModelTester(self				)
       __lowercase						=						ConfigTester(self   ,							config_class=A_   ,							has_text_modality=A_   ,							hidden_size=3_7				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Optional[Any]				):






       '''simple docstring'''


       self.config_tester.run_common_tests()
      @unittest.skip(reason="""DeiT does not use inputs_embeds"""				)
      def SCREAMING_SNAKE_CASE_						(		self						:						List[str]				):






       '''simple docstring'''


       pass
      def SCREAMING_SNAKE_CASE_						(		self						:						Any				):






       '''simple docstring'''


       __lowercase				,		__lowercase						=						self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
        __lowercase						=						model_class(A_				)
        self.assertIsInstance(model.get_input_embeddings()   ,							(nn.Module)				)
        __lowercase						=						model.get_output_embeddings()
        self.assertTrue(x is None or isinstance(A_   ,							nn.Linear				)				)
      def SCREAMING_SNAKE_CASE_						(		self						:						str				):






       '''simple docstring'''


       __lowercase				,		__lowercase						=						self.model_tester.prepare_config_and_inputs_for_common()

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

        __lowercase						=						["""pixel_values"""]
        self.assertListEqual(arg_names[:1]   ,							A_				)
      def SCREAMING_SNAKE_CASE_						(		self						:						List[Any]				):






       '''simple docstring'''


       __lowercase						=						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*A_				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Union[str, Any]				):






       '''simple docstring'''


       __lowercase						=						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*A_				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Dict				):






       '''simple docstring'''


       __lowercase						=						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*A_				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Optional[int]   ,							A_						:						Optional[Any]   ,							A_						:						int   ,							A_						:						int=False				):






       '''simple docstring'''


       __lowercase						=						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						:						Union[str, Any]				):






       '''simple docstring'''


       if not self.model_tester.is_training:
        return

       __lowercase				,		__lowercase						=						self.model_tester.prepare_config_and_inputs_for_common()
       __lowercase						=						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
        __lowercase						=						model_class(A_				)
        model.to(A_				)
        model.train()
        __lowercase						=						self._prepare_for_class(A_   ,							A_   ,							return_labels=A_				)
        __lowercase						=						model(**A_				).loss
        loss.backward()
      def SCREAMING_SNAKE_CASE_						(		self						:						Dict				):






       '''simple docstring'''


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

       __lowercase						=						False
       __lowercase						=						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
        __lowercase						=						model_class(A_				)
        model.gradient_checkpointing_enable()
        model.to(A_				)
        model.train()
        __lowercase						=						self._prepare_for_class(A_   ,							A_   ,							return_labels=A_				)
        __lowercase						=						model(**A_				).loss
        loss.backward()
      def SCREAMING_SNAKE_CASE_						(		self						:						Optional[Any]				):






       '''simple docstring'''


       __lowercase				,		__lowercase						=						self.model_tester.prepare_config_and_inputs_for_common()

       __lowercase						=						[
           {"""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']}'''				):
          __lowercase						=						problem_type["""title"""]
          __lowercase						=						problem_type["""num_labels"""]

          __lowercase						=						model_class(A_				)
          model.to(A_				)
          model.train()

          __lowercase						=						self._prepare_for_class(A_   ,							A_   ,							return_labels=A_				)

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

          __lowercase						=						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:
           __lowercase						=						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						:						Dict				):






       '''simple docstring'''


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






def lowerCAmelCase_   (					):






 """simple docstring"""







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





@require_torch
@require_vision
class  lowerCamelCase__    (		unittest.TestCase     ):
      @cached_property
      def SCREAMING_SNAKE_CASE_						(		self						:						Tuple				):






       '''simple docstring'''


       return (
           DeiTImageProcessor.from_pretrained("""facebook/deit-base-distilled-patch16-224"""				)
           if is_vision_available()
           else None
       )
      @slow
      def SCREAMING_SNAKE_CASE_						(		self						:						int				):






       '''simple docstring'''


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

       __lowercase						=						self.default_image_processor
       __lowercase						=						prepare_img()
       __lowercase						=						image_processor(images=A_   ,							return_tensors="""pt"""				).to(A_				)

       # forward pass
       with torch.no_grad():
        __lowercase						=						model(**A_				)

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

       __lowercase						=						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						:						Tuple				):






       '''simple docstring'''


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

       __lowercase						=						prepare_img()
       __lowercase						=						image_processor(images=A_   ,							return_tensors="""pt"""				)
       __lowercase						=						inputs.pixel_values.to(A_				)

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




"""simple docstring"""

def lowerCAmelCase_   (					UpperCamelCase__						:			list					, UpperCamelCase__						:			int					, UpperCamelCase__						:			int = 0					, UpperCamelCase__						:			int = 0			):






 """simple docstring"""







 __lowercase						=						right or len(UpperCamelCase__			) - 1
 if left > right:
  return -1
 elif list_data[left] == key:
  return left
 elif list_data[right] == key:
  return right
 else:
  return search(UpperCamelCase__					, UpperCamelCase__					, left + 1					, right - 1			)


if __name__ == "__main__":
       import doctest

       doctest.testmod()
