






import unittest

from transformers import DebertaConfig, 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 (
		    DebertaForMaskedLM,
		    DebertaForQuestionAnswering,
		    DebertaForSequenceClassification,
		    DebertaForTokenClassification,
		    DebertaModel,
		)
		from transformers.models.deberta.modeling_deberta import DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST





class __lowerCamelCase (      __UpperCAmelCase):





	"""simple docstring"""






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







							_UpperCAmelCase									=			parent
							_UpperCAmelCase									=			batch_size
							_UpperCAmelCase									=			seq_length
							_UpperCAmelCase									=			is_training
							_UpperCAmelCase									=			use_input_mask
							_UpperCAmelCase									=			use_token_type_ids
							_UpperCAmelCase									=			use_labels
							_UpperCAmelCase									=			vocab_size
							_UpperCAmelCase									=			hidden_size
							_UpperCAmelCase									=			num_hidden_layers
							_UpperCAmelCase									=			num_attention_heads
							_UpperCAmelCase									=			intermediate_size
							_UpperCAmelCase									=			hidden_act
							_UpperCAmelCase									=			hidden_dropout_prob
							_UpperCAmelCase									=			attention_probs_dropout_prob
							_UpperCAmelCase									=			max_position_embeddings
							_UpperCAmelCase									=			type_vocab_size
							_UpperCAmelCase									=			type_sequence_label_size
							_UpperCAmelCase									=			initializer_range
							_UpperCAmelCase									=			num_labels
							_UpperCAmelCase									=			num_choices
							_UpperCAmelCase									=			relative_attention
							_UpperCAmelCase									=			position_biased_input
							_UpperCAmelCase									=			pos_att_type
							_UpperCAmelCase									=			scope

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			ids_tensor([self.batch_size, self.seq_length]       ,    self.vocab_size    )

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

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

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

							_UpperCAmelCase									=			self.get_config()

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

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							return DebertaConfig(
							    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    ):
							"""simple docstring"""







							_UpperCAmelCase									=			self.get_config()
							_UpperCAmelCase									=			300
							return config

	def        UpperCamelCase    (							self       ,    UpperCAmelCase    ):
							"""simple docstring"""







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

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase    ):
							"""simple docstring"""







							_UpperCAmelCase									=			DebertaModel(config=__SCREAMING_SNAKE_CASE    )
							model.to(__SCREAMING_SNAKE_CASE    )
							model.eval()
							_UpperCAmelCase									=			model(__SCREAMING_SNAKE_CASE       ,    attention_mask=__SCREAMING_SNAKE_CASE       ,    token_type_ids=__SCREAMING_SNAKE_CASE    )[0]
							_UpperCAmelCase									=			model(__SCREAMING_SNAKE_CASE       ,    token_type_ids=__SCREAMING_SNAKE_CASE    )[0]
							_UpperCAmelCase									=			model(__SCREAMING_SNAKE_CASE    )[0]

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

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase    ):
							"""simple docstring"""







							_UpperCAmelCase									=			DebertaForMaskedLM(config=__SCREAMING_SNAKE_CASE    )
							model.to(__SCREAMING_SNAKE_CASE    )
							model.eval()
							_UpperCAmelCase									=			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        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase    ):
							"""simple docstring"""







							_UpperCAmelCase									=			self.num_labels
							_UpperCAmelCase									=			DebertaForSequenceClassification(__SCREAMING_SNAKE_CASE    )
							model.to(__SCREAMING_SNAKE_CASE    )
							model.eval()
							_UpperCAmelCase									=			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        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase    ):
							"""simple docstring"""







							_UpperCAmelCase									=			self.num_labels
							_UpperCAmelCase									=			DebertaForTokenClassification(config=__SCREAMING_SNAKE_CASE    )
							model.to(__SCREAMING_SNAKE_CASE    )
							model.eval()
							_UpperCAmelCase									=			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        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase    ):
							"""simple docstring"""







							_UpperCAmelCase									=			DebertaForQuestionAnswering(config=__SCREAMING_SNAKE_CASE    )
							model.to(__SCREAMING_SNAKE_CASE    )
							model.eval()
							_UpperCAmelCase									=			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        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			self.prepare_config_and_inputs()
							(
							    (
							    _UpperCAmelCase   
							)					, (
							    _UpperCAmelCase   
							)					, (
							    _UpperCAmelCase   
							)					, (
							    _UpperCAmelCase   
							)					, (
							    _UpperCAmelCase   
							)					, (
							    _UpperCAmelCase   
							)					, (
							    _UpperCAmelCase   
							)					,    
							)									=			config_and_inputs
							_UpperCAmelCase									=			{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
							return config, inputs_dict





@require_torch
class __lowerCamelCase (      __UpperCAmelCase     ,       __UpperCAmelCase     ,       unittest.TestCase):





	"""simple docstring"""






	UpperCamelCase__								=      (
	    (
	        DebertaModel,
	        DebertaForMaskedLM,
	        DebertaForSequenceClassification,
	        DebertaForTokenClassification,
	        DebertaForQuestionAnswering,
	    )
	    if is_torch_available()
	    else ()
	)
	UpperCamelCase__								=      (
	    {
	        '''feature-extraction''': DebertaModel,
	        '''fill-mask''': DebertaForMaskedLM,
	        '''question-answering''': DebertaForQuestionAnswering,
	        '''text-classification''': DebertaForSequenceClassification,
	        '''token-classification''': DebertaForTokenClassification,
	        '''zero-shot''': DebertaForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)

	UpperCamelCase__								=      True
	UpperCamelCase__								=      False
	UpperCamelCase__								=      False
	UpperCamelCase__								=      False
	UpperCamelCase__								=      False

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			DebertaModelTester(self    )
							_UpperCAmelCase									=			ConfigTester(self       ,    config_class=__SCREAMING_SNAKE_CASE       ,    hidden_size=37    )

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							self.config_tester.run_common_tests()

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_model(*__SCREAMING_SNAKE_CASE    )

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_sequence_classification(*__SCREAMING_SNAKE_CASE    )

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_masked_lm(*__SCREAMING_SNAKE_CASE    )

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_question_answering(*__SCREAMING_SNAKE_CASE    )

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_token_classification(*__SCREAMING_SNAKE_CASE    )

	@slow
	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													_UpperCAmelCase									=			DebertaModel.from_pretrained(__SCREAMING_SNAKE_CASE    )
													self.assertIsNotNone(__SCREAMING_SNAKE_CASE    )





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





	"""simple docstring"""






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







							pass

	@slow
	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			DebertaModel.from_pretrained('microsoft/deberta-base'    )

							_UpperCAmelCase									=			torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]    )
							_UpperCAmelCase									=			torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]    )
							with torch.no_grad():
													_UpperCAmelCase									=			model(__SCREAMING_SNAKE_CASE       ,    attention_mask=__SCREAMING_SNAKE_CASE    )[0]
							# compare the actual values for a slice.
							_UpperCAmelCase									=			torch.tensor(
							    [[[-0.59_86, -0.80_55, -0.84_62], [1.44_84, -0.93_48, -0.80_59], [0.31_23, 0.00_32, -1.41_31]]]    )
							self.assertTrue(torch.allclose(output[:, 1:4, 1:4]       ,    __SCREAMING_SNAKE_CASE       ,    atol=1e-4    )       ,    F"""{output[:, 1:4, 1:4]}"""    )





import logging

from transformers.configuration_utils import PretrainedConfig


__snake_case						:Any      		=     logging.getLogger(__name__)

class     _A       (	__UpperCAmelCase    ):
       UpperCamelCase__  :    Optional[Any]											=						'''masked_bert'''



       def __init__(		self							:     str     ,		__SCREAMING_SNAKE_CASE							:     int=30_522     ,		__SCREAMING_SNAKE_CASE							:     str=768     ,		__SCREAMING_SNAKE_CASE							:     List[str]=12     ,		__SCREAMING_SNAKE_CASE							:     Any=12     ,		__SCREAMING_SNAKE_CASE							:     Union[str, Any]=3_072     ,		__SCREAMING_SNAKE_CASE							:     List[Any]="gelu"     ,		__SCREAMING_SNAKE_CASE							:     int=0.1     ,		__SCREAMING_SNAKE_CASE							:     Dict=0.1     ,		__SCREAMING_SNAKE_CASE							:     Optional[Any]=512     ,		__SCREAMING_SNAKE_CASE							:     Optional[int]=2     ,		__SCREAMING_SNAKE_CASE							:     Dict=0.02     ,		__SCREAMING_SNAKE_CASE							:     List[Any]=1E-12     ,		__SCREAMING_SNAKE_CASE							:     Union[str, Any]=0     ,		__SCREAMING_SNAKE_CASE							:     List[str]="topK"     ,		__SCREAMING_SNAKE_CASE							:     List[Any]="constant"     ,		__SCREAMING_SNAKE_CASE							:     int=0.0     ,		**__SCREAMING_SNAKE_CASE							:     List[Any]     ,		):


           '''simple docstring'''

           super().__init__(pad_token_id=__SCREAMING_SNAKE_CASE     ,		**__SCREAMING_SNAKE_CASE)

           __a						       =				vocab_size
           __a						       =				hidden_size
           __a						       =				num_hidden_layers
           __a						       =				num_attention_heads
           __a						       =				hidden_act
           __a						       =				intermediate_size
           __a						       =				hidden_dropout_prob
           __a						       =				attention_probs_dropout_prob
           __a						       =				max_position_embeddings
           __a						       =				type_vocab_size
           __a						       =				initializer_range
           __a						       =				layer_norm_eps
           __a						       =				pruning_method
           __a						       =				mask_init
           __a						       =				mask_scale





