



import inspect
import unittest
import warnings

from transformers import DeiTConfig
from transformers.models.auto import get_values
from transformers.testing_utils import (
    require_accelerate,
    require_torch,
    require_torch_gpu,
    require_vision,
    slow,
    torch_device,
)
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch
				from torch import nn

				from transformers import (
				    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
				    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
				    MODEL_MAPPING,
				    DeiTForImageClassification,
				    DeiTForImageClassificationWithTeacher,
				    DeiTForMaskedImageModeling,
				    DeiTModel,
				)
				from transformers.models.deit.modeling_deit import DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
				from PIL import Image

				from transformers import DeiTImageProcessor


class       a							:



		def __init__(     self     :Dict   ,__lowercase     :Any   ,__lowercase     :int=1_3   ,__lowercase     :int=3_0   ,__lowercase     :List[Any]=2   ,__lowercase     :Optional[Any]=3   ,__lowercase     :str=True   ,__lowercase     :Union[str, Any]=True   ,__lowercase     :Tuple=3_2   ,__lowercase     :Optional[Any]=5   ,__lowercase     :Optional[int]=4   ,__lowercase     :str=3_7   ,__lowercase     :Any="gelu"   ,__lowercase     :List[str]=0.1   ,__lowercase     :int=0.1   ,__lowercase     :str=1_0   ,__lowercase     :Tuple=0.02   ,__lowercase     :Tuple=3   ,__lowercase     :List[str]=None   ,__lowercase     :int=2   ,):
						snake_case__	:      Union[str, Any]     		=	parent
						snake_case__	:      List[Any]     		=	batch_size
						snake_case__	:      Union[str, Any]     		=	image_size
						snake_case__	:      int     		=	patch_size
						snake_case__	:      Tuple     		=	num_channels
						snake_case__	:      Optional[Any]     		=	is_training
						snake_case__	:      Dict     		=	use_labels
						snake_case__	:      Union[str, Any]     		=	hidden_size
						snake_case__	:      List[Any]     		=	num_hidden_layers
						snake_case__	:      str     		=	num_attention_heads
						snake_case__	:      Optional[Any]     		=	intermediate_size
						snake_case__	:      Union[str, Any]     		=	hidden_act
						snake_case__	:      Tuple     		=	hidden_dropout_prob
						snake_case__	:      Optional[Any]     		=	attention_probs_dropout_prob
						snake_case__	:      Tuple     		=	type_sequence_label_size
						snake_case__	:      Any     		=	initializer_range
						snake_case__	:      int     		=	scope
						snake_case__	:      List[str]     		=	encoder_stride

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



		def 	__lowerCamelCase			(     self     :Union[str, Any]  ):
						snake_case__	:      Optional[Any]     		=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

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

						snake_case__	:      Any     		=	self.get_config()

						return config, pixel_values, labels



		def 	__lowerCamelCase			(     self     :int  ):
						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=__lowercase   ,initializer_range=self.initializer_range   ,encoder_stride=self.encoder_stride   ,)



		def 	__lowerCamelCase			(     self     :Dict   ,__lowercase     :Dict   ,__lowercase     :Union[str, Any]   ,__lowercase     :Optional[int]  ):
						snake_case__	:      List[Any]     		=	DeiTModel(config=__lowercase  )
						model.to(__lowercase  )
						model.eval()
						snake_case__	:      Any     		=	model(__lowercase  )
						self.parent.assertEqual(result.last_hidden_state.shape   ,(self.batch_size, self.seq_length, self.hidden_size)  )



		def 	__lowerCamelCase			(     self     :List[Any]   ,__lowercase     :int   ,__lowercase     :Union[str, Any]   ,__lowercase     :Optional[int]  ):
						snake_case__	:      Union[str, Any]     		=	DeiTForMaskedImageModeling(config=__lowercase  )
						model.to(__lowercase  )
						model.eval()
						snake_case__	:      Optional[int]     		=	model(__lowercase  )
						self.parent.assertEqual(
						    result.reconstruction.shape   ,(self.batch_size, self.num_channels, self.image_size, self.image_size)  )

						# test greyscale images
						snake_case__	:      List[Any]     		=	1
						snake_case__	:      Union[str, Any]     		=	DeiTForMaskedImageModeling(__lowercase  )
						model.to(__lowercase  )
						model.eval()

						snake_case__	:      Union[str, Any]     		=	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]  )
						snake_case__	:      List[str]     		=	model(__lowercase  )
						self.parent.assertEqual(result.reconstruction.shape   ,(self.batch_size, 1, self.image_size, self.image_size)  )



		def 	__lowerCamelCase			(     self     :List[Any]   ,__lowercase     :Optional[Any]   ,__lowercase     :Optional[int]   ,__lowercase     :List[str]  ):
						snake_case__	:      Optional[int]     		=	self.type_sequence_label_size
						snake_case__	:      Optional[Any]     		=	DeiTForImageClassification(__lowercase  )
						model.to(__lowercase  )
						model.eval()
						snake_case__	:      List[Any]     		=	model(__lowercase   ,labels=__lowercase  )
						self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.type_sequence_label_size)  )

						# test greyscale images
						snake_case__	:      Any     		=	1
						snake_case__	:      Union[str, Any]     		=	DeiTForImageClassification(__lowercase  )
						model.to(__lowercase  )
						model.eval()

						snake_case__	:      Dict     		=	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]  )
						snake_case__	:      int     		=	model(__lowercase   ,labels=__lowercase  )
						self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.type_sequence_label_size)  )






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


@require_torch
class       a							(			__lowerCamelCase  ,							__lowerCamelCase  ,							unittest.TestCase				):
		__lowerCAmelCase	:    Optional[int]											=					(
		    (
		        DeiTModel,
		        DeiTForImageClassification,
		        DeiTForImageClassificationWithTeacher,
		        DeiTForMaskedImageModeling,
		    )
		    if is_torch_available()
		    else ()
		)
		__lowerCAmelCase	:    List[Any]											=					(
		    {
		        """feature-extraction""": DeiTModel,
		        """image-classification""": (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
		    }
		    if is_torch_available()
		    else {}
		)

		__lowerCAmelCase	:    Dict											=					False
		__lowerCAmelCase	:    str											=					False
		__lowerCAmelCase	:    Dict											=					False



		def 	__lowerCamelCase			(     self     :Tuple  ):
						snake_case__	:      int     		=	DeiTModelTester(self  )
						snake_case__	:      Any     		=	ConfigTester(self   ,config_class=__lowercase   ,has_text_modality=__lowercase   ,hidden_size=3_7  )



		def 	__lowerCamelCase			(     self     :Union[str, Any]  ):
						self.config_tester.run_common_tests()



		@unittest.skip(reason='''DeiT does not use inputs_embeds'''  )
		def 	__lowerCamelCase			(     self     :Union[str, Any]  ):
						pass



		def 	__lowerCamelCase			(     self     :List[Any]  ):
						snake_case__		, snake_case__	:      str     		=	self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
										snake_case__	:      Tuple     		=	model_class(__lowercase  )
										self.assertIsInstance(model.get_input_embeddings()   ,(nn.Module)  )
										snake_case__	:      Union[str, Any]     		=	model.get_output_embeddings()
										self.assertTrue(x is None or isinstance(__lowercase   ,nn.Linear  )  )



		def 	__lowerCamelCase			(     self     :Union[str, Any]  ):
						snake_case__		, snake_case__	:      List[Any]     		=	self.model_tester.prepare_config_and_inputs_for_common()

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

										snake_case__	:      Union[str, Any]     		=	['''pixel_values''']
										self.assertListEqual(arg_names[:1]   ,__lowercase  )



		def 	__lowerCamelCase			(     self     :Union[str, Any]  ):
						snake_case__	:      Optional[int]     		=	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__lowercase  )



		def 	__lowerCamelCase			(     self     :List[Any]  ):
						snake_case__	:      int     		=	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_image_modeling(*__lowercase  )



		def 	__lowerCamelCase			(     self     :List[Any]  ):
						snake_case__	:      Any     		=	self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*__lowercase  )



		def 	__lowerCamelCase			(     self     :Any   ,__lowercase     :int   ,__lowercase     :List[str]   ,__lowercase     :Tuple=False  ):
						snake_case__	:      Optional[int]     		=	super()._prepare_for_class(__lowercase   ,__lowercase   ,return_labels=__lowercase  )

						if return_labels:
										if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
														del inputs_dict["labels"]

						return inputs_dict



		def 	__lowerCamelCase			(     self     :Tuple  ):
						if not self.model_tester.is_training:
										return

						snake_case__		, snake_case__	:      Optional[int]     		=	self.model_tester.prepare_config_and_inputs_for_common()
						snake_case__	:      Optional[int]     		=	True

						for model_class in self.all_model_classes:
										# DeiTForImageClassificationWithTeacher supports inference-only
										if (
										    model_class in get_values(__lowercase  )
										    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
										):
														continue
										snake_case__	:      str     		=	model_class(__lowercase  )
										model.to(__lowercase  )
										model.train()
										snake_case__	:      Tuple     		=	self._prepare_for_class(__lowercase   ,__lowercase   ,return_labels=__lowercase  )
										snake_case__	:      List[str]     		=	model(**__lowercase  ).loss
										loss.backward()



		def 	__lowerCamelCase			(     self     :str  ):
						snake_case__		, snake_case__	:      Optional[int]     		=	self.model_tester.prepare_config_and_inputs_for_common()
						if not self.model_tester.is_training:
										return

						snake_case__	:      Dict     		=	False
						snake_case__	:      List[Any]     		=	True

						for model_class in self.all_model_classes:
										if model_class in get_values(__lowercase  ) or not model_class.supports_gradient_checkpointing:
														continue
										# DeiTForImageClassificationWithTeacher supports inference-only
										if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
														continue
										snake_case__	:      List[Any]     		=	model_class(__lowercase  )
										model.gradient_checkpointing_enable()
										model.to(__lowercase  )
										model.train()
										snake_case__	:      str     		=	self._prepare_for_class(__lowercase   ,__lowercase   ,return_labels=__lowercase  )
										snake_case__	:      str     		=	model(**__lowercase  ).loss
										loss.backward()



		def 	__lowerCamelCase			(     self     :Optional[int]  ):
						snake_case__		, snake_case__	:      List[str]     		=	self.model_tester.prepare_config_and_inputs_for_common()

						snake_case__	:      Optional[Any]     		=	[
						    {'''title''': '''multi_label_classification''', '''num_labels''': 2, '''dtype''': torch.float},
						    {'''title''': '''single_label_classification''', '''num_labels''': 1, '''dtype''': torch.long},
						    {'''title''': '''regression''', '''num_labels''': 1, '''dtype''': torch.float},
						]

						for model_class in self.all_model_classes:
										if (
										    model_class
										    not in [
										        *get_values(__lowercase  ),
										        *get_values(__lowercase  ),
										    ]
										    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
										):
														continue

										for problem_type in problem_types:
														with self.subTest(msg=F"""Testing {model_class} with {problem_type['title']}"""  ):
																		snake_case__	:      Dict     		=	problem_type['''title''']
																		snake_case__	:      Any     		=	problem_type['''num_labels''']

																		snake_case__	:      int     		=	model_class(__lowercase  )
																		model.to(__lowercase  )
																		model.train()

																		snake_case__	:      Dict     		=	self._prepare_for_class(__lowercase   ,__lowercase   ,return_labels=__lowercase  )

																		if problem_type["num_labels"] > 1:
																						snake_case__	:      int     		=	inputs['''labels'''].unsqueeze(1  ).repeat(1   ,problem_type['''num_labels''']  )

																		snake_case__	:      List[str]     		=	inputs['''labels'''].to(problem_type['''dtype''']  )

																		# This tests that we do not trigger the warning form PyTorch "Using a target size that is different
																		# to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
																		# they have the same size." which is a symptom something in wrong for the regression problem.
																		# See https://github.com/huggingface/transformers/issues/11780
																		with warnings.catch_warnings(record=__lowercase  ) as warning_list:
																						snake_case__	:      List[str]     		=	model(**__lowercase  ).loss
																		for w in warning_list:
																						if "Using a target size that is different to the input size" in str(w.message  ):
																										raise ValueError(
																										    F"""Something is going wrong in the regression problem: intercepted {w.message}"""  )

																		loss.backward()






		@slow
		def 	__lowerCamelCase			(     self     :Tuple  ):
						for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										snake_case__	:      List[Any]     		=	DeiTModel.from_pretrained(__lowercase  )
										self.assertIsNotNone(__lowercase  )



def 			_lowerCAmelCase       (					)   ->					str:


				"""simple docstring"""

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





@require_torch
@require_vision
class       a							(			unittest.TestCase				):



		@cached_property
		def 	__lowerCamelCase			(     self     :Union[str, Any]  ):
						return (
						    DeiTImageProcessor.from_pretrained('''facebook/deit-base-distilled-patch16-224'''  )
						    if is_vision_available()
						    else None
						)



		@slow
		def 	__lowerCamelCase			(     self     :List[str]  ):
						snake_case__	:      List[str]     		=	DeiTForImageClassificationWithTeacher.from_pretrained('''facebook/deit-base-distilled-patch16-224'''  ).to(
						    __lowercase  )

						snake_case__	:      List[str]     		=	self.default_image_processor
						snake_case__	:      Optional[Any]     		=	prepare_img()
						snake_case__	:      int     		=	image_processor(images=__lowercase   ,return_tensors='''pt'''  ).to(__lowercase  )

						# forward pass
						with torch.no_grad():
										snake_case__	:      Dict     		=	model(**__lowercase  )

						# verify the logits
						snake_case__	:      List[Any]     		=	torch.Size((1, 1_0_0_0)  )
						self.assertEqual(outputs.logits.shape   ,__lowercase  )

						snake_case__	:      List[str]     		=	torch.tensor([-1.0266, 0.1912, -1.2861]  ).to(__lowercase  )

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






		@slow
		@require_accelerate
		@require_torch_gpu
		def 	__lowerCamelCase			(     self     :Optional[Any]  ):
						snake_case__	:      List[str]     		=	DeiTModel.from_pretrained(
						    '''facebook/deit-base-distilled-patch16-224'''   ,torch_dtype=torch.floataa   ,device_map='''auto'''  )
						snake_case__	:      str     		=	self.default_image_processor

						snake_case__	:      Any     		=	prepare_img()
						snake_case__	:      Optional[int]     		=	image_processor(images=__lowercase   ,return_tensors='''pt'''  )
						snake_case__	:      str     		=	inputs.pixel_values.to(__lowercase  )

						# forward pass to make sure inference works in fp16
						with torch.no_grad():
										snake_case__	:      int     		=	model(__lowercase  )






from typing import TYPE_CHECKING

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


A__								=						{
    '''configuration_ctrl''': ['''CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''CTRLConfig'''],
    '''tokenization_ctrl''': ['''CTRLTokenizer'''],
}

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				A__								=						[
				    '''CTRL_PRETRAINED_MODEL_ARCHIVE_LIST''',
				    '''CTRLForSequenceClassification''',
				    '''CTRLLMHeadModel''',
				    '''CTRLModel''',
				    '''CTRLPreTrainedModel''',
				]

try:
				if not is_tf_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				A__								=						[
				    '''TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST''',
				    '''TFCTRLForSequenceClassification''',
				    '''TFCTRLLMHeadModel''',
				    '''TFCTRLModel''',
				    '''TFCTRLPreTrainedModel''',
				]


if TYPE_CHECKING:
				from .configuration_ctrl import CTRL_PRETRAINED_CONFIG_ARCHIVE_MAP, CTRLConfig
				from .tokenization_ctrl import CTRLTokenizer

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_ctrl import (
								    CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
								    CTRLForSequenceClassification,
								    CTRLLMHeadModel,
								    CTRLModel,
								    CTRLPreTrainedModel,
								)

				try:
								if not is_tf_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_tf_ctrl import (
								    TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
								    TFCTRLForSequenceClassification,
								    TFCTRLLMHeadModel,
								    TFCTRLModel,
								    TFCTRLPreTrainedModel,
								)

else:
				import sys

				A__								=						_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)

