





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   (   _UpperCamelCase   ):

   """simple docstring"""


   def __init__(    self			:		Optional[Any] ,  lowerCAmelCase_			:		List[Any] ,  lowerCAmelCase_			:		Optional[Any]=1_3 ,  lowerCAmelCase_			:		Dict=7 ,  lowerCAmelCase_			:		Optional[int]=True ,  lowerCAmelCase_			:		Union[str, Any]=True ,  lowerCAmelCase_			:		Dict=True ,  lowerCAmelCase_			:		Optional[int]=True ,  lowerCAmelCase_			:		List[str]=9_9 ,  lowerCAmelCase_			:		str=3_2 ,  lowerCAmelCase_			:		str=5 ,  lowerCAmelCase_			:		Dict=4 ,  lowerCAmelCase_			:		List[Any]=3_7 ,  lowerCAmelCase_			:		int="gelu" ,  lowerCAmelCase_			:		Tuple=0.1 ,  lowerCAmelCase_			:		Union[str, Any]=0.1 ,  lowerCAmelCase_			:		Tuple=5_1_2 ,  lowerCAmelCase_			:		List[str]=1_6 ,  lowerCAmelCase_			:		List[str]=2 ,  lowerCAmelCase_			:		Tuple=0.02 ,  lowerCAmelCase_			:		int=False ,  lowerCAmelCase_			:		Any=True ,  lowerCAmelCase_			:		int="None" ,  lowerCAmelCase_			:		Optional[Any]=3 ,  lowerCAmelCase_			:		List[str]=4 ,  lowerCAmelCase_			:		Optional[Any]=None ,  )							->						int:
      __lowerCAmelCase							     =     parent
      __lowerCAmelCase							     =     batch_size
      __lowerCAmelCase							     =     seq_length
      __lowerCAmelCase							     =     is_training
      __lowerCAmelCase							     =     use_input_mask
      __lowerCAmelCase							     =     use_token_type_ids
      __lowerCAmelCase							     =     use_labels
      __lowerCAmelCase							     =     vocab_size
      __lowerCAmelCase							     =     hidden_size
      __lowerCAmelCase							     =     num_hidden_layers
      __lowerCAmelCase							     =     num_attention_heads
      __lowerCAmelCase							     =     intermediate_size
      __lowerCAmelCase							     =     hidden_act
      __lowerCAmelCase							     =     hidden_dropout_prob
      __lowerCAmelCase							     =     attention_probs_dropout_prob
      __lowerCAmelCase							     =     max_position_embeddings
      __lowerCAmelCase							     =     type_vocab_size
      __lowerCAmelCase							     =     type_sequence_label_size
      __lowerCAmelCase							     =     initializer_range
      __lowerCAmelCase							     =     num_labels
      __lowerCAmelCase							     =     num_choices
      __lowerCAmelCase							     =     relative_attention
      __lowerCAmelCase							     =     position_biased_input
      __lowerCAmelCase							     =     pos_att_type
      __lowerCAmelCase							     =     scope






   def     lowercase		(    self			:		Optional[int]					)							->						str:
      __lowerCAmelCase							     =     ids_tensor([self.batch_size, self.seq_length] ,  self.vocab_size					)

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

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

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

      __lowerCAmelCase							     =     self.get_config()

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






   def     lowercase		(    self			:		Union[str, Any]					)							->						Union[str, Any]:
      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     lowercase		(    self			:		List[str] ,  lowerCAmelCase_			:		str					)							->						Optional[Any]:
      self.parent.assertListEqual(list(result.loss.size()					) ,  []					)






   def     lowercase		(    self			:		int ,  lowerCAmelCase_			:		Union[str, Any] ,  lowerCAmelCase_			:		Union[str, Any] ,  lowerCAmelCase_			:		List[Any] ,  lowerCAmelCase_			:		Optional[Any] ,  lowerCAmelCase_			:		str ,  lowerCAmelCase_			:		Optional[Any] ,  lowerCAmelCase_			:		Any					)							->						Optional[int]:
      __lowerCAmelCase							     =     DebertaVaModel(config=lowerCAmelCase_					)
      model.to(lowerCAmelCase_					)
      model.eval()
      __lowerCAmelCase							     =     model(lowerCAmelCase_ ,  attention_mask=lowerCAmelCase_ ,  token_type_ids=lowerCAmelCase_					)[0]
      __lowerCAmelCase							     =     model(lowerCAmelCase_ ,  token_type_ids=lowerCAmelCase_					)[0]
      __lowerCAmelCase							     =     model(lowerCAmelCase_					)[0]

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






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






   def     lowercase		(    self			:		Union[str, Any]					)							->						Optional[Any]:
      __lowerCAmelCase							     =     self.prepare_config_and_inputs()
      (
          (
          __lowerCAmelCase     
      )     ,     (
          __lowerCAmelCase     
      )     ,     (
          __lowerCAmelCase     
      )     ,     (
          __lowerCAmelCase     
      )     ,     (
          __lowerCAmelCase     
      )     ,     (
          __lowerCAmelCase     
      )     ,     (
          __lowerCAmelCase     
      )     ,          
      )							     =     config_and_inputs
      __lowerCAmelCase							     =     {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
      return config, inputs_dict





@require_torch
class     _UpperCAmelCase   (   _UpperCamelCase		,   _UpperCamelCase		,   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     lowercase		(    self			:		List[str]					)							->						List[Any]:
      __lowerCAmelCase							     =     DebertaVaModelTester(self					)
      __lowerCAmelCase							     =     ConfigTester(self ,  config_class=lowerCAmelCase_ ,  hidden_size=3_7					)






   def     lowercase		(    self			:		int					)							->						Any:
      self.config_tester.run_common_tests()






   def     lowercase		(    self			:		int					)							->						Union[str, Any]:
      __lowerCAmelCase							     =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_model(*lowerCAmelCase_					)






   def     lowercase		(    self			:		Union[str, Any]					)							->						Dict:
      __lowerCAmelCase							     =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_sequence_classification(*lowerCAmelCase_					)






   def     lowercase		(    self			:		Optional[int]					)							->						Optional[int]:
      __lowerCAmelCase							     =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_masked_lm(*lowerCAmelCase_					)






   def     lowercase		(    self			:		Tuple					)							->						Optional[Any]:
      __lowerCAmelCase							     =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_question_answering(*lowerCAmelCase_					)






   def     lowercase		(    self			:		Tuple					)							->						List[str]:
      __lowerCAmelCase							     =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_token_classification(*lowerCAmelCase_					)






   def     lowercase		(    self			:		Optional[Any]					)							->						Tuple:
      __lowerCAmelCase							     =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_deberta_for_multiple_choice(*lowerCAmelCase_					)






   @slow
   def     lowercase		(    self			:		Dict					)							->						int:
      for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         __lowerCAmelCase							     =     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     lowercase		(    self			:		List[str]					)							->						Union[str, Any]:
      pass






   @slow
   def     lowercase		(    self			:		Tuple					)							->						Dict:
      __lowerCAmelCase							     =     DebertaVaModel.from_pretrained('microsoft/deberta-v2-xlarge'					)

      __lowerCAmelCase							     =     torch.tensor([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]]					)
      __lowerCAmelCase							     =     torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]					)
      with torch.no_grad():
         __lowerCAmelCase							     =     model(lowerCAmelCase_ ,  attention_mask=lowerCAmelCase_					)[0]
      # compare the actual values for a slice.
      __lowerCAmelCase							     =     torch.tensor(
          [[[0.23_56, 0.19_48, 0.03_69], [-0.10_63, 0.35_86, -0.51_52], [-0.63_99, -0.02_59, -0.25_25]]]					)
      self.assertTrue(torch.allclose(output[:, 1:4, 1:4] ,  lowerCAmelCase_ ,  atol=1e-4					) ,  f"""{output[:, 1:4, 1:4]}"""					)








import unittest

import numpy as np
import torch

from .utils_summarization import build_mask, compute_token_type_ids, process_story, truncate_or_pad



class     _UpperCAmelCase   (   unittest.TestCase   ):

  """simple docstring"""


  def     lowercase		(    self			:		Any					)							->						Optional[int]:
     __lowerCAmelCase							     =     1_0






  def     lowercase		(    self			:		int					)							->						Union[str, Any]:
     __lowerCAmelCase							     =     [1, 2, 3, 4]
     __lowerCAmelCase							     =     [1, 2, 3, 4, 0, 0, 0, 0, 0, 0]
     self.assertEqual(truncate_or_pad(lowerCAmelCase_ ,  self.block_size ,  0					) ,  lowerCAmelCase_					)






  def     lowercase		(    self			:		Optional[Any]					)							->						List[str]:
     __lowerCAmelCase							     =     [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0]
     __lowerCAmelCase							     =     [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0]
     self.assertEqual(truncate_or_pad(lowerCAmelCase_ ,  self.block_size ,  0					) ,  lowerCAmelCase_					)






  def     lowercase		(    self			:		Any					)							->						Optional[Any]:
     __lowerCAmelCase							     =     [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0, 1_1, 1_2, 1_3]
     __lowerCAmelCase							     =     [1, 2, 3, 4, 5, 6, 7, 8, 9, 1_0]
     self.assertEqual(truncate_or_pad(lowerCAmelCase_ ,  self.block_size ,  0					) ,  lowerCAmelCase_					)






  def     lowercase		(    self			:		List[str]					)							->						Any:
     __lowerCAmelCase							     =     'It was the year of Our Lord one thousand seven hundred and\n        seventy-five.\n\nSpiritual revelations were conceded to England at that\n        favoured period, as at this.'
     __lowerCAmelCase     ,     __lowerCAmelCase							     =     process_story(lowerCAmelCase_					)
     self.assertEqual(lowerCAmelCase_ ,  []					)






  def     lowercase		(    self			:		Any					)							->						str:
     __lowerCAmelCase							     =     ''
     __lowerCAmelCase     ,     __lowerCAmelCase							     =     process_story(lowerCAmelCase_					)
     self.assertEqual(lowerCAmelCase_ ,  []					)
     self.assertEqual(lowerCAmelCase_ ,  []					)






  def     lowercase		(    self			:		int					)							->						int:
     __lowerCAmelCase							     =     (
         'It was the year of Our Lord one thousand seven hundred and '
         'seventy-five\n\nSpiritual revelations were conceded to England '
         'at that favoured period, as at this.\n@highlight\n\nIt was the best of times'
     )
     __lowerCAmelCase     ,     __lowerCAmelCase							     =     process_story(lowerCAmelCase_					)

     __lowerCAmelCase							     =     [
         'It was the year of Our Lord one thousand seven hundred and seventy-five.',
         'Spiritual revelations were conceded to England at that favoured period, as at this.',
     ]
     self.assertEqual(lowerCAmelCase_ ,  lowerCAmelCase_					)

     __lowerCAmelCase							     =     ['It was the best of times.']
     self.assertEqual(lowerCAmelCase_ ,  lowerCAmelCase_					)






  def     lowercase		(    self			:		Dict					)							->						Any:
     __lowerCAmelCase							     =     torch.tensor([1, 2, 3, 4]					)
     __lowerCAmelCase							     =     torch.tensor([1, 1, 1, 1]					)
     np.testing.assert_array_equal(build_mask(lowerCAmelCase_ ,  0					).numpy() ,  expected.numpy()					)






  def     lowercase		(    self			:		List[Any]					)							->						Optional[int]:
     __lowerCAmelCase							     =     torch.tensor([1, 2, 3, 4, 2_3, 2_3, 2_3]					)
     __lowerCAmelCase							     =     torch.tensor([1, 1, 1, 1, 0, 0, 0]					)
     np.testing.assert_array_equal(build_mask(lowerCAmelCase_ ,  2_3					).numpy() ,  expected.numpy()					)






  def     lowercase		(    self			:		str					)							->						List[Any]:
     __lowerCAmelCase							     =     torch.tensor([8, 2, 3, 4, 1, 1, 1]					)
     __lowerCAmelCase							     =     torch.tensor([1, 1, 1, 1, 0, 0, 0]					)
     np.testing.assert_array_equal(build_mask(lowerCAmelCase_ ,  1					).numpy() ,  expected.numpy()					)






  def     lowercase		(    self			:		Optional[Any]					)							->						Optional[int]:
     __lowerCAmelCase							     =     1_0_1
     __lowerCAmelCase							     =     torch.tensor([[1, 2, 3, 4, 5, 6], [1, 2, 3, 1_0_1, 5, 6], [1, 1_0_1, 3, 4, 1_0_1, 6]]					)
     __lowerCAmelCase							     =     torch.tensor([[1, 1, 1, 1, 1, 1], [1, 1, 1, 0, 0, 0], [1, 0, 0, 0, 1, 1]]					)

     __lowerCAmelCase							     =     compute_token_type_ids(lowerCAmelCase_ ,  lowerCAmelCase_					)
     np.testing.assert_array_equal(lowerCAmelCase_ ,  lowerCAmelCase_					)

