


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


							"""simple docstring"""






							def __init__(						self :     str		, lowerCamelCase_ :     Tuple		, lowerCamelCase_ :     str=13		, lowerCamelCase_ :     Optional[int]=7		, lowerCamelCase_ :     Union[str, Any]=True		, lowerCamelCase_ :     Any=True		, lowerCamelCase_ :     int=True		, lowerCamelCase_ :     List[str]=99		, lowerCamelCase_ :     Union[str, Any]=32		, lowerCamelCase_ :     Dict=5		, lowerCamelCase_ :     Union[str, Any]=4		, lowerCamelCase_ :     str=37		, lowerCamelCase_ :     Union[str, Any]="gelu"		, lowerCamelCase_ :     Any=0.1		, lowerCamelCase_ :     Union[str, Any]=0.1		, lowerCamelCase_ :     Optional[int]=512		, lowerCamelCase_ :     Dict=16		, lowerCamelCase_ :     Optional[int]=2		, lowerCamelCase_ :     int=0.02		, lowerCamelCase_ :     Optional[int]=3		, lowerCamelCase_ :     Tuple=4		, lowerCamelCase_ :     Tuple=None		, ):
													UpperCAmelCase__		=    parent
													UpperCAmelCase__		=    batch_size
													UpperCAmelCase__		=    seq_length
													UpperCAmelCase__		=    is_training
													UpperCAmelCase__		=    use_input_mask
													UpperCAmelCase__		=    use_labels
													UpperCAmelCase__		=    vocab_size
													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__		=    max_position_embeddings
													UpperCAmelCase__		=    type_vocab_size
													UpperCAmelCase__		=    type_sequence_label_size
													UpperCAmelCase__		=    initializer_range
													UpperCAmelCase__		=    num_labels
													UpperCAmelCase__		=    num_choices
													UpperCAmelCase__		=    scope






							def 					lowerCAmelCase     (						self :     int				):
													UpperCAmelCase__		=    ids_tensor([self.batch_size, self.seq_length]		, self.vocab_size				)

													UpperCAmelCase__		=    None
													if self.use_input_mask:
																			UpperCAmelCase__		=    random_attention_mask([self.batch_size, self.seq_length]				)

													UpperCAmelCase__		=    None
													UpperCAmelCase__		=    None
													UpperCAmelCase__		=    None
													if self.use_labels:
																			UpperCAmelCase__		=    ids_tensor([self.batch_size]		, self.type_sequence_label_size				)
																			UpperCAmelCase__		=    ids_tensor([self.batch_size, self.seq_length]		, self.num_labels				)
																			UpperCAmelCase__		=    ids_tensor([self.batch_size]		, self.num_choices				)

													UpperCAmelCase__		=    self.get_config()

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






							def 					lowerCAmelCase     (						self :     Optional[int]				):
													UpperCAmelCase__		=    ids_tensor([self.batch_size, self.seq_length]		, self.vocab_size				)

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

													return config, input_ids, input_mask






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






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






							def 					lowerCAmelCase     (						self :     str		, lowerCamelCase_ :     Any		, lowerCamelCase_ :     Optional[int]		, lowerCamelCase_ :     int		, lowerCamelCase_ :     Dict		, lowerCamelCase_ :     Optional[int]		, lowerCamelCase_ :     int				):
													UpperCAmelCase__		=    self.num_labels
													UpperCAmelCase__		=    ErnieMForTokenClassification(config=lowerCamelCase_				)
													model.to(lowerCamelCase_				)
													model.eval()
													input_ids.to(lowerCamelCase_				)
													input_mask.to(lowerCamelCase_				)
													token_labels.to(lowerCamelCase_				)

													UpperCAmelCase__		=    model(lowerCamelCase_		, attention_mask=lowerCamelCase_		, labels=lowerCamelCase_				)

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






							def 					lowerCAmelCase     (						self :     Dict		, lowerCamelCase_ :     str		, lowerCamelCase_ :     Any		, lowerCamelCase_ :     Tuple		, lowerCamelCase_ :     Tuple		, lowerCamelCase_ :     int		, lowerCamelCase_ :     Tuple				):
													UpperCAmelCase__		=    self.num_choices
													UpperCAmelCase__		=    ErnieMForMultipleChoice(config=lowerCamelCase_				)
													model.to(lowerCamelCase_				)
													model.eval()
													UpperCAmelCase__		=    input_ids.unsqueeze(1				).expand(-1		, self.num_choices		, -1				).contiguous()
													UpperCAmelCase__		=    input_mask.unsqueeze(1				).expand(-1		, self.num_choices		, -1				).contiguous()
													UpperCAmelCase__		=    model(
													    lowerCamelCase_		, attention_mask=lowerCamelCase_		, labels=lowerCamelCase_		, )
													self.parent.assertEqual(result.logits.shape		, (self.batch_size, self.num_choices)				)






							def 					lowerCAmelCase     (						self :     List[str]				):
													UpperCAmelCase__		=    self.prepare_config_and_inputs()
													(
													    (
													    UpperCAmelCase__
													)	,				(
													    UpperCAmelCase__
													)	,				(
													    UpperCAmelCase__
													)	,				(
													    UpperCAmelCase__
													)	,				(
													    UpperCAmelCase__
													)	,				(
													    UpperCAmelCase__
													)	,				
													)		=    config_and_inputs
													UpperCAmelCase__		=    {'''input_ids''': input_ids, '''attention_mask''': input_mask}
													return config, inputs_dict




@require_torch
class 							lowerCamelCase       (			__SCREAMING_SNAKE_CASE				,					__SCREAMING_SNAKE_CASE				,					unittest.TestCase ):


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






							def 					lowerCAmelCase     (						self :     List[Any]		, lowerCamelCase_ :     List[Any]		, lowerCamelCase_ :     Dict		, lowerCamelCase_ :     int		, lowerCamelCase_ :     Any		, lowerCamelCase_ :     Any				):
													if pipeline_test_casse_name == "QAPipelineTests":
																			return True

													return False






							def 					lowerCAmelCase     (						self :     str				):
													UpperCAmelCase__		=    ErnieMModelTester(self				)
													UpperCAmelCase__		=    ConfigTester(self		, config_class=lowerCamelCase_		, hidden_size=37				)






							def 					lowerCAmelCase     (						self :     Tuple				):
													self.config_tester.run_common_tests()






							def 					lowerCAmelCase     (						self :     Tuple				):
													UpperCAmelCase__		=    self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*lowerCamelCase_				)






							def 					lowerCAmelCase     (						self :     Union[str, Any]				):
													UpperCAmelCase__		=    self.model_tester.prepare_config_and_inputs()
													for type in ["absolute", "relative_key", "relative_key_query"]:
																			UpperCAmelCase__		=    type
																			self.model_tester.create_and_check_model(*lowerCamelCase_				)






							def 					lowerCAmelCase     (						self :     int				):
													UpperCAmelCase__		=    self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase_				)






							def 					lowerCAmelCase     (						self :     int				):
													UpperCAmelCase__		=    self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_question_answering(*lowerCamelCase_				)






							def 					lowerCAmelCase     (						self :     Optional[int]				):
													UpperCAmelCase__		=    self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase_				)






							def 					lowerCAmelCase     (						self :     Tuple				):
													UpperCAmelCase__		=    self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_information_extraction(*lowerCamelCase_				)






							def 					lowerCAmelCase     (						self :     Any				):
													UpperCAmelCase__		=    self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_token_classification(*lowerCamelCase_				)






							@slow
							def 					lowerCAmelCase     (						self :     Optional[Any]				):
													for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			UpperCAmelCase__		=    ErnieMModel.from_pretrained(lowerCamelCase_				)
																			self.assertIsNotNone(lowerCamelCase_				)




@require_torch
class 							lowerCamelCase       (			unittest.TestCase ):


							"""simple docstring"""






							@slow
							def 					lowerCAmelCase     (						self :     int				):
													UpperCAmelCase__		=    ErnieMModel.from_pretrained('''susnato/ernie-m-base_pytorch'''				)
													model.eval()
													UpperCAmelCase__		=    torch.tensor([[0, 1, 2, 3, 4, 5]]				)
													UpperCAmelCase__		=    model(lowerCamelCase_				)[0]

													# TODO Replace vocab size
													UpperCAmelCase__		=    768

													UpperCAmelCase__		=    torch.Size((1, 6, hidden_size)				)
													self.assertEqual(output.shape		, lowerCamelCase_				)

													UpperCAmelCase__		=    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]		, lowerCamelCase_		, atol=1E-3				)				)



"""simple docstring"""







from typing import TYPE_CHECKING

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


__a			 =   {
    "configuration_camembert": ["CAMEMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "CamembertConfig", "CamembertOnnxConfig"],
}

try:
						if not is_sentencepiece_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__a			 =   ["CamembertTokenizer"]

try:
						if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__a			 =   ["CamembertTokenizerFast"]

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__a			 =   [
						    "CAMEMBERT_PRETRAINED_MODEL_ARCHIVE_LIST",
						    "CamembertForCausalLM",
						    "CamembertForMaskedLM",
						    "CamembertForMultipleChoice",
						    "CamembertForQuestionAnswering",
						    "CamembertForSequenceClassification",
						    "CamembertForTokenClassification",
						    "CamembertModel",
						    "CamembertPreTrainedModel",
						]

try:
						if not is_tf_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__a			 =   [
						    "TF_CAMEMBERT_PRETRAINED_MODEL_ARCHIVE_LIST",
						    "TFCamembertForCausalLM",
						    "TFCamembertForMaskedLM",
						    "TFCamembertForMultipleChoice",
						    "TFCamembertForQuestionAnswering",
						    "TFCamembertForSequenceClassification",
						    "TFCamembertForTokenClassification",
						    "TFCamembertModel",
						    "TFCamembertPreTrainedModel",
						]


if TYPE_CHECKING:
						from .configuration_camembert import CAMEMBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, CamembertConfig, CamembertOnnxConfig

						try:
												if not is_sentencepiece_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .tokenization_camembert import CamembertTokenizer

						try:
												if not is_tokenizers_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .tokenization_camembert_fast import CamembertTokenizerFast

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_camembert import (
												    CAMEMBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
												    CamembertForCausalLM,
												    CamembertForMaskedLM,
												    CamembertForMultipleChoice,
												    CamembertForQuestionAnswering,
												    CamembertForSequenceClassification,
												    CamembertForTokenClassification,
												    CamembertModel,
												    CamembertPreTrainedModel,
												)

						try:
												if not is_tf_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_tf_camembert import (
												    TF_CAMEMBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
												    TFCamembertForCausalLM,
												    TFCamembertForMaskedLM,
												    TFCamembertForMultipleChoice,
												    TFCamembertForQuestionAnswering,
												    TFCamembertForSequenceClassification,
												    TFCamembertForTokenClassification,
												    TFCamembertModel,
												    TFCamembertPreTrainedModel,
												)

else:
						import sys

						__a			 =   _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)