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: List[Any]		,     __lowerCamelCase: int		,     __lowerCamelCase: Optional[Any]=13		,     __lowerCamelCase: str=7		,     __lowerCamelCase: Optional[int]=True		,     __lowerCamelCase: List[Any]=True		,     __lowerCamelCase: Any=True		,     __lowerCamelCase: List[str]=True		,     __lowerCamelCase: str=99		,     __lowerCamelCase: int=32		,     __lowerCamelCase: Any=5		,     __lowerCamelCase: Union[str, Any]=4		,     __lowerCamelCase: Optional[int]=37		,     __lowerCamelCase: Optional[Any]="gelu"		,     __lowerCamelCase: Any=0.1		,     __lowerCamelCase: str=0.1		,     __lowerCamelCase: Any=5_12		,     __lowerCamelCase: Optional[Any]=16		,     __lowerCamelCase: Dict=2		,     __lowerCamelCase: Union[str, Any]=0.02		,     __lowerCamelCase: Any=False		,     __lowerCamelCase: Optional[int]=True		,     __lowerCamelCase: List[Any]="None"		,     __lowerCamelCase: Optional[int]=3		,     __lowerCamelCase: Dict=4		,     __lowerCamelCase: List[str]=None		,     )			-> List[Any]:
												__UpperCAmelCase  :	List[str]   			=      parent
												__UpperCAmelCase  :	Dict   			=      batch_size
												__UpperCAmelCase  :	str   			=      seq_length
												__UpperCAmelCase  :	List[str]   			=      is_training
												__UpperCAmelCase  :	Tuple   			=      use_input_mask
												__UpperCAmelCase  :	Tuple   			=      use_token_type_ids
												__UpperCAmelCase  :	Optional[Any]   			=      use_labels
												__UpperCAmelCase  :	str   			=      vocab_size
												__UpperCAmelCase  :	Optional[int]   			=      hidden_size
												__UpperCAmelCase  :	Dict   			=      num_hidden_layers
												__UpperCAmelCase  :	List[str]   			=      num_attention_heads
												__UpperCAmelCase  :	Any   			=      intermediate_size
												__UpperCAmelCase  :	Optional[Any]   			=      hidden_act
												__UpperCAmelCase  :	Union[str, Any]   			=      hidden_dropout_prob
												__UpperCAmelCase  :	Dict   			=      attention_probs_dropout_prob
												__UpperCAmelCase  :	Optional[Any]   			=      max_position_embeddings
												__UpperCAmelCase  :	List[str]   			=      type_vocab_size
												__UpperCAmelCase  :	Optional[Any]   			=      type_sequence_label_size
												__UpperCAmelCase  :	Any   			=      initializer_range
												__UpperCAmelCase  :	int   			=      num_labels
												__UpperCAmelCase  :	List[Any]   			=      num_choices
												__UpperCAmelCase  :	Optional[int]   			=      relative_attention
												__UpperCAmelCase  :	Tuple   			=      position_biased_input
												__UpperCAmelCase  :	Dict   			=      pos_att_type
												__UpperCAmelCase  :	Optional[int]   			=      scope
					def       _lowerCamelCase       (     self: Optional[int]   )			-> Any:
												__UpperCAmelCase  :	List[str]   			=      ids_tensor([self.batch_size, self.seq_length]		,     self.vocab_size   )

												__UpperCAmelCase  :	int   			=      None
												if self.use_input_mask:
																			__UpperCAmelCase  :	List[str]   			=      ids_tensor([self.batch_size, self.seq_length]		,     vocab_size=2   )

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

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

												__UpperCAmelCase  :	int   			=      self.get_config()

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

												self.parent.assertListEqual(list(sequence_output.size()   )		,     [self.batch_size, self.seq_length, self.hidden_size]   )
					def       _lowerCamelCase       (     self: str		,     __lowerCamelCase: List[str]		,     __lowerCamelCase: Optional[Any]		,     __lowerCamelCase: Tuple		,     __lowerCamelCase: List[Any]		,     __lowerCamelCase: Dict		,     __lowerCamelCase: Optional[Any]		,     __lowerCamelCase: List[str]   )			-> List[Any]:
												__UpperCAmelCase  :	int   			=      DebertaForMaskedLM(config=__a   )
												model.to(__a   )
												model.eval()
												__UpperCAmelCase  :	str   			=      model(__a		,     attention_mask=__a		,     token_type_ids=__a		,     labels=__a   )

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

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





					@slow
					def       _lowerCamelCase       (     self: List[str]   )			-> str:
												for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			__UpperCAmelCase  :	Dict   			=      DebertaModel.from_pretrained(__a   )
																			self.assertIsNotNone(__a   )




@require_torch
@require_sentencepiece
@require_tokenizers
class     _snake_case  (  unittest.TestCase ):
					@unittest.skip(reason="Model not available yet"   )
					def       _lowerCamelCase       (     self: Tuple   )			-> int:
												pass





					@slow
					def       _lowerCamelCase       (     self: List[Any]   )			-> Optional[int]:
												__UpperCAmelCase  :	Tuple   			=      DebertaModel.from_pretrained("microsoft/deberta-base"   )

												__UpperCAmelCase  :	Optional[int]   			=      torch.tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]   )
												__UpperCAmelCase  :	Any   			=      torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
												with torch.no_grad():
																			__UpperCAmelCase  :	Tuple   			=      model(__a		,     attention_mask=__a   )[0]
												# compare the actual values for a slice.
												__UpperCAmelCase  :	Optional[int]   			=      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]		,     __a		,     atol=1e-4   )		,     f'''{output[:, 1:4, 1:4]}'''   )











'''simple docstring'''







def  _lowerCamelCase				( lowercase						:      int						)	->	bool:
				if num < 0:
								return False

				_a								= num
				_a								= 0
				while num > 0:
								_a								= rev_num * 10 + (num % 10)
								num //= 10

				return num_copy == rev_num


if __name__ == "__main__":
					import doctest

					doctest.testmod()
