


import os
from distutils.util import strtobool
def   _UpperCamelCase				(						lowerCAmelCase_  ,						lowerCAmelCase_   )						->Any:
   for e in env_keys:
      UpperCAmelCase							=     int(os.environ.get(lowerCAmelCase_  ,						-1   )   )
      if val >= 0:
         return val
   return default
def   _UpperCamelCase				(						lowerCAmelCase_  ,						lowerCAmelCase_=False   )						->Dict:
   UpperCAmelCase							=     os.environ.get(lowerCAmelCase_  ,						str(lowerCAmelCase_   )   )
   return strtobool(lowerCAmelCase_   ) == 1  # As its name indicates `strtobool` actually returns an int...







def   _UpperCamelCase				(						lowerCAmelCase_  ,						lowerCAmelCase_="no"   )						->Union[str, Any]:
   UpperCAmelCase							=     os.environ.get(lowerCAmelCase_  ,						str(lowerCAmelCase_   )   )
   return value







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





      def __init__(		self						:					str					,       __lowerCamelCase						:					Optional[Any]					,       __lowerCamelCase						:					Optional[Any]=1_3					,       __lowerCamelCase						:					Optional[Any]=3_0					,       __lowerCamelCase						:					Any=2					,       __lowerCamelCase						:					List[str]=3					,       __lowerCamelCase						:					str=True					,       __lowerCamelCase						:					str=True					,       __lowerCamelCase						:					Union[str, Any]=3_2					,       __lowerCamelCase						:					Optional[int]=2					,       __lowerCamelCase						:					Optional[int]=4					,       __lowerCamelCase						:					Union[str, Any]=3_7					,       __lowerCamelCase						:					str="gelu"					,       __lowerCamelCase						:					str=0.1					,       __lowerCamelCase						:					List[str]=0.1					,       __lowerCamelCase						:					str=1_0					,       __lowerCamelCase						:					Union[str, Any]=0.02					,       __lowerCamelCase						:					List[str]=3					,       __lowerCamelCase						:					Optional[Any]=None					,       __lowerCamelCase						:					Any=2					,       )    ->   Tuple:






         """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        _lowercase				(		self						:					Tuple       )    ->   int:






         """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        _lowercase				(		self						:					Tuple       )    ->   str:






         """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						:					Any					,       __lowerCamelCase						:					Optional[int]					,       __lowerCamelCase						:					Any					,       __lowerCamelCase						:					int       )    ->   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        _lowercase				(		self						:					Optional[Any]					,       __lowerCamelCase						:					int					,       __lowerCamelCase						:					Any					,       __lowerCamelCase						:					Optional[Any]       )    ->   Union[str, Any]:






         """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        _lowercase				(		self						:					Union[str, Any]					,       __lowerCamelCase						:					List[Any]					,       __lowerCamelCase						:					Dict					,       __lowerCamelCase						:					int       )    ->   Any:






         """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        _lowercase				(		self						:					Tuple       )    ->   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   __lowercase				(  __snake_case ,					__snake_case ,					unittest.TestCase  ):
      UpperCamelCase								=       (
          (
              TFDeiTModel,
              TFDeiTForImageClassification,
              TFDeiTForImageClassificationWithTeacher,
              TFDeiTForMaskedImageModeling,
          )
          if is_tf_available()
          else ()
      )
      UpperCamelCase								=       (
          {
              '''feature-extraction''': TFDeiTModel,
              '''image-classification''': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
          }
          if is_tf_available()
          else {}
      )

      UpperCamelCase								=       False
      UpperCamelCase								=       False
      UpperCamelCase								=       False
      UpperCamelCase								=       False





      def        _lowercase				(		self						:					str       )    ->   str:






         """simple docstring"""







         UpperCAmelCase							=     TFDeiTModelTester(self       )
         UpperCAmelCase							=     ConfigTester(self					,       config_class=__lowerCamelCase					,       has_text_modality=__lowerCamelCase					,       hidden_size=3_7       )





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






         """simple docstring"""







         self.config_tester.run_common_tests()





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






         """simple docstring"""







         pass





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






         """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        _lowercase				(		self						:					Optional[Any]       )    ->   List[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       )
            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        _lowercase				(		self						:					Optional[int]       )    ->   Tuple:






         """simple docstring"""







         UpperCAmelCase							=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__lowerCamelCase       )





      def        _lowercase				(		self						:					Any       )    ->   Optional[Any]:






         """simple docstring"""







         UpperCAmelCase							=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_image_modeling(*__lowerCamelCase       )





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






         """simple docstring"""







         UpperCAmelCase							=     self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase       )





      def        _lowercase				(		self						:					Optional[int]					,       __lowerCamelCase						:					List[Any]					,       __lowerCamelCase						:					List[Any]					,       __lowerCamelCase						:					Any=False       )    ->   int:






         """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        _lowercase				(		self						:					Optional[int]       )    ->   str:






         """simple docstring"""







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

def   _UpperCamelCase				(						)						->Tuple:
   UpperCAmelCase							=     Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""   )
   return image







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





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






         """simple docstring"""







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





      @slow
      def        _lowercase				(		self						:					Optional[int]       )    ->   List[str]:






         """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_0_0_0)       )
         self.assertEqual(outputs.logits.shape					,       __lowerCamelCase       )

         UpperCAmelCase							=     tf.constant([-1.0_266, 0.1_912, -1.2_861]       )

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



