



"""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 				lowercase__       (   SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


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


             '''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      :  str   )			->						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_      			=							ids_tensor([self.batch_size, self.seq_length]		,    vocab_size=2   )

             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_      			=							ids_tensor([self.batch_size]		,    self.num_choices   )

             UpperCAmelCase_      			=							self.get_config()

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



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


             '''simple docstring'''




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



       def 					lowercase__    (							self      :  str		,    _UpperCAmelCase      :  List[Any]   )			->						Dict:


             '''simple docstring'''




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



       def 					lowercase__    (							self      :  Optional[int]		,    _UpperCAmelCase      :  Any		,    _UpperCAmelCase      :  Dict		,    _UpperCAmelCase      :  List[Any]		,    _UpperCAmelCase      :  Optional[Any]		,    _UpperCAmelCase      :  Tuple		,    _UpperCAmelCase      :  Optional[Any]		,    _UpperCAmelCase      :  List[str]   )			->						Tuple:


             '''simple docstring'''




             UpperCAmelCase_      			=							DebertaModel(config=_UpperCAmelCase   )
             model.to(_UpperCAmelCase   )
             model.eval()
             UpperCAmelCase_      			=							model(_UpperCAmelCase		,    attention_mask=_UpperCAmelCase		,    token_type_ids=_UpperCAmelCase   )[0]
             UpperCAmelCase_      			=							model(_UpperCAmelCase		,    token_type_ids=_UpperCAmelCase   )[0]
             UpperCAmelCase_      			=							model(_UpperCAmelCase   )[0]

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



       def 					lowercase__    (							self      :  List[Any]		,    _UpperCAmelCase      :  List[str]		,    _UpperCAmelCase      :  List[str]		,    _UpperCAmelCase      :  Optional[int]		,    _UpperCAmelCase      :  List[str]		,    _UpperCAmelCase      :  Optional[int]		,    _UpperCAmelCase      :  Any		,    _UpperCAmelCase      :  List[Any]   )			->						List[Any]:


             '''simple docstring'''




             UpperCAmelCase_      			=							DebertaForMaskedLM(config=_UpperCAmelCase   )
             model.to(_UpperCAmelCase   )
             model.eval()
             UpperCAmelCase_      			=							model(_UpperCAmelCase		,    attention_mask=_UpperCAmelCase		,    token_type_ids=_UpperCAmelCase		,    labels=_UpperCAmelCase   )

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



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


             '''simple docstring'''




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



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


             '''simple docstring'''




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



       def 					lowercase__    (							self      :  List[Any]		,    _UpperCAmelCase      :  Any		,    _UpperCAmelCase      :  int		,    _UpperCAmelCase      :  List[Any]		,    _UpperCAmelCase      :  int		,    _UpperCAmelCase      :  Tuple		,    _UpperCAmelCase      :  Tuple		,    _UpperCAmelCase      :  Dict   )			->						Optional[Any]:


             '''simple docstring'''




             UpperCAmelCase_      			=							DebertaForQuestionAnswering(config=_UpperCAmelCase   )
             model.to(_UpperCAmelCase   )
             model.eval()
             UpperCAmelCase_      			=							model(
                 _UpperCAmelCase		,    attention_mask=_UpperCAmelCase		,    token_type_ids=_UpperCAmelCase		,    start_positions=_UpperCAmelCase		,    end_positions=_UpperCAmelCase		,    )
             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      :  Any   )			->						List[Any]:


             '''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_torch
class 				lowercase__       (   SCREAMING_SNAKE_CASE    , SCREAMING_SNAKE_CASE    , unittest.TestCase				):







       '''simple docstring'''


       UpperCamelCase						=					(
           (
               DebertaModel,
               DebertaForMaskedLM,
               DebertaForSequenceClassification,
               DebertaForTokenClassification,
               DebertaForQuestionAnswering,
           )
           if is_torch_available()
           else ()
       )
       UpperCamelCase						=					(
           {
               '''feature-extraction''': DebertaModel,
               '''fill-mask''': DebertaForMaskedLM,
               '''question-answering''': DebertaForQuestionAnswering,
               '''text-classification''': DebertaForSequenceClassification,
               '''token-classification''': DebertaForTokenClassification,
               '''zero-shot''': DebertaForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )

       UpperCamelCase						=					True
       UpperCamelCase						=					False
       UpperCamelCase						=					False
       UpperCamelCase						=					False
       UpperCamelCase						=					False



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


             '''simple docstring'''




             UpperCAmelCase_      			=							DebertaModelTester(self   )
             UpperCAmelCase_      			=							ConfigTester(self		,    config_class=_UpperCAmelCase		,    hidden_size=37   )



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


             '''simple docstring'''




             self.config_tester.run_common_tests()



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


             '''simple docstring'''




             UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_deberta_model(*_UpperCAmelCase   )



       def 					lowercase__    (							self      :  int   )			->						Optional[int]:


             '''simple docstring'''




             UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_deberta_for_sequence_classification(*_UpperCAmelCase   )



       def 					lowercase__    (							self      :  Tuple   )			->						Any:


             '''simple docstring'''




             UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_deberta_for_masked_lm(*_UpperCAmelCase   )



       def 					lowercase__    (							self      :  int   )			->						str:


             '''simple docstring'''




             UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_deberta_for_question_answering(*_UpperCAmelCase   )



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


             '''simple docstring'''




             UpperCAmelCase_      			=							self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_deberta_for_token_classification(*_UpperCAmelCase   )







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


             '''simple docstring'''




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



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







       '''simple docstring'''


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


             '''simple docstring'''




             pass







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


             '''simple docstring'''




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

             UpperCAmelCase_      			=							torch.tensor([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]   )
             UpperCAmelCase_      			=							torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
             with torch.no_grad():
                   UpperCAmelCase_      			=							model(_UpperCAmelCase		,    attention_mask=_UpperCAmelCase   )[0]
             # compare the actual values for a slice.
             UpperCAmelCase_      			=							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]		,    _UpperCAmelCase		,    atol=1e-4   )		,    F"""{output[:, 1:4, 1:4]}"""   )









"""simple docstring"""





import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
class 				lowercase__       (   SCREAMING_SNAKE_CASE				):







       '''simple docstring'''


       UpperCamelCase						=					['''image_processor''', '''tokenizer''']
       UpperCamelCase						=					'''ViTImageProcessor'''
       UpperCamelCase						=					('''CLIPTokenizer''', '''CLIPTokenizerFast''')



       def __init__(							self      :  List[Any]		,    _UpperCAmelCase      :  str=None		,    _UpperCAmelCase      :  Optional[int]=None		,    **_UpperCAmelCase      :  List[str]   )			->						Any:


             '''simple docstring'''




             UpperCAmelCase_      			=							None
             if "feature_extractor" in kwargs:
                   warnings.warn(
                       "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
                       " instead."		,    _UpperCAmelCase		,    )
                   UpperCAmelCase_      			=							kwargs.pop("feature_extractor"   )

             UpperCAmelCase_      			=							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__(_UpperCAmelCase		,    _UpperCAmelCase   )



       def __call__(							self      :  Optional[Any]		,    _UpperCAmelCase      :  Any=None		,    _UpperCAmelCase      :  Optional[int]=None		,    _UpperCAmelCase      :  str=None		,    _UpperCAmelCase      :  List[Any]=None		,    **_UpperCAmelCase      :  str   )			->						Optional[int]:


             '''simple docstring'''




             if text is None and visual_prompt is None and images is None:
                   raise ValueError("You have to specify either text, visual prompt or images."   )

             if text is not None and visual_prompt is not None:
                   raise ValueError("You have to specify exactly one type of prompt. Either text or visual prompt."   )

             if text is not None:
                   UpperCAmelCase_      			=							self.tokenizer(_UpperCAmelCase		,    return_tensors=_UpperCAmelCase		,    **_UpperCAmelCase   )

             if visual_prompt is not None:
                   UpperCAmelCase_      			=							self.image_processor(_UpperCAmelCase		,    return_tensors=_UpperCAmelCase		,    **_UpperCAmelCase   )

             if images is not None:
                   UpperCAmelCase_      			=							self.image_processor(_UpperCAmelCase		,    return_tensors=_UpperCAmelCase		,    **_UpperCAmelCase   )

             if visual_prompt is not None and images is not None:
                   UpperCAmelCase_      			=							{
                       "pixel_values": image_features.pixel_values,
                       "conditional_pixel_values": prompt_features.pixel_values,
                   }
                   return encoding
             elif text is not None and images is not None:
                   UpperCAmelCase_      			=							image_features.pixel_values
                   return encoding
             elif text is not None:
                   return encoding
             elif visual_prompt is not None:
                   UpperCAmelCase_      			=							{
                       "conditional_pixel_values": prompt_features.pixel_values,
                   }
                   return encoding
             else:
                   return BatchEncoding(data=dict(**_UpperCAmelCase   )		,    tensor_type=_UpperCAmelCase   )



       def 					lowercase__    (							self      :  List[Any]		,    *_UpperCAmelCase      :  List[str]		,    **_UpperCAmelCase      :  List[Any]   )			->						Any:


             '''simple docstring'''




             return self.tokenizer.batch_decode(*_UpperCAmelCase		,    **_UpperCAmelCase   )



       def 					lowercase__    (							self      :  Dict		,    *_UpperCAmelCase      :  Tuple		,    **_UpperCAmelCase      :  List[Any]   )			->						Union[str, Any]:


             '''simple docstring'''




             return self.tokenizer.decode(*_UpperCAmelCase		,    **_UpperCAmelCase   )



       @property
       def 					lowercase__    (							self      :  int   )			->						Optional[int]:


             '''simple docstring'''




             warnings.warn(
                 "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."		,    _UpperCAmelCase		,    )
             return self.image_processor_class







       @property
       def 					lowercase__    (							self      :  str   )			->						List[Any]:


             '''simple docstring'''




             warnings.warn(
                 "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."		,    _UpperCAmelCase		,    )
             return self.image_processor




