






import unittest

from transformers import DebertaVaConfig, 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 (
          DebertaVaForMaskedLM,
          DebertaVaForMultipleChoice,
          DebertaVaForQuestionAnswering,
          DebertaVaForSequenceClassification,
          DebertaVaForTokenClassification,
          DebertaVaModel,
      )
      from transformers.models.deberta_va.modeling_deberta_va import DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST







class 				UpperCAmelCase_  (		__lowerCamelCase		):




     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	,						):
          UpperCAmelCase__      :  Any				      =			parent
          UpperCAmelCase__      :  Tuple				      =			batch_size
          UpperCAmelCase__      :  List[str]				      =			seq_length
          UpperCAmelCase__      :  List[Any]				      =			is_training
          UpperCAmelCase__      :  Any				      =			use_input_mask
          UpperCAmelCase__      :  List[str]				      =			use_token_type_ids
          UpperCAmelCase__      :  List[str]				      =			use_labels
          UpperCAmelCase__      :  Tuple				      =			vocab_size
          UpperCAmelCase__      :  List[Any]				      =			hidden_size
          UpperCAmelCase__      :  Dict				      =			num_hidden_layers
          UpperCAmelCase__      :  Optional[int]				      =			num_attention_heads
          UpperCAmelCase__      :  Union[str, Any]				      =			intermediate_size
          UpperCAmelCase__      :  Dict				      =			hidden_act
          UpperCAmelCase__      :  List[Any]				      =			hidden_dropout_prob
          UpperCAmelCase__      :  Dict				      =			attention_probs_dropout_prob
          UpperCAmelCase__      :  List[str]				      =			max_position_embeddings
          UpperCAmelCase__      :  List[str]				      =			type_vocab_size
          UpperCAmelCase__      :  Tuple				      =			type_sequence_label_size
          UpperCAmelCase__      :  Tuple				      =			initializer_range
          UpperCAmelCase__      :  Union[str, Any]				      =			num_labels
          UpperCAmelCase__      :  Union[str, Any]				      =			num_choices
          UpperCAmelCase__      :  List[str]				      =			relative_attention
          UpperCAmelCase__      :  Optional[Any]				      =			position_biased_input
          UpperCAmelCase__      :  int				      =			pos_att_type
          UpperCAmelCase__      :  Dict				      =			scope




     def   __UpperCAmelCase  (				self		):
          UpperCAmelCase__      :  Dict				      =			ids_tensor([self.batch_size, self.seq_length]	,						self.vocab_size		)

          UpperCAmelCase__      :  Optional[Any]				      =			None
          if self.use_input_mask:
               UpperCAmelCase__      :  List[Any]				      =			ids_tensor([self.batch_size, self.seq_length]	,						vocab_size=2		)

          UpperCAmelCase__      :  Union[str, Any]				      =			None
          if self.use_token_type_ids:
               UpperCAmelCase__      :  Optional[int]				      =			ids_tensor([self.batch_size, self.seq_length]	,						self.type_vocab_size		)

          UpperCAmelCase__      :  Dict				      =			None
          UpperCAmelCase__      :  Any				      =			None
          UpperCAmelCase__      :  Any				      =			None
          if self.use_labels:
               UpperCAmelCase__      :  Optional[int]				      =			ids_tensor([self.batch_size]	,						self.type_sequence_label_size		)
               UpperCAmelCase__      :  int				      =			ids_tensor([self.batch_size, self.seq_length]	,						self.num_labels		)
               UpperCAmelCase__      :  Any				      =			ids_tensor([self.batch_size]	,						self.num_choices		)

          UpperCAmelCase__      :  Optional[int]				      =			self.get_config()

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




     def   __UpperCAmelCase  (				self		):
          return 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	,						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	,						_lowerCAmelCase		):
          self.parent.assertListEqual(list(result.loss.size()		)	,						[]		)




     def   __UpperCAmelCase  (				self	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase		):
          UpperCAmelCase__      :  Dict				      =			DebertaVaModel(config=_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          UpperCAmelCase__      :  List[Any]				      =			model(_lowerCAmelCase	,						attention_mask=_lowerCAmelCase	,						token_type_ids=_lowerCAmelCase		)[0]
          UpperCAmelCase__      :  Dict				      =			model(_lowerCAmelCase	,						token_type_ids=_lowerCAmelCase		)[0]
          UpperCAmelCase__      :  int				      =			model(_lowerCAmelCase		)[0]

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




     def   __UpperCAmelCase  (				self	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase		):
          UpperCAmelCase__      :  Dict				      =			DebertaVaForMaskedLM(config=_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          UpperCAmelCase__      :  Union[str, Any]				      =			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   __UpperCAmelCase  (				self	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase		):
          UpperCAmelCase__      :  Union[str, Any]				      =			self.num_labels
          UpperCAmelCase__      :  Optional[int]				      =			DebertaVaForSequenceClassification(_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          UpperCAmelCase__      :  Optional[Any]				      =			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   __UpperCAmelCase  (				self	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase		):
          UpperCAmelCase__      :  int				      =			self.num_labels
          UpperCAmelCase__      :  Optional[int]				      =			DebertaVaForTokenClassification(config=_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          UpperCAmelCase__      :  Dict				      =			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   __UpperCAmelCase  (				self	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase		):
          UpperCAmelCase__      :  Dict				      =			DebertaVaForQuestionAnswering(config=_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          UpperCAmelCase__      :  List[Any]				      =			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   __UpperCAmelCase  (				self	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase	,						_lowerCAmelCase		):
          UpperCAmelCase__      :  Any				      =			DebertaVaForMultipleChoice(config=_lowerCAmelCase		)
          model.to(_lowerCAmelCase		)
          model.eval()
          UpperCAmelCase__      :  str				      =			input_ids.unsqueeze(1		).expand(-1	,						self.num_choices	,						-1		).contiguous()
          UpperCAmelCase__      :  Any				      =			token_type_ids.unsqueeze(1		).expand(-1	,						self.num_choices	,						-1		).contiguous()
          UpperCAmelCase__      :  Dict				      =			input_mask.unsqueeze(1		).expand(-1	,						self.num_choices	,						-1		).contiguous()
          UpperCAmelCase__      :  List[str]				      =			model(
              _lowerCAmelCase	,						attention_mask=_lowerCAmelCase	,						token_type_ids=_lowerCAmelCase	,						labels=_lowerCAmelCase	,						)
          self.parent.assertEqual(result.logits.shape	,						(self.batch_size, self.num_choices)		)




     def   __UpperCAmelCase  (				self		):
          UpperCAmelCase__      :  Any				      =			self.prepare_config_and_inputs()
          (
              (
              UpperCAmelCase__
          )      ,		(
              UpperCAmelCase__
          )      ,		(
              UpperCAmelCase__
          )      ,		(
              UpperCAmelCase__
          )      ,		(
              UpperCAmelCase__
          )      ,		(
              UpperCAmelCase__
          )      ,		(
              UpperCAmelCase__
          )      ,		
          )      :  Union[str, Any]				      =			config_and_inputs
          UpperCAmelCase__      :  str				      =			{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
          return config, inputs_dict







@require_torch
class 				UpperCAmelCase_  (		__lowerCamelCase							,      __lowerCamelCase							,      unittest.TestCase		):
     __lowerCamelCase      				=  (
         (
             DebertaVaModel,
             DebertaVaForMaskedLM,
             DebertaVaForSequenceClassification,
             DebertaVaForTokenClassification,
             DebertaVaForQuestionAnswering,
             DebertaVaForMultipleChoice,
         )
         if is_torch_available()
         else ()
     )
     __lowerCamelCase      				=  (
         {
             'feature-extraction': DebertaVaModel,
             'fill-mask': DebertaVaForMaskedLM,
             'question-answering': DebertaVaForQuestionAnswering,
             'text-classification': DebertaVaForSequenceClassification,
             'token-classification': DebertaVaForTokenClassification,
             'zero-shot': DebertaVaForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )

     __lowerCamelCase      				=  True
     __lowerCamelCase      				=  False
     __lowerCamelCase      				=  False
     __lowerCamelCase      				=  False
     __lowerCamelCase      				=  False




     def   __UpperCAmelCase  (				self		):
          UpperCAmelCase__      :  Optional[int]				      =			DebertaVaModelTester(self		)
          UpperCAmelCase__      :  str				      =			ConfigTester(self	,						config_class=_lowerCAmelCase	,						hidden_size=37		)




     def   __UpperCAmelCase  (				self		):
          self.config_tester.run_common_tests()




     def   __UpperCAmelCase  (				self		):
          UpperCAmelCase__      :  List[str]				      =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_model(*_lowerCAmelCase		)




     def   __UpperCAmelCase  (				self		):
          UpperCAmelCase__      :  Tuple				      =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_sequence_classification(*_lowerCAmelCase		)




     def   __UpperCAmelCase  (				self		):
          UpperCAmelCase__      :  int				      =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_masked_lm(*_lowerCAmelCase		)




     def   __UpperCAmelCase  (				self		):
          UpperCAmelCase__      :  str				      =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_question_answering(*_lowerCAmelCase		)




     def   __UpperCAmelCase  (				self		):
          UpperCAmelCase__      :  Dict				      =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_token_classification(*_lowerCAmelCase		)




     def   __UpperCAmelCase  (				self		):
          UpperCAmelCase__      :  Union[str, Any]				      =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_multiple_choice(*_lowerCAmelCase		)




     @slow
     def   __UpperCAmelCase  (				self		):
          for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               UpperCAmelCase__      :  List[Any]				      =			DebertaVaModel.from_pretrained(_lowerCAmelCase		)
               self.assertIsNotNone(_lowerCAmelCase		)







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




     @unittest.skip(reason="""Model not available yet"""		)
     def   __UpperCAmelCase  (				self		):
          pass




     @slow
     def   __UpperCAmelCase  (				self		):
          UpperCAmelCase__      :  Optional[Any]				      =			DebertaVaModel.from_pretrained("""microsoft/deberta-v2-xlarge"""		)

          UpperCAmelCase__      :  List[Any]				      =			torch.tensor([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]]		)
          UpperCAmelCase__      :  Optional[int]				      =			torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
          with torch.no_grad():
               UpperCAmelCase__      :  Dict				      =			model(_lowerCAmelCase	,						attention_mask=_lowerCAmelCase		)[0]
          # compare the actual values for a slice.
          UpperCAmelCase__      :  Tuple				      =			torch.tensor(
              [[[0.2_3_5_6, 0.1_9_4_8, 0.0_3_6_9], [-0.1_0_6_3, 0.3_5_8_6, -0.5_1_5_2], [-0.6_3_9_9, -0.0_2_5_9, -0.2_5_2_5]]]		)
          self.assertTrue(torch.allclose(output[:, 1:4, 1:4]	,						_lowerCAmelCase	,						atol=1e-4		)	,						f"{output[:, 1:4, 1:4]}"		)












import warnings
from typing import List

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import is_flax_available, is_tf_available, is_torch_available







class        __lowerCAmelCase					(			__magic_name__					):

			"""simple docstring"""







			snake_case_  						=			['''image_processor''', '''tokenizer''']
			snake_case_  						=			'''OwlViTImageProcessor'''
			snake_case_  						=			('''CLIPTokenizer''', '''CLIPTokenizerFast''')


			def __init__( self     ,		lowerCamelCase__=None     ,		lowerCamelCase__=None     ,		**lowerCamelCase__				)       -> Tuple:



							'''simple docstring'''
							__lowerCamelCase									=       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__     ,		)
											__lowerCamelCase									=       kwargs.pop('feature_extractor'				)

							__lowerCamelCase									=       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__				)


			def __call__( self     ,		lowerCamelCase__=None     ,		lowerCamelCase__=None     ,		lowerCamelCase__=None     ,		lowerCamelCase__="max_length"     ,		lowerCamelCase__="np"     ,		**lowerCamelCase__				)       -> Optional[int]:



							'''simple docstring'''

							if text is None and query_images is None and images is None:
											raise ValueError(
											    'You have to specify at least one text or query image or image. All three cannot be none.'				)

							if text is not None:
											if isinstance(lowerCamelCase__     ,		lowerCamelCase__				) or (isinstance(lowerCamelCase__     ,		lowerCamelCase__				) and not isinstance(text[0]     ,		lowerCamelCase__				)):
															__lowerCamelCase									=       [self.tokenizer(lowerCamelCase__     ,		padding=lowerCamelCase__     ,		return_tensors=lowerCamelCase__     ,		**lowerCamelCase__				)]

											elif isinstance(lowerCamelCase__     ,		lowerCamelCase__				) and isinstance(text[0]     ,		lowerCamelCase__				):
															__lowerCamelCase									=       []

															# Maximum number of queries across batch
															__lowerCamelCase									=       max([len(lowerCamelCase__				) for t in text]				)

															# Pad all batch samples to max number of text queries
															for t in text:
																			if len(lowerCamelCase__				) != max_num_queries:
																							__lowerCamelCase									=       t + [' '] * (max_num_queries - len(lowerCamelCase__				))

																			__lowerCamelCase									=       self.tokenizer(lowerCamelCase__     ,		padding=lowerCamelCase__     ,		return_tensors=lowerCamelCase__     ,		**lowerCamelCase__				)
																			encodings.append(lowerCamelCase__				)
											else:
															raise TypeError('Input text should be a string, a list of strings or a nested list of strings'				)

											if return_tensors == "np":
															__lowerCamelCase									=       np.concatenate([encoding['input_ids'] for encoding in encodings]     ,		axis=0				)
															__lowerCamelCase									=       np.concatenate([encoding['attention_mask'] for encoding in encodings]     ,		axis=0				)

											elif return_tensors == "jax" and is_flax_available():
															import jax.numpy as jnp

															__lowerCamelCase									=       jnp.concatenate([encoding['input_ids'] for encoding in encodings]     ,		axis=0				)
															__lowerCamelCase									=       jnp.concatenate([encoding['attention_mask'] for encoding in encodings]     ,		axis=0				)

											elif return_tensors == "pt" and is_torch_available():
															import torch

															__lowerCamelCase									=       torch.cat([encoding['input_ids'] for encoding in encodings]     ,		dim=0				)
															__lowerCamelCase									=       torch.cat([encoding['attention_mask'] for encoding in encodings]     ,		dim=0				)

											elif return_tensors == "tf" and is_tf_available():
															import tensorflow as tf

															__lowerCamelCase									=       tf.stack([encoding['input_ids'] for encoding in encodings]     ,		axis=0				)
															__lowerCamelCase									=       tf.stack([encoding['attention_mask'] for encoding in encodings]     ,		axis=0				)

											else:
															raise ValueError('Target return tensor type could not be returned'				)

											__lowerCamelCase									=       BatchEncoding()
											__lowerCamelCase									=       input_ids
											__lowerCamelCase									=       attention_mask

							if query_images is not None:
											__lowerCamelCase									=       BatchEncoding()
											__lowerCamelCase									=       self.image_processor(
											    lowerCamelCase__     ,		return_tensors=lowerCamelCase__     ,		**lowerCamelCase__				).pixel_values
											__lowerCamelCase									=       query_pixel_values

							if images is not None:
											__lowerCamelCase									=       self.image_processor(lowerCamelCase__     ,		return_tensors=lowerCamelCase__     ,		**lowerCamelCase__				)

							if text is not None and images is not None:
											__lowerCamelCase									=       image_features.pixel_values
											return encoding
							elif query_images is not None and images is not None:
											__lowerCamelCase									=       image_features.pixel_values
											return encoding
							elif text is not None or query_images is not None:
											return encoding
							else:
											return BatchEncoding(data=dict(**lowerCamelCase__				)     ,		tensor_type=lowerCamelCase__				)


			def  lowercase_       ( self     ,		*lowerCamelCase__     ,		**lowerCamelCase__				)       -> Tuple:



							'''simple docstring'''
							return self.image_processor.post_process(*lowerCamelCase__     ,		**lowerCamelCase__				)


			def  lowercase_       ( self     ,		*lowerCamelCase__     ,		**lowerCamelCase__				)       -> Optional[Any]:



							'''simple docstring'''
							return self.image_processor.post_process_object_detection(*lowerCamelCase__     ,		**lowerCamelCase__				)


			def  lowercase_       ( self     ,		*lowerCamelCase__     ,		**lowerCamelCase__				)       -> Optional[int]:



							'''simple docstring'''
							return self.image_processor.post_process_image_guided_detection(*lowerCamelCase__     ,		**lowerCamelCase__				)


			def  lowercase_       ( self     ,		*lowerCamelCase__     ,		**lowerCamelCase__				)       -> Optional[int]:



							'''simple docstring'''
							return self.tokenizer.batch_decode(*lowerCamelCase__     ,		**lowerCamelCase__				)


			def  lowercase_       ( self     ,		*lowerCamelCase__     ,		**lowerCamelCase__				)       -> Optional[int]:



							'''simple docstring'''
							return self.tokenizer.decode(*lowerCamelCase__     ,		**lowerCamelCase__				)


			@property
			def  lowercase_       ( self				)       -> Dict:



							'''simple docstring'''
							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  lowercase_       ( self				)       -> Union[str, Any]:



							'''simple docstring'''
							warnings.warn(
							    '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'     ,		lowerCamelCase__     ,		)
							return self.image_processor


