





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 			A__   (							__lowercase     ):




   """simple docstring"""



   def __init__( self ,  lowercase ,  lowercase=13 ,  lowercase=7 ,  lowercase=True ,  lowercase=True ,  lowercase=True ,  lowercase=True ,  lowercase=99 ,  lowercase=32 ,  lowercase=5 ,  lowercase=4 ,  lowercase=37 ,  lowercase="gelu" ,  lowercase=0.1 ,  lowercase=0.1 ,  lowercase=512 ,  lowercase=16 ,  lowercase=2 ,  lowercase=0.02 ,  lowercase=False ,  lowercase=True ,  lowercase="None" ,  lowercase=3 ,  lowercase=4 ,  lowercase=None ,  )      ->						Optional[Any]:







          '''simple docstring'''

          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 							__lowercase		( self)      ->						int:







          '''simple docstring'''

          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 							__lowercase		( self)      ->						str:







          '''simple docstring'''

          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 							__lowercase		( self)      ->						Optional[int]:







          '''simple docstring'''

          a__						:     Any			=    self.get_config()
          a__						:     Optional[Any]			=    300
          return config

   def 							__lowercase		( self ,  lowercase)      ->						Union[str, Any]:







          '''simple docstring'''

          self.parent.assertListEqual(list(result.loss.size()) ,  [])

   def 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase)      ->						int:







          '''simple docstring'''

          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 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase)      ->						List[str]:







          '''simple docstring'''

          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 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase)      ->						Union[str, Any]:







          '''simple docstring'''

          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 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase)      ->						List[Any]:







          '''simple docstring'''

          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 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  lowercase)      ->						List[Any]:







          '''simple docstring'''

          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 							__lowercase		( self)      ->						int:







          '''simple docstring'''

          a__						:     Optional[int]			=    self.prepare_config_and_inputs()
          (
              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 			A__   (							__lowercase			,  __lowercase			,  unittest.TestCase     ):




   """simple docstring"""



   __A       :    str	  =  (
       (
           DebertaModel,
           DebertaForMaskedLM,
           DebertaForSequenceClassification,
           DebertaForTokenClassification,
           DebertaForQuestionAnswering,
       )
       if is_torch_available()
       else ()
   )
   __A       :    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 {}
   )

   __A       :    Optional[Any]	  =  True
   __A       :    List[str]	  =  False
   __A       :    Union[str, Any]	  =  False
   __A       :    Tuple	  =  False
   __A       :    Dict	  =  False

   def 							__lowercase		( self)      ->						Optional[int]:







          '''simple docstring'''

          a__						:     str			=    DebertaModelTester(self)
          a__						:     List[Any]			=    ConfigTester(self ,  config_class=snake_case_ ,  hidden_size=37)

   def 							__lowercase		( self)      ->						List[str]:







          '''simple docstring'''

          self.config_tester.run_common_tests()

   def 							__lowercase		( self)      ->						List[str]:







          '''simple docstring'''

          a__						:     Union[str, Any]			=    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_model(*snake_case_)

   def 							__lowercase		( self)      ->						Tuple:







          '''simple docstring'''

          a__						:     str			=    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_sequence_classification(*snake_case_)

   def 							__lowercase		( self)      ->						List[Any]:







          '''simple docstring'''

          a__						:     int			=    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_masked_lm(*snake_case_)

   def 							__lowercase		( self)      ->						Union[str, Any]:







          '''simple docstring'''

          a__						:     Tuple			=    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_question_answering(*snake_case_)

   def 							__lowercase		( self)      ->						Dict:







          '''simple docstring'''

          a__						:     List[str]			=    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_token_classification(*snake_case_)

   @slow
   def 							__lowercase		( self)      ->						Optional[Any]:







          '''simple docstring'''

          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 			A__   (							unittest.TestCase     ):




   """simple docstring"""



   @unittest.skip(reason='Model not available yet')
   def 							__lowercase		( self)      ->						Union[str, Any]:







          '''simple docstring'''

          pass

   @slow
   def 							__lowercase		( self)      ->						Optional[Any]:







          '''simple docstring'''

          a__						:     int			=    DebertaModel.from_pretrained('microsoft/deberta-base')

          a__						:     Optional[int]			=    torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 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.59_86, -0.80_55, -0.84_62], [1.44_84, -0.93_48, -0.80_59], [0.31_23, 0.00_32, -1.41_31]]])
          self.assertTrue(torch.allclose(output[:, 1:4, 1:4] ,  snake_case_ ,  atol=1e-4) ,  F'{output[:, 1:4, 1:4]}')









import enum
import shutil
import sys


lowercase						, lowercase				:	List[Any]      						=  shutil.get_terminal_size()

lowercase				:	Union[str, Any]      						=  {"""UP""": """A""", """DOWN""": """B""", """RIGHT""": """C""", """LEFT""": """D"""}





class 			A__   (							enum.Enum     ):




   """simple docstring"""



   __A       :    List[str]	  =  0
   __A       :    str	  =  1



def A_     (		A__		,				A__=""    )  ->		int:
       sys.stdout.write(str(A__    ) + end    )
       sys.stdout.flush()



def A_     (		A__		,				A__		,				A__=""    )  ->		int:
       forceWrite(F'\u001b[{color}m{content}\u001b[0m'		,				A__    )



def A_     (		)  ->		Any:
       forceWrite('\r'    )



def A_     (		A__		,				A__    )  ->		List[str]:
       forceWrite(F'\033[{num_lines}{CURSOR_TO_CHAR[direction.upper()]}'    )



def A_     (		)  ->		Any:
       forceWrite(' ' * TERMINAL_WIDTH    )
       reset_cursor()



def A_     (		)  ->		Any:
       reset_cursor()
       forceWrite('-' * TERMINAL_WIDTH    )


