


"""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_     (			UpperCamelCase):

	"""simple docstring"""





	def __init__(			self      :    Optional[Any]      ,      UpperCAmelCase__      :    Optional[int]      ,      UpperCAmelCase__      :    Any=1_3      ,      UpperCAmelCase__      :    Dict=7      ,      UpperCAmelCase__      :    List[Any]=True      ,      UpperCAmelCase__      :    Union[str, Any]=True      ,      UpperCAmelCase__      :    Union[str, Any]=True      ,      UpperCAmelCase__      :    Optional[Any]=True      ,      UpperCAmelCase__      :    Union[str, Any]=9_9      ,      UpperCAmelCase__      :    Optional[int]=3_2      ,      UpperCAmelCase__      :    List[Any]=5      ,      UpperCAmelCase__      :    List[str]=4      ,      UpperCAmelCase__      :    Optional[int]=3_7      ,      UpperCAmelCase__      :    Union[str, Any]="gelu"      ,      UpperCAmelCase__      :    str=0.1      ,      UpperCAmelCase__      :    List[str]=0.1      ,      UpperCAmelCase__      :    str=5_1_2      ,      UpperCAmelCase__      :    Optional[Any]=1_6      ,      UpperCAmelCase__      :    Union[str, Any]=2      ,      UpperCAmelCase__      :    Tuple=0.02      ,      UpperCAmelCase__      :    Union[str, Any]=False      ,      UpperCAmelCase__      :    Any=True      ,      UpperCAmelCase__      :    List[str]="None"      ,      UpperCAmelCase__      :    int=3      ,      UpperCAmelCase__      :    Union[str, Any]=4      ,      UpperCAmelCase__      :    str=None      ,      )	->     Optional[Any]:
						__SCREAMING_SNAKE_CASE						       =    parent
						__SCREAMING_SNAKE_CASE						       =    batch_size
						__SCREAMING_SNAKE_CASE						       =    seq_length
						__SCREAMING_SNAKE_CASE						       =    is_training
						__SCREAMING_SNAKE_CASE						       =    use_input_mask
						__SCREAMING_SNAKE_CASE						       =    use_token_type_ids
						__SCREAMING_SNAKE_CASE						       =    use_labels
						__SCREAMING_SNAKE_CASE						       =    vocab_size
						__SCREAMING_SNAKE_CASE						       =    hidden_size
						__SCREAMING_SNAKE_CASE						       =    num_hidden_layers
						__SCREAMING_SNAKE_CASE						       =    num_attention_heads
						__SCREAMING_SNAKE_CASE						       =    intermediate_size
						__SCREAMING_SNAKE_CASE						       =    hidden_act
						__SCREAMING_SNAKE_CASE						       =    hidden_dropout_prob
						__SCREAMING_SNAKE_CASE						       =    attention_probs_dropout_prob
						__SCREAMING_SNAKE_CASE						       =    max_position_embeddings
						__SCREAMING_SNAKE_CASE						       =    type_vocab_size
						__SCREAMING_SNAKE_CASE						       =    type_sequence_label_size
						__SCREAMING_SNAKE_CASE						       =    initializer_range
						__SCREAMING_SNAKE_CASE						       =    num_labels
						__SCREAMING_SNAKE_CASE						       =    num_choices
						__SCREAMING_SNAKE_CASE						       =    relative_attention
						__SCREAMING_SNAKE_CASE						       =    position_biased_input
						__SCREAMING_SNAKE_CASE						       =    pos_att_type
						__SCREAMING_SNAKE_CASE						       =    scope




	def   UpperCAmelCase_				(			self      :    Any       )	->     Any:
						__SCREAMING_SNAKE_CASE						       =    ids_tensor([self.batch_size, self.seq_length]      ,      self.vocab_size       )

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

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

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

						__SCREAMING_SNAKE_CASE						       =    self.get_config()

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




	def   UpperCAmelCase_				(			self      :    int       )	->     Optional[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   UpperCAmelCase_				(			self      :    Dict       )	->     Any:
						__SCREAMING_SNAKE_CASE						       =    self.get_config()
						__SCREAMING_SNAKE_CASE						       =    3_0_0
						return config




	def   UpperCAmelCase_				(			self      :    Any      ,      UpperCAmelCase__      :    Tuple       )	->     Optional[Any]:
						self.parent.assertListEqual(list(result.loss.size()       )      ,      []       )




	def   UpperCAmelCase_				(			self      :    Union[str, Any]      ,      UpperCAmelCase__      :    Optional[Any]      ,      UpperCAmelCase__      :    Union[str, Any]      ,      UpperCAmelCase__      :    Optional[Any]      ,      UpperCAmelCase__      :    Any      ,      UpperCAmelCase__      :    List[str]      ,      UpperCAmelCase__      :    List[str]      ,      UpperCAmelCase__      :    Union[str, Any]       )	->     str:
						__SCREAMING_SNAKE_CASE						       =    DebertaModel(config=UpperCAmelCase__       )
						model.to(UpperCAmelCase__       )
						model.eval()
						__SCREAMING_SNAKE_CASE						       =    model(UpperCAmelCase__      ,      attention_mask=UpperCAmelCase__      ,      token_type_ids=UpperCAmelCase__       )[0]
						__SCREAMING_SNAKE_CASE						       =    model(UpperCAmelCase__      ,      token_type_ids=UpperCAmelCase__       )[0]
						__SCREAMING_SNAKE_CASE						       =    model(UpperCAmelCase__       )[0]

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




	def   UpperCAmelCase_				(			self      :    Tuple      ,      UpperCAmelCase__      :    Tuple      ,      UpperCAmelCase__      :    str      ,      UpperCAmelCase__      :    Any      ,      UpperCAmelCase__      :    Any      ,      UpperCAmelCase__      :    Optional[int]      ,      UpperCAmelCase__      :    Union[str, Any]      ,      UpperCAmelCase__      :    Union[str, Any]       )	->     str:
						__SCREAMING_SNAKE_CASE						       =    DebertaForMaskedLM(config=UpperCAmelCase__       )
						model.to(UpperCAmelCase__       )
						model.eval()
						__SCREAMING_SNAKE_CASE						       =    model(UpperCAmelCase__      ,      attention_mask=UpperCAmelCase__      ,      token_type_ids=UpperCAmelCase__      ,      labels=UpperCAmelCase__       )

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




	def   UpperCAmelCase_				(			self      :    Union[str, Any]      ,      UpperCAmelCase__      :    List[str]      ,      UpperCAmelCase__      :    Optional[int]      ,      UpperCAmelCase__      :    int      ,      UpperCAmelCase__      :    Optional[Any]      ,      UpperCAmelCase__      :    Optional[Any]      ,      UpperCAmelCase__      :    Optional[int]      ,      UpperCAmelCase__      :    Union[str, Any]       )	->     Dict:
						__SCREAMING_SNAKE_CASE						       =    self.num_labels
						__SCREAMING_SNAKE_CASE						       =    DebertaForSequenceClassification(UpperCAmelCase__       )
						model.to(UpperCAmelCase__       )
						model.eval()
						__SCREAMING_SNAKE_CASE						       =    model(UpperCAmelCase__      ,      attention_mask=UpperCAmelCase__      ,      token_type_ids=UpperCAmelCase__      ,      labels=UpperCAmelCase__       )
						self.parent.assertListEqual(list(result.logits.size()       )      ,      [self.batch_size, self.num_labels]       )
						self.check_loss_output(UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    Tuple      ,      UpperCAmelCase__      :    Optional[int]      ,      UpperCAmelCase__      :    str      ,      UpperCAmelCase__      :    int      ,      UpperCAmelCase__      :    str      ,      UpperCAmelCase__      :    List[str]      ,      UpperCAmelCase__      :    int      ,      UpperCAmelCase__      :    Optional[Any]       )	->     List[str]:
						__SCREAMING_SNAKE_CASE						       =    self.num_labels
						__SCREAMING_SNAKE_CASE						       =    DebertaForTokenClassification(config=UpperCAmelCase__       )
						model.to(UpperCAmelCase__       )
						model.eval()
						__SCREAMING_SNAKE_CASE						       =    model(UpperCAmelCase__      ,      attention_mask=UpperCAmelCase__      ,      token_type_ids=UpperCAmelCase__      ,      labels=UpperCAmelCase__       )
						self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.seq_length, self.num_labels)       )




	def   UpperCAmelCase_				(			self      :    Tuple      ,      UpperCAmelCase__      :    Union[str, Any]      ,      UpperCAmelCase__      :    int      ,      UpperCAmelCase__      :    str      ,      UpperCAmelCase__      :    Optional[Any]      ,      UpperCAmelCase__      :    str      ,      UpperCAmelCase__      :    List[str]      ,      UpperCAmelCase__      :    int       )	->     Union[str, Any]:
						__SCREAMING_SNAKE_CASE						       =    DebertaForQuestionAnswering(config=UpperCAmelCase__       )
						model.to(UpperCAmelCase__       )
						model.eval()
						__SCREAMING_SNAKE_CASE						       =    model(
						    UpperCAmelCase__      ,      attention_mask=UpperCAmelCase__      ,      token_type_ids=UpperCAmelCase__      ,      start_positions=UpperCAmelCase__      ,      end_positions=UpperCAmelCase__      ,      )
						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      :    List[Any]       )	->     Union[str, Any]:
						__SCREAMING_SNAKE_CASE						       =    self.prepare_config_and_inputs()
						(
						    (
						    __SCREAMING_SNAKE_CASE
						)       ,  (
						    __SCREAMING_SNAKE_CASE
						)       ,  (
						    __SCREAMING_SNAKE_CASE
						)       ,  (
						    __SCREAMING_SNAKE_CASE
						)       ,  (
						    __SCREAMING_SNAKE_CASE
						)       ,  (
						    __SCREAMING_SNAKE_CASE
						)       ,  (
						    __SCREAMING_SNAKE_CASE
						)       ,  
						)						       =    config_and_inputs
						__SCREAMING_SNAKE_CASE						       =    {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
						return config, inputs_dict




@require_torch
class 			UpperCamelCase_     (			UpperCamelCase      ,  UpperCamelCase      ,  unittest.TestCase):

	"""simple docstring"""





	snake_case__    :  Tuple    =      (
	    (
	        DebertaModel,
	        DebertaForMaskedLM,
	        DebertaForSequenceClassification,
	        DebertaForTokenClassification,
	        DebertaForQuestionAnswering,
	    )
	    if is_torch_available()
	    else ()
	)
	snake_case__    :  Union[str, Any]    =      (
	    {
	        "feature-extraction": DebertaModel,
	        "fill-mask": DebertaForMaskedLM,
	        "question-answering": DebertaForQuestionAnswering,
	        "text-classification": DebertaForSequenceClassification,
	        "token-classification": DebertaForTokenClassification,
	        "zero-shot": DebertaForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)

	snake_case__    :  List[Any]    =      True
	snake_case__    :  List[str]    =      False
	snake_case__    :  str    =      False
	snake_case__    :  List[Any]    =      False
	snake_case__    :  Tuple    =      False




	def   UpperCAmelCase_				(			self      :    Tuple       )	->     Union[str, Any]:
						__SCREAMING_SNAKE_CASE						       =    DebertaModelTester(self       )
						__SCREAMING_SNAKE_CASE						       =    ConfigTester(self      ,      config_class=UpperCAmelCase__      ,      hidden_size=3_7       )




	def   UpperCAmelCase_				(			self      :    List[str]       )	->     int:
						self.config_tester.run_common_tests()




	def   UpperCAmelCase_				(			self      :    Tuple       )	->     Union[str, Any]:
						__SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_model(*UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    str       )	->     Union[str, Any]:
						__SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_sequence_classification(*UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    Any       )	->     str:
						__SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_masked_lm(*UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    List[str]       )	->     List[str]:
						__SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_question_answering(*UpperCAmelCase__       )




	def   UpperCAmelCase_				(			self      :    int       )	->     Any:
						__SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_token_classification(*UpperCAmelCase__       )




	@slow
	def   UpperCAmelCase_				(			self      :    List[Any]       )	->     Optional[Any]:
						for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__SCREAMING_SNAKE_CASE						       =    DebertaModel.from_pretrained(UpperCAmelCase__       )
											self.assertIsNotNone(UpperCAmelCase__       )




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

	"""simple docstring"""





	@unittest.skip(reason="Model not available yet"       )
	def   UpperCAmelCase_				(			self      :    int       )	->     Dict:
						pass




	@slow
	def   UpperCAmelCase_				(			self      :    Optional[int]       )	->     List[Any]:
						__SCREAMING_SNAKE_CASE						       =    DebertaModel.from_pretrained("microsoft/deberta-base"       )

						__SCREAMING_SNAKE_CASE						       =    torch.tensor([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]]       )
						__SCREAMING_SNAKE_CASE						       =    torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]       )
						with torch.no_grad():
											__SCREAMING_SNAKE_CASE						       =    model(UpperCAmelCase__      ,      attention_mask=UpperCAmelCase__       )[0]
						# compare the actual values for a slice.
						__SCREAMING_SNAKE_CASE						       =    torch.tensor(
						    [[[-0.5_986, -0.8_055, -0.8_462], [1.4_484, -0.9_348, -0.8_059], [0.3_123, 0.0_032, -1.4_131]]]       )
						self.assertTrue(torch.allclose(output[:, 1:4, 1:4]      ,      UpperCAmelCase__      ,      atol=1E-4       )      ,      F"""{output[:, 1:4, 1:4]}"""       )




"""simple docstring"""







from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


if is_sentencepiece_available():
		from ..ta.tokenization_ta import TaTokenizer
else:
		from ...utils.dummy_sentencepiece_objects import TaTokenizer

a__    :							Dict            =					TaTokenizer

if is_tokenizers_available():
		from ..ta.tokenization_ta_fast import TaTokenizerFast
else:
		from ...utils.dummy_tokenizers_objects import TaTokenizerFast

a__    :							Any            =					TaTokenizerFast

a__    :							Tuple            =					{'''configuration_mt5''': ['''MT5Config''', '''MT5OnnxConfig''']}

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		a__    :							int            =					[
		    '''MT5EncoderModel''',
		    '''MT5ForConditionalGeneration''',
		    '''MT5ForQuestionAnswering''',
		    '''MT5Model''',
		    '''MT5PreTrainedModel''',
		    '''MT5Stack''',
		]

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		a__    :							str            =					['''TFMT5EncoderModel''', '''TFMT5ForConditionalGeneration''', '''TFMT5Model''']

try:
		if not is_flax_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		a__    :							Union[str, Any]            =					['''FlaxMT5EncoderModel''', '''FlaxMT5ForConditionalGeneration''', '''FlaxMT5Model''']


if TYPE_CHECKING:
		from .configuration_mta import MTaConfig, MTaOnnxConfig

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_mta import (
				    MTaEncoderModel,
				    MTaForConditionalGeneration,
				    MTaForQuestionAnswering,
				    MTaModel,
				    MTaPreTrainedModel,
				    MTaStack,
				)

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_tf_mta import TFMTaEncoderModel, TFMTaForConditionalGeneration, TFMTaModel

		try:
				if not is_flax_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_flax_mta import FlaxMTaEncoderModel, FlaxMTaForConditionalGeneration, FlaxMTaModel

else:
		import sys

		a__    :							str            =					_LazyModule(
		    __name__,
		    globals()['''__file__'''],
		    _import_structure,
		    extra_objects={'''MT5Tokenizer''': MTaTokenizer, '''MT5TokenizerFast''': MTaTokenizerFast},
		    module_spec=__spec__,
		)
