






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 	_UpperCAmelCase  (  _A		):




      """simple docstring"""



      def __init__(   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase=13    ,	_lowerCAmelCase=7    ,	_lowerCAmelCase=True    ,	_lowerCAmelCase=True    ,	_lowerCAmelCase=True    ,	_lowerCAmelCase=True    ,	_lowerCAmelCase=99    ,	_lowerCAmelCase=32    ,	_lowerCAmelCase=5    ,	_lowerCAmelCase=4    ,	_lowerCAmelCase=37    ,	_lowerCAmelCase="gelu"    ,	_lowerCAmelCase=0.1    ,	_lowerCAmelCase=0.1    ,	_lowerCAmelCase=512    ,	_lowerCAmelCase=16    ,	_lowerCAmelCase=2    ,	_lowerCAmelCase=0.02    ,	_lowerCAmelCase=False    ,	_lowerCAmelCase=True    ,	_lowerCAmelCase="None"    ,	_lowerCAmelCase=3    ,	_lowerCAmelCase=4    ,	_lowerCAmelCase=None    ,	):



          '''simple docstring'''
          lowerCAmelCase__   :Any      							=				parent
          lowerCAmelCase__   :Optional[Any]      							=				batch_size
          lowerCAmelCase__   :List[Any]      							=				seq_length
          lowerCAmelCase__   :List[str]      							=				is_training
          lowerCAmelCase__   :List[str]      							=				use_input_mask
          lowerCAmelCase__   :int      							=				use_token_type_ids
          lowerCAmelCase__   :List[str]      							=				use_labels
          lowerCAmelCase__   :int      							=				vocab_size
          lowerCAmelCase__   :Dict      							=				hidden_size
          lowerCAmelCase__   :int      							=				num_hidden_layers
          lowerCAmelCase__   :Dict      							=				num_attention_heads
          lowerCAmelCase__   :Any      							=				intermediate_size
          lowerCAmelCase__   :Any      							=				hidden_act
          lowerCAmelCase__   :int      							=				hidden_dropout_prob
          lowerCAmelCase__   :Tuple      							=				attention_probs_dropout_prob
          lowerCAmelCase__   :Dict      							=				max_position_embeddings
          lowerCAmelCase__   :Any      							=				type_vocab_size
          lowerCAmelCase__   :Any      							=				type_sequence_label_size
          lowerCAmelCase__   :Tuple      							=				initializer_range
          lowerCAmelCase__   :Tuple      							=				num_labels
          lowerCAmelCase__   :Dict      							=				num_choices
          lowerCAmelCase__   :Dict      							=				relative_attention
          lowerCAmelCase__   :Optional[int]      							=				position_biased_input
          lowerCAmelCase__   :List[str]      							=				pos_att_type
          lowerCAmelCase__   :List[Any]      							=				scope
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :List[str]      							=				ids_tensor([self.batch_size, self.seq_length]    ,	self.vocab_size		)

          lowerCAmelCase__   :Dict      							=				None
          if self.use_input_mask:
              lowerCAmelCase__   :List[Any]      							=				ids_tensor([self.batch_size, self.seq_length]    ,	vocab_size=2		)

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

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

          lowerCAmelCase__   :str      							=				self.get_config()

          return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
      def 			snake_case_      (   self		):



          '''simple docstring'''
          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    ,	_lowerCAmelCase		):



          '''simple docstring'''
          self.parent.assertListEqual(list(result.loss.size()		)    ,	[]		)
      def 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :str      							=				DebertaVaModel(config=_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          lowerCAmelCase__   :int      							=				model(_lowerCAmelCase    ,	attention_mask=_lowerCAmelCase    ,	token_type_ids=_lowerCAmelCase		)[0]
          lowerCAmelCase__   :Optional[Any]      							=				model(_lowerCAmelCase    ,	token_type_ids=_lowerCAmelCase		)[0]
          lowerCAmelCase__   :Any      							=				model(_lowerCAmelCase		)[0]

          self.parent.assertListEqual(list(sequence_output.size()		)    ,	[self.batch_size, self.seq_length, self.hidden_size]		)
      def 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :Any      							=				DebertaVaForMaskedLM(config=_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          lowerCAmelCase__   :List[str]      							=				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    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :str      							=				self.num_labels
          lowerCAmelCase__   :Tuple      							=				DebertaVaForSequenceClassification(_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          lowerCAmelCase__   :List[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    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :int      							=				self.num_labels
          lowerCAmelCase__   :Union[str, Any]      							=				DebertaVaForTokenClassification(config=_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          lowerCAmelCase__   :Optional[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    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :int      							=				DebertaVaForQuestionAnswering(config=_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          lowerCAmelCase__   :Dict      							=				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    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :int      							=				DebertaVaForMultipleChoice(config=_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          lowerCAmelCase__   :int      							=				input_ids.unsqueeze(1		).expand(-1    ,	self.num_choices    ,	-1		).contiguous()
          lowerCAmelCase__   :Optional[Any]      							=				token_type_ids.unsqueeze(1		).expand(-1    ,	self.num_choices    ,	-1		).contiguous()
          lowerCAmelCase__   :List[str]      							=				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		):



          '''simple docstring'''
          lowerCAmelCase__   :Any      							=				self.prepare_config_and_inputs()
          (
              lowerCAmelCase__
          )   :int      							=				config_and_inputs
          lowerCAmelCase__   :Optional[int]      							=				{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
          return config, inputs_dict







@require_torch
class 	_UpperCAmelCase  (  _A				,							_A				,							unittest.TestCase		):




      """simple docstring"""



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

      A          =    True
      A          =    False
      A          =    False
      A          =    False
      A          =    False
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Union[str, Any]      							=				DebertaVaModelTester(self		)
          lowerCAmelCase__   :Union[str, Any]      							=				ConfigTester(self    ,	config_class=_lowerCAmelCase    ,	hidden_size=37		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          self.config_tester.run_common_tests()
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Optional[int]      							=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_model(*_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Optional[Any]      							=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_sequence_classification(*_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Union[str, Any]      							=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_masked_lm(*_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :List[Any]      							=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_question_answering(*_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Union[str, Any]      							=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_token_classification(*_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :List[str]      							=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_multiple_choice(*_lowerCAmelCase		)



      @slow
      def 			snake_case_      (   self		):



          '''simple docstring'''
          for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              lowerCAmelCase__   :int      							=				DebertaVaModel.from_pretrained(_lowerCAmelCase		)
              self.assertIsNotNone(_lowerCAmelCase		)







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




      """simple docstring"""



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



          '''simple docstring'''
          pass



      @slow
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Any      							=				DebertaVaModel.from_pretrained("microsoft/deberta-v2-xlarge"		)

          lowerCAmelCase__   :Optional[Any]      							=				torch.tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]		)
          lowerCAmelCase__   :str      							=				torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
          with torch.no_grad():
              lowerCAmelCase__   :Tuple      							=				model(_lowerCAmelCase    ,	attention_mask=_lowerCAmelCase		)[0]
          # compare the actual values for a slice.
          lowerCAmelCase__   :Optional[int]      							=				torch.tensor(
              [[[0.2356, 0.1948, 0.0369], [-0.1063, 0.3586, -0.5152], [-0.6399, -0.0259, -0.2525]]]		)
          self.assertTrue(torch.allclose(output[:, 1:4, 1:4]    ,	_lowerCAmelCase    ,	atol=1e-4		)    ,	f'''{output[:, 1:4, 1:4]}'''		)









def 				snake_case__							(							UpperCAmelCase							:  float       ):

    if edge <= 0 or not isinstance(UpperCAmelCase    ,			UpperCAmelCase       ):
        raise ValueError("Length must be a positive."       )
    return 3 * ((2_5 + 1_0 * (5 ** (1 / 2))) ** (1 / 2)) * (edge**2)


def 				snake_case__							(							UpperCAmelCase							:  float       ):

    if edge <= 0 or not isinstance(UpperCAmelCase    ,			UpperCAmelCase       ):
        raise ValueError("Length must be a positive."       )
    return ((1_5 + (7 * (5 ** (1 / 2)))) / 4) * (edge**3)


if __name__ == "__main__":
 import doctest

 doctest.testmod()



