



import unittest

from transformers import DebertaVaConfig, 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 (
				    DebertaVaForMaskedLM,
				    DebertaVaForMultipleChoice,
				    DebertaVaForQuestionAnswering,
				    DebertaVaForSequenceClassification,
				    DebertaVaForTokenClassification,
				    DebertaVaModel,
				)
				from transformers.models.deberta_va.modeling_deberta_va import DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST
class 				__A					( __UpperCAmelCase      ):





			def __init__(self							,    __magic_name__							,    __magic_name__=13							,    __magic_name__=7							,    __magic_name__=True							,    __magic_name__=True							,    __magic_name__=True							,    __magic_name__=True							,    __magic_name__=99							,    __magic_name__=32							,    __magic_name__=5							,    __magic_name__=4							,    __magic_name__=37							,    __magic_name__="gelu"							,    __magic_name__=0.1							,    __magic_name__=0.1							,    __magic_name__=512							,    __magic_name__=16							,    __magic_name__=2							,    __magic_name__=0.02							,    __magic_name__=False							,    __magic_name__=True							,    __magic_name__="None"							,    __magic_name__=3							,    __magic_name__=4							,    __magic_name__=None							,    ):
						lowerCamelCase__ :      List[Any]								=		parent
						lowerCamelCase__ :      str								=		batch_size
						lowerCamelCase__ :      Optional[Any]								=		seq_length
						lowerCamelCase__ :      int								=		is_training
						lowerCamelCase__ :      List[str]								=		use_input_mask
						lowerCamelCase__ :      List[Any]								=		use_token_type_ids
						lowerCamelCase__ :      Dict								=		use_labels
						lowerCamelCase__ :      Optional[int]								=		vocab_size
						lowerCamelCase__ :      Any								=		hidden_size
						lowerCamelCase__ :      Optional[int]								=		num_hidden_layers
						lowerCamelCase__ :      Dict								=		num_attention_heads
						lowerCamelCase__ :      int								=		intermediate_size
						lowerCamelCase__ :      Any								=		hidden_act
						lowerCamelCase__ :      List[str]								=		hidden_dropout_prob
						lowerCamelCase__ :      int								=		attention_probs_dropout_prob
						lowerCamelCase__ :      int								=		max_position_embeddings
						lowerCamelCase__ :      Any								=		type_vocab_size
						lowerCamelCase__ :      Optional[int]								=		type_sequence_label_size
						lowerCamelCase__ :      List[Any]								=		initializer_range
						lowerCamelCase__ :      Union[str, Any]								=		num_labels
						lowerCamelCase__ :      Union[str, Any]								=		num_choices
						lowerCamelCase__ :      List[str]								=		relative_attention
						lowerCamelCase__ :      str								=		position_biased_input
						lowerCamelCase__ :      Dict								=		pos_att_type
						lowerCamelCase__ :      Dict								=		scope





			def        _snake_case    (self				):
						lowerCamelCase__ :      Union[str, Any]								=		ids_tensor([self.batch_size, self.seq_length]							,    self.vocab_size				)

						lowerCamelCase__ :      Union[str, Any]								=		None
						if self.use_input_mask:
									lowerCamelCase__ :      Optional[Any]								=		ids_tensor([self.batch_size, self.seq_length]							,    vocab_size=2				)

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

						lowerCamelCase__ :      Optional[int]								=		None
						lowerCamelCase__ :      str								=		None
						lowerCamelCase__ :      List[Any]								=		None
						if self.use_labels:
									lowerCamelCase__ :      Union[str, Any]								=		ids_tensor([self.batch_size]							,    self.type_sequence_label_size				)
									lowerCamelCase__ :      Optional[Any]								=		ids_tensor([self.batch_size, self.seq_length]							,    self.num_labels				)
									lowerCamelCase__ :      Union[str, Any]								=		ids_tensor([self.batch_size]							,    self.num_choices				)

						lowerCamelCase__ :      Optional[Any]								=		self.get_config()

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





			def        _snake_case    (self				):
						return 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							,    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        _snake_case    (self							,    __magic_name__				):
						self.parent.assertListEqual(list(result.loss.size()				)							,    []				)





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__				):
						lowerCamelCase__ :      Tuple								=		DebertaVaModel(config=lowerCAmelCase_				)
						model.to(lowerCAmelCase_				)
						model.eval()
						lowerCamelCase__ :      Dict								=		model(lowerCAmelCase_							,    attention_mask=lowerCAmelCase_							,    token_type_ids=lowerCAmelCase_				)[0]
						lowerCamelCase__ :      List[Any]								=		model(lowerCAmelCase_							,    token_type_ids=lowerCAmelCase_				)[0]
						lowerCamelCase__ :      List[Any]								=		model(lowerCAmelCase_				)[0]

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





			def        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__				):
						lowerCamelCase__ :      int								=		DebertaVaForMaskedLM(config=lowerCAmelCase_				)
						model.to(lowerCAmelCase_				)
						model.eval()
						lowerCamelCase__ :      List[Any]								=		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        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__				):
						lowerCamelCase__ :      Dict								=		self.num_labels
						lowerCamelCase__ :      Optional[int]								=		DebertaVaForSequenceClassification(lowerCAmelCase_				)
						model.to(lowerCAmelCase_				)
						model.eval()
						lowerCamelCase__ :      str								=		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        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__				):
						lowerCamelCase__ :      str								=		self.num_labels
						lowerCamelCase__ :      Optional[int]								=		DebertaVaForTokenClassification(config=lowerCAmelCase_				)
						model.to(lowerCAmelCase_				)
						model.eval()
						lowerCamelCase__ :      Union[str, Any]								=		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        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__				):
						lowerCamelCase__ :      Tuple								=		DebertaVaForQuestionAnswering(config=lowerCAmelCase_				)
						model.to(lowerCAmelCase_				)
						model.eval()
						lowerCamelCase__ :      Optional[int]								=		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        _snake_case    (self							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__							,    __magic_name__				):
						lowerCamelCase__ :      List[str]								=		DebertaVaForMultipleChoice(config=lowerCAmelCase_				)
						model.to(lowerCAmelCase_				)
						model.eval()
						lowerCamelCase__ :      Any								=		input_ids.unsqueeze(1				).expand(-1							,    self.num_choices							,    -1				).contiguous()
						lowerCamelCase__ :      Dict								=		token_type_ids.unsqueeze(1				).expand(-1							,    self.num_choices							,    -1				).contiguous()
						lowerCamelCase__ :      Union[str, Any]								=		input_mask.unsqueeze(1				).expand(-1							,    self.num_choices							,    -1				).contiguous()
						lowerCamelCase__ :      Any								=		model(
						    lowerCAmelCase_							,    attention_mask=lowerCAmelCase_							,    token_type_ids=lowerCAmelCase_							,    labels=lowerCAmelCase_							,    )
						self.parent.assertEqual(result.logits.shape							,    (self.batch_size, self.num_choices)				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      str								=		self.prepare_config_and_inputs()
						(
						    (
						    lowerCamelCase__    
						)							,(
						    lowerCamelCase__    
						)							,(
						    lowerCamelCase__    
						)							,(
						    lowerCamelCase__    
						)							,(
						    lowerCamelCase__    
						)							,(
						    lowerCamelCase__    
						)							,(
						    lowerCamelCase__    
						)							,    
						) :      Tuple								=		config_and_inputs
						lowerCamelCase__ :      Optional[Any]								=		{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
						return config, inputs_dict







@require_torch
class 				__A					( __UpperCAmelCase					, __UpperCAmelCase					, unittest.TestCase      ):
			UpperCamelCase					:str     =			(
			    (
			        DebertaVaModel,
			        DebertaVaForMaskedLM,
			        DebertaVaForSequenceClassification,
			        DebertaVaForTokenClassification,
			        DebertaVaForQuestionAnswering,
			        DebertaVaForMultipleChoice,
			    )
			    if is_torch_available()
			    else ()
			)
			UpperCamelCase					:List[str]     =			(
			    {
			        '''feature-extraction''': DebertaVaModel,
			        '''fill-mask''': DebertaVaForMaskedLM,
			        '''question-answering''': DebertaVaForQuestionAnswering,
			        '''text-classification''': DebertaVaForSequenceClassification,
			        '''token-classification''': DebertaVaForTokenClassification,
			        '''zero-shot''': DebertaVaForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)

			UpperCamelCase					:Optional[int]     =			True
			UpperCamelCase					:Any     =			False
			UpperCamelCase					:Optional[int]     =			False
			UpperCamelCase					:Optional[int]     =			False
			UpperCamelCase					:Dict     =			False





			def        _snake_case    (self				):
						lowerCamelCase__ :      str								=		DebertaVaModelTester(self				)
						lowerCamelCase__ :      List[str]								=		ConfigTester(self							,    config_class=lowerCAmelCase_							,    hidden_size=37				)





			def        _snake_case    (self				):
						self.config_tester.run_common_tests()





			def        _snake_case    (self				):
						lowerCamelCase__ :      List[Any]								=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_model(*lowerCAmelCase_				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Tuple								=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_sequence_classification(*lowerCAmelCase_				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Tuple								=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_masked_lm(*lowerCAmelCase_				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Union[str, Any]								=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_question_answering(*lowerCAmelCase_				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      List[Any]								=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_token_classification(*lowerCAmelCase_				)





			def        _snake_case    (self				):
						lowerCamelCase__ :      Tuple								=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_multiple_choice(*lowerCAmelCase_				)





			@slow
			def        _snake_case    (self				):







						for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowerCamelCase__ :      Tuple								=		DebertaVaModel.from_pretrained(lowerCAmelCase_				)
									self.assertIsNotNone(lowerCAmelCase_				)







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





			@unittest.skip(reason="""Model not available yet"""				)
			def        _snake_case    (self				):
						pass





			@slow
			def        _snake_case    (self				):
						lowerCamelCase__ :      Union[str, Any]								=		DebertaVaModel.from_pretrained("""microsoft/deberta-v2-xlarge"""				)

						lowerCamelCase__ :      Tuple								=		torch.tensor([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]				)
						lowerCamelCase__ :      int								=		torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]				)
						with torch.no_grad():
									lowerCamelCase__ :      Dict								=		model(lowerCAmelCase_							,    attention_mask=lowerCAmelCase_				)[0]
						# compare the actual values for a slice.
						lowerCamelCase__ :      Optional[Any]								=		torch.tensor(
						    [[[0.23_56, 0.19_48, 0.03_69], [-0.10_63, 0.35_86, -0.51_52], [-0.63_99, -0.02_59, -0.25_25]]]				)
						self.assertTrue(torch.allclose(output[:, 1:4, 1:4]							,    lowerCAmelCase_							,    atol=1E-4				)							,    f"{output[:, 1:4, 1:4]}"				)






import argparse

from transformers import TaConfig, TaForConditionalGeneration, load_tf_weights_in_ta
from transformers.utils import logging


logging.set_verbosity_info()






def      _A				(UpperCamelCase		:      Any      ,			UpperCamelCase		:      Union[str, Any]      ,			UpperCamelCase		:      Optional[Any] )  ->Any:



			'''simple docstring'''







			lowerCamelCase__ :      Optional[Any]								=		TaConfig.from_json_file(UpperCamelCase )
			print(f"Building PyTorch model from configuration: {config}" )
			lowerCamelCase__ :      Optional[int]								=		TaForConditionalGeneration(UpperCamelCase )

			# Load weights from tf checkpoint
			load_tf_weights_in_ta(UpperCamelCase      ,			UpperCamelCase      ,			UpperCamelCase )

			# Save pytorch-model
			print(f"Save PyTorch model to {pytorch_dump_path}" )
			model.save_pretrained(UpperCamelCase )


if __name__ == "__main__":
				_lowercase     							=						argparse.ArgumentParser()
				# Required parameters
				parser.add_argument(
				    '''--tf_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.'''
				)
				parser.add_argument(
				    '''--config_file''',
				    default=None,
				    type=str,
				    required=True,
				    help=(
				        '''The config json file corresponding to the pre-trained T5 model. \nThis specifies the model architecture.'''
				    ),
				)
				parser.add_argument(
				    '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.'''
				)
				_lowercase     							=						parser.parse_args()
				convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path)



