




'''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       ( a_				):






       def __init__(							self,		lowerCAmelCase,		lowerCAmelCase=13,		lowerCAmelCase=7,		lowerCAmelCase=True,		lowerCAmelCase=True,		lowerCAmelCase=True,		lowerCAmelCase=True,		lowerCAmelCase=99,		lowerCAmelCase=32,		lowerCAmelCase=5,		lowerCAmelCase=4,		lowerCAmelCase=37,		lowerCAmelCase="gelu",		lowerCAmelCase=0.1,		lowerCAmelCase=0.1,		lowerCAmelCase=512,		lowerCAmelCase=16,		lowerCAmelCase=2,		lowerCAmelCase=0.0_2,		lowerCAmelCase=False,		lowerCAmelCase=True,		lowerCAmelCase="None",		lowerCAmelCase=3,		lowerCAmelCase=4,		lowerCAmelCase=None,		):



              """simple docstring"""







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






       def      lowercase__     (							self  ):



              """simple docstring"""







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

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

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

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

              lowerCamelCase_							=self.get_config()

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






       def      lowercase__     (							self  ):



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



              """simple docstring"""







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






       def      lowercase__     (							self,		lowerCAmelCase  ):



              """simple docstring"""







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






       def      lowercase__     (							self,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase  ):



              """simple docstring"""







              lowerCamelCase_							=DebertaModel(config=_lowerCamelCase  )
              model.to(_lowerCamelCase  )
              model.eval()
              lowerCamelCase_							=model(_lowerCamelCase,		attention_mask=_lowerCamelCase,		token_type_ids=_lowerCamelCase  )[0]
              lowerCamelCase_							=model(_lowerCamelCase,		token_type_ids=_lowerCamelCase  )[0]
              lowerCamelCase_							=model(_lowerCamelCase  )[0]

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






       def      lowercase__     (							self,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase  ):



              """simple docstring"""







              lowerCamelCase_							=DebertaForMaskedLM(config=_lowerCamelCase  )
              model.to(_lowerCamelCase  )
              model.eval()
              lowerCamelCase_							=model(_lowerCamelCase,		attention_mask=_lowerCamelCase,		token_type_ids=_lowerCamelCase,		labels=_lowerCamelCase  )

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






       def      lowercase__     (							self,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase  ):



              """simple docstring"""







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






       def      lowercase__     (							self,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase  ):



              """simple docstring"""







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






       def      lowercase__     (							self,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase  ):



              """simple docstring"""







              lowerCamelCase_							=DebertaForQuestionAnswering(config=_lowerCamelCase  )
              model.to(_lowerCamelCase  )
              model.eval()
              lowerCamelCase_							=model(
                  _lowerCamelCase,		attention_mask=_lowerCamelCase,		token_type_ids=_lowerCamelCase,		start_positions=_lowerCamelCase,		end_positions=_lowerCamelCase,		)
              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  ):



              """simple docstring"""







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





@require_torch
class   __UpperCamelCase       ( a_			,					a_			,					unittest.TestCase				):
       lowercase	:    Optional[Any]      						=(
           (
               DebertaModel,
               DebertaForMaskedLM,
               DebertaForSequenceClassification,
               DebertaForTokenClassification,
               DebertaForQuestionAnswering,
           )
           if is_torch_available()
           else ()
       )
       lowercase	:    Optional[int]      						=(
           {
               "feature-extraction": DebertaModel,
               "fill-mask": DebertaForMaskedLM,
               "question-answering": DebertaForQuestionAnswering,
               "text-classification": DebertaForSequenceClassification,
               "token-classification": DebertaForTokenClassification,
               "zero-shot": DebertaForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )

       lowercase	:    List[str]      						=True
       lowercase	:    List[Any]      						=False
       lowercase	:    List[str]      						=False
       lowercase	:    List[str]      						=False
       lowercase	:    Union[str, Any]      						=False






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=DebertaModelTester(self  )
              lowerCamelCase_							=ConfigTester(self,		config_class=_lowerCamelCase,		hidden_size=37  )






       def      lowercase__     (							self  ):



              """simple docstring"""







              self.config_tester.run_common_tests()






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_deberta_model(*_lowerCamelCase  )






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_deberta_for_sequence_classification(*_lowerCamelCase  )






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_deberta_for_masked_lm(*_lowerCamelCase  )






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_deberta_for_question_answering(*_lowerCamelCase  )






       def      lowercase__     (							self  ):



              """simple docstring"""







              lowerCamelCase_							=self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_deberta_for_token_classification(*_lowerCamelCase  )






       @slow
       def      lowercase__     (							self  ):



              """simple docstring"""







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





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






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



              """simple docstring"""







              pass






       @slow
       def      lowercase__     (							self  ):



              """simple docstring"""







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

              lowerCamelCase_							=torch.tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]  )
              lowerCamelCase_							=torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]  )
              with torch.no_grad():
                     lowerCamelCase_							=model(_lowerCamelCase,		attention_mask=_lowerCamelCase  )[0]
              # compare the actual values for a slice.
              lowerCamelCase_							=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],		_lowerCamelCase,		atol=1e-4  ),		f'''{output[:, 1:4, 1:4]}'''  )












"""simple docstring"""



import re
import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin

class        a     (   a_	):
    UpperCAmelCase_ :     List[Any]	     =["image_processor", "tokenizer"]
    UpperCAmelCase_ :     str	     ="AutoImageProcessor"
    UpperCAmelCase_ :     Any	     ="AutoTokenizer"







    def __init__(			self							,		_lowerCamelCase=None							,		_lowerCamelCase=None							,		**_lowerCamelCase       ):
      lowercase							    =       None
      if "feature_extractor" in kwargs:
        warnings.warn(
            'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
            ' instead.'							,		_lowerCamelCase							,		)
        lowercase							    =       kwargs.pop('feature_extractor'       )

      lowercase							    =       image_processor if image_processor is not None else feature_extractor
      if image_processor is None:
        raise ValueError('You need to specify an `image_processor`.'       )
      if tokenizer is None:
        raise ValueError('You need to specify a `tokenizer`.'       )

      super().__init__(_lowerCamelCase							,		_lowerCamelCase       )
      lowercase							    =       self.image_processor
      lowercase							    =       False







    def __call__(			self							,		*_lowerCamelCase							,		**_lowerCamelCase       ):
      # For backward compatibility
      if self._in_target_context_manager:
        return self.current_processor(*_lowerCamelCase							,		**_lowerCamelCase       )

      lowercase							    =       kwargs.pop('images'							,		_lowerCamelCase       )
      lowercase							    =       kwargs.pop('text'							,		_lowerCamelCase       )
      if len(_lowerCamelCase       ) > 0:
        lowercase							    =       args[0]
        lowercase							    =       args[1:]

      if images is None and text is None:
        raise ValueError('You need to specify either an `images` or `text` input to process.'       )

      if images is not None:
        lowercase							    =       self.image_processor(_lowerCamelCase							,		*_lowerCamelCase							,		**_lowerCamelCase       )
      if text is not None:
        lowercase							    =       self.tokenizer(_lowerCamelCase							,		**_lowerCamelCase       )

      if text is None:
        return inputs
      elif images is None:
        return encodings
      else:
        lowercase							    =       encodings['input_ids']
        return inputs







    def 			UpperCamelCase_      (			self							,		*_lowerCamelCase							,		**_lowerCamelCase       ):
      return self.tokenizer.batch_decode(*_lowerCamelCase							,		**_lowerCamelCase       )







    def 			UpperCamelCase_      (			self							,		*_lowerCamelCase							,		**_lowerCamelCase       ):
      return self.tokenizer.decode(*_lowerCamelCase							,		**_lowerCamelCase       )







    @contextmanager
    def 			UpperCamelCase_      (			self       ):
      warnings.warn(
          '`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '
          'labels by using the argument `text` of the regular `__call__` method (either in the same call as '
          'your images inputs, or in a separate call.'       )
      lowercase							    =       True
      lowercase							    =       self.tokenizer
      yield
      lowercase							    =       self.image_processor
      lowercase							    =       False







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase=False							,		_lowerCamelCase=None       ):
      if added_vocab is None:
        lowercase							    =       self.tokenizer.get_added_vocab()

      lowercase							    =       {}

      while tokens:
        lowercase							    =       re.search(R'<s_(.*?)>'							,		_lowerCamelCase							,		re.IGNORECASE       )
        if start_token is None:
          break
        lowercase							    =       start_token.group(1       )
        lowercase							    =       re.search(RF'</s_{key}>'							,		_lowerCamelCase							,		re.IGNORECASE       )
        lowercase							    =       start_token.group()
        if end_token is None:
          lowercase							    =       tokens.replace(_lowerCamelCase							,		''       )
        else:
          lowercase							    =       end_token.group()
          lowercase							    =       re.escape(_lowerCamelCase       )
          lowercase							    =       re.escape(_lowerCamelCase       )
          lowercase							    =       re.search(F'{start_token_escaped}(.*?){end_token_escaped}'							,		_lowerCamelCase							,		re.IGNORECASE       )
          if content is not None:
            lowercase							    =       content.group(1       ).strip()
            if r"<s_" in content and r"</s_" in content:  # non-leaf node
              lowercase							    =       self.tokenajson(_lowerCamelCase							,		is_inner_value=_lowerCamelCase							,		added_vocab=_lowerCamelCase       )
              if value:
                if len(_lowerCamelCase       ) == 1:
                  lowercase							    =       value[0]
                lowercase							    =       value
            else:  # leaf nodes
              lowercase							    =       []
              for leaf in content.split(R'<sep/>'       ):
                lowercase							    =       leaf.strip()
                if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>":
                  lowercase							    =       leaf[1:-2]  # for categorical special tokens
                output[key].append(_lowerCamelCase       )
              if len(output[key]       ) == 1:
                lowercase							    =       output[key][0]

          lowercase							    =       tokens[tokens.find(_lowerCamelCase       ) + len(_lowerCamelCase       ) :].strip()
          if tokens[:6] == r"<sep/>":  # non-leaf nodes
            return [output] + self.tokenajson(tokens[6:]							,		is_inner_value=_lowerCamelCase							,		added_vocab=_lowerCamelCase       )

      if len(_lowerCamelCase       ):
        return [output] if is_inner_value else output
      else:
        return [] if is_inner_value else {"text_sequence": tokens}







    @property
    def 			UpperCamelCase_      (			self       ):
      warnings.warn(
          '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'							,		_lowerCamelCase							,		)
      return self.image_processor_class







    @property
    def 			UpperCamelCase_      (			self       ):
      warnings.warn(
          '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'							,		_lowerCamelCase							,		)
      return self.image_processor


