





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



      def __init__(	self:						Dict		,  _lowerCAmelCase:						Optional[int]		,  _lowerCAmelCase:						Union[str, Any]=13		,  _lowerCAmelCase:						Dict=7		,  _lowerCAmelCase:						Union[str, Any]=True		,  _lowerCAmelCase:						Optional[Any]=True		,  _lowerCAmelCase:						Dict=True		,  _lowerCAmelCase:						str=True		,  _lowerCAmelCase:						int=99		,  _lowerCAmelCase:						Tuple=32		,  _lowerCAmelCase:						int=5		,  _lowerCAmelCase:						Any=4		,  _lowerCAmelCase:						Union[str, Any]=37		,  _lowerCAmelCase:						List[Any]="gelu"		,  _lowerCAmelCase:						Optional[int]=0.1		,  _lowerCAmelCase:						List[Any]=0.1		,  _lowerCAmelCase:						Dict=512		,  _lowerCAmelCase:						Optional[int]=16		,  _lowerCAmelCase:						str=2		,  _lowerCAmelCase:						List[Any]=0.02		,  _lowerCAmelCase:						Tuple=False		,  _lowerCAmelCase:						Any=True		,  _lowerCAmelCase:						str="None"		,  _lowerCAmelCase:						Optional[int]=3		,  _lowerCAmelCase:						int=4		,  _lowerCAmelCase:						int=None		,  )						->    Optional[int]:
         '''simple docstring'''

         UpperCAmelCase_											=parent
         UpperCAmelCase_											=batch_size
         UpperCAmelCase_											=seq_length
         UpperCAmelCase_											=is_training
         UpperCAmelCase_											=use_input_mask
         UpperCAmelCase_											=use_token_type_ids
         UpperCAmelCase_											=use_labels
         UpperCAmelCase_											=vocab_size
         UpperCAmelCase_											=hidden_size
         UpperCAmelCase_											=num_hidden_layers
         UpperCAmelCase_											=num_attention_heads
         UpperCAmelCase_											=intermediate_size
         UpperCAmelCase_											=hidden_act
         UpperCAmelCase_											=hidden_dropout_prob
         UpperCAmelCase_											=attention_probs_dropout_prob
         UpperCAmelCase_											=max_position_embeddings
         UpperCAmelCase_											=type_vocab_size
         UpperCAmelCase_											=type_sequence_label_size
         UpperCAmelCase_											=initializer_range
         UpperCAmelCase_											=num_labels
         UpperCAmelCase_											=num_choices
         UpperCAmelCase_											=relative_attention
         UpperCAmelCase_											=position_biased_input
         UpperCAmelCase_											=pos_att_type
         UpperCAmelCase_											=scope



      def 	lowerCAmelCase__    (	self:						List[str]       )						->    str:
         '''simple docstring'''

         UpperCAmelCase_											=ids_tensor([self.batch_size, self.seq_length]		,  self.vocab_size       )

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

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

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

         UpperCAmelCase_											=self.get_config()

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



      def 	lowerCAmelCase__    (	self:						List[str]       )						->    int:
         '''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 	lowerCAmelCase__    (	self:						Optional[Any]       )						->    str:
         '''simple docstring'''

         UpperCAmelCase_											=self.get_config()
         UpperCAmelCase_											=300
         return config



      def 	lowerCAmelCase__    (	self:						int		,  _lowerCAmelCase:						List[str]       )						->    List[str]:
         '''simple docstring'''

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



      def 	lowerCAmelCase__    (	self:						Any		,  _lowerCAmelCase:						List[Any]		,  _lowerCAmelCase:						Union[str, Any]		,  _lowerCAmelCase:						Tuple		,  _lowerCAmelCase:						int		,  _lowerCAmelCase:						Optional[Any]		,  _lowerCAmelCase:						List[Any]		,  _lowerCAmelCase:						List[str]       )						->    Dict:
         '''simple docstring'''

         UpperCAmelCase_											=DebertaModel(config=_lowerCAmelCase       )
         model.to(_lowerCAmelCase       )
         model.eval()
         UpperCAmelCase_											=model(_lowerCAmelCase		,  attention_mask=_lowerCAmelCase		,  token_type_ids=_lowerCAmelCase       )[0]
         UpperCAmelCase_											=model(_lowerCAmelCase		,  token_type_ids=_lowerCAmelCase       )[0]
         UpperCAmelCase_											=model(_lowerCAmelCase       )[0]

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



      def 	lowerCAmelCase__    (	self:						Union[str, Any]		,  _lowerCAmelCase:						Tuple		,  _lowerCAmelCase:						Optional[int]		,  _lowerCAmelCase:						Optional[Any]		,  _lowerCAmelCase:						int		,  _lowerCAmelCase:						Dict		,  _lowerCAmelCase:						Dict		,  _lowerCAmelCase:						Any       )						->    Dict:
         '''simple docstring'''

         UpperCAmelCase_											=DebertaForMaskedLM(config=_lowerCAmelCase       )
         model.to(_lowerCAmelCase       )
         model.eval()
         UpperCAmelCase_											=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 	lowerCAmelCase__    (	self:						Tuple		,  _lowerCAmelCase:						Any		,  _lowerCAmelCase:						Optional[Any]		,  _lowerCAmelCase:						Union[str, Any]		,  _lowerCAmelCase:						List[Any]		,  _lowerCAmelCase:						Optional[Any]		,  _lowerCAmelCase:						Optional[Any]		,  _lowerCAmelCase:						Tuple       )						->    Union[str, Any]:
         '''simple docstring'''

         UpperCAmelCase_											=self.num_labels
         UpperCAmelCase_											=DebertaForSequenceClassification(_lowerCAmelCase       )
         model.to(_lowerCAmelCase       )
         model.eval()
         UpperCAmelCase_											=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 	lowerCAmelCase__    (	self:						int		,  _lowerCAmelCase:						str		,  _lowerCAmelCase:						int		,  _lowerCAmelCase:						Dict		,  _lowerCAmelCase:						Union[str, Any]		,  _lowerCAmelCase:						Optional[Any]		,  _lowerCAmelCase:						Dict		,  _lowerCAmelCase:						Dict       )						->    Optional[Any]:
         '''simple docstring'''

         UpperCAmelCase_											=self.num_labels
         UpperCAmelCase_											=DebertaForTokenClassification(config=_lowerCAmelCase       )
         model.to(_lowerCAmelCase       )
         model.eval()
         UpperCAmelCase_											=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 	lowerCAmelCase__    (	self:						Any		,  _lowerCAmelCase:						Dict		,  _lowerCAmelCase:						List[Any]		,  _lowerCAmelCase:						List[Any]		,  _lowerCAmelCase:						Optional[Any]		,  _lowerCAmelCase:						Any		,  _lowerCAmelCase:						Optional[int]		,  _lowerCAmelCase:						List[Any]       )						->    Optional[int]:
         '''simple docstring'''

         UpperCAmelCase_											=DebertaForQuestionAnswering(config=_lowerCAmelCase       )
         model.to(_lowerCAmelCase       )
         model.eval()
         UpperCAmelCase_											=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 	lowerCAmelCase__    (	self:						int       )						->    Any:
         '''simple docstring'''

         UpperCAmelCase_											=self.prepare_config_and_inputs()
         (
             (
             UpperCAmelCase_
         )						,       (
             UpperCAmelCase_
         )						,       (
             UpperCAmelCase_
         )						,       (
             UpperCAmelCase_
         )						,       (
             UpperCAmelCase_
         )						,       (
             UpperCAmelCase_
         )						,       (
             UpperCAmelCase_
         )						,       
         )											=config_and_inputs
         UpperCAmelCase_											={"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						):
      _snake_case							     =(
          (
              DebertaModel,
              DebertaForMaskedLM,
              DebertaForSequenceClassification,
              DebertaForTokenClassification,
              DebertaForQuestionAnswering,
          )
          if is_torch_available()
          else ()
      )
      _snake_case							     =(
          {
              '''feature-extraction''': DebertaModel,
              '''fill-mask''': DebertaForMaskedLM,
              '''question-answering''': DebertaForQuestionAnswering,
              '''text-classification''': DebertaForSequenceClassification,
              '''token-classification''': DebertaForTokenClassification,
              '''zero-shot''': DebertaForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )

      _snake_case							     =True
      _snake_case							     =False
      _snake_case							     =False
      _snake_case							     =False
      _snake_case							     =False



      def 	lowerCAmelCase__    (	self:						Union[str, Any]       )						->    List[Any]:
         '''simple docstring'''

         UpperCAmelCase_											=DebertaModelTester(self       )
         UpperCAmelCase_											=ConfigTester(self		,  config_class=_lowerCAmelCase		,  hidden_size=37       )



      def 	lowerCAmelCase__    (	self:						List[Any]       )						->    List[Any]:
         '''simple docstring'''

         self.config_tester.run_common_tests()



      def 	lowerCAmelCase__    (	self:						int       )						->    Union[str, Any]:
         '''simple docstring'''

         UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_model(*_lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						Union[str, Any]       )						->    str:
         '''simple docstring'''

         UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_sequence_classification(*_lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						Optional[Any]       )						->    Optional[int]:
         '''simple docstring'''

         UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_masked_lm(*_lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						Optional[int]       )						->    List[str]:
         '''simple docstring'''

         UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_question_answering(*_lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						Any       )						->    Any:
         '''simple docstring'''

         UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_token_classification(*_lowerCAmelCase       )






      @slow
      def 	lowerCAmelCase__    (	self:						Optional[Any]       )						->    Tuple:
         '''simple docstring'''

         for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            UpperCAmelCase_											=DebertaModel.from_pretrained(_lowerCAmelCase       )
            self.assertIsNotNone(_lowerCAmelCase       )

@require_torch
@require_sentencepiece
@require_tokenizers
class      A       (	unittest.TestCase						):



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

         pass






      @slow
      def 	lowerCAmelCase__    (	self:						str       )						->    List[str]:
         '''simple docstring'''

         UpperCAmelCase_											=DebertaModel.from_pretrained("microsoft/deberta-base"       )

         UpperCAmelCase_											=torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]       )
         UpperCAmelCase_											=torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]       )
         with torch.no_grad():
            UpperCAmelCase_											=model(_lowerCAmelCase		,  attention_mask=_lowerCAmelCase       )[0]
         # compare the actual values for a slice.
         UpperCAmelCase_											=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]		,  _lowerCAmelCase		,  atol=1e-4       )		,  F'{output[:, 1:4, 1:4]}'       )











from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__lowercase  :   Any						      ={
    """configuration_upernet""": ["""UperNetConfig"""],
}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     __lowercase  :   Tuple						      =[
         """UperNetForSemanticSegmentation""",
         """UperNetPreTrainedModel""",
     ]


if TYPE_CHECKING:
     from .configuration_upernet import UperNetConfig

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_upernet import UperNetForSemanticSegmentation, UperNetPreTrainedModel


else:
     import sys

     __lowercase  :   Any						      =_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)




