





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


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





         """simple docstring"""



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


      def    a (self       :      List[str]      ):





         """simple docstring"""



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

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

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

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

         __snake_case							       =   self.get_config()

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


      def    a (self       :      str      ):





         """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    a (self       :      Optional[Any]      ):





         """simple docstring"""



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


      def    a (self       :      str				,			a__       :      Optional[Any]      ):





         """simple docstring"""



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


      def    a (self       :      Optional[Any]				,			a__       :      Tuple				,			a__       :      Optional[int]				,			a__       :      List[Any]				,			a__       :      List[str]				,			a__       :      List[Any]				,			a__       :      int				,			a__       :      Any      ):





         """simple docstring"""



         __snake_case							       =   DebertaModel(config=a__      )
         model.to(a__      )
         model.eval()
         __snake_case							       =   model(a__				,			attention_mask=a__				,			token_type_ids=a__      )[0]
         __snake_case							       =   model(a__				,			token_type_ids=a__      )[0]
         __snake_case							       =   model(a__      )[0]

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


      def    a (self       :      Tuple				,			a__       :      List[str]				,			a__       :      Optional[Any]				,			a__       :      Any				,			a__       :      int				,			a__       :      List[str]				,			a__       :      Tuple				,			a__       :      List[str]      ):





         """simple docstring"""



         __snake_case							       =   DebertaForMaskedLM(config=a__      )
         model.to(a__      )
         model.eval()
         __snake_case							       =   model(a__				,			attention_mask=a__				,			token_type_ids=a__				,			labels=a__      )

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


      def    a (self       :      Optional[int]				,			a__       :      Optional[Any]				,			a__       :      List[str]				,			a__       :      Any				,			a__       :      int				,			a__       :      Optional[Any]				,			a__       :      List[Any]				,			a__       :      Optional[int]      ):





         """simple docstring"""



         __snake_case							       =   self.num_labels
         __snake_case							       =   DebertaForSequenceClassification(a__      )
         model.to(a__      )
         model.eval()
         __snake_case							       =   model(a__				,			attention_mask=a__				,			token_type_ids=a__				,			labels=a__      )
         self.parent.assertListEqual(list(result.logits.size()      )				,			[self.batch_size, self.num_labels]      )
         self.check_loss_output(a__      )


      def    a (self       :      List[str]				,			a__       :      Any				,			a__       :      Union[str, Any]				,			a__       :      List[Any]				,			a__       :      Tuple				,			a__       :      str				,			a__       :      Any				,			a__       :      List[Any]      ):





         """simple docstring"""



         __snake_case							       =   self.num_labels
         __snake_case							       =   DebertaForTokenClassification(config=a__      )
         model.to(a__      )
         model.eval()
         __snake_case							       =   model(a__				,			attention_mask=a__				,			token_type_ids=a__				,			labels=a__      )
         self.parent.assertEqual(result.logits.shape				,			(self.batch_size, self.seq_length, self.num_labels)      )


      def    a (self       :      Tuple				,			a__       :      Dict				,			a__       :      Optional[int]				,			a__       :      int				,			a__       :      List[str]				,			a__       :      List[Any]				,			a__       :      Optional[Any]				,			a__       :      str      ):





         """simple docstring"""



         __snake_case							       =   DebertaForQuestionAnswering(config=a__      )
         model.to(a__      )
         model.eval()
         __snake_case							       =   model(
             a__				,			attention_mask=a__				,			token_type_ids=a__				,			start_positions=a__				,			end_positions=a__				,			)
         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    a (self       :      Union[str, Any]      ):





         """simple docstring"""



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






@require_torch
class 		SCREAMING_SNAKE_CASE__				(       _UpperCAmelCase						,      _UpperCAmelCase						,      unittest.TestCase  ):
      A_						:				Optional[Any]							=    (
          (
              DebertaModel,
              DebertaForMaskedLM,
              DebertaForSequenceClassification,
              DebertaForTokenClassification,
              DebertaForQuestionAnswering,
          )
          if is_torch_available()
          else ()
      )
      A_						:				Union[str, Any]							=    (
          {
              'feature-extraction': DebertaModel,
              'fill-mask': DebertaForMaskedLM,
              'question-answering': DebertaForQuestionAnswering,
              'text-classification': DebertaForSequenceClassification,
              'token-classification': DebertaForTokenClassification,
              'zero-shot': DebertaForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )

      A_						:				Tuple							=    True
      A_						:				str							=    False
      A_						:				Optional[Any]							=    False
      A_						:				List[str]							=    False
      A_						:				Optional[int]							=    False


      def    a (self       :      Dict      ):





         """simple docstring"""



         __snake_case							       =   DebertaModelTester(self      )
         __snake_case							       =   ConfigTester(self				,			config_class=a__				,			hidden_size=37      )


      def    a (self       :      List[str]      ):





         """simple docstring"""



         self.config_tester.run_common_tests()


      def    a (self       :      Dict      ):





         """simple docstring"""



         __snake_case							       =   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_model(*a__      )


      def    a (self       :      Optional[int]      ):





         """simple docstring"""



         __snake_case							       =   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_sequence_classification(*a__      )


      def    a (self       :      Optional[Any]      ):





         """simple docstring"""



         __snake_case							       =   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_masked_lm(*a__      )


      def    a (self       :      Optional[int]      ):





         """simple docstring"""



         __snake_case							       =   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_question_answering(*a__      )


      def    a (self       :      Dict      ):





         """simple docstring"""



         __snake_case							       =   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_token_classification(*a__      )


      @slow
      def    a (self       :      Union[str, Any]      ):





         """simple docstring"""



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






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


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





         """simple docstring"""



         pass


      @slow
      def    a (self       :      Union[str, Any]      ):





         """simple docstring"""



         __snake_case							       =   DebertaModel.from_pretrained('''microsoft/deberta-base'''      )

         __snake_case							       =   torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]      )
         __snake_case							       =   torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]      )
         with torch.no_grad():
            __snake_case							       =   model(a__				,			attention_mask=a__      )[0]
         # compare the actual values for a slice.
         __snake_case							       =   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]				,			a__				,			atol=1E-4      )				,			f"""{output[:, 1:4, 1:4]}"""      )








import os
import unittest

from huggingface_hub.utils import are_progress_bars_disabled

import transformers.models.bart.tokenization_bart
from transformers import logging
from transformers.testing_utils import CaptureLogger, mockenv, mockenv_context
from transformers.utils.logging import disable_progress_bar, enable_progress_bar

class 		SCREAMING_SNAKE_CASE__				(       unittest.TestCase  ):


      def    a (self       :      Dict      ):





         """simple docstring"""



         __snake_case							       =   logging.get_logger()

         # the current default level is logging.WARNING
         __snake_case							       =   logging.get_verbosity()

         logging.set_verbosity_error()
         self.assertEqual(logger.getEffectiveLevel()				,			logging.get_verbosity()      )

         logging.set_verbosity_warning()
         self.assertEqual(logger.getEffectiveLevel()				,			logging.get_verbosity()      )

         logging.set_verbosity_info()
         self.assertEqual(logger.getEffectiveLevel()				,			logging.get_verbosity()      )

         logging.set_verbosity_debug()
         self.assertEqual(logger.getEffectiveLevel()				,			logging.get_verbosity()      )

         # restore to the original level
         logging.set_verbosity(a__      )


      def    a (self       :      Dict      ):





         """simple docstring"""



         __snake_case							       =   logging.get_verbosity()

         __snake_case							       =   logging.get_logger('''transformers.models.bart.tokenization_bart'''      )
         __snake_case							       =   '''Testing 1, 2, 3'''

         # should be able to log warnings (if default settings weren't overridden by `pytest --log-level-all`)
         if level_origin <= logging.WARNING:
            with CaptureLogger(a__      ) as cl:
               logger.warning(a__      )
            self.assertEqual(cl.out				,			msg + '''\n'''      )

         # this is setting the level for all of `transformers.*` loggers
         logging.set_verbosity_error()

         # should not be able to log warnings
         with CaptureLogger(a__      ) as cl:
            logger.warning(a__      )
         self.assertEqual(cl.out				,			''''''      )

         # should be able to log warnings again
         logging.set_verbosity_warning()
         with CaptureLogger(a__      ) as cl:
            logger.warning(a__      )
         self.assertEqual(cl.out				,			msg + '''\n'''      )

         # restore to the original level
         logging.set_verbosity(a__      )


      @mockenv(TRANSFORMERS_VERBOSITY='''error'''      )
      def    a (self       :      Dict      ):





         """simple docstring"""



         transformers.utils.logging._reset_library_root_logger()
         # this action activates the env var
         __snake_case							       =   logging.get_logger('''transformers.models.bart.tokenization_bart'''      )

         __snake_case							       =   os.getenv('''TRANSFORMERS_VERBOSITY'''				,			a__      )
         __snake_case							       =   logging.log_levels[env_level_str]

         __snake_case							       =   logging.get_verbosity()
         self.assertEqual(
             a__				,			a__				,			f"""TRANSFORMERS_VERBOSITY={env_level_str}/{env_level}, but internal verbosity is {current_level}"""				,			)

         # restore to the original level
         __snake_case							       =   ''''''
         transformers.utils.logging._reset_library_root_logger()


      @mockenv(TRANSFORMERS_VERBOSITY='''super-error'''      )
      def    a (self       :      List[Any]      ):





         """simple docstring"""



         transformers.utils.logging._reset_library_root_logger()
         __snake_case							       =   logging.logging.getLogger()
         with CaptureLogger(a__      ) as cl:
            # this action activates the env var
            logging.get_logger('''transformers.models.bart.tokenization_bart'''      )
         self.assertIn('''Unknown option TRANSFORMERS_VERBOSITY=super-error'''				,			cl.out      )

         # no need to restore as nothing was changed


      def    a (self       :      Any      ):





         """simple docstring"""



         transformers.utils.logging._reset_library_root_logger()

         __snake_case							       =   logging.get_logger('''transformers.models.bart.tokenization_bart'''      )
         __snake_case							       =   '''Testing 1, 2, 3'''

         with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS='''1'''      ):
            # nothing should be logged as env var disables this method
            with CaptureLogger(a__      ) as cl:
               logger.warning_advice(a__      )
            self.assertEqual(cl.out				,			''''''      )

         with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS=''''''      ):
            # should log normally as TRANSFORMERS_NO_ADVISORY_WARNINGS is unset
            with CaptureLogger(a__      ) as cl:
               logger.warning_advice(a__      )
            self.assertEqual(cl.out				,			msg + '''\n'''      )






def lowerCamelCase__    (							)      ->				str:
   disable_progress_bar()
   assert are_progress_bars_disabled()

   enable_progress_bar()
   assert not are_progress_bars_disabled()

