





"""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   __SCREAMING_SNAKE_CASE     :







  '''simple docstring'''


  def __init__(		self :							Dict       , __a :							Tuple       , __a :							Any=13       , __a :							Union[str, Any]=30       , __a :							Optional[Any]=2       , __a :							List[Any]=3       , __a :							Any=True       , __a :							Optional[Any]=True       , __a :							Union[str, Any]=32       , __a :							Tuple=5       , __a :							Dict=4       , __a :							List[str]=37       , __a :							Optional[Any]="gelu"       , __a :							List[Any]=0.1       , __a :							Dict=0.1       , __a :							Optional[int]=10       , __a :							str=0.02       , __a :							List[str]=3       , __a :							List[Any]=None       , __a :							Optional[Any]=2       , )		->					str:
        _UpperCamelCase   :						Optional[int]    =							parent
        _UpperCamelCase   :						Any    =							batch_size
        _UpperCamelCase   :						Union[str, Any]    =							image_size
        _UpperCamelCase   :						Dict    =							patch_size
        _UpperCamelCase   :						Any    =							num_channels
        _UpperCamelCase   :						Optional[int]    =							is_training
        _UpperCamelCase   :						Optional[int]    =							use_labels
        _UpperCamelCase   :						int    =							hidden_size
        _UpperCamelCase   :						Dict    =							num_hidden_layers
        _UpperCamelCase   :						Any    =							num_attention_heads
        _UpperCamelCase   :						str    =							intermediate_size
        _UpperCamelCase   :						List[Any]    =							hidden_act
        _UpperCamelCase   :						Optional[Any]    =							hidden_dropout_prob
        _UpperCamelCase   :						Optional[Any]    =							attention_probs_dropout_prob
        _UpperCamelCase   :						int    =							type_sequence_label_size
        _UpperCamelCase   :						int    =							initializer_range
        _UpperCamelCase   :						List[str]    =							scope
        _UpperCamelCase   :						List[str]    =							encoder_stride

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

  def  __SCREAMING_SNAKE_CASE					(		self :							List[Any]						)		->					Dict:
        _UpperCamelCase   :						Optional[Any]    =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

        _UpperCamelCase   :						Optional[Any]    =							None
        if self.use_labels:
              _UpperCamelCase   :						Tuple    =							ids_tensor([self.batch_size]       , self.type_sequence_label_size						)

        _UpperCamelCase   :						str    =							self.get_config()

        return config, pixel_values, labels

  def  __SCREAMING_SNAKE_CASE					(		self :							Tuple						)		->					List[Any]:
        return DeiTConfig(
            image_size=self.image_size       , patch_size=self.patch_size       , num_channels=self.num_channels       , hidden_size=self.hidden_size       , num_hidden_layers=self.num_hidden_layers       , num_attention_heads=self.num_attention_heads       , intermediate_size=self.intermediate_size       , hidden_act=self.hidden_act       , hidden_dropout_prob=self.hidden_dropout_prob       , attention_probs_dropout_prob=self.attention_probs_dropout_prob       , is_decoder=__a       , initializer_range=self.initializer_range       , encoder_stride=self.encoder_stride       , )

  def  __SCREAMING_SNAKE_CASE					(		self :							str       , __a :							Dict       , __a :							List[Any]       , __a :							Tuple						)		->					Any:
        _UpperCamelCase   :						Optional[Any]    =							DeiTModel(config=__a						)
        model.to(__a						)
        model.eval()
        _UpperCamelCase   :						Dict    =							model(__a						)
        self.parent.assertEqual(result.last_hidden_state.shape       , (self.batch_size, self.seq_length, self.hidden_size)						)

  def  __SCREAMING_SNAKE_CASE					(		self :							List[str]       , __a :							str       , __a :							List[str]       , __a :							Optional[int]						)		->					Union[str, Any]:
        _UpperCamelCase   :						List[str]    =							DeiTForMaskedImageModeling(config=__a						)
        model.to(__a						)
        model.eval()
        _UpperCamelCase   :						List[str]    =							model(__a						)
        self.parent.assertEqual(
            result.reconstruction.shape       , (self.batch_size, self.num_channels, self.image_size, self.image_size)						)

        # test greyscale images
        _UpperCamelCase   :						Tuple    =							1
        _UpperCamelCase   :						str    =							DeiTForMaskedImageModeling(__a						)
        model.to(__a						)
        model.eval()

        _UpperCamelCase   :						Optional[int]    =							floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
        _UpperCamelCase   :						Any    =							model(__a						)
        self.parent.assertEqual(result.reconstruction.shape       , (self.batch_size, 1, self.image_size, self.image_size)						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Optional[Any]       , __a :							str       , __a :							Any       , __a :							Optional[Any]						)		->					int:
        _UpperCamelCase   :						str    =							self.type_sequence_label_size
        _UpperCamelCase   :						int    =							DeiTForImageClassification(__a						)
        model.to(__a						)
        model.eval()
        _UpperCamelCase   :						Optional[Any]    =							model(__a       , labels=__a						)
        self.parent.assertEqual(result.logits.shape       , (self.batch_size, self.type_sequence_label_size)						)

        # test greyscale images
        _UpperCamelCase   :						Optional[int]    =							1
        _UpperCamelCase   :						List[Any]    =							DeiTForImageClassification(__a						)
        model.to(__a						)
        model.eval()

        _UpperCamelCase   :						str    =							floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
        _UpperCamelCase   :						Union[str, Any]    =							model(__a       , labels=__a						)
        self.parent.assertEqual(result.logits.shape       , (self.batch_size, self.type_sequence_label_size)						)


  def  __SCREAMING_SNAKE_CASE					(		self :							int						)		->					Tuple:
        _UpperCamelCase   :						Dict    =							self.prepare_config_and_inputs()
        (
            (
            _UpperCamelCase
        ),	(
            _UpperCamelCase
        ),	(
            _UpperCamelCase
        ),	
        )   :						Tuple    =							config_and_inputs
        _UpperCamelCase   :						str    =							{"pixel_values": pixel_values}
        return config, inputs_dict





@require_torch
class   __SCREAMING_SNAKE_CASE     (    _UpperCamelCase		,	_UpperCamelCase		,	unittest.TestCase ):







  '''simple docstring'''


  SCREAMING_SNAKE_CASE__				:List[str]						  =       (
      (
          DeiTModel,
          DeiTForImageClassification,
          DeiTForImageClassificationWithTeacher,
          DeiTForMaskedImageModeling,
      )
      if is_torch_available()
      else ()
  )
  SCREAMING_SNAKE_CASE__				:Optional[Any]						  =       (
      {
          "feature-extraction": DeiTModel,
          "image-classification": (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
      }
      if is_torch_available()
      else {}
  )

  SCREAMING_SNAKE_CASE__				:str						  =       False
  SCREAMING_SNAKE_CASE__				:Dict						  =       False
  SCREAMING_SNAKE_CASE__				:Optional[Any]						  =       False

  def  __SCREAMING_SNAKE_CASE					(		self :							Any						)		->					Union[str, Any]:
        _UpperCamelCase   :						Optional[Any]    =							DeiTModelTester(self						)
        _UpperCamelCase   :						Union[str, Any]    =							ConfigTester(self       , config_class=__a       , has_text_modality=__a       , hidden_size=37						)

  def  __SCREAMING_SNAKE_CASE					(		self :							str						)		->					Union[str, Any]:
        self.config_tester.run_common_tests()

  @unittest.skip(reason="DeiT does not use inputs_embeds"						)
  def  __SCREAMING_SNAKE_CASE					(		self :							str						)		->					Union[str, Any]:
        pass

  def  __SCREAMING_SNAKE_CASE					(		self :							List[Any]						)		->					List[Any]:
        _UpperCamelCase,	_UpperCamelCase   :						Any    =							self.model_tester.prepare_config_and_inputs_for_common()

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

  def  __SCREAMING_SNAKE_CASE					(		self :							Tuple						)		->					Tuple:
        _UpperCamelCase,	_UpperCamelCase   :						Tuple    =							self.model_tester.prepare_config_and_inputs_for_common()

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

              _UpperCamelCase   :						Optional[Any]    =							["pixel_values"]
              self.assertListEqual(arg_names[:1]       , __a						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Any						)		->					Tuple:
        _UpperCamelCase   :						Dict    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__a						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Dict						)		->					str:
        _UpperCamelCase   :						Any    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_image_modeling(*__a						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Tuple						)		->					Optional[Any]:
        _UpperCamelCase   :						int    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*__a						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Optional[Any]       , __a :							Tuple       , __a :							Union[str, Any]       , __a :							List[Any]=False						)		->					List[str]:
        _UpperCamelCase   :						Union[str, Any]    =							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 :							Any						)		->					List[str]:
        if not self.model_tester.is_training:
              return

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

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

  def  __SCREAMING_SNAKE_CASE					(		self :							Union[str, Any]						)		->					Tuple:
        _UpperCamelCase,	_UpperCamelCase   :						Any    =							self.model_tester.prepare_config_and_inputs_for_common()
        if not self.model_tester.is_training:
              return

        _UpperCamelCase   :						List[Any]    =							False
        _UpperCamelCase   :						Dict    =							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
              _UpperCamelCase   :						Tuple    =							model_class(__a						)
              model.gradient_checkpointing_enable()
              model.to(__a						)
              model.train()
              _UpperCamelCase   :						Optional[Any]    =							self._prepare_for_class(__a       , __a       , return_labels=__a						)
              _UpperCamelCase   :						List[Any]    =							model(**__a						).loss
              loss.backward()

  def  __SCREAMING_SNAKE_CASE					(		self :							List[str]						)		->					Tuple:
        _UpperCamelCase,	_UpperCamelCase   :						Optional[int]    =							self.model_tester.prepare_config_and_inputs_for_common()

        _UpperCamelCase   :						List[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']}'''						):
                          _UpperCamelCase   :						str    =							problem_type["title"]
                          _UpperCamelCase   :						int    =							problem_type["num_labels"]

                          _UpperCamelCase   :						Dict    =							model_class(__a						)
                          model.to(__a						)
                          model.train()

                          _UpperCamelCase   :						Tuple    =							self._prepare_for_class(__a       , __a       , return_labels=__a						)

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

                          _UpperCamelCase   :						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:
                                _UpperCamelCase   :						Optional[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  __SCREAMING_SNAKE_CASE					(		self :							List[str]						)		->					Any:
        for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              _UpperCamelCase   :						Any    =							DeiTModel.from_pretrained(__a						)
              self.assertIsNotNone(__a						)





def        lowercase__     (       )       ->		Dict:




      """simple docstring"""

      _UpperCamelCase   :						List[Any]    =							Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"      )
      return image



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







  '''simple docstring'''


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

  @slow
  def  __SCREAMING_SNAKE_CASE					(		self :							Union[str, Any]						)		->					Union[str, Any]:
        _UpperCamelCase   :						Optional[int]    =							DeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224"						).to(
            __a						)

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

        # forward pass
        with torch.no_grad():
              _UpperCamelCase   :						Optional[int]    =							model(**__a						)

        # verify the logits
        _UpperCamelCase   :						Union[str, Any]    =							torch.Size((1, 1000)						)
        self.assertEqual(outputs.logits.shape       , __a						)

        _UpperCamelCase   :						str    =							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 :							Optional[int]						)		->					Tuple:
        _UpperCamelCase   :						Dict    =							DeiTModel.from_pretrained(
            "facebook/deit-base-distilled-patch16-224"       , torch_dtype=torch.floataa       , device_map="auto"						)
        _UpperCamelCase   :						Any    =							self.default_image_processor

        _UpperCamelCase   :						int    =							prepare_img()
        _UpperCamelCase   :						Union[str, Any]    =							image_processor(images=__a       , return_tensors="pt"						)
        _UpperCamelCase   :						Optional[int]    =							inputs.pixel_values.to(__a						)

        # forward pass to make sure inference works in fp16
        with torch.no_grad():
              _UpperCamelCase   :						int    =							model(__a						)







"""simple docstring"""


from collections.abc import Callable

import numpy as np




def        lowercase__     (       lowercase_		,lowercase_		,lowercase_		,lowercase_		,lowercase_      )       ->		np.ndarray:




      """simple docstring"""

      _UpperCamelCase   :						List[str]    =							int(np.ceil((x_end - xa) / step_size      )      )
      _UpperCamelCase   :						Tuple    =							np.zeros((n + 1,)      )
      _UpperCamelCase   :						List[str]    =							ya
      _UpperCamelCase   :						List[Any]    =							xa

      for k in range(lowercase_      ):
            _UpperCamelCase   :						List[str]    =							y[k] + step_size * ode_func(lowercase_		,y[k]      )
            x += step_size

      return y


if __name__ == "__main__":
  import doctest

  doctest.testmod()
