

'''simple docstring'''

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

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

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

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

     SCREAMING_SNAKE_CASE:Dict						      =					self.get_config()

     return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
   def     __UpperCamelCase						( self			:      Tuple							):
     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			:      Dict     ,SCREAMING_SNAKE_CASE__			:      Dict							):
     self.parent.assertListEqual(list(result.loss.size()							)     ,[]							)
   def     __UpperCamelCase						( self			:      Union[str, Any]     ,SCREAMING_SNAKE_CASE__			:      Tuple     ,SCREAMING_SNAKE_CASE__			:      Optional[Any]     ,SCREAMING_SNAKE_CASE__			:      Union[str, Any]     ,SCREAMING_SNAKE_CASE__			:      Union[str, Any]     ,SCREAMING_SNAKE_CASE__			:      Any     ,SCREAMING_SNAKE_CASE__			:      Any     ,SCREAMING_SNAKE_CASE__			:      List[str]							):
     SCREAMING_SNAKE_CASE:Union[str, Any]						      =					DebertaVaModel(config=SCREAMING_SNAKE_CASE__							)
     model.to(SCREAMING_SNAKE_CASE__							)
     model.eval()
     SCREAMING_SNAKE_CASE:Dict						      =					model(SCREAMING_SNAKE_CASE__     ,attention_mask=SCREAMING_SNAKE_CASE__     ,token_type_ids=SCREAMING_SNAKE_CASE__							)[0]
     SCREAMING_SNAKE_CASE:Union[str, Any]						      =					model(SCREAMING_SNAKE_CASE__     ,token_type_ids=SCREAMING_SNAKE_CASE__							)[0]
     SCREAMING_SNAKE_CASE:Optional[Any]						      =					model(SCREAMING_SNAKE_CASE__							)[0]

     self.parent.assertListEqual(list(sequence_output.size()							)     ,[self.batch_size, self.seq_length, self.hidden_size]							)
   def     __UpperCamelCase						( self			:      Optional[Any]     ,SCREAMING_SNAKE_CASE__			:      Dict     ,SCREAMING_SNAKE_CASE__			:      List[Any]     ,SCREAMING_SNAKE_CASE__			:      List[Any]     ,SCREAMING_SNAKE_CASE__			:      Optional[int]     ,SCREAMING_SNAKE_CASE__			:      int     ,SCREAMING_SNAKE_CASE__			:      Optional[int]     ,SCREAMING_SNAKE_CASE__			:      Union[str, Any]							):
     SCREAMING_SNAKE_CASE:str						      =					DebertaVaForMaskedLM(config=SCREAMING_SNAKE_CASE__							)
     model.to(SCREAMING_SNAKE_CASE__							)
     model.eval()
     SCREAMING_SNAKE_CASE:Optional[Any]						      =					model(SCREAMING_SNAKE_CASE__     ,attention_mask=SCREAMING_SNAKE_CASE__     ,token_type_ids=SCREAMING_SNAKE_CASE__     ,labels=SCREAMING_SNAKE_CASE__							)

     self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.seq_length, self.vocab_size)							)
   def     __UpperCamelCase						( self			:      int     ,SCREAMING_SNAKE_CASE__			:      Any     ,SCREAMING_SNAKE_CASE__			:      Optional[int]     ,SCREAMING_SNAKE_CASE__			:      Any     ,SCREAMING_SNAKE_CASE__			:      Dict     ,SCREAMING_SNAKE_CASE__			:      str     ,SCREAMING_SNAKE_CASE__			:      Dict     ,SCREAMING_SNAKE_CASE__			:      Tuple							):
     SCREAMING_SNAKE_CASE:Optional[Any]						      =					self.num_labels
     SCREAMING_SNAKE_CASE:List[Any]						      =					DebertaVaForSequenceClassification(SCREAMING_SNAKE_CASE__							)
     model.to(SCREAMING_SNAKE_CASE__							)
     model.eval()
     SCREAMING_SNAKE_CASE:Union[str, Any]						      =					model(SCREAMING_SNAKE_CASE__     ,attention_mask=SCREAMING_SNAKE_CASE__     ,token_type_ids=SCREAMING_SNAKE_CASE__     ,labels=SCREAMING_SNAKE_CASE__							)
     self.parent.assertListEqual(list(result.logits.size()							)     ,[self.batch_size, self.num_labels]							)
     self.check_loss_output(SCREAMING_SNAKE_CASE__							)
   def     __UpperCamelCase						( self			:      List[Any]     ,SCREAMING_SNAKE_CASE__			:      int     ,SCREAMING_SNAKE_CASE__			:      Optional[Any]     ,SCREAMING_SNAKE_CASE__			:      Optional[int]     ,SCREAMING_SNAKE_CASE__			:      Dict     ,SCREAMING_SNAKE_CASE__			:      List[str]     ,SCREAMING_SNAKE_CASE__			:      Tuple     ,SCREAMING_SNAKE_CASE__			:      Union[str, Any]							):
     SCREAMING_SNAKE_CASE:Optional[int]						      =					self.num_labels
     SCREAMING_SNAKE_CASE:Optional[int]						      =					DebertaVaForTokenClassification(config=SCREAMING_SNAKE_CASE__							)
     model.to(SCREAMING_SNAKE_CASE__							)
     model.eval()
     SCREAMING_SNAKE_CASE:Optional[Any]						      =					model(SCREAMING_SNAKE_CASE__     ,attention_mask=SCREAMING_SNAKE_CASE__     ,token_type_ids=SCREAMING_SNAKE_CASE__     ,labels=SCREAMING_SNAKE_CASE__							)
     self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.seq_length, self.num_labels)							)
   def     __UpperCamelCase						( self			:      Union[str, Any]     ,SCREAMING_SNAKE_CASE__			:      Any     ,SCREAMING_SNAKE_CASE__			:      str     ,SCREAMING_SNAKE_CASE__			:      Union[str, Any]     ,SCREAMING_SNAKE_CASE__			:      List[str]     ,SCREAMING_SNAKE_CASE__			:      Optional[int]     ,SCREAMING_SNAKE_CASE__			:      List[str]     ,SCREAMING_SNAKE_CASE__			:      List[str]							):
     SCREAMING_SNAKE_CASE:Any						      =					DebertaVaForQuestionAnswering(config=SCREAMING_SNAKE_CASE__							)
     model.to(SCREAMING_SNAKE_CASE__							)
     model.eval()
     SCREAMING_SNAKE_CASE:int						      =					model(
         SCREAMING_SNAKE_CASE__     ,attention_mask=SCREAMING_SNAKE_CASE__     ,token_type_ids=SCREAMING_SNAKE_CASE__     ,start_positions=SCREAMING_SNAKE_CASE__     ,end_positions=SCREAMING_SNAKE_CASE__     ,)
     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			:      int     ,SCREAMING_SNAKE_CASE__			:      str     ,SCREAMING_SNAKE_CASE__			:      Tuple     ,SCREAMING_SNAKE_CASE__			:      Tuple     ,SCREAMING_SNAKE_CASE__			:      Optional[Any]     ,SCREAMING_SNAKE_CASE__			:      Any     ,SCREAMING_SNAKE_CASE__			:      Tuple     ,SCREAMING_SNAKE_CASE__			:      Any							):
     SCREAMING_SNAKE_CASE:Optional[Any]						      =					DebertaVaForMultipleChoice(config=SCREAMING_SNAKE_CASE__							)
     model.to(SCREAMING_SNAKE_CASE__							)
     model.eval()
     SCREAMING_SNAKE_CASE:Union[str, Any]						      =					input_ids.unsqueeze(1							).expand(-1     ,self.num_choices     ,-1							).contiguous()
     SCREAMING_SNAKE_CASE:Union[str, Any]						      =					token_type_ids.unsqueeze(1							).expand(-1     ,self.num_choices     ,-1							).contiguous()
     SCREAMING_SNAKE_CASE:Union[str, Any]						      =					input_mask.unsqueeze(1							).expand(-1     ,self.num_choices     ,-1							).contiguous()
     SCREAMING_SNAKE_CASE:List[Any]						      =					model(
         SCREAMING_SNAKE_CASE__     ,attention_mask=SCREAMING_SNAKE_CASE__     ,token_type_ids=SCREAMING_SNAKE_CASE__     ,labels=SCREAMING_SNAKE_CASE__     ,)
     self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.num_choices)							)







   def     __UpperCamelCase						( self			:      List[str]							):
     SCREAMING_SNAKE_CASE:Dict						      =					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
     )      ,    
     ):List[str]						      =					config_and_inputs
     SCREAMING_SNAKE_CASE:Dict						      =					{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
     return config, inputs_dict





@require_torch
class 							_snake_case (       _a	,  _a	,  unittest.TestCase						):
   _A       :     int       =					(
       (
           DebertaVaModel,
           DebertaVaForMaskedLM,
           DebertaVaForSequenceClassification,
           DebertaVaForTokenClassification,
           DebertaVaForQuestionAnswering,
           DebertaVaForMultipleChoice,
       )
       if is_torch_available()
       else ()
   )
   _A       :     Union[str, Any]       =					(
       {
           '''feature-extraction''': DebertaVaModel,
           '''fill-mask''': DebertaVaForMaskedLM,
           '''question-answering''': DebertaVaForQuestionAnswering,
           '''text-classification''': DebertaVaForSequenceClassification,
           '''token-classification''': DebertaVaForTokenClassification,
           '''zero-shot''': DebertaVaForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )

   _A       :     Optional[int]       =					True
   _A       :     Dict       =					False
   _A       :     str       =					False
   _A       :     Union[str, Any]       =					False
   _A       :     List[Any]       =					False
   def     __UpperCamelCase						( self			:      List[str]							):
     SCREAMING_SNAKE_CASE:Optional[int]						      =					DebertaVaModelTester(self							)
     SCREAMING_SNAKE_CASE:Tuple						      =					ConfigTester(self     ,config_class=SCREAMING_SNAKE_CASE__     ,hidden_size=37							)
   def     __UpperCamelCase						( self			:      Tuple							):
     self.config_tester.run_common_tests()
   def     __UpperCamelCase						( self			:      Dict							):
     SCREAMING_SNAKE_CASE:str						      =					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_model(*SCREAMING_SNAKE_CASE__							)
   def     __UpperCamelCase						( self			:      Any							):
     SCREAMING_SNAKE_CASE:int						      =					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_sequence_classification(*SCREAMING_SNAKE_CASE__							)
   def     __UpperCamelCase						( self			:      Optional[Any]							):
     SCREAMING_SNAKE_CASE:Dict						      =					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_masked_lm(*SCREAMING_SNAKE_CASE__							)
   def     __UpperCamelCase						( self			:      str							):
     SCREAMING_SNAKE_CASE:Dict						      =					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_question_answering(*SCREAMING_SNAKE_CASE__							)
   def     __UpperCamelCase						( self			:      List[Any]							):
     SCREAMING_SNAKE_CASE:List[str]						      =					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_token_classification(*SCREAMING_SNAKE_CASE__							)
   def     __UpperCamelCase						( self			:      List[str]							):
     SCREAMING_SNAKE_CASE:Union[str, Any]						      =					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_multiple_choice(*SCREAMING_SNAKE_CASE__							)







   @slow
   def     __UpperCamelCase						( self			:      int							):
     for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       SCREAMING_SNAKE_CASE:Any						      =					DebertaVaModel.from_pretrained(SCREAMING_SNAKE_CASE__							)
       self.assertIsNotNone(SCREAMING_SNAKE_CASE__							)





@require_torch
@require_sentencepiece
@require_tokenizers
class 							_snake_case (       unittest.TestCase						):
   @unittest.skip(reason="Model not available yet"							)
   def     __UpperCamelCase						( self			:      List[str]							):
     pass







   @slow
   def     __UpperCamelCase						( self			:      str							):
     SCREAMING_SNAKE_CASE:List[str]						      =					DebertaVaModel.from_pretrained("microsoft/deberta-v2-xlarge"							)

     SCREAMING_SNAKE_CASE:List[str]						      =					torch.tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]							)
     SCREAMING_SNAKE_CASE:Dict						      =					torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]							)
     with torch.no_grad():
       SCREAMING_SNAKE_CASE:str						      =					model(SCREAMING_SNAKE_CASE__     ,attention_mask=SCREAMING_SNAKE_CASE__							)[0]
     # compare the actual values for a slice.
     SCREAMING_SNAKE_CASE:Optional[Any]						      =					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]     ,SCREAMING_SNAKE_CASE__     ,atol=1e-4							)     ,F'''{output[:, 1:4, 1:4]}'''							)









'''simple docstring'''

from ....utils import logging


A_       						=							logging.get_logger(__name__)





class 							_snake_case (       _a						):
   def __init__( self			:      Optional[Any]     ,SCREAMING_SNAKE_CASE__			:      Optional[int]     ,SCREAMING_SNAKE_CASE__			:      Optional[Any]=None     ,SCREAMING_SNAKE_CASE__			:      Optional[int]=2_048							):
     SCREAMING_SNAKE_CASE:int						      =					config.__dict__
     SCREAMING_SNAKE_CASE:List[str]						      =					modal_hidden_size
     if num_labels:
       SCREAMING_SNAKE_CASE:str						      =					num_labels






