
import unittest

from transformers import DebertaConfig, 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 (
          DebertaForMaskedLM,
          DebertaForQuestionAnswering,
          DebertaForSequenceClassification,
          DebertaForTokenClassification,
          DebertaModel,
      )
      from transformers.models.deberta.modeling_deberta import DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST



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

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

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

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

    __a			:  Optional[Any]		    =  self.get_config()

    return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
   def 				lowerCAmelCase      (self			: Tuple       ):
    return DebertaConfig(
        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 				lowerCAmelCase      (self			: Tuple       ):
    __a			:  Any		    =  self.get_config()
    __a			:  Optional[Any]		    =  3_0_0
    return config
   def 				lowerCAmelCase      (self			: Union[str, Any] ,    snake_case_			: Union[str, Any]       ):
    self.parent.assertListEqual(list(result.loss.size()       ) ,    []       )
   def 				lowerCAmelCase      (self			: Optional[int] ,    snake_case_			: Optional[int] ,    snake_case_			: Any ,    snake_case_			: str ,    snake_case_			: str ,    snake_case_			: int ,    snake_case_			: Optional[int] ,    snake_case_			: List[str]       ):
    __a			:  Union[str, Any]		    =  DebertaModel(config=snake_case_       )
    model.to(snake_case_       )
    model.eval()
    __a			:  Optional[Any]		    =  model(snake_case_ ,    attention_mask=snake_case_ ,    token_type_ids=snake_case_       )[0]
    __a			:  str		    =  model(snake_case_ ,    token_type_ids=snake_case_       )[0]
    __a			:  int		    =  model(snake_case_       )[0]

    self.parent.assertListEqual(list(sequence_output.size()       ) ,    [self.batch_size, self.seq_length, self.hidden_size]       )
   def 				lowerCAmelCase      (self			: Any ,    snake_case_			: Dict ,    snake_case_			: Tuple ,    snake_case_			: Dict ,    snake_case_			: List[Any] ,    snake_case_			: List[str] ,    snake_case_			: Optional[Any] ,    snake_case_			: Union[str, Any]       ):
    __a			:  List[Any]		    =  DebertaForMaskedLM(config=snake_case_       )
    model.to(snake_case_       )
    model.eval()
    __a			:  Union[str, Any]		    =  model(snake_case_ ,    attention_mask=snake_case_ ,    token_type_ids=snake_case_ ,    labels=snake_case_       )

    self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.seq_length, self.vocab_size)       )
   def 				lowerCAmelCase      (self			: Optional[Any] ,    snake_case_			: Any ,    snake_case_			: Tuple ,    snake_case_			: Tuple ,    snake_case_			: List[str] ,    snake_case_			: str ,    snake_case_			: List[str] ,    snake_case_			: Union[str, Any]       ):
    __a			:  str		    =  self.num_labels
    __a			:  Optional[Any]		    =  DebertaForSequenceClassification(snake_case_       )
    model.to(snake_case_       )
    model.eval()
    __a			:  Tuple		    =  model(snake_case_ ,    attention_mask=snake_case_ ,    token_type_ids=snake_case_ ,    labels=snake_case_       )
    self.parent.assertListEqual(list(result.logits.size()       ) ,    [self.batch_size, self.num_labels]       )
    self.check_loss_output(snake_case_       )
   def 				lowerCAmelCase      (self			: Optional[Any] ,    snake_case_			: List[Any] ,    snake_case_			: List[Any] ,    snake_case_			: Optional[Any] ,    snake_case_			: Dict ,    snake_case_			: Union[str, Any] ,    snake_case_			: str ,    snake_case_			: Any       ):
    __a			:  List[str]		    =  self.num_labels
    __a			:  Union[str, Any]		    =  DebertaForTokenClassification(config=snake_case_       )
    model.to(snake_case_       )
    model.eval()
    __a			:  Dict		    =  model(snake_case_ ,    attention_mask=snake_case_ ,    token_type_ids=snake_case_ ,    labels=snake_case_       )
    self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.seq_length, self.num_labels)       )
   def 				lowerCAmelCase      (self			: int ,    snake_case_			: Union[str, Any] ,    snake_case_			: Optional[Any] ,    snake_case_			: int ,    snake_case_			: str ,    snake_case_			: Dict ,    snake_case_			: Any ,    snake_case_			: List[str]       ):
    __a			:  str		    =  DebertaForQuestionAnswering(config=snake_case_       )
    model.to(snake_case_       )
    model.eval()
    __a			:  Union[str, Any]		    =  model(
        snake_case_ ,    attention_mask=snake_case_ ,    token_type_ids=snake_case_ ,    start_positions=snake_case_ ,    end_positions=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 				lowerCAmelCase      (self			: Optional[int]       ):
    __a			:  Optional[int]		    =  self.prepare_config_and_inputs()
    (
        (
        __a
    )    ,	(
        __a
    )    ,	(
        __a
    )    ,	(
        __a
    )    ,	(
        __a
    )    ,	(
        __a
    )    ,	(
        __a
    )    ,	
    )			:  Tuple		    =  config_and_inputs
    __a			:  Union[str, Any]		    =  {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
    return config, inputs_dict





@require_torch
class 		UpperCamelCase__  (	__lowercase   ,__lowercase   ,unittest.TestCase   ):
   _SCREAMING_SNAKE_CASE				:							str     	=				(
       (
           DebertaModel,
           DebertaForMaskedLM,
           DebertaForSequenceClassification,
           DebertaForTokenClassification,
           DebertaForQuestionAnswering,
       )
       if is_torch_available()
       else ()
   )
   _SCREAMING_SNAKE_CASE				:							List[str]     	=				(
       {
           "feature-extraction": DebertaModel,
           "fill-mask": DebertaForMaskedLM,
           "question-answering": DebertaForQuestionAnswering,
           "text-classification": DebertaForSequenceClassification,
           "token-classification": DebertaForTokenClassification,
           "zero-shot": DebertaForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )

   _SCREAMING_SNAKE_CASE				:							Optional[Any]     	=				True
   _SCREAMING_SNAKE_CASE				:							List[str]     	=				False
   _SCREAMING_SNAKE_CASE				:							Union[str, Any]     	=				False
   _SCREAMING_SNAKE_CASE				:							Tuple     	=				False
   _SCREAMING_SNAKE_CASE				:							Dict     	=				False
   def 				lowerCAmelCase      (self			: Tuple       ):
    __a			:  str		    =  DebertaModelTester(self       )
    __a			:  List[Any]		    =  ConfigTester(self ,    config_class=snake_case_ ,    hidden_size=3_7       )
   def 				lowerCAmelCase      (self			: Dict       ):
    self.config_tester.run_common_tests()
   def 				lowerCAmelCase      (self			: Union[str, Any]       ):
    __a			:  Union[str, Any]		    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_deberta_model(*snake_case_       )
   def 				lowerCAmelCase      (self			: Optional[Any]       ):
    __a			:  str		    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_deberta_for_sequence_classification(*snake_case_       )
   def 				lowerCAmelCase      (self			: List[str]       ):
    __a			:  int		    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_deberta_for_masked_lm(*snake_case_       )
   def 				lowerCAmelCase      (self			: Dict       ):
    __a			:  Tuple		    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_deberta_for_question_answering(*snake_case_       )
   def 				lowerCAmelCase      (self			: List[Any]       ):
    __a			:  List[str]		    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_deberta_for_token_classification(*snake_case_       )







   @slow
   def 				lowerCAmelCase      (self			: List[Any]       ):
    for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     __a			:  str		    =  DebertaModel.from_pretrained(snake_case_       )
     self.assertIsNotNone(snake_case_       )





@require_torch
@require_sentencepiece
@require_tokenizers
class 		UpperCamelCase__  (	unittest.TestCase   ):
   @unittest.skip(reason='''Model not available yet'''       )
   def 				lowerCAmelCase      (self			: Any       ):
    pass







   @slow
   def 				lowerCAmelCase      (self			: Tuple       ):
    __a			:  int		    =  DebertaModel.from_pretrained('''microsoft/deberta-base'''       )

    __a			:  Optional[int]		    =  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]]       )
    __a			:  Optional[int]		    =  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]       )
    with torch.no_grad():
     __a			:  str		    =  model(snake_case_ ,    attention_mask=snake_case_       )[0]
    # compare the actual values for a slice.
    __a			:  str		    =  torch.tensor(
        [[[-0.5986, -0.8055, -0.8462], [1.4484, -0.9348, -0.8059], [0.3123, 0.0032, -1.4131]]]       )
    self.assertTrue(torch.allclose(output[:, 1:4, 1:4] ,    snake_case_ ,    atol=1E-4       ) ,    f"{output[:, 1:4, 1:4]}"       )


from .glue import GlueDataset, GlueDataTrainingArguments
from .language_modeling import (
    LineByLineTextDataset,
    LineByLineWithRefDataset,
    LineByLineWithSOPTextDataset,
    TextDataset,
    TextDatasetForNextSentencePrediction,
)
from .squad import SquadDataset, SquadDataTrainingArguments
