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




		def __init__(  self					:Optional[int]			,    _SCREAMING_SNAKE_CASE					:Dict			,    _SCREAMING_SNAKE_CASE					:Optional[int]=1_3			,    _SCREAMING_SNAKE_CASE					:Dict=7			,    _SCREAMING_SNAKE_CASE					:Dict=True			,    _SCREAMING_SNAKE_CASE					:str=True			,    _SCREAMING_SNAKE_CASE					:int=True			,    _SCREAMING_SNAKE_CASE					:Optional[Any]=9_9			,    _SCREAMING_SNAKE_CASE					:Optional[int]=3_2			,    _SCREAMING_SNAKE_CASE					:Any=5			,    _SCREAMING_SNAKE_CASE					:Any=4			,    _SCREAMING_SNAKE_CASE					:Tuple=3_7			,    _SCREAMING_SNAKE_CASE					:Tuple="gelu"			,    _SCREAMING_SNAKE_CASE					:Union[str, Any]=0.1			,    _SCREAMING_SNAKE_CASE					:List[Any]=0.1			,    _SCREAMING_SNAKE_CASE					:int=5_1_2			,    _SCREAMING_SNAKE_CASE					:int=1_6			,    _SCREAMING_SNAKE_CASE					:List[Any]=2			,    _SCREAMING_SNAKE_CASE					:str=0.02			,    _SCREAMING_SNAKE_CASE					:int=3			,    _SCREAMING_SNAKE_CASE					:str=4			,    _SCREAMING_SNAKE_CASE					:Union[str, Any]=None			,    )					->			Any:






								'''simple docstring'''






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




		def 	_snake_case	(  self					:Dict)					->			List[Any]:






								'''simple docstring'''






								UpperCamelCase_     :	str					     =     ids_tensor([self.batch_size, self.seq_length]			,    self.vocab_size)

								UpperCamelCase_     :	List[Any]					     =     None
								if self.use_input_mask:
														UpperCamelCase_     :	str					     =     random_attention_mask([self.batch_size, self.seq_length])

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

								UpperCamelCase_     :	Any					     =     self.get_config()

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




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






								'''simple docstring'''






								UpperCamelCase_     :	Tuple					     =     ids_tensor([self.batch_size, self.seq_length]			,    self.vocab_size)

								UpperCamelCase_     :	Optional[Any]					     =     None
								if self.use_input_mask:
														UpperCamelCase_     :	Union[str, Any]					     =     random_attention_mask([self.batch_size, self.seq_length])
								UpperCamelCase_     :	Dict					     =     self.get_config()

								return config, input_ids, input_mask




		def 	_snake_case	(  self					:Union[str, Any])					->			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 	_snake_case	(  self					:Union[str, Any]			,    _SCREAMING_SNAKE_CASE					:Tuple			,    _SCREAMING_SNAKE_CASE					:Union[str, Any]			,    _SCREAMING_SNAKE_CASE					:Optional[Any]			,    _SCREAMING_SNAKE_CASE					:Dict			,    _SCREAMING_SNAKE_CASE					:Union[str, Any]			,    _SCREAMING_SNAKE_CASE					:Optional[Any])					->			Optional[Any]:






								'''simple docstring'''






								UpperCamelCase_     :	List[str]					     =     ErnieMModel(config=UpperCamelCase_)
								model.to(UpperCamelCase_)
								model.eval()
								UpperCamelCase_     :	List[Any]					     =     model(UpperCamelCase_			,    return_dict=UpperCamelCase_)
								self.parent.assertEqual(result.last_hidden_state.shape			,    (self.batch_size, self.seq_length, self.hidden_size))




		def 	_snake_case	(  self					:Union[str, Any]			,    _SCREAMING_SNAKE_CASE					:Union[str, Any]			,    _SCREAMING_SNAKE_CASE					:Optional[Any]			,    _SCREAMING_SNAKE_CASE					:int			,    _SCREAMING_SNAKE_CASE					:List[Any]			,    _SCREAMING_SNAKE_CASE					:List[Any]			,    _SCREAMING_SNAKE_CASE					:Optional[Any])					->			Optional[int]:






								'''simple docstring'''






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






								'''simple docstring'''






								UpperCamelCase_     :	Tuple					     =     ErnieMForInformationExtraction(config=UpperCamelCase_)
								model.to(UpperCamelCase_)
								model.eval()
								UpperCamelCase_     :	Tuple					     =     torch.ones_like(UpperCamelCase_			,    dtype=torch.floataa)
								UpperCamelCase_     :	str					     =     model(
								    UpperCamelCase_			,    attention_mask=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 	_snake_case	(  self					:Optional[int]			,    _SCREAMING_SNAKE_CASE					:Optional[int]			,    _SCREAMING_SNAKE_CASE					:Dict			,    _SCREAMING_SNAKE_CASE					:int			,    _SCREAMING_SNAKE_CASE					:int			,    _SCREAMING_SNAKE_CASE					:Union[str, Any]			,    _SCREAMING_SNAKE_CASE					:Any)					->			int:






								'''simple docstring'''






								UpperCamelCase_     :	Union[str, Any]					     =     self.num_labels
								UpperCamelCase_     :	Tuple					     =     ErnieMForSequenceClassification(UpperCamelCase_)
								model.to(UpperCamelCase_)
								model.eval()
								UpperCamelCase_     :	Union[str, Any]					     =     model(UpperCamelCase_			,    attention_mask=UpperCamelCase_			,    labels=UpperCamelCase_)
								self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.num_labels))




		def 	_snake_case	(  self					:Optional[int]			,    _SCREAMING_SNAKE_CASE					:Any			,    _SCREAMING_SNAKE_CASE					:List[str]			,    _SCREAMING_SNAKE_CASE					:int			,    _SCREAMING_SNAKE_CASE					:Dict			,    _SCREAMING_SNAKE_CASE					:str			,    _SCREAMING_SNAKE_CASE					:List[str])					->			str:






								'''simple docstring'''






								UpperCamelCase_     :	Union[str, Any]					     =     self.num_labels
								UpperCamelCase_     :	List[Any]					     =     ErnieMForTokenClassification(config=UpperCamelCase_)
								model.to(UpperCamelCase_)
								model.eval()
								input_ids.to(UpperCamelCase_)
								input_mask.to(UpperCamelCase_)
								token_labels.to(UpperCamelCase_)

								UpperCamelCase_     :	Optional[Any]					     =     model(UpperCamelCase_			,    attention_mask=UpperCamelCase_			,    labels=UpperCamelCase_)

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




		def 	_snake_case	(  self					:Any			,    _SCREAMING_SNAKE_CASE					:str			,    _SCREAMING_SNAKE_CASE					:Optional[int]			,    _SCREAMING_SNAKE_CASE					:Tuple			,    _SCREAMING_SNAKE_CASE					:Dict			,    _SCREAMING_SNAKE_CASE					:Dict			,    _SCREAMING_SNAKE_CASE					:str)					->			Union[str, Any]:






								'''simple docstring'''






								UpperCamelCase_     :	Union[str, Any]					     =     self.num_choices
								UpperCamelCase_     :	Any					     =     ErnieMForMultipleChoice(config=UpperCamelCase_)
								model.to(UpperCamelCase_)
								model.eval()
								UpperCamelCase_     :	Any					     =     input_ids.unsqueeze(1).expand(-1			,    self.num_choices			,    -1).contiguous()
								UpperCamelCase_     :	int					     =     input_mask.unsqueeze(1).expand(-1			,    self.num_choices			,    -1).contiguous()
								UpperCamelCase_     :	Any					     =     model(
								    UpperCamelCase_			,    attention_mask=UpperCamelCase_			,    labels=UpperCamelCase_			,    )
								self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.num_choices))





		def 	_snake_case	(  self					:Tuple)					->			Optional[int]:






								'''simple docstring'''






								UpperCamelCase_     :	List[Any]					     =     self.prepare_config_and_inputs()
								(
								    UpperCamelCase_   
								)     :	str					     =     config_and_inputs
								UpperCamelCase_     :	Dict					     =     {'''input_ids''': input_ids, '''attention_mask''': input_mask}
								return config, inputs_dict







@require_torch
class     __snake_case			(    a__					,			a__					,			unittest.TestCase	):
		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					:Optional[Any]			,    _SCREAMING_SNAKE_CASE					:str			,    _SCREAMING_SNAKE_CASE					:List[Any]			,    _SCREAMING_SNAKE_CASE					:Tuple			,    _SCREAMING_SNAKE_CASE					:int			,    _SCREAMING_SNAKE_CASE					:Any)					->			Optional[int]:






								'''simple docstring'''






								if pipeline_test_casse_name == "QAPipelineTests":
														return True

								return False




		def 	_snake_case	(  self					:Dict)					->			Tuple:






								'''simple docstring'''






								UpperCamelCase_     :	str					     =     ErnieMModelTester(self)
								UpperCamelCase_     :	Optional[Any]					     =     ConfigTester(self			,    config_class=UpperCamelCase_			,    hidden_size=3_7)




		def 	_snake_case	(  self					:Tuple)					->			Optional[Any]:






								'''simple docstring'''






								self.config_tester.run_common_tests()




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






								'''simple docstring'''






								UpperCamelCase_     :	str					     =     self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*UpperCamelCase_)




		def 	_snake_case	(  self					:Union[str, Any])					->			Optional[Any]:






								'''simple docstring'''






								UpperCamelCase_     :	Optional[Any]					     =     self.model_tester.prepare_config_and_inputs()
								for type in ["absolute", "relative_key", "relative_key_query"]:
														UpperCamelCase_     :	Union[str, Any]					     =     type
														self.model_tester.create_and_check_model(*UpperCamelCase_)




		def 	_snake_case	(  self					:Tuple)					->			List[Any]:






								'''simple docstring'''






								UpperCamelCase_     :	Dict					     =     self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase_)




		def 	_snake_case	(  self					:Dict)					->			int:






								'''simple docstring'''






								UpperCamelCase_     :	int					     =     self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*UpperCamelCase_)




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






								'''simple docstring'''






								UpperCamelCase_     :	Tuple					     =     self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase_)




		def 	_snake_case	(  self					:Tuple)					->			Optional[int]:






								'''simple docstring'''






								UpperCamelCase_     :	List[str]					     =     self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_information_extraction(*UpperCamelCase_)




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






								'''simple docstring'''






								UpperCamelCase_     :	str					     =     self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*UpperCamelCase_)





		@slow
		def 	_snake_case	(  self					:List[str])					->			int:






								'''simple docstring'''






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







@require_torch
class     __snake_case			(    unittest.TestCase	):




		@slow
		def 	_snake_case	(  self					:Dict)					->			Any:






								'''simple docstring'''






								UpperCamelCase_     :	Optional[int]					     =     ErnieMModel.from_pretrained("""susnato/ernie-m-base_pytorch""")
								model.eval()
								UpperCamelCase_     :	Optional[int]					     =     torch.tensor([[0, 1, 2, 3, 4, 5]])
								UpperCamelCase_     :	str					     =     model(UpperCamelCase_)[0]

								# TODO Replace vocab size
								UpperCamelCase_     :	Any					     =     7_6_8

								UpperCamelCase_     :	int					     =     torch.Size((1, 6, hidden_size))
								self.assertEqual(output.shape			,    UpperCamelCase_)

								UpperCamelCase_     :	str					     =     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]			,    UpperCamelCase_			,    atol=1E-3))








"""simple docstring"""





def   _snake_case    (    _snake_case					:      int      ):
      if not isinstance(_snake_case			, _snake_case      ):
            lowerCAmelCase       :						int							= f'''Input value of [number={number}] must be an integer'''
            raise TypeError(_snake_case      )
      if number < 0 or number % 2 == 1:
            return False
      lowerCAmelCase       :						int							= int(number**0.5      )
      return number == number_sqrt * (number_sqrt + 1)


if __name__ == "__main__":
 import doctest

 doctest.testmod()


