

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       snake_case__  ( lowerCAmelCase_     ):







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





								'''simple docstring'''



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







			def 			__lowerCAmelCase				(			self			: List[str]  ):





								'''simple docstring'''



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

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

								UpperCAmelCase			:   List[Any]      =				None
								if self.use_token_type_ids:
													UpperCAmelCase			:   Union[str, Any]      =				ids_tensor([self.batch_size, self.seq_length]			,  self.type_vocab_size  )

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

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

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







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





								'''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 			__lowerCAmelCase				(			self			: Any  ):





								'''simple docstring'''



								UpperCAmelCase			:   Optional[Any]      =				self.get_config()
								UpperCAmelCase			:   str      =				3_00
								return config







			def 			__lowerCAmelCase				(			self			: List[Any]			,  lowercase			: List[Any]  ):





								'''simple docstring'''



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







			def 			__lowerCAmelCase				(			self			: Dict			,  lowercase			: Union[str, Any]			,  lowercase			: Optional[Any]			,  lowercase			: Tuple			,  lowercase			: List[str]			,  lowercase			: Optional[int]			,  lowercase			: str			,  lowercase			: int  ):





								'''simple docstring'''



								UpperCAmelCase			:   int      =				DebertaModel(config=lowercase  )
								model.to(lowercase  )
								model.eval()
								UpperCAmelCase			:   Union[str, Any]      =				model(lowercase			,  attention_mask=lowercase			,  token_type_ids=lowercase  )[0]
								UpperCAmelCase			:   int      =				model(lowercase			,  token_type_ids=lowercase  )[0]
								UpperCAmelCase			:   Optional[int]      =				model(lowercase  )[0]

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







			def 			__lowerCAmelCase				(			self			: List[str]			,  lowercase			: Optional[Any]			,  lowercase			: Optional[int]			,  lowercase			: Union[str, Any]			,  lowercase			: int			,  lowercase			: int			,  lowercase			: Tuple			,  lowercase			: Union[str, Any]  ):





								'''simple docstring'''



								UpperCAmelCase			:   List[str]      =				DebertaForMaskedLM(config=lowercase  )
								model.to(lowercase  )
								model.eval()
								UpperCAmelCase			:   Any      =				model(lowercase			,  attention_mask=lowercase			,  token_type_ids=lowercase			,  labels=lowercase  )

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







			def 			__lowerCAmelCase				(			self			: Optional[Any]			,  lowercase			: int			,  lowercase			: Tuple			,  lowercase			: Tuple			,  lowercase			: Optional[Any]			,  lowercase			: Optional[Any]			,  lowercase			: Optional[int]			,  lowercase			: Union[str, Any]  ):





								'''simple docstring'''



								UpperCAmelCase			:   Optional[Any]      =				self.num_labels
								UpperCAmelCase			:   Any      =				DebertaForSequenceClassification(lowercase  )
								model.to(lowercase  )
								model.eval()
								UpperCAmelCase			:   Union[str, Any]      =				model(lowercase			,  attention_mask=lowercase			,  token_type_ids=lowercase			,  labels=lowercase  )
								self.parent.assertListEqual(list(result.logits.size()  )			,  [self.batch_size, self.num_labels]  )
								self.check_loss_output(lowercase  )







			def 			__lowerCAmelCase				(			self			: List[str]			,  lowercase			: Optional[Any]			,  lowercase			: Union[str, Any]			,  lowercase			: Optional[int]			,  lowercase			: str			,  lowercase			: List[Any]			,  lowercase			: Any			,  lowercase			: str  ):





								'''simple docstring'''



								UpperCAmelCase			:   int      =				self.num_labels
								UpperCAmelCase			:   List[Any]      =				DebertaForTokenClassification(config=lowercase  )
								model.to(lowercase  )
								model.eval()
								UpperCAmelCase			:   Dict      =				model(lowercase			,  attention_mask=lowercase			,  token_type_ids=lowercase			,  labels=lowercase  )
								self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.seq_length, self.num_labels)  )







			def 			__lowerCAmelCase				(			self			: Any			,  lowercase			: Union[str, Any]			,  lowercase			: Optional[Any]			,  lowercase			: int			,  lowercase			: int			,  lowercase			: Union[str, Any]			,  lowercase			: Tuple			,  lowercase			: Optional[Any]  ):





								'''simple docstring'''



								UpperCAmelCase			:   int      =				DebertaForQuestionAnswering(config=lowercase  )
								model.to(lowercase  )
								model.eval()
								UpperCAmelCase			:   Tuple      =				model(
								    lowercase			,  attention_mask=lowercase			,  token_type_ids=lowercase			,  start_positions=lowercase			,  end_positions=lowercase			,  )
								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 			__lowerCAmelCase				(			self			: Tuple  ):





								'''simple docstring'''



								UpperCAmelCase			:   str      =				self.prepare_config_and_inputs()
								(
								    UpperCAmelCase							
								)			:   Optional[Any]      =				config_and_inputs
								UpperCAmelCase			:   int      =				{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
								return config, inputs_dict







@require_torch
class       snake_case__  ( lowerCAmelCase_					,  lowerCAmelCase_					,  unittest.TestCase     ):
			SCREAMING_SNAKE_CASE__     				=						(
			    (
			        DebertaModel,
			        DebertaForMaskedLM,
			        DebertaForSequenceClassification,
			        DebertaForTokenClassification,
			        DebertaForQuestionAnswering,
			    )
			    if is_torch_available()
			    else ()
			)
			SCREAMING_SNAKE_CASE__     				=						(
			    {
			        '''feature-extraction''': DebertaModel,
			        '''fill-mask''': DebertaForMaskedLM,
			        '''question-answering''': DebertaForQuestionAnswering,
			        '''text-classification''': DebertaForSequenceClassification,
			        '''token-classification''': DebertaForTokenClassification,
			        '''zero-shot''': DebertaForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)

			SCREAMING_SNAKE_CASE__     				=						True
			SCREAMING_SNAKE_CASE__     				=						False
			SCREAMING_SNAKE_CASE__     				=						False
			SCREAMING_SNAKE_CASE__     				=						False
			SCREAMING_SNAKE_CASE__     				=						False







			def 			__lowerCAmelCase				(			self			: List[str]  ):





								'''simple docstring'''



								UpperCAmelCase			:   Union[str, Any]      =				DebertaModelTester(self  )
								UpperCAmelCase			:   str      =				ConfigTester(self			,  config_class=lowercase			,  hidden_size=37  )







			def 			__lowerCAmelCase				(			self			: List[str]  ):





								'''simple docstring'''



								self.config_tester.run_common_tests()







			def 			__lowerCAmelCase				(			self			: Optional[Any]  ):





								'''simple docstring'''



								UpperCAmelCase			:   List[str]      =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_model(*lowercase  )







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





								'''simple docstring'''



								UpperCAmelCase			:   Tuple      =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_sequence_classification(*lowercase  )







			def 			__lowerCAmelCase				(			self			: Any  ):





								'''simple docstring'''



								UpperCAmelCase			:   Optional[int]      =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_masked_lm(*lowercase  )







			def 			__lowerCAmelCase				(			self			: List[str]  ):





								'''simple docstring'''



								UpperCAmelCase			:   Dict      =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_question_answering(*lowercase  )







			def 			__lowerCAmelCase				(			self			: Optional[Any]  ):





								'''simple docstring'''



								UpperCAmelCase			:   str      =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_token_classification(*lowercase  )







			@slow
			def 			__lowerCAmelCase				(			self			: int  ):





								'''simple docstring'''



								for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													UpperCAmelCase			:   int      =				DebertaModel.from_pretrained(lowercase  )
													self.assertIsNotNone(lowercase  )







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







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





								'''simple docstring'''



								pass







			@slow
			def 			__lowerCAmelCase				(			self			: str  ):





								'''simple docstring'''



								UpperCAmelCase			:   Optional[Any]      =				DebertaModel.from_pretrained("microsoft/deberta-base"  )

								UpperCAmelCase			:   Optional[int]      =				torch.tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]  )
								UpperCAmelCase			:   Dict      =				torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]  )
								with torch.no_grad():
													UpperCAmelCase			:   Tuple      =				model(lowercase			,  attention_mask=lowercase  )[0]
								# compare the actual values for a slice.
								UpperCAmelCase			:   int      =				torch.tensor(
								    [[[-0.5_9_8_6, -0.8_0_5_5, -0.8_4_6_2], [1.4_4_8_4, -0.9_3_4_8, -0.8_0_5_9], [0.3_1_2_3, 0.0_0_3_2, -1.4_1_3_1]]]  )
								self.assertTrue(torch.allclose(output[:, 1:4, 1:4]			,  lowercase			,  atol=1E-4  )			,  f"""{output[:, 1:4, 1:4]}"""  )





"""simple docstring"""






# tests directory-specific settings - this file is run automatically
# by pytest before any tests are run

import doctest
import sys
import warnings
from os.path import abspath, dirname, join

import _pytest

from transformers.testing_utils import HfDoctestModule, HfDocTestParser


# allow having multiple repository checkouts and not needing to remember to rerun
# 'pip install -e .[dev]' when switching between checkouts and running tests.
snake_case_   :   Union[str, Any]        =       abspath(join(dirname(__file__), """src"""))
sys.path.insert(1, git_repo_path)

# silence FutureWarning warnings in tests since often we can't act on them until
# they become normal warnings - i.e. the tests still need to test the current functionality
warnings.simplefilter(action="""ignore""", category=FutureWarning)



def   lowercase_	(    _lowercase       :		List[str]	):




					'''simple docstring'''






					config.addinivalue_line(
					    "markers"							,      "is_pt_tf_cross_test: mark test to run only when PT and TF interactions are tested"	)
					config.addinivalue_line(
					    "markers"							,      "is_pt_flax_cross_test: mark test to run only when PT and FLAX interactions are tested"	)
					config.addinivalue_line("markers"							,      "is_pipeline_test: mark test to run only when pipelines are tested"	)
					config.addinivalue_line("markers"							,      "is_staging_test: mark test to run only in the staging environment"	)
					config.addinivalue_line("markers"							,      "accelerate_tests: mark test that require accelerate"	)
					config.addinivalue_line("markers"							,      "tool_tests: mark the tool tests that are run on their specific schedule"	)



def   lowercase_	(    _lowercase       :		List[str]	):




					'''simple docstring'''






					from transformers.testing_utils import pytest_addoption_shared

					pytest_addoption_shared(_lowercase	)



def   lowercase_	(    _lowercase       :		Tuple	):




					'''simple docstring'''






					from transformers.testing_utils import pytest_terminal_summary_main

					UpperCAmelCase			:   Optional[int]      =				terminalreporter.config.getoption("--make-reports"	)
					if make_reports:
										pytest_terminal_summary_main(_lowercase							,      id=_lowercase	)



def   lowercase_	(    _lowercase       :		str							,      _lowercase       :		Dict	):




					'''simple docstring'''






					if exitstatus == 5:
										UpperCAmelCase			:   List[str]      =				0


# Doctest custom flag to ignore output.
snake_case_   :   Union[str, Any]        =       doctest.register_optionflag("""IGNORE_RESULT""")

snake_case_   :   Optional[int]        =       doctest.OutputChecker







class       snake_case__  ( lowerCAmelCase_     ):







			def 			__lowerCAmelCase				(			self			: List[Any]			,  lowercase			: Tuple			,  lowercase			: Optional[Any]			,  lowercase			: int  ):





								'''simple docstring'''



								if IGNORE_RESULT & optionflags:
													return True
								return OutputChecker.check_output(self			,  lowercase			,  lowercase			,  lowercase  )

snake_case_   :   List[str]        =       CustomOutputChecker
snake_case_   :   Optional[Any]        =       HfDoctestModule




snake_case_   :   List[str]        =       HfDocTestParser


