



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 				lowerCAmelCase_		:
       def __init__(			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_=13,			SCREAMING_SNAKE_CASE_=7,			SCREAMING_SNAKE_CASE_=True,			SCREAMING_SNAKE_CASE_=True,			SCREAMING_SNAKE_CASE_=True,			SCREAMING_SNAKE_CASE_=True,			SCREAMING_SNAKE_CASE_=99,			SCREAMING_SNAKE_CASE_=32,			SCREAMING_SNAKE_CASE_=2,			SCREAMING_SNAKE_CASE_=4,			SCREAMING_SNAKE_CASE_=37,			SCREAMING_SNAKE_CASE_="gelu",			SCREAMING_SNAKE_CASE_=0.1,			SCREAMING_SNAKE_CASE_=0.1,			SCREAMING_SNAKE_CASE_=512,			SCREAMING_SNAKE_CASE_=16,			SCREAMING_SNAKE_CASE_=2,			SCREAMING_SNAKE_CASE_=0.02,			SCREAMING_SNAKE_CASE_=False,			SCREAMING_SNAKE_CASE_=True,			SCREAMING_SNAKE_CASE_="None",			SCREAMING_SNAKE_CASE_=3,			SCREAMING_SNAKE_CASE_=4,			SCREAMING_SNAKE_CASE_=None,			)     ->		Any:
          UpperCamelCase    :  List[str]							=	parent
          UpperCamelCase    :  List[Any]							=	batch_size
          UpperCamelCase    :  List[Any]							=	seq_length
          UpperCamelCase    :  List[Any]							=	is_training
          UpperCamelCase    :  int							=	use_input_mask
          UpperCamelCase    :  Union[str, Any]							=	use_token_type_ids
          UpperCamelCase    :  Dict							=	use_labels
          UpperCamelCase    :  Tuple							=	vocab_size
          UpperCamelCase    :  Optional[int]							=	hidden_size
          UpperCamelCase    :  Dict							=	num_hidden_layers
          UpperCamelCase    :  Optional[int]							=	num_attention_heads
          UpperCamelCase    :  List[str]							=	intermediate_size
          UpperCamelCase    :  int							=	hidden_act
          UpperCamelCase    :  List[str]							=	hidden_dropout_prob
          UpperCamelCase    :  Any							=	attention_probs_dropout_prob
          UpperCamelCase    :  str							=	max_position_embeddings
          UpperCamelCase    :  Union[str, Any]							=	type_vocab_size
          UpperCamelCase    :  int							=	type_sequence_label_size
          UpperCamelCase    :  int							=	initializer_range
          UpperCamelCase    :  List[Any]							=	num_labels
          UpperCamelCase    :  List[Any]							=	num_choices
          UpperCamelCase    :  Union[str, Any]							=	relative_attention
          UpperCamelCase    :  List[Any]							=	position_biased_input
          UpperCamelCase    :  Optional[Any]							=	pos_att_type
          UpperCamelCase    :  Optional[Any]							=	scope
       def 					snake_case_       (			self       )     ->		Optional[int]:
          UpperCamelCase    :  Union[str, Any]							=	ids_tensor([self.batch_size, self.seq_length],			self.vocab_size       )

          UpperCamelCase    :  int							=	None
          if self.use_input_mask:
             UpperCamelCase    :  Optional[int]							=	random_attention_mask([self.batch_size, self.seq_length]       )

          UpperCamelCase    :  List[Any]							=	None
          if self.use_token_type_ids:
             UpperCamelCase    :  List[str]							=	ids_tensor([self.batch_size, self.seq_length],			self.type_vocab_size       )

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

          UpperCamelCase    :  Optional[Any]							=	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,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )     ->		List[Any]:
          UpperCamelCase    :  Any							=	TFDebertaVaModel(config=_UpperCamelCase       )
          UpperCamelCase    :  int							=	{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

          UpperCamelCase    :  List[str]							=	[input_ids, input_mask]
          UpperCamelCase    :  Optional[int]							=	model(_UpperCamelCase       )

          UpperCamelCase    :  str							=	model(_UpperCamelCase       )

          self.parent.assertEqual(result.last_hidden_state.shape,			(self.batch_size, self.seq_length, self.hidden_size)       )
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )     ->		Optional[int]:
          UpperCamelCase    :  List[Any]							=	TFDebertaVaForMaskedLM(config=_UpperCamelCase       )
          UpperCamelCase    :  Tuple							=	{
              'input_ids': input_ids,
              'attention_mask': input_mask,
              'token_type_ids': token_type_ids,
          }
          UpperCamelCase    :  Union[str, Any]							=	model(_UpperCamelCase       )
          self.parent.assertEqual(result.logits.shape,			(self.batch_size, self.seq_length, self.vocab_size)       )
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )     ->		str:
          UpperCamelCase    :  int							=	self.num_labels
          UpperCamelCase    :  List[str]							=	TFDebertaVaForSequenceClassification(config=_UpperCamelCase       )
          UpperCamelCase    :  Union[str, Any]							=	{
              'input_ids': input_ids,
              'attention_mask': input_mask,
              'token_type_ids': token_type_ids,
          }

          UpperCamelCase    :  str							=	model(_UpperCamelCase       )
          self.parent.assertEqual(result.logits.shape,			(self.batch_size, self.num_labels)       )
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )     ->		List[Any]:
          UpperCamelCase    :  List[Any]							=	self.num_labels
          UpperCamelCase    :  Tuple							=	TFDebertaVaForTokenClassification(config=_UpperCamelCase       )
          UpperCamelCase    :  Optional[int]							=	{
              'input_ids': input_ids,
              'attention_mask': input_mask,
              'token_type_ids': token_type_ids,
          }
          UpperCamelCase    :  Optional[Any]							=	model(_UpperCamelCase       )
          self.parent.assertEqual(result.logits.shape,			(self.batch_size, self.seq_length, self.num_labels)       )
       def 					snake_case_       (			self,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_,			SCREAMING_SNAKE_CASE_       )     ->		List[str]:
          UpperCamelCase    :  List[str]							=	TFDebertaVaForQuestionAnswering(config=_UpperCamelCase       )
          UpperCamelCase    :  Union[str, Any]							=	{
              'input_ids': input_ids,
              'attention_mask': input_mask,
              'token_type_ids': token_type_ids,
          }

          UpperCamelCase    :  Optional[int]							=	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       )     ->		Tuple:
          UpperCamelCase    :  List[Any]							=	self.prepare_config_and_inputs()
          (
              (
              UpperCamelCase    
          )	,     (
              UpperCamelCase    
          )	,     (
              UpperCamelCase    
          )	,     (
              UpperCamelCase    
          )	,     (
              UpperCamelCase    
          )	,     (
              UpperCamelCase    
          )	,     (
              UpperCamelCase    
          )	,         
          )    :  Optional[Any]							=	config_and_inputs
          UpperCamelCase    :  Optional[int]							=	{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
          return config, inputs_dict






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

       UpperCAmelCase__				:					Any        =  False
       UpperCAmelCase__				:					int        =  False
       def 					snake_case_       (			self       )     ->		int:
          UpperCamelCase    :  Any							=	TFDebertaVaModelTester(self       )
          UpperCamelCase    :  int							=	ConfigTester(self,			config_class=_UpperCamelCase,			hidden_size=37       )
       def 					snake_case_       (			self       )     ->		Dict:
          self.config_tester.run_common_tests()
       def 					snake_case_       (			self       )     ->		List[Any]:
          UpperCamelCase    :  Union[str, Any]							=	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*_UpperCamelCase       )
       def 					snake_case_       (			self       )     ->		List[Any]:
          UpperCamelCase    :  Tuple							=	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_lm(*_UpperCamelCase       )
       def 					snake_case_       (			self       )     ->		Tuple:
          UpperCamelCase    :  List[Any]							=	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(*_UpperCamelCase       )
       def 					snake_case_       (			self       )     ->		int:
          UpperCamelCase    :  int							=	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_sequence_classification(*_UpperCamelCase       )
       def 					snake_case_       (			self       )     ->		Dict:
          UpperCamelCase    :  str							=	self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*_UpperCamelCase       )






       @slow
       def 					snake_case_       (			self       )     ->		List[str]:
          UpperCamelCase    :  Optional[Any]							=	TFDebertaVaModel.from_pretrained('kamalkraj/deberta-v2-xlarge'       )
          self.assertIsNotNone(_UpperCamelCase       )






@require_tf
class 				lowerCAmelCase_		(  unittest.TestCase							):
       @unittest.skip(reason='Model not available yet'       )
       def 					snake_case_       (			self       )     ->		int:
          pass






       @slow
       def 					snake_case_       (			self       )     ->		Dict:
          UpperCamelCase    :  Optional[Any]							=	TFDebertaVaModel.from_pretrained('kamalkraj/deberta-v2-xlarge'       )
          UpperCamelCase    :  Union[str, Any]							=	tf.constant([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]       )
          UpperCamelCase    :  Union[str, Any]							=	tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]       )
          UpperCamelCase    :  Optional[Any]							=	model(_UpperCamelCase,			attention_mask=_UpperCamelCase       )[0]

          UpperCamelCase    :  List[str]							=	tf.constant(
              [[[0.23_56, 0.19_48, 0.03_69], [-0.10_63, 0.35_86, -0.51_52], [-0.63_99, -0.02_59, -0.25_25]]]       )
          tf.debugging.assert_near(output[:, 1:4, 1:4],			_UpperCamelCase,			atol=1e-4       )






from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase_       	=	logging.get_logger(__name__)

lowerCAmelCase_       	=	{
    '''sayakpaul/vit-msn-base''': '''https://huggingface.co/sayakpaul/vit-msn-base/resolve/main/config.json''',
    # See all ViT MSN models at https://huggingface.co/models?filter=vit_msn
}





class 	snake_case_   (		__A					):







			'''simple docstring'''

			SCREAMING_SNAKE_CASE     :   List[Any]									=				"vit_msn"







			def __init__(     self :				Dict							,   _UpperCamelCase :				Optional[int]=7_6_8							,   _UpperCamelCase :				Optional[Any]=1_2							,   _UpperCamelCase :				Union[str, Any]=1_2							,   _UpperCamelCase :				str=3_0_7_2							,   _UpperCamelCase :				Tuple="gelu"							,   _UpperCamelCase :				Dict=0.0							,   _UpperCamelCase :				Dict=0.0							,   _UpperCamelCase :				List[str]=0.02							,   _UpperCamelCase :				List[Any]=1e-06							,   _UpperCamelCase :				Any=2_2_4							,   _UpperCamelCase :				Optional[Any]=1_6							,   _UpperCamelCase :				Any=3							,   _UpperCamelCase :				str=True							,   **_UpperCamelCase :				Any							,   )      ->int:
									super().__init__(**_UpperCamelCase							)

									snake_case_   =	hidden_size
									snake_case_   =	num_hidden_layers
									snake_case_   =	num_attention_heads
									snake_case_   =	intermediate_size
									snake_case_   =	hidden_act
									snake_case_   =	hidden_dropout_prob
									snake_case_   =	attention_probs_dropout_prob
									snake_case_   =	initializer_range
									snake_case_   =	layer_norm_eps
									snake_case_   =	image_size
									snake_case_   =	patch_size
									snake_case_   =	num_channels
									snake_case_   =	qkv_bias