


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_   (lowerCamelCase_      ):
					def __init__(      self   :int			,__snake_case   :int			,__snake_case   :str=13			,__snake_case   :int=7			,__snake_case   :Optional[int]=True			,__snake_case   :int=True			,__snake_case   :Dict=True			,__snake_case   :Tuple=True			,__snake_case   :Dict=99			,__snake_case   :Tuple=32			,__snake_case   :str=5			,__snake_case   :Optional[int]=4			,__snake_case   :Optional[Any]=37			,__snake_case   :List[str]="gelu"			,__snake_case   :List[Any]=0.1			,__snake_case   :Tuple=0.1			,__snake_case   :str=5_12			,__snake_case   :Union[str, Any]=16			,__snake_case   :List[str]=2			,__snake_case   :Tuple=0.02			,__snake_case   :List[Any]=False			,__snake_case   :Optional[int]=True			,__snake_case   :Union[str, Any]="None"			,__snake_case   :List[str]=3			,__snake_case   :Union[str, Any]=4			,__snake_case   :Optional[int]=None			,)					->    List[Any]:
												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   :Optional[int]      )					->    Optional[Any]:
												a__             =     ids_tensor([self.batch_size, self.seq_length]			,self.vocab_size      )

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

												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__             =     ids_tensor([self.batch_size]			,self.num_choices      )

												a__             =     self.get_config()

												return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
					def  lowerCamelCase__(      self   :Any      )					->    Tuple:
												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  lowerCamelCase__(      self   :str      )					->    Tuple:
												a__             =     self.get_config()
												a__             =     3_00
												return config
					def  lowerCamelCase__(      self   :Dict			,__snake_case   :Dict      )					->    List[Any]:
												self.parent.assertListEqual(list(result.loss.size()      )			,[]      )
					def  lowerCamelCase__(      self   :Tuple			,__snake_case   :List[str]			,__snake_case   :Optional[Any]			,__snake_case   :List[Any]			,__snake_case   :Any			,__snake_case   :Tuple			,__snake_case   :Dict			,__snake_case   :Optional[int]      )					->    List[str]:
												a__             =     DebertaModel(config=__snake_case      )
												model.to(__snake_case      )
												model.eval()
												a__             =     model(__snake_case			,attention_mask=__snake_case			,token_type_ids=__snake_case      )[0]
												a__             =     model(__snake_case			,token_type_ids=__snake_case      )[0]
												a__             =     model(__snake_case      )[0]

												self.parent.assertListEqual(list(sequence_output.size()      )			,[self.batch_size, self.seq_length, self.hidden_size]      )
					def  lowerCamelCase__(      self   :Tuple			,__snake_case   :Optional[int]			,__snake_case   :int			,__snake_case   :List[Any]			,__snake_case   :Any			,__snake_case   :Tuple			,__snake_case   :Union[str, Any]			,__snake_case   :Union[str, Any]      )					->    str:
												a__             =     DebertaForMaskedLM(config=__snake_case      )
												model.to(__snake_case      )
												model.eval()
												a__             =     model(__snake_case			,attention_mask=__snake_case			,token_type_ids=__snake_case			,labels=__snake_case      )

												self.parent.assertEqual(result.logits.shape			,(self.batch_size, self.seq_length, self.vocab_size)      )
					def  lowerCamelCase__(      self   :Any			,__snake_case   :Any			,__snake_case   :Optional[Any]			,__snake_case   :Any			,__snake_case   :Union[str, Any]			,__snake_case   :Union[str, Any]			,__snake_case   :Optional[Any]			,__snake_case   :int      )					->    Optional[Any]:
												a__             =     self.num_labels
												a__             =     DebertaForSequenceClassification(__snake_case      )
												model.to(__snake_case      )
												model.eval()
												a__             =     model(__snake_case			,attention_mask=__snake_case			,token_type_ids=__snake_case			,labels=__snake_case      )
												self.parent.assertListEqual(list(result.logits.size()      )			,[self.batch_size, self.num_labels]      )
												self.check_loss_output(__snake_case      )
					def  lowerCamelCase__(      self   :Union[str, Any]			,__snake_case   :List[Any]			,__snake_case   :Optional[Any]			,__snake_case   :Dict			,__snake_case   :Optional[int]			,__snake_case   :Optional[Any]			,__snake_case   :List[str]			,__snake_case   :int      )					->    List[Any]:
												a__             =     self.num_labels
												a__             =     DebertaForTokenClassification(config=__snake_case      )
												model.to(__snake_case      )
												model.eval()
												a__             =     model(__snake_case			,attention_mask=__snake_case			,token_type_ids=__snake_case			,labels=__snake_case      )
												self.parent.assertEqual(result.logits.shape			,(self.batch_size, self.seq_length, self.num_labels)      )
					def  lowerCamelCase__(      self   :List[str]			,__snake_case   :Optional[Any]			,__snake_case   :Any			,__snake_case   :Tuple			,__snake_case   :List[Any]			,__snake_case   :Any			,__snake_case   :Tuple			,__snake_case   :Optional[Any]      )					->    Tuple:
												a__             =     DebertaForQuestionAnswering(config=__snake_case      )
												model.to(__snake_case      )
												model.eval()
												a__             =     model(
												    __snake_case			,attention_mask=__snake_case			,token_type_ids=__snake_case			,start_positions=__snake_case			,end_positions=__snake_case			,)
												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   :List[Any]      )					->    List[Any]:
												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_torch
class        snake_case_   (lowerCamelCase_	,						lowerCamelCase_	,						unittest.TestCase      ):
					UpperCAmelCase__					:    Optional[int]          =			(
					    (
					        DebertaModel,
					        DebertaForMaskedLM,
					        DebertaForSequenceClassification,
					        DebertaForTokenClassification,
					        DebertaForQuestionAnswering,
					    )
					    if is_torch_available()
					    else ()
					)
					UpperCAmelCase__					:    List[Any]          =			(
					    {
					        '''feature-extraction''': DebertaModel,
					        '''fill-mask''': DebertaForMaskedLM,
					        '''question-answering''': DebertaForQuestionAnswering,
					        '''text-classification''': DebertaForSequenceClassification,
					        '''token-classification''': DebertaForTokenClassification,
					        '''zero-shot''': DebertaForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)

					UpperCAmelCase__					:    Optional[Any]          =			True
					UpperCAmelCase__					:    Dict          =			False
					UpperCAmelCase__					:    int          =			False
					UpperCAmelCase__					:    Union[str, Any]          =			False
					UpperCAmelCase__					:    Optional[int]          =			False
					def  lowerCamelCase__(      self   :str      )					->    Optional[Any]:
												a__             =     DebertaModelTester(self      )
												a__             =     ConfigTester(self			,config_class=__snake_case			,hidden_size=37      )
					def  lowerCamelCase__(      self   :Union[str, Any]      )					->    Tuple:
												self.config_tester.run_common_tests()
					def  lowerCamelCase__(      self   :Any      )					->    int:
												a__             =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_deberta_model(*__snake_case      )
					def  lowerCamelCase__(      self   :str      )					->    str:
												a__             =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_deberta_for_sequence_classification(*__snake_case      )
					def  lowerCamelCase__(      self   :Dict      )					->    Union[str, Any]:
												a__             =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_deberta_for_masked_lm(*__snake_case      )
					def  lowerCamelCase__(      self   :List[Any]      )					->    Dict:
												a__             =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_deberta_for_question_answering(*__snake_case      )
					def  lowerCamelCase__(      self   :List[Any]      )					->    Tuple:
												a__             =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_deberta_for_token_classification(*__snake_case      )



					@slow
					def  lowerCamelCase__(      self   :Tuple      )					->    Tuple:
												for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			a__             =     DebertaModel.from_pretrained(__snake_case      )
																			self.assertIsNotNone(__snake_case      )







@require_torch
@require_sentencepiece
@require_tokenizers
class        snake_case_   (unittest.TestCase      ):
					@unittest.skip(reason='Model not available yet'      )
					def  lowerCamelCase__(      self   :Any      )					->    List[str]:
												pass



					@slow
					def  lowerCamelCase__(      self   :Any      )					->    Optional[int]:
												a__             =     DebertaModel.from_pretrained('microsoft/deberta-base'      )

												a__             =     torch.tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]      )
												a__             =     torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]      )
												with torch.no_grad():
																			a__             =     model(__snake_case			,attention_mask=__snake_case      )[0]
												# compare the actual values for a slice.
												a__             =     torch.tensor(
												    [[[-0.59_86, -0.80_55, -0.84_62], [1.44_84, -0.93_48, -0.80_59], [0.31_23, 0.00_32, -1.41_31]]]      )
												self.assertTrue(torch.allclose(output[:, 1:4, 1:4]			,__snake_case			,atol=1E-4      )			,F'{output[:, 1:4, 1:4]}'      )








from abc import ABC, abstractmethod
from argparse import ArgumentParser







class        snake_case_   (lowerCamelCase_      ):
					@staticmethod
					@abstractmethod
					def  lowerCamelCase__(      __snake_case   :ArgumentParser      )					->    Dict:
												raise NotImplementedError()



					@abstractmethod
					def  lowerCamelCase__(      self   :Union[str, Any]      )					->    Dict:
												raise NotImplementedError()




