






"""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     __UpperCAmelCase:




						"""simple docstring"""




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




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      Union[str, Any]=       ids_tensor([self.batch_size, self.seq_length]       ,  self.vocab_size			)

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

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

												lowercase__				:      Optional[Any]=       self.get_config()

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




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      Tuple=       ids_tensor([self.batch_size, self.seq_length]       ,  self.vocab_size			)

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

												return config, input_ids, input_mask




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




						def 		UpperCAmelCase_       (   self       ,  snake_case__       ,  snake_case__       ,  snake_case__       ,  snake_case__       ,  snake_case__       ,  snake_case__			):
												'''simple docstring'''
												lowercase__				:      int=       self.num_labels
												lowercase__				:      Optional[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__			)

												lowercase__				:      Tuple=       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 		UpperCAmelCase_       (   self       ,  snake_case__       ,  snake_case__       ,  snake_case__       ,  snake_case__       ,  snake_case__       ,  snake_case__			):
												'''simple docstring'''
												lowercase__				:      Optional[int]=       self.num_choices
												lowercase__				:      Tuple=       ErnieMForMultipleChoice(config=snake_case__			)
												model.to(snake_case__			)
												model.eval()
												lowercase__				:      Any=       input_ids.unsqueeze(1			).expand(-1       ,  self.num_choices       ,  -1			).contiguous()
												lowercase__				:      List[str]=       input_mask.unsqueeze(1			).expand(-1       ,  self.num_choices       ,  -1			).contiguous()
												lowercase__				:      Dict=       model(
												    snake_case__       ,  attention_mask=snake_case__       ,  labels=snake_case__       ,  )
												self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.num_choices)			)




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      Optional[int]=       self.prepare_config_and_inputs()
												(
												    (
												    lowercase__				
												),	(
												    lowercase__				
												),	(
												    lowercase__				
												),	(
												    lowercase__				
												),	(
												    lowercase__				
												),	(
												    lowercase__				
												),					
												)				:      List[Any]=       config_and_inputs
												lowercase__				:      Dict=       {"input_ids": input_ids, "attention_mask": input_mask}
												return config, inputs_dict


@require_torch
class     __UpperCAmelCase(   __snake_case  ,  __snake_case  ,  unittest.TestCase							):




						"""simple docstring"""




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




						def 		UpperCAmelCase_       (   self       ,  snake_case__       ,  snake_case__       ,  snake_case__       ,  snake_case__       ,  snake_case__			):
												'''simple docstring'''
												if pipeline_test_casse_name == "QAPipelineTests":
																		return True

												return False




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      List[str]=       ErnieMModelTester(self			)
												lowercase__				:      Optional[Any]=       ConfigTester(self       ,  config_class=snake_case__       ,  hidden_size=37			)




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												self.config_tester.run_common_tests()




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      int=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*snake_case__			)




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      int=       self.model_tester.prepare_config_and_inputs()
												for type in ["absolute", "relative_key", "relative_key_query"]:
																		lowercase__				:      str=       type
																		self.model_tester.create_and_check_model(*snake_case__			)




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      Tuple=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_multiple_choice(*snake_case__			)




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      Tuple=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_question_answering(*snake_case__			)




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      Any=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_sequence_classification(*snake_case__			)




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      int=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_information_extraction(*snake_case__			)




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      Optional[int]=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_token_classification(*snake_case__			)




						@slow
						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		lowercase__				:      List[str]=       ErnieMModel.from_pretrained(snake_case__			)
																		self.assertIsNotNone(snake_case__			)


@require_torch
class     __UpperCAmelCase(   unittest.TestCase							):




						"""simple docstring"""




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

												# TODO Replace vocab size
												lowercase__				:      List[str]=       768

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

												lowercase__				:      int=       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]       ,  snake_case__       ,  atol=1e-3			)			)









from typing import TYPE_CHECKING

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


lowerCamelCase_    =					{
    '''configuration_mpnet''': ['''MPNET_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MPNetConfig'''],
    '''tokenization_mpnet''': ['''MPNetTokenizer'''],
}

try:
    if not is_tokenizers_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    lowerCamelCase_    =					['''MPNetTokenizerFast''']

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    lowerCamelCase_    =					[
        '''MPNET_PRETRAINED_MODEL_ARCHIVE_LIST''',
        '''MPNetForMaskedLM''',
        '''MPNetForMultipleChoice''',
        '''MPNetForQuestionAnswering''',
        '''MPNetForSequenceClassification''',
        '''MPNetForTokenClassification''',
        '''MPNetLayer''',
        '''MPNetModel''',
        '''MPNetPreTrainedModel''',
    ]

try:
    if not is_tf_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    lowerCamelCase_    =					[
        '''TF_MPNET_PRETRAINED_MODEL_ARCHIVE_LIST''',
        '''TFMPNetEmbeddings''',
        '''TFMPNetForMaskedLM''',
        '''TFMPNetForMultipleChoice''',
        '''TFMPNetForQuestionAnswering''',
        '''TFMPNetForSequenceClassification''',
        '''TFMPNetForTokenClassification''',
        '''TFMPNetMainLayer''',
        '''TFMPNetModel''',
        '''TFMPNetPreTrainedModel''',
    ]


if TYPE_CHECKING:
    from .configuration_mpnet import MPNET_PRETRAINED_CONFIG_ARCHIVE_MAP, MPNetConfig
    from .tokenization_mpnet import MPNetTokenizer

    try:
        if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .tokenization_mpnet_fast import MPNetTokenizerFast

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_mpnet import (
            MPNET_PRETRAINED_MODEL_ARCHIVE_LIST,
            MPNetForMaskedLM,
            MPNetForMultipleChoice,
            MPNetForQuestionAnswering,
            MPNetForSequenceClassification,
            MPNetForTokenClassification,
            MPNetLayer,
            MPNetModel,
            MPNetPreTrainedModel,
        )

    try:
        if not is_tf_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_tf_mpnet import (
            TF_MPNET_PRETRAINED_MODEL_ARCHIVE_LIST,
            TFMPNetEmbeddings,
            TFMPNetForMaskedLM,
            TFMPNetForMultipleChoice,
            TFMPNetForQuestionAnswering,
            TFMPNetForSequenceClassification,
            TFMPNetForTokenClassification,
            TFMPNetMainLayer,
            TFMPNetModel,
            TFMPNetPreTrainedModel,
        )

else:
    import sys

    lowerCamelCase_    =					_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)