
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			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__=13			,						SCREAMING_SNAKE_CASE__=30			,						SCREAMING_SNAKE_CASE__=2			,						SCREAMING_SNAKE_CASE__=3			,						SCREAMING_SNAKE_CASE__=True			,						SCREAMING_SNAKE_CASE__=True			,						SCREAMING_SNAKE_CASE__=32			,						SCREAMING_SNAKE_CASE__=2			,						SCREAMING_SNAKE_CASE__=4			,						SCREAMING_SNAKE_CASE__=37			,						SCREAMING_SNAKE_CASE__="gelu"			,						SCREAMING_SNAKE_CASE__=0.1			,						SCREAMING_SNAKE_CASE__=0.1			,						SCREAMING_SNAKE_CASE__=10			,						SCREAMING_SNAKE_CASE__=0.02			,						SCREAMING_SNAKE_CASE__=3			,						SCREAMING_SNAKE_CASE__=None			,						SCREAMING_SNAKE_CASE__=2			,						):
								"""simple docstring"""







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

								# in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
								lowerCAmelCase__			:     str					   =      (image_size // patch_size) ** 2
								lowerCAmelCase__			:     Dict					   =      num_patches + 2
					def lowercase_      (		self     ):
								"""simple docstring"""







								lowerCAmelCase__			:     int					   =      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

								lowerCAmelCase__			:     Union[str, Any]					   =      None
								if self.use_labels:
											lowerCAmelCase__			:     str					   =      ids_tensor([self.batch_size]			,						self.type_sequence_label_size     )

								lowerCAmelCase__			:     Optional[int]					   =      self.get_config()

								return config, pixel_values, labels
					def lowercase_      (		self     ):
								"""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=UpperCamelCase__			,						initializer_range=self.initializer_range			,						encoder_stride=self.encoder_stride			,						)
					def lowercase_      (		self			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__     ):
								"""simple docstring"""







								lowerCAmelCase__			:     List[Any]					   =      TFDeiTModel(config=UpperCamelCase__     )
								lowerCAmelCase__			:     Optional[Any]					   =      model(UpperCamelCase__     )
								self.parent.assertEqual(result.last_hidden_state.shape			,						(self.batch_size, self.seq_length, self.hidden_size)     )
					def lowercase_      (		self			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__     ):
								"""simple docstring"""







								lowerCAmelCase__			:     Optional[Any]					   =      TFDeiTForMaskedImageModeling(config=UpperCamelCase__     )
								lowerCAmelCase__			:     Optional[Any]					   =      model(UpperCamelCase__     )
								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__			:     Optional[int]					   =      TFDeiTForMaskedImageModeling(UpperCamelCase__     )

								lowerCAmelCase__			:     Optional[int]					   =      floats_tensor([self.batch_size, 1, self.image_size, self.image_size]     )
								lowerCAmelCase__			:     str					   =      model(UpperCamelCase__     )
								self.parent.assertEqual(result.reconstruction.shape			,						(self.batch_size, 1, self.image_size, self.image_size)     )
					def lowercase_      (		self			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__     ):
								"""simple docstring"""







								lowerCAmelCase__			:     List[str]					   =      self.type_sequence_label_size
								lowerCAmelCase__			:     str					   =      TFDeiTForImageClassification(UpperCamelCase__     )
								lowerCAmelCase__			:     Tuple					   =      model(UpperCamelCase__			,						labels=UpperCamelCase__     )
								self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.type_sequence_label_size)     )

								# test greyscale images
								lowerCAmelCase__			:     str					   =      1
								lowerCAmelCase__			:     Tuple					   =      TFDeiTForImageClassification(UpperCamelCase__     )

								lowerCAmelCase__			:     Optional[int]					   =      floats_tensor([self.batch_size, 1, self.image_size, self.image_size]     )
								lowerCAmelCase__			:     Union[str, Any]					   =      model(UpperCamelCase__			,						labels=UpperCamelCase__     )
								self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.type_sequence_label_size)     )
					def lowercase_      (		self     ):
								"""simple docstring"""







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





@require_tf
class     lowercase							(	_A						,		_A						,		unittest.TestCase       ):
					__a											=     (
					    (
					        TFDeiTModel,
					        TFDeiTForImageClassification,
					        TFDeiTForImageClassificationWithTeacher,
					        TFDeiTForMaskedImageModeling,
					    )
					    if is_tf_available()
					    else ()
					)
					__a											=     (
					    {
					        """feature-extraction""": TFDeiTModel,
					        """image-classification""": (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
					    }
					    if is_tf_available()
					    else {}
					)

					__a											=     False
					__a											=     False
					__a											=     False
					__a											=     False
					def lowercase_      (		self     ):
								"""simple docstring"""







								lowerCAmelCase__			:     int					   =      TFDeiTModelTester(self     )
								lowerCAmelCase__			:     Optional[int]					   =      ConfigTester(self			,						config_class=UpperCamelCase__			,						has_text_modality=UpperCamelCase__			,						hidden_size=37     )
					def lowercase_      (		self     ):
								"""simple docstring"""







								self.config_tester.run_common_tests()
					@unittest.skip(reason='''DeiT does not use inputs_embeds'''     )
					def lowercase_      (		self     ):
								"""simple docstring"""







								pass
					def lowercase_      (		self     ):
								"""simple docstring"""







								lowerCAmelCase__   ,					lowerCAmelCase__			:     List[str]					   =      self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
											lowerCAmelCase__			:     str					   =      model_class(UpperCamelCase__     )
											self.assertIsInstance(model.get_input_embeddings()			,						(tf.keras.layers.Layer)     )
											lowerCAmelCase__			:     List[str]					   =      model.get_output_embeddings()
											self.assertTrue(x is None or isinstance(UpperCamelCase__			,						tf.keras.layers.Dense     )     )
					def lowercase_      (		self     ):
								"""simple docstring"""







								lowerCAmelCase__   ,					lowerCAmelCase__			:     Union[str, Any]					   =      self.model_tester.prepare_config_and_inputs_for_common()

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

											lowerCAmelCase__			:     int					   =      ['''pixel_values''']
											self.assertListEqual(arg_names[:1]			,						UpperCamelCase__     )
					def lowercase_      (		self     ):
								"""simple docstring"""







								lowerCAmelCase__			:     int					   =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*UpperCamelCase__     )
					def lowercase_      (		self     ):
								"""simple docstring"""







								lowerCAmelCase__			:     Dict					   =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_image_modeling(*UpperCamelCase__     )
					def lowercase_      (		self     ):
								"""simple docstring"""







								lowerCAmelCase__			:     Union[str, Any]					   =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__     )
					def lowercase_      (		self			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__=False     ):
								"""simple docstring"""







								lowerCAmelCase__			:     str					   =      super()._prepare_for_class(UpperCamelCase__			,						UpperCamelCase__			,						return_labels=UpperCamelCase__     )

								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     ):
								"""simple docstring"""







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





def   _a  ( ):
			lowerCAmelCase__			:     Dict					   =      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''     )
			return image





@require_tf
@require_vision
class     lowercase							(	unittest.TestCase       ):
					@cached_property
					def lowercase_      (		self     ):
								"""simple docstring"""







								return (
								    DeiTImageProcessor.from_pretrained('''facebook/deit-base-distilled-patch16-224'''     )
								    if is_vision_available()
								    else None
								)
					@slow
					def lowercase_      (		self     ):
								"""simple docstring"""







								lowerCAmelCase__			:     Tuple					   =      TFDeiTForImageClassificationWithTeacher.from_pretrained('''facebook/deit-base-distilled-patch16-224'''     )

								lowerCAmelCase__			:     Any					   =      self.default_image_processor
								lowerCAmelCase__			:     Dict					   =      prepare_img()
								lowerCAmelCase__			:     List[str]					   =      image_processor(images=UpperCamelCase__			,						return_tensors='''tf'''     )

								# forward pass
								lowerCAmelCase__			:     Optional[int]					   =      model(**UpperCamelCase__     )

								# verify the logits
								lowerCAmelCase__			:     Any					   =      tf.TensorShape((1, 1000)     )
								self.assertEqual(outputs.logits.shape			,						UpperCamelCase__     )

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

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





from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__lowerCAmelCase     :	Union[str, Any]       			=  {
    'configuration_informer': [
        'INFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'InformerConfig',
    ],
}

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    __lowerCAmelCase     :	Optional[Any]       			=  [
        'INFORMER_PRETRAINED_MODEL_ARCHIVE_LIST',
        'InformerForPrediction',
        'InformerModel',
        'InformerPreTrainedModel',
    ]


if TYPE_CHECKING:
    from .configuration_informer import INFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, InformerConfig

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_informer import (
            INFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
            InformerForPrediction,
            InformerModel,
            InformerPreTrainedModel,
        )

else:
    import sys

    __lowerCAmelCase     :	List[str]       			=  _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)

