





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  :							List[str]   ,      snake_case__  :							Union[str, Any]   ,      snake_case__  :							Any=13   ,      snake_case__  :							str=30   ,      snake_case__  :							List[Any]=2   ,      snake_case__  :							Any=3   ,      snake_case__  :							Tuple=True   ,      snake_case__  :							Union[str, Any]=True   ,      snake_case__  :							List[str]=32   ,      snake_case__  :							Optional[int]=5   ,      snake_case__  :							Any=4   ,      snake_case__  :							Union[str, Any]=37   ,      snake_case__  :							Optional[int]="gelu"   ,      snake_case__  :							int=0.1   ,      snake_case__  :							Optional[Any]=0.1   ,      snake_case__  :							List[str]=10   ,      snake_case__  :							Union[str, Any]=0.02   ,      snake_case__  :							List[str]=3   ,      snake_case__  :							List[str]=None   ,      snake_case__  :							List[str]=2   ,      ):
							lowercase							=     parent
							lowercase							=     batch_size
							lowercase							=     image_size
							lowercase							=     patch_size
							lowercase							=     num_channels
							lowercase							=     is_training
							lowercase							=     use_labels
							lowercase							=     hidden_size
							lowercase							=     num_hidden_layers
							lowercase							=     num_attention_heads
							lowercase							=     intermediate_size
							lowercase							=     hidden_act
							lowercase							=     hidden_dropout_prob
							lowercase							=     attention_probs_dropout_prob
							lowercase							=     type_sequence_label_size
							lowercase							=     initializer_range
							lowercase							=     scope
							lowercase							=     encoder_stride

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







			def 						SCREAMING_SNAKE_CASE__	(				self  :							Tuple      ):
							lowercase							=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

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

							lowercase							=     self.get_config()

							return config, pixel_values, labels







			def 						SCREAMING_SNAKE_CASE__	(				self  :							Dict      ):
							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=snake_case_   ,      initializer_range=self.initializer_range   ,      encoder_stride=self.encoder_stride   ,      )







			def 						SCREAMING_SNAKE_CASE__	(				self  :							Tuple   ,      snake_case__  :							Union[str, Any]   ,      snake_case__  :							List[Any]   ,      snake_case__  :							List[Any]      ):
							lowercase							=     DeiTModel(config=snake_case_      )
							model.to(snake_case_      )
							model.eval()
							lowercase							=     model(snake_case_      )
							self.parent.assertEqual(result.last_hidden_state.shape   ,      (self.batch_size, self.seq_length, self.hidden_size)      )







			def 						SCREAMING_SNAKE_CASE__	(				self  :							Dict   ,      snake_case__  :							int   ,      snake_case__  :							Any   ,      snake_case__  :							Any      ):
							lowercase							=     DeiTForMaskedImageModeling(config=snake_case_      )
							model.to(snake_case_      )
							model.eval()
							lowercase							=     model(snake_case_      )
							self.parent.assertEqual(
							    result.reconstruction.shape   ,      (self.batch_size, self.num_channels, self.image_size, self.image_size)      )

							# test greyscale images
							lowercase							=     1
							lowercase							=     DeiTForMaskedImageModeling(snake_case_      )
							model.to(snake_case_      )
							model.eval()

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







			def 						SCREAMING_SNAKE_CASE__	(				self  :							Tuple   ,      snake_case__  :							Optional[int]   ,      snake_case__  :							List[Any]   ,      snake_case__  :							int      ):
							lowercase							=     self.type_sequence_label_size
							lowercase							=     DeiTForImageClassification(snake_case_      )
							model.to(snake_case_      )
							model.eval()
							lowercase							=     model(snake_case_   ,      labels=snake_case_      )
							self.parent.assertEqual(result.logits.shape   ,      (self.batch_size, self.type_sequence_label_size)      )

							# test greyscale images
							lowercase							=     1
							lowercase							=     DeiTForImageClassification(snake_case_      )
							model.to(snake_case_      )
							model.eval()

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







			def 						SCREAMING_SNAKE_CASE__	(				self  :							Optional[int]      ):
							lowercase							=     self.prepare_config_and_inputs()
							(
							    (
							    lowercase						
							)					,    (
							    lowercase						
							)					,    (
							    lowercase						
							)					,    						
							)							=     config_and_inputs
							lowercase							=     {"""pixel_values""": pixel_values}
							return config, inputs_dict

@require_torch
class     A_		(			__a      ,      __a      ,      unittest.TestCase	):
			_A   :List[Any]  				=      (
			    (
			        DeiTModel,
			        DeiTForImageClassification,
			        DeiTForImageClassificationWithTeacher,
			        DeiTForMaskedImageModeling,
			    )
			    if is_torch_available()
			    else ()
			)
			_A   :int  				=      (
			    {
			        '''feature-extraction''': DeiTModel,
			        '''image-classification''': (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
			    }
			    if is_torch_available()
			    else {}
			)

			_A   :List[str]  				=      False
			_A   :Dict  				=      False
			_A   :List[str]  				=      False







			def 						SCREAMING_SNAKE_CASE__	(				self  :							Any      ):
							lowercase							=     DeiTModelTester(self      )
							lowercase							=     ConfigTester(self   ,      config_class=snake_case_   ,      has_text_modality=snake_case_   ,      hidden_size=37      )







			def 						SCREAMING_SNAKE_CASE__	(				self  :							Optional[int]      ):
							self.config_tester.run_common_tests()







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







			def 						SCREAMING_SNAKE_CASE__	(				self  :							List[str]      ):
							lowercase					,    lowercase							=     self.model_tester.prepare_config_and_inputs_for_common()

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







			def 						SCREAMING_SNAKE_CASE__	(				self  :							str      ):
							lowercase					,    lowercase							=     self.model_tester.prepare_config_and_inputs_for_common()

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

											lowercase							=     ["""pixel_values"""]
											self.assertListEqual(arg_names[:1]   ,      snake_case_      )







			def 						SCREAMING_SNAKE_CASE__	(				self  :							Dict      ):
							lowercase							=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*snake_case_      )







			def 						SCREAMING_SNAKE_CASE__	(				self  :							Any      ):
							lowercase							=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_image_modeling(*snake_case_      )







			def 						SCREAMING_SNAKE_CASE__	(				self  :							Tuple      ):
							lowercase							=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*snake_case_      )







			def 						SCREAMING_SNAKE_CASE__	(				self  :							Optional[Any]   ,      snake_case__  :							Union[str, Any]   ,      snake_case__  :							Optional[int]   ,      snake_case__  :							Tuple=False      ):
							lowercase							=     super()._prepare_for_class(snake_case_   ,      snake_case_   ,      return_labels=snake_case_      )

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

							return inputs_dict







			def 						SCREAMING_SNAKE_CASE__	(				self  :							List[str]      ):
							if not self.model_tester.is_training:
											return

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

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







			def 						SCREAMING_SNAKE_CASE__	(				self  :							Any      ):
							lowercase					,    lowercase							=     self.model_tester.prepare_config_and_inputs_for_common()
							if not self.model_tester.is_training:
											return

							lowercase							=     False
							lowercase							=     True

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







			def 						SCREAMING_SNAKE_CASE__	(				self  :							Tuple      ):
							lowercase					,    lowercase							=     self.model_tester.prepare_config_and_inputs_for_common()

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

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

																			lowercase							=     model_class(snake_case_      )
																			model.to(snake_case_      )
																			model.train()

																			lowercase							=     self._prepare_for_class(snake_case_   ,      snake_case_   ,      return_labels=snake_case_      )

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

																			lowercase							=     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=snake_case_      ) as warning_list:
																							lowercase							=     model(**snake_case_      ).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  :							Tuple      ):
							for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											lowercase							=     DeiTModel.from_pretrained(snake_case_      )
											self.assertIsNotNone(snake_case_      )


def       UpperCamelCase__    (		):
				lowercase							=     Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" )
				return image







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







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







			@slow
			def 						SCREAMING_SNAKE_CASE__	(				self  :							Tuple      ):
							lowercase							=     DeiTForImageClassificationWithTeacher.from_pretrained("""facebook/deit-base-distilled-patch16-224"""      ).to(
							    snake_case_      )

							lowercase							=     self.default_image_processor
							lowercase							=     prepare_img()
							lowercase							=     image_processor(images=snake_case_   ,      return_tensors="""pt"""      ).to(snake_case_      )

							# forward pass
							with torch.no_grad():
											lowercase							=     model(**snake_case_      )

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

							lowercase							=     torch.tensor([-1.0_266, 0.1_912, -1.2_861]      ).to(snake_case_      )

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







			@slow
			@require_accelerate
			@require_torch_gpu
			def 						SCREAMING_SNAKE_CASE__	(				self  :							Dict      ):
							lowercase							=     DeiTModel.from_pretrained(
							    """facebook/deit-base-distilled-patch16-224"""   ,      torch_dtype=torch.floataa   ,      device_map="""auto"""      )
							lowercase							=     self.default_image_processor

							lowercase							=     prepare_img()
							lowercase							=     image_processor(images=snake_case_   ,      return_tensors="""pt"""      )
							lowercase							=     inputs.pixel_values.to(snake_case_      )

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







import pytest

from datasets import Dataset, DatasetDict, Features, NamedSplit, Value
from datasets.io.text import TextDatasetReader

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases






def        __lowerCAmelCase		(	__lowerCamelCase						:     List[Any]					, __lowerCamelCase						:     Any						)	->    Dict:
					assert isinstance(__lowerCamelCase					, __lowerCamelCase						)
					assert dataset.num_rows == 4
					assert dataset.num_columns == 1
					assert dataset.column_names == ["text"]
					for feature, expected_dtype in expected_features.items():
										assert dataset.features[feature].dtype == expected_dtype






@pytest.mark.parametrize("""keep_in_memory"""					, [False, True]						)
def        __lowerCAmelCase		(	__lowerCamelCase						:     Tuple					, __lowerCamelCase						:     str					, __lowerCamelCase						:     Optional[Any]						)	->    List[Any]:
					__lowerCAmelCase						 =tmp_path / """cache"""
					__lowerCAmelCase						 ={"""text""": """string"""}
					with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
										__lowerCAmelCase						 =TextDatasetReader(__lowerCamelCase					, cache_dir=__lowerCamelCase					, keep_in_memory=__lowerCamelCase						).read()
					_check_text_dataset(__lowerCamelCase					, __lowerCamelCase						)






@pytest.mark.parametrize(
    """features"""					, [
        None,
        {"""text""": """string"""},
        {"""text""": """int32"""},
        {"""text""": """float32"""},
    ]					, )
def        __lowerCAmelCase		(	__lowerCamelCase						:     Tuple					, __lowerCamelCase						:     Any					, __lowerCamelCase						:     Tuple						)	->    List[Any]:
					__lowerCAmelCase						 =tmp_path / """cache"""
					__lowerCAmelCase						 ={"""text""": """string"""}
					__lowerCAmelCase						 =features.copy() if features else default_expected_features
					__lowerCAmelCase						 =(
					    Features({feature: Value(__lowerCamelCase						) for feature, dtype in features.items()}						) if features is not None else None
					)
					__lowerCAmelCase						 =TextDatasetReader(__lowerCamelCase					, features=__lowerCamelCase					, cache_dir=__lowerCamelCase						).read()
					_check_text_dataset(__lowerCamelCase					, __lowerCamelCase						)






@pytest.mark.parametrize("""split"""					, [None, NamedSplit("""train"""						), """train""", """test"""]						)
def        __lowerCAmelCase		(	__lowerCamelCase						:     Union[str, Any]					, __lowerCamelCase						:     List[str]					, __lowerCamelCase						:     Union[str, Any]						)	->    Any:
					__lowerCAmelCase						 =tmp_path / """cache"""
					__lowerCAmelCase						 ={"""text""": """string"""}
					__lowerCAmelCase						 =TextDatasetReader(__lowerCamelCase					, cache_dir=__lowerCamelCase					, split=__lowerCamelCase						).read()
					_check_text_dataset(__lowerCamelCase					, __lowerCamelCase						)
					assert dataset.split == split if split else "train"






@pytest.mark.parametrize("""path_type"""					, [str, list]						)
def        __lowerCAmelCase		(	__lowerCamelCase						:     List[Any]					, __lowerCamelCase						:     int					, __lowerCamelCase						:     List[str]						)	->    Any:
					if issubclass(__lowerCamelCase					, __lowerCamelCase						):
										__lowerCAmelCase						 =text_path
					elif issubclass(__lowerCamelCase					, __lowerCamelCase						):
										__lowerCAmelCase						 =[text_path]
					__lowerCAmelCase						 =tmp_path / """cache"""
					__lowerCAmelCase						 ={"""text""": """string"""}
					__lowerCAmelCase						 =TextDatasetReader(__lowerCamelCase					, cache_dir=__lowerCamelCase						).read()
					_check_text_dataset(__lowerCamelCase					, __lowerCamelCase						)






def        __lowerCAmelCase		(	__lowerCamelCase						:     Tuple					, __lowerCamelCase						:     List[str]					, __lowerCamelCase						:     str=("train",)						)	->    Dict:
					assert isinstance(__lowerCamelCase					, __lowerCamelCase						)
					for split in splits:
										__lowerCAmelCase						 =dataset_dict[split]
										assert dataset.num_rows == 4
										assert dataset.num_columns == 1
										assert dataset.column_names == ["text"]
										for feature, expected_dtype in expected_features.items():
															assert dataset.features[feature].dtype == expected_dtype






@pytest.mark.parametrize("""keep_in_memory"""					, [False, True]						)
def        __lowerCAmelCase		(	__lowerCamelCase						:     Optional[int]					, __lowerCamelCase						:     List[Any]					, __lowerCamelCase						:     List[Any]						)	->    Dict:
					__lowerCAmelCase						 =tmp_path / """cache"""
					__lowerCAmelCase						 ={"""text""": """string"""}
					with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
										__lowerCAmelCase						 =TextDatasetReader({"""train""": text_path}					, cache_dir=__lowerCamelCase					, keep_in_memory=__lowerCamelCase						).read()
					_check_text_datasetdict(__lowerCamelCase					, __lowerCamelCase						)






@pytest.mark.parametrize(
    """features"""					, [
        None,
        {"""text""": """string"""},
        {"""text""": """int32"""},
        {"""text""": """float32"""},
    ]					, )
def        __lowerCAmelCase		(	__lowerCamelCase						:     Any					, __lowerCamelCase						:     Optional[int]					, __lowerCamelCase						:     Union[str, Any]						)	->    Tuple:
					__lowerCAmelCase						 =tmp_path / """cache"""
					# CSV file loses col_1 string dtype information: default now is "int64" instead of "string"
					__lowerCAmelCase						 ={"""text""": """string"""}
					__lowerCAmelCase						 =features.copy() if features else default_expected_features
					__lowerCAmelCase						 =(
					    Features({feature: Value(__lowerCamelCase						) for feature, dtype in features.items()}						) if features is not None else None
					)
					__lowerCAmelCase						 =TextDatasetReader({"""train""": text_path}					, features=__lowerCamelCase					, cache_dir=__lowerCamelCase						).read()
					_check_text_datasetdict(__lowerCamelCase					, __lowerCamelCase						)






@pytest.mark.parametrize("""split"""					, [None, NamedSplit("""train"""						), """train""", """test"""]						)
def        __lowerCAmelCase		(	__lowerCamelCase						:     int					, __lowerCamelCase						:     List[str]					, __lowerCamelCase						:     Any						)	->    int:
					if split:
										__lowerCAmelCase						 ={split: text_path}
					else:
										__lowerCAmelCase						 ="""train"""
										__lowerCAmelCase						 ={"""train""": text_path, """test""": text_path}
					__lowerCAmelCase						 =tmp_path / """cache"""
					__lowerCAmelCase						 ={"""text""": """string"""}
					__lowerCAmelCase						 =TextDatasetReader(__lowerCamelCase					, cache_dir=__lowerCamelCase						).read()
					_check_text_datasetdict(__lowerCamelCase					, __lowerCamelCase					, splits=list(path.keys()						)						)
					assert all(dataset[split].split == split for split in path.keys()						)




