



"""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__     (						__SCREAMING_SNAKE_CASE							):




 '''simple docstring'''





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

    """simple docstring"""

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

 def 							_SCREAMING_SNAKE_CASE     (  self:	Optional[int])       -> Tuple:

    """simple docstring"""

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

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

    __lowerCAmelCase   :						Tuple				= None
    if self.use_token_type_ids:
       __lowerCAmelCase   :						Optional[Any]				= ids_tensor([self.batch_size, self.seq_length] ,    self.type_vocab_size)

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

    __lowerCAmelCase   :						str				= self.get_config()

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

 def 							_SCREAMING_SNAKE_CASE     (  self:	Any)       -> Tuple:

    """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:	str)       -> str:

    """simple docstring"""

    __lowerCAmelCase   :						List[Any]				= self.get_config()
    __lowerCAmelCase   :						Tuple				= 300
    return config

 def 							_SCREAMING_SNAKE_CASE     (  self:	Dict ,    _SCREAMING_SNAKE_CASE:	Optional[int])       -> Union[str, Any]:

    """simple docstring"""

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

 def 							_SCREAMING_SNAKE_CASE     (  self:	int ,    _SCREAMING_SNAKE_CASE:	Optional[Any] ,    _SCREAMING_SNAKE_CASE:	Optional[int] ,    _SCREAMING_SNAKE_CASE:	str ,    _SCREAMING_SNAKE_CASE:	List[str] ,    _SCREAMING_SNAKE_CASE:	Optional[Any] ,    _SCREAMING_SNAKE_CASE:	List[str] ,    _SCREAMING_SNAKE_CASE:	List[Any])       -> Union[str, Any]:

    """simple docstring"""

    __lowerCAmelCase   :						List[str]				= DebertaModel(config=_SCREAMING_SNAKE_CASE)
    model.to(_SCREAMING_SNAKE_CASE)
    model.eval()
    __lowerCAmelCase   :						List[str]				= model(_SCREAMING_SNAKE_CASE ,    attention_mask=_SCREAMING_SNAKE_CASE ,    token_type_ids=_SCREAMING_SNAKE_CASE)[0]
    __lowerCAmelCase   :						List[str]				= model(_SCREAMING_SNAKE_CASE ,    token_type_ids=_SCREAMING_SNAKE_CASE)[0]
    __lowerCAmelCase   :						Union[str, Any]				= model(_SCREAMING_SNAKE_CASE)[0]

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

 def 							_SCREAMING_SNAKE_CASE     (  self:	Optional[Any] ,    _SCREAMING_SNAKE_CASE:	Union[str, Any] ,    _SCREAMING_SNAKE_CASE:	Optional[Any] ,    _SCREAMING_SNAKE_CASE:	Tuple ,    _SCREAMING_SNAKE_CASE:	int ,    _SCREAMING_SNAKE_CASE:	str ,    _SCREAMING_SNAKE_CASE:	Optional[Any] ,    _SCREAMING_SNAKE_CASE:	int)       -> List[str]:

    """simple docstring"""

    __lowerCAmelCase   :						Any				= DebertaForMaskedLM(config=_SCREAMING_SNAKE_CASE)
    model.to(_SCREAMING_SNAKE_CASE)
    model.eval()
    __lowerCAmelCase   :						Tuple				= model(_SCREAMING_SNAKE_CASE ,    attention_mask=_SCREAMING_SNAKE_CASE ,    token_type_ids=_SCREAMING_SNAKE_CASE ,    labels=_SCREAMING_SNAKE_CASE)

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

 def 							_SCREAMING_SNAKE_CASE     (  self:	str ,    _SCREAMING_SNAKE_CASE:	Union[str, Any] ,    _SCREAMING_SNAKE_CASE:	Optional[Any] ,    _SCREAMING_SNAKE_CASE:	int ,    _SCREAMING_SNAKE_CASE:	List[str] ,    _SCREAMING_SNAKE_CASE:	Dict ,    _SCREAMING_SNAKE_CASE:	str ,    _SCREAMING_SNAKE_CASE:	Union[str, Any])       -> str:

    """simple docstring"""

    __lowerCAmelCase   :						Dict				= self.num_labels
    __lowerCAmelCase   :						Tuple				= DebertaForSequenceClassification(_SCREAMING_SNAKE_CASE)
    model.to(_SCREAMING_SNAKE_CASE)
    model.eval()
    __lowerCAmelCase   :						str				= model(_SCREAMING_SNAKE_CASE ,    attention_mask=_SCREAMING_SNAKE_CASE ,    token_type_ids=_SCREAMING_SNAKE_CASE ,    labels=_SCREAMING_SNAKE_CASE)
    self.parent.assertListEqual(list(result.logits.size()) ,    [self.batch_size, self.num_labels])
    self.check_loss_output(_SCREAMING_SNAKE_CASE)

 def 							_SCREAMING_SNAKE_CASE     (  self:	Union[str, Any] ,    _SCREAMING_SNAKE_CASE:	List[Any] ,    _SCREAMING_SNAKE_CASE:	List[str] ,    _SCREAMING_SNAKE_CASE:	Tuple ,    _SCREAMING_SNAKE_CASE:	int ,    _SCREAMING_SNAKE_CASE:	Optional[int] ,    _SCREAMING_SNAKE_CASE:	List[str] ,    _SCREAMING_SNAKE_CASE:	str)       -> Union[str, Any]:

    """simple docstring"""

    __lowerCAmelCase   :						Dict				= self.num_labels
    __lowerCAmelCase   :						Optional[Any]				= DebertaForTokenClassification(config=_SCREAMING_SNAKE_CASE)
    model.to(_SCREAMING_SNAKE_CASE)
    model.eval()
    __lowerCAmelCase   :						Union[str, Any]				= model(_SCREAMING_SNAKE_CASE ,    attention_mask=_SCREAMING_SNAKE_CASE ,    token_type_ids=_SCREAMING_SNAKE_CASE ,    labels=_SCREAMING_SNAKE_CASE)
    self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.seq_length, self.num_labels))

 def 							_SCREAMING_SNAKE_CASE     (  self:	Dict ,    _SCREAMING_SNAKE_CASE:	List[str] ,    _SCREAMING_SNAKE_CASE:	Optional[int] ,    _SCREAMING_SNAKE_CASE:	Union[str, Any] ,    _SCREAMING_SNAKE_CASE:	Dict ,    _SCREAMING_SNAKE_CASE:	Union[str, Any] ,    _SCREAMING_SNAKE_CASE:	Union[str, Any] ,    _SCREAMING_SNAKE_CASE:	Tuple)       -> List[str]:

    """simple docstring"""

    __lowerCAmelCase   :						Tuple				= DebertaForQuestionAnswering(config=_SCREAMING_SNAKE_CASE)
    model.to(_SCREAMING_SNAKE_CASE)
    model.eval()
    __lowerCAmelCase   :						str				= model(
        _SCREAMING_SNAKE_CASE ,    attention_mask=_SCREAMING_SNAKE_CASE ,    token_type_ids=_SCREAMING_SNAKE_CASE ,    start_positions=_SCREAMING_SNAKE_CASE ,    end_positions=_SCREAMING_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:	Tuple)       -> Optional[Any]:

    """simple docstring"""

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






@require_torch
class     A__     (						__SCREAMING_SNAKE_CASE						,	__SCREAMING_SNAKE_CASE						,	unittest.TestCase							):




 '''simple docstring'''





 SCREAMING_SNAKE_CASE					   =			(
     (
         DebertaModel,
         DebertaForMaskedLM,
         DebertaForSequenceClassification,
         DebertaForTokenClassification,
         DebertaForQuestionAnswering,
     )
     if is_torch_available()
     else ()
 )
 SCREAMING_SNAKE_CASE					   =			(
     {
         'feature-extraction': DebertaModel,
         'fill-mask': DebertaForMaskedLM,
         'question-answering': DebertaForQuestionAnswering,
         'text-classification': DebertaForSequenceClassification,
         'token-classification': DebertaForTokenClassification,
         'zero-shot': DebertaForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )

 SCREAMING_SNAKE_CASE					   =			True
 SCREAMING_SNAKE_CASE					   =			False
 SCREAMING_SNAKE_CASE					   =			False
 SCREAMING_SNAKE_CASE					   =			False
 SCREAMING_SNAKE_CASE					   =			False

 def 							_SCREAMING_SNAKE_CASE     (  self:	Dict)       -> int:

    """simple docstring"""

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

 def 							_SCREAMING_SNAKE_CASE     (  self:	int)       -> str:

    """simple docstring"""

    self.config_tester.run_common_tests()

 def 							_SCREAMING_SNAKE_CASE     (  self:	Dict)       -> List[str]:

    """simple docstring"""

    __lowerCAmelCase   :						int				= self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_deberta_model(*_SCREAMING_SNAKE_CASE)

 def 							_SCREAMING_SNAKE_CASE     (  self:	Optional[int])       -> Union[str, Any]:

    """simple docstring"""

    __lowerCAmelCase   :						Dict				= self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_deberta_for_sequence_classification(*_SCREAMING_SNAKE_CASE)

 def 							_SCREAMING_SNAKE_CASE     (  self:	List[Any])       -> List[str]:

    """simple docstring"""

    __lowerCAmelCase   :						int				= self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_deberta_for_masked_lm(*_SCREAMING_SNAKE_CASE)

 def 							_SCREAMING_SNAKE_CASE     (  self:	Any)       -> int:

    """simple docstring"""

    __lowerCAmelCase   :						Optional[Any]				= self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_deberta_for_question_answering(*_SCREAMING_SNAKE_CASE)

 def 							_SCREAMING_SNAKE_CASE     (  self:	Any)       -> Optional[int]:

    """simple docstring"""

    __lowerCAmelCase   :						Optional[int]				= self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_deberta_for_token_classification(*_SCREAMING_SNAKE_CASE)







 @slow
 def 							_SCREAMING_SNAKE_CASE     (  self:	Optional[Any])       -> Optional[int]:

    """simple docstring"""

    for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __lowerCAmelCase   :						int				= DebertaModel.from_pretrained(_SCREAMING_SNAKE_CASE)
       self.assertIsNotNone(_SCREAMING_SNAKE_CASE)






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




 '''simple docstring'''





 @unittest.skip(reason="Model not available yet")
 def 							_SCREAMING_SNAKE_CASE     (  self:	int)       -> str:

    """simple docstring"""

    pass







 @slow
 def 							_SCREAMING_SNAKE_CASE     (  self:	Tuple)       -> Tuple:

    """simple docstring"""

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

    __lowerCAmelCase   :						Tuple				= torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]])
    __lowerCAmelCase   :						Union[str, Any]				= torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
    with torch.no_grad():
       __lowerCAmelCase   :						List[str]				= model(_SCREAMING_SNAKE_CASE ,    attention_mask=_SCREAMING_SNAKE_CASE)[0]
    # compare the actual values for a slice.
    __lowerCAmelCase   :						Any				= torch.tensor(
        [[[-0.5986, -0.8055, -0.8462], [1.4484, -0.9348, -0.8059], [0.3123, 0.0032, -1.4131]]])
    self.assertTrue(torch.allclose(output[:, 1:4, 1:4] ,    _SCREAMING_SNAKE_CASE ,    atol=1e-4) ,    F"""{output[:, 1:4, 1:4]}""")




"""simple docstring"""






from ..utils import DummyObject, requires_backends



class     A__     (						metaclass=__SCREAMING_SNAKE_CASE							):




	'''simple docstring'''





	SCREAMING_SNAKE_CASE					   =			['torch', 'scipy']

	def __init__(  self:	Tuple ,    *_SCREAMING_SNAKE_CASE:	Dict ,    **_SCREAMING_SNAKE_CASE:	str)       -> Union[str, Any]:

				"""simple docstring"""

				requires_backends(self ,    ["torch", "scipy"])

	@classmethod
	def 							_SCREAMING_SNAKE_CASE     (  cls:	Optional[Any] ,    *_SCREAMING_SNAKE_CASE:	Tuple ,    **_SCREAMING_SNAKE_CASE:	Optional[Any])       -> Optional[Any]:

				"""simple docstring"""

				requires_backends(cls ,    ["torch", "scipy"])







	@classmethod
	def 							_SCREAMING_SNAKE_CASE     (  cls:	int ,    *_SCREAMING_SNAKE_CASE:	List[Any] ,    **_SCREAMING_SNAKE_CASE:	Dict)       -> Union[str, Any]:

				"""simple docstring"""

				requires_backends(cls ,    ["torch", "scipy"])