
from __future__ import annotations

import unittest

from transformers import DebertaConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
 import tensorflow as tf

 from transformers import (
     TFDebertaForMaskedLM,
     TFDebertaForQuestionAnswering,
     TFDebertaForSequenceClassification,
     TFDebertaForTokenClassification,
     TFDebertaModel,
 )


class   __lowerCAmelCase		:



      """simple docstring"""







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




      def 						snake_case_     ( self						:    str			):
          __lowercase						:     Any		      =    ids_tensor([self.batch_size, self.seq_length]			,    self.vocab_size			)

          __lowercase						:     Union[str, Any]		      =    None
          if self.use_input_mask:
              __lowercase						:     Union[str, Any]		      =    random_attention_mask([self.batch_size, self.seq_length]			)

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

          __lowercase						:     Tuple		      =    None
          __lowercase						:     Optional[Any]		      =    None
          __lowercase						:     Tuple		      =    None
          if self.use_labels:
              __lowercase						:     Union[str, Any]		      =    ids_tensor([self.batch_size]			,    self.type_sequence_label_size			)
              __lowercase						:     Any		      =    ids_tensor([self.batch_size, self.seq_length]			,    self.num_labels			)

          __lowercase						:     Tuple		      =    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			,    relative_attention=self.relative_attention			,    max_relative_positions=self.max_relative_positions			,    position_biased_input=self.position_biased_input			,    initializer_range=self.initializer_range			,    return_dict=_snake_case			,    )

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




      def 						snake_case_     ( self						:    Optional[int]			,    _snake_case						:    Dict			,    _snake_case						:    Optional[int]			,    _snake_case						:    str			,    _snake_case						:    Dict			,    _snake_case						:    str			,    _snake_case						:    Any			,    _snake_case						:    Optional[int]			):
          __lowercase						:     Any		      =    TFDebertaModel(config=_snake_case			)
          __lowercase						:     int		      =    {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

          __lowercase						:     Tuple		      =    [input_ids, input_mask]
          __lowercase						:     str		      =    model(_snake_case			)

          __lowercase						:     Optional[int]		      =    model(_snake_case			)

          self.parent.assertEqual(result.last_hidden_state.shape			,    (self.batch_size, self.seq_length, self.hidden_size)			)




      def 						snake_case_     ( self						:    str			,    _snake_case						:    Union[str, Any]			,    _snake_case						:    int			,    _snake_case						:    List[str]			,    _snake_case						:    str			,    _snake_case						:    Dict			,    _snake_case						:    Dict			,    _snake_case						:    List[str]			):
          __lowercase						:     Tuple		      =    TFDebertaForMaskedLM(config=_snake_case			)
          __lowercase						:     Union[str, Any]		      =    {
              '''input_ids''': input_ids,
              '''attention_mask''': input_mask,
              '''token_type_ids''': token_type_ids,
          }
          __lowercase						:     List[str]		      =    model(_snake_case			)
          self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.seq_length, self.vocab_size)			)




      def 						snake_case_     ( self						:    Optional[int]			,    _snake_case						:    Optional[int]			,    _snake_case						:    str			,    _snake_case						:    str			,    _snake_case						:    Tuple			,    _snake_case						:    Any			,    _snake_case						:    Optional[int]			,    _snake_case						:    Optional[int]			):
          __lowercase						:     Any		      =    self.num_labels
          __lowercase						:     List[str]		      =    TFDebertaForSequenceClassification(config=_snake_case			)
          __lowercase						:     Any		      =    {
              '''input_ids''': input_ids,
              '''attention_mask''': input_mask,
              '''token_type_ids''': token_type_ids,
          }

          __lowercase						:     Optional[Any]		      =    model(_snake_case			)
          self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.num_labels)			)




      def 						snake_case_     ( self						:    Optional[Any]			,    _snake_case						:    int			,    _snake_case						:    str			,    _snake_case						:    Optional[Any]			,    _snake_case						:    Any			,    _snake_case						:    Optional[Any]			,    _snake_case						:    List[Any]			,    _snake_case						:    List[str]			):
          __lowercase						:     Dict		      =    self.num_labels
          __lowercase						:     Any		      =    TFDebertaForTokenClassification(config=_snake_case			)
          __lowercase						:     List[Any]		      =    {
              '''input_ids''': input_ids,
              '''attention_mask''': input_mask,
              '''token_type_ids''': token_type_ids,
          }
          __lowercase						:     int		      =    model(_snake_case			)
          self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.seq_length, self.num_labels)			)




      def 						snake_case_     ( self						:    str			,    _snake_case						:    List[Any]			,    _snake_case						:    str			,    _snake_case						:    List[str]			,    _snake_case						:    Dict			,    _snake_case						:    Any			,    _snake_case						:    Optional[Any]			,    _snake_case						:    Optional[int]			):
          __lowercase						:     Dict		      =    TFDebertaForQuestionAnswering(config=_snake_case			)
          __lowercase						:     int		      =    {
              '''input_ids''': input_ids,
              '''attention_mask''': input_mask,
              '''token_type_ids''': token_type_ids,
          }

          __lowercase						:     Tuple		      =    model(_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 						snake_case_     ( self						:    Union[str, Any]			):
          __lowercase						:     List[str]		      =    self.prepare_config_and_inputs()
          (
              (
              __lowercase
          )      , (
              __lowercase
          )      , (
              __lowercase
          )      , (
              __lowercase
          )      , (
              __lowercase
          )      , (
              __lowercase
          )      , (
              __lowercase
          )      , 
          )						:     Any		      =    config_and_inputs
          __lowercase						:     int		      =    {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
          return config, inputs_dict






@require_tf
class   __lowerCAmelCase		(		lowerCAmelCase_	,	lowerCAmelCase_	,	unittest.TestCase ):



      """simple docstring"""







      A__		:     str       				=      (
          (
              TFDebertaModel,
              TFDebertaForMaskedLM,
              TFDebertaForQuestionAnswering,
              TFDebertaForSequenceClassification,
              TFDebertaForTokenClassification,
          )
          if is_tf_available()
          else ()
      )
      A__		:     Dict       				=      (
          {
              '''feature-extraction''': TFDebertaModel,
              '''fill-mask''': TFDebertaForMaskedLM,
              '''question-answering''': TFDebertaForQuestionAnswering,
              '''text-classification''': TFDebertaForSequenceClassification,
              '''token-classification''': TFDebertaForTokenClassification,
              '''zero-shot''': TFDebertaForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )

      A__		:     str       				=      False
      A__		:     List[Any]       				=      False




      def 						snake_case_     ( self						:    List[str]			):
          __lowercase						:     Optional[int]		      =    TFDebertaModelTester(self			)
          __lowercase						:     Any		      =    ConfigTester(self			,    config_class=_snake_case			,    hidden_size=37			)




      def 						snake_case_     ( self						:    Tuple			):
          self.config_tester.run_common_tests()




      def 						snake_case_     ( self						:    Optional[Any]			):
          __lowercase						:     Optional[Any]		      =    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*_snake_case			)




      def 						snake_case_     ( self						:    Optional[int]			):
          __lowercase						:     Any		      =    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_lm(*_snake_case			)




      def 						snake_case_     ( self						:    Any			):
          __lowercase						:     Any		      =    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(*_snake_case			)




      def 						snake_case_     ( self						:    Dict			):
          __lowercase						:     Optional[int]		      =    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_sequence_classification(*_snake_case			)




      def 						snake_case_     ( self						:    List[str]			):
          __lowercase						:     Dict		      =    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*_snake_case			)




      @slow
      def 						snake_case_     ( self						:    Any			):
          __lowercase						:     List[Any]		      =    TFDebertaModel.from_pretrained('''kamalkraj/deberta-base'''			)
          self.assertIsNotNone(_snake_case			)






@require_tf
class   __lowerCAmelCase		(		unittest.TestCase ):



      """simple docstring"""







      @unittest.skip(reason='''Model not available yet'''			)
      def 						snake_case_     ( self						:    List[str]			):
          pass




      @slow
      def 						snake_case_     ( self						:    Optional[int]			):
          __lowercase						:     Optional[int]		      =    TFDebertaModel.from_pretrained('''kamalkraj/deberta-base'''			)
          __lowercase						:     Union[str, Any]		      =    tf.constant([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]			)
          __lowercase						:     Tuple		      =    tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]			)
          __lowercase						:     List[Any]		      =    model(_snake_case			,    attention_mask=_snake_case			)[0]

          __lowercase						:     List[str]		      =    tf.constant(
              [
                  [
                      [-0.59_85_58_96, -0.80_55_25_66, -0.8_46_21_35],
                      [1.4_48_40_25, -0.93_48_37_94, -0.80_59_30_85],
                      [0.3_12_27_41, 0.00_31_60_59, -1.4_13_13_77],
                  ]
              ]			)
          tf.debugging.assert_near(output[:, 1:4, 1:4]			,    _snake_case			,    atol=1E-4			)




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


__lowerCAmelCase   :     Optional[int]      =						{"configuration_vit_hybrid": ["VIT_HYBRID_PRETRAINED_CONFIG_ARCHIVE_MAP", "ViTHybridConfig"]}

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 __lowerCAmelCase   :     List[Any]      =						[
     "VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST",
     "ViTHybridForImageClassification",
     "ViTHybridModel",
     "ViTHybridPreTrainedModel",
 ]

try:
 if not is_vision_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 __lowerCAmelCase   :     Tuple      =						["ViTHybridImageProcessor"]


if TYPE_CHECKING:
 from .configuration_vit_hybrid import VIT_HYBRID_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTHybridConfig

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_vit_hybrid import (
      VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST,
      ViTHybridForImageClassification,
      ViTHybridModel,
      ViTHybridPreTrainedModel,
  )

 try:
  if not is_vision_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .image_processing_vit_hybrid import ViTHybridImageProcessor


else:
 import sys

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


