




'''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	:  int							,      lowerCAmelCase_	:  Tuple							,      lowerCAmelCase_	:  Optional[Any]=13							,      lowerCAmelCase_	:  Dict=30							,      lowerCAmelCase_	:  Any=2							,      lowerCAmelCase_	:  List[Any]=3							,      lowerCAmelCase_	:  Optional[Any]=True							,      lowerCAmelCase_	:  int=True							,      lowerCAmelCase_	:  Optional[int]=32							,      lowerCAmelCase_	:  List[str]=5							,      lowerCAmelCase_	:  List[Any]=4							,      lowerCAmelCase_	:  Optional[Any]=37							,      lowerCAmelCase_	:  Any="gelu"							,      lowerCAmelCase_	:  Any=0.1							,      lowerCAmelCase_	:  List[str]=0.1							,      lowerCAmelCase_	:  int=10							,      lowerCAmelCase_	:  int=0.0_2							,      lowerCAmelCase_	:  int=3							,      lowerCAmelCase_	:  Dict=None							,      lowerCAmelCase_	:  Tuple=2							,      )      ->							Optional[int]:


														"""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        __lowerCAmelCase      (  self	:  str )      ->							Union[str, 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        __lowerCAmelCase      (  self	:  Tuple )      ->							Any:


														"""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=lowerCAmelCase_							,      initializer_range=self.initializer_range							,      encoder_stride=self.encoder_stride							,      )







							def        __lowerCAmelCase      (  self	:  Optional[Any]							,      lowerCAmelCase_	:  List[str]							,      lowerCAmelCase_	:  Any							,      lowerCAmelCase_	:  Optional[Any] )      ->							Any:


														"""simple docstring"""


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







							def        __lowerCAmelCase      (  self	:  Any							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  Optional[int]							,      lowerCAmelCase_	:  Union[str, Any] )      ->							List[Any]:


														"""simple docstring"""


														_a     					=    DeiTForMaskedImageModeling(config=lowerCAmelCase_ )
														model.to(lowerCAmelCase_ )
														model.eval()
														_a     					=    model(lowerCAmelCase_ )
														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(lowerCAmelCase_ )
														model.to(lowerCAmelCase_ )
														model.eval()

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







							def        __lowerCAmelCase      (  self	:  Tuple							,      lowerCAmelCase_	:  Optional[int]							,      lowerCAmelCase_	:  Union[str, Any]							,      lowerCAmelCase_	:  Union[str, Any] )      ->							Optional[int]:


														"""simple docstring"""


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

														# test greyscale images
														_a     					=    1
														_a     					=    DeiTForImageClassification(lowerCAmelCase_ )
														model.to(lowerCAmelCase_ )
														model.eval()

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







							def        __lowerCAmelCase      (  self	:  Tuple )      ->							List[Any]:


														"""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       A   (		_a					,_a					,unittest.TestCase   ):
							lowercase_          =    (
							    (
							        DeiTModel,
							        DeiTForImageClassification,
							        DeiTForImageClassificationWithTeacher,
							        DeiTForMaskedImageModeling,
							    )
							    if is_torch_available()
							    else ()
							)
							lowercase_          =    (
							    {
							        'feature-extraction': DeiTModel,
							        'image-classification': (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
							    }
							    if is_torch_available()
							    else {}
							)

							lowercase_          =    False
							lowercase_          =    False
							lowercase_          =    False







							def        __lowerCAmelCase      (  self	:  Any )      ->							List[Any]:


														"""simple docstring"""


														_a     					=    DeiTModelTester(self )
														_a     					=    ConfigTester(self							,      config_class=lowerCAmelCase_							,      has_text_modality=lowerCAmelCase_							,      hidden_size=37 )







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


														"""simple docstring"""


														self.config_tester.run_common_tests()







							@unittest.skip(reason='''DeiT does not use inputs_embeds''' )
							def        __lowerCAmelCase      (  self	:  Any )      ->							List[Any]:


														"""simple docstring"""


														pass







							def        __lowerCAmelCase      (  self	:  str )      ->							Any:


														"""simple docstring"""


														_a					,							_a     					=    self.model_tester.prepare_config_and_inputs_for_common()

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







							def        __lowerCAmelCase      (  self	:  List[str] )      ->							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(lowerCAmelCase_ )
																					_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]							,      lowerCAmelCase_ )







							def        __lowerCAmelCase      (  self	:  int )      ->							Dict:


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*lowerCAmelCase_ )







							def        __lowerCAmelCase      (  self	:  int )      ->							str:


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase_ )







							def        __lowerCAmelCase      (  self	:  int )      ->							List[str]:


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase_ )







							def        __lowerCAmelCase      (  self	:  List[Any]							,      lowerCAmelCase_	:  Any							,      lowerCAmelCase_	:  Tuple							,      lowerCAmelCase_	:  List[Any]=False )      ->							Union[str, Any]:


														"""simple docstring"""


														_a     					=    super()._prepare_for_class(lowerCAmelCase_							,      lowerCAmelCase_							,      return_labels=lowerCAmelCase_ )

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

														return inputs_dict







							def        __lowerCAmelCase      (  self	:  Dict )      ->							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(lowerCAmelCase_ )
																					    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
																					):
																												continue
																					_a     					=    model_class(lowerCAmelCase_ )
																					model.to(lowerCAmelCase_ )
																					model.train()
																					_a     					=    self._prepare_for_class(lowerCAmelCase_							,      lowerCAmelCase_							,      return_labels=lowerCAmelCase_ )
																					_a     					=    model(**lowerCAmelCase_ ).loss
																					loss.backward()







							def        __lowerCAmelCase      (  self	:  int )      ->							Optional[int]:


														"""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(lowerCAmelCase_ ) or not model_class.supports_gradient_checkpointing:
																												continue
																					# DeiTForImageClassificationWithTeacher supports inference-only
																					if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
																												continue
																					_a     					=    model_class(lowerCAmelCase_ )
																					model.gradient_checkpointing_enable()
																					model.to(lowerCAmelCase_ )
																					model.train()
																					_a     					=    self._prepare_for_class(lowerCAmelCase_							,      lowerCAmelCase_							,      return_labels=lowerCAmelCase_ )
																					_a     					=    model(**lowerCAmelCase_ ).loss
																					loss.backward()







							def        __lowerCAmelCase      (  self	:  Tuple )      ->							Union[str, Any]:


														"""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(lowerCAmelCase_ ),
																					        *get_values(lowerCAmelCase_ ),
																					    ]
																					    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(lowerCAmelCase_ )
																																			model.to(lowerCAmelCase_ )
																																			model.train()

																																			_a     					=    self._prepare_for_class(lowerCAmelCase_							,      lowerCAmelCase_							,      return_labels=lowerCAmelCase_ )

																																			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=lowerCAmelCase_ ) as warning_list:
																																										_a     					=    model(**lowerCAmelCase_ ).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        __lowerCAmelCase      (  self	:  str )      ->							List[str]:


														"""simple docstring"""


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



def 		snake_case_   ():





							'''simple docstring'''


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

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







							@cached_property
							def        __lowerCAmelCase      (  self	:  List[Any] )      ->							Dict:


														"""simple docstring"""


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







							@slow
							def        __lowerCAmelCase      (  self	:  Union[str, Any] )      ->							Optional[Any]:


														"""simple docstring"""


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

														_a     					=    self.default_image_processor
														_a     					=    prepare_img()
														_a     					=    image_processor(images=lowerCAmelCase_							,      return_tensors='''pt''' ).to(lowerCAmelCase_ )

														# forward pass
														with torch.no_grad():
																					_a     					=    model(**lowerCAmelCase_ )

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

														_a     					=    torch.tensor([-1.0_2_6_6, 0.1_9_1_2, -1.2_8_6_1] ).to(lowerCAmelCase_ )

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







							@slow
							@require_accelerate
							@require_torch_gpu
							def        __lowerCAmelCase      (  self	:  Optional[int] )      ->							Union[str, 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=lowerCAmelCase_							,      return_tensors='''pt''' )
														_a     					=    inputs.pixel_values.to(lowerCAmelCase_ )

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







'''simple docstring'''


from .configuration_bert_masked import MaskedBertConfig
from .modeling_bert_masked import (
    MaskedBertForMultipleChoice,
    MaskedBertForQuestionAnswering,
    MaskedBertForSequenceClassification,
    MaskedBertForTokenClassification,
    MaskedBertModel,
)
from .modules import *

