



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       a		(   a__					):







							def __init__(						self  :  Optional[Any]      ,				snake_case__  :  Optional[int]      ,				snake_case__  :  List[Any]=13      ,				snake_case__  :  Optional[Any]=7      ,				snake_case__  :  str=True      ,				snake_case__  :  int=True      ,				snake_case__  :  Union[str, Any]=True      ,				snake_case__  :  Dict=True      ,				snake_case__  :  List[Any]=99      ,				snake_case__  :  str=32      ,				snake_case__  :  Dict=5      ,				snake_case__  :  int=4      ,				snake_case__  :  Optional[Any]=37      ,				snake_case__  :  Dict="gelu"      ,				snake_case__  :  Optional[int]=0.1      ,				snake_case__  :  Optional[Any]=0.1      ,				snake_case__  :  Any=512      ,				snake_case__  :  Dict=16      ,				snake_case__  :  int=2      ,				snake_case__  :  int=0.0_2      ,				snake_case__  :  Dict=False      ,				snake_case__  :  Any=True      ,				snake_case__  :  Optional[int]="None"      ,				snake_case__  :  Dict=3      ,				snake_case__  :  Any=4      ,				snake_case__  :  Optional[int]=None      ,				):
								"""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       UpperCAmelCase__					(						self  :  str ):
								"""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       UpperCAmelCase__					(						self  :  List[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       UpperCAmelCase__					(						self  :  Optional[int]      ,				snake_case__  :  Union[str, Any] ):
								"""simple docstring"""







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







							def       UpperCAmelCase__					(						self  :  List[str]      ,				snake_case__  :  Union[str, Any]      ,				snake_case__  :  Optional[Any]      ,				snake_case__  :  Any      ,				snake_case__  :  Optional[int]      ,				snake_case__  :  Union[str, Any]      ,				snake_case__  :  List[Any]      ,				snake_case__  :  Any ):
								"""simple docstring"""







								__lowerCAmelCase         =							DebertaVaModel(config=_A )
								model.to(_A )
								model.eval()
								__lowerCAmelCase         =							model(_A      ,				attention_mask=_A      ,				token_type_ids=_A )[0]
								__lowerCAmelCase         =							model(_A      ,				token_type_ids=_A )[0]
								__lowerCAmelCase         =							model(_A )[0]

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







							def       UpperCAmelCase__					(						self  :  Dict      ,				snake_case__  :  str      ,				snake_case__  :  Any      ,				snake_case__  :  str      ,				snake_case__  :  Tuple      ,				snake_case__  :  Union[str, Any]      ,				snake_case__  :  Tuple      ,				snake_case__  :  str ):
								"""simple docstring"""







								__lowerCAmelCase         =							DebertaVaForMaskedLM(config=_A )
								model.to(_A )
								model.eval()
								__lowerCAmelCase         =							model(_A      ,				attention_mask=_A      ,				token_type_ids=_A      ,				labels=_A )

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







							def       UpperCAmelCase__					(						self  :  Optional[int]      ,				snake_case__  :  Union[str, Any]      ,				snake_case__  :  int      ,				snake_case__  :  List[str]      ,				snake_case__  :  Tuple      ,				snake_case__  :  Dict      ,				snake_case__  :  List[str]      ,				snake_case__  :  Union[str, Any] ):
								"""simple docstring"""







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







							def       UpperCAmelCase__					(						self  :  Optional[int]      ,				snake_case__  :  str      ,				snake_case__  :  str      ,				snake_case__  :  Any      ,				snake_case__  :  int      ,				snake_case__  :  Union[str, Any]      ,				snake_case__  :  str      ,				snake_case__  :  int ):
								"""simple docstring"""







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







							def       UpperCAmelCase__					(						self  :  Optional[Any]      ,				snake_case__  :  Union[str, Any]      ,				snake_case__  :  Optional[Any]      ,				snake_case__  :  Any      ,				snake_case__  :  List[str]      ,				snake_case__  :  Optional[Any]      ,				snake_case__  :  int      ,				snake_case__  :  str ):
								"""simple docstring"""







								__lowerCAmelCase         =							DebertaVaForQuestionAnswering(config=_A )
								model.to(_A )
								model.eval()
								__lowerCAmelCase         =							model(
								    _A      ,				attention_mask=_A      ,				token_type_ids=_A      ,				start_positions=_A      ,				end_positions=_A      ,				)
								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       UpperCAmelCase__					(						self  :  str      ,				snake_case__  :  List[str]      ,				snake_case__  :  Optional[Any]      ,				snake_case__  :  Optional[int]      ,				snake_case__  :  Tuple      ,				snake_case__  :  Tuple      ,				snake_case__  :  Optional[Any]      ,				snake_case__  :  Optional[int] ):
								"""simple docstring"""







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







							def       UpperCAmelCase__					(						self  :  List[str] ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.prepare_config_and_inputs()
								(
								    __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       a		(   a__      ,	a__      ,	unittest.TestCase					):
							lowercase_		:						Optional[Any]   							=  (
							    (
							        DebertaVaModel,
							        DebertaVaForMaskedLM,
							        DebertaVaForSequenceClassification,
							        DebertaVaForTokenClassification,
							        DebertaVaForQuestionAnswering,
							        DebertaVaForMultipleChoice,
							    )
							    if is_torch_available()
							    else ()
							)
							lowercase_		:						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 {}
							)

							lowercase_		:						Union[str, Any]   							=  True
							lowercase_		:						Dict   							=  False
							lowercase_		:						int   							=  False
							lowercase_		:						Dict   							=  False
							lowercase_		:						int   							=  False







							def       UpperCAmelCase__					(						self  :  List[str] ):
								"""simple docstring"""







								__lowerCAmelCase         =							DebertaVaModelTester(self )
								__lowerCAmelCase         =							ConfigTester(self      ,				config_class=_A      ,				hidden_size=37 )







							def       UpperCAmelCase__					(						self  :  List[str] ):
								"""simple docstring"""







								self.config_tester.run_common_tests()







							def       UpperCAmelCase__					(						self  :  Optional[Any] ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_model(*_A )







							def       UpperCAmelCase__					(						self  :  Optional[int] ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_sequence_classification(*_A )







							def       UpperCAmelCase__					(						self  :  Dict ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_masked_lm(*_A )







							def       UpperCAmelCase__					(						self  :  Union[str, Any] ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_question_answering(*_A )







							def       UpperCAmelCase__					(						self  :  str ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_token_classification(*_A )







							def       UpperCAmelCase__					(						self  :  List[str] ):
								"""simple docstring"""







								__lowerCAmelCase         =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_multiple_choice(*_A )







							@slow
							def       UpperCAmelCase__					(						self  :  Any ):
								"""simple docstring"""







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

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







							@unittest.skip(reason="Model not available yet" )
							def       UpperCAmelCase__					(						self  :  Any ):
								"""simple docstring"""







								pass







							@slow
							def       UpperCAmelCase__					(						self  :  Union[str, 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(_A      ,				attention_mask=_A )[0]
								# compare the actual values for a slice.
								__lowerCAmelCase         =							torch.tensor(
								    [[[0.2_3_5_6, 0.1_9_4_8, 0.0_3_6_9], [-0.1_0_6_3, 0.3_5_8_6, -0.5_1_5_2], [-0.6_3_9_9, -0.0_2_5_9, -0.2_5_2_5]]] )
								self.assertTrue(torch.allclose(output[:, 1:4, 1:4]      ,				_A      ,				atol=1E-4 )      ,				F"{output[:, 1:4, 1:4]}" )








from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


UpperCamelCase_   	=	logging.get_logger(__name__)

UpperCamelCase_   	=	{
    "distilbert-base-uncased": "https://huggingface.co/distilbert-base-uncased/resolve/main/config.json",
    "distilbert-base-uncased-distilled-squad": (
        "https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json"
    ),
    "distilbert-base-cased": "https://huggingface.co/distilbert-base-cased/resolve/main/config.json",
    "distilbert-base-cased-distilled-squad": (
        "https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json"
    ),
    "distilbert-base-german-cased": "https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json",
    "distilbert-base-multilingual-cased": (
        "https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json"
    ),
    "distilbert-base-uncased-finetuned-sst-2-english": (
        "https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json"
    ),
}

class       a		(   __UpperCAmelCase					):
							lowercase_		:						str   							=  'distilbert'
							lowercase_		:						Any   							=  {
							    'hidden_size': 'dim',
							    'num_attention_heads': 'n_heads',
							    'num_hidden_layers': 'n_layers',
							}







							def __init__(						self  :  Optional[int]      ,				snake_case__  :  int=30_522      ,				snake_case__  :  str=512      ,				snake_case__  :  Tuple=False      ,				snake_case__  :  Tuple=6      ,				snake_case__  :  Any=12      ,				snake_case__  :  Dict=768      ,				snake_case__  :  Any=4 * 768      ,				snake_case__  :  Union[str, Any]=0.1      ,				snake_case__  :  str=0.1      ,				snake_case__  :  Tuple="gelu"      ,				snake_case__  :  str=0.0_2      ,				snake_case__  :  Optional[Any]=0.1      ,				snake_case__  :  Optional[Any]=0.2      ,				snake_case__  :  str=0      ,				**snake_case__  :  Dict      ,				):
								"""simple docstring"""







								__lowerCAmelCase         =							vocab_size
								__lowerCAmelCase         =							max_position_embeddings
								__lowerCAmelCase         =							sinusoidal_pos_embds
								__lowerCAmelCase         =							n_layers
								__lowerCAmelCase         =							n_heads
								__lowerCAmelCase         =							dim
								__lowerCAmelCase         =							hidden_dim
								__lowerCAmelCase         =							dropout
								__lowerCAmelCase         =							attention_dropout
								__lowerCAmelCase         =							activation
								__lowerCAmelCase         =							initializer_range
								__lowerCAmelCase         =							qa_dropout
								__lowerCAmelCase         =							seq_classif_dropout
								super().__init__(**snake_case__      ,				pad_token_id=snake_case__ )

class       a		(   __UpperCAmelCase					):







							@property
							def       UpperCAmelCase__					(						self  :  Tuple ):
								"""simple docstring"""







								if self.task == "multiple-choice":
									__lowerCAmelCase         =							{0: "batch", 1: "choice", 2: "sequence"}
								else:
									__lowerCAmelCase         =							{0: "batch", 1: "sequence"}
								return OrderedDict(
								    [
								        ("input_ids", dynamic_axis),
								        ("attention_mask", dynamic_axis),
								    ] )






