



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 ,						a__ ,						a__=1_3 ,						a__=7 ,						a__=True ,						a__=True ,						a__=True ,						a__=True ,						a__=9_9 ,						a__=3_2 ,						a__=2 ,						a__=4 ,						a__=3_7 ,						a__="gelu" ,						a__=0.1 ,						a__=0.1 ,						a__=5_1_2 ,						a__=1_6 ,						a__=2 ,						a__=0.0_2 ,						a__=False ,						a__=True ,						a__="None" ,						a__=3 ,						a__=4 ,						a__=None ,						):
         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 		snake_case_		(				self):
         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 		snake_case_		(				self ,						a__ ,						a__ ,						a__ ,						a__ ,						a__ ,						a__ ,						a__):
         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 		snake_case_		(				self ,						a__ ,						a__ ,						a__ ,						a__ ,						a__ ,						a__ ,						a__):
         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 		snake_case_		(				self ,						a__ ,						a__ ,						a__ ,						a__ ,						a__ ,						a__ ,						a__):
         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 		snake_case_		(				self ,						a__ ,						a__ ,						a__ ,						a__ ,						a__ ,						a__ ,						a__):
         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 		snake_case_		(				self ,						a__ ,						a__ ,						a__ ,						a__ ,						a__ ,						a__ ,						a__):
         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 		snake_case_		(				self):
         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     ):
     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 		snake_case_		(				self):
         A__		 =					TFDebertaVaModelTester(self)
         A__		 =					ConfigTester(self ,						config_class=__UpperCamelCase ,						hidden_size=3_7)




     def 		snake_case_		(				self):
         self.config_tester.run_common_tests()




     def 		snake_case_		(				self):
         A__		 =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__UpperCamelCase)




     def 		snake_case_		(				self):
         A__		 =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_lm(*__UpperCamelCase)




     def 		snake_case_		(				self):
         A__		 =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*__UpperCamelCase)




     def 		snake_case_		(				self):
         A__		 =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*__UpperCamelCase)




     def 		snake_case_		(				self):
         A__		 =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*__UpperCamelCase)




     @slow
     def 		snake_case_		(				self):
         A__		 =					TFDebertaVaModel.from_pretrained('''kamalkraj/deberta-v2-xlarge''')
         self.assertIsNotNone(__UpperCamelCase)







@require_tf
class     _UpperCAmelCase			(       unittest.TestCase     ):




     @unittest.skip(reason='''Model not available yet''')
     def 		snake_case_		(				self):
         pass




     @slow
     def 		snake_case_		(				self):
         A__		 =					TFDebertaVaModel.from_pretrained('''kamalkraj/deberta-v2-xlarge''')
         A__		 =					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]])
         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.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)





from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig


_lowercase						  =		{
    "albert-base-v1": "https://huggingface.co/albert-base-v1/resolve/main/config.json",
    "albert-large-v1": "https://huggingface.co/albert-large-v1/resolve/main/config.json",
    "albert-xlarge-v1": "https://huggingface.co/albert-xlarge-v1/resolve/main/config.json",
    "albert-xxlarge-v1": "https://huggingface.co/albert-xxlarge-v1/resolve/main/config.json",
    "albert-base-v2": "https://huggingface.co/albert-base-v2/resolve/main/config.json",
    "albert-large-v2": "https://huggingface.co/albert-large-v2/resolve/main/config.json",
    "albert-xlarge-v2": "https://huggingface.co/albert-xlarge-v2/resolve/main/config.json",
    "albert-xxlarge-v2": "https://huggingface.co/albert-xxlarge-v2/resolve/main/config.json",
}



class     _UpperCAmelCase			(       A__     ):
     UpperCamelCase__		    =				'''albert'''




     def __init__(				self ,						a__=3_0_0_0_0 ,						a__=1_2_8 ,						a__=4_0_9_6 ,						a__=1_2 ,						a__=1 ,						a__=6_4 ,						a__=1_6_3_8_4 ,						a__=1 ,						a__="gelu_new" ,						a__=0 ,						a__=0 ,						a__=5_1_2 ,						a__=2 ,						a__=0.0_2 ,						a__=1e-12 ,						a__=0.1 ,						a__="absolute" ,						a__=0 ,						a__=2 ,						a__=3 ,						**a__ ,						):
         super().__init__(pad_token_id=a__ ,						bos_token_id=a__ ,						eos_token_id=a__ ,						**a__)

         A__		 =					vocab_size
         A__		 =					embedding_size
         A__		 =					hidden_size
         A__		 =					num_hidden_layers
         A__		 =					num_hidden_groups
         A__		 =					num_attention_heads
         A__		 =					inner_group_num
         A__		 =					hidden_act
         A__		 =					intermediate_size
         A__		 =					hidden_dropout_prob
         A__		 =					attention_probs_dropout_prob
         A__		 =					max_position_embeddings
         A__		 =					type_vocab_size
         A__		 =					initializer_range
         A__		 =					layer_norm_eps
         A__		 =					classifier_dropout_prob
         A__		 =					position_embedding_type







class     _UpperCAmelCase			(       A__     ):




     @property
     def 		snake_case_		(				self):
         if self.task == "multiple-choice":
             A__		 =					{0: '''batch''', 1: '''choice''', 2: '''sequence'''}
         else:
             A__		 =					{0: '''batch''', 1: '''sequence'''}
         return OrderedDict(
             [
                 ('''input_ids''', dynamic_axis),
                 ('''attention_mask''', dynamic_axis),
                 ('''token_type_ids''', dynamic_axis),
             ])
