


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






		'''simple docstring'''

		def __init__(self				,       a_				,       a_=13				,       a_=30				,       a_=2				,       a_=3				,       a_=True				,       a_=True				,       a_=32				,       a_=2				,       a_=4				,       a_=37				,       a_="gelu"				,       a_=0.1				,       a_=0.1				,       a_=10				,       a_=0.02				,       a_=3				,       a_=None				,       a_=2				,       ):





			'''simple docstring'''

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

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



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

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

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

			__snake_case	:						Optional[Any]						=				self.get_config()

			return config, pixel_values, labels



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



		def 				SCREAMING_SNAKE_CASE  (self				,       a_				,       a_				,       a_					):





			'''simple docstring'''

			__snake_case	:						Tuple						=				TFDeiTModel(config=a_					)
			__snake_case	:						str						=				model(a_					)
			self.parent.assertEqual(result.last_hidden_state.shape				,       (self.batch_size, self.seq_length, self.hidden_size)					)



		def 				SCREAMING_SNAKE_CASE  (self				,       a_				,       a_				,       a_					):





			'''simple docstring'''

			__snake_case	:						Dict						=				TFDeiTForMaskedImageModeling(config=a_					)
			__snake_case	:						Any						=				model(a_					)
			self.parent.assertEqual(
			    result.reconstruction.shape				,       (self.batch_size, self.num_channels, self.image_size, self.image_size)					)

			# test greyscale images
			__snake_case	:						Optional[Any]						=				1
			__snake_case	:						Any						=				TFDeiTForMaskedImageModeling(a_					)

			__snake_case	:						str						=				floats_tensor([self.batch_size, 1, self.image_size, self.image_size]					)
			__snake_case	:						List[str]						=				model(a_					)
			self.parent.assertEqual(result.reconstruction.shape				,       (self.batch_size, 1, self.image_size, self.image_size)					)



		def 				SCREAMING_SNAKE_CASE  (self				,       a_				,       a_				,       a_					):





			'''simple docstring'''

			__snake_case	:						List[str]						=				self.type_sequence_label_size
			__snake_case	:						Union[str, Any]						=				TFDeiTForImageClassification(a_					)
			__snake_case	:						Union[str, Any]						=				model(a_				,       labels=a_					)
			self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.type_sequence_label_size)					)

			# test greyscale images
			__snake_case	:						str						=				1
			__snake_case	:						str						=				TFDeiTForImageClassification(a_					)

			__snake_case	:						Tuple						=				floats_tensor([self.batch_size, 1, self.image_size, self.image_size]					)
			__snake_case	:						str						=				model(a_				,       labels=a_					)
			self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.type_sequence_label_size)					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Dict						=				self.prepare_config_and_inputs()
			__snake_case	,							__snake_case	,							__snake_case	:						Optional[Any]						=				config_and_inputs
			__snake_case	:						Union[str, Any]						=				{'''pixel_values''': pixel_values}
			return config, inputs_dict



@require_tf
class 			_UpperCAmelCase (							__snake_case,				__snake_case,				unittest.TestCase				):






		'''simple docstring'''

		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 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Dict						=				TFDeiTModelTester(self					)
			__snake_case	:						int						=				ConfigTester(self				,       config_class=a_				,       has_text_modality=a_				,       hidden_size=37					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			self.config_tester.run_common_tests()



		@unittest.skip(reason='''DeiT does not use inputs_embeds'''					)
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			pass



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	,							__snake_case	:						Any						=				self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
				__snake_case	:						Optional[Any]						=				model_class(a_					)
				self.assertIsInstance(model.get_input_embeddings()				,       (tf.keras.layers.Layer)					)
				__snake_case	:						int						=				model.get_output_embeddings()
				self.assertTrue(x is None or isinstance(a_				,       tf.keras.layers.Dense					)					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

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

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

				__snake_case	:						Dict						=				['''pixel_values''']
				self.assertListEqual(arg_names[:1]				,       a_					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Optional[int]						=				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*a_					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Tuple						=				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_masked_image_modeling(*a_					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Optional[int]						=				self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_image_classification(*a_					)



		def 				SCREAMING_SNAKE_CASE  (self				,       a_				,       a_				,       a_=False					):





			'''simple docstring'''

			__snake_case	:						List[str]						=				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 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
				__snake_case	:						Tuple						=				TFDeiTModel.from_pretrained(a_					)
				self.assertIsNotNone(a_					)






def 		lowercase							(     )     ->Union[str, Any]:
	"""simple docstring"""





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



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






		'''simple docstring'''

		@cached_property
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

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



		@slow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

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

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

			# forward pass
			__snake_case	:						Union[str, Any]						=				model(**a_					)

			# verify the logits
			__snake_case	:						Optional[int]						=				tf.TensorShape((1, 10_00)					)
			self.assertEqual(outputs.logits.shape				,       a_					)

			__snake_case	:						Optional[int]						=				tf.constant([-1.0266, 0.1912, -1.2861]					)

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









"""simple docstring"""
from ...utils import is_torch_available, is_transformers_available


if is_transformers_available() and is_torch_available():
			from .pipeline_vq_diffusion import LearnedClassifierFreeSamplingEmbeddings, VQDiffusionPipeline





