






"""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        lowercase__      :


			def __init__(				self					:  int       ,	snake_case__					:  Optional[int]       ,	snake_case__					:  List[str]=13       ,	snake_case__					:  Dict=30       ,	snake_case__					:  Any=2       ,	snake_case__					:  Optional[Any]=3       ,	snake_case__					:  Any=True       ,	snake_case__					:  Dict=True       ,	snake_case__					:  Dict=32       ,	snake_case__					:  Dict=2       ,	snake_case__					:  str=4       ,	snake_case__					:  Tuple=37       ,	snake_case__					:  Optional[Any]="gelu"       ,	snake_case__					:  int=0.1       ,	snake_case__					:  Union[str, Any]=0.1       ,	snake_case__					:  int=10       ,	snake_case__					:  Tuple=0.02       ,	snake_case__					:  List[str]=3       ,	snake_case__					:  Union[str, Any]=None       ,	snake_case__					:  Tuple=2       ,	):
										lowerCamelCase_	:							Any									=parent
										lowerCamelCase_	:							List[str]									=batch_size
										lowerCamelCase_	:							Dict									=image_size
										lowerCamelCase_	:							Any									=patch_size
										lowerCamelCase_	:							List[str]									=num_channels
										lowerCamelCase_	:							int									=is_training
										lowerCamelCase_	:							Optional[Any]									=use_labels
										lowerCamelCase_	:							List[str]									=hidden_size
										lowerCamelCase_	:							Optional[int]									=num_hidden_layers
										lowerCamelCase_	:							Optional[Any]									=num_attention_heads
										lowerCamelCase_	:							List[str]									=intermediate_size
										lowerCamelCase_	:							Any									=hidden_act
										lowerCamelCase_	:							Optional[int]									=hidden_dropout_prob
										lowerCamelCase_	:							Dict									=attention_probs_dropout_prob
										lowerCamelCase_	:							Optional[Any]									=type_sequence_label_size
										lowerCamelCase_	:							Dict									=initializer_range
										lowerCamelCase_	:							Optional[int]									=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_	:							Tuple									=num_patches + 2


			def      UpperCAmelCase__						(				self					:  int ):
										lowerCamelCase_	:							int									=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

										lowerCamelCase_	:							Dict									=None
										if self.use_labels:
																	lowerCamelCase_	:							List[Any]									=ids_tensor([self.batch_size]       ,	self.type_sequence_label_size )

										lowerCamelCase_	:							int									=self.get_config()

										return config, pixel_values, labels


			def      UpperCAmelCase__						(				self					:  Tuple ):
										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=_A       ,	initializer_range=self.initializer_range       ,	encoder_stride=self.encoder_stride       ,	)


			def      UpperCAmelCase__						(				self					:  Tuple       ,	snake_case__					:  str       ,	snake_case__					:  str       ,	snake_case__					:  Optional[Any] ):
										lowerCamelCase_	:							Tuple									=TFDeiTModel(config=_A )
										lowerCamelCase_	:							str									=model(_A )
										self.parent.assertEqual(result.last_hidden_state.shape       ,	(self.batch_size, self.seq_length, self.hidden_size) )


			def      UpperCAmelCase__						(				self					:  Union[str, Any]       ,	snake_case__					:  Any       ,	snake_case__					:  int       ,	snake_case__					:  Union[str, Any] ):
										lowerCamelCase_	:							Optional[Any]									=TFDeiTForMaskedImageModeling(config=_A )
										lowerCamelCase_	:							Any									=model(_A )
										self.parent.assertEqual(
										    result.reconstruction.shape       ,	(self.batch_size, self.num_channels, self.image_size, self.image_size) )

										# test greyscale images
										lowerCamelCase_	:							int									=1
										lowerCamelCase_	:							Optional[int]									=TFDeiTForMaskedImageModeling(_A )

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


			def      UpperCAmelCase__						(				self					:  Optional[int]       ,	snake_case__					:  List[Any]       ,	snake_case__					:  List[str]       ,	snake_case__					:  Optional[int] ):
										lowerCamelCase_	:							int									=self.type_sequence_label_size
										lowerCamelCase_	:							Union[str, Any]									=TFDeiTForImageClassification(_A )
										lowerCamelCase_	:							Optional[int]									=model(_A       ,	labels=_A )
										self.parent.assertEqual(result.logits.shape       ,	(self.batch_size, self.type_sequence_label_size) )

										# test greyscale images
										lowerCamelCase_	:							int									=1
										lowerCamelCase_	:							List[Any]									=TFDeiTForImageClassification(_A )

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


			def      UpperCAmelCase__						(				self					:  Dict ):
										lowerCamelCase_	:							List[str]									=self.prepare_config_and_inputs()
										lowerCamelCase_							,						lowerCamelCase_							,						lowerCamelCase_	:							str									=config_and_inputs
										lowerCamelCase_	:							List[str]									={"pixel_values": pixel_values}
										return config, inputs_dict







@require_tf
class        lowercase__      (							snake_case__,       snake_case__,       unittest.TestCase       ):
			_UpperCAmelCase		:Optional[Any]             =			(
			    (
			        TFDeiTModel,
			        TFDeiTForImageClassification,
			        TFDeiTForImageClassificationWithTeacher,
			        TFDeiTForMaskedImageModeling,
			    )
			    if is_tf_available()
			    else ()
			)
			_UpperCAmelCase		:Optional[Any]             =			(
			    {
			        """feature-extraction""": TFDeiTModel,
			        """image-classification""": (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
			    }
			    if is_tf_available()
			    else {}
			)

			_UpperCAmelCase		:str             =			False
			_UpperCAmelCase		:str             =			False
			_UpperCAmelCase		:List[str]             =			False
			_UpperCAmelCase		:Optional[int]             =			False


			def      UpperCAmelCase__						(				self					:  List[str] ):
										lowerCamelCase_	:							int									=TFDeiTModelTester(self )
										lowerCamelCase_	:							Optional[Any]									=ConfigTester(self       ,	config_class=_A       ,	has_text_modality=_A       ,	hidden_size=37 )


			def      UpperCAmelCase__						(				self					:  str ):
										self.config_tester.run_common_tests()


			@unittest.skip(reason="DeiT does not use inputs_embeds" )
			def      UpperCAmelCase__						(				self					:  List[str] ):
										pass


			def      UpperCAmelCase__						(				self					:  Tuple ):
										lowerCamelCase_							,						lowerCamelCase_	:							Union[str, Any]									=self.model_tester.prepare_config_and_inputs_for_common()

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


			def      UpperCAmelCase__						(				self					:  Tuple ):
										lowerCamelCase_							,						lowerCamelCase_	:							List[Any]									=self.model_tester.prepare_config_and_inputs_for_common()

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

																	lowerCamelCase_	:							List[Any]									=["pixel_values"]
																	self.assertListEqual(arg_names[:1]       ,	_A )


			def      UpperCAmelCase__						(				self					:  List[Any] ):
										lowerCamelCase_	:							List[Any]									=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_A )


			def      UpperCAmelCase__						(				self					:  List[str] ):
										lowerCamelCase_	:							List[Any]									=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_image_modeling(*_A )


			def      UpperCAmelCase__						(				self					:  List[str] ):
										lowerCamelCase_	:							Optional[Any]									=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*_A )


			def      UpperCAmelCase__						(				self					:  List[str]       ,	snake_case__					:  str       ,	snake_case__					:  Optional[int]       ,	snake_case__					:  Optional[int]=False ):
										lowerCamelCase_	:							Dict									=super()._prepare_for_class(_A       ,	_A       ,	return_labels=_A )

										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					:  Optional[int] ):



										for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowerCamelCase_	:							int									=TFDeiTModel.from_pretrained(_A )
																	self.assertIsNotNone(_A )


def 			_snake_case    (					)    ->     Optional[int]:
							lowerCamelCase_	:							int									=Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"  )
							return image







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


			@cached_property
			def      UpperCAmelCase__						(				self					:  List[Any] ):
										return (
										    DeiTImageProcessor.from_pretrained("facebook/deit-base-distilled-patch16-224" )
										    if is_vision_available()
										    else None
										)


			@slow
			def      UpperCAmelCase__						(				self					:  Optional[Any] ):
										lowerCamelCase_	:							Dict									=TFDeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224" )

										lowerCamelCase_	:							List[str]									=self.default_image_processor
										lowerCamelCase_	:							int									=prepare_img()
										lowerCamelCase_	:							Union[str, Any]									=image_processor(images=_A       ,	return_tensors="tf" )

										# forward pass
										lowerCamelCase_	:							List[Any]									=model(**_A )

										# verify the logits
										lowerCamelCase_	:							Optional[Any]									=tf.TensorShape((1, 1000) )
										self.assertEqual(outputs.logits.shape       ,	_A )

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

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











"""simple docstring"""



import copy
import json
import os
import tempfile

from transformers import is_torch_available

from .test_configuration_utils import config_common_kwargs







class        lowercase__      (							snake_case__       ):


			def __init__(				self					:  Tuple       ,	snake_case__					:  Optional[int]       ,	snake_case__					:  int=None       ,	snake_case__					:  Union[str, Any]=True       ,	snake_case__					:  Optional[int]=None       ,	**snake_case__					:  Optional[int] ):
										lowerCamelCase_	:							Dict									=parent
										lowerCamelCase_	:							List[str]									=config_class
										lowerCamelCase_	:							Union[str, Any]									=has_text_modality
										lowerCamelCase_	:							Optional[int]									=kwargs
										lowerCamelCase_	:							List[str]									=common_properties


			def      UpperCAmelCase__						(				self					:  Optional[Any] ):
										lowerCamelCase_	:							List[str]									=self.config_class(**self.inputs_dict )
										lowerCamelCase_	:							Any									=(
										    ["hidden_size", "num_attention_heads", "num_hidden_layers"]
										    if self.common_properties is None
										    else self.common_properties
										)

										# Add common fields for text models
										if self.has_text_modality:
																	common_properties.extend(["vocab_size"] )

										# Test that config has the common properties as getters
										for prop in common_properties:
																	self.parent.assertTrue(hasattr(snake_case__       ,	snake_case__ )       ,	msg=F"""`{prop}` does not exist""" )

										# Test that config has the common properties as setter
										for idx, name in enumerate(snake_case__ ):
																	try:
																								setattr(snake_case__       ,	snake_case__       ,	snake_case__ )
																								self.parent.assertEqual(
																								    getattr(snake_case__       ,	snake_case__ )       ,	snake_case__       ,	msg=F"""`{name} value {idx} expected, but was {getattr(snake_case__       ,	snake_case__ )}""" )
																	except NotImplementedError:
																								# Some models might not be able to implement setters for common_properties
																								# In that case, a NotImplementedError is raised
																								pass

        # Test if config class can be called with Config(prop_name=..)
										for idx, name in enumerate(snake_case__ ):
																	try:
																								lowerCamelCase_	:							Dict									=self.config_class(**{name: idx} )
																								self.parent.assertEqual(
																								    getattr(snake_case__       ,	snake_case__ )       ,	snake_case__       ,	msg=F"""`{name} value {idx} expected, but was {getattr(snake_case__       ,	snake_case__ )}""" )
																	except NotImplementedError:
																								# Some models might not be able to implement setters for common_properties
																								# In that case, a NotImplementedError is raised
																								pass


			def      UpperCAmelCase__						(				self					:  Any ):
										lowerCamelCase_	:							Tuple									=self.config_class(**self.inputs_dict )
										lowerCamelCase_	:							Any									=json.loads(config.to_json_string() )
										for key, value in self.inputs_dict.items():
																	self.parent.assertEqual(obj[key]       ,	snake_case__ )


			def      UpperCAmelCase__						(				self					:  int ):
										lowerCamelCase_	:							Tuple									=self.config_class(**self.inputs_dict )

										with tempfile.TemporaryDirectory() as tmpdirname:
																	lowerCamelCase_	:							List[Any]									=os.path.join(snake_case__       ,	"config.json" )
																	config_first.to_json_file(snake_case__ )
																	lowerCamelCase_	:							Optional[int]									=self.config_class.from_json_file(snake_case__ )

										self.parent.assertEqual(config_second.to_dict()       ,	config_first.to_dict() )


			def      UpperCAmelCase__						(				self					:  Dict ):
										lowerCamelCase_	:							Dict									=self.config_class(**self.inputs_dict )

										with tempfile.TemporaryDirectory() as tmpdirname:
																	config_first.save_pretrained(snake_case__ )
																	lowerCamelCase_	:							Optional[int]									=self.config_class.from_pretrained(snake_case__ )

										self.parent.assertEqual(config_second.to_dict()       ,	config_first.to_dict() )


			def      UpperCAmelCase__						(				self					:  str ):
										lowerCamelCase_	:							Dict									=self.config_class(**self.inputs_dict )

										lowerCamelCase_	:							Dict									="test"
										with tempfile.TemporaryDirectory() as tmpdirname:
																	lowerCamelCase_	:							str									=os.path.join(snake_case__       ,	snake_case__ )
																	config_first.save_pretrained(snake_case__ )
																	lowerCamelCase_	:							Optional[Any]									=self.config_class.from_pretrained(snake_case__       ,	subfolder=snake_case__ )

										self.parent.assertEqual(config_second.to_dict()       ,	config_first.to_dict() )


			def      UpperCAmelCase__						(				self					:  Optional[Any] ):
										lowerCamelCase_	:							Optional[Any]									=self.config_class(**self.inputs_dict       ,	num_labels=5 )
										self.parent.assertEqual(len(config.idalabel )       ,	5 )
										self.parent.assertEqual(len(config.labelaid )       ,	5 )

										lowerCamelCase_	:							List[Any]									=3
										self.parent.assertEqual(len(config.idalabel )       ,	3 )
										self.parent.assertEqual(len(config.labelaid )       ,	3 )


			def      UpperCAmelCase__						(				self					:  List[Any] ):
										if self.config_class.is_composition:
																	return
										lowerCamelCase_	:							Tuple									=self.config_class()
										self.parent.assertIsNotNone(snake_case__ )


			def      UpperCAmelCase__						(				self					:  List[Any] ):
										lowerCamelCase_	:							List[str]									=copy.deepcopy(snake_case__ )
										lowerCamelCase_	:							Optional[int]									=self.config_class(**snake_case__ )
										lowerCamelCase_	:							Union[str, Any]									=[]
										for key, value in config_common_kwargs.items():
																	if key == "torch_dtype":
																								if not is_torch_available():
																															continue
																								else:
																															import torch

																															if config.torch_dtype != torch.floataa:
																																						wrong_values.append(("torch_dtype", config.torch_dtype, torch.floataa) )
																	elif getattr(snake_case__       ,	snake_case__ ) != value:
																								wrong_values.append((key, getattr(snake_case__       ,	snake_case__ ), value) )

										if len(snake_case__ ) > 0:
																	lowerCamelCase_	:							Any									="\n".join([F"""- {v[0]}: got {v[1]} instead of {v[2]}""" for v in wrong_values] )
																	raise ValueError(F"""The following keys were not properly set in the config:\n{errors}""" )


			def      UpperCAmelCase__						(				self					:  int ):
										self.create_and_test_config_common_properties()
										self.create_and_test_config_to_json_string()
										self.create_and_test_config_to_json_file()
										self.create_and_test_config_from_and_save_pretrained()
										self.create_and_test_config_from_and_save_pretrained_subfolder()
										self.create_and_test_config_with_num_labels()
										self.check_config_can_be_init_without_params()
										self.check_config_arguments_init()



