




"""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      UpperCAmelCase_  (						SCREAMING_SNAKE_CASE__							):




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




     '''simple docstring'''

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




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




     '''simple docstring'''

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

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

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

     A__	=    None
     A__	=    None
     A__	=    None
     if self.use_labels:
      A__	=    ids_tensor([self.batch_size]     ,			self.type_sequence_label_size				)
      A__	=    ids_tensor([self.batch_size, self.seq_length]     ,			self.num_labels				)
      A__	=    ids_tensor([self.batch_size]     ,			self.num_choices				)

     A__	=    self.get_config()

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




    def 							__magic_name__							(   self						:	str				)				->							Dict:




     '''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 							__magic_name__							(   self						:	Dict				)				->							Optional[int]:




     '''simple docstring'''

     A__	=    self.get_config()
     A__	=    300
     return config




    def 							__magic_name__							(   self						:	Optional[int]     ,			snake_case_						:	Tuple				)				->							List[Any]:




     '''simple docstring'''

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




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




     '''simple docstring'''

     A__	=    DebertaModel(config=_SCREAMING_SNAKE_CASE				)
     model.to(_SCREAMING_SNAKE_CASE				)
     model.eval()
     A__	=    model(_SCREAMING_SNAKE_CASE     ,			attention_mask=_SCREAMING_SNAKE_CASE     ,			token_type_ids=_SCREAMING_SNAKE_CASE				)[0]
     A__	=    model(_SCREAMING_SNAKE_CASE     ,			token_type_ids=_SCREAMING_SNAKE_CASE				)[0]
     A__	=    model(_SCREAMING_SNAKE_CASE				)[0]

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




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




     '''simple docstring'''

     A__	=    DebertaForMaskedLM(config=_SCREAMING_SNAKE_CASE				)
     model.to(_SCREAMING_SNAKE_CASE				)
     model.eval()
     A__	=    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 							__magic_name__							(   self						:	List[Any]     ,			snake_case_						:	Tuple     ,			snake_case_						:	Union[str, Any]     ,			snake_case_						:	Dict     ,			snake_case_						:	List[str]     ,			snake_case_						:	Union[str, Any]     ,			snake_case_						:	Dict     ,			snake_case_						:	Optional[int]				)				->							Union[str, Any]:




     '''simple docstring'''

     A__	=    self.num_labels
     A__	=    DebertaForSequenceClassification(_SCREAMING_SNAKE_CASE				)
     model.to(_SCREAMING_SNAKE_CASE				)
     model.eval()
     A__	=    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 							__magic_name__							(   self						:	Dict     ,			snake_case_						:	str     ,			snake_case_						:	int     ,			snake_case_						:	Optional[int]     ,			snake_case_						:	str     ,			snake_case_						:	Optional[int]     ,			snake_case_						:	Any     ,			snake_case_						:	Union[str, Any]				)				->							Optional[Any]:




     '''simple docstring'''

     A__	=    self.num_labels
     A__	=    DebertaForTokenClassification(config=_SCREAMING_SNAKE_CASE				)
     model.to(_SCREAMING_SNAKE_CASE				)
     model.eval()
     A__	=    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 							__magic_name__							(   self						:	Union[str, Any]     ,			snake_case_						:	Optional[Any]     ,			snake_case_						:	str     ,			snake_case_						:	str     ,			snake_case_						:	Dict     ,			snake_case_						:	str     ,			snake_case_						:	int     ,			snake_case_						:	List[Any]				)				->							str:




     '''simple docstring'''

     A__	=    DebertaForQuestionAnswering(config=_SCREAMING_SNAKE_CASE				)
     model.to(_SCREAMING_SNAKE_CASE				)
     model.eval()
     A__	=    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 							__magic_name__							(   self						:	List[Any]				)				->							int:




     '''simple docstring'''

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






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

    lowercase__						 =				True
    lowercase__						 =				False
    lowercase__						 =				False
    lowercase__						 =				False
    lowercase__						 =				False




    def 							__magic_name__							(   self						:	str				)				->							Tuple:




     '''simple docstring'''

     A__	=    DebertaModelTester(self				)
     A__	=    ConfigTester(self     ,			config_class=_SCREAMING_SNAKE_CASE     ,			hidden_size=37				)




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




     '''simple docstring'''

     self.config_tester.run_common_tests()




    def 							__magic_name__							(   self						:	str				)				->							Optional[Any]:




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_model(*_SCREAMING_SNAKE_CASE				)




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




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_sequence_classification(*_SCREAMING_SNAKE_CASE				)




    def 							__magic_name__							(   self						:	Union[str, Any]				)				->							str:




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_masked_lm(*_SCREAMING_SNAKE_CASE				)




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




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_question_answering(*_SCREAMING_SNAKE_CASE				)




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




     '''simple docstring'''

     A__	=    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_deberta_for_token_classification(*_SCREAMING_SNAKE_CASE				)




    @slow
    def 							__magic_name__							(   self						:	str				)				->							Optional[Any]:




     '''simple docstring'''

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






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




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




     '''simple docstring'''

     pass




    @slow
    def 							__magic_name__							(   self						:	Tuple				)				->							str:




     '''simple docstring'''

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

     A__	=    torch.tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]				)
     A__	=    torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]				)
     with torch.no_grad():
      A__	=    model(_SCREAMING_SNAKE_CASE     ,			attention_mask=_SCREAMING_SNAKE_CASE				)[0]
     # compare the actual values for a slice.
     A__	=    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"""



import baseaa







def   _SCREAMING_SNAKE_CASE       (							lowercase_						)						->		bytes:
 return baseaa.aaaencode(string.encode("utf-8"						)						)







def   _SCREAMING_SNAKE_CASE       (							lowercase_						)						->		str:
 return baseaa.aaadecode(lowercase_						).decode("utf-8"						)


if __name__ == "__main__":
      import doctest

      doctest.testmod()





