





import unittest

from transformers import DebertaConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    DebertaForMaskedLM,
		    DebertaForQuestionAnswering,
		    DebertaForSequenceClassification,
		    DebertaForTokenClassification,
		    DebertaModel,
		)
		from transformers.models.deberta.modeling_deberta import DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST

class 		snake_case_   (							UpperCAmelCase_						):







	'''simple docstring'''




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

			'''simple docstring'''




			__lowercase											=     parent
			__lowercase											=     batch_size
			__lowercase											=     seq_length
			__lowercase											=     is_training
			__lowercase											=     use_input_mask
			__lowercase											=     use_token_type_ids
			__lowercase											=     use_labels
			__lowercase											=     vocab_size
			__lowercase											=     hidden_size
			__lowercase											=     num_hidden_layers
			__lowercase											=     num_attention_heads
			__lowercase											=     intermediate_size
			__lowercase											=     hidden_act
			__lowercase											=     hidden_dropout_prob
			__lowercase											=     attention_probs_dropout_prob
			__lowercase											=     max_position_embeddings
			__lowercase											=     type_vocab_size
			__lowercase											=     type_sequence_label_size
			__lowercase											=     initializer_range
			__lowercase											=     num_labels
			__lowercase											=     num_choices
			__lowercase											=     relative_attention
			__lowercase											=     position_biased_input
			__lowercase											=     pos_att_type
			__lowercase											=     scope




	def    UpperCAmelCase			(						self							:     Optional[Any]   )       -> str:

			'''simple docstring'''




			__lowercase											=     ids_tensor([self.batch_size, self.seq_length]					,      self.vocab_size   )

			__lowercase											=     None
			if self.use_input_mask:
					__lowercase											=     ids_tensor([self.batch_size, self.seq_length]					,      vocab_size=2   )

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

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

			__lowercase											=     self.get_config()

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




	def    UpperCAmelCase			(						self							:     Optional[Any]   )       -> Dict:

			'''simple docstring'''




			return 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					,      initializer_range=self.initializer_range					,      relative_attention=self.relative_attention					,      position_biased_input=self.position_biased_input					,      pos_att_type=self.pos_att_type					,      )




	def    UpperCAmelCase			(						self							:     Any   )       -> Tuple:

			'''simple docstring'''




			__lowercase											=     self.get_config()
			__lowercase											=     300
			return config




	def    UpperCAmelCase			(						self							:     Optional[Any]					,      __lowerCamelCase							:     Optional[Any]   )       -> str:

			'''simple docstring'''




			self.parent.assertListEqual(list(result.loss.size()   )					,      []   )




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

			'''simple docstring'''




			__lowercase											=     DebertaModel(config=__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()
			__lowercase											=     model(__lowerCamelCase					,      attention_mask=__lowerCamelCase					,      token_type_ids=__lowerCamelCase   )[0]
			__lowercase											=     model(__lowerCamelCase					,      token_type_ids=__lowerCamelCase   )[0]
			__lowercase											=     model(__lowerCamelCase   )[0]

			self.parent.assertListEqual(list(sequence_output.size()   )					,      [self.batch_size, self.seq_length, self.hidden_size]   )




	def    UpperCAmelCase			(						self							:     Any					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     List[str]					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     Dict					,      __lowerCamelCase							:     List[str]   )       -> Tuple:

			'''simple docstring'''




			__lowercase											=     DebertaForMaskedLM(config=__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()
			__lowercase											=     model(__lowerCamelCase					,      attention_mask=__lowerCamelCase					,      token_type_ids=__lowerCamelCase					,      labels=__lowerCamelCase   )

			self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.seq_length, self.vocab_size)   )




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

			'''simple docstring'''




			__lowercase											=     self.num_labels
			__lowercase											=     DebertaForSequenceClassification(__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()
			__lowercase											=     model(__lowerCamelCase					,      attention_mask=__lowerCamelCase					,      token_type_ids=__lowerCamelCase					,      labels=__lowerCamelCase   )
			self.parent.assertListEqual(list(result.logits.size()   )					,      [self.batch_size, self.num_labels]   )
			self.check_loss_output(__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     Union[str, Any]					,      __lowerCamelCase							:     str					,      __lowerCamelCase							:     List[Any]					,      __lowerCamelCase							:     List[Any]					,      __lowerCamelCase							:     str					,      __lowerCamelCase							:     Tuple					,      __lowerCamelCase							:     Optional[int]					,      __lowerCamelCase							:     Dict   )       -> int:

			'''simple docstring'''




			__lowercase											=     self.num_labels
			__lowercase											=     DebertaForTokenClassification(config=__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()
			__lowercase											=     model(__lowerCamelCase					,      attention_mask=__lowerCamelCase					,      token_type_ids=__lowerCamelCase					,      labels=__lowerCamelCase   )
			self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.seq_length, self.num_labels)   )




	def    UpperCAmelCase			(						self							:     Tuple					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     Any					,      __lowerCamelCase							:     List[str]					,      __lowerCamelCase							:     Union[str, Any]					,      __lowerCamelCase							:     Dict					,      __lowerCamelCase							:     int					,      __lowerCamelCase							:     Dict   )       -> List[str]:

			'''simple docstring'''




			__lowercase											=     DebertaForQuestionAnswering(config=__lowerCamelCase   )
			model.to(__lowerCamelCase   )
			model.eval()
			__lowercase											=     model(
			    __lowerCamelCase					,      attention_mask=__lowerCamelCase					,      token_type_ids=__lowerCamelCase					,      start_positions=__lowerCamelCase					,      end_positions=__lowerCamelCase					,      )
			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    UpperCAmelCase			(						self							:     int   )       -> Dict:

			'''simple docstring'''




			__lowercase											=     self.prepare_config_and_inputs()
			(
			    (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      (
			    __lowercase
			)						,      
			)											=     config_and_inputs
			__lowercase											=     {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
			return config, inputs_dict





@require_torch
class 		snake_case_   (							UpperCAmelCase_     ,     UpperCAmelCase_     ,     unittest.TestCase						):







	'''simple docstring'''
	__UpperCamelCase   =		(
	    (
	        DebertaModel,
	        DebertaForMaskedLM,
	        DebertaForSequenceClassification,
	        DebertaForTokenClassification,
	        DebertaForQuestionAnswering,
	    )
	    if is_torch_available()
	    else ()
	)
	__UpperCamelCase   =		(
	    {
	        '''feature-extraction''': DebertaModel,
	        '''fill-mask''': DebertaForMaskedLM,
	        '''question-answering''': DebertaForQuestionAnswering,
	        '''text-classification''': DebertaForSequenceClassification,
	        '''token-classification''': DebertaForTokenClassification,
	        '''zero-shot''': DebertaForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)

	__UpperCamelCase   =		True
	__UpperCamelCase   =		False
	__UpperCamelCase   =		False
	__UpperCamelCase   =		False
	__UpperCamelCase   =		False




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

			'''simple docstring'''




			__lowercase											=     DebertaModelTester(self   )
			__lowercase											=     ConfigTester(self					,      config_class=__lowerCamelCase					,      hidden_size=37   )




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

			'''simple docstring'''




			self.config_tester.run_common_tests()




	def    UpperCAmelCase			(						self							:     Any   )       -> Optional[int]:

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_deberta_model(*__lowerCamelCase   )




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

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_deberta_for_sequence_classification(*__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     Dict   )       -> Union[str, Any]:

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_deberta_for_masked_lm(*__lowerCamelCase   )




	def    UpperCAmelCase			(						self							:     Optional[int]   )       -> Tuple:

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_deberta_for_question_answering(*__lowerCamelCase   )




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

			'''simple docstring'''




			__lowercase											=     self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_deberta_for_token_classification(*__lowerCamelCase   )






	@slow
	def    UpperCAmelCase			(						self							:     int   )       -> Union[str, Any]:

			'''simple docstring'''




			for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					__lowercase											=     DebertaModel.from_pretrained(__lowerCamelCase   )
					self.assertIsNotNone(__lowerCamelCase   )





@require_torch
@require_sentencepiece
@require_tokenizers
class 		snake_case_   (							unittest.TestCase						):







	'''simple docstring'''




	@unittest.skip(reason='Model not available yet'   )
	def    UpperCAmelCase			(						self							:     List[str]   )       -> int:

			'''simple docstring'''




			pass






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

			'''simple docstring'''




			__lowercase											=     DebertaModel.from_pretrained('microsoft/deberta-base'   )

			__lowercase											=     torch.tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]   )
			__lowercase											=     torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
			with torch.no_grad():
					__lowercase											=     model(__lowerCamelCase					,      attention_mask=__lowerCamelCase   )[0]
			# compare the actual values for a slice.
			__lowercase											=     torch.tensor(
			    [[[-0.5986, -0.8055, -0.8462], [1.4484, -0.9348, -0.8059], [0.3123, 0.0032, -1.4131]]]   )
			self.assertTrue(torch.allclose(output[:, 1:4, 1:4]					,      __lowerCamelCase					,      atol=1E-4   )					,      F"{output[:, 1:4, 1:4]}"   )











import argparse
import json
import re
from pathlib import Path

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import (
    MobileNetVaConfig,
    MobileNetVaForImageClassification,
    MobileNetVaImageProcessor,
    load_tf_weights_in_mobilenet_va,
)
from transformers.utils import logging


logging.set_verbosity_info()
SCREAMING_SNAKE_CASE_   :      str			=   logging.get_logger(__name__)



def  SCREAMING_SNAKE_CASE				(      snake_case							)		->    Tuple:
		__lowercase											=     MobileNetVaConfig(layer_norm_eps=0.001							)

		if "_quant" in model_name:
				raise ValueError('Quantized models are not supported.'							)

		__lowercase											=     re.match(r'^mobilenet_v1_([^_]*)_([^_]*)$'						,   snake_case							)
		if matches:
				__lowercase											=     float(matches[1]							)
				__lowercase											=     int(matches[2]							)

		# The TensorFlow version of MobileNetV1 predicts 1001 classes instead of
		# the usual 1000. The first class (index 0) is "background".
		__lowercase											=     1_001
		__lowercase											=     'imagenet-1k-id2label.json'
		__lowercase											=     'huggingface/label-files'
		__lowercase											=     json.load(open(hf_hub_download(snake_case						,   snake_case						,   repo_type='dataset'							)						,   'r'							)							)
		__lowercase											=     {int(snake_case							) + 1: v for k, v in idalabel.items()}
		__lowercase											=     'background'
		__lowercase											=     idalabel
		__lowercase											=     {v: k for k, v in idalabel.items()}

		return config



def  SCREAMING_SNAKE_CASE				(      )		->    List[Any]:
		__lowercase											=     'http://images.cocodataset.org/val2017/000000039769.jpg'
		__lowercase											=     Image.open(requests.get(snake_case						,   stream=snake_case							).raw							)
		return im



@torch.no_grad()
def  SCREAMING_SNAKE_CASE				(      snake_case						,   snake_case						,   snake_case						,   snake_case=False							)		->    Union[str, Any]:
		__lowercase											=     get_mobilenet_va_config(snake_case							)

		# Load 🤗 model
		__lowercase											=     MobileNetVaForImageClassification(snake_case							).eval()

		# Load weights from TensorFlow checkpoint
		load_tf_weights_in_mobilenet_va(snake_case						,   snake_case						,   snake_case							)

		# Check outputs on an image, prepared by MobileNetV1ImageProcessor
		__lowercase											=     MobileNetVaImageProcessor(
		    crop_size={'width': config.image_size, 'height': config.image_size}						,   size={'shortest_edge': config.image_size + 32}						,   )
		__lowercase											=     image_processor(images=prepare_img()						,   return_tensors='pt'							)
		__lowercase											=     model(**snake_case							)
		__lowercase											=     outputs.logits

		assert logits.shape == (1, 1_001)

		if model_name == "mobilenet_v1_1.0_224":
				__lowercase											=     torch.tensor([-4.1739, -1.1233, 3.1205]							)
		elif model_name == "mobilenet_v1_0.75_192":
				__lowercase											=     torch.tensor([-3.9440, -2.3141, -0.3333]							)
		else:
				__lowercase											=     None

		if expected_logits is not None:
				assert torch.allclose(logits[0, :3]						,   snake_case						,   atol=1E-4							)

		Path(snake_case							).mkdir(exist_ok=snake_case							)
		print(F"Saving model {model_name} to {pytorch_dump_folder_path}"							)
		model.save_pretrained(snake_case							)
		print(F"Saving image processor to {pytorch_dump_folder_path}"							)
		image_processor.save_pretrained(snake_case							)

		if push_to_hub:
				print('Pushing to the hub...'							)
				__lowercase											=     'google/' + model_name
				image_processor.push_to_hub(snake_case							)
				model.push_to_hub(snake_case							)


if __name__ == "__main__":
		SCREAMING_SNAKE_CASE_   :      Union[str, Any]			=   argparse.ArgumentParser()
		# Required parameters
		parser.add_argument(
		    '''--model_name''',
		    default='''mobilenet_v1_1.0_224''',
		    type=str,
		    help='''Name of the MobileNetV1 model you\'d like to convert. Should in the form \'mobilenet_v1_<depth>_<size>\'.''',
		)
		parser.add_argument(
		    '''--checkpoint_path''', required=True, type=str, help='''Path to the original TensorFlow checkpoint (.ckpt file).'''
		)
		parser.add_argument(
		    '''--pytorch_dump_folder_path''', required=True, type=str, help='''Path to the output PyTorch model directory.'''
		)
		parser.add_argument(
		    '''--push_to_hub''', action='''store_true''', help='''Whether or not to push the converted model to the 🤗 hub.'''
		)

		SCREAMING_SNAKE_CASE_   :      int			=   parser.parse_args()
		convert_movilevit_checkpoint(
		    args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub
		)




