


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__:







						'''simple docstring'''




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




						def     lowercase_			(					self				)       ->     Union[str, Any]:
										lowerCAmelCase_			:   Optional[int]     					=					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_			:   str     					=					None
										lowerCAmelCase_			:   int     					=					None
										lowerCAmelCase_			:   str     					=					None
										if self.use_labels:
														lowerCAmelCase_			:   Optional[int]     					=					ids_tensor([self.batch_size] ,    self.type_sequence_label_size				)
														lowerCAmelCase_			:   Any     					=					ids_tensor([self.batch_size, self.seq_length] ,    self.num_labels				)
														lowerCAmelCase_			:   str     					=					ids_tensor([self.batch_size] ,    self.num_choices				)

										lowerCAmelCase_			:   Tuple     					=					self.get_config()

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




						def     lowercase_			(					self				)       ->     Any:
										lowerCAmelCase_			:   Dict     					=					ids_tensor([self.batch_size, self.seq_length] ,    self.vocab_size				)

										lowerCAmelCase_			:   Any     					=					None
										if self.use_input_mask:
														lowerCAmelCase_			:   Union[str, Any]     					=					random_attention_mask([self.batch_size, self.seq_length]				)
										lowerCAmelCase_			:   int     					=					self.get_config()

										return config, input_ids, input_mask




						def     lowercase_			(					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     lowercase_			(					self ,    __lowercase ,    __lowercase ,    __lowercase ,    __lowercase ,    __lowercase ,    __lowercase				)       ->     Dict:
										lowerCAmelCase_			:   Any     					=					ErnieMModel(config=__lowercase				)
										model.to(__lowercase				)
										model.eval()
										lowerCAmelCase_			:   str     					=					model(__lowercase ,    return_dict=__lowercase				)
										self.parent.assertEqual(result.last_hidden_state.shape ,    (self.batch_size, self.seq_length, self.hidden_size)				)




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




						def     lowercase_			(					self ,    __lowercase ,    __lowercase ,    __lowercase ,    __lowercase ,    __lowercase ,    __lowercase				)       ->     Any:
										lowerCAmelCase_			:   Tuple     					=					self.num_labels
										lowerCAmelCase_			:   List[str]     					=					ErnieMForTokenClassification(config=__lowercase				)
										model.to(__lowercase				)
										model.eval()
										input_ids.to(__lowercase				)
										input_mask.to(__lowercase				)
										token_labels.to(__lowercase				)

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

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




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




						def     lowercase_			(					self				)       ->     Dict:
										lowerCAmelCase_			:   Tuple     					=					self.prepare_config_and_inputs()
										(
										    (
										    lowerCAmelCase_
										)					,	(
										    lowerCAmelCase_
										)					,	(
										    lowerCAmelCase_
										)					,	(
										    lowerCAmelCase_
										)					,	(
										    lowerCAmelCase_
										)					,	(
										    lowerCAmelCase_
										)					,	
										)			:   List[Any]     					=					config_and_inputs
										lowerCAmelCase_			:   Tuple     					=					{'''input_ids''': input_ids, '''attention_mask''': input_mask}
										return config, inputs_dict







@require_torch
class 					snake_case__(				UpperCAmelCase__,    UpperCAmelCase__,    unittest.TestCase				):







						'''simple docstring'''




						SCREAMING_SNAKE_CASE__   :	Dict       =				(
						    (
						        ErnieMModel,
						        ErnieMForMultipleChoice,
						        ErnieMForQuestionAnswering,
						        ErnieMForSequenceClassification,
						        ErnieMForTokenClassification,
						    )
						    if is_torch_available()
						    else ()
						)
						SCREAMING_SNAKE_CASE__   :	Union[str, Any]       =				()
						SCREAMING_SNAKE_CASE__   :	str       =				(
						    {
						        """feature-extraction""": ErnieMModel,
						        """question-answering""": ErnieMForQuestionAnswering,
						        """text-classification""": ErnieMForSequenceClassification,
						        """token-classification""": ErnieMForTokenClassification,
						        """zero-shot""": ErnieMForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						SCREAMING_SNAKE_CASE__   :	Tuple       =				False




						def     lowercase_			(					self ,    __lowercase ,    __lowercase ,    __lowercase ,    __lowercase ,    __lowercase				)       ->     Optional[Any]:
										if pipeline_test_casse_name == "QAPipelineTests":
														return True

										return False




						def     lowercase_			(					self				)       ->     Optional[int]:
										lowerCAmelCase_			:   Union[str, Any]     					=					ErnieMModelTester(self				)
										lowerCAmelCase_			:   List[Any]     					=					ConfigTester(self ,    config_class=__lowercase ,    hidden_size=3_7				)




						def     lowercase_			(					self				)       ->     Any:
										self.config_tester.run_common_tests()




						def     lowercase_			(					self				)       ->     int:
										lowerCAmelCase_			:   int     					=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*__lowercase				)




						def     lowercase_			(					self				)       ->     Optional[int]:
										lowerCAmelCase_			:   int     					=					self.model_tester.prepare_config_and_inputs()
										for type in ["absolute", "relative_key", "relative_key_query"]:
														lowerCAmelCase_			:   Tuple     					=					type
														self.model_tester.create_and_check_model(*__lowercase				)




						def     lowercase_			(					self				)       ->     Optional[int]:
										lowerCAmelCase_			:   Any     					=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_multiple_choice(*__lowercase				)




						def     lowercase_			(					self				)       ->     int:
										lowerCAmelCase_			:   Any     					=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*__lowercase				)




						def     lowercase_			(					self				)       ->     Dict:
										lowerCAmelCase_			:   Dict     					=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*__lowercase				)




						def     lowercase_			(					self				)       ->     Tuple:
										lowerCAmelCase_			:   int     					=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_information_extraction(*__lowercase				)




						def     lowercase_			(					self				)       ->     Union[str, Any]:
										lowerCAmelCase_			:   Union[str, Any]     					=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*__lowercase				)




						@slow
						def     lowercase_			(					self				)       ->     Tuple:
										for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														lowerCAmelCase_			:   int     					=					ErnieMModel.from_pretrained(__lowercase				)
														self.assertIsNotNone(__lowercase				)







@require_torch
class 					snake_case__(				unittest.TestCase				):







						'''simple docstring'''




						@slow
						def     lowercase_			(					self				)       ->     Tuple:
										lowerCAmelCase_			:   List[str]     					=					ErnieMModel.from_pretrained('''susnato/ernie-m-base_pytorch'''				)
										model.eval()
										lowerCAmelCase_			:   Union[str, Any]     					=					torch.tensor([[0, 1, 2, 3, 4, 5]]				)
										lowerCAmelCase_			:   Tuple     					=					model(__lowercase				)[0]

										# TODO Replace vocab size
										lowerCAmelCase_			:   int     					=					7_6_8

										lowerCAmelCase_			:   Dict     					=					torch.Size((1, 6, hidden_size)				)
										self.assertEqual(output.shape ,    __lowercase				)

										lowerCAmelCase_			:   Union[str, Any]     					=					torch.tensor(
										    [[[-0.00_12, 0.12_45, -0.02_14], [-0.07_42, 0.02_44, -0.07_71], [-0.03_33, 0.11_64, -0.15_54]]]				)

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



def       lowerCAmelCase			(		lowerCAmelCase_			)->			int:
				lowerCAmelCase_			:   Optional[int]     					=					len(lowerCAmelCase_			)
				if n in (0, 1):
								return 0
				return sum(query * (n - i - 1) for i, query in enumerate(sorted(lowerCAmelCase_			)			)			)


if __name__ == "__main__":
						import doctest

						doctest.testmod()