





from __future__ import annotations

import unittest

from transformers import DebertaVaConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
     import tensorflow as tf

     from transformers import (
         TFDebertaVaForMaskedLM,
         TFDebertaVaForQuestionAnswering,
         TFDebertaVaForSequenceClassification,
         TFDebertaVaForTokenClassification,
         TFDebertaVaModel,
     )



class 							_UpperCamelCase					:

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




              """simple docstring"""


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

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




              """simple docstring"""


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

              UpperCamelCase_          =  None
              if self.use_input_mask:
                     UpperCamelCase_          =  random_attention_mask([self.batch_size, self.seq_length]		)

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

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

              UpperCamelCase_          =  DebertaVaConfig(
                  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					,   relative_attention=self.relative_attention					,   position_biased_input=self.position_biased_input					,   initializer_range=self.initializer_range					,   return_dict=_SCREAMING_SNAKE_CASE					,   )

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

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




              """simple docstring"""


              UpperCamelCase_          =  TFDebertaVaModel(config=_SCREAMING_SNAKE_CASE		)
              UpperCamelCase_          =  {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

              UpperCamelCase_          =  [input_ids, input_mask]
              UpperCamelCase_          =  model(_SCREAMING_SNAKE_CASE		)

              UpperCamelCase_          =  model(_SCREAMING_SNAKE_CASE		)

              self.parent.assertEqual(result.last_hidden_state.shape					,   (self.batch_size, self.seq_length, self.hidden_size)		)

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




              """simple docstring"""


              UpperCamelCase_          =  TFDebertaVaForMaskedLM(config=_SCREAMING_SNAKE_CASE		)
              UpperCamelCase_          =  {
                  "input_ids": input_ids,
                  "attention_mask": input_mask,
                  "token_type_ids": token_type_ids,
              }
              UpperCamelCase_          =  model(_SCREAMING_SNAKE_CASE		)
              self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.seq_length, self.vocab_size)		)

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




              """simple docstring"""


              UpperCamelCase_          =  self.num_labels
              UpperCamelCase_          =  TFDebertaVaForSequenceClassification(config=_SCREAMING_SNAKE_CASE		)
              UpperCamelCase_          =  {
                  "input_ids": input_ids,
                  "attention_mask": input_mask,
                  "token_type_ids": token_type_ids,
              }

              UpperCamelCase_          =  model(_SCREAMING_SNAKE_CASE		)
              self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.num_labels)		)

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




              """simple docstring"""


              UpperCamelCase_          =  self.num_labels
              UpperCamelCase_          =  TFDebertaVaForTokenClassification(config=_SCREAMING_SNAKE_CASE		)
              UpperCamelCase_          =  {
                  "input_ids": input_ids,
                  "attention_mask": input_mask,
                  "token_type_ids": token_type_ids,
              }
              UpperCamelCase_          =  model(_SCREAMING_SNAKE_CASE		)
              self.parent.assertEqual(result.logits.shape					,   (self.batch_size, self.seq_length, self.num_labels)		)

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




              """simple docstring"""


              UpperCamelCase_          =  TFDebertaVaForQuestionAnswering(config=_SCREAMING_SNAKE_CASE		)
              UpperCamelCase_          =  {
                  "input_ids": input_ids,
                  "attention_mask": input_mask,
                  "token_type_ids": token_type_ids,
              }

              UpperCamelCase_          =  model(_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 lowercase (		self: Optional[Any]		)       -> Tuple:




              """simple docstring"""


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



@require_tf
class 							_UpperCamelCase					(	lowerCAmelCase_  ,						lowerCAmelCase_  ,						unittest.TestCase     ):
       _UpperCamelCase				:					Any         =   (
           (
               TFDebertaVaModel,
               TFDebertaVaForMaskedLM,
               TFDebertaVaForQuestionAnswering,
               TFDebertaVaForSequenceClassification,
               TFDebertaVaForTokenClassification,
           )
           if is_tf_available()
           else ()
       )
       _UpperCamelCase				:					Any         =   (
           {
               '''feature-extraction''': TFDebertaVaModel,
               '''fill-mask''': TFDebertaVaForMaskedLM,
               '''question-answering''': TFDebertaVaForQuestionAnswering,
               '''text-classification''': TFDebertaVaForSequenceClassification,
               '''token-classification''': TFDebertaVaForTokenClassification,
               '''zero-shot''': TFDebertaVaForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )

       _UpperCamelCase				:					Dict         =   False
       _UpperCamelCase				:					Optional[int]         =   False

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




              """simple docstring"""


              UpperCamelCase_          =  TFDebertaVaModelTester(self		)
              UpperCamelCase_          =  ConfigTester(self					,   config_class=_SCREAMING_SNAKE_CASE					,   hidden_size=37		)

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




              """simple docstring"""


              self.config_tester.run_common_tests()

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




              """simple docstring"""


              UpperCamelCase_          =  self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE		)

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




              """simple docstring"""


              UpperCamelCase_          =  self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_masked_lm(*_SCREAMING_SNAKE_CASE		)

       def lowercase (		self: Optional[Any]		)       -> Any:




              """simple docstring"""


              UpperCamelCase_          =  self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_question_answering(*_SCREAMING_SNAKE_CASE		)

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




              """simple docstring"""


              UpperCamelCase_          =  self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_sequence_classification(*_SCREAMING_SNAKE_CASE		)

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




              """simple docstring"""


              UpperCamelCase_          =  self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_token_classification(*_SCREAMING_SNAKE_CASE		)



       @slow
       def lowercase (		self: Tuple		)       -> List[str]:




              """simple docstring"""


              UpperCamelCase_          =  TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"		)
              self.assertIsNotNone(_SCREAMING_SNAKE_CASE		)



@require_tf
class 							_UpperCamelCase					(	unittest.TestCase     ):

       @unittest.skip(reason="Model not available yet"		)
       def lowercase (		self: Optional[Any]		)       -> List[Any]:




              """simple docstring"""


              pass



       @slow
       def lowercase (		self: Dict		)       -> Tuple:




              """simple docstring"""


              UpperCamelCase_          =  TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"		)
              UpperCamelCase_          =  tf.constant([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]		)
              UpperCamelCase_          =  tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
              UpperCamelCase_          =  model(_SCREAMING_SNAKE_CASE					,   attention_mask=_SCREAMING_SNAKE_CASE		)[0]

              UpperCamelCase_          =  tf.constant(
                  [[[0.23_56, 0.19_48, 0.03_69], [-0.10_63, 0.35_86, -0.51_52], [-0.63_99, -0.02_59, -0.25_25]]]		)
              tf.debugging.assert_near(output[:, 1:4, 1:4]					,   _SCREAMING_SNAKE_CASE					,   atol=1e-4		)








import unittest

from transformers.testing_utils import CaptureStdout
from transformers.tools.python_interpreter import evaluate




def  lowerCAmelCase_ (							UpperCamelCase_  )     ->		Optional[int]:
       return x + 2







class 							_UpperCamelCase					(	unittest.TestCase     ):

       def lowercase (		self: Optional[int]		)       -> str:




              """simple docstring"""


              UpperCamelCase_          =  "x = 3"
              UpperCamelCase_          =  {}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {}					,   state=_SCREAMING_SNAKE_CASE		)
              assert result == 3
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3}		)

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

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




              """simple docstring"""


              UpperCamelCase_          =  "y = add_two(x)"
              UpperCamelCase_          =  {"x": 3}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {"add_two": add_two}					,   state=_SCREAMING_SNAKE_CASE		)
              assert result == 5
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3, "y": 5}		)

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

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




              """simple docstring"""


              UpperCamelCase_          =  "x = 3"
              UpperCamelCase_          =  {}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {}					,   state=_SCREAMING_SNAKE_CASE		)
              assert result == 3
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3}		)

       def lowercase (		self: Dict		)       -> Any:




              """simple docstring"""


              UpperCamelCase_          =  "test_dict = {'x': x, 'y': add_two(x)}"
              UpperCamelCase_          =  {"x": 3}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {"add_two": add_two}					,   state=_SCREAMING_SNAKE_CASE		)
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3, "y": 5}		)
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3, "test_dict": {"x": 3, "y": 5}}		)

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




              """simple docstring"""


              UpperCamelCase_          =  "x = 3\ny = 5"
              UpperCamelCase_          =  {}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {}					,   state=_SCREAMING_SNAKE_CASE		)
              # evaluate returns the value of the last assignment.
              assert result == 5
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3, "y": 5}		)

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




              """simple docstring"""


              UpperCamelCase_          =  "text = f'This is x: {x}.'"
              UpperCamelCase_          =  {"x": 3}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {}					,   state=_SCREAMING_SNAKE_CASE		)
              # evaluate returns the value of the last assignment.
              assert result == "This is x: 3."
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3, "text": "This is x: 3."}		)

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




              """simple docstring"""


              UpperCamelCase_          =  "if x <= 3:\n    y = 2\nelse:\n    y = 5"
              UpperCamelCase_          =  {"x": 3}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {}					,   state=_SCREAMING_SNAKE_CASE		)
              # evaluate returns the value of the last assignment.
              assert result == 2
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3, "y": 2}		)

              UpperCamelCase_          =  {"x": 8}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {}					,   state=_SCREAMING_SNAKE_CASE		)
              # evaluate returns the value of the last assignment.
              assert result == 5
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 8, "y": 5}		)

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




              """simple docstring"""


              UpperCamelCase_          =  "test_list = [x, add_two(x)]"
              UpperCamelCase_          =  {"x": 3}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {"add_two": add_two}					,   state=_SCREAMING_SNAKE_CASE		)
              self.assertListEqual(_SCREAMING_SNAKE_CASE					,   [3, 5]		)
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3, "test_list": [3, 5]}		)

       def lowercase (		self: List[str]		)       -> int:




              """simple docstring"""


              UpperCamelCase_          =  "y = x"
              UpperCamelCase_          =  {"x": 3}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {}					,   state=_SCREAMING_SNAKE_CASE		)
              assert result == 3
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3, "y": 3}		)

       def lowercase (		self: str		)       -> int:




              """simple docstring"""


              UpperCamelCase_          =  "test_list = [x, add_two(x)]\ntest_list[1]"
              UpperCamelCase_          =  {"x": 3}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {"add_two": add_two}					,   state=_SCREAMING_SNAKE_CASE		)
              assert result == 5
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3, "test_list": [3, 5]}		)

              UpperCamelCase_          =  "test_dict = {'x': x, 'y': add_two(x)}\ntest_dict['y']"
              UpperCamelCase_          =  {"x": 3}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {"add_two": add_two}					,   state=_SCREAMING_SNAKE_CASE		)
              assert result == 5
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3, "test_dict": {"x": 3, "y": 5}}		)



       def lowercase (		self: Dict		)       -> Optional[int]:




              """simple docstring"""


              UpperCamelCase_          =  "x = 0\nfor i in range(3):\n    x = i"
              UpperCamelCase_          =  {}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {"range": range}					,   state=_SCREAMING_SNAKE_CASE		)
              assert result == 2
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 2, "i": 2}		)

