





import os
import tempfile
import unittest

from transformers import DistilBertConfig, is_torch_available
from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import (
							    DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
							    DistilBertForMaskedLM,
							    DistilBertForMultipleChoice,
							    DistilBertForQuestionAnswering,
							    DistilBertForSequenceClassification,
							    DistilBertForTokenClassification,
							    DistilBertModel,
							)





class 				a			(      __lowercase			):






				def __init__(  self			, _lowerCAmelCase			, _lowerCAmelCase=13			, _lowerCAmelCase=7			, _lowerCAmelCase=True			, _lowerCAmelCase=True			, _lowerCAmelCase=False			, _lowerCAmelCase=True			, _lowerCAmelCase=99			, _lowerCAmelCase=32			, _lowerCAmelCase=5			, _lowerCAmelCase=4			, _lowerCAmelCase=37			, _lowerCAmelCase="gelu"			, _lowerCAmelCase=0.1			, _lowerCAmelCase=0.1			, _lowerCAmelCase=512			, _lowerCAmelCase=16			, _lowerCAmelCase=2			, _lowerCAmelCase=0.02			, _lowerCAmelCase=3			, _lowerCAmelCase=4			, _lowerCAmelCase=None			, ):

										"""simple docstring"""




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






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Dict           =   ids_tensor([self.batch_size, self.seq_length]			, self.vocab_size					)

										__SCREAMING_SNAKE_CASE:				Optional[int]           =   None
										if self.use_input_mask:
																__SCREAMING_SNAKE_CASE:				List[Any]           =   random_attention_mask([self.batch_size, self.seq_length]					)

										__SCREAMING_SNAKE_CASE:				Tuple           =   None
										__SCREAMING_SNAKE_CASE:				Optional[int]           =   None
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   None
										if self.use_labels:
																__SCREAMING_SNAKE_CASE:				List[Any]           =   ids_tensor([self.batch_size]			, self.type_sequence_label_size					)
																__SCREAMING_SNAKE_CASE:				int           =   ids_tensor([self.batch_size, self.seq_length]			, self.num_labels					)
																__SCREAMING_SNAKE_CASE:				Optional[int]           =   ids_tensor([self.batch_size]			, self.num_choices					)

										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   self.get_config()

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






				def 			snake_case_    (  self					):

										"""simple docstring"""




										return DistilBertConfig(
										    vocab_size=self.vocab_size			, dim=self.hidden_size			, n_layers=self.num_hidden_layers			, n_heads=self.num_attention_heads			, hidden_dim=self.intermediate_size			, hidden_act=self.hidden_act			, dropout=self.hidden_dropout_prob			, attention_dropout=self.attention_probs_dropout_prob			, max_position_embeddings=self.max_position_embeddings			, initializer_range=self.initializer_range			, )






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Any           =   DistilBertModel(config=_lowerCAmelCase					)
										model.to(_lowerCAmelCase					)
										model.eval()
										__SCREAMING_SNAKE_CASE:				List[str]           =   model(_lowerCAmelCase			, _lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   model(_lowerCAmelCase					)
										self.parent.assertEqual(result.last_hidden_state.shape			, (self.batch_size, self.seq_length, self.hidden_size)					)






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				List[Any]           =   DistilBertForMaskedLM(config=_lowerCAmelCase					)
										model.to(_lowerCAmelCase					)
										model.eval()
										__SCREAMING_SNAKE_CASE:				List[Any]           =   model(_lowerCAmelCase			, attention_mask=_lowerCAmelCase			, labels=_lowerCAmelCase					)
										self.parent.assertEqual(result.logits.shape			, (self.batch_size, self.seq_length, self.vocab_size)					)






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Optional[int]           =   DistilBertForQuestionAnswering(config=_lowerCAmelCase					)
										model.to(_lowerCAmelCase					)
										model.eval()
										__SCREAMING_SNAKE_CASE:				List[Any]           =   model(
										    _lowerCAmelCase			, attention_mask=_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 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Optional[int]           =   self.num_labels
										__SCREAMING_SNAKE_CASE:				Tuple           =   DistilBertForSequenceClassification(_lowerCAmelCase					)
										model.to(_lowerCAmelCase					)
										model.eval()
										__SCREAMING_SNAKE_CASE:				Any           =   model(_lowerCAmelCase			, attention_mask=_lowerCAmelCase			, labels=_lowerCAmelCase					)
										self.parent.assertEqual(result.logits.shape			, (self.batch_size, self.num_labels)					)






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				List[Any]           =   self.num_labels
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   DistilBertForTokenClassification(config=_lowerCAmelCase					)
										model.to(_lowerCAmelCase					)
										model.eval()

										__SCREAMING_SNAKE_CASE:				Dict           =   model(_lowerCAmelCase			, attention_mask=_lowerCAmelCase			, labels=_lowerCAmelCase					)
										self.parent.assertEqual(result.logits.shape			, (self.batch_size, self.seq_length, self.num_labels)					)






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase			, _lowerCAmelCase					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Tuple           =   self.num_choices
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   DistilBertForMultipleChoice(config=_lowerCAmelCase					)
										model.to(_lowerCAmelCase					)
										model.eval()
										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   input_ids.unsqueeze(1					).expand(-1			, self.num_choices			, -1					).contiguous()
										__SCREAMING_SNAKE_CASE:				List[str]           =   input_mask.unsqueeze(1					).expand(-1			, self.num_choices			, -1					).contiguous()
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   model(
										    _lowerCAmelCase			, attention_mask=_lowerCAmelCase			, labels=_lowerCAmelCase			, )
										self.parent.assertEqual(result.logits.shape			, (self.batch_size, self.num_choices)					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   self.prepare_config_and_inputs()
										((__SCREAMING_SNAKE_CASE)       ,(__SCREAMING_SNAKE_CASE)       ,(__SCREAMING_SNAKE_CASE)       ,(__SCREAMING_SNAKE_CASE)       ,(__SCREAMING_SNAKE_CASE)       ,(__SCREAMING_SNAKE_CASE)):				int           =   config_and_inputs
										__SCREAMING_SNAKE_CASE:				Optional[int]           =   {'''input_ids''': input_ids, '''attention_mask''': input_mask}
										return config, inputs_dict





@require_torch
class 				a			(      __lowercase   ,__lowercase   ,unittest.TestCase			):
				SCREAMING_SNAKE_CASE__     :   Tuple		      =     (
				    (
				        DistilBertModel,
				        DistilBertForMaskedLM,
				        DistilBertForMultipleChoice,
				        DistilBertForQuestionAnswering,
				        DistilBertForSequenceClassification,
				        DistilBertForTokenClassification,
				    )
				    if is_torch_available()
				    else None
				)
				SCREAMING_SNAKE_CASE__     :   str		      =     (
				    {
				        '''feature-extraction''': DistilBertModel,
				        '''fill-mask''': DistilBertForMaskedLM,
				        '''question-answering''': DistilBertForQuestionAnswering,
				        '''text-classification''': DistilBertForSequenceClassification,
				        '''token-classification''': DistilBertForTokenClassification,
				        '''zero-shot''': DistilBertForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				SCREAMING_SNAKE_CASE__     :   Optional[int]		      =     True
				SCREAMING_SNAKE_CASE__     :   str		      =     True
				SCREAMING_SNAKE_CASE__     :   List[Any]		      =     True
				SCREAMING_SNAKE_CASE__     :   Optional[int]		      =     True






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				List[str]           =   DistilBertModelTester(self					)
										__SCREAMING_SNAKE_CASE:				str           =   ConfigTester(self			, config_class=_lowerCAmelCase			, dim=37					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										self.config_tester.run_common_tests()






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_model(*_lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_for_masked_lm(*_lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				List[Any]           =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_for_question_answering(*_lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Tuple           =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_for_sequence_classification(*_lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_for_token_classification(*_lowerCAmelCase					)






				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				str           =   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_distilbert_for_multiple_choice(*_lowerCAmelCase					)






				@slow
				def 			snake_case_    (  self					):

										"""simple docstring"""




										for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__SCREAMING_SNAKE_CASE:				Any           =   DistilBertModel.from_pretrained(_lowerCAmelCase					)
																self.assertIsNotNone(_lowerCAmelCase					)






				@slow
				@require_torch_gpu
				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE:				int           =   self.model_tester.prepare_config_and_inputs_for_common()
										for model_class in self.all_model_classes:
																# BertForMultipleChoice behaves incorrectly in JIT environments.
																if model_class == DistilBertForMultipleChoice:
																						return

																__SCREAMING_SNAKE_CASE:				Any           =   True
																__SCREAMING_SNAKE_CASE:				Any           =   model_class(config=_lowerCAmelCase					)

																__SCREAMING_SNAKE_CASE:				List[str]           =   self._prepare_for_class(_lowerCAmelCase			, _lowerCAmelCase					)
																__SCREAMING_SNAKE_CASE:				List[str]           =   torch.jit.trace(
																    _lowerCAmelCase			, (inputs_dict['''input_ids'''].to('''cpu'''					), inputs_dict['''attention_mask'''].to('''cpu'''					))					)

																with tempfile.TemporaryDirectory() as tmp:
																						torch.jit.save(_lowerCAmelCase			, os.path.join(_lowerCAmelCase			, '''traced_model.pt'''					)					)
																						__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   torch.jit.load(os.path.join(_lowerCAmelCase			, '''traced_model.pt'''					)			, map_location=_lowerCAmelCase					)
																						loaded(inputs_dict['''input_ids'''].to(_lowerCAmelCase					)			, inputs_dict['''attention_mask'''].to(_lowerCAmelCase					)					)





@require_torch
class 				a			(      unittest.TestCase			):






				@slow
				def 			snake_case_    (  self					):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				Optional[Any]           =   DistilBertModel.from_pretrained('''distilbert-base-uncased'''					)
										__SCREAMING_SNAKE_CASE:				Tuple           =   torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]					)
										__SCREAMING_SNAKE_CASE:				List[Any]           =   torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]					)
										with torch.no_grad():
																__SCREAMING_SNAKE_CASE:				Any           =   model(_lowerCAmelCase			, attention_mask=_lowerCAmelCase					)[0]
										__SCREAMING_SNAKE_CASE:				str           =   torch.Size((1, 11, 768)					)
										self.assertEqual(output.shape			, _lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   torch.tensor(
										    [[[-0.1639, 0.3299, 0.1648], [-0.1746, 0.3289, 0.1710], [-0.1884, 0.3357, 0.1810]]]					)

										self.assertTrue(torch.allclose(output[:, 1:4, 1:4]			, _lowerCAmelCase			, atol=1e-4					)					)










from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional, Union

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


if TYPE_CHECKING:
							from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType


lowerCAmelCase							:       Tuple					 =	logging.get_logger(__name__)

lowerCAmelCase							:       List[Any]					 =	{
    """microsoft/deberta-v2-xlarge""": """https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json""",
    """microsoft/deberta-v2-xxlarge""": """https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json""",
    """microsoft/deberta-v2-xlarge-mnli""": (
        """https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json"""
    ),
    """microsoft/deberta-v2-xxlarge-mnli""": (
        """https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json"""
    ),
}





class 				a			(      __lowercase			):
				SCREAMING_SNAKE_CASE__     :   str		      =     '''deberta-v2'''






				def __init__(  self			, _lowerCAmelCase=128100			, _lowerCAmelCase=1536			, _lowerCAmelCase=24			, _lowerCAmelCase=24			, _lowerCAmelCase=6144			, _lowerCAmelCase="gelu"			, _lowerCAmelCase=0.1			, _lowerCAmelCase=0.1			, _lowerCAmelCase=512			, _lowerCAmelCase=0			, _lowerCAmelCase=0.02			, _lowerCAmelCase=1e-7			, _lowerCAmelCase=False			, _lowerCAmelCase=-1			, _lowerCAmelCase=0			, _lowerCAmelCase=True			, _lowerCAmelCase=None			, _lowerCAmelCase=0			, _lowerCAmelCase="gelu"			, **_lowerCAmelCase			, ):

										"""simple docstring"""




										super().__init__(**_lowerCAmelCase					)

										__SCREAMING_SNAKE_CASE:				Union[str, Any]           =   hidden_size
										__SCREAMING_SNAKE_CASE:				Tuple           =   num_hidden_layers
										__SCREAMING_SNAKE_CASE:				str           =   num_attention_heads
										__SCREAMING_SNAKE_CASE:				List[Any]           =   intermediate_size
										__SCREAMING_SNAKE_CASE:				str           =   hidden_act
										__SCREAMING_SNAKE_CASE:				Dict           =   hidden_dropout_prob
										__SCREAMING_SNAKE_CASE:				Optional[int]           =   attention_probs_dropout_prob
										__SCREAMING_SNAKE_CASE:				Dict           =   max_position_embeddings
										__SCREAMING_SNAKE_CASE:				Optional[int]           =   type_vocab_size
										__SCREAMING_SNAKE_CASE:				str           =   initializer_range
										__SCREAMING_SNAKE_CASE:				Any           =   relative_attention
										__SCREAMING_SNAKE_CASE:				Tuple           =   max_relative_positions
										__SCREAMING_SNAKE_CASE:				List[str]           =   pad_token_id
										__SCREAMING_SNAKE_CASE:				List[str]           =   position_biased_input

										# Backwards compatibility
										if type(_lowerCAmelCase					) == str:
																__SCREAMING_SNAKE_CASE:				str           =   [x.strip() for x in pos_att_type.lower().split('''|'''					)]

										__SCREAMING_SNAKE_CASE:				int           =   pos_att_type
										__SCREAMING_SNAKE_CASE:				List[Any]           =   vocab_size
										__SCREAMING_SNAKE_CASE:				List[str]           =   layer_norm_eps

										__SCREAMING_SNAKE_CASE:				Optional[int]           =   kwargs.get('''pooler_hidden_size'''			, _lowerCAmelCase					)
										__SCREAMING_SNAKE_CASE:				Dict           =   pooler_dropout
										__SCREAMING_SNAKE_CASE:				int           =   pooler_hidden_act





class 				a			(      __lowercase			):






				@property
				def 			snake_case_    (  self					):

										"""simple docstring"""




										if self.task == "multiple-choice":
																__SCREAMING_SNAKE_CASE:				Any           =   {0: '''batch''', 1: '''choice''', 2: '''sequence'''}
										else:
																__SCREAMING_SNAKE_CASE:				Any           =   {0: '''batch''', 1: '''sequence'''}
										if self._config.type_vocab_size > 0:
																return OrderedDict(
																    [('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis), ('''token_type_ids''', dynamic_axis)]					)
										else:
																return OrderedDict([('''input_ids''', dynamic_axis), ('''attention_mask''', dynamic_axis)]					)






				@property
				def 			snake_case_    (  self					):

										"""simple docstring"""




										return 12






				def 			snake_case_    (  self			, _lowerCAmelCase			, _lowerCAmelCase = -1			, _lowerCAmelCase = -1			, _lowerCAmelCase = -1			, _lowerCAmelCase = False			, _lowerCAmelCase = None			, _lowerCAmelCase = 3			, _lowerCAmelCase = 40			, _lowerCAmelCase = 40			, _lowerCAmelCase = None			, ):

										"""simple docstring"""




										__SCREAMING_SNAKE_CASE:				str           =   super().generate_dummy_inputs(preprocessor=_lowerCAmelCase			, framework=_lowerCAmelCase					)
										if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs:
																del dummy_inputs["token_type_ids"]
										return dummy_inputs



