



"""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       _a  :



			def __init__(			self							:				Optional[int]   ,	_lowercase							:				int   ,	_lowercase							:				List[Any]=13   ,	_lowercase							:				Union[str, Any]=30   ,	_lowercase							:				Tuple=2   ,	_lowercase							:				Any=3   ,	_lowercase							:				Any=True   ,	_lowercase							:				List[Any]=True   ,	_lowercase							:				Any=32   ,	_lowercase							:				List[str]=5   ,	_lowercase							:				Optional[int]=4   ,	_lowercase							:				int=37   ,	_lowercase							:				List[Any]="gelu"   ,	_lowercase							:				Union[str, Any]=0.1   ,	_lowercase							:				Dict=0.1   ,	_lowercase							:				Optional[Any]=10   ,	_lowercase							:				Tuple=0.02   ,	_lowercase							:				Dict=3   ,	_lowercase							:				Tuple=None   ,	_lowercase							:				int=2   ,	)							->					List[Any]:
									snake_case   :			int						    = parent
									snake_case   :			Optional[int]						    = batch_size
									snake_case   :			List[str]						    = image_size
									snake_case   :			Tuple						    = patch_size
									snake_case   :			Union[str, Any]						    = num_channels
									snake_case   :			Dict						    = is_training
									snake_case   :			List[str]						    = use_labels
									snake_case   :			Tuple						    = hidden_size
									snake_case   :			str						    = num_hidden_layers
									snake_case   :			Any						    = num_attention_heads
									snake_case   :			int						    = intermediate_size
									snake_case   :			List[Any]						    = hidden_act
									snake_case   :			Optional[Any]						    = hidden_dropout_prob
									snake_case   :			Tuple						    = attention_probs_dropout_prob
									snake_case   :			int						    = type_sequence_label_size
									snake_case   :			Optional[int]						    = initializer_range
									snake_case   :			str						    = scope
									snake_case   :			Any						    = encoder_stride

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



			def 				__lowercase					(			self							:				List[Any]		)							->					Tuple:
									snake_case   :			List[str]						    = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

									snake_case   :			Optional[int]						    = None
									if self.use_labels:
															snake_case   :			Union[str, Any]						    = ids_tensor([self.batch_size]   ,	self.type_sequence_label_size		)

									snake_case   :			int						    = self.get_config()

									return config, pixel_values, labels



			def 				__lowercase					(			self							:				Dict		)							->					Any:
									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 				__lowercase					(			self							:				Optional[int]   ,	_lowercase							:				Any   ,	_lowercase							:				List[str]   ,	_lowercase							:				Dict		)							->					Union[str, Any]:
									snake_case   :			Dict						    = DeiTModel(config=_lowercase		)
									model.to(_lowercase		)
									model.eval()
									snake_case   :			str						    = model(_lowercase		)
									self.parent.assertEqual(result.last_hidden_state.shape   ,	(self.batch_size, self.seq_length, self.hidden_size)		)



			def 				__lowercase					(			self							:				List[str]   ,	_lowercase							:				Dict   ,	_lowercase							:				List[Any]   ,	_lowercase							:				Any		)							->					Optional[Any]:
									snake_case   :			str						    = DeiTForMaskedImageModeling(config=_lowercase		)
									model.to(_lowercase		)
									model.eval()
									snake_case   :			List[str]						    = model(_lowercase		)
									self.parent.assertEqual(
									    result.reconstruction.shape   ,	(self.batch_size, self.num_channels, self.image_size, self.image_size)		)

									# test greyscale images
									snake_case   :			str						    = 1
									snake_case   :			str						    = DeiTForMaskedImageModeling(_lowercase		)
									model.to(_lowercase		)
									model.eval()

									snake_case   :			Optional[Any]						    = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
									snake_case   :			Optional[int]						    = model(_lowercase		)
									self.parent.assertEqual(result.reconstruction.shape   ,	(self.batch_size, 1, self.image_size, self.image_size)		)



			def 				__lowercase					(			self							:				str   ,	_lowercase							:				Union[str, Any]   ,	_lowercase							:				Any   ,	_lowercase							:				Optional[Any]		)							->					List[Any]:
									snake_case   :			List[Any]						    = self.type_sequence_label_size
									snake_case   :			Optional[Any]						    = DeiTForImageClassification(_lowercase		)
									model.to(_lowercase		)
									model.eval()
									snake_case   :			Union[str, Any]						    = model(_lowercase   ,	labels=_lowercase		)
									self.parent.assertEqual(result.logits.shape   ,	(self.batch_size, self.type_sequence_label_size)		)

									# test greyscale images
									snake_case   :			int						    = 1
									snake_case   :			Tuple						    = DeiTForImageClassification(_lowercase		)
									model.to(_lowercase		)
									model.eval()

									snake_case   :			List[str]						    = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
									snake_case   :			List[str]						    = model(_lowercase   ,	labels=_lowercase		)
									self.parent.assertEqual(result.logits.shape   ,	(self.batch_size, self.type_sequence_label_size)		)



			def 				__lowercase					(			self							:				Optional[int]		)							->					int:
									snake_case   :			Optional[int]						    = self.prepare_config_and_inputs()
									(
									    (
									    snake_case
									)    ,			(
									    snake_case
									)    ,			(
									    snake_case
									)    ,			
									)   :			Union[str, Any]						    = config_and_inputs
									snake_case   :			Optional[Any]						    = {"pixel_values": pixel_values}
									return config, inputs_dict



@require_torch
class       _a  ( SCREAMING_SNAKE_CASE__     ,	SCREAMING_SNAKE_CASE__     ,	unittest.TestCase):
			__magic_name__     					=     (
			    (
			        DeiTModel,
			        DeiTForImageClassification,
			        DeiTForImageClassificationWithTeacher,
			        DeiTForMaskedImageModeling,
			    )
			    if is_torch_available()
			    else ()
			)
			__magic_name__     					=     (
			    {
			        """feature-extraction""": DeiTModel,
			        """image-classification""": (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
			    }
			    if is_torch_available()
			    else {}
			)

			__magic_name__     					=     False
			__magic_name__     					=     False
			__magic_name__     					=     False



			def 				__lowercase					(			self							:				Any		)							->					int:
									snake_case   :			List[Any]						    = DeiTModelTester(self		)
									snake_case   :			Any						    = ConfigTester(self   ,	config_class=_lowercase   ,	has_text_modality=_lowercase   ,	hidden_size=37		)



			def 				__lowercase					(			self							:				str		)							->					Tuple:
									self.config_tester.run_common_tests()



			@unittest.skip(reason="DeiT does not use inputs_embeds"		)
			def 				__lowercase					(			self							:				int		)							->					Optional[Any]:
									pass



			def 				__lowercase					(			self							:				Optional[int]		)							->					List[Any]:
									snake_case    ,			snake_case   :			Optional[int]						    = self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
															snake_case   :			Optional[Any]						    = model_class(_lowercase		)
															self.assertIsInstance(model.get_input_embeddings()   ,	(nn.Module)		)
															snake_case   :			Any						    = model.get_output_embeddings()
															self.assertTrue(x is None or isinstance(_lowercase   ,	nn.Linear		)		)



			def 				__lowercase					(			self							:				int		)							->					Dict:
									snake_case    ,			snake_case   :			Optional[int]						    = self.model_tester.prepare_config_and_inputs_for_common()

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

															snake_case   :			Any						    = ["pixel_values"]
															self.assertListEqual(arg_names[:1]   ,	_lowercase		)



			def 				__lowercase					(			self							:				str		)							->					str:
									snake_case   :			Tuple						    = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_lowercase		)



			def 				__lowercase					(			self							:				str		)							->					Union[str, Any]:
									snake_case   :			Tuple						    = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_image_modeling(*_lowercase		)



			def 				__lowercase					(			self							:				str		)							->					int:
									snake_case   :			Dict						    = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*_lowercase		)



			def 				__lowercase					(			self							:				str   ,	_lowercase							:				List[Any]   ,	_lowercase							:				Any   ,	_lowercase							:				Dict=False		)							->					int:
									snake_case   :			Optional[int]						    = super()._prepare_for_class(_lowercase   ,	_lowercase   ,	return_labels=_lowercase		)

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

									return inputs_dict



			def 				__lowercase					(			self							:				Optional[int]		)							->					List[Any]:
									if not self.model_tester.is_training:
															return

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



			def 				__lowercase					(			self							:				str		)							->					Optional[int]:
									snake_case    ,			snake_case   :			int						    = self.model_tester.prepare_config_and_inputs_for_common()
									if not self.model_tester.is_training:
															return

									snake_case   :			Any						    = False
									snake_case   :			Union[str, Any]						    = True

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



			def 				__lowercase					(			self							:				str		)							->					Optional[Any]:
									snake_case    ,			snake_case   :			Union[str, Any]						    = self.model_tester.prepare_config_and_inputs_for_common()

									snake_case   :			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"]}'''		):
																											snake_case   :			Union[str, Any]						    = problem_type["title"]
																											snake_case   :			str						    = problem_type["num_labels"]

																											snake_case   :			Union[str, Any]						    = model_class(_lowercase		)
																											model.to(_lowercase		)
																											model.train()

																											snake_case   :			Tuple						    = self._prepare_for_class(_lowercase   ,	_lowercase   ,	return_labels=_lowercase		)

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

																											snake_case   :			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:
																																	snake_case   :			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 				__lowercase					(			self							:				Union[str, Any]		)							->					Any:
									for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															snake_case   :			List[Any]						    = DeiTModel.from_pretrained(_lowercase		)
															self.assertIsNotNone(_lowercase		)



def  __SCREAMING_SNAKE_CASE		(			):
						"""simple docstring"""






						snake_case   :			List[Any]						    = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" )
						return image

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



			@cached_property
			def 				__lowercase					(			self							:				Dict		)							->					int:
									return (
									    DeiTImageProcessor.from_pretrained("facebook/deit-base-distilled-patch16-224"		)
									    if is_vision_available()
									    else None
									)



			@slow
			def 				__lowercase					(			self							:				Optional[Any]		)							->					int:
									snake_case   :			Optional[Any]						    = DeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224"		).to(
									    _lowercase		)

									snake_case   :			str						    = self.default_image_processor
									snake_case   :			Dict						    = prepare_img()
									snake_case   :			Dict						    = image_processor(images=_lowercase   ,	return_tensors="pt"		).to(_lowercase		)

									# forward pass
									with torch.no_grad():
															snake_case   :			Any						    = model(**_lowercase		)

									# verify the logits
									snake_case   :			Optional[int]						    = torch.Size((1, 1000)		)
									self.assertEqual(outputs.logits.shape   ,	_lowercase		)

									snake_case   :			int						    = torch.tensor([-1.0266, 0.1912, -1.2861]		).to(_lowercase		)

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



			@slow
			@require_accelerate
			@require_torch_gpu
			def 				__lowercase					(			self							:				Any		)							->					List[Any]:
									snake_case   :			str						    = DeiTModel.from_pretrained(
									    "facebook/deit-base-distilled-patch16-224"   ,	torch_dtype=torch.floataa   ,	device_map="auto"		)
									snake_case   :			Union[str, Any]						    = self.default_image_processor

									snake_case   :			Dict						    = prepare_img()
									snake_case   :			List[str]						    = image_processor(images=_lowercase   ,	return_tensors="pt"		)
									snake_case   :			List[Any]						    = inputs.pixel_values.to(_lowercase		)

									# forward pass to make sure inference works in fp16
									with torch.no_grad():
															snake_case   :			str						    = model(_lowercase		)








"""simple docstring"""







import contextlib
import importlib
import io
import unittest

import transformers

# Try to import everything from transformers to ensure every object can be loaded.
from transformers import *  # noqa F406
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, require_tf, require_torch
from transformers.utils import ContextManagers, find_labels, is_flax_available, is_tf_available, is_torch_available


if is_torch_available():
			from transformers import BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification

if is_tf_available():
			from transformers import TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification

if is_flax_available():
			from transformers import FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification


A		=						DUMMY_UNKNOWN_IDENTIFIER
# An actual model hosted on huggingface.co

A		=						'main'
# Default branch name
A		=						'f2c752cfc5c0ab6f4bdec59acea69eefbee381c2'
# One particular commit (not the top of `main`)
A		=						'aaaaaaa'
# This commit does not exist, so we should 404.

A		=						'd9e9f15bc825e4b2c9249e9578f884bbcb5e3684'
# Sha-1 of config.json on the top of `main`, for checking purposes
A		=						'4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d3'

@contextlib.contextmanager
def  __SCREAMING_SNAKE_CASE		(			):
						"""simple docstring"""






						print("Welcome!" )
						yield
						print("Bye!" )

@contextlib.contextmanager
def  __SCREAMING_SNAKE_CASE		(			):
						"""simple docstring"""






						print("Bonjour!" )
						yield
						print("Au revoir!" )

class       _a  ( unittest.TestCase):



			def 				__lowercase					(			self							:				Dict		)							->					Optional[int]:
									# If the spec is missing, importlib would not be able to import the module dynamically.
									assert transformers.__spec__ is not None
									assert importlib.util.find_spec("transformers"		) is not None



class       _a  ( unittest.TestCase):



			@unittest.mock.patch("sys.stdout"   ,	new_callable=io.StringIO		)
			def 				__lowercase					(			self							:				Union[str, Any]   ,	_lowercase							:				Optional[int]		)							->					Optional[Any]:
									with ContextManagers([]		):
															print("Transformers are awesome!"		)
									# The print statement adds a new line at the end of the output
									self.assertEqual(mock_stdout.getvalue()   ,	"Transformers are awesome!\n"		)



			@unittest.mock.patch("sys.stdout"   ,	new_callable=io.StringIO		)
			def 				__lowercase					(			self							:				Any   ,	_lowercase							:				Any		)							->					Dict:
									with ContextManagers([context_en()]		):
															print("Transformers are awesome!"		)
									# The output should be wrapped with an English welcome and goodbye
									self.assertEqual(mock_stdout.getvalue()   ,	"Welcome!\nTransformers are awesome!\nBye!\n"		)



			@unittest.mock.patch("sys.stdout"   ,	new_callable=io.StringIO		)
			def 				__lowercase					(			self							:				str   ,	_lowercase							:				List[Any]		)							->					Optional[int]:
									with ContextManagers([context_fr(), context_en()]		):
															print("Transformers are awesome!"		)
									# The output should be wrapped with an English and French welcome and goodbye
									self.assertEqual(mock_stdout.getvalue()   ,	"Bonjour!\nWelcome!\nTransformers are awesome!\nBye!\nAu revoir!\n"		)



			@require_torch
			def 				__lowercase					(			self							:				Any		)							->					Union[str, Any]:
									self.assertEqual(find_labels(_lowercase		)   ,	["labels"]		)
									self.assertEqual(find_labels(_lowercase		)   ,	["labels", "next_sentence_label"]		)
									self.assertEqual(find_labels(_lowercase		)   ,	["start_positions", "end_positions"]		)

									class       _a  ( SCREAMING_SNAKE_CASE__):
												pass



									self.assertEqual(find_labels(_lowercase		)   ,	["labels"]		)



			@require_tf
			def 				__lowercase					(			self							:				Optional[Any]		)							->					int:
									self.assertEqual(find_labels(_lowercase		)   ,	["labels"]		)
									self.assertEqual(find_labels(_lowercase		)   ,	["labels", "next_sentence_label"]		)
									self.assertEqual(find_labels(_lowercase		)   ,	["start_positions", "end_positions"]		)

									class       _a  ( SCREAMING_SNAKE_CASE__):
												pass



									self.assertEqual(find_labels(_lowercase		)   ,	["labels"]		)



			@require_flax
			def 				__lowercase					(			self							:				Tuple		)							->					List[Any]:
									# Flax models don't have labels
									self.assertEqual(find_labels(_lowercase		)   ,	[]		)
									self.assertEqual(find_labels(_lowercase		)   ,	[]		)
									self.assertEqual(find_labels(_lowercase		)   ,	[]		)

									class       _a  ( SCREAMING_SNAKE_CASE__):
												pass



									self.assertEqual(find_labels(_lowercase		)   ,	[]		)



