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 			UpperCAmelCase__    :



  """simple docstring"""






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




    '''simple docstring'''




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

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






  def 	_UpperCAmelCase				(				self:			Optional[int]   ) -> Tuple:




    '''simple docstring'''




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

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

    __UpperCAmelCase						    =       self.get_config()

    return config, pixel_values, labels






  def 	_UpperCAmelCase				(				self:			List[str]   ) -> Optional[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 	_UpperCAmelCase				(				self:			List[Any]				,				__lowerCAmelCase:			Any				,				__lowerCAmelCase:			List[str]				,				__lowerCAmelCase:			Optional[Any]   ) -> Union[str, Any]:




    '''simple docstring'''




    __UpperCAmelCase						    =       TFDeiTModel(config=__lowerCAmelCase   )
    __UpperCAmelCase						    =       model(__lowerCAmelCase   )
    self.parent.assertEqual(result.last_hidden_state.shape				,				(self.batch_size, self.seq_length, self.hidden_size)   )






  def 	_UpperCAmelCase				(				self:			Optional[Any]				,				__lowerCAmelCase:			Union[str, Any]				,				__lowerCAmelCase:			Optional[int]				,				__lowerCAmelCase:			Optional[Any]   ) -> Dict:




    '''simple docstring'''




    __UpperCAmelCase						    =       TFDeiTForMaskedImageModeling(config=__lowerCAmelCase   )
    __UpperCAmelCase						    =       model(__lowerCAmelCase   )
    self.parent.assertEqual(
        result.reconstruction.shape				,				(self.batch_size, self.num_channels, self.image_size, self.image_size)   )

    # test greyscale images
    __UpperCAmelCase						    =       1
    __UpperCAmelCase						    =       TFDeiTForMaskedImageModeling(__lowerCAmelCase   )

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






  def 	_UpperCAmelCase				(				self:			int				,				__lowerCAmelCase:			Optional[Any]				,				__lowerCAmelCase:			str				,				__lowerCAmelCase:			Dict   ) -> Tuple:




    '''simple docstring'''




    __UpperCAmelCase						    =       self.type_sequence_label_size
    __UpperCAmelCase						    =       TFDeiTForImageClassification(__lowerCAmelCase   )
    __UpperCAmelCase						    =       model(__lowerCAmelCase				,				labels=__lowerCAmelCase   )
    self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.type_sequence_label_size)   )

    # test greyscale images
    __UpperCAmelCase						    =       1
    __UpperCAmelCase						    =       TFDeiTForImageClassification(__lowerCAmelCase   )

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






  def 	_UpperCAmelCase				(				self:			str   ) -> List[str]:




    '''simple docstring'''




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






@require_tf
class 			UpperCAmelCase__    (		snake_case		,	snake_case		,	unittest.TestCase					):



  """simple docstring"""






  lowerCAmelCase__		:	Union[str, Any]    		= (
      (
          TFDeiTModel,
          TFDeiTForImageClassification,
          TFDeiTForImageClassificationWithTeacher,
          TFDeiTForMaskedImageModeling,
      )
      if is_tf_available()
      else ()
  )
  lowerCAmelCase__		:	Dict    		= (
      {
          'feature-extraction': TFDeiTModel,
          'image-classification': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
      }
      if is_tf_available()
      else {}
  )

  lowerCAmelCase__		:	List[Any]    		= False
  lowerCAmelCase__		:	Any    		= False
  lowerCAmelCase__		:	Union[str, Any]    		= False
  lowerCAmelCase__		:	Dict    		= False






  def 	_UpperCAmelCase				(				self:			List[str]   ) -> Optional[Any]:




    '''simple docstring'''




    __UpperCAmelCase						    =       TFDeiTModelTester(self   )
    __UpperCAmelCase						    =       ConfigTester(self				,				config_class=__lowerCAmelCase				,				has_text_modality=__lowerCAmelCase				,				hidden_size=37   )






  def 	_UpperCAmelCase				(				self:			Optional[int]   ) -> Optional[Any]:




    '''simple docstring'''




    self.config_tester.run_common_tests()






  @unittest.skip(reason="DeiT does not use inputs_embeds"   )
  def 	_UpperCAmelCase				(				self:			List[Any]   ) -> Dict:




    '''simple docstring'''




    pass






  def 	_UpperCAmelCase				(				self:			List[Any]   ) -> str:




    '''simple docstring'''




    __UpperCAmelCase    ,		__UpperCAmelCase						    =       self.model_tester.prepare_config_and_inputs_for_common()

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






  def 	_UpperCAmelCase				(				self:			Tuple   ) -> Tuple:




    '''simple docstring'''




    __UpperCAmelCase    ,		__UpperCAmelCase						    =       self.model_tester.prepare_config_and_inputs_for_common()

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

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






  def 	_UpperCAmelCase				(				self:			Optional[int]   ) -> int:




    '''simple docstring'''




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






  def 	_UpperCAmelCase				(				self:			Tuple   ) -> Dict:




    '''simple docstring'''




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






  def 	_UpperCAmelCase				(				self:			Optional[int]   ) -> List[str]:




    '''simple docstring'''




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






  def 	_UpperCAmelCase				(				self:			List[str]				,				__lowerCAmelCase:			Tuple				,				__lowerCAmelCase:			Optional[int]				,				__lowerCAmelCase:			Union[str, Any]=False   ) -> List[str]:




    '''simple docstring'''




    __UpperCAmelCase						    =       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 	_UpperCAmelCase				(				self:			Tuple   ) -> int:




    '''simple docstring'''




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






def       __lowerCAmelCase      (						)    ->  Any:
  __UpperCAmelCase						    =       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"					)
  return image





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



  """simple docstring"""






  @cached_property
  def 	_UpperCAmelCase				(				self:			Any   ) -> Union[str, Any]:




    '''simple docstring'''




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






  @slow
  def 	_UpperCAmelCase				(				self:			Union[str, Any]   ) -> Union[str, Any]:




    '''simple docstring'''




    __UpperCAmelCase						    =       TFDeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224"   )

    __UpperCAmelCase						    =       self.default_image_processor
    __UpperCAmelCase						    =       prepare_img()
    __UpperCAmelCase						    =       image_processor(images=__lowerCAmelCase				,				return_tensors="tf"   )

    # forward pass
    __UpperCAmelCase						    =       model(**__lowerCAmelCase   )

    # verify the logits
    __UpperCAmelCase						    =       tf.TensorShape((1, 1_000)   )
    self.assertEqual(outputs.logits.shape				,				__lowerCAmelCase   )

    __UpperCAmelCase						    =       tf.constant([-1.0266, 0.1912, -1.2861]   )

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




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


a_							=      {
    """configuration_altclip""": [
        """ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """AltCLIPConfig""",
        """AltCLIPTextConfig""",
        """AltCLIPVisionConfig""",
    ],
    """processing_altclip""": ["""AltCLIPProcessor"""],
}

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       a_							=      [
           """ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST""",
           """AltCLIPPreTrainedModel""",
           """AltCLIPModel""",
           """AltCLIPTextModel""",
           """AltCLIPVisionModel""",
       ]


if TYPE_CHECKING:
       from .configuration_altclip import (
           ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
           AltCLIPConfig,
           AltCLIPTextConfig,
           AltCLIPVisionConfig,
       )
       from .processing_altclip import AltCLIPProcessor

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_altclip import (
                  ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
                  AltCLIPModel,
                  AltCLIPPreTrainedModel,
                  AltCLIPTextModel,
                  AltCLIPVisionModel,
              )


else:
       import sys

       a_							=      _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



