






"""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					:						List[Any]   ,						A					:						int   ,						A					:						Union[str, Any]=13   ,						A					:						List[Any]=7   ,						A					:						Tuple=True   ,						A					:						Dict=True   ,						A					:						Optional[Any]=True   ,						A					:						Any=99   ,						A					:						Optional[int]=32   ,						A					:						Optional[int]=5   ,						A					:						Tuple=4   ,						A					:						List[str]=37   ,						A					:						Any="gelu"   ,						A					:						List[Any]=0.1   ,						A					:						List[str]=0.1   ,						A					:						Any=5_12   ,						A					:						List[str]=16   ,						A					:						Optional[Any]=2   ,						A					:						Optional[Any]=0.02   ,						A					:						Optional[int]=3   ,						A					:						List[Any]=4   ,						A					:						List[Any]=None   ,						)  ->  Optional[Any]:
							lowercase_			:			str								=       parent
							lowercase_			:			Dict								=       batch_size
							lowercase_			:			Optional[int]								=       seq_length
							lowercase_			:			str								=       is_training
							lowercase_			:			int								=       use_input_mask
							lowercase_			:			Optional[Any]								=       use_labels
							lowercase_			:			int								=       vocab_size
							lowercase_			:			Any								=       hidden_size
							lowercase_			:			int								=       num_hidden_layers
							lowercase_			:			str								=       num_attention_heads
							lowercase_			:			Optional[int]								=       intermediate_size
							lowercase_			:			Union[str, Any]								=       hidden_act
							lowercase_			:			Tuple								=       hidden_dropout_prob
							lowercase_			:			int								=       attention_probs_dropout_prob
							lowercase_			:			Tuple								=       max_position_embeddings
							lowercase_			:			str								=       type_vocab_size
							lowercase_			:			Any								=       type_sequence_label_size
							lowercase_			:			int								=       initializer_range
							lowercase_			:			Tuple								=       num_labels
							lowercase_			:			int								=       num_choices
							lowercase_			:			Any								=       scope




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

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

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

							lowercase_			:			Optional[int]								=       self.get_config()

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




					def   A  (    self					:						int       )  ->  int:
							lowercase_			:			Optional[int]								=       ids_tensor([self.batch_size, self.seq_length]   ,						self.vocab_size       )

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

							return config, input_ids, input_mask




					def   A  (    self					:						str       )  ->  Union[str, 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   A  (    self					:						Any   ,						A					:						Optional[Any]   ,						A					:						Any   ,						A					:						Any   ,						A					:						Any   ,						A					:						Dict   ,						A					:						List[Any]       )  ->  Optional[Any]:
							lowercase_			:			Tuple								=       ErnieMModel(config=lowercase__       )
							model.to(lowercase__       )
							model.eval()
							lowercase_			:			Optional[int]								=       model(lowercase__   ,						return_dict=lowercase__       )
							self.parent.assertEqual(result.last_hidden_state.shape   ,						(self.batch_size, self.seq_length, self.hidden_size)       )




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




					def   A  (    self					:						Any   ,						A					:						List[Any]   ,						A					:						Optional[int]   ,						A					:						Union[str, Any]   ,						A					:						Tuple   ,						A					:						List[Any]   ,						A					:						Dict       )  ->  List[Any]:
							lowercase_			:			Dict								=       self.num_labels
							lowercase_			:			int								=       ErnieMForTokenClassification(config=lowercase__       )
							model.to(lowercase__       )
							model.eval()
							input_ids.to(lowercase__       )
							input_mask.to(lowercase__       )
							token_labels.to(lowercase__       )

							lowercase_			:			int								=       model(lowercase__   ,						attention_mask=lowercase__   ,						labels=lowercase__       )

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




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






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





@require_torch
class  a_	(			_UpperCAmelCase       ,						_UpperCAmelCase       ,						unittest.TestCase							):
					SCREAMING_SNAKE_CASE_		:    Dict							   =       (
					    (
					        ErnieMModel,
					        ErnieMForMultipleChoice,
					        ErnieMForQuestionAnswering,
					        ErnieMForSequenceClassification,
					        ErnieMForTokenClassification,
					    )
					    if is_torch_available()
					    else ()
					)
					SCREAMING_SNAKE_CASE_		:    List[Any]							   =       ()
					SCREAMING_SNAKE_CASE_		:    Tuple							   =       (
					    {
					        "feature-extraction": ErnieMModel,
					        "question-answering": ErnieMForQuestionAnswering,
					        "text-classification": ErnieMForSequenceClassification,
					        "token-classification": ErnieMForTokenClassification,
					        "zero-shot": ErnieMForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					SCREAMING_SNAKE_CASE_		:    Union[str, Any]							   =       False




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






							if pipeline_test_casse_name == "QAPipelineTests":
									return True

							return False




					def   A  (    self					:						Tuple       )  ->  Any:
							lowercase_			:			List[Any]								=       ErnieMModelTester(self       )
							lowercase_			:			List[Any]								=       ConfigTester(self   ,						config_class=lowercase__   ,						hidden_size=37       )




					def   A  (    self					:						Optional[int]       )  ->  Optional[int]:
							self.config_tester.run_common_tests()




					def   A  (    self					:						Union[str, Any]       )  ->  Dict:
							lowercase_			:			List[Any]								=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowercase__       )




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




					def   A  (    self					:						Any       )  ->  Dict:
							lowercase_			:			Optional[int]								=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*lowercase__       )




					def   A  (    self					:						Dict       )  ->  Optional[int]:
							lowercase_			:			Any								=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*lowercase__       )




					def   A  (    self					:						int       )  ->  int:
							lowercase_			:			Any								=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*lowercase__       )




					def   A  (    self					:						Any       )  ->  Tuple:
							lowercase_			:			Optional[Any]								=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_information_extraction(*lowercase__       )




					def   A  (    self					:						Dict       )  ->  List[Any]:
							lowercase_			:			Union[str, Any]								=       self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*lowercase__       )






					@slow
					def   A  (    self					:						Optional[int]       )  ->  Any:






							for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowercase_			:			str								=       ErnieMModel.from_pretrained(lowercase__       )
									self.assertIsNotNone(lowercase__       )





@require_torch
class  a_	(			unittest.TestCase							):




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

							# TODO Replace vocab size
							lowercase_			:			Tuple								=       7_68

							lowercase_			:			Optional[int]								=       torch.Size((1, 6, hidden_size)       )
							self.assertEqual(output.shape   ,						lowercase__       )

							lowercase_			:			List[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]   ,						lowercase__   ,						atol=1e-3       )       )











"""simple docstring"""



from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


__A		:	Any      =		{
    '''configuration_realm''': ['''REALM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''RealmConfig'''],
    '''tokenization_realm''': ['''RealmTokenizer'''],
}

try:
						if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__A		:	Tuple      =		['''RealmTokenizerFast''']

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__A		:	Optional[int]      =		[
						    '''REALM_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''RealmEmbedder''',
						    '''RealmForOpenQA''',
						    '''RealmKnowledgeAugEncoder''',
						    '''RealmPreTrainedModel''',
						    '''RealmReader''',
						    '''RealmScorer''',
						    '''load_tf_weights_in_realm''',
						]
						__A		:	Union[str, Any]      =		['''RealmRetriever''']


if TYPE_CHECKING:
						from .configuration_realm import REALM_PRETRAINED_CONFIG_ARCHIVE_MAP, RealmConfig
						from .tokenization_realm import RealmTokenizer

						try:
												if not is_tokenizers_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .tokenization_realm import RealmTokenizerFast

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_realm import (
												    REALM_PRETRAINED_MODEL_ARCHIVE_LIST,
												    RealmEmbedder,
												    RealmForOpenQA,
												    RealmKnowledgeAugEncoder,
												    RealmPreTrainedModel,
												    RealmReader,
												    RealmScorer,
												    load_tf_weights_in_realm,
												)
												from .retrieval_realm import RealmRetriever


else:
						import sys

						__A		:	Optional[int]      =		_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)



