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



	"""simple docstring"""




	def __init__(			self    :     Any			,				SCREAMING_SNAKE_CASE_    :     str			,				SCREAMING_SNAKE_CASE_    :     str=1_3			,				SCREAMING_SNAKE_CASE_    :     Tuple=7			,				SCREAMING_SNAKE_CASE_    :     Optional[Any]=True			,				SCREAMING_SNAKE_CASE_    :     int=True			,				SCREAMING_SNAKE_CASE_    :     str=True			,				SCREAMING_SNAKE_CASE_    :     Dict=True			,				SCREAMING_SNAKE_CASE_    :     Optional[Any]=9_9			,				SCREAMING_SNAKE_CASE_    :     List[str]=3_2			,				SCREAMING_SNAKE_CASE_    :     Any=5			,				SCREAMING_SNAKE_CASE_    :     List[str]=4			,				SCREAMING_SNAKE_CASE_    :     Union[str, Any]=3_7			,				SCREAMING_SNAKE_CASE_    :     int="gelu"			,				SCREAMING_SNAKE_CASE_    :     Any=0.1			,				SCREAMING_SNAKE_CASE_    :     Any=0.1			,				SCREAMING_SNAKE_CASE_    :     str=5_1_2			,				SCREAMING_SNAKE_CASE_    :     str=1_6			,				SCREAMING_SNAKE_CASE_    :     List[Any]=2			,				SCREAMING_SNAKE_CASE_    :     Tuple=0.02			,				SCREAMING_SNAKE_CASE_    :     Union[str, Any]=False			,				SCREAMING_SNAKE_CASE_    :     Tuple=True			,				SCREAMING_SNAKE_CASE_    :     Optional[Any]="None"			,				SCREAMING_SNAKE_CASE_    :     str=3			,				SCREAMING_SNAKE_CASE_    :     Dict=4			,				SCREAMING_SNAKE_CASE_    :     Any=None			,				)			->	List[Any]:
					lowercase_	    =   parent
					lowercase_	    =   batch_size
					lowercase_	    =   seq_length
					lowercase_	    =   is_training
					lowercase_	    =   use_input_mask
					lowercase_	    =   use_token_type_ids
					lowercase_	    =   use_labels
					lowercase_	    =   vocab_size
					lowercase_	    =   hidden_size
					lowercase_	    =   num_hidden_layers
					lowercase_	    =   num_attention_heads
					lowercase_	    =   intermediate_size
					lowercase_	    =   hidden_act
					lowercase_	    =   hidden_dropout_prob
					lowercase_	    =   attention_probs_dropout_prob
					lowercase_	    =   max_position_embeddings
					lowercase_	    =   type_vocab_size
					lowercase_	    =   type_sequence_label_size
					lowercase_	    =   initializer_range
					lowercase_	    =   num_labels
					lowercase_	    =   num_choices
					lowercase_	    =   relative_attention
					lowercase_	    =   position_biased_input
					lowercase_	    =   pos_att_type
					lowercase_	    =   scope
	def 		_lowercase    (			self    :     Dict			)			->	List[Any]:
					lowercase_	    =   ids_tensor([self.batch_size, self.seq_length]			,				self.vocab_size			)

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

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

					lowercase_	    =   None
					lowercase_	    =   None
					lowercase_	    =   None
					if self.use_labels:
									lowercase_	    =   ids_tensor([self.batch_size]			,				self.type_sequence_label_size			)
									lowercase_	    =   ids_tensor([self.batch_size, self.seq_length]			,				self.num_labels			)
									lowercase_	    =   ids_tensor([self.batch_size]			,				self.num_choices			)

					lowercase_	    =   self.get_config()

					return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
	def 		_lowercase    (			self    :     List[str]			)			->	Tuple:
					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 		_lowercase    (			self    :     int			,				SCREAMING_SNAKE_CASE_    :     Any			)			->	Tuple:
					self.parent.assertListEqual(list(result.loss.size()			)			,				[]			)
	def 		_lowercase    (			self    :     Optional[int]			,				SCREAMING_SNAKE_CASE_    :     List[str]			,				SCREAMING_SNAKE_CASE_    :     Optional[int]			,				SCREAMING_SNAKE_CASE_    :     Optional[int]			,				SCREAMING_SNAKE_CASE_    :     Union[str, Any]			,				SCREAMING_SNAKE_CASE_    :     Dict			,				SCREAMING_SNAKE_CASE_    :     Optional[Any]			,				SCREAMING_SNAKE_CASE_    :     Optional[Any]			)			->	Optional[int]:
					lowercase_	    =   DebertaVaModel(config=SCREAMING_SNAKE_CASE_			)
					model.to(SCREAMING_SNAKE_CASE_			)
					model.eval()
					lowercase_	    =   model(SCREAMING_SNAKE_CASE_			,				attention_mask=SCREAMING_SNAKE_CASE_			,				token_type_ids=SCREAMING_SNAKE_CASE_			)[0]
					lowercase_	    =   model(SCREAMING_SNAKE_CASE_			,				token_type_ids=SCREAMING_SNAKE_CASE_			)[0]
					lowercase_	    =   model(SCREAMING_SNAKE_CASE_			)[0]

					self.parent.assertListEqual(list(sequence_output.size()			)			,				[self.batch_size, self.seq_length, self.hidden_size]			)
	def 		_lowercase    (			self    :     List[str]			,				SCREAMING_SNAKE_CASE_    :     Any			,				SCREAMING_SNAKE_CASE_    :     Any			,				SCREAMING_SNAKE_CASE_    :     Optional[Any]			,				SCREAMING_SNAKE_CASE_    :     Any			,				SCREAMING_SNAKE_CASE_    :     int			,				SCREAMING_SNAKE_CASE_    :     str			,				SCREAMING_SNAKE_CASE_    :     List[Any]			)			->	Optional[Any]:
					lowercase_	    =   DebertaVaForMaskedLM(config=SCREAMING_SNAKE_CASE_			)
					model.to(SCREAMING_SNAKE_CASE_			)
					model.eval()
					lowercase_	    =   model(SCREAMING_SNAKE_CASE_			,				attention_mask=SCREAMING_SNAKE_CASE_			,				token_type_ids=SCREAMING_SNAKE_CASE_			,				labels=SCREAMING_SNAKE_CASE_			)

					self.parent.assertEqual(result.logits.shape			,				(self.batch_size, self.seq_length, self.vocab_size)			)
	def 		_lowercase    (			self    :     List[Any]			,				SCREAMING_SNAKE_CASE_    :     Optional[int]			,				SCREAMING_SNAKE_CASE_    :     Optional[int]			,				SCREAMING_SNAKE_CASE_    :     Dict			,				SCREAMING_SNAKE_CASE_    :     Tuple			,				SCREAMING_SNAKE_CASE_    :     Union[str, Any]			,				SCREAMING_SNAKE_CASE_    :     Dict			,				SCREAMING_SNAKE_CASE_    :     List[str]			)			->	Tuple:
					lowercase_	    =   self.num_labels
					lowercase_	    =   DebertaVaForSequenceClassification(SCREAMING_SNAKE_CASE_			)
					model.to(SCREAMING_SNAKE_CASE_			)
					model.eval()
					lowercase_	    =   model(SCREAMING_SNAKE_CASE_			,				attention_mask=SCREAMING_SNAKE_CASE_			,				token_type_ids=SCREAMING_SNAKE_CASE_			,				labels=SCREAMING_SNAKE_CASE_			)
					self.parent.assertListEqual(list(result.logits.size()			)			,				[self.batch_size, self.num_labels]			)
					self.check_loss_output(SCREAMING_SNAKE_CASE_			)
	def 		_lowercase    (			self    :     Any			,				SCREAMING_SNAKE_CASE_    :     Optional[Any]			,				SCREAMING_SNAKE_CASE_    :     Optional[Any]			,				SCREAMING_SNAKE_CASE_    :     Tuple			,				SCREAMING_SNAKE_CASE_    :     Dict			,				SCREAMING_SNAKE_CASE_    :     Tuple			,				SCREAMING_SNAKE_CASE_    :     Union[str, Any]			,				SCREAMING_SNAKE_CASE_    :     List[Any]			)			->	Dict:
					lowercase_	    =   self.num_labels
					lowercase_	    =   DebertaVaForTokenClassification(config=SCREAMING_SNAKE_CASE_			)
					model.to(SCREAMING_SNAKE_CASE_			)
					model.eval()
					lowercase_	    =   model(SCREAMING_SNAKE_CASE_			,				attention_mask=SCREAMING_SNAKE_CASE_			,				token_type_ids=SCREAMING_SNAKE_CASE_			,				labels=SCREAMING_SNAKE_CASE_			)
					self.parent.assertEqual(result.logits.shape			,				(self.batch_size, self.seq_length, self.num_labels)			)
	def 		_lowercase    (			self    :     Tuple			,				SCREAMING_SNAKE_CASE_    :     str			,				SCREAMING_SNAKE_CASE_    :     Union[str, Any]			,				SCREAMING_SNAKE_CASE_    :     int			,				SCREAMING_SNAKE_CASE_    :     int			,				SCREAMING_SNAKE_CASE_    :     Union[str, Any]			,				SCREAMING_SNAKE_CASE_    :     List[Any]			,				SCREAMING_SNAKE_CASE_    :     Any			)			->	str:
					lowercase_	    =   DebertaVaForQuestionAnswering(config=SCREAMING_SNAKE_CASE_			)
					model.to(SCREAMING_SNAKE_CASE_			)
					model.eval()
					lowercase_	    =   model(
					    SCREAMING_SNAKE_CASE_			,				attention_mask=SCREAMING_SNAKE_CASE_			,				token_type_ids=SCREAMING_SNAKE_CASE_			,				start_positions=SCREAMING_SNAKE_CASE_			,				end_positions=SCREAMING_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 		_lowercase    (			self    :     Tuple			,				SCREAMING_SNAKE_CASE_    :     str			,				SCREAMING_SNAKE_CASE_    :     Tuple			,				SCREAMING_SNAKE_CASE_    :     str			,				SCREAMING_SNAKE_CASE_    :     int			,				SCREAMING_SNAKE_CASE_    :     List[Any]			,				SCREAMING_SNAKE_CASE_    :     str			,				SCREAMING_SNAKE_CASE_    :     Dict			)			->	Union[str, Any]:
					lowercase_	    =   DebertaVaForMultipleChoice(config=SCREAMING_SNAKE_CASE_			)
					model.to(SCREAMING_SNAKE_CASE_			)
					model.eval()
					lowercase_	    =   input_ids.unsqueeze(1			).expand(-1			,				self.num_choices			,				-1			).contiguous()
					lowercase_	    =   token_type_ids.unsqueeze(1			).expand(-1			,				self.num_choices			,				-1			).contiguous()
					lowercase_	    =   input_mask.unsqueeze(1			).expand(-1			,				self.num_choices			,				-1			).contiguous()
					lowercase_	    =   model(
					    SCREAMING_SNAKE_CASE_			,				attention_mask=SCREAMING_SNAKE_CASE_			,				token_type_ids=SCREAMING_SNAKE_CASE_			,				labels=SCREAMING_SNAKE_CASE_			,				)
					self.parent.assertEqual(result.logits.shape			,				(self.batch_size, self.num_choices)			)




	def 		_lowercase    (			self    :     Tuple			)			->	Dict:
					lowercase_	    =   self.prepare_config_and_inputs()
					(
					    (
					    lowercase_
					)    ,      (
					    lowercase_
					)    ,      (
					    lowercase_
					)    ,      (
					    lowercase_
					)    ,      (
					    lowercase_
					)    ,      (
					    lowercase_
					)    ,      (
					    lowercase_
					)    ,      
					)	    =   config_and_inputs
					lowercase_	    =   {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
					return config, inputs_dict

@require_torch
class 					lowercase__(							UpperCAmelCase					,		UpperCAmelCase					,		unittest.TestCase				):



	"""simple docstring"""




	a     :Optional[int]       	=      (
	    (
	        DebertaVaModel,
	        DebertaVaForMaskedLM,
	        DebertaVaForSequenceClassification,
	        DebertaVaForTokenClassification,
	        DebertaVaForQuestionAnswering,
	        DebertaVaForMultipleChoice,
	    )
	    if is_torch_available()
	    else ()
	)
	a     :List[Any]       	=      (
	    {
	        'feature-extraction': DebertaVaModel,
	        'fill-mask': DebertaVaForMaskedLM,
	        'question-answering': DebertaVaForQuestionAnswering,
	        'text-classification': DebertaVaForSequenceClassification,
	        'token-classification': DebertaVaForTokenClassification,
	        'zero-shot': DebertaVaForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)

	a     :Optional[Any]       	=      True
	a     :Tuple       	=      False
	a     :Optional[int]       	=      False
	a     :Optional[Any]       	=      False
	a     :Union[str, Any]       	=      False
	def 		_lowercase    (			self    :     List[str]			)			->	Dict:
					lowercase_	    =   DebertaVaModelTester(self			)
					lowercase_	    =   ConfigTester(self			,				config_class=SCREAMING_SNAKE_CASE_			,				hidden_size=3_7			)
	def 		_lowercase    (			self    :     Union[str, Any]			)			->	Optional[int]:
					self.config_tester.run_common_tests()
	def 		_lowercase    (			self    :     Any			)			->	Union[str, Any]:
					lowercase_	    =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_deberta_model(*SCREAMING_SNAKE_CASE_			)
	def 		_lowercase    (			self    :     Dict			)			->	str:
					lowercase_	    =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_deberta_for_sequence_classification(*SCREAMING_SNAKE_CASE_			)
	def 		_lowercase    (			self    :     Any			)			->	Optional[Any]:
					lowercase_	    =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_deberta_for_masked_lm(*SCREAMING_SNAKE_CASE_			)
	def 		_lowercase    (			self    :     Optional[int]			)			->	Any:
					lowercase_	    =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_deberta_for_question_answering(*SCREAMING_SNAKE_CASE_			)
	def 		_lowercase    (			self    :     List[Any]			)			->	List[Any]:
					lowercase_	    =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_deberta_for_token_classification(*SCREAMING_SNAKE_CASE_			)
	def 		_lowercase    (			self    :     Any			)			->	Optional[int]:
					lowercase_	    =   self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_deberta_for_multiple_choice(*SCREAMING_SNAKE_CASE_			)




	@slow
	def 		_lowercase    (			self    :     List[str]			)			->	str:
					for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowercase_	    =   DebertaVaModel.from_pretrained(SCREAMING_SNAKE_CASE_			)
									self.assertIsNotNone(SCREAMING_SNAKE_CASE_			)

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



	"""simple docstring"""




	@unittest.skip(reason='''Model not available yet'''			)
	def 		_lowercase    (			self    :     Optional[Any]			)			->	Optional[Any]:
					pass




	@slow
	def 		_lowercase    (			self    :     str			)			->	List[str]:
					lowercase_	    =   DebertaVaModel.from_pretrained('''microsoft/deberta-v2-xlarge'''			)

					lowercase_	    =   torch.tensor([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]]			)
					lowercase_	    =   torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]			)
					with torch.no_grad():
									lowercase_	    =   model(SCREAMING_SNAKE_CASE_			,				attention_mask=SCREAMING_SNAKE_CASE_			)[0]
					# compare the actual values for a slice.
					lowercase_	    =   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]			,				SCREAMING_SNAKE_CASE_			,				atol=1e-4			)			,				f'''{output[:, 1:4, 1:4]}'''			)






__a         =   'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'




def 							a		(				snake_case__:   bytes   ):


				'''simple docstring'''
				# Make sure the supplied data is a bytes-like object
				if not isinstance(snake_case__		,  snake_case__   ):
								lowercase_	    =   F'''a bytes-like object is required, not \'{data.__class__.__name__}\''''
								raise TypeError(snake_case__   )

				lowercase_	    =   ''''''.join(bin(snake_case__   )[2:].zfill(8   ) for byte in data   )

				lowercase_	    =   len(snake_case__   ) % 6 != 0

				if padding_needed:
								# The padding that will be added later
								lowercase_	    =   B'''=''' * ((6 - len(snake_case__   ) % 6) // 2)

								# Append binary_stream with arbitrary binary digits (0's by default) to make its
								# length a multiple of 6.
								binary_stream += "0" * (6 - len(snake_case__   ) % 6)
				else:
								lowercase_	    =   B''''''

				# Encode every 6 binary digits to their corresponding Base64 character
				return (
				    "".join(
				        B64_CHARSET[int(binary_stream[index : index + 6]		,  2   )]
				        for index in range(0		,  len(snake_case__   )		,  6   )   ).encode()
				    + padding
				)




def 							a		(				snake_case__:   str   ):


				'''simple docstring'''
				# Make sure encoded_data is either a string or a bytes-like object
				if not isinstance(snake_case__		,  snake_case__   ) and not isinstance(snake_case__		,  snake_case__   ):
								lowercase_	    =   (
								    '''argument should be a bytes-like object or ASCII string, '''
								    F'''not \'{encoded_data.__class__.__name__}\''''
								)
								raise TypeError(snake_case__   )

				# In case encoded_data is a bytes-like object, make sure it contains only
				# ASCII characters so we convert it to a string object
				if isinstance(snake_case__		,  snake_case__   ):
								try:
												lowercase_	    =   encoded_data.decode('''utf-8'''   )
								except UnicodeDecodeError:
												raise ValueError('''base64 encoded data should only contain ASCII characters'''   )

				lowercase_	    =   encoded_data.count('''='''   )

				# Check if the encoded string contains non base64 characters
				if padding:
								assert all(
								    char in B64_CHARSET for char in encoded_data[:-padding]   ), "Invalid base64 character(s) found."
				else:
								assert all(
								    char in B64_CHARSET for char in encoded_data   ), "Invalid base64 character(s) found."

				# Check the padding
				assert len(snake_case__   ) % 4 == 0 and padding < 3, "Incorrect padding"

				if padding:
								# Remove padding if there is one
								lowercase_	    =   encoded_data[:-padding]

								lowercase_	    =   ''''''.join(
								    bin(B64_CHARSET.index(snake_case__   )   )[2:].zfill(6   ) for char in encoded_data   )[: -padding * 2]
				else:
								lowercase_	    =   ''''''.join(
								    bin(B64_CHARSET.index(snake_case__   )   )[2:].zfill(6   ) for char in encoded_data   )

				lowercase_	    =   [
				    int(binary_stream[index : index + 8]		,  2   )
				    for index in range(0		,  len(snake_case__   )		,  8   )
				]

				return bytes(snake_case__   )


if __name__ == "__main__":
						import doctest

						doctest.testmod()




