




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				:







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




									"""simple docstring"""


									_UpperCAmelCase									=  parent
									_UpperCAmelCase									=  13
									_UpperCAmelCase									=  7
									_UpperCAmelCase									=  True
									_UpperCAmelCase									=  True
									_UpperCAmelCase									=  True
									_UpperCAmelCase									=  True
									_UpperCAmelCase									=  99
									_UpperCAmelCase									=  32
									_UpperCAmelCase									=  2
									_UpperCAmelCase									=  4
									_UpperCAmelCase									=  37
									_UpperCAmelCase									=  "gelu"
									_UpperCAmelCase									=  0.1
									_UpperCAmelCase									=  0.1
									_UpperCAmelCase									=  512
									_UpperCAmelCase									=  16
									_UpperCAmelCase									=  False
									_UpperCAmelCase									=  -1
									_UpperCAmelCase									=  True
									_UpperCAmelCase									=  2
									_UpperCAmelCase									=  0.02
									_UpperCAmelCase									=  3
									_UpperCAmelCase									=  4
									_UpperCAmelCase									=  None







			def UpperCamelCase    ( self					:    List[Any]   ):




									"""simple docstring"""


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

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

									_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									=  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=lowerCAmelCase__					,					)

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







			def UpperCamelCase    ( self					:    Dict					,					snake_case__					:    List[Any]					,					snake_case__					:    Optional[Any]					,					snake_case__					:    Optional[Any]					,					snake_case__					:    Optional[Any]					,					snake_case__					:    Union[str, Any]					,					snake_case__					:    List[Any]					,					snake_case__					:    Any   ):




									"""simple docstring"""


									_UpperCAmelCase									=  TFDebertaModel(config=lowerCAmelCase__   )
									_UpperCAmelCase									=  {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

									_UpperCAmelCase									=  [input_ids, input_mask]
									_UpperCAmelCase									=  model(lowerCAmelCase__   )

									_UpperCAmelCase									=  model(lowerCAmelCase__   )

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







			def UpperCamelCase    ( self					:    Union[str, Any]					,					snake_case__					:    Optional[Any]					,					snake_case__					:    Optional[Any]					,					snake_case__					:    Union[str, Any]					,					snake_case__					:    Optional[int]					,					snake_case__					:    Dict					,					snake_case__					:    Optional[Any]					,					snake_case__					:    List[str]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  TFDebertaForMaskedLM(config=lowerCAmelCase__   )
									_UpperCAmelCase									=  {
									    "input_ids": input_ids,
									    "attention_mask": input_mask,
									    "token_type_ids": token_type_ids,
									}
									_UpperCAmelCase									=  model(lowerCAmelCase__   )
									self.parent.assertEqual(result.logits.shape					,					(self.batch_size, self.seq_length, self.vocab_size)   )







			def UpperCamelCase    ( self					:    Tuple					,					snake_case__					:    Union[str, Any]					,					snake_case__					:    Optional[int]					,					snake_case__					:    str					,					snake_case__					:    Optional[Any]					,					snake_case__					:    Any					,					snake_case__					:    Any					,					snake_case__					:    int   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.num_labels
									_UpperCAmelCase									=  TFDebertaForSequenceClassification(config=lowerCAmelCase__   )
									_UpperCAmelCase									=  {
									    "input_ids": input_ids,
									    "attention_mask": input_mask,
									    "token_type_ids": token_type_ids,
									}

									_UpperCAmelCase									=  model(lowerCAmelCase__   )
									self.parent.assertEqual(result.logits.shape					,					(self.batch_size, self.num_labels)   )







			def UpperCamelCase    ( self					:    Optional[Any]					,					snake_case__					:    Any					,					snake_case__					:    Any					,					snake_case__					:    Any					,					snake_case__					:    str					,					snake_case__					:    List[Any]					,					snake_case__					:    Dict					,					snake_case__					:    Any   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.num_labels
									_UpperCAmelCase									=  TFDebertaForTokenClassification(config=lowerCAmelCase__   )
									_UpperCAmelCase									=  {
									    "input_ids": input_ids,
									    "attention_mask": input_mask,
									    "token_type_ids": token_type_ids,
									}
									_UpperCAmelCase									=  model(lowerCAmelCase__   )
									self.parent.assertEqual(result.logits.shape					,					(self.batch_size, self.seq_length, self.num_labels)   )







			def UpperCamelCase    ( self					:    List[Any]					,					snake_case__					:    Any					,					snake_case__					:    str					,					snake_case__					:    Any					,					snake_case__					:    Optional[Any]					,					snake_case__					:    str					,					snake_case__					:    List[str]					,					snake_case__					:    Dict   ):




									"""simple docstring"""


									_UpperCAmelCase									=  TFDebertaForQuestionAnswering(config=lowerCAmelCase__   )
									_UpperCAmelCase									=  {
									    "input_ids": input_ids,
									    "attention_mask": input_mask,
									    "token_type_ids": token_type_ids,
									}

									_UpperCAmelCase									=  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					:    List[Any]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.prepare_config_and_inputs()
									(
									    _UpperCAmelCase							
									)									=  config_and_inputs
									_UpperCAmelCase									=  {"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				):
			snake_case_   :				str				  =  (
			    (
			        TFDebertaModel,
			        TFDebertaForMaskedLM,
			        TFDebertaForQuestionAnswering,
			        TFDebertaForSequenceClassification,
			        TFDebertaForTokenClassification,
			    )
			    if is_tf_available()
			    else ()
			)
			snake_case_   :				Dict				  =  (
			    {
			        """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_   :				str				  =  False
			snake_case_   :				Optional[Any]				  =  False







			def UpperCamelCase    ( self					:    Tuple   ):




									"""simple docstring"""


									_UpperCAmelCase									=  TFDebertaModelTester(self   )
									_UpperCAmelCase									=  ConfigTester(self					,					config_class=lowerCAmelCase__					,					hidden_size=37   )







			def UpperCamelCase    ( self					:    Any   ):




									"""simple docstring"""


									self.config_tester.run_common_tests()







			def UpperCamelCase    ( self					:    List[Any]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*lowerCAmelCase__   )







			def UpperCamelCase    ( self					:    Tuple   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase__   )







			def UpperCamelCase    ( self					:    str   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase__   )







			def UpperCamelCase    ( self					:    Optional[int]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase__   )







			def UpperCamelCase    ( self					:    Optional[int]   ):




									"""simple docstring"""


									_UpperCAmelCase									=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase__   )







			@slow
			def UpperCamelCase    ( self					:    Any   ):




									"""simple docstring"""


									_UpperCAmelCase									=  TFDebertaModel.from_pretrained("kamalkraj/deberta-base"   )
									self.assertIsNotNone(lowerCAmelCase__   )






@require_tf
class 					__lowerCAmelCase				( unittest.TestCase				):







			@unittest.skip(reason="Model not available yet"   )
			def UpperCamelCase    ( self					:    Tuple   ):




									"""simple docstring"""


									pass







			@slow
			def UpperCamelCase    ( self					:    Dict   ):




									"""simple docstring"""


									_UpperCAmelCase									=  TFDebertaModel.from_pretrained("kamalkraj/deberta-base"   )
									_UpperCAmelCase									=  tf.constant([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]   )
									_UpperCAmelCase									=  tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
									_UpperCAmelCase									=  model(lowerCAmelCase__					,					attention_mask=lowerCAmelCase__   )[0]

									_UpperCAmelCase									=  tf.constant(
									    [
									        [
									            [-0.59_855_896, -0.80_552_566, -0.8_462_135],
									            [1.4_484_025, -0.93_483_794, -0.80_593_085],
									            [0.3_122_741, 0.00_316_059, -1.4_131_377],
									        ]
									    ]   )
									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,
)


lowercase_			:       List[str]						       =				{
    'configuration_rwkv': ['RWKV_PRETRAINED_CONFIG_ARCHIVE_MAP', 'RwkvConfig', 'RwkvOnnxConfig'],
}

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				lowercase_			:       Any						       =				[
				    'RWKV_PRETRAINED_MODEL_ARCHIVE_LIST',
				    'RwkvForCausalLM',
				    'RwkvModel',
				    'RwkvPreTrainedModel',
				]


if TYPE_CHECKING:
				from .configuration_rwkv import RWKV_PRETRAINED_CONFIG_ARCHIVE_MAP, RwkvConfig, RwkvOnnxConfig

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_rwkv import (
								    RWKV_PRETRAINED_MODEL_ARCHIVE_LIST,
								    RwkvForCausalLM,
								    RwkvModel,
								    RwkvPreTrainedModel,
								)
else:
				import sys

				lowercase_			:       Union[str, Any]						       =				_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




