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_):


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





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							parent
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							batch_size
												SCREAMING_SNAKE_CASE							:					List[str]           =							seq_length
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							is_training
												SCREAMING_SNAKE_CASE							:					Dict           =							use_input_mask
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							use_token_type_ids
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							use_labels
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							vocab_size
												SCREAMING_SNAKE_CASE							:					str           =							hidden_size
												SCREAMING_SNAKE_CASE							:					List[Any]           =							num_hidden_layers
												SCREAMING_SNAKE_CASE							:					int           =							num_attention_heads
												SCREAMING_SNAKE_CASE							:					Tuple           =							intermediate_size
												SCREAMING_SNAKE_CASE							:					str           =							hidden_act
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							hidden_dropout_prob
												SCREAMING_SNAKE_CASE							:					List[Any]           =							attention_probs_dropout_prob
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							max_position_embeddings
												SCREAMING_SNAKE_CASE							:					str           =							type_vocab_size
												SCREAMING_SNAKE_CASE							:					List[str]           =							type_sequence_label_size
												SCREAMING_SNAKE_CASE							:					Tuple           =							initializer_range
												SCREAMING_SNAKE_CASE							:					List[str]           =							num_labels
												SCREAMING_SNAKE_CASE							:					Tuple           =							num_choices
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							relative_attention
												SCREAMING_SNAKE_CASE							:					Any           =							position_biased_input
												SCREAMING_SNAKE_CASE							:					str           =							pos_att_type
												SCREAMING_SNAKE_CASE							:					List[Any]           =							scope


						def  __A ( self			:       Any		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					List[Any]           =							ids_tensor([self.batch_size, self.seq_length]					,	self.vocab_size		)

												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							None
												if self.use_input_mask:
																		SCREAMING_SNAKE_CASE							:					int           =							ids_tensor([self.batch_size, self.seq_length]					,	vocab_size=2		)

												SCREAMING_SNAKE_CASE							:					int           =							None
												if self.use_token_type_ids:
																		SCREAMING_SNAKE_CASE							:					List[str]           =							ids_tensor([self.batch_size, self.seq_length]					,	self.type_vocab_size		)

												SCREAMING_SNAKE_CASE							:					List[Any]           =							None
												SCREAMING_SNAKE_CASE							:					int           =							None
												SCREAMING_SNAKE_CASE							:					Dict           =							None
												if self.use_labels:
																		SCREAMING_SNAKE_CASE							:					List[str]           =							ids_tensor([self.batch_size]					,	self.type_sequence_label_size		)
																		SCREAMING_SNAKE_CASE							:					List[Any]           =							ids_tensor([self.batch_size, self.seq_length]					,	self.num_labels		)
																		SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							ids_tensor([self.batch_size]					,	self.num_choices		)

												SCREAMING_SNAKE_CASE							:					List[Any]           =							self.get_config()

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


						def  __A ( self			:       str		):





												'''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  __A ( self			:       int					,	UpperCamelCase__			:       List[Any]		):





												'''simple docstring'''





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


						def  __A ( self			:       str					,	UpperCamelCase__			:       Optional[int]					,	UpperCamelCase__			:       Tuple					,	UpperCamelCase__			:       Dict					,	UpperCamelCase__			:       Union[str, Any]					,	UpperCamelCase__			:       str					,	UpperCamelCase__			:       str					,	UpperCamelCase__			:       str		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					str           =							DebertaVaModel(config=UpperCamelCase__		)
												model.to(UpperCamelCase__		)
												model.eval()
												SCREAMING_SNAKE_CASE							:					Any           =							model(UpperCamelCase__					,	attention_mask=UpperCamelCase__					,	token_type_ids=UpperCamelCase__		)[0]
												SCREAMING_SNAKE_CASE							:					int           =							model(UpperCamelCase__					,	token_type_ids=UpperCamelCase__		)[0]
												SCREAMING_SNAKE_CASE							:					Dict           =							model(UpperCamelCase__		)[0]

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


						def  __A ( self			:       int					,	UpperCamelCase__			:       Any					,	UpperCamelCase__			:       Tuple					,	UpperCamelCase__			:       Optional[Any]					,	UpperCamelCase__			:       Union[str, Any]					,	UpperCamelCase__			:       Dict					,	UpperCamelCase__			:       int					,	UpperCamelCase__			:       str		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Tuple           =							DebertaVaForMaskedLM(config=UpperCamelCase__		)
												model.to(UpperCamelCase__		)
												model.eval()
												SCREAMING_SNAKE_CASE							:					Any           =							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  __A ( self			:       Any					,	UpperCamelCase__			:       List[str]					,	UpperCamelCase__			:       Union[str, Any]					,	UpperCamelCase__			:       Optional[Any]					,	UpperCamelCase__			:       Union[str, Any]					,	UpperCamelCase__			:       Union[str, Any]					,	UpperCamelCase__			:       str					,	UpperCamelCase__			:       int		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					List[Any]           =							self.num_labels
												SCREAMING_SNAKE_CASE							:					List[Any]           =							DebertaVaForSequenceClassification(UpperCamelCase__		)
												model.to(UpperCamelCase__		)
												model.eval()
												SCREAMING_SNAKE_CASE							:					str           =							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  __A ( self			:       int					,	UpperCamelCase__			:       str					,	UpperCamelCase__			:       Dict					,	UpperCamelCase__			:       int					,	UpperCamelCase__			:       List[str]					,	UpperCamelCase__			:       Tuple					,	UpperCamelCase__			:       List[str]					,	UpperCamelCase__			:       str		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					List[Any]           =							self.num_labels
												SCREAMING_SNAKE_CASE							:					List[str]           =							DebertaVaForTokenClassification(config=UpperCamelCase__		)
												model.to(UpperCamelCase__		)
												model.eval()
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							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  __A ( self			:       Union[str, Any]					,	UpperCamelCase__			:       Tuple					,	UpperCamelCase__			:       int					,	UpperCamelCase__			:       List[Any]					,	UpperCamelCase__			:       Optional[Any]					,	UpperCamelCase__			:       List[Any]					,	UpperCamelCase__			:       int					,	UpperCamelCase__			:       List[Any]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					int           =							DebertaVaForQuestionAnswering(config=UpperCamelCase__		)
												model.to(UpperCamelCase__		)
												model.eval()
												SCREAMING_SNAKE_CASE							:					Tuple           =							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  __A ( self			:       int					,	UpperCamelCase__			:       Union[str, Any]					,	UpperCamelCase__			:       Optional[Any]					,	UpperCamelCase__			:       Tuple					,	UpperCamelCase__			:       Union[str, Any]					,	UpperCamelCase__			:       Union[str, Any]					,	UpperCamelCase__			:       str					,	UpperCamelCase__			:       str		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					int           =							DebertaVaForMultipleChoice(config=UpperCamelCase__		)
												model.to(UpperCamelCase__		)
												model.eval()
												SCREAMING_SNAKE_CASE							:					List[Any]           =							input_ids.unsqueeze(1		).expand(-1					,	self.num_choices					,	-1		).contiguous()
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							token_type_ids.unsqueeze(1		).expand(-1					,	self.num_choices					,	-1		).contiguous()
												SCREAMING_SNAKE_CASE							:					Optional[int]           =							input_mask.unsqueeze(1		).expand(-1					,	self.num_choices					,	-1		).contiguous()
												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							model(
												    UpperCamelCase__					,	attention_mask=UpperCamelCase__					,	token_type_ids=UpperCamelCase__					,	labels=UpperCamelCase__					,	)
												self.parent.assertEqual(result.logits.shape					,	(self.batch_size, self.num_choices)		)



						def  __A ( self			:       int		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					List[str]           =							self.prepare_config_and_inputs()
												(
												    (
												    SCREAMING_SNAKE_CASE  
												)       ,						(
												    SCREAMING_SNAKE_CASE  
												)       ,						(
												    SCREAMING_SNAKE_CASE  
												)       ,						(
												    SCREAMING_SNAKE_CASE  
												)       ,						(
												    SCREAMING_SNAKE_CASE  
												)       ,						(
												    SCREAMING_SNAKE_CASE  
												)       ,						(
												    SCREAMING_SNAKE_CASE  
												)       ,						  
												)							:					Tuple           =							config_and_inputs
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							{'''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):
						UpperCamelCase_  =	(
						    (
						        DebertaVaModel,
						        DebertaVaForMaskedLM,
						        DebertaVaForSequenceClassification,
						        DebertaVaForTokenClassification,
						        DebertaVaForQuestionAnswering,
						        DebertaVaForMultipleChoice,
						    )
						    if is_torch_available()
						    else ()
						)
						UpperCamelCase_  =	(
						    {
						        """feature-extraction""": DebertaVaModel,
						        """fill-mask""": DebertaVaForMaskedLM,
						        """question-answering""": DebertaVaForQuestionAnswering,
						        """text-classification""": DebertaVaForSequenceClassification,
						        """token-classification""": DebertaVaForTokenClassification,
						        """zero-shot""": DebertaVaForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)

						UpperCamelCase_  =	True
						UpperCamelCase_  =	False
						UpperCamelCase_  =	False
						UpperCamelCase_  =	False
						UpperCamelCase_  =	False


						def  __A ( self			:       Any		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							DebertaVaModelTester(self		)
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							ConfigTester(self					,	config_class=UpperCamelCase__					,	hidden_size=37		)


						def  __A ( self			:       List[str]		):





												'''simple docstring'''





												self.config_tester.run_common_tests()


						def  __A ( self			:       Tuple		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					str           =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_deberta_model(*UpperCamelCase__		)


						def  __A ( self			:       Union[str, Any]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Optional[int]           =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_deberta_for_sequence_classification(*UpperCamelCase__		)


						def  __A ( self			:       Optional[int]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Tuple           =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_deberta_for_masked_lm(*UpperCamelCase__		)


						def  __A ( self			:       Union[str, Any]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					int           =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_deberta_for_question_answering(*UpperCamelCase__		)


						def  __A ( self			:       str		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					Tuple           =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_deberta_for_token_classification(*UpperCamelCase__		)


						def  __A ( self			:       Optional[int]		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					int           =							self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_deberta_for_multiple_choice(*UpperCamelCase__		)



						@slow
						def  __A ( self			:       Dict		):





												'''simple docstring'''





												for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		SCREAMING_SNAKE_CASE							:					str           =							DebertaVaModel.from_pretrained(UpperCamelCase__		)
																		self.assertIsNotNone(UpperCamelCase__		)






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


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





												'''simple docstring'''





												pass



						@slow
						def  __A ( self			:       Tuple		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					List[Any]           =							DebertaVaModel.from_pretrained('''microsoft/deberta-v2-xlarge'''		)

												SCREAMING_SNAKE_CASE							:					Any           =							torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]		)
												SCREAMING_SNAKE_CASE							:					str           =							torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
												with torch.no_grad():
																		SCREAMING_SNAKE_CASE							:					Tuple           =							model(UpperCamelCase__					,	attention_mask=UpperCamelCase__		)[0]
												# compare the actual values for a slice.
												SCREAMING_SNAKE_CASE							:					int           =							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]}"""		)






# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse

from ...utils.dataclasses import (
    ComputeEnvironment,
    DistributedType,
    DynamoBackend,
    PrecisionType,
    SageMakerDistributedType,
)
from ..menu import BulletMenu


__UpperCamelCase	: Dict    		=  [
    'EAGER',
    'AOT_EAGER',
    'INDUCTOR',
    'NVFUSER',
    'AOT_NVFUSER',
    'AOT_CUDAGRAPHS',
    'OFI',
    'FX2TRT',
    'ONNXRT',
    'IPEX',
]
def    A			(					_lowercase	,						_lowercase=None	,						_lowercase=None	,						_lowercase=None		):
						SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							True
						while ask_again:
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							input(_lowercase		)
												try:
																		if default is not None and len(_lowercase		) == 0:
																								return default
																		return convert_value(_lowercase		) if convert_value is not None else result
												except Exception:
																		if error_message is not None:
																								print(_lowercase		)
def    A			(					_lowercase	,						_lowercase=[]	,						_lowercase=None	,						_lowercase=0		):
						SCREAMING_SNAKE_CASE							:					Dict           =							BulletMenu(_lowercase	,						_lowercase		)
						SCREAMING_SNAKE_CASE							:					str           =							menu.run(default_choice=_lowercase		)
						return convert_value(_lowercase		) if convert_value is not None else result
def    A			(					_lowercase		):
						SCREAMING_SNAKE_CASE							:					Dict           =							int(_lowercase		)
						return ComputeEnvironment(['''LOCAL_MACHINE''', '''AMAZON_SAGEMAKER'''][value]		)
def    A			(					_lowercase		):
						SCREAMING_SNAKE_CASE							:					Any           =							int(_lowercase		)
						return DistributedType(['''NO''', '''MULTI_CPU''', '''MULTI_XPU''', '''MULTI_GPU''', '''MULTI_NPU''', '''TPU'''][value]		)
def    A			(					_lowercase		):
						SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							int(_lowercase		)
						return DynamoBackend(DYNAMO_BACKENDS[value]		).value
def    A			(					_lowercase		):
						SCREAMING_SNAKE_CASE							:					Union[str, Any]           =							int(_lowercase		)
						return PrecisionType(['''no''', '''fp16''', '''bf16''', '''fp8'''][value]		)
def    A			(					_lowercase		):
						SCREAMING_SNAKE_CASE							:					Dict           =							int(_lowercase		)
						return SageMakerDistributedType(['''NO''', '''DATA_PARALLEL''', '''MODEL_PARALLEL'''][value]		)
def    A			(					_lowercase		):
						return {"yes": True, "no": False}[value.lower()]





class       lowercase__		(     argparse.RawDescriptionHelpFormatter):


						def  __A ( self			:       List[str]					,	UpperCamelCase__			:       List[str]					,	UpperCamelCase__			:       Dict					,	UpperCamelCase__			:       Dict					,	UpperCamelCase__			:       Dict		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:					str           =							super()._format_usage(UpperCamelCase__					,	UpperCamelCase__					,	UpperCamelCase__					,	UpperCamelCase__		)
												SCREAMING_SNAKE_CASE							:					Optional[Any]           =							usage.replace('''<command> [<args>] '''					,	''''''		)
												return usage





