


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


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

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


	def    A(						self):
							__UpperCAmelCase :       int  						=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

							__UpperCAmelCase :       Tuple  						=  None
							if self.use_labels:
													__UpperCAmelCase :       Optional[Any]  						=  ids_tensor([self.batch_size]							,    self.type_sequence_label_size)

							__UpperCAmelCase :       str  						=  self.get_config()

							return config, pixel_values, labels


	def    A(						self):
							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    A(						self							,    lowercase__							,    lowercase__							,    lowercase__):
							__UpperCAmelCase :       str  						=  DeiTModel(config=lowercase__)
							model.to(lowercase__)
							model.eval()
							__UpperCAmelCase :       List[Any]  						=  model(lowercase__)
							self.parent.assertEqual(result.last_hidden_state.shape							,    (self.batch_size, self.seq_length, self.hidden_size))


	def    A(						self							,    lowercase__							,    lowercase__							,    lowercase__):
							__UpperCAmelCase :       Union[str, Any]  						=  DeiTForMaskedImageModeling(config=lowercase__)
							model.to(lowercase__)
							model.eval()
							__UpperCAmelCase :       Union[str, Any]  						=  model(lowercase__)
							self.parent.assertEqual(
							    result.reconstruction.shape							,    (self.batch_size, self.num_channels, self.image_size, self.image_size))

							# test greyscale images
							__UpperCAmelCase :       List[str]  						=  1
							__UpperCAmelCase :       Dict  						=  DeiTForMaskedImageModeling(lowercase__)
							model.to(lowercase__)
							model.eval()

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


	def    A(						self							,    lowercase__							,    lowercase__							,    lowercase__):
							__UpperCAmelCase :       List[str]  						=  self.type_sequence_label_size
							__UpperCAmelCase :       Optional[Any]  						=  DeiTForImageClassification(lowercase__)
							model.to(lowercase__)
							model.eval()
							__UpperCAmelCase :       Tuple  						=  model(lowercase__							,    labels=lowercase__)
							self.parent.assertEqual(result.logits.shape							,    (self.batch_size, self.type_sequence_label_size))

							# test greyscale images
							__UpperCAmelCase :       List[str]  						=  1
							__UpperCAmelCase :       List[Any]  						=  DeiTForImageClassification(lowercase__)
							model.to(lowercase__)
							model.eval()

							__UpperCAmelCase :       Any  						=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
							__UpperCAmelCase :       int  						=  model(lowercase__							,    labels=lowercase__)
							self.parent.assertEqual(result.logits.shape							,    (self.batch_size, self.type_sequence_label_size))


	def    A(						self):
							__UpperCAmelCase :       Optional[int]  						=  self.prepare_config_and_inputs()
							(
							    (
							    __UpperCAmelCase
							)						,			(
							    __UpperCAmelCase
							)						,			(
							    __UpperCAmelCase
							)						,			
							) :       Optional[Any]  						=  config_and_inputs
							__UpperCAmelCase :       Dict  						=  {'''pixel_values''': pixel_values}
							return config, inputs_dict






@require_torch
class        lowerCamelCase      (     _UpperCamelCase       ,	_UpperCamelCase       ,	unittest.TestCase   ):
	_lowerCAmelCase    :		Optional[Any]        =	(
	    (
	        DeiTModel,
	        DeiTForImageClassification,
	        DeiTForImageClassificationWithTeacher,
	        DeiTForMaskedImageModeling,
	    )
	    if is_torch_available()
	    else ()
	)
	_lowerCAmelCase    :		Tuple        =	(
	    {
	        '''feature-extraction''': DeiTModel,
	        '''image-classification''': (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
	    }
	    if is_torch_available()
	    else {}
	)

	_lowerCAmelCase    :		List[Any]        =	False
	_lowerCAmelCase    :		int        =	False
	_lowerCAmelCase    :		Tuple        =	False


	def    A(						self):
							__UpperCAmelCase :       Dict  						=  DeiTModelTester(self)
							__UpperCAmelCase :       Union[str, Any]  						=  ConfigTester(self							,    config_class=lowercase__							,    has_text_modality=lowercase__							,    hidden_size=3_7)


	def    A(						self):
							self.config_tester.run_common_tests()


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


	def    A(						self):
							__UpperCAmelCase						,			__UpperCAmelCase :       Union[str, Any]  						=  self.model_tester.prepare_config_and_inputs_for_common()

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


	def    A(						self):
							__UpperCAmelCase						,			__UpperCAmelCase :       str  						=  self.model_tester.prepare_config_and_inputs_for_common()

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

													__UpperCAmelCase :       str  						=  ['''pixel_values''']
													self.assertListEqual(arg_names[:1]							,    lowercase__)


	def    A(						self):
							__UpperCAmelCase :       Union[str, Any]  						=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowercase__)


	def    A(						self):
							__UpperCAmelCase :       str  						=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_image_modeling(*lowercase__)


	def    A(						self):
							__UpperCAmelCase :       Optional[int]  						=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*lowercase__)


	def    A(						self							,    lowercase__							,    lowercase__							,    lowercase__=False):
							__UpperCAmelCase :       List[str]  						=  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    A(						self):
							if not self.model_tester.is_training:
													return

							__UpperCAmelCase						,			__UpperCAmelCase :       Any  						=  self.model_tester.prepare_config_and_inputs_for_common()
							__UpperCAmelCase :       List[Any]  						=  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
													__UpperCAmelCase :       Union[str, Any]  						=  model_class(lowercase__)
													model.to(lowercase__)
													model.train()
													__UpperCAmelCase :       Optional[int]  						=  self._prepare_for_class(lowercase__							,    lowercase__							,    return_labels=lowercase__)
													__UpperCAmelCase :       Optional[int]  						=  model(**lowercase__).loss
													loss.backward()


	def    A(						self):
							__UpperCAmelCase						,			__UpperCAmelCase :       List[str]  						=  self.model_tester.prepare_config_and_inputs_for_common()
							if not self.model_tester.is_training:
													return

							__UpperCAmelCase :       str  						=  False
							__UpperCAmelCase :       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
													__UpperCAmelCase :       List[str]  						=  model_class(lowercase__)
													model.gradient_checkpointing_enable()
													model.to(lowercase__)
													model.train()
													__UpperCAmelCase :       Any  						=  self._prepare_for_class(lowercase__							,    lowercase__							,    return_labels=lowercase__)
													__UpperCAmelCase :       Optional[Any]  						=  model(**lowercase__).loss
													loss.backward()


	def    A(						self):
							__UpperCAmelCase						,			__UpperCAmelCase :       Any  						=  self.model_tester.prepare_config_and_inputs_for_common()

							__UpperCAmelCase :       str  						=  [
							    {'''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']}"):
																									__UpperCAmelCase :       Optional[int]  						=  problem_type['''title''']
																									__UpperCAmelCase :       List[Any]  						=  problem_type['''num_labels''']

																									__UpperCAmelCase :       Any  						=  model_class(lowercase__)
																									model.to(lowercase__)
																									model.train()

																									__UpperCAmelCase :       str  						=  self._prepare_for_class(lowercase__							,    lowercase__							,    return_labels=lowercase__)

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

																									__UpperCAmelCase :       int  						=  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:
																															__UpperCAmelCase :       Optional[Any]  						=  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    A(						self):
							for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__UpperCAmelCase :       Optional[Any]  						=  DeiTModel.from_pretrained(lowercase__)
													self.assertIsNotNone(lowercase__)






def 						__SCREAMING_SNAKE_CASE       (					)		->    Tuple:




						'''simple docstring'''







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





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


	@cached_property
	def    A(						self):
							return (
							    DeiTImageProcessor.from_pretrained('''facebook/deit-base-distilled-patch16-224''')
							    if is_vision_available()
							    else None
							)


	@slow
	def    A(						self):
							__UpperCAmelCase :       Optional[int]  						=  DeiTForImageClassificationWithTeacher.from_pretrained('''facebook/deit-base-distilled-patch16-224''').to(
							    lowercase__)

							__UpperCAmelCase :       List[Any]  						=  self.default_image_processor
							__UpperCAmelCase :       Optional[Any]  						=  prepare_img()
							__UpperCAmelCase :       Tuple  						=  image_processor(images=lowercase__							,    return_tensors='''pt''').to(lowercase__)

							# forward pass
							with torch.no_grad():
													__UpperCAmelCase :       Optional[Any]  						=  model(**lowercase__)

							# verify the logits
							__UpperCAmelCase :       List[str]  						=  torch.Size((1, 1_0_0_0))
							self.assertEqual(outputs.logits.shape							,    lowercase__)

							__UpperCAmelCase :       List[str]  						=  torch.tensor([-1.0_2_6_6, 0.1_9_1_2, -1.2_8_6_1]).to(lowercase__)

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


	@slow
	@require_accelerate
	@require_torch_gpu
	def    A(						self):
							__UpperCAmelCase :       str  						=  DeiTModel.from_pretrained(
							    '''facebook/deit-base-distilled-patch16-224'''							,    torch_dtype=torch.floataa							,    device_map='''auto''')
							__UpperCAmelCase :       Tuple  						=  self.default_image_processor

							__UpperCAmelCase :       Optional[int]  						=  prepare_img()
							__UpperCAmelCase :       Tuple  						=  image_processor(images=lowercase__							,    return_tensors='''pt''')
							__UpperCAmelCase :       int  						=  inputs.pixel_values.to(lowercase__)

							# forward pass to make sure inference works in fp16
							with torch.no_grad():
													__UpperCAmelCase :       Any  						=  model(lowercase__)





import pytest

import datasets


# Import fixture modules as plugins
lowerCAmelCase							=						["""tests.fixtures.files""", """tests.fixtures.hub""", """tests.fixtures.fsspec"""]




def 						__SCREAMING_SNAKE_CASE       (					lowercase_			,					lowercase_			)		->    str:




						'''simple docstring'''







						for item in items:
												if any(marker in item.keywords for marker in ['''integration''', '''unit''']			):
																		continue
												item.add_marker(pytest.mark.unit			)




def 						__SCREAMING_SNAKE_CASE       (					lowercase_			)		->    Tuple:




						'''simple docstring'''







						config.addinivalue_line('''markers'''			,					'''torchaudio_latest: mark test to run with torchaudio>=0.12'''			)




@pytest.fixture(autouse=lowercase_			)
def 						__SCREAMING_SNAKE_CASE       (					lowercase_			,					lowercase_			)		->    Dict:




						'''simple docstring'''







						__UpperCAmelCase :       Dict  						=  tmp_path_factory.getbasetemp() / '''cache'''
						__UpperCAmelCase :       List[Any]  						=  test_hf_cache_home / '''datasets'''
						__UpperCAmelCase :       Union[str, Any]  						=  test_hf_cache_home / '''metrics'''
						__UpperCAmelCase :       List[Any]  						=  test_hf_cache_home / '''modules'''
						monkeypatch.setattr('''datasets.config.HF_DATASETS_CACHE'''			,					str(lowercase_			)			)
						monkeypatch.setattr('''datasets.config.HF_METRICS_CACHE'''			,					str(lowercase_			)			)
						monkeypatch.setattr('''datasets.config.HF_MODULES_CACHE'''			,					str(lowercase_			)			)
						__UpperCAmelCase :       Any  						=  test_hf_datasets_cache / '''downloads'''
						monkeypatch.setattr('''datasets.config.DOWNLOADED_DATASETS_PATH'''			,					str(lowercase_			)			)
						__UpperCAmelCase :       List[Any]  						=  test_hf_datasets_cache / '''downloads''' / '''extracted'''
						monkeypatch.setattr('''datasets.config.EXTRACTED_DATASETS_PATH'''			,					str(lowercase_			)			)




@pytest.fixture(autouse=lowercase_			,					scope='''session'''			)
def 						__SCREAMING_SNAKE_CASE       (					)		->    str:




						'''simple docstring'''







						datasets.disable_progress_bar()




@pytest.fixture(autouse=lowercase_			)
def 						__SCREAMING_SNAKE_CASE       (					lowercase_			)		->    Tuple:




						'''simple docstring'''







						monkeypatch.setattr('''datasets.config.HF_UPDATE_DOWNLOAD_COUNTS'''			,					lowercase_			)





@pytest.fixture
def 						__SCREAMING_SNAKE_CASE       (					lowercase_			)		->    List[str]:




						'''simple docstring'''







						monkeypatch.setattr('''sqlalchemy.util.deprecations.SILENCE_UBER_WARNING'''			,					lowercase_			)

