

'''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 					__a   :

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

							"""simple docstring"""



							_lowercase							=     parent
							_lowercase							=     batch_size
							_lowercase							=     seq_length
							_lowercase							=     is_training
							_lowercase							=     use_input_mask
							_lowercase							=     use_labels
							_lowercase							=     vocab_size
							_lowercase							=     hidden_size
							_lowercase							=     num_hidden_layers
							_lowercase							=     num_attention_heads
							_lowercase							=     intermediate_size
							_lowercase							=     hidden_act
							_lowercase							=     hidden_dropout_prob
							_lowercase							=     attention_probs_dropout_prob
							_lowercase							=     max_position_embeddings
							_lowercase							=     type_vocab_size
							_lowercase							=     type_sequence_label_size
							_lowercase							=     initializer_range
							_lowercase							=     num_labels
							_lowercase							=     num_choices
							_lowercase							=     scope

						def 	_UpperCAmelCase							(		self	:						str)						->List[str]:

							"""simple docstring"""



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

							_lowercase							=     None
							if self.use_input_mask:
								_lowercase							=     random_attention_mask([self.batch_size, self.seq_length])

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

							_lowercase							=     self.get_config()

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

						def 	_UpperCAmelCase							(		self	:						Optional[Any])						->Dict:

							"""simple docstring"""



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

							_lowercase							=     None
							if self.use_input_mask:
								_lowercase							=     random_attention_mask([self.batch_size, self.seq_length])
							_lowercase							=     self.get_config()

							return config, input_ids, input_mask

						def 	_UpperCAmelCase							(		self	:						List[str])						->List[Any]:

							"""simple docstring"""



							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 	_UpperCAmelCase							(		self	:						Optional[int]    ,			lowercase__	:						Tuple    ,			lowercase__	:						Union[str, Any]    ,			lowercase__	:						Optional[Any]    ,			lowercase__	:						Dict    ,			lowercase__	:						str    ,			lowercase__	:						str)						->List[Any]:

							"""simple docstring"""



							_lowercase							=     ErnieMModel(config=lowercase__)
							model.to(lowercase__)
							model.eval()
							_lowercase							=     model(lowercase__    ,			return_dict=lowercase__)
							self.parent.assertEqual(result.last_hidden_state.shape    ,			(self.batch_size, self.seq_length, self.hidden_size))

						def 	_UpperCAmelCase							(		self	:						Dict    ,			lowercase__	:						Optional[int]    ,			lowercase__	:						int    ,			lowercase__	:						Optional[int]    ,			lowercase__	:						List[Any]    ,			lowercase__	:						Tuple    ,			lowercase__	:						Dict)						->List[Any]:

							"""simple docstring"""



							_lowercase							=     ErnieMForQuestionAnswering(config=lowercase__)
							model.to(lowercase__)
							model.eval()
							_lowercase							=     model(
							    lowercase__    ,			attention_mask=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 	_UpperCAmelCase							(		self	:						List[str]    ,			lowercase__	:						Dict    ,			lowercase__	:						List[Any]    ,			lowercase__	:						Dict    ,			lowercase__	:						Optional[int]    ,			lowercase__	:						Union[str, Any]    ,			lowercase__	:						Optional[Any])						->int:

							"""simple docstring"""



							_lowercase							=     ErnieMForInformationExtraction(config=lowercase__)
							model.to(lowercase__)
							model.eval()
							_lowercase							=     torch.ones_like(lowercase__    ,			dtype=torch.floataa)
							_lowercase							=     model(
							    lowercase__    ,			attention_mask=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 	_UpperCAmelCase							(		self	:						List[str]    ,			lowercase__	:						Optional[Any]    ,			lowercase__	:						List[str]    ,			lowercase__	:						Any    ,			lowercase__	:						List[Any]    ,			lowercase__	:						Optional[Any]    ,			lowercase__	:						Any)						->Tuple:

							"""simple docstring"""



							_lowercase							=     self.num_labels
							_lowercase							=     ErnieMForSequenceClassification(lowercase__)
							model.to(lowercase__)
							model.eval()
							_lowercase							=     model(lowercase__    ,			attention_mask=lowercase__    ,			labels=lowercase__)
							self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.num_labels))

						def 	_UpperCAmelCase							(		self	:						str    ,			lowercase__	:						Optional[int]    ,			lowercase__	:						int    ,			lowercase__	:						Any    ,			lowercase__	:						Optional[int]    ,			lowercase__	:						int    ,			lowercase__	:						int)						->Optional[Any]:

							"""simple docstring"""



							_lowercase							=     self.num_labels
							_lowercase							=     ErnieMForTokenClassification(config=lowercase__)
							model.to(lowercase__)
							model.eval()
							input_ids.to(lowercase__)
							input_mask.to(lowercase__)
							token_labels.to(lowercase__)

							_lowercase							=     model(lowercase__    ,			attention_mask=lowercase__    ,			labels=lowercase__)

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

						def 	_UpperCAmelCase							(		self	:						Dict    ,			lowercase__	:						List[str]    ,			lowercase__	:						Optional[Any]    ,			lowercase__	:						Dict    ,			lowercase__	:						List[str]    ,			lowercase__	:						List[str]    ,			lowercase__	:						str)						->Dict:

							"""simple docstring"""



							_lowercase							=     self.num_choices
							_lowercase							=     ErnieMForMultipleChoice(config=lowercase__)
							model.to(lowercase__)
							model.eval()
							_lowercase							=     input_ids.unsqueeze(1).expand(-1    ,			self.num_choices    ,			-1).contiguous()
							_lowercase							=     input_mask.unsqueeze(1).expand(-1    ,			self.num_choices    ,			-1).contiguous()
							_lowercase							=     model(
							    lowercase__    ,			attention_mask=lowercase__    ,			labels=lowercase__    ,			)
							self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.num_choices))


						def 	_UpperCAmelCase							(		self	:						Dict)						->Optional[int]:

							"""simple docstring"""



							_lowercase							=     self.prepare_config_and_inputs()
							(
							    (
							    _lowercase
							)		,		(
							    _lowercase
							)		,		(
							    _lowercase
							)		,		(
							    _lowercase
							)		,		(
							    _lowercase
							)		,		(
							    _lowercase
							)		,		
							)							=     config_and_inputs
							_lowercase							=     {"""input_ids""": input_ids, """attention_mask""": input_mask}
							return config, inputs_dict






@require_torch
class 					__a   (		_snake_case							,_snake_case							,unittest.TestCase  ):
						__SCREAMING_SNAKE_CASE     :					int  =      (
						    (
						        ErnieMModel,
						        ErnieMForMultipleChoice,
						        ErnieMForQuestionAnswering,
						        ErnieMForSequenceClassification,
						        ErnieMForTokenClassification,
						    )
						    if is_torch_available()
						    else ()
						)
						__SCREAMING_SNAKE_CASE     :					int  =      ()
						__SCREAMING_SNAKE_CASE     :					int  =      (
						    {
						        'feature-extraction': ErnieMModel,
						        'question-answering': ErnieMForQuestionAnswering,
						        'text-classification': ErnieMForSequenceClassification,
						        'token-classification': ErnieMForTokenClassification,
						        'zero-shot': ErnieMForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						__SCREAMING_SNAKE_CASE     :					Optional[Any]  =      False

						def 	_UpperCAmelCase							(		self	:						int    ,			lowercase__	:						Optional[Any]    ,			lowercase__	:						Optional[Any]    ,			lowercase__	:						Tuple    ,			lowercase__	:						List[str]    ,			lowercase__	:						int)						->Any:

							"""simple docstring"""



							if pipeline_test_casse_name == "QAPipelineTests":
								return True

							return False

						def 	_UpperCAmelCase							(		self	:						Any)						->Any:

							"""simple docstring"""



							_lowercase							=     ErnieMModelTester(self)
							_lowercase							=     ConfigTester(self    ,			config_class=lowercase__    ,			hidden_size=37)

						def 	_UpperCAmelCase							(		self	:						Tuple)						->Tuple:

							"""simple docstring"""



							self.config_tester.run_common_tests()

						def 	_UpperCAmelCase							(		self	:						Any)						->str:

							"""simple docstring"""



							_lowercase							=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowercase__)

						def 	_UpperCAmelCase							(		self	:						int)						->Tuple:

							"""simple docstring"""



							_lowercase							=     self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
								_lowercase							=     type
								self.model_tester.create_and_check_model(*lowercase__)

						def 	_UpperCAmelCase							(		self	:						Dict)						->List[str]:

							"""simple docstring"""



							_lowercase							=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*lowercase__)

						def 	_UpperCAmelCase							(		self	:						List[str])						->List[str]:

							"""simple docstring"""



							_lowercase							=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*lowercase__)

						def 	_UpperCAmelCase							(		self	:						str)						->Optional[int]:

							"""simple docstring"""



							_lowercase							=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*lowercase__)

						def 	_UpperCAmelCase							(		self	:						Optional[Any])						->Optional[int]:

							"""simple docstring"""



							_lowercase							=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_information_extraction(*lowercase__)

						def 	_UpperCAmelCase							(		self	:						Any)						->Dict:

							"""simple docstring"""



							_lowercase							=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*lowercase__)


						@slow
						def 	_UpperCAmelCase							(		self	:						Union[str, Any])						->Union[str, Any]:

							"""simple docstring"""



							for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								_lowercase							=     ErnieMModel.from_pretrained(lowercase__)
								self.assertIsNotNone(lowercase__)






@require_torch
class 					__a   (		unittest.TestCase  ):

						@slow
						def 	_UpperCAmelCase							(		self	:						Dict)						->int:

							"""simple docstring"""



							_lowercase							=     ErnieMModel.from_pretrained("""susnato/ernie-m-base_pytorch""")
							model.eval()
							_lowercase							=     torch.tensor([[0, 1, 2, 3, 4, 5]])
							_lowercase							=     model(lowercase__)[0]

							# TODO Replace vocab size
							_lowercase							=     7_68

							_lowercase							=     torch.Size((1, 6, hidden_size))
							self.assertEqual(output.shape    ,			lowercase__)

							_lowercase							=     torch.tensor(
							    [[[-0.0012, 0.1245, -0.0214], [-0.0742, 0.0244, -0.0771], [-0.0333, 0.1164, -0.1554]]])

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







'''simple docstring'''







from transformers import HfArgumentParser, PyTorchBenchmark, PyTorchBenchmarkArguments






def 					_SCREAMING_SNAKE_CASE		(						):
	_lowercase							=     HfArgumentParser(snake_case_	)
	try:
		_lowercase							=     parser.parse_args_into_dataclasses()[0]
	except ValueError as e:
		_lowercase							=     """Arg --no_{0} is no longer used, please use --no-{0} instead."""
		_lowercase							=     """ """.join(str(snake_case_	).split(""" """	)[:-1]	)
		_lowercase							=     """"""
		_lowercase							=     eval(str(snake_case_	).split(""" """	)[-1]	)
		_lowercase							=     []
		for arg in depreciated_args:
			# arg[2:] removes '--'
			if arg[2:] in PyTorchBenchmarkArguments.deprecated_args:
				# arg[5:] removes '--no_'
				full_error_msg += arg_error_msg.format(arg[5:]	)
			else:
				wrong_args.append(snake_case_	)
		if len(snake_case_	) > 0:
			_lowercase							=     full_error_msg + begin_error_msg + str(snake_case_	)
		raise ValueError(snake_case_	)

	_lowercase							=     PyTorchBenchmark(args=snake_case_	)
	benchmark.run()


if __name__ == "__main__":
							main()




