




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

    '''simple docstring'''






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





           '''simple docstring'''

           A__ 					=       parent
           A__ 					=       batch_size
           A__ 					=       seq_length
           A__ 					=       is_training
           A__ 					=       use_input_mask
           A__ 					=       use_token_type_ids
           A__ 					=       use_labels
           A__ 					=       vocab_size
           A__ 					=       hidden_size
           A__ 					=       num_hidden_layers
           A__ 					=       num_attention_heads
           A__ 					=       intermediate_size
           A__ 					=       hidden_act
           A__ 					=       hidden_dropout_prob
           A__ 					=       attention_probs_dropout_prob
           A__ 					=       max_position_embeddings
           A__ 					=       type_vocab_size
           A__ 					=       type_sequence_label_size
           A__ 					=       initializer_range
           A__ 					=       num_labels
           A__ 					=       num_choices
           A__ 					=       relative_attention
           A__ 					=       position_biased_input
           A__ 					=       pos_att_type
           A__ 					=       scope


    def 			SCREAMING_SNAKE_CASE				(     self :      Optional[int])		->List[str]:





           '''simple docstring'''

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

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

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

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

           A__ 					=       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 			SCREAMING_SNAKE_CASE				(     self :      Tuple   ,					UpperCAmelCase__ :      str   ,					UpperCAmelCase__ :      Any   ,					UpperCAmelCase__ :      List[Any]   ,					UpperCAmelCase__ :      int   ,					UpperCAmelCase__ :      Tuple   ,					UpperCAmelCase__ :      List[str]   ,					UpperCAmelCase__ :      Union[str, Any])		->str:





           '''simple docstring'''

           A__ 					=       TFDebertaVaModel(config=UpperCAmelCase__)
           A__ 					=       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

           A__ 					=       [input_ids, input_mask]
           A__ 					=       model(UpperCAmelCase__)

           A__ 					=       model(UpperCAmelCase__)

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


    def 			SCREAMING_SNAKE_CASE				(     self :      str   ,					UpperCAmelCase__ :      Dict   ,					UpperCAmelCase__ :      str   ,					UpperCAmelCase__ :      List[Any]   ,					UpperCAmelCase__ :      int   ,					UpperCAmelCase__ :      List[Any]   ,					UpperCAmelCase__ :      List[str]   ,					UpperCAmelCase__ :      Tuple)		->Any:





           '''simple docstring'''

           A__ 					=       TFDebertaVaForMaskedLM(config=UpperCAmelCase__)
           A__ 					=       {
               '''input_ids''': input_ids,
               '''attention_mask''': input_mask,
               '''token_type_ids''': token_type_ids,
           }
           A__ 					=       model(UpperCAmelCase__)
           self.parent.assertEqual(result.logits.shape   ,					(self.batch_size, self.seq_length, self.vocab_size))


    def 			SCREAMING_SNAKE_CASE				(     self :      List[str]   ,					UpperCAmelCase__ :      Optional[int]   ,					UpperCAmelCase__ :      List[Any]   ,					UpperCAmelCase__ :      Optional[Any]   ,					UpperCAmelCase__ :      int   ,					UpperCAmelCase__ :      Optional[Any]   ,					UpperCAmelCase__ :      Union[str, Any]   ,					UpperCAmelCase__ :      Optional[int])		->Optional[Any]:





           '''simple docstring'''

           A__ 					=       self.num_labels
           A__ 					=       TFDebertaVaForSequenceClassification(config=UpperCAmelCase__)
           A__ 					=       {
               '''input_ids''': input_ids,
               '''attention_mask''': input_mask,
               '''token_type_ids''': token_type_ids,
           }

           A__ 					=       model(UpperCAmelCase__)
           self.parent.assertEqual(result.logits.shape   ,					(self.batch_size, self.num_labels))


    def 			SCREAMING_SNAKE_CASE				(     self :      str   ,					UpperCAmelCase__ :      Tuple   ,					UpperCAmelCase__ :      Tuple   ,					UpperCAmelCase__ :      Optional[int]   ,					UpperCAmelCase__ :      Any   ,					UpperCAmelCase__ :      Dict   ,					UpperCAmelCase__ :      int   ,					UpperCAmelCase__ :      List[str])		->Tuple:





           '''simple docstring'''

           A__ 					=       self.num_labels
           A__ 					=       TFDebertaVaForTokenClassification(config=UpperCAmelCase__)
           A__ 					=       {
               '''input_ids''': input_ids,
               '''attention_mask''': input_mask,
               '''token_type_ids''': token_type_ids,
           }
           A__ 					=       model(UpperCAmelCase__)
           self.parent.assertEqual(result.logits.shape   ,					(self.batch_size, self.seq_length, self.num_labels))


    def 			SCREAMING_SNAKE_CASE				(     self :      Tuple   ,					UpperCAmelCase__ :      Dict   ,					UpperCAmelCase__ :      Optional[int]   ,					UpperCAmelCase__ :      List[Any]   ,					UpperCAmelCase__ :      Tuple   ,					UpperCAmelCase__ :      Union[str, Any]   ,					UpperCAmelCase__ :      Dict   ,					UpperCAmelCase__ :      str)		->Optional[int]:





           '''simple docstring'''

           A__ 					=       TFDebertaVaForQuestionAnswering(config=UpperCAmelCase__)
           A__ 					=       {
               '''input_ids''': input_ids,
               '''attention_mask''': input_mask,
               '''token_type_ids''': token_type_ids,
           }

           A__ 					=       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 			SCREAMING_SNAKE_CASE				(     self :      List[str])		->Optional[int]:





           '''simple docstring'''

           A__ 					=       self.prepare_config_and_inputs()
           (
               (
               A__
           )					,				(
               A__
           )					,				(
               A__
           )					,				(
               A__
           )					,				(
               A__
           )					,				(
               A__
           )					,				(
               A__
           )					,				
           ) 					=       config_and_inputs
           A__ 					=       {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
           return config, inputs_dict


@require_tf
class 							UpperCamelCase_       (					UpperCAmelCase__						,   UpperCAmelCase__						,   unittest.TestCase	):

    '''simple docstring'''






    UpperCAmelCase__				    = (
        (
            TFDebertaVaModel,
            TFDebertaVaForMaskedLM,
            TFDebertaVaForQuestionAnswering,
            TFDebertaVaForSequenceClassification,
            TFDebertaVaForTokenClassification,
        )
        if is_tf_available()
        else ()
    )
    UpperCAmelCase__				    = (
        {
            '''feature-extraction''': TFDebertaVaModel,
            '''fill-mask''': TFDebertaVaForMaskedLM,
            '''question-answering''': TFDebertaVaForQuestionAnswering,
            '''text-classification''': TFDebertaVaForSequenceClassification,
            '''token-classification''': TFDebertaVaForTokenClassification,
            '''zero-shot''': TFDebertaVaForSequenceClassification,
        }
        if is_tf_available()
        else {}
    )

    UpperCAmelCase__				    = False
    UpperCAmelCase__				    = False


    def 			SCREAMING_SNAKE_CASE				(     self :      Any)		->Any:





           '''simple docstring'''

           A__ 					=       TFDebertaVaModelTester(self)
           A__ 					=       ConfigTester(self   ,					config_class=UpperCAmelCase__   ,					hidden_size=37)


    def 			SCREAMING_SNAKE_CASE				(     self :      List[Any])		->str:





           '''simple docstring'''

           self.config_tester.run_common_tests()


    def 			SCREAMING_SNAKE_CASE				(     self :      List[Any])		->Dict:





           '''simple docstring'''

           A__ 					=       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCAmelCase__)


    def 			SCREAMING_SNAKE_CASE				(     self :      str)		->List[Any]:





           '''simple docstring'''

           A__ 					=       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_lm(*UpperCAmelCase__)


    def 			SCREAMING_SNAKE_CASE				(     self :      Tuple)		->Optional[Any]:





           '''simple docstring'''

           A__ 					=       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*UpperCAmelCase__)


    def 			SCREAMING_SNAKE_CASE				(     self :      Any)		->Tuple:





           '''simple docstring'''

           A__ 					=       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(*UpperCAmelCase__)


    def 			SCREAMING_SNAKE_CASE				(     self :      Union[str, Any])		->Tuple:





           '''simple docstring'''

           A__ 					=       self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase__)





    @slow
    def 			SCREAMING_SNAKE_CASE				(     self :      str)		->Optional[int]:





           '''simple docstring'''

           A__ 					=       TFDebertaVaModel.from_pretrained('''kamalkraj/deberta-v2-xlarge''')
           self.assertIsNotNone(UpperCAmelCase__)


@require_tf
class 							UpperCamelCase_       (					unittest.TestCase	):

    '''simple docstring'''






    @unittest.skip(reason='''Model not available yet''')
    def 			SCREAMING_SNAKE_CASE				(     self :      Any)		->Optional[int]:





           '''simple docstring'''

           pass





    @slow
    def 			SCREAMING_SNAKE_CASE				(     self :      Any)		->List[Any]:





           '''simple docstring'''

           A__ 					=       TFDebertaVaModel.from_pretrained('''kamalkraj/deberta-v2-xlarge''')
           A__ 					=       tf.constant([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]])
           A__ 					=       tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
           A__ 					=       model(UpperCAmelCase__   ,					attention_mask=UpperCAmelCase__)[0]

           A__ 					=       tf.constant(
               [[[0.2356, 0.1948, 0.0369], [-0.1063, 0.3586, -0.5152], [-0.6399, -0.0259, -0.2525]]])
           tf.debugging.assert_near(output[:, 1:4, 1:4]   ,					UpperCAmelCase__   ,					atol=1e-4)




import fire

from transformers import AutoConfig, AutoModelForSeqaSeqLM, AutoTokenizer



def   __lowerCAmelCase    (				UpperCAmelCase__					:   str      ,					UpperCAmelCase__					:   str      ,					**UpperCAmelCase__					:   List[str]							)	->     Tuple:
							lowerCamelCase_					=       AutoConfig.from_pretrained(UpperCAmelCase__      ,					**UpperCAmelCase__							)
							lowerCamelCase_					=       AutoModelForSeqaSeqLM.from_config(UpperCAmelCase__							)
							model.save_pretrained(UpperCAmelCase__							)
							AutoTokenizer.from_pretrained(UpperCAmelCase__							).save_pretrained(UpperCAmelCase__							)
							return model


if __name__ == "__main__":
		fire.Fire(save_randomly_initialized_version)





