

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







	def __init__(   self: List[Any]  , __UpperCamelCase: int  , __UpperCamelCase: Union[str, Any]=13  , __UpperCamelCase: Optional[int]=30  , __UpperCamelCase: List[str]=2  , __UpperCamelCase: List[str]=3  , __UpperCamelCase: Optional[int]=True  , __UpperCamelCase: Optional[Any]=True  , __UpperCamelCase: Dict=32  , __UpperCamelCase: str=5  , __UpperCamelCase: str=4  , __UpperCamelCase: int=37  , __UpperCamelCase: Any="gelu"  , __UpperCamelCase: Tuple=0.1  , __UpperCamelCase: Optional[Any]=0.1  , __UpperCamelCase: str=10  , __UpperCamelCase: List[str]=0.02  , __UpperCamelCase: List[Any]=3  , __UpperCamelCase: int=None  , __UpperCamelCase: int=2  , ):

						'''simple docstring'''





						__magic_name__							=    parent
						__magic_name__							=    batch_size
						__magic_name__							=    image_size
						__magic_name__							=    patch_size
						__magic_name__							=    num_channels
						__magic_name__							=    is_training
						__magic_name__							=    use_labels
						__magic_name__							=    hidden_size
						__magic_name__							=    num_hidden_layers
						__magic_name__							=    num_attention_heads
						__magic_name__							=    intermediate_size
						__magic_name__							=    hidden_act
						__magic_name__							=    hidden_dropout_prob
						__magic_name__							=    attention_probs_dropout_prob
						__magic_name__							=    type_sequence_label_size
						__magic_name__							=    initializer_range
						__magic_name__							=    scope
						__magic_name__							=    encoder_stride

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







	def 	_SCREAMING_SNAKE_CASE							(   self: Union[str, Any]      ):

						'''simple docstring'''





						__magic_name__							=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

						__magic_name__							=    None
						if self.use_labels:
											__magic_name__							=    ids_tensor([self.batch_size]  , self.type_sequence_label_size      )

						__magic_name__							=    self.get_config()

						return config, pixel_values, labels







	def 	_SCREAMING_SNAKE_CASE							(   self: List[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=__UpperCamelCase  , initializer_range=self.initializer_range  , encoder_stride=self.encoder_stride  , )







	def 	_SCREAMING_SNAKE_CASE							(   self: List[str]  , __UpperCamelCase: List[Any]  , __UpperCamelCase: Tuple  , __UpperCamelCase: str      ):

						'''simple docstring'''





						__magic_name__							=    DeiTModel(config=__UpperCamelCase      )
						model.to(__UpperCamelCase      )
						model.eval()
						__magic_name__							=    model(__UpperCamelCase      )
						self.parent.assertEqual(result.last_hidden_state.shape  , (self.batch_size, self.seq_length, self.hidden_size)      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Tuple  , __UpperCamelCase: List[Any]  , __UpperCamelCase: Union[str, Any]  , __UpperCamelCase: Union[str, Any]      ):

						'''simple docstring'''





						__magic_name__							=    DeiTForMaskedImageModeling(config=__UpperCamelCase      )
						model.to(__UpperCamelCase      )
						model.eval()
						__magic_name__							=    model(__UpperCamelCase      )
						self.parent.assertEqual(
						    result.reconstruction.shape  , (self.batch_size, self.num_channels, self.image_size, self.image_size)      )

						# test greyscale images
						__magic_name__							=    1
						__magic_name__							=    DeiTForMaskedImageModeling(__UpperCamelCase      )
						model.to(__UpperCamelCase      )
						model.eval()

						__magic_name__							=    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
						__magic_name__							=    model(__UpperCamelCase      )
						self.parent.assertEqual(result.reconstruction.shape  , (self.batch_size, 1, self.image_size, self.image_size)      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Union[str, Any]  , __UpperCamelCase: Tuple  , __UpperCamelCase: Union[str, Any]  , __UpperCamelCase: List[Any]      ):

						'''simple docstring'''





						__magic_name__							=    self.type_sequence_label_size
						__magic_name__							=    DeiTForImageClassification(__UpperCamelCase      )
						model.to(__UpperCamelCase      )
						model.eval()
						__magic_name__							=    model(__UpperCamelCase  , labels=__UpperCamelCase      )
						self.parent.assertEqual(result.logits.shape  , (self.batch_size, self.type_sequence_label_size)      )

						# test greyscale images
						__magic_name__							=    1
						__magic_name__							=    DeiTForImageClassification(__UpperCamelCase      )
						model.to(__UpperCamelCase      )
						model.eval()

						__magic_name__							=    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
						__magic_name__							=    model(__UpperCamelCase  , labels=__UpperCamelCase      )
						self.parent.assertEqual(result.logits.shape  , (self.batch_size, self.type_sequence_label_size)      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Optional[int]      ):

						'''simple docstring'''





						__magic_name__							=    self.prepare_config_and_inputs()
						(
						    (
						    __magic_name__
						),	(
						    __magic_name__
						),	(
						    __magic_name__
						),	
						)							=    config_and_inputs
						__magic_name__							=    {'pixel_values': pixel_values}
						return config, inputs_dict




@require_torch
class 				__UpperCamelCase				(					SCREAMING_SNAKE_CASE  ,						SCREAMING_SNAKE_CASE  ,						unittest.TestCase			):
	_lowercase  :   Dict       				=  (
	    (
	        DeiTModel,
	        DeiTForImageClassification,
	        DeiTForImageClassificationWithTeacher,
	        DeiTForMaskedImageModeling,
	    )
	    if is_torch_available()
	    else ()
	)
	_lowercase  :   List[Any]       				=  (
	    {
	        "feature-extraction": DeiTModel,
	        "image-classification": (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
	    }
	    if is_torch_available()
	    else {}
	)

	_lowercase  :   int       				=  False
	_lowercase  :   Optional[int]       				=  False
	_lowercase  :   int       				=  False







	def 	_SCREAMING_SNAKE_CASE							(   self: str      ):

						'''simple docstring'''





						__magic_name__							=    DeiTModelTester(self      )
						__magic_name__							=    ConfigTester(self  , config_class=__UpperCamelCase  , has_text_modality=__UpperCamelCase  , hidden_size=37      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Any      ):

						'''simple docstring'''





						self.config_tester.run_common_tests()







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

						'''simple docstring'''





						pass







	def 	_SCREAMING_SNAKE_CASE							(   self: Any      ):

						'''simple docstring'''





						__magic_name__,	__magic_name__							=    self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
											__magic_name__							=    model_class(__UpperCamelCase      )
											self.assertIsInstance(model.get_input_embeddings()  , (nn.Module)      )
											__magic_name__							=    model.get_output_embeddings()
											self.assertTrue(x is None or isinstance(__UpperCamelCase  , nn.Linear      )      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Union[str, Any]      ):

						'''simple docstring'''





						__magic_name__,	__magic_name__							=    self.model_tester.prepare_config_and_inputs_for_common()

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

											__magic_name__							=    ['pixel_values']
											self.assertListEqual(arg_names[:1]  , __UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Any      ):

						'''simple docstring'''





						__magic_name__							=    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: List[str]      ):

						'''simple docstring'''





						__magic_name__							=    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_image_modeling(*__UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: List[Any]      ):

						'''simple docstring'''





						__magic_name__							=    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Tuple  , __UpperCamelCase: Any  , __UpperCamelCase: Tuple  , __UpperCamelCase: Tuple=False      ):

						'''simple docstring'''





						__magic_name__							=    super()._prepare_for_class(__UpperCamelCase  , __UpperCamelCase  , return_labels=__UpperCamelCase      )

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

						return inputs_dict







	def 	_SCREAMING_SNAKE_CASE							(   self: str      ):

						'''simple docstring'''





						if not self.model_tester.is_training:
											return

						__magic_name__,	__magic_name__							=    self.model_tester.prepare_config_and_inputs_for_common()
						__magic_name__							=    True

						for model_class in self.all_model_classes:
											# DeiTForImageClassificationWithTeacher supports inference-only
											if (
											    model_class in get_values(__UpperCamelCase      )
											    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
											):
																continue
											__magic_name__							=    model_class(__UpperCamelCase      )
											model.to(__UpperCamelCase      )
											model.train()
											__magic_name__							=    self._prepare_for_class(__UpperCamelCase  , __UpperCamelCase  , return_labels=__UpperCamelCase      )
											__magic_name__							=    model(**__UpperCamelCase      ).loss
											loss.backward()







	def 	_SCREAMING_SNAKE_CASE							(   self: Union[str, Any]      ):

						'''simple docstring'''





						__magic_name__,	__magic_name__							=    self.model_tester.prepare_config_and_inputs_for_common()
						if not self.model_tester.is_training:
											return

						__magic_name__							=    False
						__magic_name__							=    True

						for model_class in self.all_model_classes:
											if model_class in get_values(__UpperCamelCase      ) or not model_class.supports_gradient_checkpointing:
																continue
											# DeiTForImageClassificationWithTeacher supports inference-only
											if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
																continue
											__magic_name__							=    model_class(__UpperCamelCase      )
											model.gradient_checkpointing_enable()
											model.to(__UpperCamelCase      )
											model.train()
											__magic_name__							=    self._prepare_for_class(__UpperCamelCase  , __UpperCamelCase  , return_labels=__UpperCamelCase      )
											__magic_name__							=    model(**__UpperCamelCase      ).loss
											loss.backward()







	def 	_SCREAMING_SNAKE_CASE							(   self: Tuple      ):

						'''simple docstring'''





						__magic_name__,	__magic_name__							=    self.model_tester.prepare_config_and_inputs_for_common()

						__magic_name__							=    [
						    {'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(__UpperCamelCase      ),
											        *get_values(__UpperCamelCase      ),
											    ]
											    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
											):
																continue

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

																					__magic_name__							=    model_class(__UpperCamelCase      )
																					model.to(__UpperCamelCase      )
																					model.train()

																					__magic_name__							=    self._prepare_for_class(__UpperCamelCase  , __UpperCamelCase  , return_labels=__UpperCamelCase      )

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

																					__magic_name__							=    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=__UpperCamelCase      ) as warning_list:
																										__magic_name__							=    model(**__UpperCamelCase      ).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 	_SCREAMING_SNAKE_CASE							(   self: List[str]      ):

						'''simple docstring'''





						for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__magic_name__							=    DeiTModel.from_pretrained(__UpperCamelCase      )
											self.assertIsNotNone(__UpperCamelCase      )




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




					'''simple docstring'''







					__magic_name__							=    Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
					return image


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







	@cached_property
	def 	_SCREAMING_SNAKE_CASE							(   self: Optional[Any]      ):

						'''simple docstring'''





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







	@slow
	def 	_SCREAMING_SNAKE_CASE							(   self: List[str]      ):

						'''simple docstring'''





						__magic_name__							=    DeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224'      ).to(
						    __UpperCamelCase      )

						__magic_name__							=    self.default_image_processor
						__magic_name__							=    prepare_img()
						__magic_name__							=    image_processor(images=__UpperCamelCase  , return_tensors='pt'      ).to(__UpperCamelCase      )

						# forward pass
						with torch.no_grad():
											__magic_name__							=    model(**__UpperCamelCase      )

						# verify the logits
						__magic_name__							=    torch.Size((1, 10_00)      )
						self.assertEqual(outputs.logits.shape  , __UpperCamelCase      )

						__magic_name__							=    torch.tensor([-1.0266, 0.1912, -1.2861]      ).to(__UpperCamelCase      )

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







	@slow
	@require_accelerate
	@require_torch_gpu
	def 	_SCREAMING_SNAKE_CASE							(   self: Tuple      ):

						'''simple docstring'''





						__magic_name__							=    DeiTModel.from_pretrained(
						    'facebook/deit-base-distilled-patch16-224'  , torch_dtype=torch.floataa  , device_map='auto'      )
						__magic_name__							=    self.default_image_processor

						__magic_name__							=    prepare_img()
						__magic_name__							=    image_processor(images=__UpperCamelCase  , return_tensors='pt'      )
						__magic_name__							=    inputs.pixel_values.to(__UpperCamelCase      )

						# forward pass to make sure inference works in fp16
						with torch.no_grad():
											__magic_name__							=    model(__UpperCamelCase      )







import unittest

from transformers import AutoConfig, AutoTokenizer, BertConfig, TensorType, is_flax_available
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, slow


if is_flax_available():
						import jax

						from transformers.models.auto.modeling_flax_auto import FlaxAutoModel
						from transformers.models.bert.modeling_flax_bert import FlaxBertModel
						from transformers.models.roberta.modeling_flax_roberta import FlaxRobertaModel

@require_flax
class 				__UpperCamelCase				(					unittest.TestCase			):







	@slow
	def 	_SCREAMING_SNAKE_CASE							(   self: Any      ):

						'''simple docstring'''





						for model_name in ["bert-base-cased", "bert-large-uncased"]:
											with self.subTest(__UpperCamelCase      ):
																__magic_name__							=    AutoConfig.from_pretrained(__UpperCamelCase      )
																self.assertIsNotNone(__UpperCamelCase      )
																self.assertIsInstance(__UpperCamelCase  , __UpperCamelCase      )

																__magic_name__							=    FlaxAutoModel.from_pretrained(__UpperCamelCase      )
																self.assertIsNotNone(__UpperCamelCase      )
																self.assertIsInstance(__UpperCamelCase  , __UpperCamelCase      )







	@slow
	def 	_SCREAMING_SNAKE_CASE							(   self: Tuple      ):

						'''simple docstring'''





						for model_name in ["roberta-base", "roberta-large"]:
											with self.subTest(__UpperCamelCase      ):
																__magic_name__							=    AutoConfig.from_pretrained(__UpperCamelCase      )
																self.assertIsNotNone(__UpperCamelCase      )
																self.assertIsInstance(__UpperCamelCase  , __UpperCamelCase      )

																__magic_name__							=    FlaxAutoModel.from_pretrained(__UpperCamelCase      )
																self.assertIsNotNone(__UpperCamelCase      )
																self.assertIsInstance(__UpperCamelCase  , __UpperCamelCase      )







	@slow
	def 	_SCREAMING_SNAKE_CASE							(   self: Dict      ):

						'''simple docstring'''





						for model_name in ["bert-base-cased", "bert-large-uncased"]:
											__magic_name__							=    AutoTokenizer.from_pretrained(__UpperCamelCase      )
											__magic_name__							=    FlaxBertModel.from_pretrained(__UpperCamelCase      )
											__magic_name__							=    tokenizer('Do you support jax jitted function?'  , return_tensors=TensorType.JAX      )

											@jax.jit
											def eval(**__UpperCamelCase: Optional[Any]      ):
																return model(**__UpperCamelCase      )

											eval(**__UpperCamelCase      ).block_until_ready()







	@slow
	def 	_SCREAMING_SNAKE_CASE							(   self: int      ):

						'''simple docstring'''





						for model_name in ["roberta-base", "roberta-large"]:
											__magic_name__							=    AutoTokenizer.from_pretrained(__UpperCamelCase      )
											__magic_name__							=    FlaxRobertaModel.from_pretrained(__UpperCamelCase      )
											__magic_name__							=    tokenizer('Do you support jax jitted function?'  , return_tensors=TensorType.JAX      )

											@jax.jit
											def eval(**__UpperCamelCase: Any      ):
																return model(**__UpperCamelCase      )

											eval(**__UpperCamelCase      ).block_until_ready()







	def 	_SCREAMING_SNAKE_CASE							(   self: Tuple      ):

						'''simple docstring'''





						with self.assertRaisesRegex(
						    __UpperCamelCase  , 'bert-base is not a local folder and is not a valid model identifier'      ):
											__magic_name__							=    FlaxAutoModel.from_pretrained('bert-base'      )







	def 	_SCREAMING_SNAKE_CASE							(   self: List[Any]      ):

						'''simple docstring'''





						with self.assertRaisesRegex(
						    __UpperCamelCase  , r'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'      ):
											__magic_name__							=    FlaxAutoModel.from_pretrained(__UpperCamelCase  , revision='aaaaaa'      )







	def 	_SCREAMING_SNAKE_CASE							(   self: str      ):

						'''simple docstring'''





						with self.assertRaisesRegex(
						    __UpperCamelCase  , 'hf-internal-testing/config-no-model does not appear to have a file named flax_model.msgpack'  , ):
											__magic_name__							=    FlaxAutoModel.from_pretrained('hf-internal-testing/config-no-model'      )







	def 	_SCREAMING_SNAKE_CASE							(   self: int      ):

						'''simple docstring'''





						with self.assertRaisesRegex(__UpperCamelCase  , 'Use `from_pt=True` to load this model'      ):
											__magic_name__							=    FlaxAutoModel.from_pretrained('hf-internal-testing/tiny-bert-pt-only'      )




