





'''simple docstring'''


import warnings

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


lowerCAmelCase :					Any 							=logging.get_logger(__name__)

lowerCAmelCase :					Optional[Any] 							={
    '''RUCAIBox/mvp''': '''https://huggingface.co/RUCAIBox/mvp/resolve/main/config.json''',
}
class 				a_       (    _lowerCAmelCase							):
     __A								=      "mvp"
     __A								=      ["past_key_values"]
     __A								=      {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}





     def __init__(			self		: List[Any]     ,				lowercase		: List[str]=50_267     ,				lowercase		: Dict=1_024     ,				lowercase		: str=12     ,				lowercase		: List[Any]=4_096     ,				lowercase		: List[Any]=16     ,				lowercase		: List[str]=12     ,				lowercase		: Any=4_096     ,				lowercase		: str=16     ,				lowercase		: List[str]=0.0     ,				lowercase		: List[str]=0.0     ,				lowercase		: Union[str, Any]="gelu"     ,				lowercase		: Optional[Any]=1_024     ,				lowercase		: List[Any]=0.1     ,				lowercase		: List[Any]=0.0     ,				lowercase		: Tuple=0.0     ,				lowercase		: str=0.02     ,				lowercase		: List[Any]=0.0     ,				lowercase		: int=False     ,				lowercase		: Optional[Any]=True     ,				lowercase		: Optional[Any]=1     ,				lowercase		: Tuple=0     ,				lowercase		: int=2     ,				lowercase		: Tuple=True     ,				lowercase		: Any=2     ,				lowercase		: str=2     ,				lowercase		: Optional[Any]=False     ,				lowercase		: List[Any]=100     ,				lowercase		: Optional[Any]=800     ,				**lowercase		: Any     ,				):





        """simple docstring"""




        lowercase_      :Optional[int]         =			vocab_size
        lowercase_      :Tuple         =			max_position_embeddings
        lowercase_      :int         =			d_model
        lowercase_      :Union[str, Any]         =			encoder_ffn_dim
        lowercase_      :Dict         =			encoder_layers
        lowercase_      :Optional[int]         =			encoder_attention_heads
        lowercase_      :Optional[int]         =			decoder_ffn_dim
        lowercase_      :Dict         =			decoder_layers
        lowercase_      :List[Any]         =			decoder_attention_heads
        lowercase_      :Any         =			dropout
        lowercase_      :Optional[int]         =			attention_dropout
        lowercase_      :List[Any]         =			activation_dropout
        lowercase_      :Tuple         =			activation_function
        lowercase_      :Union[str, Any]         =			init_std
        lowercase_      :Optional[Any]         =			encoder_layerdrop
        lowercase_      :Dict         =			decoder_layerdrop
        lowercase_      :str         =			classifier_dropout
        lowercase_      :List[str]         =			use_cache
        lowercase_      :Union[str, Any]         =			encoder_layers
        lowercase_      :Optional[int]         =			scale_embedding  # scale factor will be sqrt(d_model) if True
        lowercase_      :Tuple         =			use_prompt
        lowercase_      :Tuple         =			prompt_length
        lowercase_      :Tuple         =			prompt_mid_dim

        super().__init__(
            pad_token_id=lowercase     ,				bos_token_id=lowercase     ,				eos_token_id=lowercase     ,				is_encoder_decoder=lowercase     ,				decoder_start_token_id=lowercase     ,				forced_eos_token_id=lowercase     ,				**lowercase     ,				)

        if self.forced_bos_token_id is None and kwargs.get("force_bos_token_to_be_generated"     ,				lowercase						):
           lowercase_      :Union[str, Any]         =			self.bos_token_id
           warnings.warn(
               F'Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. '
               "The config can simply be saved and uploaded again to be fixed."						)









'''simple docstring'''


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 				a_       (    _lowerCAmelCase							):





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





        """simple docstring"""




        lowercase_      :int         =			parent
        lowercase_      :str         =			batch_size
        lowercase_      :Tuple         =			seq_length
        lowercase_      :Union[str, Any]         =			is_training
        lowercase_      :Dict         =			use_input_mask
        lowercase_      :Any         =			use_token_type_ids
        lowercase_      :Tuple         =			use_labels
        lowercase_      :Dict         =			vocab_size
        lowercase_      :Tuple         =			hidden_size
        lowercase_      :Union[str, Any]         =			num_hidden_layers
        lowercase_      :int         =			num_attention_heads
        lowercase_      :List[Any]         =			intermediate_size
        lowercase_      :Tuple         =			hidden_act
        lowercase_      :str         =			hidden_dropout_prob
        lowercase_      :Any         =			attention_probs_dropout_prob
        lowercase_      :List[Any]         =			max_position_embeddings
        lowercase_      :Union[str, Any]         =			type_vocab_size
        lowercase_      :Union[str, Any]         =			type_sequence_label_size
        lowercase_      :Any         =			initializer_range
        lowercase_      :List[Any]         =			num_labels
        lowercase_      :str         =			num_choices
        lowercase_      :Optional[Any]         =			relative_attention
        lowercase_      :Tuple         =			position_biased_input
        lowercase_      :Union[str, Any]         =			pos_att_type
        lowercase_      :Tuple         =			scope





     def 							lowercase__				(			self		: Dict						):





        """simple docstring"""




        lowercase_      :List[str]         =			ids_tensor([self.batch_size, self.seq_length]     ,				self.vocab_size						)

        lowercase_      :Union[str, Any]         =			None
        if self.use_input_mask:
           lowercase_      :int         =			ids_tensor([self.batch_size, self.seq_length]     ,				vocab_size=2						)

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

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

        lowercase_      :Optional[Any]         =			self.get_config()

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





     def 							lowercase__				(			self		: Optional[Any]						):





        """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 							lowercase__				(			self		: Optional[int]						):





        """simple docstring"""




        lowercase_      :Dict         =			self.get_config()
        lowercase_      :Optional[Any]         =			300
        return config





     def 							lowercase__				(			self		: Optional[Any]     ,				lowercase		: Dict						):





        """simple docstring"""




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





     def 							lowercase__				(			self		: Optional[Any]     ,				lowercase		: Union[str, Any]     ,				lowercase		: List[Any]     ,				lowercase		: List[Any]     ,				lowercase		: Tuple     ,				lowercase		: str     ,				lowercase		: Optional[Any]     ,				lowercase		: Optional[int]						):





        """simple docstring"""




        lowercase_      :str         =			DebertaModel(config=lowercase						)
        model.to(lowercase						)
        model.eval()
        lowercase_      :Optional[int]         =			model(lowercase     ,				attention_mask=lowercase     ,				token_type_ids=lowercase						)[0]
        lowercase_      :Union[str, Any]         =			model(lowercase     ,				token_type_ids=lowercase						)[0]
        lowercase_      :Dict         =			model(lowercase						)[0]

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





     def 							lowercase__				(			self		: Dict     ,				lowercase		: Union[str, Any]     ,				lowercase		: Optional[int]     ,				lowercase		: Tuple     ,				lowercase		: Dict     ,				lowercase		: Dict     ,				lowercase		: str     ,				lowercase		: Tuple						):





        """simple docstring"""




        lowercase_      :Dict         =			DebertaForMaskedLM(config=lowercase						)
        model.to(lowercase						)
        model.eval()
        lowercase_      :Optional[Any]         =			model(lowercase     ,				attention_mask=lowercase     ,				token_type_ids=lowercase     ,				labels=lowercase						)

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





     def 							lowercase__				(			self		: Optional[int]     ,				lowercase		: Union[str, Any]     ,				lowercase		: Union[str, Any]     ,				lowercase		: Union[str, Any]     ,				lowercase		: Dict     ,				lowercase		: List[Any]     ,				lowercase		: int     ,				lowercase		: Dict						):





        """simple docstring"""




        lowercase_      :Dict         =			self.num_labels
        lowercase_      :int         =			DebertaForSequenceClassification(lowercase						)
        model.to(lowercase						)
        model.eval()
        lowercase_      :Union[str, Any]         =			model(lowercase     ,				attention_mask=lowercase     ,				token_type_ids=lowercase     ,				labels=lowercase						)
        self.parent.assertListEqual(list(result.logits.size()						)     ,				[self.batch_size, self.num_labels]						)
        self.check_loss_output(lowercase						)





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





        """simple docstring"""




        lowercase_      :List[str]         =			self.num_labels
        lowercase_      :Optional[int]         =			DebertaForTokenClassification(config=lowercase						)
        model.to(lowercase						)
        model.eval()
        lowercase_      :Dict         =			model(lowercase     ,				attention_mask=lowercase     ,				token_type_ids=lowercase     ,				labels=lowercase						)
        self.parent.assertEqual(result.logits.shape     ,				(self.batch_size, self.seq_length, self.num_labels)						)





     def 							lowercase__				(			self		: Union[str, Any]     ,				lowercase		: Tuple     ,				lowercase		: Any     ,				lowercase		: List[Any]     ,				lowercase		: List[Any]     ,				lowercase		: Union[str, Any]     ,				lowercase		: Optional[int]     ,				lowercase		: List[Any]						):





        """simple docstring"""




        lowercase_      :Any         =			DebertaForQuestionAnswering(config=lowercase						)
        model.to(lowercase						)
        model.eval()
        lowercase_      :List[Any]         =			model(
            lowercase     ,				attention_mask=lowercase     ,				token_type_ids=lowercase     ,				start_positions=lowercase     ,				end_positions=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 							lowercase__				(			self		: Any						):





        """simple docstring"""




        lowercase_      :Optional[int]         =			self.prepare_config_and_inputs()
        (
            (
            lowercase_
        )      ,							(
            lowercase_
        )      ,							(
            lowercase_
        )      ,							(
            lowercase_
        )      ,							(
            lowercase_
        )      ,							(
            lowercase_
        )      ,							(
            lowercase_
        )      ,							
        )      :List[str]         =			config_and_inputs
        lowercase_      :Tuple         =			{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
        return config, inputs_dict







@require_torch
class 				a_       (    _lowerCAmelCase     ,						_lowerCAmelCase     ,						unittest.TestCase							):
     __A								=      (
         (
             DebertaModel,
             DebertaForMaskedLM,
             DebertaForSequenceClassification,
             DebertaForTokenClassification,
             DebertaForQuestionAnswering,
         )
         if is_torch_available()
         else ()
     )
     __A								=      (
         {
             "feature-extraction": DebertaModel,
             "fill-mask": DebertaForMaskedLM,
             "question-answering": DebertaForQuestionAnswering,
             "text-classification": DebertaForSequenceClassification,
             "token-classification": DebertaForTokenClassification,
             "zero-shot": DebertaForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )

     __A								=      True
     __A								=      False
     __A								=      False
     __A								=      False
     __A								=      False





     def 							lowercase__				(			self		: Optional[int]						):





        """simple docstring"""




        lowercase_      :List[Any]         =			DebertaModelTester(self						)
        lowercase_      :str         =			ConfigTester(self     ,				config_class=lowercase     ,				hidden_size=37						)





     def 							lowercase__				(			self		: Union[str, Any]						):





        """simple docstring"""




        self.config_tester.run_common_tests()





     def 							lowercase__				(			self		: Union[str, Any]						):





        """simple docstring"""




        lowercase_      :str         =			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_deberta_model(*lowercase						)





     def 							lowercase__				(			self		: Union[str, Any]						):





        """simple docstring"""




        lowercase_      :Optional[Any]         =			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_deberta_for_sequence_classification(*lowercase						)





     def 							lowercase__				(			self		: Union[str, Any]						):





        """simple docstring"""




        lowercase_      :Optional[int]         =			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_deberta_for_masked_lm(*lowercase						)





     def 							lowercase__				(			self		: int						):





        """simple docstring"""




        lowercase_      :Union[str, Any]         =			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_deberta_for_question_answering(*lowercase						)





     def 							lowercase__				(			self		: Tuple						):





        """simple docstring"""




        lowercase_      :Union[str, Any]         =			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_deberta_for_token_classification(*lowercase						)







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





        """simple docstring"""




        for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           lowercase_      :Tuple         =			DebertaModel.from_pretrained(lowercase						)
           self.assertIsNotNone(lowercase						)







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





     @unittest.skip(reason="Model not available yet"						)
     def 							lowercase__				(			self		: Dict						):





        """simple docstring"""




        pass







     @slow
     def 							lowercase__				(			self		: List[Any]						):





        """simple docstring"""




        lowercase_      :Optional[Any]         =			DebertaModel.from_pretrained("microsoft/deberta-base"						)

        lowercase_      :Dict         =			torch.tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]						)
        lowercase_      :Optional[Any]         =			torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
        with torch.no_grad():
           lowercase_      :Optional[int]         =			model(lowercase     ,				attention_mask=lowercase						)[0]
        # compare the actual values for a slice.
        lowercase_      :List[Any]         =			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]     ,				lowercase     ,				atol=1e-4						)     ,				F'{output[:, 1:4, 1:4]}'						)


