


"""simple docstring"""






from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import DeiTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
  import tensorflow as tf

  from transformers import (
      TFDeiTForImageClassification,
      TFDeiTForImageClassificationWithTeacher,
      TFDeiTForMaskedImageModeling,
      TFDeiTModel,
  )
  from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import DeiTImageProcessor




class 							A_      :


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







     '''simple docstring'''







     _lowerCamelCase   :				Optional[int]							=     parent
     _lowerCamelCase   :				List[Any]							=     batch_size
     _lowerCamelCase   :				Optional[Any]							=     image_size
     _lowerCamelCase   :				List[str]							=     patch_size
     _lowerCamelCase   :				List[str]							=     num_channels
     _lowerCamelCase   :				List[Any]							=     is_training
     _lowerCamelCase   :				List[Any]							=     use_labels
     _lowerCamelCase   :				Optional[int]							=     hidden_size
     _lowerCamelCase   :				Optional[int]							=     num_hidden_layers
     _lowerCamelCase   :				Optional[Any]							=     num_attention_heads
     _lowerCamelCase   :				List[Any]							=     intermediate_size
     _lowerCamelCase   :				List[str]							=     hidden_act
     _lowerCamelCase   :				List[str]							=     hidden_dropout_prob
     _lowerCamelCase   :				List[str]							=     attention_probs_dropout_prob
     _lowerCamelCase   :				List[Any]							=     type_sequence_label_size
     _lowerCamelCase   :				str							=     initializer_range
     _lowerCamelCase   :				Any							=     scope
     _lowerCamelCase   :				List[str]							=     encoder_stride

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


  def 							_lowercase					(			self:						str    ):







     '''simple docstring'''







     _lowerCamelCase   :				List[str]							=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

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

     _lowerCamelCase   :				int							=     self.get_config()

     return config, pixel_values, labels


  def 							_lowercase					(			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=__lowerCAmelCase							,initializer_range=self.initializer_range							,encoder_stride=self.encoder_stride							,)


  def 							_lowercase					(			self:						str							,__lowerCAmelCase:						Optional[Any]							,__lowerCAmelCase:						Tuple							,__lowerCAmelCase:						Optional[Any]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Dict							=     TFDeiTModel(config=__lowerCAmelCase    )
     _lowerCamelCase   :				Any							=     model(__lowerCAmelCase    )
     self.parent.assertEqual(result.last_hidden_state.shape							,(self.batch_size, self.seq_length, self.hidden_size)    )


  def 							_lowercase					(			self:						List[str]							,__lowerCAmelCase:						List[Any]							,__lowerCAmelCase:						List[Any]							,__lowerCAmelCase:						int    ):







     '''simple docstring'''







     _lowerCamelCase   :				Dict							=     TFDeiTForMaskedImageModeling(config=__lowerCAmelCase    )
     _lowerCamelCase   :				Optional[Any]							=     model(__lowerCAmelCase    )
     self.parent.assertEqual(
         result.reconstruction.shape							,(self.batch_size, self.num_channels, self.image_size, self.image_size)    )

     # test greyscale images
     _lowerCamelCase   :				Any							=     1
     _lowerCamelCase   :				Union[str, Any]							=     TFDeiTForMaskedImageModeling(__lowerCAmelCase    )

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


  def 							_lowercase					(			self:						Optional[Any]							,__lowerCAmelCase:						Union[str, Any]							,__lowerCAmelCase:						List[str]							,__lowerCAmelCase:						int    ):







     '''simple docstring'''







     _lowerCamelCase   :				List[Any]							=     self.type_sequence_label_size
     _lowerCamelCase   :				List[Any]							=     TFDeiTForImageClassification(__lowerCAmelCase    )
     _lowerCamelCase   :				List[Any]							=     model(__lowerCAmelCase							,labels=__lowerCAmelCase    )
     self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.type_sequence_label_size)    )

     # test greyscale images
     _lowerCamelCase   :				int							=     1
     _lowerCamelCase   :				Dict							=     TFDeiTForImageClassification(__lowerCAmelCase    )

     _lowerCamelCase   :				List[Any]							=     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]    )
     _lowerCamelCase   :				Dict							=     model(__lowerCAmelCase							,labels=__lowerCAmelCase    )
     self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.type_sequence_label_size)    )






  def 							_lowercase					(			self:						Dict    ):







     '''simple docstring'''







     _lowerCamelCase   :				Any							=     self.prepare_config_and_inputs()
     _lowerCamelCase,					_lowerCamelCase,					_lowerCamelCase   :				Optional[Any]							=     config_and_inputs
     _lowerCamelCase   :				int							=     {"pixel_values": pixel_values}
     return config, inputs_dict




@require_tf
class 							A_      (       _a     ,	_a     ,	unittest.TestCase ):
  lowerCAmelCase__      			=	(
      (
          TFDeiTModel,
          TFDeiTForImageClassification,
          TFDeiTForImageClassificationWithTeacher,
          TFDeiTForMaskedImageModeling,
      )
      if is_tf_available()
      else ()
  )
  lowerCAmelCase__      			=	(
      {
          'feature-extraction': TFDeiTModel,
          'image-classification': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
      }
      if is_tf_available()
      else {}
  )

  lowerCAmelCase__      			=	False
  lowerCAmelCase__      			=	False
  lowerCAmelCase__      			=	False
  lowerCAmelCase__      			=	False


  def 							_lowercase					(			self:						str    ):







     '''simple docstring'''







     _lowerCamelCase   :				Union[str, Any]							=     TFDeiTModelTester(self    )
     _lowerCamelCase   :				str							=     ConfigTester(self							,config_class=__lowerCAmelCase							,has_text_modality=__lowerCAmelCase							,hidden_size=37    )


  def 							_lowercase					(			self:						Union[str, Any]    ):







     '''simple docstring'''







     self.config_tester.run_common_tests()


  @unittest.skip(reason="DeiT does not use inputs_embeds"    )
  def 							_lowercase					(			self:						Optional[int]    ):







     '''simple docstring'''







     pass


  def 							_lowercase					(			self:						str    ):







     '''simple docstring'''







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

     for model_class in self.all_model_classes:
        _lowerCamelCase   :				Dict							=     model_class(__lowerCAmelCase    )
        self.assertIsInstance(model.get_input_embeddings()							,(tf.keras.layers.Layer)    )
        _lowerCamelCase   :				int							=     model.get_output_embeddings()
        self.assertTrue(x is None or isinstance(__lowerCAmelCase							,tf.keras.layers.Dense    )    )


  def 							_lowercase					(			self:						List[Any]    ):







     '''simple docstring'''







     _lowerCamelCase,					_lowerCamelCase   :				Dict							=     self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
        _lowerCamelCase   :				Any							=     model_class(__lowerCAmelCase    )
        _lowerCamelCase   :				Optional[Any]							=     inspect.signature(model.call    )
        # signature.parameters is an OrderedDict => so arg_names order is deterministic
        _lowerCamelCase   :				List[Any]							=     [*signature.parameters.keys()]

        _lowerCamelCase   :				Any							=     ["pixel_values"]
        self.assertListEqual(arg_names[:1]							,__lowerCAmelCase    )


  def 							_lowercase					(			self:						Any    ):







     '''simple docstring'''







     _lowerCamelCase   :				str							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*__lowerCAmelCase    )


  def 							_lowercase					(			self:						Optional[Any]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Any							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_image_modeling(*__lowerCAmelCase    )


  def 							_lowercase					(			self:						Optional[Any]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Union[str, Any]							=     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*__lowerCAmelCase    )


  def 							_lowercase					(			self:						Optional[int]							,__lowerCAmelCase:						Dict							,__lowerCAmelCase:						Tuple							,__lowerCAmelCase:						Union[str, Any]=False    ):







     '''simple docstring'''







     _lowerCamelCase   :				List[Any]							=     super()._prepare_for_class(__lowerCAmelCase							,__lowerCAmelCase							,return_labels=__lowerCAmelCase    )

     if return_labels:
        if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call    ).parameters:
           del inputs_dict["labels"]

     return inputs_dict






  @slow
  def 							_lowercase					(			self:						int    ):







     '''simple docstring'''







     for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        _lowerCamelCase   :				Optional[int]							=     TFDeiTModel.from_pretrained(__lowerCAmelCase    )
        self.assertIsNotNone(__lowerCAmelCase    )







def        lowerCamelCase_(		)    ->     Optional[int]:
   '''simple docstring'''





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




@require_tf
@require_vision
class 							A_      (       unittest.TestCase ):


  @cached_property
  def 							_lowercase					(			self:						Any    ):







     '''simple docstring'''







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






  @slow
  def 							_lowercase					(			self:						List[Any]    ):







     '''simple docstring'''







     _lowerCamelCase   :				Optional[Any]							=     TFDeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224"    )

     _lowerCamelCase   :				List[Any]							=     self.default_image_processor
     _lowerCamelCase   :				Optional[Any]							=     prepare_img()
     _lowerCamelCase   :				List[Any]							=     image_processor(images=__lowerCAmelCase							,return_tensors="tf"    )

     # forward pass
     _lowerCamelCase   :				List[str]							=     model(**__lowerCAmelCase    )

     # verify the logits
     _lowerCamelCase   :				List[Any]							=     tf.TensorShape((1, 1_000)    )
     self.assertEqual(outputs.logits.shape							,__lowerCAmelCase    )

     _lowerCamelCase   :				Dict							=     tf.constant([-1.02_66, 0.19_12, -1.28_61]    )

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





from packaging import version

from .import_utils import is_accelerate_available


if is_accelerate_available():
				import accelerate

def lowercase    (  SCREAMING_SNAKE_CASE )			->							Union[str, Any]:


			'''simple docstring'''







			if not is_accelerate_available():
						return method
			SCREAMING_SNAKE_CASE_								=						version.parse(accelerate.__version__ ).base_version
			if version.parse(SCREAMING_SNAKE_CASE ) < version.parse('0.17.0' ):
						return method

			def wrapper(self       ,    *SCREAMING_SNAKE_CASE       ,    **SCREAMING_SNAKE_CASE ):
						if hasattr(self       ,    '_hf_hook' ) and hasattr(self._hf_hook       ,    'pre_forward' ):
									self._hf_hook.pre_forward(self )
						return method(self       ,    *SCREAMING_SNAKE_CASE       ,    **SCREAMING_SNAKE_CASE )

			return wrapper
