


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


      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=False					,						__UpperCamelCase=True					,						__UpperCamelCase="None"					,						__UpperCamelCase=3					,						__UpperCamelCase=4					,						__UpperCamelCase=None					,						)->	Optional[int]:
           __lowerCAmelCase								=					parent
           __lowerCAmelCase								=					batch_size
           __lowerCAmelCase								=					seq_length
           __lowerCAmelCase								=					is_training
           __lowerCAmelCase								=					use_input_mask
           __lowerCAmelCase								=					use_token_type_ids
           __lowerCAmelCase								=					use_labels
           __lowerCAmelCase								=					vocab_size
           __lowerCAmelCase								=					hidden_size
           __lowerCAmelCase								=					num_hidden_layers
           __lowerCAmelCase								=					num_attention_heads
           __lowerCAmelCase								=					intermediate_size
           __lowerCAmelCase								=					hidden_act
           __lowerCAmelCase								=					hidden_dropout_prob
           __lowerCAmelCase								=					attention_probs_dropout_prob
           __lowerCAmelCase								=					max_position_embeddings
           __lowerCAmelCase								=					type_vocab_size
           __lowerCAmelCase								=					type_sequence_label_size
           __lowerCAmelCase								=					initializer_range
           __lowerCAmelCase								=					num_labels
           __lowerCAmelCase								=					num_choices
           __lowerCAmelCase								=					relative_attention
           __lowerCAmelCase								=					position_biased_input
           __lowerCAmelCase								=					pos_att_type
           __lowerCAmelCase								=					scope


      def  __UpperCAmelCase		(	self	)->	str:
           __lowerCAmelCase								=					ids_tensor([self.batch_size, self.seq_length]					,						self.vocab_size	)

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

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

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

           __lowerCAmelCase								=					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=__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	)->	Any:
           __lowerCAmelCase								=					TFDebertaVaModel(config=__UpperCamelCase	)
           __lowerCAmelCase								=					{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

           __lowerCAmelCase								=					[input_ids, input_mask]
           __lowerCAmelCase								=					model(__UpperCamelCase	)

           __lowerCAmelCase								=					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	)->	List[Any]:
           __lowerCAmelCase								=					TFDebertaVaForMaskedLM(config=__UpperCamelCase	)
           __lowerCAmelCase								=					{
               "input_ids": input_ids,
               "attention_mask": input_mask,
               "token_type_ids": token_type_ids,
           }
           __lowerCAmelCase								=					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	)->	Union[str, Any]:
           __lowerCAmelCase								=					self.num_labels
           __lowerCAmelCase								=					TFDebertaVaForSequenceClassification(config=__UpperCamelCase	)
           __lowerCAmelCase								=					{
               "input_ids": input_ids,
               "attention_mask": input_mask,
               "token_type_ids": token_type_ids,
           }

           __lowerCAmelCase								=					model(__UpperCamelCase	)
           self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.num_labels)	)


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase	)->	Tuple:
           __lowerCAmelCase								=					self.num_labels
           __lowerCAmelCase								=					TFDebertaVaForTokenClassification(config=__UpperCamelCase	)
           __lowerCAmelCase								=					{
               "input_ids": input_ids,
               "attention_mask": input_mask,
               "token_type_ids": token_type_ids,
           }
           __lowerCAmelCase								=					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	)->	Optional[Any]:
           __lowerCAmelCase								=					TFDebertaVaForQuestionAnswering(config=__UpperCamelCase	)
           __lowerCAmelCase								=					{
               "input_ids": input_ids,
               "attention_mask": input_mask,
               "token_type_ids": token_type_ids,
           }

           __lowerCAmelCase								=					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[int]:
           __lowerCAmelCase								=					self.prepare_config_and_inputs()
           (
               (
               __lowerCAmelCase 
           )			, (
               __lowerCAmelCase 
           )			, (
               __lowerCAmelCase 
           )			, (
               __lowerCAmelCase 
           )			, (
               __lowerCAmelCase 
           )			, (
               __lowerCAmelCase 
           )			, (
               __lowerCAmelCase 
           )			,  
           )								=					config_and_inputs
           __lowerCAmelCase								=					{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
           return config, inputs_dict



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

      snake_case_  =							False
      snake_case_  =							False


      def  __UpperCAmelCase		(	self	)->	Any:
           __lowerCAmelCase								=					TFDebertaVaModelTester(self	)
           __lowerCAmelCase								=					ConfigTester(self					,						config_class=__UpperCamelCase					,						hidden_size=3_7	)


      def  __UpperCAmelCase		(	self	)->	Union[str, Any]:
           self.config_tester.run_common_tests()


      def  __UpperCAmelCase		(	self	)->	Tuple:
           __lowerCAmelCase								=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*__UpperCamelCase	)


      def  __UpperCAmelCase		(	self	)->	str:
           __lowerCAmelCase								=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_lm(*__UpperCamelCase	)


      def  __UpperCAmelCase		(	self	)->	str:
           __lowerCAmelCase								=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*__UpperCamelCase	)


      def  __UpperCAmelCase		(	self	)->	Optional[Any]:
           __lowerCAmelCase								=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(*__UpperCamelCase	)


      def  __UpperCAmelCase		(	self	)->	int:
           __lowerCAmelCase								=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*__UpperCamelCase	)


      @slow
      def  __UpperCAmelCase		(	self	)->	Union[str, Any]:
           __lowerCAmelCase								=					TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"	)
           self.assertIsNotNone(__UpperCamelCase	)



@require_tf
class 							_UpperCamelCase			(unittest.TestCase			):


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


      @slow
      def  __UpperCAmelCase		(	self	)->	Any:
           __lowerCAmelCase								=					TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"	)
           __lowerCAmelCase								=					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]]	)
           __lowerCAmelCase								=					tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]	)
           __lowerCAmelCase								=					model(__UpperCamelCase					,						attention_mask=__UpperCamelCase	)[0]

           __lowerCAmelCase								=					tf.constant(
               [[[0.2_3_5_6, 0.1_9_4_8, 0.0_3_6_9], [-0.1_0_6_3, 0.3_5_8_6, -0.5_1_5_2], [-0.6_3_9_9, -0.0_2_5_9, -0.2_5_2_5]]]	)
           tf.debugging.assert_near(output[:, 1:4, 1:4]					,						__UpperCamelCase					,						atol=1e-4	)














lowerCamelCase			:					str         =	'''
# Transformers 설치 방법
! pip install transformers datasets
# 마지막 릴리스 대신 소스에서 설치하려면, 위 명령을 주석으로 바꾸고 아래 명령을 해제하세요.
# ! pip install git+https://github.com/huggingface/transformers.git
'''

lowerCamelCase			:					Tuple         =	[{'''type''': '''code''', '''content''': INSTALL_CONTENT}]
lowerCamelCase			:					Dict         =	{
    '''{processor_class}''': '''FakeProcessorClass''',
    '''{model_class}''': '''FakeModelClass''',
    '''{object_class}''': '''FakeObjectClass''',
}






