





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



					'''simple docstring'''

					def __init__(							self						:   Any						,lowerCamelCase__						:   List[str]						,lowerCamelCase__						:   List[Any]=13						,lowerCamelCase__						:   Optional[Any]=30						,lowerCamelCase__						:   Optional[int]=2						,lowerCamelCase__						:   List[str]=3						,lowerCamelCase__						:   Dict=True						,lowerCamelCase__						:   Union[str, Any]=True						,lowerCamelCase__						:   Dict=32						,lowerCamelCase__						:   Optional[int]=5						,lowerCamelCase__						:   List[str]=4						,lowerCamelCase__						:   Any=37						,lowerCamelCase__						:   int="gelu"						,lowerCamelCase__						:   List[Any]=0.1						,lowerCamelCase__						:   List[str]=0.1						,lowerCamelCase__						:   Dict=10						,lowerCamelCase__						:   Optional[Any]=0.02						,lowerCamelCase__						:   Union[str, Any]=3						,lowerCamelCase__						:   Optional[Any]=None						,lowerCamelCase__						:   Dict=2						,)					->		int:




											'''simple docstring'''


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

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






					def     SCREAMING_SNAKE_CASE__  (							self						:   List[str]	)					->		str:




											'''simple docstring'''


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

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

											SCREAMING_SNAKE_CASE		=						self.get_config()

											return config, pixel_values, labels






					def     SCREAMING_SNAKE_CASE__  (							self						:   Union[str, Any]	)					->		int:




											'''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     SCREAMING_SNAKE_CASE__  (							self						:   int						,lowerCamelCase__						:   Union[str, Any]						,lowerCamelCase__						:   Optional[Any]						,lowerCamelCase__						:   Dict	)					->		List[Any]:




											'''simple docstring'''


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






					def     SCREAMING_SNAKE_CASE__  (							self						:   str						,lowerCamelCase__						:   int						,lowerCamelCase__						:   List[str]						,lowerCamelCase__						:   Dict	)					->		List[str]:




											'''simple docstring'''


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

											# test greyscale images
											SCREAMING_SNAKE_CASE		=						1
											SCREAMING_SNAKE_CASE		=						DeiTForMaskedImageModeling(lowerCamelCase__	)
											model.to(lowerCamelCase__	)
											model.eval()

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






					def     SCREAMING_SNAKE_CASE__  (							self						:   Union[str, Any]						,lowerCamelCase__						:   int						,lowerCamelCase__						:   List[str]						,lowerCamelCase__						:   List[str]	)					->		List[Any]:




											'''simple docstring'''


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

											# test greyscale images
											SCREAMING_SNAKE_CASE		=						1
											SCREAMING_SNAKE_CASE		=						DeiTForImageClassification(lowerCamelCase__	)
											model.to(lowerCamelCase__	)
											model.eval()

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






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




											'''simple docstring'''


											SCREAMING_SNAKE_CASE		=						self.prepare_config_and_inputs()
											(
											    (
											    SCREAMING_SNAKE_CASE 
											), (
											    SCREAMING_SNAKE_CASE 
											), (
											    SCREAMING_SNAKE_CASE 
											),  
											)		=						config_and_inputs
											SCREAMING_SNAKE_CASE		=						{"""pixel_values""": pixel_values}
											return config, inputs_dict

@require_torch
class 		UpperCamelCase__			(				lowerCAmelCase_							,				lowerCAmelCase_							,				unittest.TestCase       ):



					'''simple docstring'''

					__snake_case   :     int		     =      (
					    (
					        DeiTModel,
					        DeiTForImageClassification,
					        DeiTForImageClassificationWithTeacher,
					        DeiTForMaskedImageModeling,
					    )
					    if is_torch_available()
					    else ()
					)
					__snake_case   :     Union[str, Any]		     =      (
					    {
					        "feature-extraction": DeiTModel,
					        "image-classification": (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
					    }
					    if is_torch_available()
					    else {}
					)

					__snake_case   :     str		     =      False
					__snake_case   :     str		     =      False
					__snake_case   :     Optional[int]		     =      False






					def     SCREAMING_SNAKE_CASE__  (							self						:   Union[str, Any]	)					->		Optional[Any]:




											'''simple docstring'''


											SCREAMING_SNAKE_CASE		=						DeiTModelTester(self	)
											SCREAMING_SNAKE_CASE		=						ConfigTester(self						,config_class=lowerCamelCase__						,has_text_modality=lowerCamelCase__						,hidden_size=37	)






					def     SCREAMING_SNAKE_CASE__  (							self						:   Union[str, Any]	)					->		int:




											'''simple docstring'''


											self.config_tester.run_common_tests()






					@unittest.skip(reason="""DeiT does not use inputs_embeds"""	)
					def     SCREAMING_SNAKE_CASE__  (							self						:   Optional[int]	)					->		List[str]:




											'''simple docstring'''


											pass






					def     SCREAMING_SNAKE_CASE__  (							self						:   Any	)					->		Any:




											'''simple docstring'''


											SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE		=						self.model_tester.prepare_config_and_inputs_for_common()

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






					def     SCREAMING_SNAKE_CASE__  (							self						:   Tuple	)					->		Tuple:




											'''simple docstring'''


											SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE		=						self.model_tester.prepare_config_and_inputs_for_common()

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

																	SCREAMING_SNAKE_CASE		=						["""pixel_values"""]
																	self.assertListEqual(arg_names[:1]						,lowerCamelCase__	)






					def     SCREAMING_SNAKE_CASE__  (							self						:   int	)					->		Union[str, Any]:




											'''simple docstring'''


											SCREAMING_SNAKE_CASE		=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*lowerCamelCase__	)






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




											'''simple docstring'''


											SCREAMING_SNAKE_CASE		=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase__	)






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




											'''simple docstring'''


											SCREAMING_SNAKE_CASE		=						self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__	)






					def     SCREAMING_SNAKE_CASE__  (							self						:   List[str]						,lowerCamelCase__						:   Union[str, Any]						,lowerCamelCase__						:   int						,lowerCamelCase__						:   Any=False	)					->		int:




											'''simple docstring'''


											SCREAMING_SNAKE_CASE		=						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     SCREAMING_SNAKE_CASE__  (							self						:   Optional[int]	)					->		Optional[Any]:




											'''simple docstring'''


											if not self.model_tester.is_training:
																	return

											SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE		=						self.model_tester.prepare_config_and_inputs_for_common()
											SCREAMING_SNAKE_CASE		=						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
																	SCREAMING_SNAKE_CASE		=						model_class(lowerCamelCase__	)
																	model.to(lowerCamelCase__	)
																	model.train()
																	SCREAMING_SNAKE_CASE		=						self._prepare_for_class(lowerCamelCase__						,lowerCamelCase__						,return_labels=lowerCamelCase__	)
																	SCREAMING_SNAKE_CASE		=						model(**lowerCamelCase__	).loss
																	loss.backward()






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




											'''simple docstring'''


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

											SCREAMING_SNAKE_CASE		=						False
											SCREAMING_SNAKE_CASE		=						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
																	SCREAMING_SNAKE_CASE		=						model_class(lowerCamelCase__	)
																	model.gradient_checkpointing_enable()
																	model.to(lowerCamelCase__	)
																	model.train()
																	SCREAMING_SNAKE_CASE		=						self._prepare_for_class(lowerCamelCase__						,lowerCamelCase__						,return_labels=lowerCamelCase__	)
																	SCREAMING_SNAKE_CASE		=						model(**lowerCamelCase__	).loss
																	loss.backward()






					def     SCREAMING_SNAKE_CASE__  (							self						:   Union[str, Any]	)					->		Optional[Any]:




											'''simple docstring'''


											SCREAMING_SNAKE_CASE, SCREAMING_SNAKE_CASE		=						self.model_tester.prepare_config_and_inputs_for_common()

											SCREAMING_SNAKE_CASE		=						[
											    {"""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']}"""	):
																													SCREAMING_SNAKE_CASE		=						problem_type["""title"""]
																													SCREAMING_SNAKE_CASE		=						problem_type["""num_labels"""]

																													SCREAMING_SNAKE_CASE		=						model_class(lowerCamelCase__	)
																													model.to(lowerCamelCase__	)
																													model.train()

																													SCREAMING_SNAKE_CASE		=						self._prepare_for_class(lowerCamelCase__						,lowerCamelCase__						,return_labels=lowerCamelCase__	)

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

																													SCREAMING_SNAKE_CASE		=						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:
																																			SCREAMING_SNAKE_CASE		=						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     SCREAMING_SNAKE_CASE__  (							self						:   List[Any]	)					->		Tuple:




											'''simple docstring'''


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



def        __lowercase       (				)			->       Optional[int]:


						'''simple docstring'''


						SCREAMING_SNAKE_CASE		=						Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""     )
						return image



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



					'''simple docstring'''

					@cached_property
					def     SCREAMING_SNAKE_CASE__  (							self						:   Tuple	)					->		int:




											'''simple docstring'''


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






					@slow
					def     SCREAMING_SNAKE_CASE__  (							self						:   Optional[Any]	)					->		List[Any]:




											'''simple docstring'''


											SCREAMING_SNAKE_CASE		=						DeiTForImageClassificationWithTeacher.from_pretrained("""facebook/deit-base-distilled-patch16-224"""	).to(
											    lowerCamelCase__	)

											SCREAMING_SNAKE_CASE		=						self.default_image_processor
											SCREAMING_SNAKE_CASE		=						prepare_img()
											SCREAMING_SNAKE_CASE		=						image_processor(images=lowerCamelCase__						,return_tensors="""pt"""	).to(lowerCamelCase__	)

											# forward pass
											with torch.no_grad():
																	SCREAMING_SNAKE_CASE		=						model(**lowerCamelCase__	)

											# verify the logits
											SCREAMING_SNAKE_CASE		=						torch.Size((1, 1000)	)
											self.assertEqual(outputs.logits.shape						,lowerCamelCase__	)

											SCREAMING_SNAKE_CASE		=						torch.tensor([-1.0266, 0.1912, -1.2861]	).to(lowerCamelCase__	)

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






					@slow
					@require_accelerate
					@require_torch_gpu
					def     SCREAMING_SNAKE_CASE__  (							self						:   Union[str, Any]	)					->		Any:




											'''simple docstring'''


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

											SCREAMING_SNAKE_CASE		=						prepare_img()
											SCREAMING_SNAKE_CASE		=						image_processor(images=lowerCamelCase__						,return_tensors="""pt"""	)
											SCREAMING_SNAKE_CASE		=						inputs.pixel_values.to(lowerCamelCase__	)

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









import json
import sys



def        __lowercase       (				_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE     )			->       Any:


      '''simple docstring'''


      with open(_SCREAMING_SNAKE_CASE			,						encoding="""utf-8"""     ) as f:
            SCREAMING_SNAKE_CASE		=						json.load(_SCREAMING_SNAKE_CASE     )

      SCREAMING_SNAKE_CASE		=						["""<details>""", """<summary>Show updated benchmarks!</summary>""", """ """]

      for benchmark_name in sorted(_SCREAMING_SNAKE_CASE     ):
            SCREAMING_SNAKE_CASE		=						results[benchmark_name]

            SCREAMING_SNAKE_CASE		=						benchmark_name.split("""/"""     )[-1]
            output_md.append(F"""### Benchmark: {benchmark_file_name}"""     )

            SCREAMING_SNAKE_CASE		=						"""| metric |"""
            SCREAMING_SNAKE_CASE		=						"""|--------|"""
            SCREAMING_SNAKE_CASE		=						"""| new / old (diff) |"""
            for metric_name in sorted(_SCREAMING_SNAKE_CASE     ):
                  SCREAMING_SNAKE_CASE		=						benchmark_res[metric_name]
                  SCREAMING_SNAKE_CASE		=						metric_vals["""new"""]
                  SCREAMING_SNAKE_CASE		=						metric_vals.get("""old"""			,						_SCREAMING_SNAKE_CASE     )
                  SCREAMING_SNAKE_CASE		=						metric_vals.get("""diff"""			,						_SCREAMING_SNAKE_CASE     )

                  SCREAMING_SNAKE_CASE		=						F""" {new_val:f}""" if isinstance(_SCREAMING_SNAKE_CASE			,						(int, float)     ) else """None"""

                  if old_val is not None:
                        val_str += F""" / {old_val:f}""" if isinstance(_SCREAMING_SNAKE_CASE			,						(int, float)     ) else "None"
                  if dif_val is not None:
                        val_str += F""" ({dif_val:f})""" if isinstance(_SCREAMING_SNAKE_CASE			,						(int, float)     ) else "None"

                  title += " " + metric_name + " |"
                  lines += "---|"
                  value += val_str + " |"

            output_md += [title, lines, value, " "]

      output_md.append("""</details>"""     )

      with open(_SCREAMING_SNAKE_CASE			,						"""w"""			,						encoding="""utf-8"""     ) as f:
            f.writelines("""\n""".join(_SCREAMING_SNAKE_CASE     )     )


if __name__ == "__main__":
      SCREAMING_SNAKE_CASE_						       =	sys.argv[1]
      SCREAMING_SNAKE_CASE_						       =	sys.argv[2]

      format_json_to_md(input_json_file, output_md_file)


