






"""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     :							List[Any]       ,						__SCREAMING_SNAKE_CASE     :							List[Any]       ,						__SCREAMING_SNAKE_CASE     :							Any=13       ,						__SCREAMING_SNAKE_CASE     :							List[Any]=30       ,						__SCREAMING_SNAKE_CASE     :							List[str]=2       ,						__SCREAMING_SNAKE_CASE     :							List[Any]=3       ,						__SCREAMING_SNAKE_CASE     :							Optional[int]=True       ,						__SCREAMING_SNAKE_CASE     :							Optional[Any]=True       ,						__SCREAMING_SNAKE_CASE     :							List[str]=32       ,						__SCREAMING_SNAKE_CASE     :							Optional[Any]=5       ,						__SCREAMING_SNAKE_CASE     :							Tuple=4       ,						__SCREAMING_SNAKE_CASE     :							str=37       ,						__SCREAMING_SNAKE_CASE     :							str="gelu"       ,						__SCREAMING_SNAKE_CASE     :							Tuple=0.1       ,						__SCREAMING_SNAKE_CASE     :							int=0.1       ,						__SCREAMING_SNAKE_CASE     :							Dict=10       ,						__SCREAMING_SNAKE_CASE     :							int=0.02       ,						__SCREAMING_SNAKE_CASE     :							Tuple=3       ,						__SCREAMING_SNAKE_CASE     :							Tuple=None       ,						__SCREAMING_SNAKE_CASE     :							Optional[int]=2       ,						)     ->		str:
      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    UpperCamelCase       (       self     :							List[Any]					)     ->		Optional[Any]:
      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    UpperCamelCase       (       self     :							Optional[int]					)     ->		Dict:
      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=__SCREAMING_SNAKE_CASE       ,						initializer_range=self.initializer_range       ,						encoder_stride=self.encoder_stride       ,						)






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






    def    UpperCamelCase       (       self     :							int       ,						__SCREAMING_SNAKE_CASE     :							Union[str, Any]       ,						__SCREAMING_SNAKE_CASE     :							Optional[Any]       ,						__SCREAMING_SNAKE_CASE     :							int					)     ->		List[Any]:
      lowerCamelCase_         =						DeiTForMaskedImageModeling(config=__SCREAMING_SNAKE_CASE					)
      model.to(__SCREAMING_SNAKE_CASE					)
      model.eval()
      lowerCamelCase_         =						model(__SCREAMING_SNAKE_CASE					)
      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(__SCREAMING_SNAKE_CASE					)
      model.to(__SCREAMING_SNAKE_CASE					)
      model.eval()

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






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

      # test greyscale images
      lowerCamelCase_         =						1
      lowerCamelCase_         =						DeiTForImageClassification(__SCREAMING_SNAKE_CASE					)
      model.to(__SCREAMING_SNAKE_CASE					)
      model.eval()

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






    def    UpperCamelCase       (       self     :							str					)     ->		int:
      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		):
    SCREAMING_SNAKE_CASE						:     Tuple						= (
        (
            DeiTModel,
            DeiTForImageClassification,
            DeiTForImageClassificationWithTeacher,
            DeiTForMaskedImageModeling,
        )
        if is_torch_available()
        else ()
    )
    SCREAMING_SNAKE_CASE						:     List[str]						= (
        {
            """feature-extraction""": DeiTModel,
            """image-classification""": (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
        }
        if is_torch_available()
        else {}
    )

    SCREAMING_SNAKE_CASE						:     Optional[Any]						= False
    SCREAMING_SNAKE_CASE						:     Tuple						= False
    SCREAMING_SNAKE_CASE						:     int						= False






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






    def    UpperCamelCase       (       self     :							Dict					)     ->		int:
      self.config_tester.run_common_tests()






    @unittest.skip(reason='DeiT does not use inputs_embeds'					)
    def    UpperCamelCase       (       self     :							List[Any]					)     ->		Optional[int]:
      pass






    def    UpperCamelCase       (       self     :							Optional[Any]					)     ->		Tuple:
      lowerCamelCase_     ,	lowerCamelCase_         =						self.model_tester.prepare_config_and_inputs_for_common()

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






    def    UpperCamelCase       (       self     :							str					)     ->		Union[str, Any]:
      lowerCamelCase_     ,	lowerCamelCase_         =						self.model_tester.prepare_config_and_inputs_for_common()

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






    def    UpperCamelCase       (       self     :							Dict					)     ->		Tuple:
      lowerCamelCase_         =						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE					)






    def    UpperCamelCase       (       self     :							Tuple					)     ->		int:
      lowerCamelCase_         =						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_image_modeling(*__SCREAMING_SNAKE_CASE					)






    def    UpperCamelCase       (       self     :							Dict					)     ->		Optional[Any]:
      lowerCamelCase_         =						self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*__SCREAMING_SNAKE_CASE					)






    def    UpperCamelCase       (       self     :							str       ,						__SCREAMING_SNAKE_CASE     :							Any       ,						__SCREAMING_SNAKE_CASE     :							List[Any]       ,						__SCREAMING_SNAKE_CASE     :							Union[str, Any]=False					)     ->		Any:
      lowerCamelCase_         =						super()._prepare_for_class(__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       ,						return_labels=__SCREAMING_SNAKE_CASE					)

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

      return inputs_dict






    def    UpperCamelCase       (       self     :							str					)     ->		int:
      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(__SCREAMING_SNAKE_CASE					)
            or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
        ):
          continue
        lowerCamelCase_         =						model_class(__SCREAMING_SNAKE_CASE					)
        model.to(__SCREAMING_SNAKE_CASE					)
        model.train()
        lowerCamelCase_         =						self._prepare_for_class(__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       ,						return_labels=__SCREAMING_SNAKE_CASE					)
        lowerCamelCase_         =						model(**__SCREAMING_SNAKE_CASE					).loss
        loss.backward()






    def    UpperCamelCase       (       self     :							Optional[Any]					)     ->		int:
      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(__SCREAMING_SNAKE_CASE					) or not model_class.supports_gradient_checkpointing:
          continue
        # DeiTForImageClassificationWithTeacher supports inference-only
        if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
          continue
        lowerCamelCase_         =						model_class(__SCREAMING_SNAKE_CASE					)
        model.gradient_checkpointing_enable()
        model.to(__SCREAMING_SNAKE_CASE					)
        model.train()
        lowerCamelCase_         =						self._prepare_for_class(__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       ,						return_labels=__SCREAMING_SNAKE_CASE					)
        lowerCamelCase_         =						model(**__SCREAMING_SNAKE_CASE					).loss
        loss.backward()






    def    UpperCamelCase       (       self     :							Union[str, Any]					)     ->		Tuple:
      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(__SCREAMING_SNAKE_CASE					),
                *get_values(__SCREAMING_SNAKE_CASE					),
            ]
            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(__SCREAMING_SNAKE_CASE					)
            model.to(__SCREAMING_SNAKE_CASE					)
            model.train()

            lowerCamelCase_         =						self._prepare_for_class(__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       ,						return_labels=__SCREAMING_SNAKE_CASE					)

            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=__SCREAMING_SNAKE_CASE					) as warning_list:
              lowerCamelCase_         =						model(**__SCREAMING_SNAKE_CASE					).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     :							List[str]					)     ->		Tuple:
      for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        lowerCamelCase_         =						DeiTModel.from_pretrained(__SCREAMING_SNAKE_CASE					)
        self.assertIsNotNone(__SCREAMING_SNAKE_CASE					)







def      lowerCamelCase__       (							)      ->				Any:
  lowerCamelCase_         =						Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png' )
  return image






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






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






    @slow
    def    UpperCamelCase       (       self     :							Optional[Any]					)     ->		int:
      lowerCamelCase_         =						DeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224'					).to(
          __SCREAMING_SNAKE_CASE					)

      lowerCamelCase_         =						self.default_image_processor
      lowerCamelCase_         =						prepare_img()
      lowerCamelCase_         =						image_processor(images=__SCREAMING_SNAKE_CASE       ,						return_tensors='pt'					).to(__SCREAMING_SNAKE_CASE					)

      # forward pass
      with torch.no_grad():
        lowerCamelCase_         =						model(**__SCREAMING_SNAKE_CASE					)

      # verify the logits
      lowerCamelCase_         =						torch.Size((1, 1000)					)
      self.assertEqual(outputs.logits.shape       ,						__SCREAMING_SNAKE_CASE					)

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

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






    @slow
    @require_accelerate
    @require_torch_gpu
    def    UpperCamelCase       (       self     :							str					)     ->		Dict:
      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=__SCREAMING_SNAKE_CASE       ,						return_tensors='pt'					)
      lowerCamelCase_         =						inputs.pixel_values.to(__SCREAMING_SNAKE_CASE					)

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












"""simple docstring"""





from math import pow, sqrt







def      lowerCamelCase__       (							*_lowerCamelCase       :		float )      ->				bool:
  lowerCamelCase_         =						len(_lowerCamelCase ) > 0 and all(value > 0.0 for value in values )
  return result







def      lowerCamelCase__       (							_lowerCamelCase       :		float		,				_lowerCamelCase       :		float )      ->				float | ValueError:
  return (
      round(sqrt(molar_mass_a / molar_mass_a )		,				6 )
      if validate(_lowerCamelCase		,				_lowerCamelCase )
      else ValueError('Input Error: Molar mass values must greater than 0.' )
  )







def      lowerCamelCase__       (							_lowerCamelCase       :		float		,				_lowerCamelCase       :		float		,				_lowerCamelCase       :		float )      ->				float | ValueError:
  return (
      round(effusion_rate * sqrt(molar_mass_a / molar_mass_a )		,				6 )
      if validate(_lowerCamelCase		,				_lowerCamelCase		,				_lowerCamelCase )
      else ValueError(
          'Input Error: Molar mass and effusion rate values must greater than 0.' )
  )







def      lowerCamelCase__       (							_lowerCamelCase       :		float		,				_lowerCamelCase       :		float		,				_lowerCamelCase       :		float )      ->				float | ValueError:
  return (
      round(effusion_rate / sqrt(molar_mass_a / molar_mass_a )		,				6 )
      if validate(_lowerCamelCase		,				_lowerCamelCase		,				_lowerCamelCase )
      else ValueError(
          'Input Error: Molar mass and effusion rate values must greater than 0.' )
  )







def      lowerCamelCase__       (							_lowerCamelCase       :		float		,				_lowerCamelCase       :		float		,				_lowerCamelCase       :		float )      ->				float | ValueError:
  return (
      round(molar_mass / pow(effusion_rate_a / effusion_rate_a		,				2 )		,				6 )
      if validate(_lowerCamelCase		,				_lowerCamelCase		,				_lowerCamelCase )
      else ValueError(
          'Input Error: Molar mass and effusion rate values must greater than 0.' )
  )







def      lowerCamelCase__       (							_lowerCamelCase       :		float		,				_lowerCamelCase       :		float		,				_lowerCamelCase       :		float )      ->				float | ValueError:
  return (
      round(pow(effusion_rate_a / effusion_rate_a		,				2 ) / molar_mass		,				6 )
      if validate(_lowerCamelCase		,				_lowerCamelCase		,				_lowerCamelCase )
      else ValueError(
          'Input Error: Molar mass and effusion rate values must greater than 0.' )
  )




