



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




       """simple docstring"""






       def __init__(  self  ,lowercase  ,lowercase=13  ,lowercase=7  ,lowercase=True  ,lowercase=True  ,lowercase=True  ,lowercase=True  ,lowercase=99  ,lowercase=32  ,lowercase=2  ,lowercase=4  ,lowercase=37  ,lowercase="gelu"  ,lowercase=0.1  ,lowercase=0.1  ,lowercase=512  ,lowercase=16  ,lowercase=2  ,lowercase=0.02  ,lowercase=3  ,lowercase=4  ,lowercase=None  ,):

             """simple docstring"""





             UpperCAmelCase_	:						int					   =		parent
             UpperCAmelCase_	:						Optional[Any]					   =		13
             UpperCAmelCase_	:						List[str]					   =		7
             UpperCAmelCase_	:						Optional[Any]					   =		True
             UpperCAmelCase_	:						List[Any]					   =		True
             UpperCAmelCase_	:						Optional[int]					   =		True
             UpperCAmelCase_	:						Optional[int]					   =		True
             UpperCAmelCase_	:						Optional[int]					   =		99
             UpperCAmelCase_	:						List[Any]					   =		32
             UpperCAmelCase_	:						List[str]					   =		2
             UpperCAmelCase_	:						List[Any]					   =		4
             UpperCAmelCase_	:						Optional[int]					   =		37
             UpperCAmelCase_	:						Dict					   =		'''gelu'''
             UpperCAmelCase_	:						Optional[Any]					   =		0.1
             UpperCAmelCase_	:						Dict					   =		0.1
             UpperCAmelCase_	:						Any					   =		512
             UpperCAmelCase_	:						Any					   =		16
             UpperCAmelCase_	:						Optional[int]					   =		False
             UpperCAmelCase_	:						Tuple					   =		-1
             UpperCAmelCase_	:						int					   =		True
             UpperCAmelCase_	:						int					   =		2
             UpperCAmelCase_	:						Any					   =		0.02
             UpperCAmelCase_	:						Any					   =		3
             UpperCAmelCase_	:						Union[str, Any]					   =		4
             UpperCAmelCase_	:						int					   =		None



       def 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						Optional[Any]					   =		ids_tensor([self.batch_size, self.seq_length]  ,self.vocab_size)

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

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

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

             UpperCAmelCase_	:						Tuple					   =		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=a__  ,)

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



       def 			A_ (  self  ,lowercase  ,lowercase  ,lowercase  ,lowercase  ,lowercase  ,lowercase  ,lowercase):

             """simple docstring"""





             UpperCAmelCase_	:						Optional[Any]					   =		TFDebertaModel(config=a__)
             UpperCAmelCase_	:						Any					   =		{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

             UpperCAmelCase_	:						Dict					   =		[input_ids, input_mask]
             UpperCAmelCase_	:						Optional[int]					   =		model(a__)

             UpperCAmelCase_	:						List[Any]					   =		model(a__)

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



       def 			A_ (  self  ,lowercase  ,lowercase  ,lowercase  ,lowercase  ,lowercase  ,lowercase  ,lowercase):

             """simple docstring"""





             UpperCAmelCase_	:						List[Any]					   =		TFDebertaForMaskedLM(config=a__)
             UpperCAmelCase_	:						Any					   =		{
                 '''input_ids''': input_ids,
                 '''attention_mask''': input_mask,
                 '''token_type_ids''': token_type_ids,
             }
             UpperCAmelCase_	:						Optional[Any]					   =		model(a__)
             self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.seq_length, self.vocab_size))



       def 			A_ (  self  ,lowercase  ,lowercase  ,lowercase  ,lowercase  ,lowercase  ,lowercase  ,lowercase):

             """simple docstring"""





             UpperCAmelCase_	:						str					   =		self.num_labels
             UpperCAmelCase_	:						Dict					   =		TFDebertaForSequenceClassification(config=a__)
             UpperCAmelCase_	:						List[str]					   =		{
                 '''input_ids''': input_ids,
                 '''attention_mask''': input_mask,
                 '''token_type_ids''': token_type_ids,
             }

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



       def 			A_ (  self  ,lowercase  ,lowercase  ,lowercase  ,lowercase  ,lowercase  ,lowercase  ,lowercase):

             """simple docstring"""





             UpperCAmelCase_	:						Optional[int]					   =		self.num_labels
             UpperCAmelCase_	:						Optional[Any]					   =		TFDebertaForTokenClassification(config=a__)
             UpperCAmelCase_	:						Tuple					   =		{
                 '''input_ids''': input_ids,
                 '''attention_mask''': input_mask,
                 '''token_type_ids''': token_type_ids,
             }
             UpperCAmelCase_	:						Dict					   =		model(a__)
             self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.seq_length, self.num_labels))



       def 			A_ (  self  ,lowercase  ,lowercase  ,lowercase  ,lowercase  ,lowercase  ,lowercase  ,lowercase):

             """simple docstring"""





             UpperCAmelCase_	:						Tuple					   =		TFDebertaForQuestionAnswering(config=a__)
             UpperCAmelCase_	:						Optional[int]					   =		{
                 '''input_ids''': input_ids,
                 '''attention_mask''': input_mask,
                 '''token_type_ids''': token_type_ids,
             }

             UpperCAmelCase_	:						Optional[int]					   =		model(a__)
             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 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						Any					   =		self.prepare_config_and_inputs()
             (
                 UpperCAmelCase_					
             )	:						Union[str, Any]					   =		config_and_inputs
             UpperCAmelCase_	:						Union[str, Any]					   =		{'''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	):




       """simple docstring"""






       _lowerCamelCase           =					(
           (
               TFDebertaModel,
               TFDebertaForMaskedLM,
               TFDebertaForQuestionAnswering,
               TFDebertaForSequenceClassification,
               TFDebertaForTokenClassification,
           )
           if is_tf_available()
           else ()
       )
       _lowerCamelCase           =					(
           {
               """feature-extraction""": TFDebertaModel,
               """fill-mask""": TFDebertaForMaskedLM,
               """question-answering""": TFDebertaForQuestionAnswering,
               """text-classification""": TFDebertaForSequenceClassification,
               """token-classification""": TFDebertaForTokenClassification,
               """zero-shot""": TFDebertaForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )

       _lowerCamelCase           =					False
       _lowerCamelCase           =					False



       def 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						Optional[int]					   =		TFDebertaModelTester(self)
             UpperCAmelCase_	:						Any					   =		ConfigTester(self  ,config_class=a__  ,hidden_size=37)



       def 			A_ (  self):

             """simple docstring"""





             self.config_tester.run_common_tests()



       def 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						Any					   =		self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*a__)



       def 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						str					   =		self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_masked_lm(*a__)



       def 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						Union[str, Any]					   =		self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_question_answering(*a__)



       def 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						str					   =		self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_sequence_classification(*a__)



       def 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						Any					   =		self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_token_classification(*a__)



       @slow
       def 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						List[str]					   =		TFDebertaModel.from_pretrained("kamalkraj/deberta-base")
             self.assertIsNotNone(a__)


@require_tf
class 	snake_case_			(unittest.TestCase	):




       """simple docstring"""






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

             """simple docstring"""





             pass



       @slow
       def 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						Union[str, Any]					   =		TFDebertaModel.from_pretrained("kamalkraj/deberta-base")
             UpperCAmelCase_	:						int					   =		tf.constant([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]])
             UpperCAmelCase_	:						List[Any]					   =		tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
             UpperCAmelCase_	:						List[str]					   =		model(a__  ,attention_mask=a__)[0]

             UpperCAmelCase_	:						Any					   =		tf.constant(
                 [
                     [
                         [-0.5985_5896, -0.8055_2566, -0.846_2135],
                         [1.448_4025, -0.9348_3794, -0.8059_3085],
                         [0.312_2741, 0.0031_6059, -1.413_1377],
                     ]
                 ])
             tf.debugging.assert_near(output[:, 1:4, 1:4]  ,a__  ,atol=1E-4)






'''simple docstring'''





from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxSeqaSeqConfigWithPast
from ...utils import logging


lowercase__								=						logging.get_logger(__name__)






class     lowercase  ( UpperCAmelCase_):
      __lowercase					=	'''mt5'''
      __lowercase					=	['''past_key_values''']






      def __init__(self			,	a__=2_5_0_1_1_2			,	a__=5_1_2			,	a__=6_4			,	a__=1_0_2_4			,	a__=8			,	a__=None			,	a__=6			,	a__=3_2			,	a__=1_2_8			,	a__=0.1			,	a__=1E-6			,	a__=1.0			,	a__="gated-gelu"			,	a__=True			,	a__=True			,	a__="T5Tokenizer"			,	a__=False			,	a__=0			,	a__=1			,	a__=0			,	**a__			,	)							-> List[Any]:
         super().__init__(
             is_encoder_decoder=a__			,	tokenizer_class=a__			,	tie_word_embeddings=a__			,	pad_token_id=a__			,	eos_token_id=a__			,	decoder_start_token_id=a__			,	**a__			,	)
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			vocab_size
         SCREAMING_SNAKE_CASE__      :					Tuple							    =			d_model
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			d_kv
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			d_ff
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			num_layers
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			(
             num_decoder_layers if num_decoder_layers is not None else self.num_layers
         )  # default = symmetry
         SCREAMING_SNAKE_CASE__      :					Any							    =			num_heads
         SCREAMING_SNAKE_CASE__      :					Any							    =			relative_attention_num_buckets
         SCREAMING_SNAKE_CASE__      :					Any							    =			relative_attention_max_distance
         SCREAMING_SNAKE_CASE__      :					Any							    =			dropout_rate
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			layer_norm_epsilon
         SCREAMING_SNAKE_CASE__      :					List[str]							    =			initializer_factor
         SCREAMING_SNAKE_CASE__      :					List[Any]							    =			feed_forward_proj
         SCREAMING_SNAKE_CASE__      :					Any							    =			use_cache

         SCREAMING_SNAKE_CASE__      :					int							    =			self.feed_forward_proj.split('''-'''       )
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			act_info[-1]
         SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			act_info[0] == '''gated'''

         if len(a__       ) > 1 and act_info[0] != "gated" or len(a__       ) > 2:
            raise ValueError(
                f"""`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer."""
                '''Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. '''
                '''\'gated-gelu\' or \'relu\''''       )

         # for backwards compatibility
         if feed_forward_proj == "gated-gelu":
            SCREAMING_SNAKE_CASE__      :					int							    =			'''gelu_new'''






      @property
      def  A__(self       )							-> Dict:
         return self.d_model






      @property
      def  A__(self       )							-> Tuple:
         return self.num_heads






      @property
      def  A__(self       )							-> List[str]:
         return self.num_layers






class     lowercase  ( UpperCAmelCase_):






      @property
      # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.inputs
      def  A__(self       )							-> Mapping[str, Mapping[int, str]]:
         SCREAMING_SNAKE_CASE__      :					List[Any]							    =			{
             '''input_ids''': {0: '''batch''', 1: '''encoder_sequence'''},
             '''attention_mask''': {0: '''batch''', 1: '''encoder_sequence'''},
         }
         if self.use_past:
            SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			'''past_encoder_sequence + sequence'''
            SCREAMING_SNAKE_CASE__      :					str							    =			{0: '''batch'''}
            SCREAMING_SNAKE_CASE__      :					List[Any]							    =			{0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
         else:
            SCREAMING_SNAKE_CASE__      :					int							    =			{0: '''batch''', 1: '''decoder_sequence'''}
            SCREAMING_SNAKE_CASE__      :					Tuple							    =			{0: '''batch''', 1: '''decoder_sequence'''}

         if self.use_past:
            self.fill_with_past_key_values_(a__			,	direction='''inputs'''       )

         return common_inputs






      @property
      # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.default_onnx_opset
      def  A__(self       )							-> int:
         return 1_3






      @property
      def  A__(self       )							-> float:
         return 5E-4

