

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



   def __init__(				self				:	int,						__A				:	Union[str, Any],						__A				:	int=1_3,						__A				:	Union[str, Any]=7,						__A				:	Dict=True,						__A				:	Any=True,						__A				:	Any=True,						__A				:	str=True,						__A				:	str=9_9,						__A				:	Optional[Any]=3_2,						__A				:	Any=2,						__A				:	Union[str, Any]=4,						__A				:	Optional[Any]=3_7,						__A				:	List[Any]="gelu",						__A				:	Optional[Any]=0.1,						__A				:	int=0.1,						__A				:	List[Any]=5_1_2,						__A				:	List[str]=1_6,						__A				:	Any=2,						__A				:	Dict=0.0_2,						__A				:	Optional[int]=3,						__A				:	List[str]=4,						__A				:	List[Any]=None,						):
     UpperCAmelCase			:				Any										=					parent
     UpperCAmelCase			:				Any										=					1_3
     UpperCAmelCase			:				Tuple										=					7
     UpperCAmelCase			:				Optional[Any]										=					True
     UpperCAmelCase			:				List[Any]										=					True
     UpperCAmelCase			:				List[Any]										=					True
     UpperCAmelCase			:				Tuple										=					True
     UpperCAmelCase			:				Dict										=					9_9
     UpperCAmelCase			:				Optional[int]										=					3_2
     UpperCAmelCase			:				str										=					2
     UpperCAmelCase			:				Tuple										=					4
     UpperCAmelCase			:				Tuple										=					3_7
     UpperCAmelCase			:				Tuple										=					'''gelu'''
     UpperCAmelCase			:				int										=					0.1
     UpperCAmelCase			:				Optional[Any]										=					0.1
     UpperCAmelCase			:				Optional[int]										=					5_1_2
     UpperCAmelCase			:				str										=					1_6
     UpperCAmelCase			:				Tuple										=					False
     UpperCAmelCase			:				Union[str, Any]										=					-1
     UpperCAmelCase			:				Any										=					True
     UpperCAmelCase			:				Optional[Any]										=					2
     UpperCAmelCase			:				List[Any]										=					0.0_2
     UpperCAmelCase			:				Optional[Any]										=					3
     UpperCAmelCase			:				Tuple										=					4
     UpperCAmelCase			:				str										=					None



   def 			__magic_name__       (				self				:	int		):
     UpperCAmelCase			:				Optional[Any]										=					ids_tensor([self.batch_size, self.seq_length],						self.vocab_size		)

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

     UpperCAmelCase			:				int										=					None
     if self.use_token_type_ids:
       UpperCAmelCase			:				List[Any]										=					ids_tensor([self.batch_size, self.seq_length],						self.type_vocab_size		)

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

     UpperCAmelCase			:				List[Any]										=					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 			__magic_name__       (				self				:	str,						__A				:	Any,						__A				:	List[Any],						__A				:	Union[str, Any],						__A				:	List[str],						__A				:	Optional[int],						__A				:	Union[str, Any],						__A				:	Tuple		):
     UpperCAmelCase			:				List[str]										=					TFDebertaModel(config=UpperCAmelCase_		)
     UpperCAmelCase			:				List[str]										=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

     UpperCAmelCase			:				str										=					[input_ids, input_mask]
     UpperCAmelCase			:				Dict										=					model(UpperCAmelCase_		)

     UpperCAmelCase			:				Union[str, Any]										=					model(UpperCAmelCase_		)

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



   def 			__magic_name__       (				self				:	Dict,						__A				:	Union[str, Any],						__A				:	List[Any],						__A				:	Optional[int],						__A				:	Optional[Any],						__A				:	Any,						__A				:	Optional[Any],						__A				:	List[Any]		):
     UpperCAmelCase			:				Optional[int]										=					TFDebertaForMaskedLM(config=UpperCAmelCase_		)
     UpperCAmelCase			:				int										=					{
         '''input_ids''': input_ids,
         '''attention_mask''': input_mask,
         '''token_type_ids''': token_type_ids,
     }
     UpperCAmelCase			:				Optional[Any]										=					model(UpperCAmelCase_		)
     self.parent.assertEqual(result.logits.shape,						(self.batch_size, self.seq_length, self.vocab_size)		)



   def 			__magic_name__       (				self				:	Dict,						__A				:	Tuple,						__A				:	Dict,						__A				:	Any,						__A				:	Union[str, Any],						__A				:	Optional[int],						__A				:	Tuple,						__A				:	Dict		):
     UpperCAmelCase			:				Union[str, Any]										=					self.num_labels
     UpperCAmelCase			:				Optional[Any]										=					TFDebertaForSequenceClassification(config=UpperCAmelCase_		)
     UpperCAmelCase			:				Dict										=					{
         '''input_ids''': input_ids,
         '''attention_mask''': input_mask,
         '''token_type_ids''': token_type_ids,
     }

     UpperCAmelCase			:				Any										=					model(UpperCAmelCase_		)
     self.parent.assertEqual(result.logits.shape,						(self.batch_size, self.num_labels)		)



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



   def 			__magic_name__       (				self				:	str,						__A				:	int,						__A				:	Union[str, Any],						__A				:	Optional[int],						__A				:	Tuple,						__A				:	List[Any],						__A				:	Optional[Any],						__A				:	Optional[Any]		):
     UpperCAmelCase			:				Dict										=					TFDebertaForQuestionAnswering(config=UpperCAmelCase_		)
     UpperCAmelCase			:				List[Any]										=					{
         '''input_ids''': input_ids,
         '''attention_mask''': input_mask,
         '''token_type_ids''': token_type_ids,
     }

     UpperCAmelCase			:				List[str]										=					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 			__magic_name__       (				self				:	Optional[Any]		):
     UpperCAmelCase			:				int										=					self.prepare_config_and_inputs()
     (
         (
         UpperCAmelCase	
     )					,		(
         UpperCAmelCase	
     )					,		(
         UpperCAmelCase	
     )					,		(
         UpperCAmelCase	
     )					,		(
         UpperCAmelCase	
     )					,		(
         UpperCAmelCase	
     )					,		(
         UpperCAmelCase	
     )					,			
     )			:				str										=					config_and_inputs
     UpperCAmelCase			:				List[str]										=					{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
     return config, inputs_dict






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

   UpperCamelCase  	=	False
   UpperCamelCase  	=	False



   def 			__magic_name__       (				self				:	Optional[Any]		):
     UpperCAmelCase			:				Union[str, Any]										=					TFDebertaModelTester(self		)
     UpperCAmelCase			:				List[Any]										=					ConfigTester(self,						config_class=UpperCAmelCase_,						hidden_size=3_7		)



   def 			__magic_name__       (				self				:	Optional[int]		):
     self.config_tester.run_common_tests()



   def 			__magic_name__       (				self				:	Tuple		):
     UpperCAmelCase			:				Optional[Any]										=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*UpperCAmelCase_		)



   def 			__magic_name__       (				self				:	Dict		):
     UpperCAmelCase			:				List[str]										=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_lm(*UpperCAmelCase_		)



   def 			__magic_name__       (				self				:	List[str]		):
     UpperCAmelCase			:				Optional[int]										=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*UpperCAmelCase_		)



   def 			__magic_name__       (				self				:	Union[str, Any]		):
     UpperCAmelCase			:				int										=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*UpperCAmelCase_		)



   def 			__magic_name__       (				self				:	List[Any]		):
     UpperCAmelCase			:				int										=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase_		)





   @slow
   def 			__magic_name__       (				self				:	Optional[int]		):
     UpperCAmelCase			:				int										=					TFDebertaModel.from_pretrained('''kamalkraj/deberta-base'''		)
     self.assertIsNotNone(UpperCAmelCase_		)






@require_tf
class    __UpperCAmelCase	(			unittest.TestCase		):



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





   @slow
   def 			__magic_name__       (				self				:	Dict		):
     UpperCAmelCase			:				Optional[int]										=					TFDebertaModel.from_pretrained('''kamalkraj/deberta-base'''		)
     UpperCAmelCase			:				List[str]										=					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]]		)
     UpperCAmelCase			:				Optional[int]										=					tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
     UpperCAmelCase			:				Union[str, Any]										=					model(UpperCAmelCase_,						attention_mask=UpperCAmelCase_		)[0]

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












from typing import TYPE_CHECKING

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


UpperCAmelCase__    :      Optional[Any]       					={
    '''configuration_rwkv''': ['''RWKV_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''RwkvConfig''', '''RwkvOnnxConfig'''],
}

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							UpperCAmelCase__    :      Optional[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

							UpperCAmelCase__    :      int       					=_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)





