



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



	def __init__(    self				:     Dict					,						snake_case_				:     Union[str, Any]					,						snake_case_				:     Union[str, Any]=13					,						snake_case_				:     Optional[Any]=7					,						snake_case_				:     Dict=True					,						snake_case_				:     Union[str, Any]=True					,						snake_case_				:     str=True					,						snake_case_				:     Dict=True					,						snake_case_				:     Any=99					,						snake_case_				:     Optional[Any]=32					,						snake_case_				:     str=5					,						snake_case_				:     int=4					,						snake_case_				:     str=37					,						snake_case_				:     int="gelu"					,						snake_case_				:     List[Any]=0.1					,						snake_case_				:     Any=0.1					,						snake_case_				:     List[Any]=5_12					,						snake_case_				:     Tuple=16					,						snake_case_				:     List[Any]=2					,						snake_case_				:     Any=0.0_2					,						snake_case_				:     Union[str, Any]=False					,						snake_case_				:     str=True					,						snake_case_				:     str="None"					,						snake_case_				:     Optional[Any]=3					,						snake_case_				:     Dict=4					,						snake_case_				:     Optional[Any]=None					,						)->				Optional[int]:
						__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 		UpperCamelCase				(    self				:     Any)->				Tuple:
						__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 		UpperCamelCase				(    self				:     Optional[int])->				Optional[Any]:
						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				:     Tuple					,						snake_case_				:     Tuple)->				Union[str, Any]:
						self.parent.assertListEqual(list(result.loss.size())					,						[])



	def 		UpperCamelCase				(    self				:     Tuple					,						snake_case_				:     List[str]					,						snake_case_				:     List[str]					,						snake_case_				:     List[Any]					,						snake_case_				:     Union[str, Any]					,						snake_case_				:     Union[str, Any]					,						snake_case_				:     Any					,						snake_case_				:     List[str])->				Tuple:
						__lowerCAmelCase						 =DebertaVaModel(config=snake_case_)
						model.to(snake_case_)
						model.eval()
						__lowerCAmelCase						 =model(snake_case_					,						attention_mask=snake_case_					,						token_type_ids=snake_case_)[0]
						__lowerCAmelCase						 =model(snake_case_					,						token_type_ids=snake_case_)[0]
						__lowerCAmelCase						 =model(snake_case_)[0]

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



	def 		UpperCamelCase				(    self				:     List[str]					,						snake_case_				:     Optional[Any]					,						snake_case_				:     List[str]					,						snake_case_				:     Optional[Any]					,						snake_case_				:     str					,						snake_case_				:     int					,						snake_case_				:     Optional[Any]					,						snake_case_				:     int)->				Dict:
						__lowerCAmelCase						 =DebertaVaForMaskedLM(config=snake_case_)
						model.to(snake_case_)
						model.eval()
						__lowerCAmelCase						 =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				:     str					,						snake_case_				:     Tuple					,						snake_case_				:     Dict					,						snake_case_				:     List[str]					,						snake_case_				:     str					,						snake_case_				:     Any					,						snake_case_				:     Tuple					,						snake_case_				:     str)->				Union[str, Any]:
						__lowerCAmelCase						 =self.num_labels
						__lowerCAmelCase						 =DebertaVaForSequenceClassification(snake_case_)
						model.to(snake_case_)
						model.eval()
						__lowerCAmelCase						 =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				:     Union[str, Any]					,						snake_case_				:     int					,						snake_case_				:     Optional[int]					,						snake_case_				:     Dict					,						snake_case_				:     List[Any]					,						snake_case_				:     Union[str, Any]					,						snake_case_				:     str					,						snake_case_				:     List[str])->				Tuple:
						__lowerCAmelCase						 =self.num_labels
						__lowerCAmelCase						 =DebertaVaForTokenClassification(config=snake_case_)
						model.to(snake_case_)
						model.eval()
						__lowerCAmelCase						 =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				:     str					,						snake_case_				:     Optional[int]					,						snake_case_				:     str					,						snake_case_				:     Union[str, Any]					,						snake_case_				:     int					,						snake_case_				:     int					,						snake_case_				:     Any					,						snake_case_				:     List[str])->				List[str]:
						__lowerCAmelCase						 =DebertaVaForQuestionAnswering(config=snake_case_)
						model.to(snake_case_)
						model.eval()
						__lowerCAmelCase						 =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				:     Optional[Any]					,						snake_case_				:     Any					,						snake_case_				:     int					,						snake_case_				:     Any					,						snake_case_				:     str					,						snake_case_				:     int					,						snake_case_				:     Tuple					,						snake_case_				:     Optional[Any])->				str:
						__lowerCAmelCase						 =DebertaVaForMultipleChoice(config=snake_case_)
						model.to(snake_case_)
						model.eval()
						__lowerCAmelCase						 =input_ids.unsqueeze(1).expand(-1					,						self.num_choices					,						-1).contiguous()
						__lowerCAmelCase						 =token_type_ids.unsqueeze(1).expand(-1					,						self.num_choices					,						-1).contiguous()
						__lowerCAmelCase						 =input_mask.unsqueeze(1).expand(-1					,						self.num_choices					,						-1).contiguous()
						__lowerCAmelCase						 =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				:     Tuple)->				int:
						__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     __a			(  SCREAMING_SNAKE_CASE						,     SCREAMING_SNAKE_CASE						,     unittest.TestCase					):
	SCREAMING_SNAKE_CASE  	=							(
	    (
	        DebertaVaModel,
	        DebertaVaForMaskedLM,
	        DebertaVaForSequenceClassification,
	        DebertaVaForTokenClassification,
	        DebertaVaForQuestionAnswering,
	        DebertaVaForMultipleChoice,
	    )
	    if is_torch_available()
	    else ()
	)
	SCREAMING_SNAKE_CASE  	=							(
	    {
	        "feature-extraction": DebertaVaModel,
	        "fill-mask": DebertaVaForMaskedLM,
	        "question-answering": DebertaVaForQuestionAnswering,
	        "text-classification": DebertaVaForSequenceClassification,
	        "token-classification": DebertaVaForTokenClassification,
	        "zero-shot": DebertaVaForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)

	SCREAMING_SNAKE_CASE  	=							True
	SCREAMING_SNAKE_CASE  	=							False
	SCREAMING_SNAKE_CASE  	=							False
	SCREAMING_SNAKE_CASE  	=							False
	SCREAMING_SNAKE_CASE  	=							False



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



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



	def 		UpperCamelCase				(    self				:     Union[str, Any])->				int:
						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_model(*snake_case_)



	def 		UpperCamelCase				(    self				:     Union[str, Any])->				List[Any]:
						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_sequence_classification(*snake_case_)



	def 		UpperCamelCase				(    self				:     int)->				Dict:
						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_masked_lm(*snake_case_)



	def 		UpperCamelCase				(    self				:     Tuple)->				Any:
						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_question_answering(*snake_case_)



	def 		UpperCamelCase				(    self				:     Union[str, Any])->				Optional[int]:
						__lowerCAmelCase						 =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_token_classification(*snake_case_)



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



	@slow
	def 		UpperCamelCase				(    self				:     List[Any])->				Any:
						for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__lowerCAmelCase						 =DebertaVaModel.from_pretrained(snake_case_)
											self.assertIsNotNone(snake_case_)






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



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



	@slow
	def 		UpperCamelCase				(    self				:     List[str])->				Optional[int]:
						__lowerCAmelCase						 =DebertaVaModel.from_pretrained("""microsoft/deberta-v2-xlarge""")

						__lowerCAmelCase						 =torch.tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]])
						__lowerCAmelCase						 =torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
						with torch.no_grad():
											__lowerCAmelCase						 =model(snake_case_					,						attention_mask=snake_case_)[0]
						# compare the actual values for a slice.
						__lowerCAmelCase						 =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]					,						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_)




