

"""simple docstring"""

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







						def __init__(self  ,				UpperCAmelCase_  ,				UpperCAmelCase_=1_3  ,				UpperCAmelCase_=7  ,				UpperCAmelCase_=True  ,				UpperCAmelCase_=True  ,				UpperCAmelCase_=True  ,				UpperCAmelCase_=True  ,				UpperCAmelCase_=9_9  ,				UpperCAmelCase_=3_2  ,				UpperCAmelCase_=2  ,				UpperCAmelCase_=4  ,				UpperCAmelCase_=3_7  ,				UpperCAmelCase_="gelu"  ,				UpperCAmelCase_=0.1  ,				UpperCAmelCase_=0.1  ,				UpperCAmelCase_=5_1_2  ,				UpperCAmelCase_=1_6  ,				UpperCAmelCase_=2  ,				UpperCAmelCase_=0.0_2  ,				UpperCAmelCase_=3  ,				UpperCAmelCase_=4  ,				UpperCAmelCase_=None  ,				)      ->    Optional[int]:
											__lowercase        =      parent
											__lowercase        =      1_3
											__lowercase        =      7
											__lowercase        =      True
											__lowercase        =      True
											__lowercase        =      True
											__lowercase        =      True
											__lowercase        =      9_9
											__lowercase        =      3_2
											__lowercase        =      2
											__lowercase        =      4
											__lowercase        =      3_7
											__lowercase        =      '''gelu'''
											__lowercase        =      0.1
											__lowercase        =      0.1
											__lowercase        =      5_1_2
											__lowercase        =      1_6
											__lowercase        =      False
											__lowercase        =      -1
											__lowercase        =      True
											__lowercase        =      2
											__lowercase        =      0.0_2
											__lowercase        =      3
											__lowercase        =      4
											__lowercase        =      None







						def 							_UpperCamelCase     (self				)      ->    List[str]:
											__lowercase        =      ids_tensor([self.batch_size, self.seq_length]  ,				self.vocab_size				)

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

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

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

											__lowercase        =      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=UpperCAmelCase_  ,				)

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







						def 							_UpperCamelCase     (self  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_				)      ->    List[Any]:
											__lowercase        =      TFDebertaModel(config=UpperCAmelCase_				)
											__lowercase        =      {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

											__lowercase        =      [input_ids, input_mask]
											__lowercase        =      model(UpperCAmelCase_				)

											__lowercase        =      model(UpperCAmelCase_				)

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







						def 							_UpperCamelCase     (self  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_				)      ->    int:
											__lowercase        =      TFDebertaForMaskedLM(config=UpperCAmelCase_				)
											__lowercase        =      {
											    '''input_ids''': input_ids,
											    '''attention_mask''': input_mask,
											    '''token_type_ids''': token_type_ids,
											}
											__lowercase        =      model(UpperCAmelCase_				)
											self.parent.assertEqual(result.logits.shape  ,				(self.batch_size, self.seq_length, self.vocab_size)				)







						def 							_UpperCamelCase     (self  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_				)      ->    List[Any]:
											__lowercase        =      self.num_labels
											__lowercase        =      TFDebertaForSequenceClassification(config=UpperCAmelCase_				)
											__lowercase        =      {
											    '''input_ids''': input_ids,
											    '''attention_mask''': input_mask,
											    '''token_type_ids''': token_type_ids,
											}

											__lowercase        =      model(UpperCAmelCase_				)
											self.parent.assertEqual(result.logits.shape  ,				(self.batch_size, self.num_labels)				)







						def 							_UpperCamelCase     (self  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_				)      ->    Optional[Any]:
											__lowercase        =      self.num_labels
											__lowercase        =      TFDebertaForTokenClassification(config=UpperCAmelCase_				)
											__lowercase        =      {
											    '''input_ids''': input_ids,
											    '''attention_mask''': input_mask,
											    '''token_type_ids''': token_type_ids,
											}
											__lowercase        =      model(UpperCAmelCase_				)
											self.parent.assertEqual(result.logits.shape  ,				(self.batch_size, self.seq_length, self.num_labels)				)







						def 							_UpperCamelCase     (self  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_  ,				UpperCAmelCase_				)      ->    int:
											__lowercase        =      TFDebertaForQuestionAnswering(config=UpperCAmelCase_				)
											__lowercase        =      {
											    '''input_ids''': input_ids,
											    '''attention_mask''': input_mask,
											    '''token_type_ids''': token_type_ids,
											}

											__lowercase        =      model(UpperCAmelCase_				)
											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				)      ->    Optional[Any]:
											__lowercase        =      self.prepare_config_and_inputs()
											(
											    (
											    __lowercase
											)    ,			(
											    __lowercase
											)    ,			(
											    __lowercase
											)    ,			(
											    __lowercase
											)    ,			(
											    __lowercase
											)    ,			(
											    __lowercase
											)    ,			(
											    __lowercase
											)    ,			
											)        =      config_and_inputs
											__lowercase        =      {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
											return config, inputs_dict



@require_tf
class       _snake_case  (    UpperCamelCase    ,      UpperCamelCase    ,      unittest.TestCase							):
						__snake_case			      =  (
						    (
						        TFDebertaModel,
						        TFDebertaForMaskedLM,
						        TFDebertaForQuestionAnswering,
						        TFDebertaForSequenceClassification,
						        TFDebertaForTokenClassification,
						    )
						    if is_tf_available()
						    else ()
						)
						__snake_case			      =  (
						    {
						        'feature-extraction': TFDebertaModel,
						        'fill-mask': TFDebertaForMaskedLM,
						        'question-answering': TFDebertaForQuestionAnswering,
						        'text-classification': TFDebertaForSequenceClassification,
						        'token-classification': TFDebertaForTokenClassification,
						        'zero-shot': TFDebertaForSequenceClassification,
						    }
						    if is_tf_available()
						    else {}
						)

						__snake_case			      =  False
						__snake_case			      =  False







						def 							_UpperCamelCase     (self				)      ->    Optional[int]:
											__lowercase        =      TFDebertaModelTester(self				)
											__lowercase        =      ConfigTester(self  ,				config_class=UpperCAmelCase_  ,				hidden_size=3_7				)







						def 							_UpperCamelCase     (self				)      ->    str:
											self.config_tester.run_common_tests()







						def 							_UpperCamelCase     (self				)      ->    Tuple:
											__lowercase        =      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*UpperCAmelCase_				)







						def 							_UpperCamelCase     (self				)      ->    str:
											__lowercase        =      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_lm(*UpperCAmelCase_				)







						def 							_UpperCamelCase     (self				)      ->    Optional[Any]:
											__lowercase        =      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_question_answering(*UpperCAmelCase_				)







						def 							_UpperCamelCase     (self				)      ->    Optional[int]:
											__lowercase        =      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_sequence_classification(*UpperCAmelCase_				)







						def 							_UpperCamelCase     (self				)      ->    Optional[Any]:
											__lowercase        =      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase_				)







						@slow
						def 							_UpperCamelCase     (self				)      ->    int:
											__lowercase        =      TFDebertaModel.from_pretrained('''kamalkraj/deberta-base'''				)
											self.assertIsNotNone(UpperCAmelCase_				)



@require_tf
class       _snake_case  (    unittest.TestCase							):







						@unittest.skip(reason='''Model not available yet'''				)
						def 							_UpperCamelCase     (self				)      ->    int:
											pass







						@slow
						def 							_UpperCamelCase     (self				)      ->    Optional[int]:
											__lowercase        =      TFDebertaModel.from_pretrained('''kamalkraj/deberta-base'''				)
											__lowercase        =      tf.constant([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]]				)
											__lowercase        =      tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]				)
											__lowercase        =      model(UpperCAmelCase_  ,				attention_mask=UpperCAmelCase_				)[0]

											__lowercase        =      tf.constant(
											    [
											        [
											            [-0.5_9_8_5_5_8_9_6, -0.8_0_5_5_2_5_6_6, -0.8_4_6_2_1_3_5],
											            [1.4_4_8_4_0_2_5, -0.9_3_4_8_3_7_9_4, -0.8_0_5_9_3_0_8_5],
											            [0.3_1_2_2_7_4_1, 0.0_0_3_1_6_0_5_9, -1.4_1_3_1_3_7_7],
											        ]
											    ]				)
											tf.debugging.assert_near(output[:, 1:4, 1:4]  ,				UpperCAmelCase_  ,				atol=1e-4				)





"""simple docstring"""

from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_torch_available,
    is_vision_available,
)


a__			:						Tuple							=			{
    """configuration_segformer""": ["""SEGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SegformerConfig""", """SegformerOnnxConfig"""]
}

try:
							if not is_vision_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							a__			:						List[str]							=			["""SegformerFeatureExtractor"""]
							a__			:						str							=			["""SegformerImageProcessor"""]

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							a__			:						Optional[Any]							=			[
							    """SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
							    """SegformerDecodeHead""",
							    """SegformerForImageClassification""",
							    """SegformerForSemanticSegmentation""",
							    """SegformerLayer""",
							    """SegformerModel""",
							    """SegformerPreTrainedModel""",
							]

try:
							if not is_tf_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							a__			:						List[Any]							=			[
							    """TF_SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
							    """TFSegformerDecodeHead""",
							    """TFSegformerForImageClassification""",
							    """TFSegformerForSemanticSegmentation""",
							    """TFSegformerModel""",
							    """TFSegformerPreTrainedModel""",
							]


if TYPE_CHECKING:
							from .configuration_segformer import SEGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, SegformerConfig, SegformerOnnxConfig

							try:
														if not is_vision_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .feature_extraction_segformer import SegformerFeatureExtractor
														from .image_processing_segformer import SegformerImageProcessor

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_segformer import (
														    SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
														    SegformerDecodeHead,
														    SegformerForImageClassification,
														    SegformerForSemanticSegmentation,
														    SegformerLayer,
														    SegformerModel,
														    SegformerPreTrainedModel,
														)
							try:
														if not is_tf_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_tf_segformer import (
														    TF_SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
														    TFSegformerDecodeHead,
														    TFSegformerForImageClassification,
														    TFSegformerForSemanticSegmentation,
														    TFSegformerModel,
														    TFSegformerPreTrainedModel,
														)

else:
							import sys

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


