

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 					__UpperCAmelCase     (       _a						):



   def __init__(							self							:     Union[str, Any],		__A							:     Dict,		__A							:     Any=1_3,		__A							:     Optional[int]=7,		__A							:     List[Any]=True,		__A							:     Dict=True,		__A							:     str=True,		__A							:     Union[str, Any]=True,		__A							:     Union[str, Any]=9_9,		__A							:     Any=3_2,		__A							:     Optional[Any]=5,		__A							:     Dict=4,		__A							:     str=3_7,		__A							:     str="gelu",		__A							:     List[str]=0.1,		__A							:     int=0.1,		__A							:     str=5_1_2,		__A							:     int=1_6,		__A							:     Dict=2,		__A							:     Optional[Any]=0.0_2,		__A							:     str=False,		__A							:     Any=True,		__A							:     Optional[int]="None",		__A							:     Tuple=3,		__A							:     Dict=4,		__A							:     List[Any]=None,		):
     UpperCAmelCase       :							Tuple  = parent
     UpperCAmelCase       :							Dict  = batch_size
     UpperCAmelCase       :							List[Any]  = seq_length
     UpperCAmelCase       :							Optional[Any]  = is_training
     UpperCAmelCase       :							Optional[Any]  = use_input_mask
     UpperCAmelCase       :							List[Any]  = use_token_type_ids
     UpperCAmelCase       :							Any  = use_labels
     UpperCAmelCase       :							Optional[int]  = vocab_size
     UpperCAmelCase       :							str  = hidden_size
     UpperCAmelCase       :							Any  = num_hidden_layers
     UpperCAmelCase       :							Union[str, Any]  = num_attention_heads
     UpperCAmelCase       :							Optional[Any]  = intermediate_size
     UpperCAmelCase       :							str  = hidden_act
     UpperCAmelCase       :							Optional[int]  = hidden_dropout_prob
     UpperCAmelCase       :							Any  = attention_probs_dropout_prob
     UpperCAmelCase       :							List[Any]  = max_position_embeddings
     UpperCAmelCase       :							str  = type_vocab_size
     UpperCAmelCase       :							List[Any]  = type_sequence_label_size
     UpperCAmelCase       :							List[Any]  = initializer_range
     UpperCAmelCase       :							Any  = num_labels
     UpperCAmelCase       :							Dict  = num_choices
     UpperCAmelCase       :							Any  = relative_attention
     UpperCAmelCase       :							Union[str, Any]  = position_biased_input
     UpperCAmelCase       :							Tuple  = pos_att_type
     UpperCAmelCase       :							List[Any]  = scope



   def        __magic_name__    (							self							:     int						):
     UpperCAmelCase       :							List[Any]  = ids_tensor([self.batch_size, self.seq_length],		self.vocab_size						)

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

     UpperCAmelCase       :							Dict  = None
     if self.use_token_type_ids:
       UpperCAmelCase       :							Tuple  = ids_tensor([self.batch_size, self.seq_length],		self.type_vocab_size						)

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

     UpperCAmelCase       :							Tuple  = self.get_config()

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



   def        __magic_name__    (							self							:     Tuple						):
     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        __magic_name__    (							self							:     Tuple						):
     UpperCAmelCase       :							Any  = self.get_config()
     UpperCAmelCase       :							int  = 3_0_0
     return config



   def        __magic_name__    (							self							:     Dict,		__A							:     List[str]						):
     self.parent.assertListEqual(list(result.loss.size()						),		[]						)



   def        __magic_name__    (							self							:     Any,		__A							:     str,		__A							:     Tuple,		__A							:     Optional[int],		__A							:     str,		__A							:     Dict,		__A							:     str,		__A							:     Optional[int]						):
     UpperCAmelCase       :							Dict  = DebertaModel(config=__UpperCAmelCase						)
     model.to(__UpperCAmelCase						)
     model.eval()
     UpperCAmelCase       :							Optional[int]  = model(__UpperCAmelCase,		attention_mask=__UpperCAmelCase,		token_type_ids=__UpperCAmelCase						)[0]
     UpperCAmelCase       :							Any  = model(__UpperCAmelCase,		token_type_ids=__UpperCAmelCase						)[0]
     UpperCAmelCase       :							Any  = model(__UpperCAmelCase						)[0]

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



   def        __magic_name__    (							self							:     Optional[Any],		__A							:     Union[str, Any],		__A							:     List[Any],		__A							:     Union[str, Any],		__A							:     int,		__A							:     Any,		__A							:     Any,		__A							:     int						):
     UpperCAmelCase       :							Any  = DebertaForMaskedLM(config=__UpperCAmelCase						)
     model.to(__UpperCAmelCase						)
     model.eval()
     UpperCAmelCase       :							Union[str, Any]  = model(__UpperCAmelCase,		attention_mask=__UpperCAmelCase,		token_type_ids=__UpperCAmelCase,		labels=__UpperCAmelCase						)

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



   def        __magic_name__    (							self							:     List[Any],		__A							:     List[str],		__A							:     Optional[int],		__A							:     Optional[int],		__A							:     int,		__A							:     Any,		__A							:     Optional[Any],		__A							:     Union[str, Any]						):
     UpperCAmelCase       :							List[str]  = self.num_labels
     UpperCAmelCase       :							Union[str, Any]  = DebertaForSequenceClassification(__UpperCAmelCase						)
     model.to(__UpperCAmelCase						)
     model.eval()
     UpperCAmelCase       :							Tuple  = model(__UpperCAmelCase,		attention_mask=__UpperCAmelCase,		token_type_ids=__UpperCAmelCase,		labels=__UpperCAmelCase						)
     self.parent.assertListEqual(list(result.logits.size()						),		[self.batch_size, self.num_labels]						)
     self.check_loss_output(__UpperCAmelCase						)



   def        __magic_name__    (							self							:     Dict,		__A							:     int,		__A							:     Dict,		__A							:     Optional[Any],		__A							:     List[str],		__A							:     Optional[int],		__A							:     Any,		__A							:     str						):
     UpperCAmelCase       :							Optional[Any]  = self.num_labels
     UpperCAmelCase       :							Tuple  = DebertaForTokenClassification(config=__UpperCAmelCase						)
     model.to(__UpperCAmelCase						)
     model.eval()
     UpperCAmelCase       :							Union[str, Any]  = model(__UpperCAmelCase,		attention_mask=__UpperCAmelCase,		token_type_ids=__UpperCAmelCase,		labels=__UpperCAmelCase						)
     self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.seq_length, self.num_labels)						)



   def        __magic_name__    (							self							:     Tuple,		__A							:     List[str],		__A							:     Dict,		__A							:     Union[str, Any],		__A							:     Optional[int],		__A							:     List[str],		__A							:     str,		__A							:     Optional[Any]						):
     UpperCAmelCase       :							Optional[int]  = DebertaForQuestionAnswering(config=__UpperCAmelCase						)
     model.to(__UpperCAmelCase						)
     model.eval()
     UpperCAmelCase       :							Optional[int]  = model(
         __UpperCAmelCase,		attention_mask=__UpperCAmelCase,		token_type_ids=__UpperCAmelCase,		start_positions=__UpperCAmelCase,		end_positions=__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        __magic_name__    (							self							:     int						):
     UpperCAmelCase       :							Union[str, Any]  = self.prepare_config_and_inputs()
     (
         UpperCAmelCase		
     )       :							Tuple  = config_and_inputs
     UpperCAmelCase       :							int  = {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
     return config, inputs_dict






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

   UpperCamelCase        =     True
   UpperCamelCase        =     False
   UpperCamelCase        =     False
   UpperCamelCase        =     False
   UpperCamelCase        =     False



   def        __magic_name__    (							self							:     List[str]						):
     UpperCAmelCase       :							int  = DebertaModelTester(self						)
     UpperCAmelCase       :							Optional[int]  = ConfigTester(self,		config_class=__UpperCAmelCase,		hidden_size=3_7						)



   def        __magic_name__    (							self							:     Tuple						):
     self.config_tester.run_common_tests()



   def        __magic_name__    (							self							:     List[Any]						):
     UpperCAmelCase       :							Optional[int]  = self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_model(*__UpperCAmelCase						)



   def        __magic_name__    (							self							:     Union[str, Any]						):
     UpperCAmelCase       :							str  = self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_sequence_classification(*__UpperCAmelCase						)



   def        __magic_name__    (							self							:     List[Any]						):
     UpperCAmelCase       :							Union[str, Any]  = self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_masked_lm(*__UpperCAmelCase						)



   def        __magic_name__    (							self							:     int						):
     UpperCAmelCase       :							int  = self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_question_answering(*__UpperCAmelCase						)



   def        __magic_name__    (							self							:     str						):
     UpperCAmelCase       :							List[Any]  = self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_token_classification(*__UpperCAmelCase						)





   @slow
   def        __magic_name__    (							self							:     str						):
     for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCAmelCase       :							str  = DebertaModel.from_pretrained(__UpperCAmelCase						)
       self.assertIsNotNone(__UpperCAmelCase						)






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



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





   @slow
   def        __magic_name__    (							self							:     int						):
     UpperCAmelCase       :							Dict  = DebertaModel.from_pretrained('''microsoft/deberta-base'''						)

     UpperCAmelCase       :							Optional[Any]  = torch.tensor([[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]]						)
     UpperCAmelCase       :							Any  = torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
     with torch.no_grad():
       UpperCAmelCase       :							int  = model(__UpperCAmelCase,		attention_mask=__UpperCAmelCase						)[0]
     # compare the actual values for a slice.
     UpperCAmelCase       :							Optional[int]  = torch.tensor(
         [[[-0.5_9_8_6, -0.8_0_5_5, -0.8_4_6_2], [1.4_4_8_4, -0.9_3_4_8, -0.8_0_5_9], [0.3_1_2_3, 0.0_0_3_2, -1.4_1_3_1]]]						)
     self.assertTrue(torch.allclose(output[:, 1:4, 1:4],		__UpperCAmelCase,		atol=1E-4						),		F'''{output[:, 1:4, 1:4]}'''						)









"""simple docstring"""







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


__lowercase    					=      logging.get_logger(__name__)

__lowercase    					=      {
    """facebook/dpr-ctx_encoder-single-nq-base""": (
        """https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/config.json"""
    ),
    """facebook/dpr-question_encoder-single-nq-base""": (
        """https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/config.json"""
    ),
    """facebook/dpr-reader-single-nq-base""": (
        """https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/config.json"""
    ),
    """facebook/dpr-ctx_encoder-multiset-base""": (
        """https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/config.json"""
    ),
    """facebook/dpr-question_encoder-multiset-base""": (
        """https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/config.json"""
    ),
    """facebook/dpr-reader-multiset-base""": (
        """https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/config.json"""
    ),
}






class 						_A  (  _a  ):







    """simple docstring"""




    UpperCAmelCase       :							int	=       """dpr"""






    def __init__(					self				:    List[Any]				, __UpperCAmelCase				:    int=30522				, __UpperCAmelCase				:    Union[str, Any]=768				, __UpperCAmelCase				:    Dict=12				, __UpperCAmelCase				:    List[str]=12				, __UpperCAmelCase				:    Any=3072				, __UpperCAmelCase				:    Optional[int]="gelu"				, __UpperCAmelCase				:    Any=0.1				, __UpperCAmelCase				:    Union[str, Any]=0.1				, __UpperCAmelCase				:    str=512				, __UpperCAmelCase				:    List[str]=2				, __UpperCAmelCase				:    Tuple=0.02				, __UpperCAmelCase				:    List[str]=1e-12				, __UpperCAmelCase				:    List[str]=0				, __UpperCAmelCase				:    str="absolute"				, __UpperCAmelCase				:    int = 0				, **__UpperCAmelCase				:    Tuple				, ):
      super().__init__(pad_token_id=__UpperCAmelCase				, **__UpperCAmelCase)

      a						:		List[Any]      							=			vocab_size
      a						:		Optional[Any]      							=			hidden_size
      a						:		Union[str, Any]      							=			num_hidden_layers
      a						:		Dict      							=			num_attention_heads
      a						:		int      							=			hidden_act
      a						:		Any      							=			intermediate_size
      a						:		Any      							=			hidden_dropout_prob
      a						:		Dict      							=			attention_probs_dropout_prob
      a						:		Any      							=			max_position_embeddings
      a						:		Union[str, Any]      							=			type_vocab_size
      a						:		Optional[Any]      							=			initializer_range
      a						:		Dict      							=			layer_norm_eps
      a						:		int      							=			projection_dim
      a						:		str      							=			position_embedding_type






