


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:   List[str],A_:   List[str],A_:   Optional[int]=13,A_:   Dict=7,A_:   Union[str, Any]=True,A_:   List[Any]=True,A_:   Optional[Any]=True,A_:   Optional[int]=True,A_:   List[str]=99,A_:   List[Any]=32,A_:   Optional[Any]=2,A_:   int=4,A_:   str=37,A_:   List[str]="gelu",A_:   List[Any]=0.1,A_:   str=0.1,A_:   int=512,A_:   Optional[int]=16,A_:   Dict=2,A_:   str=0.0_2,A_:   List[str]=3,A_:   Dict=4,A_:   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.0_2
            __UpperCamelCase   = 3
            __UpperCamelCase   = 4
            __UpperCamelCase   = None







       def 		snake_case_  ( self:   int				):






            '''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=lowercase_,)

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







       def 		snake_case_  ( self:   List[Any],A_:   int,A_:   List[Any],A_:   Optional[Any],A_:   Union[str, Any],A_:   Any,A_:   Any,A_:   Union[str, Any]				):






            '''simple docstring'''




            __UpperCamelCase   = TFDebertaModel(config=lowercase_				)
            __UpperCamelCase   = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

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

            __UpperCamelCase   = model(lowercase_				)

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







       def 		snake_case_  ( self:   List[Any],A_:   str,A_:   str,A_:   Any,A_:   Tuple,A_:   Optional[int],A_:   List[str],A_:   int				):






            '''simple docstring'''




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







       def 		snake_case_  ( self:   Optional[Any],A_:   Union[str, Any],A_:   Optional[Any],A_:   str,A_:   Union[str, Any],A_:   Optional[Any],A_:   str,A_:   List[str]				):






            '''simple docstring'''




            __UpperCamelCase   = self.num_labels
            __UpperCamelCase   = TFDebertaForSequenceClassification(config=lowercase_				)
            __UpperCamelCase   = {
                '''input_ids''': input_ids,
                '''attention_mask''': input_mask,
                '''token_type_ids''': token_type_ids,
            }

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







       def 		snake_case_  ( self:   Union[str, Any],A_:   Dict,A_:   List[str],A_:   Dict,A_:   Tuple,A_:   List[Any],A_:   Any,A_:   Optional[int]				):






            '''simple docstring'''




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







       def 		snake_case_  ( self:   Union[str, Any],A_:   Dict,A_:   str,A_:   Optional[int],A_:   Union[str, Any],A_:   List[str],A_:   int,A_:   Optional[Any]				):






            '''simple docstring'''




            __UpperCamelCase   = TFDebertaForQuestionAnswering(config=lowercase_				)
            __UpperCamelCase   = {
                '''input_ids''': input_ids,
                '''attention_mask''': input_mask,
                '''token_type_ids''': token_type_ids,
            }

            __UpperCamelCase   = model(lowercase_				)
            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 		snake_case_  ( self:   str				):






            '''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	):
       _lowercase       							=    (
           (
               TFDebertaModel,
               TFDebertaForMaskedLM,
               TFDebertaForQuestionAnswering,
               TFDebertaForSequenceClassification,
               TFDebertaForTokenClassification,
           )
           if is_tf_available()
           else ()
       )
       _lowercase       							=    (
           {
               """feature-extraction""": TFDebertaModel,
               """fill-mask""": TFDebertaForMaskedLM,
               """question-answering""": TFDebertaForQuestionAnswering,
               """text-classification""": TFDebertaForSequenceClassification,
               """token-classification""": TFDebertaForTokenClassification,
               """zero-shot""": TFDebertaForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )

       _lowercase       							=    False
       _lowercase       							=    False







       def 		snake_case_  ( self:   Tuple				):






            '''simple docstring'''




            __UpperCamelCase   = TFDebertaModelTester(self				)
            __UpperCamelCase   = ConfigTester(self,config_class=lowercase_,hidden_size=37				)







       def 		snake_case_  ( self:   Any				):






            '''simple docstring'''




            self.config_tester.run_common_tests()







       def 		snake_case_  ( self:   str				):






            '''simple docstring'''




            __UpperCamelCase   = self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*lowercase_				)







       def 		snake_case_  ( self:   Tuple				):






            '''simple docstring'''




            __UpperCamelCase   = self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_masked_lm(*lowercase_				)







       def 		snake_case_  ( self:   List[str]				):






            '''simple docstring'''




            __UpperCamelCase   = self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_question_answering(*lowercase_				)







       def 		snake_case_  ( self:   List[str]				):






            '''simple docstring'''




            __UpperCamelCase   = self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_sequence_classification(*lowercase_				)







       def 		snake_case_  ( self:   Any				):






            '''simple docstring'''




            __UpperCamelCase   = self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_token_classification(*lowercase_				)







       @slow
       def 		snake_case_  ( self:   Optional[int]				):






            '''simple docstring'''




            __UpperCamelCase   = TFDebertaModel.from_pretrained('kamalkraj/deberta-base'				)
            self.assertIsNotNone(lowercase_				)



@require_tf
class   __lowerCamelCase (unittest.TestCase	):







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






            '''simple docstring'''




            pass







       @slow
       def 		snake_case_  ( self:   Any				):






            '''simple docstring'''




            __UpperCamelCase   = TFDebertaModel.from_pretrained('kamalkraj/deberta-base'				)
            __UpperCamelCase   = tf.constant([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]				)
            __UpperCamelCase   = tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]				)
            __UpperCamelCase   = model(lowercase_,attention_mask=lowercase_				)[0]

            __UpperCamelCase   = 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],lowercase_,atol=1E-4				)






from ..utils import DummyObject, requires_backends



class   __lowerCamelCase (metaclass=_a	):
       _lowercase       							=    ["""torch""", """transformers""", """k_diffusion"""]







       def __init__( self:   Tuple,*A_:   Union[str, Any],**A_:   Tuple				):






            '''simple docstring'''




            requires_backends(self,['torch', 'transformers', 'k_diffusion']				)







       @classmethod
       def 		snake_case_  ( cls:   int,*A_:   Optional[Any],**A_:   Tuple				):






            '''simple docstring'''




            requires_backends(cls,['torch', 'transformers', 'k_diffusion']				)







       @classmethod
       def 		snake_case_  ( cls:   int,*A_:   Tuple,**A_:   Tuple				):






            '''simple docstring'''




            requires_backends(cls,['torch', 'transformers', 'k_diffusion']				)


