



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  __magic_name__   (							lowercase_						):






		def __init__(						self      :     List[str]				,		UpperCamelCase__      :     str				,		UpperCamelCase__      :     int=13				,		UpperCamelCase__      :     Tuple=7				,		UpperCamelCase__      :     Optional[int]=True				,		UpperCamelCase__      :     Optional[Any]=True				,		UpperCamelCase__      :     Optional[Any]=True				,		UpperCamelCase__      :     Any=True				,		UpperCamelCase__      :     str=99				,		UpperCamelCase__      :     Optional[Any]=32				,		UpperCamelCase__      :     Union[str, Any]=5				,		UpperCamelCase__      :     Tuple=4				,		UpperCamelCase__      :     List[str]=37				,		UpperCamelCase__      :     List[str]="gelu"				,		UpperCamelCase__      :     Any=0.1				,		UpperCamelCase__      :     Any=0.1				,		UpperCamelCase__      :     Dict=5_12				,		UpperCamelCase__      :     int=16				,		UpperCamelCase__      :     Optional[Any]=2				,		UpperCamelCase__      :     str=0.02				,		UpperCamelCase__      :     Dict=False				,		UpperCamelCase__      :     Dict=True				,		UpperCamelCase__      :     Optional[Any]="None"				,		UpperCamelCase__      :     Any=3				,		UpperCamelCase__      :     Union[str, Any]=4				,		UpperCamelCase__      :     Tuple=None				,		)				->					Dict:
								'''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 SCREAMING_SNAKE_CASE_			(						self      :     Tuple      )				->					str:
								'''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 SCREAMING_SNAKE_CASE_			(						self      :     int      )				->					Optional[int]:
								'''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 SCREAMING_SNAKE_CASE_			(						self      :     int				,		UpperCamelCase__      :     Dict      )				->					Tuple:
								'''simple docstring'''







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






		def SCREAMING_SNAKE_CASE_			(						self      :     Dict				,		UpperCamelCase__      :     str				,		UpperCamelCase__      :     List[str]				,		UpperCamelCase__      :     Optional[Any]				,		UpperCamelCase__      :     Union[str, Any]				,		UpperCamelCase__      :     Optional[int]				,		UpperCamelCase__      :     int				,		UpperCamelCase__      :     Tuple      )				->					int:
								'''simple docstring'''







								UpperCAmelCase         =    DebertaVaModel(config=lowerCamelCase_      )
								model.to(lowerCamelCase_      )
								model.eval()
								UpperCAmelCase         =    model(lowerCamelCase_				,		attention_mask=lowerCamelCase_				,		token_type_ids=lowerCamelCase_      )[0]
								UpperCAmelCase         =    model(lowerCamelCase_				,		token_type_ids=lowerCamelCase_      )[0]
								UpperCAmelCase         =    model(lowerCamelCase_      )[0]

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






		def SCREAMING_SNAKE_CASE_			(						self      :     Optional[Any]				,		UpperCamelCase__      :     int				,		UpperCamelCase__      :     Tuple				,		UpperCamelCase__      :     int				,		UpperCamelCase__      :     Optional[Any]				,		UpperCamelCase__      :     Dict				,		UpperCamelCase__      :     Optional[int]				,		UpperCamelCase__      :     Optional[Any]      )				->					Optional[int]:
								'''simple docstring'''







								UpperCAmelCase         =    DebertaVaForMaskedLM(config=lowerCamelCase_      )
								model.to(lowerCamelCase_      )
								model.eval()
								UpperCAmelCase         =    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 SCREAMING_SNAKE_CASE_			(						self      :     Dict				,		UpperCamelCase__      :     List[str]				,		UpperCamelCase__      :     List[Any]				,		UpperCamelCase__      :     Tuple				,		UpperCamelCase__      :     List[Any]				,		UpperCamelCase__      :     Union[str, Any]				,		UpperCamelCase__      :     Optional[int]				,		UpperCamelCase__      :     Dict      )				->					List[Any]:
								'''simple docstring'''







								UpperCAmelCase         =    self.num_labels
								UpperCAmelCase         =    DebertaVaForSequenceClassification(lowerCamelCase_      )
								model.to(lowerCamelCase_      )
								model.eval()
								UpperCAmelCase         =    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 SCREAMING_SNAKE_CASE_			(						self      :     Any				,		UpperCamelCase__      :     Union[str, Any]				,		UpperCamelCase__      :     Optional[Any]				,		UpperCamelCase__      :     Union[str, Any]				,		UpperCamelCase__      :     Tuple				,		UpperCamelCase__      :     Union[str, Any]				,		UpperCamelCase__      :     Optional[Any]				,		UpperCamelCase__      :     Tuple      )				->					Tuple:
								'''simple docstring'''







								UpperCAmelCase         =    self.num_labels
								UpperCAmelCase         =    DebertaVaForTokenClassification(config=lowerCamelCase_      )
								model.to(lowerCamelCase_      )
								model.eval()
								UpperCAmelCase         =    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 SCREAMING_SNAKE_CASE_			(						self      :     Dict				,		UpperCamelCase__      :     Optional[int]				,		UpperCamelCase__      :     Any				,		UpperCamelCase__      :     Any				,		UpperCamelCase__      :     List[Any]				,		UpperCamelCase__      :     List[str]				,		UpperCamelCase__      :     List[str]				,		UpperCamelCase__      :     List[str]      )				->					Optional[Any]:
								'''simple docstring'''







								UpperCAmelCase         =    DebertaVaForQuestionAnswering(config=lowerCamelCase_      )
								model.to(lowerCamelCase_      )
								model.eval()
								UpperCAmelCase         =    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 SCREAMING_SNAKE_CASE_			(						self      :     str				,		UpperCamelCase__      :     str				,		UpperCamelCase__      :     Any				,		UpperCamelCase__      :     Dict				,		UpperCamelCase__      :     Tuple				,		UpperCamelCase__      :     Optional[int]				,		UpperCamelCase__      :     Optional[int]				,		UpperCamelCase__      :     Tuple      )				->					List[str]:
								'''simple docstring'''







								UpperCAmelCase         =    DebertaVaForMultipleChoice(config=lowerCamelCase_      )
								model.to(lowerCamelCase_      )
								model.eval()
								UpperCAmelCase         =    input_ids.unsqueeze(1      ).expand(-1				,		self.num_choices				,		-1      ).contiguous()
								UpperCAmelCase         =    token_type_ids.unsqueeze(1      ).expand(-1				,		self.num_choices				,		-1      ).contiguous()
								UpperCAmelCase         =    input_mask.unsqueeze(1      ).expand(-1				,		self.num_choices				,		-1      ).contiguous()
								UpperCAmelCase         =    model(
								    lowerCamelCase_				,		attention_mask=lowerCamelCase_				,		token_type_ids=lowerCamelCase_				,		labels=lowerCamelCase_				,		)
								self.parent.assertEqual(result.logits.shape				,		(self.batch_size, self.num_choices)      )







		def SCREAMING_SNAKE_CASE_			(						self      :     Tuple      )				->					Dict:
								'''simple docstring'''







								UpperCAmelCase         =    self.prepare_config_and_inputs()
								(
								    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  __magic_name__   (							lowercase_,  lowercase_,  unittest.TestCase						):
		lowercase      :       Tuple          =(
		    (
		        DebertaVaModel,
		        DebertaVaForMaskedLM,
		        DebertaVaForSequenceClassification,
		        DebertaVaForTokenClassification,
		        DebertaVaForQuestionAnswering,
		        DebertaVaForMultipleChoice,
		    )
		    if is_torch_available()
		    else ()
		)
		lowercase      :       Optional[Any]          =(
		    {
		        '''feature-extraction''': DebertaVaModel,
		        '''fill-mask''': DebertaVaForMaskedLM,
		        '''question-answering''': DebertaVaForQuestionAnswering,
		        '''text-classification''': DebertaVaForSequenceClassification,
		        '''token-classification''': DebertaVaForTokenClassification,
		        '''zero-shot''': DebertaVaForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)

		lowercase      :       Any          =True
		lowercase      :       Any          =False
		lowercase      :       List[str]          =False
		lowercase      :       str          =False
		lowercase      :       Tuple          =False






		def SCREAMING_SNAKE_CASE_			(						self      :     Tuple      )				->					Any:
								'''simple docstring'''







								UpperCAmelCase         =    DebertaVaModelTester(self      )
								UpperCAmelCase         =    ConfigTester(self				,		config_class=lowerCamelCase_				,		hidden_size=37      )






		def SCREAMING_SNAKE_CASE_			(						self      :     Dict      )				->					List[Any]:
								'''simple docstring'''







								self.config_tester.run_common_tests()






		def SCREAMING_SNAKE_CASE_			(						self      :     str      )				->					Optional[int]:
								'''simple docstring'''







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






		def SCREAMING_SNAKE_CASE_			(						self      :     Any      )				->					Optional[int]:
								'''simple docstring'''







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






		def SCREAMING_SNAKE_CASE_			(						self      :     Tuple      )				->					Optional[int]:
								'''simple docstring'''







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






		def SCREAMING_SNAKE_CASE_			(						self      :     Union[str, Any]      )				->					int:
								'''simple docstring'''







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






		def SCREAMING_SNAKE_CASE_			(						self      :     str      )				->					int:
								'''simple docstring'''







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






		def SCREAMING_SNAKE_CASE_			(						self      :     Union[str, Any]      )				->					List[str]:
								'''simple docstring'''







								UpperCAmelCase         =    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_multiple_choice(*lowerCamelCase_      )







		@slow
		def SCREAMING_SNAKE_CASE_			(						self      :     Tuple      )				->					List[str]:
								'''simple docstring'''







								for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														UpperCAmelCase         =    DebertaVaModel.from_pretrained(lowerCamelCase_      )
														self.assertIsNotNone(lowerCamelCase_      )





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






		@unittest.skip(reason="Model not available yet"      )
		def SCREAMING_SNAKE_CASE_			(						self      :     Dict      )				->					List[Any]:
								'''simple docstring'''







								pass







		@slow
		def SCREAMING_SNAKE_CASE_			(						self      :     List[str]      )				->					Union[str, Any]:
								'''simple docstring'''







								UpperCAmelCase         =    DebertaVaModel.from_pretrained("microsoft/deberta-v2-xlarge"      )

								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(lowerCamelCase_				,		attention_mask=lowerCamelCase_      )[0]
								# compare the actual values for a slice.
								UpperCAmelCase         =    torch.tensor(
								    [[[0.23_56, 0.19_48, 0.03_69], [-0.10_63, 0.35_86, -0.51_52], [-0.63_99, -0.02_59, -0.25_25]]]      )
								self.assertTrue(torch.allclose(output[:, 1:4, 1:4]				,		lowerCamelCase_				,		atol=1e-4      )				,		F'{output[:, 1:4, 1:4]}'      )








import unittest

import torch
from torch import nn

from diffusers.models.activations import get_activation





class  __magic_name__   (							unittest.TestCase						):






		def SCREAMING_SNAKE_CASE_			(						self      :     int      )				->					str:
								'''simple docstring'''







								UpperCAmelCase         =     get_activation("swish"      )

								self.assertIsInstance(UpperCamelCase__				,		nn.SiLU      )

								self.assertEqual(act(torch.tensor(-1_00				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertNotEqual(act(torch.tensor(-1				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(0				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(20				,		dtype=torch.floataa      )      ).item()				,		20      )






		def SCREAMING_SNAKE_CASE_			(						self      :     Tuple      )				->					List[Any]:
								'''simple docstring'''







								UpperCAmelCase         =     get_activation("silu"      )

								self.assertIsInstance(UpperCamelCase__				,		nn.SiLU      )

								self.assertEqual(act(torch.tensor(-1_00				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertNotEqual(act(torch.tensor(-1				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(0				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(20				,		dtype=torch.floataa      )      ).item()				,		20      )






		def SCREAMING_SNAKE_CASE_			(						self      :     Optional[Any]      )				->					int:
								'''simple docstring'''







								UpperCAmelCase         =     get_activation("mish"      )

								self.assertIsInstance(UpperCamelCase__				,		nn.Mish      )

								self.assertEqual(act(torch.tensor(-2_00				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertNotEqual(act(torch.tensor(-1				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(0				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(20				,		dtype=torch.floataa      )      ).item()				,		20      )







		def SCREAMING_SNAKE_CASE_			(						self      :     str      )				->					List[str]:
								'''simple docstring'''







								UpperCAmelCase         =     get_activation("gelu"      )

								self.assertIsInstance(UpperCamelCase__				,		nn.GELU      )

								self.assertEqual(act(torch.tensor(-1_00				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertNotEqual(act(torch.tensor(-1				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(0				,		dtype=torch.floataa      )      ).item()				,		0      )
								self.assertEqual(act(torch.tensor(20				,		dtype=torch.floataa      )      ).item()				,		20      )



