





from __future__ import annotations

import unittest

from transformers import DebertaConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
			import tensorflow as tf

			from transformers import (
			    TFDebertaForMaskedLM,
			    TFDebertaForQuestionAnswering,
			    TFDebertaForSequenceClassification,
			    TFDebertaForTokenClassification,
			    TFDebertaModel,
			)


class 			__lowerCAmelCase				:


	'''simple docstring'''





	def __init__(	self:	Dict,				lowerCamelCase_:	List[Any],				lowerCamelCase_:	Tuple=13,				lowerCamelCase_:	int=7,				lowerCamelCase_:	int=True,				lowerCamelCase_:	int=True,				lowerCamelCase_:	Optional[int]=True,				lowerCamelCase_:	Any=True,				lowerCamelCase_:	Union[str, Any]=99,				lowerCamelCase_:	int=32,				lowerCamelCase_:	str=2,				lowerCamelCase_:	Optional[Any]=4,				lowerCamelCase_:	Dict=37,				lowerCamelCase_:	List[str]="gelu",				lowerCamelCase_:	Union[str, Any]=0.1,				lowerCamelCase_:	Optional[int]=0.1,				lowerCamelCase_:	Union[str, Any]=512,				lowerCamelCase_:	Dict=16,				lowerCamelCase_:	Any=2,				lowerCamelCase_:	List[str]=0.0_2,				lowerCamelCase_:	int=3,				lowerCamelCase_:	int=4,				lowerCamelCase_:	Tuple=None,				):
			lowercase__				:			Dict            =		parent
			lowercase__				:			List[str]            =		13
			lowercase__				:			Union[str, Any]            =		7
			lowercase__				:			Tuple            =		True
			lowercase__				:			Tuple            =		True
			lowercase__				:			Optional[Any]            =		True
			lowercase__				:			List[str]            =		True
			lowercase__				:			Dict            =		99
			lowercase__				:			Optional[Any]            =		32
			lowercase__				:			str            =		2
			lowercase__				:			Any            =		4
			lowercase__				:			Tuple            =		37
			lowercase__				:			Any            =		'''gelu'''
			lowercase__				:			Dict            =		0.1
			lowercase__				:			Any            =		0.1
			lowercase__				:			List[str]            =		512
			lowercase__				:			int            =		16
			lowercase__				:			Union[str, Any]            =		False
			lowercase__				:			Tuple            =		-1
			lowercase__				:			Optional[Any]            =		True
			lowercase__				:			List[str]            =		2
			lowercase__				:			Any            =		0.0_2
			lowercase__				:			Dict            =		3
			lowercase__				:			List[str]            =		4
			lowercase__				:			str            =		None

	def       snake_case__(	self:	Tuple ):
			lowercase__				:			Any            =		ids_tensor([self.batch_size, self.seq_length],				self.vocab_size )

			lowercase__				:			Optional[Any]            =		None
			if self.use_input_mask:
					lowercase__				:			Union[str, Any]            =		random_attention_mask([self.batch_size, self.seq_length] )

			lowercase__				:			Tuple            =		None
			if self.use_token_type_ids:
					lowercase__				:			List[Any]            =		ids_tensor([self.batch_size, self.seq_length],				self.type_vocab_size )

			lowercase__				:			Dict            =		None
			lowercase__				:			Tuple            =		None
			lowercase__				:			int            =		None
			if self.use_labels:
					lowercase__				:			str            =		ids_tensor([self.batch_size],				self.type_sequence_label_size )
					lowercase__				:			Optional[Any]            =		ids_tensor([self.batch_size, self.seq_length],				self.num_labels )

			lowercase__				:			str            =		DebertaConfig(
			    vocab_size=self.vocab_size,				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,				max_position_embeddings=self.max_position_embeddings,				type_vocab_size=self.type_vocab_size,				relative_attention=self.relative_attention,				max_relative_positions=self.max_relative_positions,				position_biased_input=self.position_biased_input,				initializer_range=self.initializer_range,				return_dict=lowercase_,				)

			return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels

	def       snake_case__(	self:	str,				lowerCamelCase_:	Union[str, Any],				lowerCamelCase_:	List[Any],				lowerCamelCase_:	int,				lowerCamelCase_:	List[Any],				lowerCamelCase_:	Dict,				lowerCamelCase_:	Tuple,				lowerCamelCase_:	Tuple ):
			lowercase__				:			int            =		TFDebertaModel(config=lowercase_ )
			lowercase__				:			int            =		{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

			lowercase__				:			List[str]            =		[input_ids, input_mask]
			lowercase__				:			Tuple            =		model(lowercase_ )

			lowercase__				:			List[Any]            =		model(lowercase_ )

			self.parent.assertEqual(result.last_hidden_state.shape,				(self.batch_size, self.seq_length, self.hidden_size) )

	def       snake_case__(	self:	str,				lowerCamelCase_:	Dict,				lowerCamelCase_:	Tuple,				lowerCamelCase_:	int,				lowerCamelCase_:	str,				lowerCamelCase_:	List[str],				lowerCamelCase_:	Optional[Any],				lowerCamelCase_:	List[str] ):
			lowercase__				:			Optional[int]            =		TFDebertaForMaskedLM(config=lowercase_ )
			lowercase__				:			str            =		{
			    '''input_ids''': input_ids,
			    '''attention_mask''': input_mask,
			    '''token_type_ids''': token_type_ids,
			}
			lowercase__				:			Dict            =		model(lowercase_ )
			self.parent.assertEqual(result.logits.shape,				(self.batch_size, self.seq_length, self.vocab_size) )

	def       snake_case__(	self:	Tuple,				lowerCamelCase_:	int,				lowerCamelCase_:	List[Any],				lowerCamelCase_:	int,				lowerCamelCase_:	Union[str, Any],				lowerCamelCase_:	List[str],				lowerCamelCase_:	List[str],				lowerCamelCase_:	Tuple ):
			lowercase__				:			Optional[int]            =		self.num_labels
			lowercase__				:			Tuple            =		TFDebertaForSequenceClassification(config=lowercase_ )
			lowercase__				:			str            =		{
			    '''input_ids''': input_ids,
			    '''attention_mask''': input_mask,
			    '''token_type_ids''': token_type_ids,
			}

			lowercase__				:			Any            =		model(lowercase_ )
			self.parent.assertEqual(result.logits.shape,				(self.batch_size, self.num_labels) )

	def       snake_case__(	self:	Tuple,				lowerCamelCase_:	List[Any],				lowerCamelCase_:	int,				lowerCamelCase_:	Any,				lowerCamelCase_:	Optional[Any],				lowerCamelCase_:	str,				lowerCamelCase_:	Any,				lowerCamelCase_:	Union[str, Any] ):
			lowercase__				:			Any            =		self.num_labels
			lowercase__				:			Dict            =		TFDebertaForTokenClassification(config=lowercase_ )
			lowercase__				:			Dict            =		{
			    '''input_ids''': input_ids,
			    '''attention_mask''': input_mask,
			    '''token_type_ids''': token_type_ids,
			}
			lowercase__				:			Any            =		model(lowercase_ )
			self.parent.assertEqual(result.logits.shape,				(self.batch_size, self.seq_length, self.num_labels) )

	def       snake_case__(	self:	str,				lowerCamelCase_:	List[str],				lowerCamelCase_:	List[Any],				lowerCamelCase_:	int,				lowerCamelCase_:	Union[str, Any],				lowerCamelCase_:	Any,				lowerCamelCase_:	List[Any],				lowerCamelCase_:	Optional[int] ):
			lowercase__				:			str            =		TFDebertaForQuestionAnswering(config=lowercase_ )
			lowercase__				:			List[Any]            =		{
			    '''input_ids''': input_ids,
			    '''attention_mask''': input_mask,
			    '''token_type_ids''': token_type_ids,
			}

			lowercase__				:			Any            =		model(lowercase_ )
			self.parent.assertEqual(result.start_logits.shape,				(self.batch_size, self.seq_length) )
			self.parent.assertEqual(result.end_logits.shape,				(self.batch_size, self.seq_length) )





	def       snake_case__(	self:	Union[str, Any] ):
			lowercase__				:			Optional[int]            =		self.prepare_config_and_inputs()
			(
			    lowercase__    
			)				:			Optional[int]            =		config_and_inputs
			lowercase__				:			Optional[int]            =		{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
			return config, inputs_dict


@require_tf
class 			__lowerCAmelCase				(					__UpperCAmelCase      ,						__UpperCAmelCase      ,						unittest.TestCase			):


	'''simple docstring'''





	_A       	=							(
	    (
	        TFDebertaModel,
	        TFDebertaForMaskedLM,
	        TFDebertaForQuestionAnswering,
	        TFDebertaForSequenceClassification,
	        TFDebertaForTokenClassification,
	    )
	    if is_tf_available()
	    else ()
	)
	_A       	=							(
	    {
	        "feature-extraction": TFDebertaModel,
	        "fill-mask": TFDebertaForMaskedLM,
	        "question-answering": TFDebertaForQuestionAnswering,
	        "text-classification": TFDebertaForSequenceClassification,
	        "token-classification": TFDebertaForTokenClassification,
	        "zero-shot": TFDebertaForSequenceClassification,
	    }
	    if is_tf_available()
	    else {}
	)

	_A       	=							False
	_A       	=							False

	def       snake_case__(	self:	Optional[int] ):
			lowercase__				:			Union[str, Any]            =		TFDebertaModelTester(self )
			lowercase__				:			Optional[int]            =		ConfigTester(self,				config_class=lowercase_,				hidden_size=37 )

	def       snake_case__(	self:	Optional[Any] ):
			self.config_tester.run_common_tests()

	def       snake_case__(	self:	List[str] ):
			lowercase__				:			List[Any]            =		self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*lowercase_ )

	def       snake_case__(	self:	Optional[Any] ):
			lowercase__				:			Optional[Any]            =		self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_masked_lm(*lowercase_ )

	def       snake_case__(	self:	Union[str, Any] ):
			lowercase__				:			List[Any]            =		self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_question_answering(*lowercase_ )

	def       snake_case__(	self:	Dict ):
			lowercase__				:			Optional[Any]            =		self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_sequence_classification(*lowercase_ )

	def       snake_case__(	self:	List[Any] ):
			lowercase__				:			int            =		self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_token_classification(*lowercase_ )





	@slow
	def       snake_case__(	self:	int ):
			lowercase__				:			Dict            =		TFDebertaModel.from_pretrained('kamalkraj/deberta-base' )
			self.assertIsNotNone(lowercase_ )


@require_tf
class 			__lowerCAmelCase				(					unittest.TestCase			):


	'''simple docstring'''





	@unittest.skip(reason='Model not available yet' )
	def       snake_case__(	self:	List[str] ):
			pass





	@slow
	def       snake_case__(	self:	Any ):
			lowercase__				:			List[Any]            =		TFDebertaModel.from_pretrained('kamalkraj/deberta-base' )
			lowercase__				:			Tuple            =		tf.constant([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]] )
			lowercase__				:			Optional[int]            =		tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] )
			lowercase__				:			int            =		model(lowercase_,				attention_mask=lowercase_ )[0]

			lowercase__				:			int            =		tf.constant(
			    [
			        [
			            [-0.5_9_8_5_5_8_9_6, -0.8_0_5_5_2_5_6_6, -0.8_4_6_2_1_3_5],
			            [1.4_4_8_4_0_2_5, -0.9_3_4_8_3_7_9_4, -0.8_0_5_9_3_0_8_5],
			            [0.3_1_2_2_7_4_1, 0.0_0_3_1_6_0_5_9, -1.4_1_3_1_3_7_7],
			        ]
			    ] )
			tf.debugging.assert_near(output[:, 1:4, 1:4],				lowercase_,				atol=1E-4 )










import argparse
import json
from pathlib import Path

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

from transformers import DeformableDetrConfig, DeformableDetrForObjectDetection, DeformableDetrImageProcessor
from transformers.utils import logging


logging.set_verbosity_info()
__UpperCamelCase:  Tuple   =   logging.get_logger(__name__)


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




		'''simple docstring'''
		if "backbone.0.body" in orig_key:
				lowercase__				:			Optional[int]            =		orig_key.replace('backbone.0.body'      ,						'backbone.conv_encoder.model' )
		if "transformer" in orig_key:
				lowercase__				:			Any            =		orig_key.replace('transformer.'      ,						'' )
		if "norm1" in orig_key:
				if "encoder" in orig_key:
						lowercase__				:			str            =		orig_key.replace('norm1'      ,						'self_attn_layer_norm' )
				else:
						lowercase__				:			int            =		orig_key.replace('norm1'      ,						'encoder_attn_layer_norm' )
		if "norm2" in orig_key:
				if "encoder" in orig_key:
						lowercase__				:			Any            =		orig_key.replace('norm2'      ,						'final_layer_norm' )
				else:
						lowercase__				:			Optional[int]            =		orig_key.replace('norm2'      ,						'self_attn_layer_norm' )
		if "norm3" in orig_key:
				lowercase__				:			Union[str, Any]            =		orig_key.replace('norm3'      ,						'final_layer_norm' )
		if "linear1" in orig_key:
				lowercase__				:			List[str]            =		orig_key.replace('linear1'      ,						'fc1' )
		if "linear2" in orig_key:
				lowercase__				:			int            =		orig_key.replace('linear2'      ,						'fc2' )
		if "query_embed" in orig_key:
				lowercase__				:			str            =		orig_key.replace('query_embed'      ,						'query_position_embeddings' )
		if "cross_attn" in orig_key:
				lowercase__				:			int            =		orig_key.replace('cross_attn'      ,						'encoder_attn' )

		return orig_key


def 			SCREAMING_SNAKE_CASE__     (					_lowercase    :   Tuple )      ->       Optional[int]:




		'''simple docstring'''
		# transformer decoder self-attention layers
		for i in range(6 ):
				# read in weights + bias of input projection layer of self-attention
				lowercase__				:			int            =		state_dict.pop(f"""decoder.layers.{i}.self_attn.in_proj_weight""" )
				lowercase__				:			List[Any]            =		state_dict.pop(f"""decoder.layers.{i}.self_attn.in_proj_bias""" )
				# next, add query, keys and values (in that order) to the state dict
				lowercase__				:			str            =		in_proj_weight[:256, :]
				lowercase__				:			Tuple            =		in_proj_bias[:256]
				lowercase__				:			List[str]            =		in_proj_weight[256:512, :]
				lowercase__				:			str            =		in_proj_bias[256:512]
				lowercase__				:			Optional[int]            =		in_proj_weight[-256:, :]
				lowercase__				:			str            =		in_proj_bias[-256:]


def 			SCREAMING_SNAKE_CASE__     (					)      ->       Tuple:




		'''simple docstring'''
		lowercase__				:			str            =		'http://images.cocodataset.org/val2017/000000039769.jpg'
		lowercase__				:			Optional[int]            =		Image.open(requests.get(_lowercase      ,						stream=_lowercase ).raw )

		return im


@torch.no_grad()
def 			SCREAMING_SNAKE_CASE__     (					_lowercase    :   Dict      ,						_lowercase    :   Tuple      ,						_lowercase    :   List[Any]      ,						_lowercase    :   Optional[Any]      ,						_lowercase    :   List[str]      ,						_lowercase    :   List[str]      ,						_lowercase    :   Union[str, Any]      ,						)      ->       List[str]:




		'''simple docstring'''
		lowercase__				:			List[str]            =		DeformableDetrConfig()
		# set config attributes
		if single_scale:
				lowercase__				:			List[str]            =		1
		lowercase__				:			Dict            =		dilation
		lowercase__				:			int            =		with_box_refine
		lowercase__				:			Optional[int]            =		two_stage
		# set labels
		lowercase__				:			Optional[Any]            =		91
		lowercase__				:			Any            =		'huggingface/label-files'
		lowercase__				:			List[Any]            =		'coco-detection-id2label.json'
		lowercase__				:			Any            =		json.load(open(cached_download(hf_hub_url(_lowercase      ,						_lowercase      ,						repo_type='dataset' ) )      ,						'r' ) )
		lowercase__				:			Union[str, Any]            =		{int(_lowercase ): v for k, v in idalabel.items()}
		lowercase__				:			Tuple            =		idalabel
		lowercase__				:			Optional[int]            =		{v: k for k, v in idalabel.items()}

		# load image processor
		lowercase__				:			Tuple            =		DeformableDetrImageProcessor(format='coco_detection' )

		# prepare image
		lowercase__				:			List[Any]            =		prepare_img()
		lowercase__				:			Optional[int]            =		image_processor(images=_lowercase      ,						return_tensors='pt' )
		lowercase__				:			int            =		encoding['pixel_values']

		logger.info('Converting model...' )

		# load original state dict
		lowercase__				:			Dict            =		torch.load(_lowercase      ,						map_location='cpu' )['model']
		# rename keys
		for key in state_dict.copy().keys():
				lowercase__				:			int            =		state_dict.pop(_lowercase )
				lowercase__				:			Optional[Any]            =		val
		# query, key and value matrices need special treatment
		read_in_q_k_v(_lowercase )
		# important: we need to prepend a prefix to each of the base model keys as the head models use different attributes for them
		lowercase__				:			List[str]            =		'model.'
		for key in state_dict.copy().keys():
				if not key.startswith('class_embed' ) and not key.startswith('bbox_embed' ):
						lowercase__				:			Optional[Any]            =		state_dict.pop(_lowercase )
						lowercase__				:			Dict            =		val
    # finally, create HuggingFace model and load state dict
		lowercase__				:			Tuple            =		DeformableDetrForObjectDetection(_lowercase )
		model.load_state_dict(_lowercase )
		model.eval()

		lowercase__				:			str            =		'cuda' if torch.cuda.is_available() else 'cpu'
		model.to(_lowercase )
		# verify our conversion
		lowercase__				:			Dict            =		model(pixel_values.to(_lowercase ) )

		lowercase__				:			List[Any]            =		torch.tensor(
		    [[-9.6_645, -4.3_449, -5.8_705], [-9.7_035, -3.8_504, -5.0_724], [-10.5_634, -5.3_379, -7.5_116]] )
		lowercase__				:			str            =		torch.tensor([[0.8_693, 0.2_289, 0.2_492], [0.3_150, 0.5_489, 0.5_845], [0.5_563, 0.7_580, 0.8_518]] )

		if single_scale:
				lowercase__				:			Any            =		torch.tensor(
				    [[-9.9_051, -4.2_541, -6.4_852], [-9.6_947, -4.0_854, -6.8_033], [-10.0_665, -5.8_470, -7.7_003]] )
				lowercase__				:			Any            =		torch.tensor([[0.7_292, 0.4_991, 0.5_532], [0.7_959, 0.2_426, 0.4_236], [0.7_582, 0.3_518, 0.4_451]] )

		if single_scale and dilation:
				lowercase__				:			Optional[int]            =		torch.tensor(
				    [[-8.9_652, -4.1_074, -5.6_635], [-9.0_596, -4.9_447, -6.6_075], [-10.1_178, -4.5_275, -6.2_671]] )
				lowercase__				:			Dict            =		torch.tensor([[0.7_665, 0.4_130, 0.4_769], [0.8_364, 0.1_841, 0.3_391], [0.6_261, 0.3_895, 0.7_978]] )

		if with_box_refine:
				lowercase__				:			List[Any]            =		torch.tensor(
				    [[-8.8_895, -5.4_187, -6.8_153], [-8.4_706, -6.1_668, -7.6_184], [-9.0_042, -5.5_359, -6.9_141]] )
				lowercase__				:			int            =		torch.tensor([[0.7_828, 0.2_208, 0.4_323], [0.0_892, 0.5_996, 0.1_319], [0.5_524, 0.6_389, 0.8_914]] )

		if with_box_refine and two_stage:
				lowercase__				:			Union[str, Any]            =		torch.tensor(
				    [[-6.7_108, -4.3_213, -6.3_777], [-8.9_014, -6.1_799, -6.7_240], [-6.9_315, -4.4_735, -6.2_298]] )
				lowercase__				:			List[Any]            =		torch.tensor([[0.2_583, 0.5_499, 0.4_683], [0.7_652, 0.9_068, 0.4_882], [0.5_490, 0.2_763, 0.0_564]] )

		print('Logits:'      ,						outputs.logits[0, :3, :3] )

		assert torch.allclose(outputs.logits[0, :3, :3]      ,						expected_logits.to(_lowercase )      ,						atol=1e-4 )
		assert torch.allclose(outputs.pred_boxes[0, :3, :3]      ,						expected_boxes.to(_lowercase )      ,						atol=1e-4 )

		print('Everything ok!' )

		# Save model and image processor
		logger.info(f"""Saving PyTorch model and image processor to {pytorch_dump_folder_path}...""" )
		Path(_lowercase ).mkdir(exist_ok=_lowercase )
		model.save_pretrained(_lowercase )
		image_processor.save_pretrained(_lowercase )

		# Push to hub
		if push_to_hub:
				lowercase__				:			Any            =		'deformable-detr'
				model_name += "-single-scale" if single_scale else ""
				model_name += "-dc5" if dilation else ""
				model_name += "-with-box-refine" if with_box_refine else ""
				model_name += "-two-stage" if two_stage else ""
				print('Pushing model to hub...' )
				model.push_to_hub(repo_path_or_name=_lowercase      ,						organization='nielsr'      ,						commit_message='Add model' )


if __name__ == "__main__":
			__UpperCamelCase:  List[str]   =   argparse.ArgumentParser()

			parser.add_argument(
			    """--checkpoint_path""",
			    type=str,
			    default="""/home/niels/checkpoints/deformable_detr/r50_deformable_detr-checkpoint.pth""",
			    help="""Path to Pytorch checkpoint (.pth file) you'd like to convert.""",
			)
			parser.add_argument("""--single_scale""", action="""store_true""", help="""Whether to set config.num_features_levels = 1.""")
			parser.add_argument("""--dilation""", action="""store_true""", help="""Whether to set config.dilation=True.""")
			parser.add_argument("""--with_box_refine""", action="""store_true""", help="""Whether to set config.with_box_refine=True.""")
			parser.add_argument("""--two_stage""", action="""store_true""", help="""Whether to set config.two_stage=True.""")
			parser.add_argument(
			    """--pytorch_dump_folder_path""",
			    default=None,
			    type=str,
			    required=True,
			    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."""
			)
			__UpperCamelCase:  List[str]   =   parser.parse_args()
			convert_deformable_detr_checkpoint(
			    args.checkpoint_path,
			    args.single_scale,
			    args.dilation,
			    args.with_box_refine,
			    args.two_stage,
			    args.pytorch_dump_folder_path,
			    args.push_to_hub,
			)


