

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 			_SCREAMING_SNAKE_CASE (   __snake_case		):







   '''simple docstring'''




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





   def      _snake_case		(       self					:			int			):
       SCREAMING_SNAKE_CASE										=   ids_tensor([self.batch_size, self.seq_length]     , self.vocab_size			)

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

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

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

       SCREAMING_SNAKE_CASE										=   self.get_config()

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





   def      _snake_case		(       self					:			int			):
       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					:			Any     , __lowerCamelCase					:			List[str]			):
       self.parent.assertListEqual(list(result.loss.size()			)     , []			)





   def      _snake_case		(       self					:			List[Any]     , __lowerCamelCase					:			Optional[Any]     , __lowerCamelCase					:			str     , __lowerCamelCase					:			Dict     , __lowerCamelCase					:			str     , __lowerCamelCase					:			int     , __lowerCamelCase					:			Optional[Any]     , __lowerCamelCase					:			Any			):
       SCREAMING_SNAKE_CASE										=   DebertaVaModel(config=__lowerCamelCase			)
       model.to(__lowerCamelCase			)
       model.eval()
       SCREAMING_SNAKE_CASE										=   model(__lowerCamelCase     , attention_mask=__lowerCamelCase     , token_type_ids=__lowerCamelCase			)[0]
       SCREAMING_SNAKE_CASE										=   model(__lowerCamelCase     , token_type_ids=__lowerCamelCase			)[0]
       SCREAMING_SNAKE_CASE										=   model(__lowerCamelCase			)[0]

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





   def      _snake_case		(       self					:			List[Any]     , __lowerCamelCase					:			Optional[Any]     , __lowerCamelCase					:			List[Any]     , __lowerCamelCase					:			Dict     , __lowerCamelCase					:			int     , __lowerCamelCase					:			Tuple     , __lowerCamelCase					:			Optional[Any]     , __lowerCamelCase					:			Tuple			):
       SCREAMING_SNAKE_CASE										=   DebertaVaForMaskedLM(config=__lowerCamelCase			)
       model.to(__lowerCamelCase			)
       model.eval()
       SCREAMING_SNAKE_CASE										=   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					:			Dict     , __lowerCamelCase					:			Dict     , __lowerCamelCase					:			Dict     , __lowerCamelCase					:			int     , __lowerCamelCase					:			int     , __lowerCamelCase					:			Optional[int]     , __lowerCamelCase					:			List[Any]     , __lowerCamelCase					:			Any			):
       SCREAMING_SNAKE_CASE										=   self.num_labels
       SCREAMING_SNAKE_CASE										=   DebertaVaForSequenceClassification(__lowerCamelCase			)
       model.to(__lowerCamelCase			)
       model.eval()
       SCREAMING_SNAKE_CASE										=   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					:			int     , __lowerCamelCase					:			Dict     , __lowerCamelCase					:			Optional[int]     , __lowerCamelCase					:			Any     , __lowerCamelCase					:			Optional[int]     , __lowerCamelCase					:			Optional[int]     , __lowerCamelCase					:			Union[str, Any]     , __lowerCamelCase					:			List[Any]			):
       SCREAMING_SNAKE_CASE										=   self.num_labels
       SCREAMING_SNAKE_CASE										=   DebertaVaForTokenClassification(config=__lowerCamelCase			)
       model.to(__lowerCamelCase			)
       model.eval()
       SCREAMING_SNAKE_CASE										=   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					:			Optional[int]     , __lowerCamelCase					:			List[Any]     , __lowerCamelCase					:			str     , __lowerCamelCase					:			Tuple     , __lowerCamelCase					:			Union[str, Any]     , __lowerCamelCase					:			List[str]     , __lowerCamelCase					:			List[Any]     , __lowerCamelCase					:			Union[str, Any]			):
       SCREAMING_SNAKE_CASE										=   DebertaVaForQuestionAnswering(config=__lowerCamelCase			)
       model.to(__lowerCamelCase			)
       model.eval()
       SCREAMING_SNAKE_CASE										=   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					:			Optional[Any]     , __lowerCamelCase					:			Optional[int]     , __lowerCamelCase					:			Dict     , __lowerCamelCase					:			Optional[Any]     , __lowerCamelCase					:			str     , __lowerCamelCase					:			Union[str, Any]     , __lowerCamelCase					:			Dict     , __lowerCamelCase					:			str			):
       SCREAMING_SNAKE_CASE										=   DebertaVaForMultipleChoice(config=__lowerCamelCase			)
       model.to(__lowerCamelCase			)
       model.eval()
       SCREAMING_SNAKE_CASE										=   input_ids.unsqueeze(1			).expand(-1     , self.num_choices     , -1			).contiguous()
       SCREAMING_SNAKE_CASE										=   token_type_ids.unsqueeze(1			).expand(-1     , self.num_choices     , -1			).contiguous()
       SCREAMING_SNAKE_CASE										=   input_mask.unsqueeze(1			).expand(-1     , self.num_choices     , -1			).contiguous()
       SCREAMING_SNAKE_CASE										=   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					:			Union[str, Any]			):
       SCREAMING_SNAKE_CASE										=   self.prepare_config_and_inputs()
       (
           (
           SCREAMING_SNAKE_CASE
       )							,	(
           SCREAMING_SNAKE_CASE
       )							,	(
           SCREAMING_SNAKE_CASE
       )							,	(
           SCREAMING_SNAKE_CASE
       )							,	(
           SCREAMING_SNAKE_CASE
       )							,	(
           SCREAMING_SNAKE_CASE
       )							,	(
           SCREAMING_SNAKE_CASE
       )							,	
       )										=   config_and_inputs
       SCREAMING_SNAKE_CASE										=   {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
       return config, inputs_dict





@require_torch
class 			_SCREAMING_SNAKE_CASE (   __snake_case					,       __snake_case					,       unittest.TestCase		):







   '''simple docstring'''




   lowerCamelCase__				=      (
       (
           DebertaVaModel,
           DebertaVaForMaskedLM,
           DebertaVaForSequenceClassification,
           DebertaVaForTokenClassification,
           DebertaVaForQuestionAnswering,
           DebertaVaForMultipleChoice,
       )
       if is_torch_available()
       else ()
   )
   lowerCamelCase__				=      (
       {
           "feature-extraction": DebertaVaModel,
           "fill-mask": DebertaVaForMaskedLM,
           "question-answering": DebertaVaForQuestionAnswering,
           "text-classification": DebertaVaForSequenceClassification,
           "token-classification": DebertaVaForTokenClassification,
           "zero-shot": DebertaVaForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )

   lowerCamelCase__				=      True
   lowerCamelCase__				=      False
   lowerCamelCase__				=      False
   lowerCamelCase__				=      False
   lowerCamelCase__				=      False





   def      _snake_case		(       self					:			str			):
       SCREAMING_SNAKE_CASE										=   DebertaVaModelTester(self			)
       SCREAMING_SNAKE_CASE										=   ConfigTester(self     , config_class=__lowerCamelCase     , hidden_size=37			)





   def      _snake_case		(       self					:			Union[str, Any]			):
       self.config_tester.run_common_tests()





   def      _snake_case		(       self					:			Any			):
       SCREAMING_SNAKE_CASE										=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_model(*__lowerCamelCase			)





   def      _snake_case		(       self					:			int			):
       SCREAMING_SNAKE_CASE										=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_sequence_classification(*__lowerCamelCase			)





   def      _snake_case		(       self					:			str			):
       SCREAMING_SNAKE_CASE										=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_masked_lm(*__lowerCamelCase			)





   def      _snake_case		(       self					:			int			):
       SCREAMING_SNAKE_CASE										=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_question_answering(*__lowerCamelCase			)





   def      _snake_case		(       self					:			Optional[Any]			):
       SCREAMING_SNAKE_CASE										=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_token_classification(*__lowerCamelCase			)





   def      _snake_case		(       self					:			List[Any]			):
       SCREAMING_SNAKE_CASE										=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_multiple_choice(*__lowerCamelCase			)





   @slow
   def      _snake_case		(       self					:			Optional[int]			):
       for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           SCREAMING_SNAKE_CASE										=   DebertaVaModel.from_pretrained(__lowerCamelCase			)
           self.assertIsNotNone(__lowerCamelCase			)





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







   '''simple docstring'''




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





   @slow
   def      _snake_case		(       self					:			Tuple			):
       SCREAMING_SNAKE_CASE										=   DebertaVaModel.from_pretrained("microsoft/deberta-v2-xlarge"			)

       SCREAMING_SNAKE_CASE										=   torch.tensor([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]			)
       SCREAMING_SNAKE_CASE										=   torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]			)
       with torch.no_grad():
           SCREAMING_SNAKE_CASE										=   model(__lowerCamelCase     , attention_mask=__lowerCamelCase			)[0]
       # compare the actual values for a slice.
       SCREAMING_SNAKE_CASE										=   torch.tensor(
           [[[0.2_356, 0.1_948, 0.0_369], [-0.1_063, 0.3_586, -0.5_152], [-0.6_399, -0.0_259, -0.2_525]]]			)
       self.assertTrue(torch.allclose(output[:, 1:4, 1:4]     , __lowerCamelCase     , atol=1e-4			)     , f"{output[:, 1:4, 1:4]}"			)


def 	__a     (      A__			:  int       ):
    if not isinstance(A__			,  A__       ):
        raise ValueError("Input must be an integer"       )
    if input_num <= 0:
        raise ValueError("Input must be positive"       )
    return sum(
        divisor for divisor in range(1			,  input_num // 2 + 1       ) if input_num % divisor == 0       )


if __name__ == "__main__":
    import doctest

    doctest.testmod()