
"""simple docstring"""





from __future__ import annotations

import unittest

from transformers import DebertaVaConfig, 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 (
		    TFDebertaVaForMaskedLM,
		    TFDebertaVaForQuestionAnswering,
		    TFDebertaVaForSequenceClassification,
		    TFDebertaVaForTokenClassification,
		    TFDebertaVaModel,
		)



class __lowerCAmelCase					:




			"""simple docstring"""







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







								"""simple docstring"""
								A_								=    parent
								A_								=    batch_size
								A_								=    seq_length
								A_								=    is_training
								A_								=    use_input_mask
								A_								=    use_token_type_ids
								A_								=    use_labels
								A_								=    vocab_size
								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_								=    max_position_embeddings
								A_								=    type_vocab_size
								A_								=    type_sequence_label_size
								A_								=    initializer_range
								A_								=    num_labels
								A_								=    num_choices
								A_								=    relative_attention
								A_								=    position_biased_input
								A_								=    pos_att_type
								A_								=    scope




			def   lowerCamelCase__  (					self    :       int						)   ->			Any:







								"""simple docstring"""
								A_								=    ids_tensor([self.batch_size, self.seq_length]							, self.vocab_size						)

								A_								=    None
								if self.use_input_mask:
													A_								=    random_attention_mask([self.batch_size, self.seq_length]						)

								A_								=    None
								if self.use_token_type_ids:
													A_								=    ids_tensor([self.batch_size, self.seq_length]							, self.type_vocab_size						)

								A_								=    None
								A_								=    None
								A_								=    None
								if self.use_labels:
													A_								=    ids_tensor([self.batch_size]							, self.type_sequence_label_size						)
													A_								=    ids_tensor([self.batch_size, self.seq_length]							, self.num_labels						)

								A_								=    DebertaVaConfig(
								    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							, 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   lowerCamelCase__  (					self    :       Any							, _snake_case    :       str							, _snake_case    :       Union[str, Any]							, _snake_case    :       str							, _snake_case    :       Union[str, Any]							, _snake_case    :       Union[str, Any]							, _snake_case    :       Optional[Any]							, _snake_case    :       List[Any]						)   ->			Union[str, Any]:







								"""simple docstring"""
								A_								=    TFDebertaVaModel(config=lowercase_						)
								A_								=    {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

								A_								=    [input_ids, input_mask]
								A_								=    model(lowercase_						)

								A_								=    model(lowercase_						)

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




			def   lowerCamelCase__  (					self    :       str							, _snake_case    :       Dict							, _snake_case    :       Optional[int]							, _snake_case    :       Any							, _snake_case    :       str							, _snake_case    :       Optional[Any]							, _snake_case    :       Dict							, _snake_case    :       Tuple						)   ->			List[str]:







								"""simple docstring"""
								A_								=    TFDebertaVaForMaskedLM(config=lowercase_						)
								A_								=    {
								    "input_ids": input_ids,
								    "attention_mask": input_mask,
								    "token_type_ids": token_type_ids,
								}
								A_								=    model(lowercase_						)
								self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.seq_length, self.vocab_size)						)




			def   lowerCamelCase__  (					self    :       List[Any]							, _snake_case    :       Dict							, _snake_case    :       Dict							, _snake_case    :       List[str]							, _snake_case    :       int							, _snake_case    :       List[Any]							, _snake_case    :       Dict							, _snake_case    :       str						)   ->			Optional[int]:







								"""simple docstring"""
								A_								=    self.num_labels
								A_								=    TFDebertaVaForSequenceClassification(config=lowercase_						)
								A_								=    {
								    "input_ids": input_ids,
								    "attention_mask": input_mask,
								    "token_type_ids": token_type_ids,
								}

								A_								=    model(lowercase_						)
								self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.num_labels)						)




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







								"""simple docstring"""
								A_								=    self.num_labels
								A_								=    TFDebertaVaForTokenClassification(config=lowercase_						)
								A_								=    {
								    "input_ids": input_ids,
								    "attention_mask": input_mask,
								    "token_type_ids": token_type_ids,
								}
								A_								=    model(lowercase_						)
								self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.seq_length, self.num_labels)						)




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







								"""simple docstring"""
								A_								=    TFDebertaVaForQuestionAnswering(config=lowercase_						)
								A_								=    {
								    "input_ids": input_ids,
								    "attention_mask": input_mask,
								    "token_type_ids": token_type_ids,
								}

								A_								=    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   lowerCamelCase__  (					self    :       Any						)   ->			Union[str, Any]:







								"""simple docstring"""
								A_								=    self.prepare_config_and_inputs()
								(
								    (
								    A_
								)			,     (
								    A_
								)			,     (
								    A_
								)			,     (
								    A_
								)			,     (
								    A_
								)			,     (
								    A_
								)			,     (
								    A_
								)			,     
								)								=    config_and_inputs
								A_								=    {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
								return config, inputs_dict





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




			"""simple docstring"""







			snake_case						   =					(
			    (
			        TFDebertaVaModel,
			        TFDebertaVaForMaskedLM,
			        TFDebertaVaForQuestionAnswering,
			        TFDebertaVaForSequenceClassification,
			        TFDebertaVaForTokenClassification,
			    )
			    if is_tf_available()
			    else ()
			)
			snake_case						   =					(
			    {
			        "feature-extraction": TFDebertaVaModel,
			        "fill-mask": TFDebertaVaForMaskedLM,
			        "question-answering": TFDebertaVaForQuestionAnswering,
			        "text-classification": TFDebertaVaForSequenceClassification,
			        "token-classification": TFDebertaVaForTokenClassification,
			        "zero-shot": TFDebertaVaForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)

			snake_case						   =					False
			snake_case						   =					False




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







								"""simple docstring"""
								A_								=    TFDebertaVaModelTester(self						)
								A_								=    ConfigTester(self							, config_class=lowercase_							, hidden_size=37						)




			def   lowerCamelCase__  (					self    :       Optional[Any]						)   ->			Tuple:







								"""simple docstring"""
								self.config_tester.run_common_tests()




			def   lowerCamelCase__  (					self    :       Tuple						)   ->			Optional[Any]:







								"""simple docstring"""
								A_								=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*lowercase_						)




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







								"""simple docstring"""
								A_								=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_lm(*lowercase_						)




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







								"""simple docstring"""
								A_								=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*lowercase_						)




			def   lowerCamelCase__  (					self    :       int						)   ->			Tuple:







								"""simple docstring"""
								A_								=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_sequence_classification(*lowercase_						)




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







								"""simple docstring"""
								A_								=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*lowercase_						)






			@slow
			def   lowerCamelCase__  (					self    :       Union[str, Any]						)   ->			List[str]:







								"""simple docstring"""
								A_								=    TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"						)
								self.assertIsNotNone(lowercase_						)





@require_tf
class __lowerCAmelCase					(       unittest.TestCase   ):




			"""simple docstring"""







			@unittest.skip(reason="Model not available yet"						)
			def   lowerCamelCase__  (					self    :       Any						)   ->			List[Any]:







								"""simple docstring"""
								pass






			@slow
			def   lowerCamelCase__  (					self    :       List[Any]						)   ->			List[str]:







								"""simple docstring"""
								A_								=    TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"						)
								A_								=    tf.constant([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]						)
								A_								=    tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
								A_								=    model(lowercase_							, attention_mask=lowercase_						)[0]

								A_								=    tf.constant(
								    [[[0.2_3_5_6, 0.1_9_4_8, 0.0_3_6_9], [-0.1_0_6_3, 0.3_5_8_6, -0.5_1_5_2], [-0.6_3_9_9, -0.0_2_5_9, -0.2_5_2_5]]]						)
								tf.debugging.assert_near(output[:, 1:4, 1:4]							, lowercase_							, atol=1e-4						)










'''simple docstring'''

import argparse

import requests
import torch
from PIL import Image

from transformers import CLIPProcessor, GroupViTConfig, GroupViTModel


def 						__magic_name__       (      __UpperCAmelCase      )						->   str:

					'''simple docstring'''

					if "img_encoder.pos_embed" in name:
										snake_case_		     =							name.replace('''img_encoder.pos_embed''', '''vision_model.embeddings.position_embeddings'''      )
					if "img_encoder.patch_embed.proj" in name:
										snake_case_		     =							name.replace('''img_encoder.patch_embed.proj''', '''vision_model.embeddings.patch_embeddings.projection'''      )
					if "img_encoder.patch_embed.norm" in name:
										snake_case_		     =							name.replace('''img_encoder.patch_embed.norm''', '''vision_model.embeddings.layernorm'''      )
					if "img_encoder.layers" in name:
										snake_case_		     =							name.replace('''img_encoder.layers''', '''vision_model.encoder.stages'''      )
					if "blocks" in name and "res" not in name:
										snake_case_		     =							name.replace('''blocks''', '''layers'''      )
					if "attn" in name and "pre_assign" not in name:
										snake_case_		     =							name.replace('''attn''', '''self_attn'''      )
					if "proj" in name and "self_attn" in name and "text" not in name:
										snake_case_		     =							name.replace('''proj''', '''out_proj'''      )
					if "pre_assign_attn.attn.proj" in name:
										snake_case_		     =							name.replace('''pre_assign_attn.attn.proj''', '''pre_assign_attn.attn.out_proj'''      )
					if "norm1" in name:
										snake_case_		     =							name.replace('''norm1''', '''layer_norm1'''      )
					if "norm2" in name and "pre_assign" not in name:
										snake_case_		     =							name.replace('''norm2''', '''layer_norm2'''      )
					if "img_encoder.norm" in name:
										snake_case_		     =							name.replace('''img_encoder.norm''', '''vision_model.layernorm'''      )
					# text encoder
					if "text_encoder.token_embedding" in name:
										snake_case_		     =							name.replace('''text_encoder.token_embedding''', '''text_model.embeddings.token_embedding'''      )
					if "text_encoder.positional_embedding" in name:
										snake_case_		     =							name.replace('''text_encoder.positional_embedding''', '''text_model.embeddings.position_embedding.weight'''      )
					if "text_encoder.transformer.resblocks." in name:
										snake_case_		     =							name.replace('''text_encoder.transformer.resblocks.''', '''text_model.encoder.layers.'''      )
					if "ln_1" in name:
										snake_case_		     =							name.replace('''ln_1''', '''layer_norm1'''      )
					if "ln_2" in name:
										snake_case_		     =							name.replace('''ln_2''', '''layer_norm2'''      )
					if "c_fc" in name:
										snake_case_		     =							name.replace('''c_fc''', '''fc1'''      )
					if "c_proj" in name:
										snake_case_		     =							name.replace('''c_proj''', '''fc2'''      )
					if "text_encoder" in name:
										snake_case_		     =							name.replace('''text_encoder''', '''text_model'''      )
					if "ln_final" in name:
										snake_case_		     =							name.replace('''ln_final''', '''final_layer_norm'''      )
					# projection layers
					if "img_projector.linear_hidden." in name:
										snake_case_		     =							name.replace('''img_projector.linear_hidden.''', '''visual_projection.'''      )
					if "img_projector.linear_out." in name:
										snake_case_		     =							name.replace('''img_projector.linear_out.''', '''visual_projection.3.'''      )
					if "text_projector.linear_hidden" in name:
										snake_case_		     =							name.replace('''text_projector.linear_hidden''', '''text_projection'''      )
					if "text_projector.linear_out" in name:
										snake_case_		     =							name.replace('''text_projector.linear_out''', '''text_projection.3'''      )

					return name


def 						__magic_name__       (      __UpperCAmelCase, __UpperCAmelCase      )						->   Dict:

					'''simple docstring'''

					for key in orig_state_dict.copy().keys():
										snake_case_		     =							orig_state_dict.pop(__UpperCAmelCase      )

										if "qkv" in key:
															# weights and biases of the key, value and query projections of vision encoder's attention layers require special treatment:
															# we need to split them up into separate matrices/vectors
															snake_case_		     =							key.split('''.'''      )
															snake_case_     ,snake_case_		     =							int(key_split[2]      ), int(key_split[4]      )
															snake_case_		     =							config.vision_config.hidden_size
															if "weight" in key:
																				snake_case_		     =							val[:dim, :]
																				snake_case_		     =							val[dim : dim * 2, :]
																				snake_case_		     =							val[-dim:, :]
															else:
																				snake_case_		     =							val[:dim]
																				snake_case_		     =							val[dim : dim * 2]
																				snake_case_		     =							val[-dim:]
										elif "in_proj" in key:
															# weights and biases of the key, value and query projections of text encoder's attention layers require special treatment:
															# we need to split them up into separate matrices/vectors
															snake_case_		     =							key.split('''.'''      )
															snake_case_		     =							int(key_split[3]      )
															snake_case_		     =							config.text_config.hidden_size
															if "weight" in key:
																				snake_case_		     =							val[:dim, :]
																				snake_case_		     =							val[
																				    dim : dim * 2, :
																				]
																				snake_case_		     =							val[-dim:, :]
															else:
																				snake_case_		     =							val[:dim]
																				snake_case_		     =							val[dim : dim * 2]
																				snake_case_		     =							val[-dim:]
										else:
															snake_case_		     =							rename_key(__UpperCAmelCase      )
															# squeeze if necessary
															if (
															    "text_projection.0" in new_name
															    or "text_projection.3" in new_name
															    or "visual_projection.0" in new_name
															    or "visual_projection.3" in new_name
															):
																				snake_case_		     =							val.squeeze_()
															else:
																				snake_case_		     =							val

					return orig_state_dict


def 						__magic_name__       (      )						->   Union[str, Any]:

					'''simple docstring'''

					snake_case_		     =							'''http://images.cocodataset.org/val2017/000000039769.jpg'''
					snake_case_		     =							Image.open(requests.get(__UpperCAmelCase, stream=__UpperCAmelCase      ).raw      )
					return im


@torch.no_grad()
def 						__magic_name__       (      __UpperCAmelCase, __UpperCAmelCase, __UpperCAmelCase="groupvit-gcc-yfcc", __UpperCAmelCase=False      )						->   List[str]:

					'''simple docstring'''

					snake_case_		     =							GroupViTConfig()
					snake_case_		     =							GroupViTModel(__UpperCAmelCase      ).eval()

					snake_case_		     =							torch.load(__UpperCAmelCase, map_location='''cpu'''      )['''model''']
					snake_case_		     =							convert_state_dict(__UpperCAmelCase, __UpperCAmelCase      )
					snake_case_     ,snake_case_		     =							model.load_state_dict(__UpperCAmelCase, strict=__UpperCAmelCase      )
					assert missing_keys == ["text_model.embeddings.position_ids"]
					assert (unexpected_keys == ["multi_label_logit_scale"]) or (len(__UpperCAmelCase      ) == 0)

					# verify result
					snake_case_		     =							CLIPProcessor.from_pretrained('''openai/clip-vit-base-patch32'''      )
					snake_case_		     =							prepare_img()
					snake_case_		     =							processor(text=['''a photo of a cat''', '''a photo of a dog'''], images=__UpperCAmelCase, padding=__UpperCAmelCase, return_tensors='''pt'''      )

					with torch.no_grad():
										snake_case_		     =							model(**__UpperCAmelCase      )

					if model_name == "groupvit-gcc-yfcc":
										snake_case_		     =							torch.tensor([[1_3.3_5_2_3, 6.3_6_2_9]]      )
					elif model_name == "groupvit-gcc-redcaps":
										snake_case_		     =							torch.tensor([[1_6.1_8_7_3, 8.6_2_3_0]]      )
					else:
										raise ValueError(F"Model name {model_name} not supported."      )
					assert torch.allclose(outputs.logits_per_image, __UpperCAmelCase, atol=1e-3      )

					processor.save_pretrained(__UpperCAmelCase      )
					model.save_pretrained(__UpperCAmelCase      )
					print('''Successfully saved processor and model to''', __UpperCAmelCase      )

					if push_to_hub:
										print('''Pushing to the hub...'''      )
										processor.push_to_hub(__UpperCAmelCase, organization='''nielsr'''      )
										model.push_to_hub(__UpperCAmelCase, organization='''nielsr'''      )


if __name__ == "__main__":
							a    :							Optional[int]										=    argparse.ArgumentParser()
							parser.add_argument(
							    '--pytorch_dump_folder_path', default=None, type=str, help='Path to dump the processor and PyTorch model.'
							)
							parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to GroupViT checkpoint')
							parser.add_argument(
							    '--model_name',
							    default='groupvit-gccy-fcc',
							    type=str,
							    help='Name of the model. Expecting either \'groupvit-gcc-yfcc\' or \'groupvit-gcc-redcaps\'',
							)
							parser.add_argument(
							    '--push_to_hub',
							    action='store_true',
							    help='Whether or not to push the converted model and processor to the 🤗 hub using the provided `model_name`.',
							)
							a    :							str										=    parser.parse_args()

							convert_groupvit_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.model_name, args.push_to_hub)

