






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_  (		__lowerCamelCase		):




     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.0_2	,						_lowerCAmelCase=False	,						_lowerCAmelCase=True	,						_lowerCAmelCase="None"	,						_lowerCAmelCase=3	,						_lowerCAmelCase=4	,						_lowerCAmelCase=None	,						):
          UpperCAmelCase__      :  Any				      =			parent
          UpperCAmelCase__      :  Tuple				      =			batch_size
          UpperCAmelCase__      :  List[str]				      =			seq_length
          UpperCAmelCase__      :  List[Any]				      =			is_training
          UpperCAmelCase__      :  Any				      =			use_input_mask
          UpperCAmelCase__      :  List[str]				      =			use_token_type_ids
          UpperCAmelCase__      :  List[str]				      =			use_labels
          UpperCAmelCase__      :  Tuple				      =			vocab_size
          UpperCAmelCase__      :  List[Any]				      =			hidden_size
          UpperCAmelCase__      :  Dict				      =			num_hidden_layers
          UpperCAmelCase__      :  Optional[int]				      =			num_attention_heads
          UpperCAmelCase__      :  Union[str, Any]				      =			intermediate_size
          UpperCAmelCase__      :  Dict				      =			hidden_act
          UpperCAmelCase__      :  List[Any]				      =			hidden_dropout_prob
          UpperCAmelCase__      :  Dict				      =			attention_probs_dropout_prob
          UpperCAmelCase__      :  List[str]				      =			max_position_embeddings
          UpperCAmelCase__      :  List[str]				      =			type_vocab_size
          UpperCAmelCase__      :  Tuple				      =			type_sequence_label_size
          UpperCAmelCase__      :  Tuple				      =			initializer_range
          UpperCAmelCase__      :  Union[str, Any]				      =			num_labels
          UpperCAmelCase__      :  Union[str, Any]				      =			num_choices
          UpperCAmelCase__      :  List[str]				      =			relative_attention
          UpperCAmelCase__      :  Optional[Any]				      =			position_biased_input
          UpperCAmelCase__      :  int				      =			pos_att_type
          UpperCAmelCase__      :  Dict				      =			scope




     def   __UpperCAmelCase  (				self		):
          UpperCAmelCase__      :  Dict				      =			ids_tensor([self.batch_size, self.seq_length]	,						self.vocab_size		)

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

          UpperCAmelCase__      :  Union[str, Any]				      =			None
          if self.use_token_type_ids:
               UpperCAmelCase__      :  Optional[int]				      =			ids_tensor([self.batch_size, self.seq_length]	,						self.type_vocab_size		)

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

          UpperCAmelCase__      :  Optional[int]				      =			self.get_config()

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




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




     def   __UpperCAmelCase  (				self	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase		):
          UpperCAmelCase__      :  Dict				      =			DebertaVaModel(config=_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          UpperCAmelCase__      :  List[Any]				      =			model(_lowerCAmelCase	,						attention_mask=_lowerCAmelCase	,						token_type_ids=_lowerCAmelCase		)[0]
          UpperCAmelCase__      :  Dict				      =			model(_lowerCAmelCase	,						token_type_ids=_lowerCAmelCase		)[0]
          UpperCAmelCase__      :  int				      =			model(_lowerCAmelCase		)[0]

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




     def   __UpperCAmelCase  (				self	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase		):
          UpperCAmelCase__      :  Dict				      =			DebertaVaForMaskedLM(config=_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          UpperCAmelCase__      :  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.vocab_size)		)




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




     def   __UpperCAmelCase  (				self		):
          UpperCAmelCase__      :  Any				      =			self.prepare_config_and_inputs()
          (
              (
              UpperCAmelCase__
          )      ,		(
              UpperCAmelCase__
          )      ,		(
              UpperCAmelCase__
          )      ,		(
              UpperCAmelCase__
          )      ,		(
              UpperCAmelCase__
          )      ,		(
              UpperCAmelCase__
          )      ,		(
              UpperCAmelCase__
          )      ,		
          )      :  Union[str, Any]				      =			config_and_inputs
          UpperCAmelCase__      :  str				      =			{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
          return config, inputs_dict







@require_torch
class 				UpperCAmelCase_  (		__lowerCamelCase							,      __lowerCamelCase							,      unittest.TestCase		):
     __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   __UpperCAmelCase  (				self		):
          UpperCAmelCase__      :  Optional[int]				      =			DebertaVaModelTester(self		)
          UpperCAmelCase__      :  str				      =			ConfigTester(self	,						config_class=_lowerCAmelCase	,						hidden_size=37		)




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




     def   __UpperCAmelCase  (				self		):
          UpperCAmelCase__      :  List[str]				      =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_model(*_lowerCAmelCase		)




     def   __UpperCAmelCase  (				self		):
          UpperCAmelCase__      :  Tuple				      =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_sequence_classification(*_lowerCAmelCase		)




     def   __UpperCAmelCase  (				self		):
          UpperCAmelCase__      :  int				      =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_masked_lm(*_lowerCAmelCase		)




     def   __UpperCAmelCase  (				self		):
          UpperCAmelCase__      :  str				      =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_question_answering(*_lowerCAmelCase		)




     def   __UpperCAmelCase  (				self		):
          UpperCAmelCase__      :  Dict				      =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_token_classification(*_lowerCAmelCase		)




     def   __UpperCAmelCase  (				self		):
          UpperCAmelCase__      :  Union[str, Any]				      =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_multiple_choice(*_lowerCAmelCase		)




     @slow
     def   __UpperCAmelCase  (				self		):
          for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               UpperCAmelCase__      :  List[Any]				      =			DebertaVaModel.from_pretrained(_lowerCAmelCase		)
               self.assertIsNotNone(_lowerCAmelCase		)







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




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




     @slow
     def   __UpperCAmelCase  (				self		):
          UpperCAmelCase__      :  Optional[Any]				      =			DebertaVaModel.from_pretrained("""microsoft/deberta-v2-xlarge"""		)

          UpperCAmelCase__      :  List[Any]				      =			torch.tensor([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]		)
          UpperCAmelCase__      :  Optional[int]				      =			torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
          with torch.no_grad():
               UpperCAmelCase__      :  Dict				      =			model(_lowerCAmelCase	,						attention_mask=_lowerCAmelCase		)[0]
          # compare the actual values for a slice.
          UpperCAmelCase__      :  Tuple				      =			torch.tensor(
              [[[0.2_3_5_6, 0.1_9_4_8, 0.0_3_6_9], [-0.1_0_6_3, 0.3_5_8_6, -0.5_1_5_2], [-0.6_3_9_9, -0.0_2_5_9, -0.2_5_2_5]]]		)
          self.assertTrue(torch.allclose(output[:, 1:4, 1:4]	,						_lowerCAmelCase	,						atol=1e-4		)	,						f"{output[:, 1:4, 1:4]}"		)










import numpy as np




def       a_ (       SCREAMING_SNAKE_CASE__   : np.ndarray						,     SCREAMING_SNAKE_CASE__   : float	):
       '''simple docstring'''





       return np.where(vector > 0						,     SCREAMING_SNAKE_CASE__						,     (alpha * (np.exp(SCREAMING_SNAKE_CASE__	) - 1))	)


if __name__ == "__main__":
 import doctest

 doctest.testmod()


