




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 		_lowerCAmelCase       (							__UpperCamelCase	):
			"""simple docstring"""
			def __init__(					self     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE=13     ,		__SCREAMING_SNAKE_CASE=7     ,		__SCREAMING_SNAKE_CASE=True     ,		__SCREAMING_SNAKE_CASE=True     ,		__SCREAMING_SNAKE_CASE=True     ,		__SCREAMING_SNAKE_CASE=True     ,		__SCREAMING_SNAKE_CASE=99     ,		__SCREAMING_SNAKE_CASE=32     ,		__SCREAMING_SNAKE_CASE=5     ,		__SCREAMING_SNAKE_CASE=4     ,		__SCREAMING_SNAKE_CASE=37     ,		__SCREAMING_SNAKE_CASE="gelu"     ,		__SCREAMING_SNAKE_CASE=0.1     ,		__SCREAMING_SNAKE_CASE=0.1     ,		__SCREAMING_SNAKE_CASE=512     ,		__SCREAMING_SNAKE_CASE=16     ,		__SCREAMING_SNAKE_CASE=2     ,		__SCREAMING_SNAKE_CASE=0.02     ,		__SCREAMING_SNAKE_CASE=False     ,		__SCREAMING_SNAKE_CASE=True     ,		__SCREAMING_SNAKE_CASE="None"     ,		__SCREAMING_SNAKE_CASE=3     ,		__SCREAMING_SNAKE_CASE=4     ,		__SCREAMING_SNAKE_CASE=None     ,		)		->							Optional[int]:





				"""simple docstring"""







				snake_case__						:     Optional[int]		=parent
				snake_case__						:     Dict		=batch_size
				snake_case__						:     int		=seq_length
				snake_case__						:     Optional[int]		=is_training
				snake_case__						:     Dict		=use_input_mask
				snake_case__						:     List[str]		=use_token_type_ids
				snake_case__						:     Optional[Any]		=use_labels
				snake_case__						:     Union[str, Any]		=vocab_size
				snake_case__						:     List[Any]		=hidden_size
				snake_case__						:     Tuple		=num_hidden_layers
				snake_case__						:     Optional[int]		=num_attention_heads
				snake_case__						:     List[Any]		=intermediate_size
				snake_case__						:     Dict		=hidden_act
				snake_case__						:     Union[str, Any]		=hidden_dropout_prob
				snake_case__						:     int		=attention_probs_dropout_prob
				snake_case__						:     str		=max_position_embeddings
				snake_case__						:     int		=type_vocab_size
				snake_case__						:     int		=type_sequence_label_size
				snake_case__						:     Tuple		=initializer_range
				snake_case__						:     Union[str, Any]		=num_labels
				snake_case__						:     List[str]		=num_choices
				snake_case__						:     int		=relative_attention
				snake_case__						:     Optional[Any]		=position_biased_input
				snake_case__						:     Tuple		=pos_att_type
				snake_case__						:     List[str]		=scope
			def 					UpperCAmelCase     (					self       )		->							Tuple:





				"""simple docstring"""







				snake_case__						:     Union[str, Any]		=ids_tensor([self.batch_size, self.seq_length]     ,		self.vocab_size       )

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

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

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

				snake_case__						:     int		=self.get_config()

				return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
			def 					UpperCAmelCase     (					self       )		->							Optional[Any]:





				"""simple docstring"""







				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     ,		__SCREAMING_SNAKE_CASE       )		->							Union[str, Any]:





				"""simple docstring"""







				self.parent.assertListEqual(list(result.loss.size()       )     ,		[]       )
			def 					UpperCAmelCase     (					self     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE       )		->							Tuple:





				"""simple docstring"""







				snake_case__						:     List[str]		=DebertaVaModel(config=snake_case_       )
				model.to(snake_case_       )
				model.eval()
				snake_case__						:     Optional[Any]		=model(snake_case_     ,		attention_mask=snake_case_     ,		token_type_ids=snake_case_       )[0]
				snake_case__						:     Union[str, Any]		=model(snake_case_     ,		token_type_ids=snake_case_       )[0]
				snake_case__						:     Union[str, Any]		=model(snake_case_       )[0]

				self.parent.assertListEqual(list(sequence_output.size()       )     ,		[self.batch_size, self.seq_length, self.hidden_size]       )
			def 					UpperCAmelCase     (					self     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE       )		->							Dict:





				"""simple docstring"""







				snake_case__						:     Any		=DebertaVaForMaskedLM(config=snake_case_       )
				model.to(snake_case_       )
				model.eval()
				snake_case__						:     Tuple		=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     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE       )		->							Union[str, Any]:





				"""simple docstring"""







				snake_case__						:     Optional[int]		=self.num_labels
				snake_case__						:     Tuple		=DebertaVaForSequenceClassification(snake_case_       )
				model.to(snake_case_       )
				model.eval()
				snake_case__						:     List[Any]		=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     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE       )		->							Tuple:





				"""simple docstring"""







				snake_case__						:     List[str]		=self.num_labels
				snake_case__						:     Any		=DebertaVaForTokenClassification(config=snake_case_       )
				model.to(snake_case_       )
				model.eval()
				snake_case__						:     int		=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     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE       )		->							List[str]:





				"""simple docstring"""







				snake_case__						:     Any		=DebertaVaForQuestionAnswering(config=snake_case_       )
				model.to(snake_case_       )
				model.eval()
				snake_case__						:     Optional[Any]		=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     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE       )		->							str:





				"""simple docstring"""







				snake_case__						:     int		=DebertaVaForMultipleChoice(config=snake_case_       )
				model.to(snake_case_       )
				model.eval()
				snake_case__						:     str		=input_ids.unsqueeze(1       ).expand(-1     ,		self.num_choices     ,		-1       ).contiguous()
				snake_case__						:     Dict		=token_type_ids.unsqueeze(1       ).expand(-1     ,		self.num_choices     ,		-1       ).contiguous()
				snake_case__						:     List[Any]		=input_mask.unsqueeze(1       ).expand(-1     ,		self.num_choices     ,		-1       ).contiguous()
				snake_case__						:     Dict		=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.num_choices)       )


			def 					UpperCAmelCase     (					self       )		->							int:





				"""simple docstring"""







				snake_case__						:     List[str]		=self.prepare_config_and_inputs()
				(
				    (
				    snake_case__						
				),    (
				    snake_case__						
				),    (
				    snake_case__						
				),    (
				    snake_case__						
				),    (
				    snake_case__						
				),    (
				    snake_case__						
				),    (
				    snake_case__						
				),    						
				)						:     Dict		=config_and_inputs
				snake_case__						:     int		={'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
				return config, inputs_dict






@require_torch
class 		_lowerCAmelCase       (							__UpperCamelCase			,    __UpperCamelCase			,    unittest.TestCase	):
			"""simple docstring"""
			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       )		->							Optional[Any]:





				"""simple docstring"""







				snake_case__						:     Optional[Any]		=DebertaVaModelTester(self       )
				snake_case__						:     List[Any]		=ConfigTester(self     ,		config_class=snake_case_     ,		hidden_size=37       )
			def 					UpperCAmelCase     (					self       )		->							List[Any]:





				"""simple docstring"""







				self.config_tester.run_common_tests()
			def 					UpperCAmelCase     (					self       )		->							int:





				"""simple docstring"""







				snake_case__						:     List[Any]		=self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_model(*snake_case_       )
			def 					UpperCAmelCase     (					self       )		->							List[Any]:





				"""simple docstring"""







				snake_case__						:     List[Any]		=self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_sequence_classification(*snake_case_       )
			def 					UpperCAmelCase     (					self       )		->							Dict:





				"""simple docstring"""







				snake_case__						:     Any		=self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_masked_lm(*snake_case_       )
			def 					UpperCAmelCase     (					self       )		->							Any:





				"""simple docstring"""







				snake_case__						:     Dict		=self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_question_answering(*snake_case_       )
			def 					UpperCAmelCase     (					self       )		->							Optional[int]:





				"""simple docstring"""







				snake_case__						:     List[str]		=self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_token_classification(*snake_case_       )
			def 					UpperCAmelCase     (					self       )		->							Any:





				"""simple docstring"""







				snake_case__						:     List[str]		=self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_deberta_for_multiple_choice(*snake_case_       )


			@slow
			def 					UpperCAmelCase     (					self       )		->							Any:





				"""simple docstring"""







				for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					snake_case__						:     Optional[Any]		=DebertaVaModel.from_pretrained(snake_case_       )
					self.assertIsNotNone(snake_case_       )






@require_torch
@require_sentencepiece
@require_tokenizers
class 		_lowerCAmelCase       (							unittest.TestCase	):
			"""simple docstring"""
			@unittest.skip(reason='''Model not available yet'''       )
			def 					UpperCAmelCase     (					self       )		->							Any:





				"""simple docstring"""







				pass


			@slow
			def 					UpperCAmelCase     (					self       )		->							Optional[int]:





				"""simple docstring"""







				snake_case__						:     Dict		=DebertaVaModel.from_pretrained('''microsoft/deberta-v2-xlarge'''       )

				snake_case__						:     List[Any]		=torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]       )
				snake_case__						:     str		=torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]       )
				with torch.no_grad():
					snake_case__						:     int		=model(snake_case_     ,		attention_mask=snake_case_       )[0]
				# compare the actual values for a slice.
				snake_case__						:     List[str]		=torch.tensor(
				    [[[0.2356, 0.1948, 0.0369], [-0.1063, 0.3586, -0.5152], [-0.6399, -0.0259, -0.2525]]]       )
				self.assertTrue(torch.allclose(output[:, 1:4, 1:4]     ,		snake_case_     ,		atol=1e-4       )     ,		f'''{output[:, 1:4, 1:4]}'''       )









from __future__ import annotations

import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_tf, slow
from transformers.utils import is_tf_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask


if is_tf_available():
				import tensorflow as tf

				from transformers import TFBlipTextModel
				from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST






class     __a			:



	def __init__(    self				:     int					,						snake_case_				:     Dict					,						snake_case_				:     List[str]=12					,						snake_case_				:     Optional[int]=7					,						snake_case_				:     Optional[Any]=True					,						snake_case_				:     int=True					,						snake_case_				:     int=True					,						snake_case_				:     Any=99					,						snake_case_				:     List[Any]=32					,						snake_case_				:     List[Any]=32					,						snake_case_				:     Any=2					,						snake_case_				:     Optional[Any]=4					,						snake_case_				:     Optional[Any]=37					,						snake_case_				:     Tuple=0.1					,						snake_case_				:     str=0.1					,						snake_case_				:     Any=5_12					,						snake_case_				:     Optional[Any]=0.0_2					,						snake_case_				:     List[Any]=0					,						snake_case_				:     Union[str, Any]=None					,						)->				List[Any]:
						__lowerCAmelCase						 =parent
						__lowerCAmelCase						 =batch_size
						__lowerCAmelCase						 =seq_length
						__lowerCAmelCase						 =is_training
						__lowerCAmelCase						 =use_input_mask
						__lowerCAmelCase						 =use_labels
						__lowerCAmelCase						 =vocab_size
						__lowerCAmelCase						 =hidden_size
						__lowerCAmelCase						 =projection_dim
						__lowerCAmelCase						 =num_hidden_layers
						__lowerCAmelCase						 =num_attention_heads
						__lowerCAmelCase						 =intermediate_size
						__lowerCAmelCase						 =dropout
						__lowerCAmelCase						 =attention_dropout
						__lowerCAmelCase						 =max_position_embeddings
						__lowerCAmelCase						 =initializer_range
						__lowerCAmelCase						 =scope
						__lowerCAmelCase						 =bos_token_id



	def 		UpperCamelCase				(    self				:     List[Any])->				Optional[int]:
						__lowerCAmelCase						 =ids_tensor([self.batch_size, self.seq_length]					,						self.vocab_size)

						__lowerCAmelCase						 =None
						if self.use_input_mask:
											__lowerCAmelCase						 =random_attention_mask([self.batch_size, self.seq_length])

						if input_mask is not None:
											__lowerCAmelCase						 =input_mask.numpy()
											__lowerCAmelCase ,						__lowerCAmelCase						 =input_mask.shape
											__lowerCAmelCase						 =np.random.randint(1					,						seq_length - 1					,						size=(batch_size,))
											for batch_idx, start_index in enumerate(snake_case_):
																__lowerCAmelCase						 =1
																__lowerCAmelCase						 =0

						__lowerCAmelCase						 =self.get_config()

						return config, input_ids, tf.convert_to_tensor(snake_case_)



	def 		UpperCamelCase				(    self				:     List[str])->				str:
						return BlipTextConfig(
						    vocab_size=self.vocab_size					,						hidden_size=self.hidden_size					,						projection_dim=self.projection_dim					,						num_hidden_layers=self.num_hidden_layers					,						num_attention_heads=self.num_attention_heads					,						intermediate_size=self.intermediate_size					,						dropout=self.dropout					,						attention_dropout=self.attention_dropout					,						max_position_embeddings=self.max_position_embeddings					,						initializer_range=self.initializer_range					,						bos_token_id=self.bos_token_id					,						)



	def 		UpperCamelCase				(    self				:     Union[str, Any]					,						snake_case_				:     Tuple					,						snake_case_				:     List[Any]					,						snake_case_				:     List[Any])->				List[str]:
						__lowerCAmelCase						 =TFBlipTextModel(config=snake_case_)
						__lowerCAmelCase						 =model(snake_case_					,						attention_mask=snake_case_					,						training=snake_case_)
						__lowerCAmelCase						 =model(snake_case_					,						training=snake_case_)
						self.parent.assertEqual(result.last_hidden_state.shape					,						(self.batch_size, self.seq_length, self.hidden_size))
						self.parent.assertEqual(result.pooler_output.shape					,						(self.batch_size, self.hidden_size))



	def 		UpperCamelCase				(    self				:     Dict)->				Tuple:
						__lowerCAmelCase						 =self.prepare_config_and_inputs()
						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase						 =config_and_inputs
						__lowerCAmelCase						 ={"""input_ids""": input_ids, """attention_mask""": input_mask}
						return config, inputs_dict






@require_tf
class     __a			(  SCREAMING_SNAKE_CASE						,     unittest.TestCase					):
	SCREAMING_SNAKE_CASE  	=							(TFBlipTextModel,) if is_tf_available() else ()
	SCREAMING_SNAKE_CASE  	=							False
	SCREAMING_SNAKE_CASE  	=							False
	SCREAMING_SNAKE_CASE  	=							False



	def 		UpperCamelCase				(    self				:     Optional[Any])->				Any:
						__lowerCAmelCase						 =BlipTextModelTester(self)
						__lowerCAmelCase						 =ConfigTester(self					,						config_class=snake_case_					,						hidden_size=37)



	def 		UpperCamelCase				(    self				:     List[Any])->				List[str]:
						self.config_tester.run_common_tests()



	def 		UpperCamelCase				(    self				:     List[str])->				Dict:
						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*snake_case_)



	def 		UpperCamelCase				(    self				:     Optional[Any])->				str:
						pass



	def 		UpperCamelCase				(    self				:     List[str])->				int:
						pass



	@unittest.skip(reason="""Blip does not use inputs_embeds""")
	def 		UpperCamelCase				(    self				:     Optional[Any])->				int:
						pass



	@unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING""")
	def 		UpperCamelCase				(    self				:     List[Any])->				str:
						pass



	@unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING""")
	def 		UpperCamelCase				(    self				:     Optional[int])->				Tuple:
						pass



	@slow
	def 		UpperCamelCase				(    self				:     int)->				str:
						for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__lowerCAmelCase						 =TFBlipTextModel.from_pretrained(snake_case_)
											self.assertIsNotNone(snake_case_)



	def 		UpperCamelCase				(    self				:     str					,						snake_case_				:     List[str]=True)->				int:
						super().test_pt_tf_model_equivalence(allow_missing_keys=snake_case_)




