
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   : Optional[int]       ,	a   : Optional[int]       ,	a   : int=13       ,	a   : int=7       ,	a   : int=True       ,	a   : Optional[Any]=True       ,	a   : Tuple=True       ,	a   : Dict=99       ,	a   : Dict=32       ,	a   : Tuple=5       ,	a   : Dict=4       ,	a   : Dict=37       ,	a   : List[Any]="gelu"       ,	a   : Union[str, Any]=0.1       ,	a   : Optional[Any]=0.1       ,	a   : List[Any]=512       ,	a   : List[Any]=16       ,	a   : Dict=2       ,	a   : Union[str, Any]=0.0_2       ,	a   : Optional[int]=3       ,	a   : int=4       ,	a   : List[Any]=None       ,	):






							'''simple docstring'''







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






				def 				_lowerCamelCase						(		self   : List[Any]  ):






							'''simple docstring'''







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

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

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

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

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






				def 				_lowerCamelCase						(		self   : Optional[int]  ):






							'''simple docstring'''







							lowerCAmelCase__ : Union[str, Any]   =						ids_tensor([self.batch_size, self.seq_length]       ,	self.vocab_size  )

							lowerCAmelCase__ : Optional[int]   =						None
							if self.use_input_mask:
										lowerCAmelCase__ : List[str]   =						random_attention_mask([self.batch_size, self.seq_length]  )
							lowerCAmelCase__ : Tuple   =						self.get_config()

							return config, input_ids, input_mask






				def 				_lowerCamelCase						(		self   : int  ):






							'''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 				_lowerCamelCase						(		self   : int       ,	a   : Optional[Any]       ,	a   : str       ,	a   : Union[str, Any]       ,	a   : Tuple       ,	a   : Optional[int]       ,	a   : List[str]  ):






							'''simple docstring'''







							lowerCAmelCase__ : Dict   =						ErnieMModel(config=snake_case__  )
							model.to(snake_case__  )
							model.eval()
							lowerCAmelCase__ : int   =						model(snake_case__       ,	return_dict=snake_case__  )
							self.parent.assertEqual(result.last_hidden_state.shape       ,	(self.batch_size, self.seq_length, self.hidden_size)  )






				def 				_lowerCamelCase						(		self   : Any       ,	a   : Dict       ,	a   : Optional[Any]       ,	a   : Dict       ,	a   : List[Any]       ,	a   : Tuple       ,	a   : int  ):






							'''simple docstring'''







							lowerCAmelCase__ : Union[str, Any]   =						ErnieMForQuestionAnswering(config=snake_case__  )
							model.to(snake_case__  )
							model.eval()
							lowerCAmelCase__ : Optional[Any]   =						model(
							    snake_case__       ,	attention_mask=snake_case__       ,	start_positions=snake_case__       ,	end_positions=snake_case__       ,	)
							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 				_lowerCamelCase						(		self   : str       ,	a   : Optional[Any]       ,	a   : List[Any]       ,	a   : List[Any]       ,	a   : Any       ,	a   : Union[str, Any]       ,	a   : List[Any]  ):






							'''simple docstring'''







							lowerCAmelCase__ : Tuple   =						ErnieMForInformationExtraction(config=snake_case__  )
							model.to(snake_case__  )
							model.eval()
							lowerCAmelCase__ : Tuple   =						torch.ones_like(snake_case__       ,	dtype=torch.floataa  )
							lowerCAmelCase__ : Dict   =						model(
							    snake_case__       ,	attention_mask=snake_case__       ,	start_positions=snake_case__       ,	end_positions=snake_case__       ,	)
							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 				_lowerCamelCase						(		self   : Tuple       ,	a   : Optional[Any]       ,	a   : Dict       ,	a   : Tuple       ,	a   : List[str]       ,	a   : Union[str, Any]       ,	a   : int  ):






							'''simple docstring'''







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






				def 				_lowerCamelCase						(		self   : str       ,	a   : Optional[Any]       ,	a   : Union[str, Any]       ,	a   : List[Any]       ,	a   : List[str]       ,	a   : Dict       ,	a   : Optional[Any]  ):






							'''simple docstring'''







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

							lowerCAmelCase__ : Union[str, Any]   =						model(snake_case__       ,	attention_mask=snake_case__       ,	labels=snake_case__  )

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






				def 				_lowerCamelCase						(		self   : Any       ,	a   : List[Any]       ,	a   : str       ,	a   : str       ,	a   : Union[str, Any]       ,	a   : Optional[int]       ,	a   : List[Any]  ):






							'''simple docstring'''







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






				def 				_lowerCamelCase						(		self   : int  ):






							'''simple docstring'''







							lowerCAmelCase__ : List[str]   =						self.prepare_config_and_inputs()
							(
							    lowerCAmelCase__					
							) : int   =						config_and_inputs
							lowerCAmelCase__ : int   =						{"""input_ids""": input_ids, """attention_mask""": input_mask}
							return config, inputs_dict







@require_torch
class 	A__				(	_lowercase		,  _lowercase		,  unittest.TestCase      ):
				lowercase            =    (
				    (
				        ErnieMModel,
				        ErnieMForMultipleChoice,
				        ErnieMForQuestionAnswering,
				        ErnieMForSequenceClassification,
				        ErnieMForTokenClassification,
				    )
				    if is_torch_available()
				    else ()
				)
				lowercase            =    ()
				lowercase            =    (
				    {
				        'feature-extraction': ErnieMModel,
				        'question-answering': ErnieMForQuestionAnswering,
				        'text-classification': ErnieMForSequenceClassification,
				        'token-classification': ErnieMForTokenClassification,
				        'zero-shot': ErnieMForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				lowercase            =    False






				def 				_lowerCamelCase						(		self   : Dict       ,	a   : str       ,	a   : Any       ,	a   : Dict       ,	a   : Union[str, Any]       ,	a   : List[Any]  ):






							'''simple docstring'''







							if pipeline_test_casse_name == "QAPipelineTests":
										return True

							return False






				def 				_lowerCamelCase						(		self   : List[str]  ):






							'''simple docstring'''







							lowerCAmelCase__ : Union[str, Any]   =						ErnieMModelTester(self  )
							lowerCAmelCase__ : Any   =						ConfigTester(self       ,	config_class=snake_case__       ,	hidden_size=37  )






				def 				_lowerCamelCase						(		self   : Any  ):






							'''simple docstring'''







							self.config_tester.run_common_tests()






				def 				_lowerCamelCase						(		self   : Optional[Any]  ):






							'''simple docstring'''







							lowerCAmelCase__ : Dict   =						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*snake_case__  )






				def 				_lowerCamelCase						(		self   : str  ):






							'''simple docstring'''







							lowerCAmelCase__ : Dict   =						self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
										lowerCAmelCase__ : int   =						type
										self.model_tester.create_and_check_model(*snake_case__  )






				def 				_lowerCamelCase						(		self   : str  ):






							'''simple docstring'''







							lowerCAmelCase__ : Dict   =						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*snake_case__  )






				def 				_lowerCamelCase						(		self   : int  ):






							'''simple docstring'''







							lowerCAmelCase__ : Union[str, Any]   =						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*snake_case__  )






				def 				_lowerCamelCase						(		self   : Dict  ):






							'''simple docstring'''







							lowerCAmelCase__ : int   =						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*snake_case__  )






				def 				_lowerCamelCase						(		self   : str  ):






							'''simple docstring'''







							lowerCAmelCase__ : Tuple   =						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_information_extraction(*snake_case__  )






				def 				_lowerCamelCase						(		self   : Dict  ):






							'''simple docstring'''







							lowerCAmelCase__ : Any   =						self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*snake_case__  )






				@slow
				def 				_lowerCamelCase						(		self   : Any  ):






							'''simple docstring'''







							for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowerCAmelCase__ : Tuple   =						ErnieMModel.from_pretrained(snake_case__  )
										self.assertIsNotNone(snake_case__  )







@require_torch
class 	A__				(	unittest.TestCase      ):






				@slow
				def 				_lowerCamelCase						(		self   : Tuple  ):






							'''simple docstring'''







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

							# TODO Replace vocab size
							lowerCAmelCase__ : Union[str, Any]   =						768

							lowerCAmelCase__ : List[Any]   =						torch.Size((1, 6, hidden_size)  )
							self.assertEqual(output.shape       ,	snake_case__  )

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

"""simple docstring"""
from transformers import ProcessorMixin




class  _snake_case	(    _lowercase							):
							SCREAMING_SNAKE_CASE		  =    '''AutoFeatureExtractor'''
							SCREAMING_SNAKE_CASE		  =    '''AutoTokenizer'''
