






"""simple docstring"""



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 					_UpperCAmelCase       ( _A							):




					def __init__(				self     :						int    ,						A     :						Tuple    ,						A     :						Dict=13    ,						A     :						Any=7    ,						A     :						Dict=True    ,						A     :						Optional[int]=True    ,						A     :						Optional[Any]=True    ,						A     :						List[Any]=True    ,						A     :						Tuple=99    ,						A     :						List[str]=32    ,						A     :						str=5    ,						A     :						int=4    ,						A     :						int=37    ,						A     :						str="gelu"    ,						A     :						Tuple=0.1    ,						A     :						Optional[int]=0.1    ,						A     :						List[str]=5_12    ,						A     :						Any=16    ,						A     :						Any=2    ,						A     :						Optional[Any]=0.02    ,						A     :						Any=False    ,						A     :						Any=True    ,						A     :						List[Any]="None"    ,						A     :						str=3    ,						A     :						List[Any]=4    ,						A     :						str=None    ,						)  ->  Union[str, Any]:
							lowercase_ : Optional[int]							       =   parent
							lowercase_ : Dict							       =   batch_size
							lowercase_ : Any							       =   seq_length
							lowercase_ : List[Any]							       =   is_training
							lowercase_ : Tuple							       =   use_input_mask
							lowercase_ : str							       =   use_token_type_ids
							lowercase_ : str							       =   use_labels
							lowercase_ : Optional[Any]							       =   vocab_size
							lowercase_ : List[Any]							       =   hidden_size
							lowercase_ : Dict							       =   num_hidden_layers
							lowercase_ : List[Any]							       =   num_attention_heads
							lowercase_ : Tuple							       =   intermediate_size
							lowercase_ : Union[str, Any]							       =   hidden_act
							lowercase_ : Optional[int]							       =   hidden_dropout_prob
							lowercase_ : List[Any]							       =   attention_probs_dropout_prob
							lowercase_ : List[Any]							       =   max_position_embeddings
							lowercase_ : Optional[int]							       =   type_vocab_size
							lowercase_ : int							       =   type_sequence_label_size
							lowercase_ : List[str]							       =   initializer_range
							lowercase_ : int							       =   num_labels
							lowercase_ : List[str]							       =   num_choices
							lowercase_ : int							       =   relative_attention
							lowercase_ : Union[str, Any]							       =   position_biased_input
							lowercase_ : List[str]							       =   pos_att_type
							lowercase_ : str							       =   scope




					def   A  (				self     :						List[Any]   )  ->  List[str]:
							lowercase_ : List[Any]							       =   ids_tensor([self.batch_size, self.seq_length]    ,						self.vocab_size   )

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

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

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

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

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




					def   A  (				self     :						List[str]   )  ->  Any:
							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   A  (				self     :						Dict   )  ->  Tuple:
							lowercase_ : Any							       =   self.get_config()
							lowercase_ : Union[str, Any]							       =   3_00
							return config




					def   A  (				self     :						Tuple    ,						A     :						int   )  ->  str:
							self.parent.assertListEqual(list(result.loss.size()   )    ,						[]   )




					def   A  (				self     :						Optional[int]    ,						A     :						int    ,						A     :						Tuple    ,						A     :						int    ,						A     :						Union[str, Any]    ,						A     :						Any    ,						A     :						Tuple    ,						A     :						List[str]   )  ->  List[str]:
							lowercase_ : List[str]							       =   DebertaModel(config=A   )
							model.to(A   )
							model.eval()
							lowercase_ : str							       =   model(A    ,						attention_mask=A    ,						token_type_ids=A   )[0]
							lowercase_ : Optional[int]							       =   model(A    ,						token_type_ids=A   )[0]
							lowercase_ : List[Any]							       =   model(A   )[0]

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




					def   A  (				self     :						Tuple    ,						A     :						str    ,						A     :						List[str]    ,						A     :						Any    ,						A     :						Optional[Any]    ,						A     :						List[Any]    ,						A     :						int    ,						A     :						List[Any]   )  ->  Optional[int]:
							lowercase_ : str							       =   DebertaForMaskedLM(config=A   )
							model.to(A   )
							model.eval()
							lowercase_ : Any							       =   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   A  (				self     :						Tuple    ,						A     :						List[str]    ,						A     :						Optional[Any]    ,						A     :						Union[str, Any]    ,						A     :						Dict    ,						A     :						Dict    ,						A     :						List[Any]    ,						A     :						List[Any]   )  ->  Optional[int]:
							lowercase_ : Optional[int]							       =   self.num_labels
							lowercase_ : Optional[int]							       =   DebertaForSequenceClassification(A   )
							model.to(A   )
							model.eval()
							lowercase_ : Tuple							       =   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   A  (				self     :						Optional[Any]    ,						A     :						Union[str, Any]    ,						A     :						Optional[Any]    ,						A     :						Union[str, Any]    ,						A     :						Any    ,						A     :						str    ,						A     :						Optional[int]    ,						A     :						Tuple   )  ->  Optional[int]:
							lowercase_ : Tuple							       =   self.num_labels
							lowercase_ : Any							       =   DebertaForTokenClassification(config=A   )
							model.to(A   )
							model.eval()
							lowercase_ : Tuple							       =   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   A  (				self     :						Optional[int]    ,						A     :						Dict    ,						A     :						int    ,						A     :						str    ,						A     :						str    ,						A     :						List[str]    ,						A     :						int    ,						A     :						str   )  ->  Tuple:
							lowercase_ : str							       =   DebertaForQuestionAnswering(config=A   )
							model.to(A   )
							model.eval()
							lowercase_ : Optional[int]							       =   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   A  (				self     :						Optional[int]   )  ->  Any:
							lowercase_ : int							       =   self.prepare_config_and_inputs()
							(
							    (
							    lowercase_
							)       ,					(
							    lowercase_
							)       ,					(
							    lowercase_
							)       ,					(
							    lowercase_
							)       ,					(
							    lowercase_
							)       ,					(
							    lowercase_
							)       ,					(
							    lowercase_
							)       ,					
							) : str							       =   config_and_inputs
							lowercase_ : Optional[Any]							       =   {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
							return config, inputs_dict





@require_torch
class 					_UpperCAmelCase       ( _A   ,						_A   ,						unittest.TestCase							):
					SCREAMING_SNAKE_CASE_		:				Union[str, Any]							    =   (
					    (
					        DebertaModel,
					        DebertaForMaskedLM,
					        DebertaForSequenceClassification,
					        DebertaForTokenClassification,
					        DebertaForQuestionAnswering,
					    )
					    if is_torch_available()
					    else ()
					)
					SCREAMING_SNAKE_CASE_		:				str							    =   (
					    {
					        "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_		:				Tuple							    =   True
					SCREAMING_SNAKE_CASE_		:				Optional[Any]							    =   False
					SCREAMING_SNAKE_CASE_		:				Any							    =   False
					SCREAMING_SNAKE_CASE_		:				Union[str, Any]							    =   False
					SCREAMING_SNAKE_CASE_		:				Tuple							    =   False




					def   A  (				self     :						int   )  ->  Dict:
							lowercase_ : List[Any]							       =   DebertaModelTester(self   )
							lowercase_ : Optional[int]							       =   ConfigTester(self    ,						config_class=A    ,						hidden_size=37   )




					def   A  (				self     :						Dict   )  ->  Any:
							self.config_tester.run_common_tests()




					def   A  (				self     :						int   )  ->  List[str]:
							lowercase_ : Optional[Any]							       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_model(*A   )




					def   A  (				self     :						str   )  ->  Optional[int]:
							lowercase_ : Dict							       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_sequence_classification(*A   )




					def   A  (				self     :						str   )  ->  Tuple:
							lowercase_ : List[str]							       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_masked_lm(*A   )




					def   A  (				self     :						Dict   )  ->  Dict:
							lowercase_ : Dict							       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_question_answering(*A   )




					def   A  (				self     :						List[Any]   )  ->  Dict:
							lowercase_ : Optional[int]							       =   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_token_classification(*A   )






					@slow
					def   A  (				self     :						Optional[int]   )  ->  Union[str, Any]:
							for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowercase_ : List[Any]							       =   DebertaModel.from_pretrained(A   )
									self.assertIsNotNone(A   )





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




					@unittest.skip(reason='''Model not available yet'''   )
					def   A  (				self     :						List[str]   )  ->  Optional[Any]:
							pass






					@slow
					def   A  (				self     :						int   )  ->  Dict:
							lowercase_ : List[str]							       =   DebertaModel.from_pretrained('''microsoft/deberta-base'''   )

							lowercase_ : List[Any]							       =   torch.tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]   )
							lowercase_ : Dict							       =   torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
							with torch.no_grad():
									lowercase_ : Optional[Any]							       =   model(A    ,						attention_mask=A   )[0]
							# compare the actual values for a slice.
							lowercase_ : int							       =   torch.tensor(
							    [[[-0.5986, -0.8055, -0.8462], [1.4484, -0.9348, -0.8059], [0.3123, 0.0032, -1.4131]]]   )
							self.assertTrue(torch.allclose(output[:, 1:4, 1:4]    ,						A    ,						atol=1e-4   )    ,						F'''{output[:, 1:4, 1:4]}'''   )











"""simple docstring"""



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


__A		:       List[Any]  				=		{
    '''configuration_mega''': ['''MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MegaConfig''', '''MegaOnnxConfig'''],
}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__A		:       List[str]  				=		[
						    '''MEGA_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''MegaForCausalLM''',
						    '''MegaForMaskedLM''',
						    '''MegaForMultipleChoice''',
						    '''MegaForQuestionAnswering''',
						    '''MegaForSequenceClassification''',
						    '''MegaForTokenClassification''',
						    '''MegaModel''',
						    '''MegaPreTrainedModel''',
						]

if TYPE_CHECKING:
						from .configuration_mega import MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP, MegaConfig, MegaOnnxConfig

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_mega import (
												    MEGA_PRETRAINED_MODEL_ARCHIVE_LIST,
												    MegaForCausalLM,
												    MegaForMaskedLM,
												    MegaForMultipleChoice,
												    MegaForQuestionAnswering,
												    MegaForSequenceClassification,
												    MegaForTokenClassification,
												    MegaModel,
												    MegaPreTrainedModel,
												)

else:
						import sys

						__A		:       Any  				=		_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)



