





"""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						:       Optional[Any],					lowerCAmelCase__						:       Any,					lowerCAmelCase__						:       Any=1_3,					lowerCAmelCase__						:       Dict=7,					lowerCAmelCase__						:       str=True,					lowerCAmelCase__						:       str=True,					lowerCAmelCase__						:       str=True,					lowerCAmelCase__						:       Union[str, Any]=9_9,					lowerCAmelCase__						:       int=3_2,					lowerCAmelCase__						:       Optional[Any]=5,					lowerCAmelCase__						:       List[Any]=4,					lowerCAmelCase__						:       List[Any]=3_7,					lowerCAmelCase__						:       Dict="gelu",					lowerCAmelCase__						:       Optional[Any]=0.1,					lowerCAmelCase__						:       Dict=0.1,					lowerCAmelCase__						:       List[Any]=5_1_2,					lowerCAmelCase__						:       int=1_6,					lowerCAmelCase__						:       Optional[Any]=2,					lowerCAmelCase__						:       List[Any]=0.02,					lowerCAmelCase__						:       int=3,					lowerCAmelCase__						:       List[Any]=4,					lowerCAmelCase__						:       Any=None,					)      -> Optional[int]:




										'''simple docstring'''



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






				def 							snake_case      (					self						:       Union[str, Any]				)      -> List[Any]:




										'''simple docstring'''



										_UpperCamelCase     :   Optional[int]      =       ids_tensor([self.batch_size, self.seq_length],					self.vocab_size				)

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

										_UpperCamelCase     :   int      =       None
										_UpperCamelCase     :   Tuple      =       None
										_UpperCamelCase     :   Optional[int]      =       None
										if self.use_labels:
																_UpperCamelCase     :   List[Any]      =       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     :   Dict      =       self.get_config()

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






				def 							snake_case      (					self						:       Optional[int]				)      -> int:




										'''simple docstring'''



										_UpperCamelCase     :   int      =       ids_tensor([self.batch_size, self.seq_length],					self.vocab_size				)

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

										return config, input_ids, input_mask






				def 							snake_case      (					self						:       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						:       List[str],					lowerCAmelCase__						:       List[Any],					lowerCAmelCase__						:       Dict,					lowerCAmelCase__						:       List[str],					lowerCAmelCase__						:       Dict,					lowerCAmelCase__						:       Dict,					lowerCAmelCase__						:       List[str]				)      -> Any:




										'''simple docstring'''



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






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




										'''simple docstring'''



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




										'''simple docstring'''



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




										'''simple docstring'''



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






				def 							snake_case      (					self						:       List[str],					lowerCAmelCase__						:       Dict,					lowerCAmelCase__						:       Any,					lowerCAmelCase__						:       Tuple,					lowerCAmelCase__						:       Dict,					lowerCAmelCase__						:       Tuple,					lowerCAmelCase__						:       Optional[int]				)      -> Dict:




										'''simple docstring'''



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

										_UpperCamelCase     :   Any      =       model(lowerCAmelCase__,					attention_mask=lowerCAmelCase__,					labels=lowerCAmelCase__				)

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






				def 							snake_case      (					self						:       Union[str, Any],					lowerCAmelCase__						:       List[Any],					lowerCAmelCase__						:       str,					lowerCAmelCase__						:       Union[str, Any],					lowerCAmelCase__						:       Any,					lowerCAmelCase__						:       Any,					lowerCAmelCase__						:       Tuple				)      -> Optional[int]:




										'''simple docstring'''



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






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




										'''simple docstring'''



										_UpperCamelCase     :   Tuple      =       self.prepare_config_and_inputs()
										(
										    (
										    _UpperCamelCase
										)  ,				(
										    _UpperCamelCase
										)  ,				(
										    _UpperCamelCase
										)  ,				(
										    _UpperCamelCase
										)  ,				(
										    _UpperCamelCase
										)  ,				(
										    _UpperCamelCase
										)  ,				
										)     :   Tuple      =       config_and_inputs
										_UpperCamelCase     :   Tuple      =       {'''input_ids''': input_ids, '''attention_mask''': input_mask}
										return config, inputs_dict




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






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




										'''simple docstring'''



										if pipeline_test_casse_name == "QAPipelineTests":
																return True

										return False






				def 							snake_case      (					self						:       List[str]				)      -> Tuple:




										'''simple docstring'''



										_UpperCamelCase     :   int      =       ErnieMModelTester(self				)
										_UpperCamelCase     :   Tuple      =       ConfigTester(self,					config_class=lowerCAmelCase__,					hidden_size=3_7				)






				def 							snake_case      (					self						:       Tuple				)      -> Tuple:




										'''simple docstring'''



										self.config_tester.run_common_tests()






				def 							snake_case      (					self						:       Dict				)      -> Tuple:




										'''simple docstring'''



										_UpperCamelCase     :   int      =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCAmelCase__				)






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




										'''simple docstring'''



										_UpperCamelCase     :   Optional[int]      =       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(*lowerCAmelCase__				)






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




										'''simple docstring'''



										_UpperCamelCase     :   Optional[int]      =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_multiple_choice(*lowerCAmelCase__				)






				def 							snake_case      (					self						:       List[str]				)      -> Union[str, Any]:




										'''simple docstring'''



										_UpperCamelCase     :   Optional[Any]      =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase__				)






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




										'''simple docstring'''



										_UpperCamelCase     :   Optional[Any]      =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase__				)






				def 							snake_case      (					self						:       str				)      -> int:




										'''simple docstring'''



										_UpperCamelCase     :   int      =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_information_extraction(*lowerCAmelCase__				)






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




										'''simple docstring'''



										_UpperCamelCase     :   Dict      =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase__				)






				@slow
				def 							snake_case      (					self						:       List[Any]				)      -> str:




										'''simple docstring'''



										for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																_UpperCamelCase     :   List[str]      =       ErnieMModel.from_pretrained(lowerCAmelCase__				)
																self.assertIsNotNone(lowerCAmelCase__				)




@require_torch
class 					_a		( unittest.TestCase							):






				@slow
				def 							snake_case      (					self						:       Tuple				)      -> Any:




										'''simple docstring'''



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

										# TODO Replace vocab size
										_UpperCamelCase     :   List[Any]      =       7_6_8

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

										_UpperCamelCase     :   Tuple      =       torch.tensor(
										    [[[-0.0_012, 0.1_245, -0.0_214], [-0.0_742, 0.0_244, -0.0_771], [-0.0_333, 0.1_164, -0.1_554]]]				)

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











"""simple docstring"""





from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


A__       :			Union[str, Any]=						logging.get_logger(__name__)

A__       :			Dict=						{
    """google/rembert""": """https://huggingface.co/google/rembert/resolve/main/config.json""",
    # See all RemBERT models at https://huggingface.co/models?filter=rembert
}
class  __lowerCamelCase     (	_a	):
   a   :					Optional[int]			    ="""rembert"""

   def __init__(       self			,					snake_case_=25_0300			,					snake_case_=1152			,					snake_case_=32			,					snake_case_=18			,					snake_case_=256			,					snake_case_=1664			,					snake_case_=4608			,					snake_case_="gelu"			,					snake_case_=0.0			,					snake_case_=0.0			,					snake_case_=0.1			,					snake_case_=512			,					snake_case_=2			,					snake_case_=0.02			,					snake_case_=1E-12			,					snake_case_=True			,					snake_case_=0			,					snake_case_=312			,					snake_case_=313			,					**snake_case_			,					)		->							str:
       super().__init__(pad_token_id=snake_case_			,					bos_token_id=snake_case_			,					eos_token_id=snake_case_			,					**snake_case_   )

       UpperCamelCase__								=     vocab_size
       UpperCamelCase__								=     input_embedding_size
       UpperCamelCase__								=     output_embedding_size
       UpperCamelCase__								=     max_position_embeddings
       UpperCamelCase__								=     hidden_size
       UpperCamelCase__								=     num_hidden_layers
       UpperCamelCase__								=     num_attention_heads
       UpperCamelCase__								=     intermediate_size
       UpperCamelCase__								=     hidden_act
       UpperCamelCase__								=     hidden_dropout_prob
       UpperCamelCase__								=     attention_probs_dropout_prob
       UpperCamelCase__								=     classifier_dropout_prob
       UpperCamelCase__								=     initializer_range
       UpperCamelCase__								=     type_vocab_size
       UpperCamelCase__								=     layer_norm_eps
       UpperCamelCase__								=     use_cache
       UpperCamelCase__								=     False







class  __lowerCamelCase     (	_a	):

   @property
   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Mapping[str, Mapping[int, str]]:
       if self.task == "multiple-choice":
           UpperCamelCase__								=     {0: 'batch', 1: 'choice', 2: 'sequence'}
       else:
           UpperCamelCase__								=     {0: 'batch', 1: 'sequence'}
       return OrderedDict(
           [
               ('input_ids', dynamic_axis),
               ('attention_mask', dynamic_axis),
               ('token_type_ids', dynamic_axis),
           ]   )







   @property
   def     SCREAMING_SNAKE_CASE__			(       self   )		->							float:
       return 1E-4




