



"""simple docstring"""







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  :    str      ,							snake_case  :    Any      ,							snake_case  :    Union[str, Any]=13      ,							snake_case  :    Union[str, Any]=30      ,							snake_case  :    int=2      ,							snake_case  :    Optional[Any]=3      ,							snake_case  :    Any=True      ,							snake_case  :    Any=True      ,							snake_case  :    Any=32      ,							snake_case  :    Tuple=5      ,							snake_case  :    int=4      ,							snake_case  :    Optional[Any]=37      ,							snake_case  :    Optional[int]="gelu"      ,							snake_case  :    str=0.1      ,							snake_case  :    Any=0.1      ,							snake_case  :    Optional[int]=10      ,							snake_case  :    Any=0.02      ,							snake_case  :    Any=3      ,							snake_case  :    Union[str, Any]=None      ,							snake_case  :    int=2      ,							)			->						str:






									'''simple docstring'''

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

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



		def 					A_		( self  :    Dict				)			->						Optional[Any]:






									'''simple docstring'''

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

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

									A         =					self.get_config()

									return config, pixel_values, labels



		def 					A_		( self  :    Optional[int]				)			->						Dict:






									'''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 					A_		( self  :    int      ,							snake_case  :    Union[str, Any]      ,							snake_case  :    int      ,							snake_case  :    Any				)			->						List[Any]:






									'''simple docstring'''

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



		def 					A_		( self  :    Optional[Any]      ,							snake_case  :    Any      ,							snake_case  :    int      ,							snake_case  :    List[str]				)			->						Any:






									'''simple docstring'''

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

									# test greyscale images
									A         =					1
									A         =					DeiTForMaskedImageModeling(__A				)
									model.to(__A				)
									model.eval()

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



		def 					A_		( self  :    Dict      ,							snake_case  :    Optional[int]      ,							snake_case  :    Dict      ,							snake_case  :    str				)			->						Union[str, Any]:






									'''simple docstring'''

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

									# test greyscale images
									A         =					1
									A         =					DeiTForImageClassification(__A				)
									model.to(__A				)
									model.eval()

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



		def 					A_		( self  :    str				)			->						str:






									'''simple docstring'''

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






@require_torch
class      UpperCAmelCase__		(      UpperCamelCase   ,UpperCamelCase   ,unittest.TestCase     ):
		lowerCAmelCase_       :	List[str] 							=     (
		    (
		        DeiTModel,
		        DeiTForImageClassification,
		        DeiTForImageClassificationWithTeacher,
		        DeiTForMaskedImageModeling,
		    )
		    if is_torch_available()
		    else ()
		)
		lowerCAmelCase_       :	List[str] 							=     (
		    {
		        """feature-extraction""": DeiTModel,
		        """image-classification""": (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
		    }
		    if is_torch_available()
		    else {}
		)

		lowerCAmelCase_       :	Dict 							=     False
		lowerCAmelCase_       :	Tuple 							=     False
		lowerCAmelCase_       :	Optional[Any] 							=     False



		def 					A_		( self  :    List[Any]				)			->						Optional[int]:






									'''simple docstring'''

									A         =					DeiTModelTester(self				)
									A         =					ConfigTester(self      ,							config_class=__A      ,							has_text_modality=__A      ,							hidden_size=37				)



		def 					A_		( self  :    Tuple				)			->						Dict:






									'''simple docstring'''

									self.config_tester.run_common_tests()



		@unittest.skip(reason='DeiT does not use inputs_embeds'				)
		def 					A_		( self  :    Optional[int]				)			->						Union[str, Any]:






									'''simple docstring'''

									pass



		def 					A_		( self  :    Tuple				)			->						str:






									'''simple docstring'''

									A      ,  A         =					self.model_tester.prepare_config_and_inputs_for_common()

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



		def 					A_		( self  :    List[str]				)			->						Tuple:






									'''simple docstring'''

									A      ,  A         =					self.model_tester.prepare_config_and_inputs_for_common()

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

																A         =					['pixel_values']
																self.assertListEqual(arg_names[:1]      ,							__A				)



		def 					A_		( self  :    Dict				)			->						Dict:






									'''simple docstring'''

									A         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*__A				)



		def 					A_		( self  :    Optional[int]				)			->						Tuple:






									'''simple docstring'''

									A         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_image_modeling(*__A				)



		def 					A_		( self  :    Dict				)			->						Any:






									'''simple docstring'''

									A         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*__A				)



		def 					A_		( self  :    Tuple      ,							snake_case  :    Optional[Any]      ,							snake_case  :    Optional[int]      ,							snake_case  :    Tuple=False				)			->						List[Any]:






									'''simple docstring'''

									A         =					super()._prepare_for_class(__A      ,							__A      ,							return_labels=__A				)

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

									return inputs_dict



		def 					A_		( self  :    Tuple				)			->						Optional[int]:






									'''simple docstring'''

									if not self.model_tester.is_training:
																return

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

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



		def 					A_		( self  :    Optional[Any]				)			->						Tuple:






									'''simple docstring'''

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

									A         =					False
									A         =					True

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



		def 					A_		( self  :    List[Any]				)			->						str:






									'''simple docstring'''

									A      ,  A         =					self.model_tester.prepare_config_and_inputs_for_common()

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

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

																														A         =					model_class(__A				)
																														model.to(__A				)
																														model.train()

																														A         =					self._prepare_for_class(__A      ,							__A      ,							return_labels=__A				)

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

																														A         =					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=__A				) as warning_list:
																																					A         =					model(**__A				).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  :    Optional[Any]				)			->						Union[str, Any]:






									'''simple docstring'''

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



def   lowerCAmelCase__			(     ) ->							Optional[int]:
							A         =					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'			)
							return image






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



		@cached_property
		def 					A_		( self  :    Any				)			->						Optional[int]:






									'''simple docstring'''

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



		@slow
		def 					A_		( self  :    Dict				)			->						Any:






									'''simple docstring'''

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

									A         =					self.default_image_processor
									A         =					prepare_img()
									A         =					image_processor(images=__A      ,							return_tensors='pt'				).to(__A				)

									# forward pass
									with torch.no_grad():
																A         =					model(**__A				)

									# verify the logits
									A         =					torch.Size((1, 1_000)				)
									self.assertEqual(outputs.logits.shape      ,							__A				)

									A         =					torch.tensor([-1.0266, 0.1912, -1.2861]				).to(__A				)

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



		@slow
		@require_accelerate
		@require_torch_gpu
		def 					A_		( self  :    Optional[int]				)			->						List[Any]:






									'''simple docstring'''

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

									A         =					prepare_img()
									A         =					image_processor(images=__A      ,							return_tensors='pt'				)
									A         =					inputs.pixel_values.to(__A				)

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











"""simple docstring"""







from datasets.utils.patching import _PatchedModuleObj, patch_submodule

from . import _test_patching

def   lowerCAmelCase__			(     ) ->							Optional[Any]:
							import os as original_os
							from os import path as original_path
							from os import rename as original_rename
							from os.path import dirname as original_dirname
							from os.path import join as original_join

							assert _test_patching.os is original_os
							assert _test_patching.path is original_path
							assert _test_patching.join is original_join

							assert _test_patching.renamed_os is original_os
							assert _test_patching.renamed_path is original_path
							assert _test_patching.renamed_join is original_join

							A         =					'__test_patch_submodule_mock__'
							with patch_submodule(_test_patching				,      'os.path.join'				,      lowerCamelCase__			):
														# Every way to access os.path.join must be patched, and the rest must stay untouched

														# check os.path.join
														assert isinstance(_test_patching.os				,      _PatchedModuleObj			)
														assert isinstance(_test_patching.os.path				,      _PatchedModuleObj			)
														assert _test_patching.os.path.join is mock

														# check path.join
														assert isinstance(_test_patching.path				,      _PatchedModuleObj			)
														assert _test_patching.path.join is mock

														# check join
														assert _test_patching.join is mock

														# check that the other attributes are untouched
														assert _test_patching.os.rename is original_rename
														assert _test_patching.path.dirname is original_dirname
														assert _test_patching.os.path.dirname is original_dirname

														# Even renamed modules or objects must be patched

														# check renamed_os.path.join
														assert isinstance(_test_patching.renamed_os				,      _PatchedModuleObj			)
														assert isinstance(_test_patching.renamed_os.path				,      _PatchedModuleObj			)
														assert _test_patching.renamed_os.path.join is mock

														# check renamed_path.join
														assert isinstance(_test_patching.renamed_path				,      _PatchedModuleObj			)
														assert _test_patching.renamed_path.join is mock

														# check renamed_join
														assert _test_patching.renamed_join is mock

														# check that the other attributes are untouched
														assert _test_patching.renamed_os.rename is original_rename
														assert _test_patching.renamed_path.dirname is original_dirname
														assert _test_patching.renamed_os.path.dirname is original_dirname

							# check that everthing is back to normal when the patch is over

							assert _test_patching.os is original_os
							assert _test_patching.path is original_path
							assert _test_patching.join is original_join

							assert _test_patching.renamed_os is original_os
							assert _test_patching.renamed_path is original_path
							assert _test_patching.renamed_join is original_join

def   lowerCAmelCase__			(     ) ->							str:
							assert _test_patching.open is open

							A         =					'__test_patch_submodule_builtin_mock__'
							# _test_patching has "open" in its globals
							assert _test_patching.open is open
							with patch_submodule(_test_patching				,      'open'				,      lowerCamelCase__			):
														assert _test_patching.open is mock

							# check that everthing is back to normal when the patch is over

							assert _test_patching.open is open

def   lowerCAmelCase__			(     ) ->							List[Any]:
							# pandas.read_csv is not present in _test_patching
							A         =					'__test_patch_submodule_missing_mock__'
							with patch_submodule(_test_patching				,      'pandas.read_csv'				,      lowerCamelCase__			):
														pass

def   lowerCAmelCase__			(     ) ->							Union[str, Any]:
							# builtin should always be mocked even if they're not in the globals
							# in case they're loaded at one point
							A         =					'__test_patch_submodule_missing_builtin_mock__'
							# _test_patching doesn't have "len" in its globals
							assert getattr(_test_patching				,      'len'				,      lowerCamelCase__			) is None
							with patch_submodule(_test_patching				,      'len'				,      lowerCamelCase__			):
														assert _test_patching.len is mock
							assert _test_patching.len is len

def   lowerCAmelCase__			(     ) ->							Union[str, Any]:
							A         =					'__test_patch_submodule_start_and_stop_mock__'
							A         =					patch_submodule(_test_patching				,      'open'				,      lowerCamelCase__			)
							assert _test_patching.open is open
							patch.start()
							assert _test_patching.open is mock
							patch.stop()
							assert _test_patching.open is open

def   lowerCAmelCase__			(     ) ->							int:
							from os import rename as original_rename
							from os.path import dirname as original_dirname
							from os.path import join as original_join

							A         =					'__test_patch_submodule_successive_join__'
							A         =					'__test_patch_submodule_successive_dirname__'
							A         =					'__test_patch_submodule_successive_rename__'
							assert _test_patching.os.path.join is original_join
							assert _test_patching.os.path.dirname is original_dirname
							assert _test_patching.os.rename is original_rename

							with patch_submodule(_test_patching				,      'os.path.join'				,      lowerCamelCase__			):
														with patch_submodule(_test_patching				,      'os.rename'				,      lowerCamelCase__			):
																					with patch_submodule(_test_patching				,      'os.path.dirname'				,      lowerCamelCase__			):
																												assert _test_patching.os.path.join is mock_join
																												assert _test_patching.os.path.dirname is mock_dirname
																												assert _test_patching.os.rename is mock_rename

    # try another order
							with patch_submodule(_test_patching				,      'os.rename'				,      lowerCamelCase__			):
														with patch_submodule(_test_patching				,      'os.path.join'				,      lowerCamelCase__			):
																					with patch_submodule(_test_patching				,      'os.path.dirname'				,      lowerCamelCase__			):
																												assert _test_patching.os.path.join is mock_join
																												assert _test_patching.os.path.dirname is mock_dirname
																												assert _test_patching.os.rename is mock_rename

							assert _test_patching.os.path.join is original_join
							assert _test_patching.os.path.dirname is original_dirname
							assert _test_patching.os.rename is original_rename

def   lowerCAmelCase__			(     ) ->							Optional[Any]:
							A         =					'__test_patch_submodule_doesnt_exist_mock__'
							with patch_submodule(_test_patching				,      '__module_that_doesn_exist__.__attribute_that_doesn_exist__'				,      lowerCamelCase__			):
														pass
							with patch_submodule(_test_patching				,      'os.__attribute_that_doesn_exist__'				,      lowerCamelCase__			):
														pass



