





'''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 							__UpperCAmelCase     (				__lowercase  ):







		'''simple docstring'''







		def __init__(self  :					Optional[int]					,					_lowerCAmelCase  :					int					,					_lowerCAmelCase  :					Tuple=13					,					_lowerCAmelCase  :					Tuple=7					,					_lowerCAmelCase  :					Tuple=True					,					_lowerCAmelCase  :					Union[str, Any]=True					,					_lowerCAmelCase  :					Any=True					,					_lowerCAmelCase  :					Union[str, Any]=True					,					_lowerCAmelCase  :					Union[str, Any]=99					,					_lowerCAmelCase  :					str=32					,					_lowerCAmelCase  :					int=5					,					_lowerCAmelCase  :					str=4					,					_lowerCAmelCase  :					str=37					,					_lowerCAmelCase  :					str="gelu"					,					_lowerCAmelCase  :					Optional[Any]=0.1					,					_lowerCAmelCase  :					int=0.1					,					_lowerCAmelCase  :					Dict=512					,					_lowerCAmelCase  :					Optional[Any]=16					,					_lowerCAmelCase  :					Tuple=2					,					_lowerCAmelCase  :					Tuple=0.02					,					_lowerCAmelCase  :					Any=False					,					_lowerCAmelCase  :					Optional[Any]=True					,					_lowerCAmelCase  :					Dict="None"					,					_lowerCAmelCase  :					Dict=3					,					_lowerCAmelCase  :					Union[str, Any]=4					,					_lowerCAmelCase  :					Optional[int]=None					,					):
			A							     =					parent
			A							     =					batch_size
			A							     =					seq_length
			A							     =					is_training
			A							     =					use_input_mask
			A							     =					use_token_type_ids
			A							     =					use_labels
			A							     =					vocab_size
			A							     =					hidden_size
			A							     =					num_hidden_layers
			A							     =					num_attention_heads
			A							     =					intermediate_size
			A							     =					hidden_act
			A							     =					hidden_dropout_prob
			A							     =					attention_probs_dropout_prob
			A							     =					max_position_embeddings
			A							     =					type_vocab_size
			A							     =					type_sequence_label_size
			A							     =					initializer_range
			A							     =					num_labels
			A							     =					num_choices
			A							     =					relative_attention
			A							     =					position_biased_input
			A							     =					pos_att_type
			A							     =					scope







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

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

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

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

			A							     =					self.get_config()

			return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels







		def 				A   (self  :					str					):
			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 				A   (self  :					str					,					_lowerCAmelCase  :					Any					):
			self.parent.assertListEqual(list(result.loss.size()					)					,					[]					)







		def 				A   (self  :					str					,					_lowerCAmelCase  :					List[Any]					,					_lowerCAmelCase  :					str					,					_lowerCAmelCase  :					Any					,					_lowerCAmelCase  :					str					,					_lowerCAmelCase  :					List[Any]					,					_lowerCAmelCase  :					List[Any]					,					_lowerCAmelCase  :					Dict					):
			A							     =					DebertaVaModel(config=_lowerCAmelCase					)
			model.to(_lowerCAmelCase					)
			model.eval()
			A							     =					model(_lowerCAmelCase					,					attention_mask=_lowerCAmelCase					,					token_type_ids=_lowerCAmelCase					)[0]
			A							     =					model(_lowerCAmelCase					,					token_type_ids=_lowerCAmelCase					)[0]
			A							     =					model(_lowerCAmelCase					)[0]

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







		def 				A   (self  :					Optional[Any]					,					_lowerCAmelCase  :					int					,					_lowerCAmelCase  :					List[Any]					,					_lowerCAmelCase  :					Optional[int]					,					_lowerCAmelCase  :					Dict					,					_lowerCAmelCase  :					Dict					,					_lowerCAmelCase  :					Dict					,					_lowerCAmelCase  :					str					):
			A							     =					DebertaVaForMaskedLM(config=_lowerCAmelCase					)
			model.to(_lowerCAmelCase					)
			model.eval()
			A							     =					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 				A   (self  :					Optional[Any]					,					_lowerCAmelCase  :					str					,					_lowerCAmelCase  :					Optional[int]					,					_lowerCAmelCase  :					Dict					,					_lowerCAmelCase  :					Tuple					,					_lowerCAmelCase  :					Union[str, Any]					,					_lowerCAmelCase  :					Any					,					_lowerCAmelCase  :					Any					):
			A							     =					self.num_labels
			A							     =					DebertaVaForSequenceClassification(_lowerCAmelCase					)
			model.to(_lowerCAmelCase					)
			model.eval()
			A							     =					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 				A   (self  :					Optional[Any]					,					_lowerCAmelCase  :					Union[str, Any]					,					_lowerCAmelCase  :					List[str]					,					_lowerCAmelCase  :					int					,					_lowerCAmelCase  :					Dict					,					_lowerCAmelCase  :					List[str]					,					_lowerCAmelCase  :					Tuple					,					_lowerCAmelCase  :					Optional[Any]					):
			A							     =					self.num_labels
			A							     =					DebertaVaForTokenClassification(config=_lowerCAmelCase					)
			model.to(_lowerCAmelCase					)
			model.eval()
			A							     =					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 				A   (self  :					str					,					_lowerCAmelCase  :					Dict					,					_lowerCAmelCase  :					Union[str, Any]					,					_lowerCAmelCase  :					str					,					_lowerCAmelCase  :					int					,					_lowerCAmelCase  :					Optional[Any]					,					_lowerCAmelCase  :					Optional[Any]					,					_lowerCAmelCase  :					List[Any]					):
			A							     =					DebertaVaForQuestionAnswering(config=_lowerCAmelCase					)
			model.to(_lowerCAmelCase					)
			model.eval()
			A							     =					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 				A   (self  :					Optional[Any]					,					_lowerCAmelCase  :					str					,					_lowerCAmelCase  :					Tuple					,					_lowerCAmelCase  :					Optional[Any]					,					_lowerCAmelCase  :					Optional[Any]					,					_lowerCAmelCase  :					Optional[int]					,					_lowerCAmelCase  :					List[Any]					,					_lowerCAmelCase  :					int					):
			A							     =					DebertaVaForMultipleChoice(config=_lowerCAmelCase					)
			model.to(_lowerCAmelCase					)
			model.eval()
			A							     =					input_ids.unsqueeze(1					).expand(-1					,					self.num_choices					,					-1					).contiguous()
			A							     =					token_type_ids.unsqueeze(1					).expand(-1					,					self.num_choices					,					-1					).contiguous()
			A							     =					input_mask.unsqueeze(1					).expand(-1					,					self.num_choices					,					-1					).contiguous()
			A							     =					model(
			    _lowerCAmelCase					,					attention_mask=_lowerCAmelCase					,					token_type_ids=_lowerCAmelCase					,					labels=_lowerCAmelCase					,					)
			self.parent.assertEqual(result.logits.shape					,					(self.batch_size, self.num_choices)					)







		def 				A   (self  :					List[str]					):
			A							     =					self.prepare_config_and_inputs()
			(
			    A						
			)							     =					config_and_inputs
			A							     =					{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
			return config, inputs_dict






@require_torch
class 							__UpperCAmelCase     (				__lowercase						,    __lowercase						,    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 				A   (self  :					int					):
			A							     =					DebertaVaModelTester(self					)
			A							     =					ConfigTester(self					,					config_class=_lowerCAmelCase					,					hidden_size=37					)







		def 				A   (self  :					Tuple					):
			self.config_tester.run_common_tests()







		def 				A   (self  :					Any					):
			A							     =					self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_deberta_model(*_lowerCAmelCase					)







		def 				A   (self  :					Any					):
			A							     =					self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_deberta_for_sequence_classification(*_lowerCAmelCase					)







		def 				A   (self  :					List[str]					):
			A							     =					self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_deberta_for_masked_lm(*_lowerCAmelCase					)







		def 				A   (self  :					List[str]					):
			A							     =					self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_deberta_for_question_answering(*_lowerCAmelCase					)







		def 				A   (self  :					List[str]					):
			A							     =					self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_deberta_for_token_classification(*_lowerCAmelCase					)







		def 				A   (self  :					int					):
			A							     =					self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_deberta_for_multiple_choice(*_lowerCAmelCase					)







		@slow
		def 				A   (self  :					Optional[Any]					):
			for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
				A							     =					DebertaVaModel.from_pretrained(_lowerCAmelCase					)
				self.assertIsNotNone(_lowerCAmelCase					)






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







		'''simple docstring'''







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







		@slow
		def 				A   (self  :					Optional[int]					):
			A							     =					DebertaVaModel.from_pretrained("""microsoft/deberta-v2-xlarge"""					)

			A							     =					torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]					)
			A							     =					torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]					)
			with torch.no_grad():
				A							     =					model(_lowerCAmelCase					,					attention_mask=_lowerCAmelCase					)[0]
			# compare the actual values for a slice.
			A							     =					torch.tensor(
			    [[[0.2_356, 0.1_948, 0.0_369], [-0.1_063, 0.3_586, -0.5_152], [-0.6_399, -0.0_259, -0.2_525]]]					)
			self.assertTrue(torch.allclose(output[:, 1:4, 1:4]					,					_lowerCAmelCase					,					atol=1e-4					)					,					F"""{output[:, 1:4, 1:4]}"""					)



from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowercase  :					List[Any]													=       logging.get_logger(__name__)

lowercase  :					Any													=       {
    'google/pegasus-large': 'https://huggingface.co/google/pegasus-large/resolve/main/config.json',
    # See all PEGASUS models at https://huggingface.co/models?filter=pegasus
}




class  lowerCamelCase__							(				__lowercase):




	'''simple docstring'''




	_A								=     'pegasus'
	_A								=     ['past_key_values']
	_A								=     {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}







	def __init__(					self		:Dict     ,   a		:Dict=5_0_2_6_5     ,   a		:Dict=1_0_2_4     ,   a		:Union[str, Any]=1_2     ,   a		:Any=4_0_9_6     ,   a		:str=1_6     ,   a		:str=1_2     ,   a		:Optional[Any]=4_0_9_6     ,   a		:int=1_6     ,   a		:Optional[int]=0.0     ,   a		:Optional[int]=0.0     ,   a		:List[Any]=True     ,   a		:Union[str, Any]=True     ,   a		:int="gelu"     ,   a		:Dict=1_0_2_4     ,   a		:List[Any]=0.1     ,   a		:List[str]=0.0     ,   a		:List[Any]=0.0     ,   a		:str=0.02     ,   a		:int=0     ,   a		:Any=False     ,   a		:Dict=0     ,   a		:int=1     ,   a		:Optional[Any]=1     ,   **a		:Optional[int]     ,   )		-> str:
								__UpperCamelCase	:   List[Any]			   =					vocab_size
								__UpperCamelCase	:   Union[str, Any]			   =					max_position_embeddings
								__UpperCamelCase	:   str			   =					d_model
								__UpperCamelCase	:   Dict			   =					encoder_ffn_dim
								__UpperCamelCase	:   int			   =					encoder_layers
								__UpperCamelCase	:   int			   =					encoder_attention_heads
								__UpperCamelCase	:   List[Any]			   =					decoder_ffn_dim
								__UpperCamelCase	:   List[Any]			   =					decoder_layers
								__UpperCamelCase	:   List[str]			   =					decoder_attention_heads
								__UpperCamelCase	:   str			   =					dropout
								__UpperCamelCase	:   Union[str, Any]			   =					attention_dropout
								__UpperCamelCase	:   List[str]			   =					activation_dropout
								__UpperCamelCase	:   Optional[Any]			   =					activation_function
								__UpperCamelCase	:   Tuple			   =					init_std
								__UpperCamelCase	:   Optional[int]			   =					encoder_layerdrop
								__UpperCamelCase	:   Union[str, Any]			   =					decoder_layerdrop
								__UpperCamelCase	:   Optional[Any]			   =					use_cache
								__UpperCamelCase	:   Union[str, Any]			   =					encoder_layers
								__UpperCamelCase	:   int			   =					scale_embedding  # scale factor will be sqrt(d_model) if True
								super().__init__(
								    pad_token_id=a     ,   eos_token_id=a     ,   is_encoder_decoder=a     ,   decoder_start_token_id=a     ,   forced_eos_token_id=a     ,   **a     ,   )







	@property
	def    _lowerCamelCase				(					self		:Dict						)		-> int:
								return self.encoder_attention_heads







	@property
	def    _lowerCamelCase				(					self		:Optional[Any]						)		-> int:
								return self.d_model