





'''simple docstring'''





import unittest

from transformers import ErnieMConfig, is_torch_available
from transformers.testing_utils import require_torch, 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 (
			    ErnieMForInformationExtraction,
			    ErnieMForMultipleChoice,
			    ErnieMForQuestionAnswering,
			    ErnieMForSequenceClassification,
			    ErnieMForTokenClassification,
			    ErnieMModel,
			)
			from transformers.models.ernie_m.modeling_ernie_m import ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST


class 			__lowerCAmelCase				:


	'''simple docstring'''





	def __init__(	self:	Optional[Any],				lowerCamelCase_:	List[Any],				lowerCamelCase_:	Dict=13,				lowerCamelCase_:	int=7,				lowerCamelCase_:	Optional[int]=True,				lowerCamelCase_:	str=True,				lowerCamelCase_:	Optional[int]=True,				lowerCamelCase_:	Tuple=99,				lowerCamelCase_:	Tuple=32,				lowerCamelCase_:	Any=5,				lowerCamelCase_:	Optional[Any]=4,				lowerCamelCase_:	int=37,				lowerCamelCase_:	List[str]="gelu",				lowerCamelCase_:	List[Any]=0.1,				lowerCamelCase_:	Dict=0.1,				lowerCamelCase_:	Any=512,				lowerCamelCase_:	Union[str, Any]=16,				lowerCamelCase_:	Union[str, Any]=2,				lowerCamelCase_:	str=0.0_2,				lowerCamelCase_:	int=3,				lowerCamelCase_:	List[Any]=4,				lowerCamelCase_:	Tuple=None,				):
			lowercase__				:			Union[str, Any]            =		parent
			lowercase__				:			Optional[int]            =		batch_size
			lowercase__				:			Union[str, Any]            =		seq_length
			lowercase__				:			Union[str, Any]            =		is_training
			lowercase__				:			Optional[Any]            =		use_input_mask
			lowercase__				:			Union[str, Any]            =		use_labels
			lowercase__				:			int            =		vocab_size
			lowercase__				:			int            =		hidden_size
			lowercase__				:			Dict            =		num_hidden_layers
			lowercase__				:			Union[str, Any]            =		num_attention_heads
			lowercase__				:			int            =		intermediate_size
			lowercase__				:			Union[str, Any]            =		hidden_act
			lowercase__				:			Dict            =		hidden_dropout_prob
			lowercase__				:			Optional[int]            =		attention_probs_dropout_prob
			lowercase__				:			Tuple            =		max_position_embeddings
			lowercase__				:			Dict            =		type_vocab_size
			lowercase__				:			Dict            =		type_sequence_label_size
			lowercase__				:			Union[str, Any]            =		initializer_range
			lowercase__				:			Any            =		num_labels
			lowercase__				:			List[Any]            =		num_choices
			lowercase__				:			str            =		scope

	def       snake_case__(	self:	str ):
			lowercase__				:			List[str]            =		ids_tensor([self.batch_size, self.seq_length],				self.vocab_size )

			lowercase__				:			str            =		None
			if self.use_input_mask:
					lowercase__				:			Tuple            =		random_attention_mask([self.batch_size, self.seq_length] )

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

			lowercase__				:			Dict            =		self.get_config()

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

	def       snake_case__(	self:	Union[str, Any] ):
			lowercase__				:			List[Any]            =		ids_tensor([self.batch_size, self.seq_length],				self.vocab_size )

			lowercase__				:			Optional[int]            =		None
			if self.use_input_mask:
					lowercase__				:			Tuple            =		random_attention_mask([self.batch_size, self.seq_length] )
			lowercase__				:			Optional[Any]            =		self.get_config()

			return config, input_ids, input_mask

	def       snake_case__(	self:	Any ):
			return ErnieMConfig(
			    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,				)

	def       snake_case__(	self:	int,				lowerCamelCase_:	Union[str, Any],				lowerCamelCase_:	Optional[int],				lowerCamelCase_:	Any,				lowerCamelCase_:	List[Any],				lowerCamelCase_:	int,				lowerCamelCase_:	Any ):
			lowercase__				:			str            =		ErnieMModel(config=_A )
			model.to(_A )
			model.eval()
			lowercase__				:			List[str]            =		model(_A,				return_dict=_A )
			self.parent.assertEqual(result.last_hidden_state.shape,				(self.batch_size, self.seq_length, self.hidden_size) )

	def       snake_case__(	self:	Union[str, Any],				lowerCamelCase_:	List[str],				lowerCamelCase_:	List[Any],				lowerCamelCase_:	Optional[int],				lowerCamelCase_:	Tuple,				lowerCamelCase_:	Dict,				lowerCamelCase_:	List[Any] ):
			lowercase__				:			Any            =		ErnieMForQuestionAnswering(config=_A )
			model.to(_A )
			model.eval()
			lowercase__				:			List[Any]            =		model(
			    _A,				attention_mask=_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       snake_case__(	self:	Optional[Any],				lowerCamelCase_:	Union[str, Any],				lowerCamelCase_:	str,				lowerCamelCase_:	List[str],				lowerCamelCase_:	Any,				lowerCamelCase_:	Any,				lowerCamelCase_:	str ):
			lowercase__				:			List[Any]            =		ErnieMForInformationExtraction(config=_A )
			model.to(_A )
			model.eval()
			lowercase__				:			Optional[int]            =		torch.ones_like(_A,				dtype=torch.floataa )
			lowercase__				:			Dict            =		model(
			    _A,				attention_mask=_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       snake_case__(	self:	int,				lowerCamelCase_:	Union[str, Any],				lowerCamelCase_:	str,				lowerCamelCase_:	Optional[Any],				lowerCamelCase_:	Optional[Any],				lowerCamelCase_:	List[str],				lowerCamelCase_:	List[Any] ):
			lowercase__				:			List[str]            =		self.num_labels
			lowercase__				:			int            =		ErnieMForSequenceClassification(_A )
			model.to(_A )
			model.eval()
			lowercase__				:			str            =		model(_A,				attention_mask=_A,				labels=_A )
			self.parent.assertEqual(result.logits.shape,				(self.batch_size, self.num_labels) )

	def       snake_case__(	self:	Union[str, Any],				lowerCamelCase_:	List[str],				lowerCamelCase_:	Any,				lowerCamelCase_:	Tuple,				lowerCamelCase_:	Dict,				lowerCamelCase_:	str,				lowerCamelCase_:	List[str] ):
			lowercase__				:			Optional[Any]            =		self.num_labels
			lowercase__				:			Optional[int]            =		ErnieMForTokenClassification(config=_A )
			model.to(_A )
			model.eval()
			input_ids.to(_A )
			input_mask.to(_A )
			token_labels.to(_A )

			lowercase__				:			List[Any]            =		model(_A,				attention_mask=_A,				labels=_A )

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

	def       snake_case__(	self:	Dict,				lowerCamelCase_:	Optional[int],				lowerCamelCase_:	Optional[Any],				lowerCamelCase_:	str,				lowerCamelCase_:	int,				lowerCamelCase_:	List[str],				lowerCamelCase_:	Dict ):
			lowercase__				:			List[str]            =		self.num_choices
			lowercase__				:			Any            =		ErnieMForMultipleChoice(config=_A )
			model.to(_A )
			model.eval()
			lowercase__				:			List[Any]            =		input_ids.unsqueeze(1 ).expand(-1,				self.num_choices,				-1 ).contiguous()
			lowercase__				:			Tuple            =		input_mask.unsqueeze(1 ).expand(-1,				self.num_choices,				-1 ).contiguous()
			lowercase__				:			Union[str, Any]            =		model(
			    _A,				attention_mask=_A,				labels=_A,				)
			self.parent.assertEqual(result.logits.shape,				(self.batch_size, self.num_choices) )





	def       snake_case__(	self:	List[Any] ):
			lowercase__				:			Optional[int]            =		self.prepare_config_and_inputs()
			(
			    lowercase__       
			)				:			int            =		config_and_inputs
			lowercase__				:			str            =		{'input_ids': input_ids, 'attention_mask': input_mask}
			return config, inputs_dict


@require_torch
class 			__lowerCAmelCase				(					lowercase__      ,						lowercase__      ,						unittest.TestCase			):


	'''simple docstring'''





	_A       	=							(
	    (
	        ErnieMModel,
	        ErnieMForMultipleChoice,
	        ErnieMForQuestionAnswering,
	        ErnieMForSequenceClassification,
	        ErnieMForTokenClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	_A       	=							()
	_A       	=							(
	    {
	        """feature-extraction""": ErnieMModel,
	        """question-answering""": ErnieMForQuestionAnswering,
	        """text-classification""": ErnieMForSequenceClassification,
	        """token-classification""": ErnieMForTokenClassification,
	        """zero-shot""": ErnieMForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	_A       	=							False

	def       snake_case__(	self:	List[str],				lowerCamelCase_:	Optional[int],				lowerCamelCase_:	Any,				lowerCamelCase_:	Optional[int],				lowerCamelCase_:	str,				lowerCamelCase_:	List[str] ):
			if pipeline_test_casse_name == "QAPipelineTests":
					return True

			return False

	def       snake_case__(	self:	Optional[Any] ):
			lowercase__				:			Any            =		ErnieMModelTester(self )
			lowercase__				:			Optional[int]            =		ConfigTester(self,				config_class=_A,				hidden_size=37 )

	def       snake_case__(	self:	List[Any] ):
			self.config_tester.run_common_tests()

	def       snake_case__(	self:	int ):
			lowercase__				:			Union[str, Any]            =		self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*_A )

	def       snake_case__(	self:	Optional[Any] ):
			lowercase__				:			Tuple            =		self.model_tester.prepare_config_and_inputs()
			for type in ["absolute", "relative_key", "relative_key_query"]:
					lowercase__				:			int            =		type
					self.model_tester.create_and_check_model(*_A )

	def       snake_case__(	self:	int ):
			lowercase__				:			Optional[int]            =		self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_multiple_choice(*_A )

	def       snake_case__(	self:	List[Any] ):
			lowercase__				:			str            =		self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_question_answering(*_A )

	def       snake_case__(	self:	Optional[Any] ):
			lowercase__				:			List[str]            =		self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_sequence_classification(*_A )

	def       snake_case__(	self:	Dict ):
			lowercase__				:			int            =		self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_information_extraction(*_A )

	def       snake_case__(	self:	Dict ):
			lowercase__				:			Dict            =		self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_token_classification(*_A )





	@slow
	def       snake_case__(	self:	int ):
			for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					lowercase__				:			Dict            =		ErnieMModel.from_pretrained(_A )
					self.assertIsNotNone(_A )


@require_torch
class 			__lowerCAmelCase				(					unittest.TestCase			):


	'''simple docstring'''





	@slow
	def       snake_case__(	self:	int ):
			lowercase__				:			Any            =		ErnieMModel.from_pretrained('susnato/ernie-m-base_pytorch' )
			model.eval()
			lowercase__				:			Optional[int]            =		torch.tensor([[0, 1, 2, 3, 4, 5]] )
			lowercase__				:			List[str]            =		model(_A )[0]

			# TODO Replace vocab size
			lowercase__				:			int            =		768

			lowercase__				:			Optional[int]            =		torch.Size((1, 6, hidden_size) )
			self.assertEqual(output.shape,				_A )

			lowercase__				:			Tuple            =		torch.tensor(
			    [[[-0.0_0_1_2, 0.1_2_4_5, -0.0_2_1_4], [-0.0_7_4_2, 0.0_2_4_4, -0.0_7_7_1], [-0.0_3_3_3, 0.1_1_6_4, -0.1_5_5_4]]] )

			self.assertTrue(torch.allclose(output[:, :3, :3],				_A,				atol=1E-3 ) )









from typing import TYPE_CHECKING

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


__UpperCamelCase:  Dict   =   {
    """configuration_deberta_v2""": ["""DEBERTA_V2_PRETRAINED_CONFIG_ARCHIVE_MAP""", """DebertaV2Config""", """DebertaV2OnnxConfig"""],
    """tokenization_deberta_v2""": ["""DebertaV2Tokenizer"""],
}

try:
			if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			__UpperCamelCase:  List[Any]   =   ["""DebertaV2TokenizerFast"""]

try:
			if not is_tf_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			__UpperCamelCase:  str   =   [
			    """TF_DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST""",
			    """TFDebertaV2ForMaskedLM""",
			    """TFDebertaV2ForQuestionAnswering""",
			    """TFDebertaV2ForSequenceClassification""",
			    """TFDebertaV2ForTokenClassification""",
			    """TFDebertaV2Model""",
			    """TFDebertaV2PreTrainedModel""",
			]

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			__UpperCamelCase:  Tuple   =   [
			    """DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST""",
			    """DebertaV2ForMaskedLM""",
			    """DebertaV2ForMultipleChoice""",
			    """DebertaV2ForQuestionAnswering""",
			    """DebertaV2ForSequenceClassification""",
			    """DebertaV2ForTokenClassification""",
			    """DebertaV2Model""",
			    """DebertaV2PreTrainedModel""",
			]


if TYPE_CHECKING:
			from .configuration_deberta_va import (
			    DEBERTA_V2_PRETRAINED_CONFIG_ARCHIVE_MAP,
			    DebertaVaConfig,
			    DebertaVaOnnxConfig,
			)
			from .tokenization_deberta_va import DebertaVaTokenizer

			try:
						if not is_tokenizers_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .tokenization_deberta_va_fast import DebertaVaTokenizerFast

			try:
						if not is_tf_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_tf_deberta_va import (
						    TF_DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST,
						    TFDebertaVaForMaskedLM,
						    TFDebertaVaForQuestionAnswering,
						    TFDebertaVaForSequenceClassification,
						    TFDebertaVaForTokenClassification,
						    TFDebertaVaModel,
						    TFDebertaVaPreTrainedModel,
						)

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_deberta_va import (
						    DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST,
						    DebertaVaForMaskedLM,
						    DebertaVaForMultipleChoice,
						    DebertaVaForQuestionAnswering,
						    DebertaVaForSequenceClassification,
						    DebertaVaForTokenClassification,
						    DebertaVaModel,
						    DebertaVaPreTrainedModel,
						)

else:
			import sys

			__UpperCamelCase:  Optional[Any]   =   _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


