
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   lowerCamelCase					(SCREAMING_SNAKE_CASE__					):
  """simple docstring"""







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







  def    __A     (							self :       Union[str, Any]   )  ->	int:
        SCREAMING_SNAKE_CASE_          =  ids_tensor([self.batch_size, self.seq_length]  ,     self.vocab_size   )

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

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

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

        SCREAMING_SNAKE_CASE_          =  self.get_config()

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







  def    __A     (							self :       Tuple   )  ->	List[str]:
        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    __A     (							self :       List[Any]   )  ->	Union[str, Any]:
        SCREAMING_SNAKE_CASE_          =  self.get_config()
        SCREAMING_SNAKE_CASE_          =  300
        return config







  def    __A     (							self :       List[Any]  ,     __magic_name__ :       Optional[int]   )  ->	Union[str, Any]:
        self.parent.assertListEqual(list(result.loss.size()   )  ,     []   )







  def    __A     (							self :       Dict  ,     __magic_name__ :       Optional[int]  ,     __magic_name__ :       Tuple  ,     __magic_name__ :       List[str]  ,     __magic_name__ :       Optional[Any]  ,     __magic_name__ :       Optional[int]  ,     __magic_name__ :       Any  ,     __magic_name__ :       Optional[int]   )  ->	List[Any]:
        SCREAMING_SNAKE_CASE_          =  DebertaModel(config=__magic_name__   )
        model.to(__magic_name__   )
        model.eval()
        SCREAMING_SNAKE_CASE_          =  model(__magic_name__  ,     attention_mask=__magic_name__  ,     token_type_ids=__magic_name__   )[0]
        SCREAMING_SNAKE_CASE_          =  model(__magic_name__  ,     token_type_ids=__magic_name__   )[0]
        SCREAMING_SNAKE_CASE_          =  model(__magic_name__   )[0]

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







  def    __A     (							self :       Optional[Any]  ,     __magic_name__ :       List[Any]  ,     __magic_name__ :       Union[str, Any]  ,     __magic_name__ :       Dict  ,     __magic_name__ :       int  ,     __magic_name__ :       Any  ,     __magic_name__ :       Union[str, Any]  ,     __magic_name__ :       Any   )  ->	Any:
        SCREAMING_SNAKE_CASE_          =  DebertaForMaskedLM(config=__magic_name__   )
        model.to(__magic_name__   )
        model.eval()
        SCREAMING_SNAKE_CASE_          =  model(__magic_name__  ,     attention_mask=__magic_name__  ,     token_type_ids=__magic_name__  ,     labels=__magic_name__   )

        self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.seq_length, self.vocab_size)   )







  def    __A     (							self :       Any  ,     __magic_name__ :       Optional[Any]  ,     __magic_name__ :       Union[str, Any]  ,     __magic_name__ :       List[Any]  ,     __magic_name__ :       Optional[int]  ,     __magic_name__ :       Optional[Any]  ,     __magic_name__ :       str  ,     __magic_name__ :       Optional[Any]   )  ->	Dict:
        SCREAMING_SNAKE_CASE_          =  self.num_labels
        SCREAMING_SNAKE_CASE_          =  DebertaForSequenceClassification(__magic_name__   )
        model.to(__magic_name__   )
        model.eval()
        SCREAMING_SNAKE_CASE_          =  model(__magic_name__  ,     attention_mask=__magic_name__  ,     token_type_ids=__magic_name__  ,     labels=__magic_name__   )
        self.parent.assertListEqual(list(result.logits.size()   )  ,     [self.batch_size, self.num_labels]   )
        self.check_loss_output(__magic_name__   )







  def    __A     (							self :       List[str]  ,     __magic_name__ :       List[str]  ,     __magic_name__ :       Any  ,     __magic_name__ :       str  ,     __magic_name__ :       Optional[Any]  ,     __magic_name__ :       str  ,     __magic_name__ :       Optional[Any]  ,     __magic_name__ :       List[Any]   )  ->	int:
        SCREAMING_SNAKE_CASE_          =  self.num_labels
        SCREAMING_SNAKE_CASE_          =  DebertaForTokenClassification(config=__magic_name__   )
        model.to(__magic_name__   )
        model.eval()
        SCREAMING_SNAKE_CASE_          =  model(__magic_name__  ,     attention_mask=__magic_name__  ,     token_type_ids=__magic_name__  ,     labels=__magic_name__   )
        self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.seq_length, self.num_labels)   )







  def    __A     (							self :       List[Any]  ,     __magic_name__ :       Dict  ,     __magic_name__ :       Dict  ,     __magic_name__ :       Dict  ,     __magic_name__ :       int  ,     __magic_name__ :       List[Any]  ,     __magic_name__ :       int  ,     __magic_name__ :       List[str]   )  ->	Dict:
        SCREAMING_SNAKE_CASE_          =  DebertaForQuestionAnswering(config=__magic_name__   )
        model.to(__magic_name__   )
        model.eval()
        SCREAMING_SNAKE_CASE_          =  model(
            __magic_name__  ,     attention_mask=__magic_name__  ,     token_type_ids=__magic_name__  ,     start_positions=__magic_name__  ,     end_positions=__magic_name__  ,     )
        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    __A     (							self :       int   )  ->	Dict:
        SCREAMING_SNAKE_CASE_          =  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_ 
        )     ,  
        )          =  config_and_inputs
        SCREAMING_SNAKE_CASE_          =  {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
        return config, inputs_dict



@require_torch
class   lowerCamelCase					(SCREAMING_SNAKE_CASE__  ,      SCREAMING_SNAKE_CASE__  ,      unittest.TestCase					):
  """simple docstring"""







  lowerCamelCase__     =						(
      (
          DebertaModel,
          DebertaForMaskedLM,
          DebertaForSequenceClassification,
          DebertaForTokenClassification,
          DebertaForQuestionAnswering,
      )
      if is_torch_available()
      else ()
  )
  lowerCamelCase__     =						(
      {
          '''feature-extraction''': DebertaModel,
          '''fill-mask''': DebertaForMaskedLM,
          '''question-answering''': DebertaForQuestionAnswering,
          '''text-classification''': DebertaForSequenceClassification,
          '''token-classification''': DebertaForTokenClassification,
          '''zero-shot''': DebertaForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )

  lowerCamelCase__     =						True
  lowerCamelCase__     =						False
  lowerCamelCase__     =						False
  lowerCamelCase__     =						False
  lowerCamelCase__     =						False







  def    __A     (							self :       Any   )  ->	int:
        SCREAMING_SNAKE_CASE_          =  DebertaModelTester(self   )
        SCREAMING_SNAKE_CASE_          =  ConfigTester(self  ,     config_class=__magic_name__  ,     hidden_size=37   )







  def    __A     (							self :       Dict   )  ->	Dict:
        self.config_tester.run_common_tests()







  def    __A     (							self :       List[Any]   )  ->	Union[str, Any]:
        SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_deberta_model(*__magic_name__   )







  def    __A     (							self :       List[Any]   )  ->	Optional[Any]:
        SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_deberta_for_sequence_classification(*__magic_name__   )







  def    __A     (							self :       List[Any]   )  ->	Union[str, Any]:
        SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_deberta_for_masked_lm(*__magic_name__   )







  def    __A     (							self :       Optional[Any]   )  ->	int:
        SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_deberta_for_question_answering(*__magic_name__   )







  def    __A     (							self :       Any   )  ->	Dict:
        SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_deberta_for_token_classification(*__magic_name__   )







  @slow
  def    __A     (							self :       Optional[int]   )  ->	str:
        for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              SCREAMING_SNAKE_CASE_          =  DebertaModel.from_pretrained(__magic_name__   )
              self.assertIsNotNone(__magic_name__   )



@require_torch
@require_sentencepiece
@require_tokenizers
class   lowerCamelCase					(unittest.TestCase					):
  """simple docstring"""







  @unittest.skip(reason="Model not available yet"   )
  def    __A     (							self :       Any   )  ->	int:
        pass







  @slow
  def    __A     (							self :       Any   )  ->	Optional[Any]:
        SCREAMING_SNAKE_CASE_          =  DebertaModel.from_pretrained("microsoft/deberta-base"   )

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



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


A     :  str					     =       {
    "configuration_rembert": ["REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "RemBertConfig", "RemBertOnnxConfig"]
}

try:
      if not is_sentencepiece_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      A     :  Tuple					     =       ["RemBertTokenizer"]

try:
      if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      A     :  Tuple					     =       ["RemBertTokenizerFast"]

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      A     :  Tuple					     =       [
          "REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST",
          "RemBertForCausalLM",
          "RemBertForMaskedLM",
          "RemBertForMultipleChoice",
          "RemBertForQuestionAnswering",
          "RemBertForSequenceClassification",
          "RemBertForTokenClassification",
          "RemBertLayer",
          "RemBertModel",
          "RemBertPreTrainedModel",
          "load_tf_weights_in_rembert",
      ]


try:
      if not is_tf_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      A     :  List[str]					     =       [
          "TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST",
          "TFRemBertForCausalLM",
          "TFRemBertForMaskedLM",
          "TFRemBertForMultipleChoice",
          "TFRemBertForQuestionAnswering",
          "TFRemBertForSequenceClassification",
          "TFRemBertForTokenClassification",
          "TFRemBertLayer",
          "TFRemBertModel",
          "TFRemBertPreTrainedModel",
      ]


if TYPE_CHECKING:
      from .configuration_rembert import REMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, RemBertConfig, RemBertOnnxConfig

      try:
            if not is_sentencepiece_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_rembert import RemBertTokenizer

      try:
            if not is_tokenizers_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_rembert_fast import RemBertTokenizerFast

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_rembert import (
                REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
                RemBertForCausalLM,
                RemBertForMaskedLM,
                RemBertForMultipleChoice,
                RemBertForQuestionAnswering,
                RemBertForSequenceClassification,
                RemBertForTokenClassification,
                RemBertLayer,
                RemBertModel,
                RemBertPreTrainedModel,
                load_tf_weights_in_rembert,
            )

      try:
            if not is_tf_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_tf_rembert import (
                TF_REMBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
                TFRemBertForCausalLM,
                TFRemBertForMaskedLM,
                TFRemBertForMultipleChoice,
                TFRemBertForQuestionAnswering,
                TFRemBertForSequenceClassification,
                TFRemBertForTokenClassification,
                TFRemBertLayer,
                TFRemBertModel,
                TFRemBertPreTrainedModel,
            )


else:
      import sys

      A     :  Tuple					     =       _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)


