
"""simple docstring"""



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        _SCREAMING_SNAKE_CASE  (  UpperCAmelCase							):






				'''simple docstring'''



				def __init__(					self	:			Tuple				,     UpperCAmelCase_	:			int				,     UpperCAmelCase_	:			Optional[Any]=13				,     UpperCAmelCase_	:			List[Any]=7				,     UpperCAmelCase_	:			Dict=True				,     UpperCAmelCase_	:			Optional[int]=True				,     UpperCAmelCase_	:			Optional[int]=True				,     UpperCAmelCase_	:			Union[str, Any]=True				,     UpperCAmelCase_	:			Any=99				,     UpperCAmelCase_	:			str=32				,     UpperCAmelCase_	:			Any=5				,     UpperCAmelCase_	:			Dict=4				,     UpperCAmelCase_	:			Tuple=37				,     UpperCAmelCase_	:			List[str]="gelu"				,     UpperCAmelCase_	:			Union[str, Any]=0.1				,     UpperCAmelCase_	:			List[str]=0.1				,     UpperCAmelCase_	:			Tuple=512				,     UpperCAmelCase_	:			str=16				,     UpperCAmelCase_	:			Tuple=2				,     UpperCAmelCase_	:			Optional[Any]=0.02				,     UpperCAmelCase_	:			Tuple=False				,     UpperCAmelCase_	:			Optional[Any]=True				,     UpperCAmelCase_	:			Tuple="None"				,     UpperCAmelCase_	:			Optional[Any]=3				,     UpperCAmelCase_	:			List[Any]=4				,     UpperCAmelCase_	:			List[Any]=None				,     )		->  Tuple:







											"""simple docstring"""




											_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 				__lowerCamelCase	(					self	:			Optional[int]  )		->  Optional[int]:







											"""simple docstring"""




											_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 				__lowerCamelCase	(					self	:			Tuple  )		->  Dict:







											"""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 				__lowerCamelCase	(					self	:			Dict				,     UpperCAmelCase_	:			List[Any]  )		->  Optional[int]:







											"""simple docstring"""




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


				def 				__lowerCamelCase	(					self	:			int				,     UpperCAmelCase_	:			List[str]				,     UpperCAmelCase_	:			Optional[Any]				,     UpperCAmelCase_	:			Optional[int]				,     UpperCAmelCase_	:			Tuple				,     UpperCAmelCase_	:			Union[str, Any]				,     UpperCAmelCase_	:			Union[str, Any]				,     UpperCAmelCase_	:			List[Any]  )		->  List[Any]:







											"""simple docstring"""




											_lowerCAmelCase  	=			DebertaVaModel(config=UpperCAmelCase_  )
											model.to(UpperCAmelCase_  )
											model.eval()
											_lowerCAmelCase  	=			model(UpperCAmelCase_				,     attention_mask=UpperCAmelCase_				,     token_type_ids=UpperCAmelCase_  )[0]
											_lowerCAmelCase  	=			model(UpperCAmelCase_				,     token_type_ids=UpperCAmelCase_  )[0]
											_lowerCAmelCase  	=			model(UpperCAmelCase_  )[0]

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


				def 				__lowerCamelCase	(					self	:			List[Any]				,     UpperCAmelCase_	:			Optional[int]				,     UpperCAmelCase_	:			Union[str, Any]				,     UpperCAmelCase_	:			Tuple				,     UpperCAmelCase_	:			List[Any]				,     UpperCAmelCase_	:			Tuple				,     UpperCAmelCase_	:			Optional[Any]				,     UpperCAmelCase_	:			Optional[Any]  )		->  int:







											"""simple docstring"""




											_lowerCAmelCase  	=			DebertaVaForMaskedLM(config=UpperCAmelCase_  )
											model.to(UpperCAmelCase_  )
											model.eval()
											_lowerCAmelCase  	=			model(UpperCAmelCase_				,     attention_mask=UpperCAmelCase_				,     token_type_ids=UpperCAmelCase_				,     labels=UpperCAmelCase_  )

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


				def 				__lowerCamelCase	(					self	:			Optional[Any]				,     UpperCAmelCase_	:			Dict				,     UpperCAmelCase_	:			str				,     UpperCAmelCase_	:			List[Any]				,     UpperCAmelCase_	:			Any				,     UpperCAmelCase_	:			Tuple				,     UpperCAmelCase_	:			Tuple				,     UpperCAmelCase_	:			List[str]  )		->  Any:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.num_labels
											_lowerCAmelCase  	=			DebertaVaForSequenceClassification(UpperCAmelCase_  )
											model.to(UpperCAmelCase_  )
											model.eval()
											_lowerCAmelCase  	=			model(UpperCAmelCase_				,     attention_mask=UpperCAmelCase_				,     token_type_ids=UpperCAmelCase_				,     labels=UpperCAmelCase_  )
											self.parent.assertListEqual(list(result.logits.size()  )				,     [self.batch_size, self.num_labels]  )
											self.check_loss_output(UpperCAmelCase_  )


				def 				__lowerCamelCase	(					self	:			Any				,     UpperCAmelCase_	:			Optional[int]				,     UpperCAmelCase_	:			Dict				,     UpperCAmelCase_	:			Dict				,     UpperCAmelCase_	:			Tuple				,     UpperCAmelCase_	:			List[str]				,     UpperCAmelCase_	:			Optional[int]				,     UpperCAmelCase_	:			List[Any]  )		->  List[Any]:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.num_labels
											_lowerCAmelCase  	=			DebertaVaForTokenClassification(config=UpperCAmelCase_  )
											model.to(UpperCAmelCase_  )
											model.eval()
											_lowerCAmelCase  	=			model(UpperCAmelCase_				,     attention_mask=UpperCAmelCase_				,     token_type_ids=UpperCAmelCase_				,     labels=UpperCAmelCase_  )
											self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.num_labels)  )


				def 				__lowerCamelCase	(					self	:			str				,     UpperCAmelCase_	:			Dict				,     UpperCAmelCase_	:			Optional[Any]				,     UpperCAmelCase_	:			str				,     UpperCAmelCase_	:			Union[str, Any]				,     UpperCAmelCase_	:			str				,     UpperCAmelCase_	:			Any				,     UpperCAmelCase_	:			int  )		->  List[Any]:







											"""simple docstring"""




											_lowerCAmelCase  	=			DebertaVaForQuestionAnswering(config=UpperCAmelCase_  )
											model.to(UpperCAmelCase_  )
											model.eval()
											_lowerCAmelCase  	=			model(
											    UpperCAmelCase_				,     attention_mask=UpperCAmelCase_				,     token_type_ids=UpperCAmelCase_				,     start_positions=UpperCAmelCase_				,     end_positions=UpperCAmelCase_				,     )
											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 				__lowerCamelCase	(					self	:			Dict				,     UpperCAmelCase_	:			str				,     UpperCAmelCase_	:			List[Any]				,     UpperCAmelCase_	:			Any				,     UpperCAmelCase_	:			Optional[Any]				,     UpperCAmelCase_	:			Tuple				,     UpperCAmelCase_	:			Optional[Any]				,     UpperCAmelCase_	:			int  )		->  Any:







											"""simple docstring"""




											_lowerCAmelCase  	=			DebertaVaForMultipleChoice(config=UpperCAmelCase_  )
											model.to(UpperCAmelCase_  )
											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(
											    UpperCAmelCase_				,     attention_mask=UpperCAmelCase_				,     token_type_ids=UpperCAmelCase_				,     labels=UpperCAmelCase_				,     )
											self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_choices)  )







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







											"""simple docstring"""




											_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        _SCREAMING_SNAKE_CASE  (  UpperCAmelCase      ,				UpperCAmelCase      ,				unittest.TestCase							):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE_:							Optional[Any]							=					(
				    (
				        DebertaVaModel,
				        DebertaVaForMaskedLM,
				        DebertaVaForSequenceClassification,
				        DebertaVaForTokenClassification,
				        DebertaVaForQuestionAnswering,
				        DebertaVaForMultipleChoice,
				    )
				    if is_torch_available()
				    else ()
				)
				SCREAMING_SNAKE_CASE_:							Dict							=					(
				    {
				        "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_:							List[str]							=					True
				SCREAMING_SNAKE_CASE_:							str							=					False
				SCREAMING_SNAKE_CASE_:							int							=					False
				SCREAMING_SNAKE_CASE_:							Tuple							=					False
				SCREAMING_SNAKE_CASE_:							Union[str, Any]							=					False


				def 				__lowerCamelCase	(					self	:			Dict  )		->  int:







											"""simple docstring"""




											_lowerCAmelCase  	=			DebertaVaModelTester(self  )
											_lowerCAmelCase  	=			ConfigTester(self				,     config_class=UpperCAmelCase_				,     hidden_size=37  )


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







											"""simple docstring"""




											self.config_tester.run_common_tests()


				def 				__lowerCamelCase	(					self	:			Any  )		->  str:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_deberta_model(*UpperCAmelCase_  )


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







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_deberta_for_sequence_classification(*UpperCAmelCase_  )


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







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_deberta_for_masked_lm(*UpperCAmelCase_  )


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







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_deberta_for_question_answering(*UpperCAmelCase_  )


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







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_deberta_for_token_classification(*UpperCAmelCase_  )


				def 				__lowerCamelCase	(					self	:			Optional[Any]  )		->  Any:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_deberta_for_multiple_choice(*UpperCAmelCase_  )







				@slow
				def 				__lowerCamelCase	(					self	:			Union[str, Any]  )		->  Tuple:







											"""simple docstring"""




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






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






				'''simple docstring'''



				@unittest.skip(reason='Model not available yet'  )
				def 				__lowerCamelCase	(					self	:			Union[str, Any]  )		->  Dict:







											"""simple docstring"""




											pass







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







											"""simple docstring"""




											_lowerCAmelCase  	=			DebertaVaModel.from_pretrained('microsoft/deberta-v2-xlarge'  )

											_lowerCAmelCase  	=			torch.tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]  )
											_lowerCAmelCase  	=			torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]  )
											with torch.no_grad():
																		_lowerCAmelCase  	=			model(UpperCAmelCase_				,     attention_mask=UpperCAmelCase_  )[0]
											# compare the actual values for a slice.
											_lowerCAmelCase  	=			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]				,     UpperCAmelCase_				,     atol=1E-4  )				,     F"""{output[:, 1:4, 1:4]}"""  )








"""simple docstring"""



def 	__snake_case  (    SCREAMING_SNAKE_CASE:					str						):






							"""simple docstring"""



							_lowerCAmelCase  	=			0

							# if input_string is "aba" than new_input_string become "a|b|a"
							_lowerCAmelCase  	=			''
							_lowerCAmelCase  	=			''

							# append each character + "|" in new_string for range(0, length-1)
							for i in input_string[: len(SCREAMING_SNAKE_CASE						) - 1]:
														new_input_string += i + "|"
							# append last character
							new_input_string += input_string[-1]

							# we will store the starting and ending of previous furthest ending palindromic
							# substring
							_lowerCAmelCase	,		_lowerCAmelCase  	=			0, 0

							# length[i] shows the length of palindromic substring with center i
							_lowerCAmelCase  	=			[1 for i in range(len(SCREAMING_SNAKE_CASE						)						)]

							# for each character in new_string find corresponding palindromic string
							_lowerCAmelCase  	=			0
							for j in range(len(SCREAMING_SNAKE_CASE						)						):
														_lowerCAmelCase  	=			1 if j > r else min(length[l + r - j] // 2     ,  r - j + 1						)
														while (
														    j - k >= 0
														    and j + k < len(SCREAMING_SNAKE_CASE						)
														    and new_input_string[k + j] == new_input_string[j - k]
														):
																					k += 1

														_lowerCAmelCase  	=			2 * k - 1

														# does this string is ending after the previously explored end (that is r) ?
														# if yes the update the new r to the last index of this
														if j + k - 1 > r:
																					_lowerCAmelCase  	=			j - k + 1  # noqa: E741
																					_lowerCAmelCase  	=			j + k - 1

														# update max_length and start position
														if max_length < length[j]:
																					_lowerCAmelCase  	=			length[j]
																					_lowerCAmelCase  	=			j

    # create that string
							_lowerCAmelCase  	=			new_input_string[start - max_length // 2 : start + max_length // 2 + 1]
							for i in s:
														if i != "|":
																					output_string += i

							return output_string


if __name__ == "__main__":
	import doctest

	doctest.testmod()






