from __future__ import annotations

import unittest

from transformers import DebertaVaConfig, 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 (
          TFDebertaVaForMaskedLM,
          TFDebertaVaForQuestionAnswering,
          TFDebertaVaForSequenceClassification,
          TFDebertaVaForTokenClassification,
          TFDebertaVaModel,
      )






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

     SCREAMING_SNAKE_CASE__:   List[Any]=      None
     if self.use_input_mask:
      SCREAMING_SNAKE_CASE__:   Optional[int]=      random_attention_mask([self.batch_size, self.seq_length]   )

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

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

     SCREAMING_SNAKE_CASE__:   str=      DebertaVaConfig(
         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 , position_biased_input=self.position_biased_input , initializer_range=self.initializer_range , return_dict=lowerCAmelCase , )

     return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase   )       ->					List[str]:
     SCREAMING_SNAKE_CASE__:   Dict=      TFDebertaVaModel(config=lowerCAmelCase   )
     SCREAMING_SNAKE_CASE__:   Any=      {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

     SCREAMING_SNAKE_CASE__:   Dict=      [input_ids, input_mask]
     SCREAMING_SNAKE_CASE__:   str=      model(lowerCAmelCase   )

     SCREAMING_SNAKE_CASE__:   str=      model(lowerCAmelCase   )

     self.parent.assertEqual(result.last_hidden_state.shape , (self.batch_size, self.seq_length, self.hidden_size)   )
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase   )       ->					Optional[Any]:
     SCREAMING_SNAKE_CASE__:   Optional[int]=      TFDebertaVaForMaskedLM(config=lowerCAmelCase   )
     SCREAMING_SNAKE_CASE__:   List[str]=      {
         '''input_ids''': input_ids,
         '''attention_mask''': input_mask,
         '''token_type_ids''': token_type_ids,
     }
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      model(lowerCAmelCase   )
     self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.vocab_size)   )
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase   )       ->					int:
     SCREAMING_SNAKE_CASE__:   str=      self.num_labels
     SCREAMING_SNAKE_CASE__:   Tuple=      TFDebertaVaForSequenceClassification(config=lowerCAmelCase   )
     SCREAMING_SNAKE_CASE__:   List[str]=      {
         '''input_ids''': input_ids,
         '''attention_mask''': input_mask,
         '''token_type_ids''': token_type_ids,
     }

     SCREAMING_SNAKE_CASE__:   Union[str, Any]=      model(lowerCAmelCase   )
     self.parent.assertEqual(result.logits.shape , (self.batch_size, self.num_labels)   )
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase   )       ->					Any:
     SCREAMING_SNAKE_CASE__:   Any=      self.num_labels
     SCREAMING_SNAKE_CASE__:   Any=      TFDebertaVaForTokenClassification(config=lowerCAmelCase   )
     SCREAMING_SNAKE_CASE__:   Union[str, Any]=      {
         '''input_ids''': input_ids,
         '''attention_mask''': input_mask,
         '''token_type_ids''': token_type_ids,
     }
     SCREAMING_SNAKE_CASE__:   Dict=      model(lowerCAmelCase   )
     self.parent.assertEqual(result.logits.shape , (self.batch_size, self.seq_length, self.num_labels)   )
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase   )       ->					Optional[int]:
     SCREAMING_SNAKE_CASE__:   Any=      TFDebertaVaForQuestionAnswering(config=lowerCAmelCase   )
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      {
         '''input_ids''': input_ids,
         '''attention_mask''': input_mask,
         '''token_type_ids''': token_type_ids,
     }

     SCREAMING_SNAKE_CASE__:   Optional[int]=      model(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 						UpperCamelCase_				(  self   )       ->					Any:
     SCREAMING_SNAKE_CASE__:   List[Any]=      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__
     ),	
     ):   Any=      config_and_inputs
     SCREAMING_SNAKE_CASE__:   List[Any]=      {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
     return config, inputs_dict






@require_tf
class 		_lowerCamelCase		(   UpperCamelCase_    ,     UpperCamelCase_    ,     unittest.TestCase    ):
    __a  =      (
        (
            TFDebertaVaModel,
            TFDebertaVaForMaskedLM,
            TFDebertaVaForQuestionAnswering,
            TFDebertaVaForSequenceClassification,
            TFDebertaVaForTokenClassification,
        )
        if is_tf_available()
        else ()
    )
    __a  =      (
        {
            "feature-extraction": TFDebertaVaModel,
            "fill-mask": TFDebertaVaForMaskedLM,
            "question-answering": TFDebertaVaForQuestionAnswering,
            "text-classification": TFDebertaVaForSequenceClassification,
            "token-classification": TFDebertaVaForTokenClassification,
            "zero-shot": TFDebertaVaForSequenceClassification,
        }
        if is_tf_available()
        else {}
    )

    __a  =      False
    __a  =      False
    def 						UpperCamelCase_				(  self   )       ->					Tuple:
     SCREAMING_SNAKE_CASE__:   List[str]=      TFDebertaVaModelTester(self   )
     SCREAMING_SNAKE_CASE__:   str=      ConfigTester(self , config_class=lowerCAmelCase , hidden_size=37   )
    def 						UpperCamelCase_				(  self   )       ->					Tuple:
     self.config_tester.run_common_tests()
    def 						UpperCamelCase_				(  self   )       ->					Optional[int]:
     SCREAMING_SNAKE_CASE__:   Dict=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowerCAmelCase   )
    def 						UpperCamelCase_				(  self   )       ->					Optional[Any]:
     SCREAMING_SNAKE_CASE__:   str=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase   )
    def 						UpperCamelCase_				(  self   )       ->					List[Any]:
     SCREAMING_SNAKE_CASE__:   Any=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase   )
    def 						UpperCamelCase_				(  self   )       ->					Union[str, Any]:
     SCREAMING_SNAKE_CASE__:   Tuple=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase   )
    def 						UpperCamelCase_				(  self   )       ->					List[str]:
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase   )



    @slow
    def 						UpperCamelCase_				(  self   )       ->					str:
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      TFDebertaVaModel.from_pretrained('''kamalkraj/deberta-v2-xlarge'''   )
     self.assertIsNotNone(lowerCAmelCase   )






@require_tf
class 		_lowerCamelCase		(   unittest.TestCase    ):
    @unittest.skip(reason='''Model not available yet'''   )
    def 						UpperCamelCase_				(  self   )       ->					Tuple:
     pass



    @slow
    def 						UpperCamelCase_				(  self   )       ->					Optional[int]:
     SCREAMING_SNAKE_CASE__:   Any=      TFDebertaVaModel.from_pretrained('''kamalkraj/deberta-v2-xlarge'''   )
     SCREAMING_SNAKE_CASE__:   Optional[int]=      tf.constant([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]   )
     SCREAMING_SNAKE_CASE__:   List[Any]=      tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
     SCREAMING_SNAKE_CASE__:   Dict=      model(lowerCAmelCase , attention_mask=lowerCAmelCase   )[0]

     SCREAMING_SNAKE_CASE__:   Any=      tf.constant(
         [[[0.2356, 0.1948, 0.0369], [-0.1063, 0.3586, -0.5152], [-0.6399, -0.0259, -0.2525]]]   )
     tf.debugging.assert_near(output[:, 1:4, 1:4] , lowerCAmelCase , atol=1e-4   )


from typing import TYPE_CHECKING

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


lowercase_     :   List[str] 				=				{
    'configuration_maskformer': ['MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MaskFormerConfig'],
    'configuration_maskformer_swin': ['MaskFormerSwinConfig'],
}

try:
      if not is_vision_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      lowercase_     :   Optional[Any] 				=				['MaskFormerFeatureExtractor']
      lowercase_     :   Dict 				=				['MaskFormerImageProcessor']


try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      lowercase_     :   str 				=				[
          'MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST',
          'MaskFormerForInstanceSegmentation',
          'MaskFormerModel',
          'MaskFormerPreTrainedModel',
      ]
      lowercase_     :   Optional[Any] 				=				[
          'MaskFormerSwinBackbone',
          'MaskFormerSwinModel',
          'MaskFormerSwinPreTrainedModel',
      ]

if TYPE_CHECKING:
      from .configuration_maskformer import MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskFormerConfig
      from .configuration_maskformer_swin import MaskFormerSwinConfig

      try:
            if not is_vision_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .feature_extraction_maskformer import MaskFormerFeatureExtractor
            from .image_processing_maskformer import MaskFormerImageProcessor
      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_maskformer import (
                MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
                MaskFormerForInstanceSegmentation,
                MaskFormerModel,
                MaskFormerPreTrainedModel,
            )
            from .modeling_maskformer_swin import (
                MaskFormerSwinBackbone,
                MaskFormerSwinModel,
                MaskFormerSwinPreTrainedModel,
            )


else:
      import sys

      lowercase_     :   Any 				=				_LazyModule(__name__, globals()['__file__'], _import_structure)

