





'''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		:					Dict	,      lowercase		:					Optional[Any]	,      lowercase		:					Union[str, Any]=13	,      lowercase		:					str=7	,      lowercase		:					Any=True	,      lowercase		:					Tuple=True	,      lowercase		:					List[Any]=True	,      lowercase		:					Any=True	,      lowercase		:					Any=99	,      lowercase		:					Dict=32	,      lowercase		:					Dict=5	,      lowercase		:					Union[str, Any]=4	,      lowercase		:					Any=37	,      lowercase		:					Any="gelu"	,      lowercase		:					Union[str, Any]=0.1	,      lowercase		:					Optional[Any]=0.1	,      lowercase		:					str=512	,      lowercase		:					str=16	,      lowercase		:					Dict=2	,      lowercase		:					Any=0.02	,      lowercase		:					Optional[int]=False	,      lowercase		:					int=True	,      lowercase		:					Optional[int]="None"	,      lowercase		:					int=3	,      lowercase		:					str=4	,      lowercase		:					Union[str, Any]=None	,      ):





        """simple docstring"""




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





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





        """simple docstring"""




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

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

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

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

        lowercase_			:str    			= self.get_config()

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





     def 							lowercase__      ( self		:					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 							lowercase__      ( self		:					Union[str, Any]						):





        """simple docstring"""




        lowercase_			:List[Any]    			= self.get_config()
        lowercase_			:int    			= 300
        return config





     def 							lowercase__      ( self		:					str	,      lowercase		:					List[Any]						):





        """simple docstring"""




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





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





        """simple docstring"""




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

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





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





        """simple docstring"""




        lowercase_			:Optional[Any]    			= DebertaForMaskedLM(config=lowercase						)
        model.to(lowercase						)
        model.eval()
        lowercase_			: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		:					List[Any]	,      lowercase		:					Optional[int]	,      lowercase		:					List[Any]	,      lowercase		:					Dict	,      lowercase		:					str	,      lowercase		:					Tuple	,      lowercase		:					Tuple	,      lowercase		:					Optional[Any]						):





        """simple docstring"""




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





        """simple docstring"""




        lowercase_			:Dict    			= self.num_labels
        lowercase_			:Optional[Any]    			= DebertaForTokenClassification(config=lowercase						)
        model.to(lowercase						)
        model.eval()
        lowercase_			:Optional[int]    			= 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		:					Tuple	,      lowercase		:					List[Any]	,      lowercase		:					Tuple	,      lowercase		:					List[str]	,      lowercase		:					int	,      lowercase		:					int	,      lowercase		:					Optional[Any]	,      lowercase		:					List[Any]						):





        """simple docstring"""




        lowercase_			:str    			= DebertaForQuestionAnswering(config=lowercase						)
        model.to(lowercase						)
        model.eval()
        lowercase_			:Optional[int]    			= 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		:					str						):





        """simple docstring"""




        lowercase_			:str    			= self.prepare_config_and_inputs()
        (
            (
            lowercase_
        )			,							(
            lowercase_
        )			,							(
            lowercase_
        )			,							(
            lowercase_
        )			,							(
            lowercase_
        )			,							(
            lowercase_
        )			,							(
            lowercase_
        )			,							
        )			:Optional[Any]    			= config_and_inputs
        lowercase_			:str    			= {"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[Any]						):





        """simple docstring"""




        lowercase_			:Optional[int]    			= DebertaModelTester(self						)
        lowercase_			:Optional[int]    			= ConfigTester(self	,      config_class=lowercase	,      hidden_size=37						)





     def 							lowercase__      ( self		:					Optional[int]						):





        """simple docstring"""




        self.config_tester.run_common_tests()





     def 							lowercase__      ( self		:					Optional[int]						):





        """simple docstring"""




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





     def 							lowercase__      ( self		:					List[str]						):





        """simple docstring"""




        lowercase_			:List[str]    			= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_deberta_for_sequence_classification(*lowercase						)





     def 							lowercase__      ( self		:					Optional[Any]						):





        """simple docstring"""




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





     def 							lowercase__      ( self		:					Dict						):





        """simple docstring"""




        lowercase_			:Tuple    			= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_deberta_for_question_answering(*lowercase						)





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





        """simple docstring"""




        lowercase_			:Any    			= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_deberta_for_token_classification(*lowercase						)







     @slow
     def 							lowercase__      ( self		:					Any						):





        """simple docstring"""




        for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           lowercase_			:Optional[int]    			= 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		:					Union[str, Any]						):





        """simple docstring"""




        pass







     @slow
     def 							lowercase__      ( self		:					Dict						):





        """simple docstring"""




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

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









'''simple docstring'''


import unittest

from transformers.testing_utils import CaptureStdout
from transformers.tools.python_interpreter import evaluate
def 							UpperCAmelCase_  (						__lowerCamelCase					:					List[Any] ):
   return x + 2


class       a_   (				unittest.TestCase							):





     def 							lowercase__      ( self		:					Optional[Any]						):





        """simple docstring"""




        lowercase_			:Optional[int]    			= "x = 3"
        lowercase_			:Any    			= {}
        lowercase_			:Any    			= evaluate(lowercase	,      {}	,      state=lowercase						)
        assert result == 3
        self.assertDictEqual(lowercase	,      {"x": 3}						)

        lowercase_			:Dict    			= "x = y"
        lowercase_			:Dict    			= {"y": 5}
        lowercase_			:str    			= evaluate(lowercase	,      {}	,      state=lowercase						)
        # evaluate returns the value of the last assignment.
        assert result == 5
        self.assertDictEqual(lowercase	,      {"x": 5, "y": 5}						)





     def 							lowercase__      ( self		:					List[Any]						):





        """simple docstring"""




        lowercase_			:Dict    			= "y = add_two(x)"
        lowercase_			:Optional[Any]    			= {"x": 3}
        lowercase_			:int    			= evaluate(lowercase	,      {"add_two": add_two}	,      state=lowercase						)
        assert result == 5
        self.assertDictEqual(lowercase	,      {"x": 3, "y": 5}						)

        # Won't work without the tool
        with CaptureStdout() as out:
           lowercase_			:List[Any]    			= evaluate(lowercase	,      {}	,      state=lowercase						)
        assert result is None
        assert "tried to execute add_two" in out.out





     def 							lowercase__      ( self		:					int						):





        """simple docstring"""




        lowercase_			:Optional[int]    			= "x = 3"
        lowercase_			:Any    			= {}
        lowercase_			:Any    			= evaluate(lowercase	,      {}	,      state=lowercase						)
        assert result == 3
        self.assertDictEqual(lowercase	,      {"x": 3}						)





     def 							lowercase__      ( self		:					Any						):





        """simple docstring"""




        lowercase_			:Optional[Any]    			= "test_dict = {'x': x, 'y': add_two(x)}"
        lowercase_			:List[str]    			= {"x": 3}
        lowercase_			:Any    			= evaluate(lowercase	,      {"add_two": add_two}	,      state=lowercase						)
        self.assertDictEqual(lowercase	,      {"x": 3, "y": 5}						)
        self.assertDictEqual(lowercase	,      {"x": 3, "test_dict": {"x": 3, "y": 5}}						)





     def 							lowercase__      ( self		:					Optional[Any]						):





        """simple docstring"""




        lowercase_			:Optional[int]    			= "x = 3\ny = 5"
        lowercase_			:Union[str, Any]    			= {}
        lowercase_			:Optional[int]    			= evaluate(lowercase	,      {}	,      state=lowercase						)
        # evaluate returns the value of the last assignment.
        assert result == 5
        self.assertDictEqual(lowercase	,      {"x": 3, "y": 5}						)





     def 							lowercase__      ( self		:					List[Any]						):





        """simple docstring"""




        lowercase_			:List[str]    			= "text = f'This is x: {x}.'"
        lowercase_			:int    			= {"x": 3}
        lowercase_			:int    			= evaluate(lowercase	,      {}	,      state=lowercase						)
        # evaluate returns the value of the last assignment.
        assert result == "This is x: 3."
        self.assertDictEqual(lowercase	,      {"x": 3, "text": "This is x: 3."}						)





     def 							lowercase__      ( self		:					Optional[int]						):





        """simple docstring"""




        lowercase_			:List[Any]    			= "if x <= 3:\n    y = 2\nelse:\n    y = 5"
        lowercase_			:List[str]    			= {"x": 3}
        lowercase_			:Union[str, Any]    			= evaluate(lowercase	,      {}	,      state=lowercase						)
        # evaluate returns the value of the last assignment.
        assert result == 2
        self.assertDictEqual(lowercase	,      {"x": 3, "y": 2}						)

        lowercase_			:List[str]    			= {"x": 8}
        lowercase_			:Tuple    			= evaluate(lowercase	,      {}	,      state=lowercase						)
        # evaluate returns the value of the last assignment.
        assert result == 5
        self.assertDictEqual(lowercase	,      {"x": 8, "y": 5}						)





     def 							lowercase__      ( self		:					Dict						):





        """simple docstring"""




        lowercase_			:Any    			= "test_list = [x, add_two(x)]"
        lowercase_			:Optional[int]    			= {"x": 3}
        lowercase_			:List[str]    			= evaluate(lowercase	,      {"add_two": add_two}	,      state=lowercase						)
        self.assertListEqual(lowercase	,      [3, 5]						)
        self.assertDictEqual(lowercase	,      {"x": 3, "test_list": [3, 5]}						)





     def 							lowercase__      ( self		:					List[Any]						):





        """simple docstring"""




        lowercase_			:str    			= "y = x"
        lowercase_			:Any    			= {"x": 3}
        lowercase_			:Tuple    			= evaluate(lowercase	,      {}	,      state=lowercase						)
        assert result == 3
        self.assertDictEqual(lowercase	,      {"x": 3, "y": 3}						)





     def 							lowercase__      ( self		:					Any						):





        """simple docstring"""




        lowercase_			:List[Any]    			= "test_list = [x, add_two(x)]\ntest_list[1]"
        lowercase_			:Dict    			= {"x": 3}
        lowercase_			:Any    			= evaluate(lowercase	,      {"add_two": add_two}	,      state=lowercase						)
        assert result == 5
        self.assertDictEqual(lowercase	,      {"x": 3, "test_list": [3, 5]}						)

        lowercase_			:Union[str, Any]    			= "test_dict = {'x': x, 'y': add_two(x)}\ntest_dict['y']"
        lowercase_			:List[Any]    			= {"x": 3}
        lowercase_			:List[Any]    			= evaluate(lowercase	,      {"add_two": add_two}	,      state=lowercase						)
        assert result == 5
        self.assertDictEqual(lowercase	,      {"x": 3, "test_dict": {"x": 3, "y": 5}}						)







     def 							lowercase__      ( self		:					Any						):





        """simple docstring"""




        lowercase_			:Dict    			= "x = 0\nfor i in range(3):\n    x = i"
        lowercase_			:Any    			= {}
        lowercase_			:Tuple    			= evaluate(lowercase	,      {"range": range}	,      state=lowercase						)
        assert result == 2
        self.assertDictEqual(lowercase	,      {"x": 2, "i": 2}						)


