






"""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 ,		snake_case__ ,		snake_case__=13 ,		snake_case__=7 ,		snake_case__=True ,		snake_case__=True ,		snake_case__=True ,		snake_case__=True ,		snake_case__=99 ,		snake_case__=32 ,		snake_case__=5 ,		snake_case__=4 ,		snake_case__=37 ,		snake_case__="gelu" ,		snake_case__=0.1 ,		snake_case__=0.1 ,		snake_case__=5_12 ,		snake_case__=16 ,		snake_case__=2 ,		snake_case__=0.02 ,		snake_case__=False ,		snake_case__=True ,		snake_case__="None" ,		snake_case__=3 ,		snake_case__=4 ,		snake_case__=None ,		)			->			Optional[int]:



         """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 		UpperCamelCase_				(      self				)			->			Optional[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 		UpperCamelCase_				(      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 		UpperCamelCase_				(      self				)			->			str:



         """simple docstring"""





         UpperCAmelCase	    =			self.get_config()
         UpperCAmelCase	    =			3_00
         return config



  def 		UpperCamelCase_				(      self ,		snake_case__				)			->			str:



         """simple docstring"""





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



  def 		UpperCamelCase_				(      self ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__				)			->			Optional[int]:



         """simple docstring"""





         UpperCAmelCase	    =			DebertaModel(config=snake_case__				)
         model.to(snake_case__				)
         model.eval()
         UpperCAmelCase	    =			model(snake_case__ ,		attention_mask=snake_case__ ,		token_type_ids=snake_case__				)[0]
         UpperCAmelCase	    =			model(snake_case__ ,		token_type_ids=snake_case__				)[0]
         UpperCAmelCase	    =			model(snake_case__				)[0]

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



  def 		UpperCamelCase_				(      self ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__				)			->			Dict:



         """simple docstring"""





         UpperCAmelCase	    =			DebertaForMaskedLM(config=snake_case__				)
         model.to(snake_case__				)
         model.eval()
         UpperCAmelCase	    =			model(snake_case__ ,		attention_mask=snake_case__ ,		token_type_ids=snake_case__ ,		labels=snake_case__				)

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



  def 		UpperCamelCase_				(      self ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__				)			->			Optional[Any]:



         """simple docstring"""





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



  def 		UpperCamelCase_				(      self ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__				)			->			int:



         """simple docstring"""





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



  def 		UpperCamelCase_				(      self ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__ ,		snake_case__				)			->			int:



         """simple docstring"""





         UpperCAmelCase	    =			DebertaForQuestionAnswering(config=snake_case__				)
         model.to(snake_case__				)
         model.eval()
         UpperCAmelCase	    =			model(
             snake_case__ ,		attention_mask=snake_case__ ,		token_type_ids=snake_case__ ,		start_positions=snake_case__ ,		end_positions=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 		UpperCamelCase_				(      self				)			->			List[str]:



         """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   UpperCamelCase_ (       a_   ,					a_   ,					unittest.TestCase  ):
  _A       :					Any				=	(
      (
          DebertaModel,
          DebertaForMaskedLM,
          DebertaForSequenceClassification,
          DebertaForTokenClassification,
          DebertaForQuestionAnswering,
      )
      if is_torch_available()
      else ()
  )
  _A       :					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 {}
  )

  _A       :					Dict				=	True
  _A       :					List[Any]				=	False
  _A       :					str				=	False
  _A       :					Optional[int]				=	False
  _A       :					List[str]				=	False



  def 		UpperCamelCase_				(      self				)			->			Any:



         """simple docstring"""





         UpperCAmelCase	    =			DebertaModelTester(self				)
         UpperCAmelCase	    =			ConfigTester(self ,		config_class=snake_case__ ,		hidden_size=37				)



  def 		UpperCamelCase_				(      self				)			->			Dict:



         """simple docstring"""





         self.config_tester.run_common_tests()



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



         """simple docstring"""





         UpperCAmelCase	    =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_model(*snake_case__				)



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



         """simple docstring"""





         UpperCAmelCase	    =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_sequence_classification(*snake_case__				)



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



         """simple docstring"""





         UpperCAmelCase	    =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_masked_lm(*snake_case__				)



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



         """simple docstring"""





         UpperCAmelCase	    =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_question_answering(*snake_case__				)



  def 		UpperCamelCase_				(      self				)			->			Any:



         """simple docstring"""





         UpperCAmelCase	    =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_deberta_for_token_classification(*snake_case__				)



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



         """simple docstring"""





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





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



  @unittest.skip(reason="""Model not available yet"""				)
  def 		UpperCamelCase_				(      self				)			->			Tuple:



         """simple docstring"""





         pass



  @slow
  def 		UpperCamelCase_				(      self				)			->			int:



         """simple docstring"""





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

         UpperCAmelCase	    =			torch.tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]				)
         UpperCAmelCase	    =			torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]				)
         with torch.no_grad():
                UpperCAmelCase	    =			model(snake_case__ ,		attention_mask=snake_case__				)[0]
         # compare the actual values for a slice.
         UpperCAmelCase	    =			torch.tensor(
             [[[-0.5_986, -0.8_055, -0.8_462], [1.4_484, -0.9_348, -0.8_059], [0.3_123, 0.0_032, -1.4_131]]]				)
         self.assertTrue(torch.allclose(output[:, 1:4, 1:4] ,		snake_case__ ,		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   UpperCamelCase_ (       a_  ):
  _A       :					Optional[int]				=	['image_processor', 'tokenizer']
  _A       :					Optional[Any]				=	'ViTImageProcessor'
  _A       :					Tuple				=	('CLIPTokenizer', 'CLIPTokenizerFast')



  def __init__(      self ,		snake_case__=None ,		snake_case__=None ,		**snake_case__				)			->			Union[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.""" ,		snake_case__ ,		)
                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__(snake_case__ ,		snake_case__				)



  def __call__(      self ,		snake_case__=None ,		snake_case__=None ,		snake_case__=None ,		snake_case__=None ,		**snake_case__				)			->			List[str]:



         """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(snake_case__ ,		return_tensors=snake_case__ ,		**snake_case__				)

         if visual_prompt is not None:
                UpperCAmelCase	    =			self.image_processor(snake_case__ ,		return_tensors=snake_case__ ,		**snake_case__				)

         if images is not None:
                UpperCAmelCase	    =			self.image_processor(snake_case__ ,		return_tensors=snake_case__ ,		**snake_case__				)

         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(**snake_case__				) ,		tensor_type=snake_case__				)



  def 		UpperCamelCase_				(      self ,		*snake_case__ ,		**snake_case__				)			->			Tuple:



         """simple docstring"""





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



  def 		UpperCamelCase_				(      self ,		*snake_case__ ,		**snake_case__				)			->			Optional[int]:



         """simple docstring"""





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



  @property
  def 		UpperCamelCase_				(      self				)			->			Tuple:



         """simple docstring"""





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



  @property
  def 		UpperCamelCase_				(      self				)			->			Any:



         """simple docstring"""





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