
'''simple docstring'''





import argparse
import json
from pathlib import Path

import requests
import torch
from huggingface_hub import cached_download, hf_hub_download, hf_hub_url
from PIL import Image

from transformers import DetaConfig, DetaForObjectDetection, DetaImageProcessor, SwinConfig
from transformers.utils import logging


logging.set_verbosity_info()
a__						:      Optional[int]   				=				logging.get_logger(__name__)





def 				snake_case							(						UpperCAmelCase		)->	Tuple:







      """simple docstring"""
      __A						=	SwinConfig(
          embed_dim=1_9_2					,     depths=(2, 2, 1_8, 2)					,     num_heads=(6, 1_2, 2_4, 4_8)					,     window_size=1_2					,     out_features=['stage2', 'stage3', 'stage4']					,     )

      __A						=	DetaConfig(
          backbone_config=SCREAMING_SNAKE_CASE_					,     num_queries=9_0_0					,     encoder_ffn_dim=2_0_4_8					,     decoder_ffn_dim=2_0_4_8					,     num_feature_levels=5					,     assign_first_stage=SCREAMING_SNAKE_CASE_					,     with_box_refine=SCREAMING_SNAKE_CASE_					,     two_stage=SCREAMING_SNAKE_CASE_					,     )

      # set labels
      __A						=	'huggingface/label-files'
      if "o365" in model_name:
            __A						=	3_6_6
            __A						=	'object365-id2label.json'
      else:
            __A						=	9_1
            __A						=	'coco-detection-id2label.json'

      __A						=	num_labels
      __A						=	json.load(open(cached_download(hf_hub_url(SCREAMING_SNAKE_CASE_					,     SCREAMING_SNAKE_CASE_					,     repo_type='dataset'		)		)					,     'r'		)		)
      __A						=	{int(SCREAMING_SNAKE_CASE_		): v for k, v in idalabel.items()}
      __A						=	idalabel
      __A						=	{v: k for k, v in idalabel.items()}

      return config





def 				snake_case							(						UpperCAmelCase		)->	List[Any]:







      """simple docstring"""
      __A						=	[]

      # stem
      # fmt: off
      rename_keys.append(('backbone.0.body.patch_embed.proj.weight', 'model.backbone.model.embeddings.patch_embeddings.projection.weight')		)
      rename_keys.append(('backbone.0.body.patch_embed.proj.bias', 'model.backbone.model.embeddings.patch_embeddings.projection.bias')		)
      rename_keys.append(('backbone.0.body.patch_embed.norm.weight', 'model.backbone.model.embeddings.norm.weight')		)
      rename_keys.append(('backbone.0.body.patch_embed.norm.bias', 'model.backbone.model.embeddings.norm.bias')		)
      # stages
      for i in range(len(config.backbone_config.depths		)		):
            for j in range(config.backbone_config.depths[i]		):
                  rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm1.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.weight')		)
                  rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm1.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_before.bias')		)
                  rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_bias_table', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table')		)
                  rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.relative_position_index', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index')		)
                  rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.proj.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight')		)
                  rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.attn.proj.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias')		)
                  rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm2.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.weight')		)
                  rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.norm2.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.layernorm_after.bias')		)
                  rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight')		)
                  rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc1.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias')		)
                  rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.weight', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.weight')		)
                  rename_keys.append((f'backbone.0.body.layers.{i}.blocks.{j}.mlp.fc2.bias', f'model.backbone.model.encoder.layers.{i}.blocks.{j}.output.dense.bias')		)

            if i < 3:
                  rename_keys.append((f'backbone.0.body.layers.{i}.downsample.reduction.weight', f'model.backbone.model.encoder.layers.{i}.downsample.reduction.weight')		)
                  rename_keys.append((f'backbone.0.body.layers.{i}.downsample.norm.weight', f'model.backbone.model.encoder.layers.{i}.downsample.norm.weight')		)
                  rename_keys.append((f'backbone.0.body.layers.{i}.downsample.norm.bias', f'model.backbone.model.encoder.layers.{i}.downsample.norm.bias')		)

      rename_keys.append(('backbone.0.body.norm1.weight', 'model.backbone.model.hidden_states_norms.stage2.weight')		)
      rename_keys.append(('backbone.0.body.norm1.bias', 'model.backbone.model.hidden_states_norms.stage2.bias')		)
      rename_keys.append(('backbone.0.body.norm2.weight', 'model.backbone.model.hidden_states_norms.stage3.weight')		)
      rename_keys.append(('backbone.0.body.norm2.bias', 'model.backbone.model.hidden_states_norms.stage3.bias')		)
      rename_keys.append(('backbone.0.body.norm3.weight', 'model.backbone.model.hidden_states_norms.stage4.weight')		)
      rename_keys.append(('backbone.0.body.norm3.bias', 'model.backbone.model.hidden_states_norms.stage4.bias')		)

      # transformer encoder
      for i in range(config.encoder_layers		):
            rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.sampling_offsets.weight', f'model.encoder.layers.{i}.self_attn.sampling_offsets.weight')		)
            rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.sampling_offsets.bias', f'model.encoder.layers.{i}.self_attn.sampling_offsets.bias')		)
            rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.attention_weights.weight', f'model.encoder.layers.{i}.self_attn.attention_weights.weight')		)
            rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.attention_weights.bias', f'model.encoder.layers.{i}.self_attn.attention_weights.bias')		)
            rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.value_proj.weight', f'model.encoder.layers.{i}.self_attn.value_proj.weight')		)
            rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.value_proj.bias', f'model.encoder.layers.{i}.self_attn.value_proj.bias')		)
            rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.output_proj.weight', f'model.encoder.layers.{i}.self_attn.output_proj.weight')		)
            rename_keys.append((f'transformer.encoder.layers.{i}.self_attn.output_proj.bias', f'model.encoder.layers.{i}.self_attn.output_proj.bias')		)
            rename_keys.append((f'transformer.encoder.layers.{i}.norm1.weight', f'model.encoder.layers.{i}.self_attn_layer_norm.weight')		)
            rename_keys.append((f'transformer.encoder.layers.{i}.norm1.bias', f'model.encoder.layers.{i}.self_attn_layer_norm.bias')		)
            rename_keys.append((f'transformer.encoder.layers.{i}.linear1.weight', f'model.encoder.layers.{i}.fc1.weight')		)
            rename_keys.append((f'transformer.encoder.layers.{i}.linear1.bias', f'model.encoder.layers.{i}.fc1.bias')		)
            rename_keys.append((f'transformer.encoder.layers.{i}.linear2.weight', f'model.encoder.layers.{i}.fc2.weight')		)
            rename_keys.append((f'transformer.encoder.layers.{i}.linear2.bias', f'model.encoder.layers.{i}.fc2.bias')		)
            rename_keys.append((f'transformer.encoder.layers.{i}.norm2.weight', f'model.encoder.layers.{i}.final_layer_norm.weight')		)
            rename_keys.append((f'transformer.encoder.layers.{i}.norm2.bias', f'model.encoder.layers.{i}.final_layer_norm.bias')		)

      # transformer decoder
      for i in range(config.decoder_layers		):
            rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.sampling_offsets.weight', f'model.decoder.layers.{i}.encoder_attn.sampling_offsets.weight')		)
            rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.sampling_offsets.bias', f'model.decoder.layers.{i}.encoder_attn.sampling_offsets.bias')		)
            rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.attention_weights.weight', f'model.decoder.layers.{i}.encoder_attn.attention_weights.weight')		)
            rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.attention_weights.bias', f'model.decoder.layers.{i}.encoder_attn.attention_weights.bias')		)
            rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.value_proj.weight', f'model.decoder.layers.{i}.encoder_attn.value_proj.weight')		)
            rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.value_proj.bias', f'model.decoder.layers.{i}.encoder_attn.value_proj.bias')		)
            rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.output_proj.weight', f'model.decoder.layers.{i}.encoder_attn.output_proj.weight')		)
            rename_keys.append((f'transformer.decoder.layers.{i}.cross_attn.output_proj.bias', f'model.decoder.layers.{i}.encoder_attn.output_proj.bias')		)
            rename_keys.append((f'transformer.decoder.layers.{i}.norm1.weight', f'model.decoder.layers.{i}.encoder_attn_layer_norm.weight')		)
            rename_keys.append((f'transformer.decoder.layers.{i}.norm1.bias', f'model.decoder.layers.{i}.encoder_attn_layer_norm.bias')		)
            rename_keys.append((f'transformer.decoder.layers.{i}.self_attn.out_proj.weight', f'model.decoder.layers.{i}.self_attn.out_proj.weight')		)
            rename_keys.append((f'transformer.decoder.layers.{i}.self_attn.out_proj.bias', f'model.decoder.layers.{i}.self_attn.out_proj.bias')		)
            rename_keys.append((f'transformer.decoder.layers.{i}.norm2.weight', f'model.decoder.layers.{i}.self_attn_layer_norm.weight')		)
            rename_keys.append((f'transformer.decoder.layers.{i}.norm2.bias', f'model.decoder.layers.{i}.self_attn_layer_norm.bias')		)
            rename_keys.append((f'transformer.decoder.layers.{i}.linear1.weight', f'model.decoder.layers.{i}.fc1.weight')		)
            rename_keys.append((f'transformer.decoder.layers.{i}.linear1.bias', f'model.decoder.layers.{i}.fc1.bias')		)
            rename_keys.append((f'transformer.decoder.layers.{i}.linear2.weight', f'model.decoder.layers.{i}.fc2.weight')		)
            rename_keys.append((f'transformer.decoder.layers.{i}.linear2.bias', f'model.decoder.layers.{i}.fc2.bias')		)
            rename_keys.append((f'transformer.decoder.layers.{i}.norm3.weight', f'model.decoder.layers.{i}.final_layer_norm.weight')		)
            rename_keys.append((f'transformer.decoder.layers.{i}.norm3.bias', f'model.decoder.layers.{i}.final_layer_norm.bias')		)

      # fmt: on

      return rename_keys





def 				snake_case							(						UpperCAmelCase					,     UpperCAmelCase					,     UpperCAmelCase		)->	Dict:







      """simple docstring"""
      __A						=	dct.pop(SCREAMING_SNAKE_CASE_		)
      __A						=	val





def 				snake_case							(						UpperCAmelCase					,     UpperCAmelCase		)->	Optional[Any]:







      """simple docstring"""
      __A						=	[int(backbone_config.embed_dim * 2**i		) for i in range(len(backbone_config.depths		)		)]
      for i in range(len(backbone_config.depths		)		):
            __A						=	num_features[i]
            for j in range(backbone_config.depths[i]		):
                  # fmt: off
                  # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias)
                  __A						=	state_dict.pop(f'backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.weight'		)
                  __A						=	state_dict.pop(f'backbone.0.body.layers.{i}.blocks.{j}.attn.qkv.bias'		)
                  # next, add query, keys and values (in that order) to the state dict
                  __A						=	in_proj_weight[:dim, :]
                  __A						=	in_proj_bias[: dim]
                  __A						=	in_proj_weight[
                      dim : dim * 2, :
                  ]
                  __A						=	in_proj_bias[
                      dim : dim * 2
                  ]
                  __A						=	in_proj_weight[
                      -dim :, :
                  ]
                  __A						=	in_proj_bias[-dim :]
                  # fmt: on





def 				snake_case							(						UpperCAmelCase					,     UpperCAmelCase		)->	Optional[int]:







      """simple docstring"""
      # transformer decoder self-attention layers
      __A						=	config.d_model
      for i in range(config.decoder_layers		):
            # read in weights + bias of input projection layer of self-attention
            __A						=	state_dict.pop(f'transformer.decoder.layers.{i}.self_attn.in_proj_weight'		)
            __A						=	state_dict.pop(f'transformer.decoder.layers.{i}.self_attn.in_proj_bias'		)
            # next, add query, keys and values (in that order) to the state dict
            __A						=	in_proj_weight[:hidden_size, :]
            __A						=	in_proj_bias[:hidden_size]
            __A						=	in_proj_weight[
                hidden_size : hidden_size * 2, :
            ]
            __A						=	in_proj_bias[hidden_size : hidden_size * 2]
            __A						=	in_proj_weight[-hidden_size:, :]
            __A						=	in_proj_bias[-hidden_size:]





def 				snake_case							(						)->	Optional[int]:







      """simple docstring"""
      __A						=	'http://images.cocodataset.org/val2017/000000039769.jpg'
      __A						=	Image.open(requests.get(SCREAMING_SNAKE_CASE_					,     stream=SCREAMING_SNAKE_CASE_		).raw		)

      return im





@torch.no_grad()
def 				snake_case							(						UpperCAmelCase					,     UpperCAmelCase					,     UpperCAmelCase		)->	Any:







      """simple docstring"""
      __A						=	get_deta_config(SCREAMING_SNAKE_CASE_		)

      # load original state dict
      if model_name == "deta-swin-large":
            __A						=	hf_hub_download(repo_id='nielsr/deta-checkpoints'					,     filename='adet_swin_ft.pth'		)
      elif model_name == "deta-swin-large-o365":
            __A						=	hf_hub_download(repo_id='jozhang97/deta-swin-l-o365'					,     filename='deta_swin_pt_o365.pth'		)
      else:
            raise ValueError(f'Model name {model_name} not supported'		)

      __A						=	torch.load(SCREAMING_SNAKE_CASE_					,     map_location='cpu'		)['model']

      # original state dict
      for name, param in state_dict.items():
            print(SCREAMING_SNAKE_CASE_					,     param.shape		)

      # rename keys
      __A						=	create_rename_keys(SCREAMING_SNAKE_CASE_		)
      for src, dest in rename_keys:
            rename_key(SCREAMING_SNAKE_CASE_					,     SCREAMING_SNAKE_CASE_					,     SCREAMING_SNAKE_CASE_		)
      read_in_swin_q_k_v(SCREAMING_SNAKE_CASE_					,     config.backbone_config		)
      read_in_decoder_q_k_v(SCREAMING_SNAKE_CASE_					,     SCREAMING_SNAKE_CASE_		)

      # fix some prefixes
      for key in state_dict.copy().keys():
            if "transformer.decoder.class_embed" in key or "transformer.decoder.bbox_embed" in key:
                  __A						=	state_dict.pop(SCREAMING_SNAKE_CASE_		)
                  __A						=	val
            if "input_proj" in key:
                  __A						=	state_dict.pop(SCREAMING_SNAKE_CASE_		)
                  __A						=	val
            if "level_embed" in key or "pos_trans" in key or "pix_trans" in key or "enc_output" in key:
                  __A						=	state_dict.pop(SCREAMING_SNAKE_CASE_		)
                  __A						=	val

    # finally, create HuggingFace model and load state dict
      __A						=	DetaForObjectDetection(SCREAMING_SNAKE_CASE_		)
      model.load_state_dict(SCREAMING_SNAKE_CASE_		)
      model.eval()

      __A						=	'cuda' if torch.cuda.is_available() else 'cpu'
      model.to(SCREAMING_SNAKE_CASE_		)

      # load image processor
      __A						=	DetaImageProcessor(format='coco_detection'		)

      # verify our conversion on image
      __A						=	prepare_img()
      __A						=	processor(images=SCREAMING_SNAKE_CASE_					,     return_tensors='pt'		)
      __A						=	encoding['pixel_values']
      __A						=	model(pixel_values.to(SCREAMING_SNAKE_CASE_		)		)

      # verify logits
      print('Logits:'					,     outputs.logits[0, :3, :3]		)
      print('Boxes:'					,     outputs.pred_boxes[0, :3, :3]		)
      if model_name == "deta-swin-large":
            __A						=	torch.tensor(
                [[-7.6308, -2.8485, -5.3737], [-7.2037, -4.5505, -4.8027], [-7.2943, -4.2611, -4.6617]]		)
            __A						=	torch.tensor([[0.4987, 0.4969, 0.9999], [0.2549, 0.5498, 0.4805], [0.5498, 0.2757, 0.0569]]		)
      elif model_name == "deta-swin-large-o365":
            __A						=	torch.tensor(
                [[-8.0122, -3.5720, -4.9717], [-8.1547, -3.6886, -4.6389], [-7.6610, -3.6194, -5.0134]]		)
            __A						=	torch.tensor([[0.2523, 0.5549, 0.4881], [0.7715, 0.4149, 0.4601], [0.5503, 0.2753, 0.0575]]		)
      assert torch.allclose(outputs.logits[0, :3, :3]					,     expected_logits.to(SCREAMING_SNAKE_CASE_		)					,     atol=1e-4		)
      assert torch.allclose(outputs.pred_boxes[0, :3, :3]					,     expected_boxes.to(SCREAMING_SNAKE_CASE_		)					,     atol=1e-4		)
      print('Everything ok!'		)

      if pytorch_dump_folder_path:
            # Save model and processor
            logger.info(f'Saving PyTorch model and processor to {pytorch_dump_folder_path}...'		)
            Path(SCREAMING_SNAKE_CASE_		).mkdir(exist_ok=SCREAMING_SNAKE_CASE_		)
            model.save_pretrained(SCREAMING_SNAKE_CASE_		)
            processor.save_pretrained(SCREAMING_SNAKE_CASE_		)

      # Push to hub
      if push_to_hub:
            print('Pushing model and processor to hub...'		)
            model.push_to_hub(f'jozhang97/{model_name}'		)
            processor.push_to_hub(f'jozhang97/{model_name}'		)


if __name__ == "__main__":
 a__						:      int   				=				argparse.ArgumentParser()

 parser.add_argument(
     "--model_name",
     type=str,
     default="deta-swin-large",
     choices=["deta-swin-large", "deta-swin-large-o365"],
     help="Name of the model you'd like to convert.",
 )
 parser.add_argument(
     "--pytorch_dump_folder_path",
     default=None,
     type=str,
     help="Path to the folder to output PyTorch model.",
 )
 parser.add_argument(
     "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub."
 )
 a__						:      str   				=				parser.parse_args()
 convert_deta_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)












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		,			_A		,			_A=1_3		,			_A=3_0		,			_A=2		,			_A=3		,			_A=True		,			_A=True		,			_A=3_2		,			_A=5		,			_A=4		,			_A=3_7		,			_A="gelu"		,			_A=0.1		,			_A=0.1		,			_A=1_0		,			_A=0.02		,			_A=3		,			_A=None		,			_A=2		,			):




											"""simple docstring"""



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

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






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



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

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

											__lowerCAmelCase				   =       self.get_config()

											return config, pixel_values, labels






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											return DeiTConfig(
											    image_size=self.image_size		,			patch_size=self.patch_size		,			num_channels=self.num_channels		,			hidden_size=self.hidden_size		,			num_hidden_layers=self.num_hidden_layers		,			num_attention_heads=self.num_attention_heads		,			intermediate_size=self.intermediate_size		,			hidden_act=self.hidden_act		,			hidden_dropout_prob=self.hidden_dropout_prob		,			attention_probs_dropout_prob=self.attention_probs_dropout_prob		,			is_decoder=_A		,			initializer_range=self.initializer_range		,			encoder_stride=self.encoder_stride		,			)






						def 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A		,			_A			):




											"""simple docstring"""



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






						def 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A		,			_A			):




											"""simple docstring"""



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

											# test greyscale images
											__lowerCAmelCase				   =       1
											__lowerCAmelCase				   =       DeiTForMaskedImageModeling(_A			)
											model.to(_A			)
											model.eval()

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






						def 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A		,			_A			):




											"""simple docstring"""



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

											# test greyscale images
											__lowerCAmelCase				   =       1
											__lowerCAmelCase				   =       DeiTForImageClassification(_A			)
											model.to(_A			)
											model.eval()

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






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



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






@require_torch
class 			a__    (					snake_case__ ,				snake_case__ ,				unittest.TestCase      ):
						_a							:    Optional[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							:    Optional[Any]					=    False
						_a							:    Tuple					=    False
						_a							:    Tuple					=    False






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase				   =       DeiTModelTester(self			)
											__lowerCAmelCase				   =       ConfigTester(self		,			config_class=_A		,			has_text_modality=_A		,			hidden_size=3_7			)






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											self.config_tester.run_common_tests()






						@unittest.skip(reason="DeiT does not use inputs_embeds"			)
						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											pass






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase   ,							__lowerCAmelCase				   =       self.model_tester.prepare_config_and_inputs_for_common()

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






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase   ,							__lowerCAmelCase				   =       self.model_tester.prepare_config_and_inputs_for_common()

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

																__lowerCAmelCase				   =       ["pixel_values"]
																self.assertListEqual(arg_names[:1]		,			_A			)






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase				   =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_A			)






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase				   =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_image_modeling(*_A			)






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase				   =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*_A			)






						def 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A		,			_A=False			):




											"""simple docstring"""



											__lowerCAmelCase				   =       super()._prepare_for_class(_A		,			_A		,			return_labels=_A			)

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

											return inputs_dict






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											if not self.model_tester.is_training:
																return

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

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






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



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

											__lowerCAmelCase				   =       False
											__lowerCAmelCase				   =       True

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






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase   ,							__lowerCAmelCase				   =       self.model_tester.prepare_config_and_inputs_for_common()

											__lowerCAmelCase				   =       [
											    {"title": "multi_label_classification", "num_labels": 2, "dtype": torch.float},
											    {"title": "single_label_classification", "num_labels": 1, "dtype": torch.long},
											    {"title": "regression", "num_labels": 1, "dtype": torch.float},
											]

											for model_class in self.all_model_classes:
																if (
																    model_class
																    not in [
																        *get_values(_A			),
																        *get_values(_A			),
																    ]
																    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
																):
																					continue

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

																										__lowerCAmelCase				   =       model_class(_A			)
																										model.to(_A			)
																										model.train()

																										__lowerCAmelCase				   =       self._prepare_for_class(_A		,			_A		,			return_labels=_A			)

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

																										__lowerCAmelCase				   =       inputs["labels"].to(problem_type["dtype"]			)

																										# This tests that we do not trigger the warning form PyTorch "Using a target size that is different
																										# to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
																										# they have the same size." which is a symptom something in wrong for the regression problem.
																										# See https://github.com/huggingface/transformers/issues/11780
																										with warnings.catch_warnings(record=_A			) as warning_list:
																															__lowerCAmelCase				   =       model(**_A			).loss
																										for w in warning_list:
																															if "Using a target size that is different to the input size" in str(w.message			):
																																				raise ValueError(
																																				    f"""Something is going wrong in the regression problem: intercepted {w.message}"""			)

																										loss.backward()






						@slow
						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



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







def 	_a      (						):
					__lowerCAmelCase				   =       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			):




											"""simple docstring"""



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






						@slow
						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



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

											__lowerCAmelCase				   =       self.default_image_processor
											__lowerCAmelCase				   =       prepare_img()
											__lowerCAmelCase				   =       image_processor(images=_A		,			return_tensors="pt"			).to(_A			)

											# forward pass
											with torch.no_grad():
																__lowerCAmelCase				   =       model(**_A			)

											# verify the logits
											__lowerCAmelCase				   =       torch.Size((1, 1_0_0_0)			)
											self.assertEqual(outputs.logits.shape		,			_A			)

											__lowerCAmelCase				   =       torch.tensor([-1.02_66, 0.19_12, -1.28_61]			).to(_A			)

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






						@slow
						@require_accelerate
						@require_torch_gpu
						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



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

											__lowerCAmelCase				   =       prepare_img()
											__lowerCAmelCase				   =       image_processor(images=_A		,			return_tensors="pt"			)
											__lowerCAmelCase				   =       inputs.pixel_values.to(_A			)

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




