



import unittest

from transformers import DebertaConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    DebertaForMaskedLM,
		    DebertaForQuestionAnswering,
		    DebertaForSequenceClassification,
		    DebertaForTokenClassification,
		    DebertaModel,
		)
		from transformers.models.deberta.modeling_deberta import DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST





class 			__magic_name__ (				snake_case__    ):






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







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






		def SCREAMING_SNAKE_CASE_      (    self   :  Dict   )					->       Tuple:
								'''simple docstring'''







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

								UpperCAmelCase								=  None
								if self.use_input_mask:
														UpperCAmelCase								=  ids_tensor([self.batch_size, self.seq_length]					,		vocab_size=2   )

								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								=  ids_tensor([self.batch_size]					,		self.num_choices   )

								UpperCAmelCase								=  self.get_config()

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






		def SCREAMING_SNAKE_CASE_      (    self   :  Union[str, Any]   )					->       List[str]:
								'''simple docstring'''







								return 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					,		initializer_range=self.initializer_range					,		relative_attention=self.relative_attention					,		position_biased_input=self.position_biased_input					,		pos_att_type=self.pos_att_type					,		)






		def SCREAMING_SNAKE_CASE_      (    self   :  Dict   )					->       Dict:
								'''simple docstring'''







								UpperCAmelCase								=  self.get_config()
								UpperCAmelCase								=  3_00
								return config






		def SCREAMING_SNAKE_CASE_      (    self   :  Any					,		UpperCamelCase__   :  Tuple   )					->       List[str]:
								'''simple docstring'''







								self.parent.assertListEqual(list(result.loss.size()   )					,		[]   )






		def SCREAMING_SNAKE_CASE_      (    self   :  Tuple					,		UpperCamelCase__   :  str					,		UpperCamelCase__   :  int					,		UpperCamelCase__   :  int					,		UpperCamelCase__   :  Tuple					,		UpperCamelCase__   :  Optional[Any]					,		UpperCamelCase__   :  str					,		UpperCamelCase__   :  Tuple   )					->       Optional[int]:
								'''simple docstring'''







								UpperCAmelCase								=  DebertaModel(config=__snake_case   )
								model.to(__snake_case   )
								model.eval()
								UpperCAmelCase								=  model(__snake_case					,		attention_mask=__snake_case					,		token_type_ids=__snake_case   )[0]
								UpperCAmelCase								=  model(__snake_case					,		token_type_ids=__snake_case   )[0]
								UpperCAmelCase								=  model(__snake_case   )[0]

								self.parent.assertListEqual(list(sequence_output.size()   )					,		[self.batch_size, self.seq_length, self.hidden_size]   )






		def SCREAMING_SNAKE_CASE_      (    self   :  List[str]					,		UpperCamelCase__   :  Tuple					,		UpperCamelCase__   :  Optional[int]					,		UpperCamelCase__   :  int					,		UpperCamelCase__   :  Tuple					,		UpperCamelCase__   :  Optional[int]					,		UpperCamelCase__   :  int					,		UpperCamelCase__   :  int   )					->       str:
								'''simple docstring'''







								UpperCAmelCase								=  DebertaForMaskedLM(config=__snake_case   )
								model.to(__snake_case   )
								model.eval()
								UpperCAmelCase								=  model(__snake_case					,		attention_mask=__snake_case					,		token_type_ids=__snake_case					,		labels=__snake_case   )

								self.parent.assertEqual(result.logits.shape					,		(self.batch_size, self.seq_length, self.vocab_size)   )






		def SCREAMING_SNAKE_CASE_      (    self   :  Dict					,		UpperCamelCase__   :  Optional[Any]					,		UpperCamelCase__   :  Optional[int]					,		UpperCamelCase__   :  Union[str, Any]					,		UpperCamelCase__   :  Tuple					,		UpperCamelCase__   :  Dict					,		UpperCamelCase__   :  Union[str, Any]					,		UpperCamelCase__   :  Optional[int]   )					->       Any:
								'''simple docstring'''







								UpperCAmelCase								=  self.num_labels
								UpperCAmelCase								=  DebertaForSequenceClassification(__snake_case   )
								model.to(__snake_case   )
								model.eval()
								UpperCAmelCase								=  model(__snake_case					,		attention_mask=__snake_case					,		token_type_ids=__snake_case					,		labels=__snake_case   )
								self.parent.assertListEqual(list(result.logits.size()   )					,		[self.batch_size, self.num_labels]   )
								self.check_loss_output(__snake_case   )






		def SCREAMING_SNAKE_CASE_      (    self   :  int					,		UpperCamelCase__   :  List[str]					,		UpperCamelCase__   :  Any					,		UpperCamelCase__   :  List[Any]					,		UpperCamelCase__   :  Dict					,		UpperCamelCase__   :  int					,		UpperCamelCase__   :  int					,		UpperCamelCase__   :  Dict   )					->       Optional[int]:
								'''simple docstring'''







								UpperCAmelCase								=  self.num_labels
								UpperCAmelCase								=  DebertaForTokenClassification(config=__snake_case   )
								model.to(__snake_case   )
								model.eval()
								UpperCAmelCase								=  model(__snake_case					,		attention_mask=__snake_case					,		token_type_ids=__snake_case					,		labels=__snake_case   )
								self.parent.assertEqual(result.logits.shape					,		(self.batch_size, self.seq_length, self.num_labels)   )






		def SCREAMING_SNAKE_CASE_      (    self   :  Optional[int]					,		UpperCamelCase__   :  str					,		UpperCamelCase__   :  Optional[Any]					,		UpperCamelCase__   :  Union[str, Any]					,		UpperCamelCase__   :  Tuple					,		UpperCamelCase__   :  Tuple					,		UpperCamelCase__   :  List[Any]					,		UpperCamelCase__   :  Tuple   )					->       Optional[int]:
								'''simple docstring'''







								UpperCAmelCase								=  DebertaForQuestionAnswering(config=__snake_case   )
								model.to(__snake_case   )
								model.eval()
								UpperCAmelCase								=  model(
								    __snake_case					,		attention_mask=__snake_case					,		token_type_ids=__snake_case					,		start_positions=__snake_case					,		end_positions=__snake_case					,		)
								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]   )					->       Dict:
								'''simple docstring'''







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





@require_torch
class 			__magic_name__ (				snake_case__,	snake_case__,	unittest.TestCase    ):
		lowercase   :							Optional[int]        =(
		    (
		        DebertaModel,
		        DebertaForMaskedLM,
		        DebertaForSequenceClassification,
		        DebertaForTokenClassification,
		        DebertaForQuestionAnswering,
		    )
		    if is_torch_available()
		    else ()
		)
		lowercase   :							int        =(
		    {
		        '''feature-extraction''': DebertaModel,
		        '''fill-mask''': DebertaForMaskedLM,
		        '''question-answering''': DebertaForQuestionAnswering,
		        '''text-classification''': DebertaForSequenceClassification,
		        '''token-classification''': DebertaForTokenClassification,
		        '''zero-shot''': DebertaForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)

		lowercase   :							Optional[Any]        =True
		lowercase   :							Any        =False
		lowercase   :							Dict        =False
		lowercase   :							str        =False
		lowercase   :							List[str]        =False






		def SCREAMING_SNAKE_CASE_      (    self   :  Optional[int]   )					->       Dict:
								'''simple docstring'''







								UpperCAmelCase								=  DebertaModelTester(self   )
								UpperCAmelCase								=  ConfigTester(self					,		config_class=__snake_case					,		hidden_size=37   )






		def SCREAMING_SNAKE_CASE_      (    self   :  Optional[int]   )					->       List[str]:
								'''simple docstring'''







								self.config_tester.run_common_tests()






		def SCREAMING_SNAKE_CASE_      (    self   :  Union[str, Any]   )					->       Optional[int]:
								'''simple docstring'''







								UpperCAmelCase								=  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_model(*__snake_case   )






		def SCREAMING_SNAKE_CASE_      (    self   :  Union[str, Any]   )					->       str:
								'''simple docstring'''







								UpperCAmelCase								=  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_sequence_classification(*__snake_case   )






		def SCREAMING_SNAKE_CASE_      (    self   :  Union[str, Any]   )					->       Dict:
								'''simple docstring'''







								UpperCAmelCase								=  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_masked_lm(*__snake_case   )






		def SCREAMING_SNAKE_CASE_      (    self   :  Any   )					->       Any:
								'''simple docstring'''







								UpperCAmelCase								=  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_question_answering(*__snake_case   )






		def SCREAMING_SNAKE_CASE_      (    self   :  List[Any]   )					->       List[Any]:
								'''simple docstring'''







								UpperCAmelCase								=  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_token_classification(*__snake_case   )







		@slow
		def SCREAMING_SNAKE_CASE_      (    self   :  List[Any]   )					->       List[Any]:
								'''simple docstring'''







								for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														UpperCAmelCase								=  DebertaModel.from_pretrained(__snake_case   )
														self.assertIsNotNone(__snake_case   )





@require_torch
@require_sentencepiece
@require_tokenizers
class 			__magic_name__ (				unittest.TestCase    ):






		@unittest.skip(reason="Model not available yet"   )
		def SCREAMING_SNAKE_CASE_      (    self   :  str   )					->       str:
								'''simple docstring'''







								pass







		@slow
		def SCREAMING_SNAKE_CASE_      (    self   :  Union[str, Any]   )					->       List[Any]:
								'''simple docstring'''







								UpperCAmelCase								=  DebertaModel.from_pretrained("microsoft/deberta-base"   )

								UpperCAmelCase								=  torch.tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]   )
								UpperCAmelCase								=  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
								with torch.no_grad():
														UpperCAmelCase								=  model(__snake_case					,		attention_mask=__snake_case   )[0]
								# compare the actual values for a slice.
								UpperCAmelCase								=  torch.tensor(
								    [[[-0.59_86, -0.80_55, -0.84_62], [1.44_84, -0.93_48, -0.80_59], [0.31_23, 0.00_32, -1.41_31]]]   )
								self.assertTrue(torch.allclose(output[:, 1:4, 1:4]					,		__snake_case					,		atol=1e-4   )					,		F'{output[:, 1:4, 1:4]}'   )







from collections import OrderedDict
from typing import Mapping

from packaging import version

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


_SCREAMING_SNAKE_CASE     :					Optional[Any]      =       logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE     :					int      =       {
    "facebook/deit-base-distilled-patch16-224": (
        "https://huggingface.co/facebook/deit-base-patch16-224/resolve/main/config.json"
    ),
    # See all DeiT models at https://huggingface.co/models?filter=deit
}

class     A__   (       snake_case__      ):







    """simple docstring"""

    __magic_name__         =						'deit'
    def __init__(			self     ,   __snake_case=7_6_8     ,   __snake_case=1_2     ,   __snake_case=1_2     ,   __snake_case=3_0_7_2     ,   __snake_case="gelu"     ,   __snake_case=0.0     ,   __snake_case=0.0     ,   __snake_case=0.02     ,   __snake_case=1E-12     ,   __snake_case=2_2_4     ,   __snake_case=1_6     ,   __snake_case=3     ,   __snake_case=True     ,   __snake_case=1_6     ,   **__snake_case     ,   ):
         super().__init__(**__snake_case       )

         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
         snake_case											=							encoder_stride






class     A__   (       snake_case__      ):







    """simple docstring"""

    __magic_name__         =						version.parse('1.11'      )
    @property
    def 						a_     (			self       ):
         return OrderedDict(
             [
                 ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
             ]       )




    @property
    def 						a_     (			self       ):
         return 1E-4




