"""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		,     lowerCAmelCase		,     lowerCAmelCase=13		,     lowerCAmelCase=7		,     lowerCAmelCase=True		,     lowerCAmelCase=True		,     lowerCAmelCase=True		,     lowerCAmelCase=99		,     lowerCAmelCase=32		,     lowerCAmelCase=5		,     lowerCAmelCase=4		,     lowerCAmelCase=37		,     lowerCAmelCase="gelu"		,     lowerCAmelCase=0.1		,     lowerCAmelCase=0.1		,     lowerCAmelCase=5_12		,     lowerCAmelCase=16		,     lowerCAmelCase=2		,     lowerCAmelCase=0.02		,     lowerCAmelCase=3		,     lowerCAmelCase=4		,     lowerCAmelCase=None		,     ):






										"""simple docstring"""

										snake_case             =      parent
										snake_case             =      batch_size
										snake_case             =      seq_length
										snake_case             =      is_training
										snake_case             =      use_input_mask
										snake_case             =      use_labels
										snake_case             =      vocab_size
										snake_case             =      hidden_size
										snake_case             =      num_hidden_layers
										snake_case             =      num_attention_heads
										snake_case             =      intermediate_size
										snake_case             =      hidden_act
										snake_case             =      hidden_dropout_prob
										snake_case             =      attention_probs_dropout_prob
										snake_case             =      max_position_embeddings
										snake_case             =      type_vocab_size
										snake_case             =      type_sequence_label_size
										snake_case             =      initializer_range
										snake_case             =      num_labels
										snake_case             =      num_choices
										snake_case             =      scope

				def 		snake_case			(					self     ):






										"""simple docstring"""

										snake_case             =      ids_tensor([self.batch_size, self.seq_length]		,     self.vocab_size     )

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

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

										snake_case             =      self.get_config()

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

				def 		snake_case			(					self     ):






										"""simple docstring"""

										snake_case             =      ids_tensor([self.batch_size, self.seq_length]		,     self.vocab_size     )

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

										return config, input_ids, input_mask

				def 		snake_case			(					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 		snake_case			(					self		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase     ):






										"""simple docstring"""

										snake_case             =      ErnieMModel(config=lowerCAmelCase     )
										model.to(lowerCAmelCase     )
										model.eval()
										snake_case             =      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		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase     ):






										"""simple docstring"""

										snake_case             =      ErnieMForQuestionAnswering(config=lowerCAmelCase     )
										model.to(lowerCAmelCase     )
										model.eval()
										snake_case             =      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		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase     ):






										"""simple docstring"""

										snake_case             =      ErnieMForInformationExtraction(config=lowerCAmelCase     )
										model.to(lowerCAmelCase     )
										model.eval()
										snake_case             =      torch.ones_like(lowerCAmelCase		,     dtype=torch.floataa     )
										snake_case             =      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		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase     ):






										"""simple docstring"""

										snake_case             =      self.num_labels
										snake_case             =      ErnieMForSequenceClassification(lowerCAmelCase     )
										model.to(lowerCAmelCase     )
										model.eval()
										snake_case             =      model(lowerCAmelCase		,     attention_mask=lowerCAmelCase		,     labels=lowerCAmelCase     )
										self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_labels)     )

				def 		snake_case			(					self		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase     ):






										"""simple docstring"""

										snake_case             =      self.num_labels
										snake_case             =      ErnieMForTokenClassification(config=lowerCAmelCase     )
										model.to(lowerCAmelCase     )
										model.eval()
										input_ids.to(lowerCAmelCase     )
										input_mask.to(lowerCAmelCase     )
										token_labels.to(lowerCAmelCase     )

										snake_case             =      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		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase     ):






										"""simple docstring"""

										snake_case             =      self.num_choices
										snake_case             =      ErnieMForMultipleChoice(config=lowerCAmelCase     )
										model.to(lowerCAmelCase     )
										model.eval()
										snake_case             =      input_ids.unsqueeze(1     ).expand(-1		,     self.num_choices		,     -1     ).contiguous()
										snake_case             =      input_mask.unsqueeze(1     ).expand(-1		,     self.num_choices		,     -1     ).contiguous()
										snake_case             =      model(
										    lowerCAmelCase		,     attention_mask=lowerCAmelCase		,     labels=lowerCAmelCase		,     )
										self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_choices)     )

				def 		snake_case			(					self     ):






										"""simple docstring"""

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





@require_torch
class 	lowerCAmelCase_     (      lowerCAmelCase  ,     lowerCAmelCase  ,     unittest.TestCase       ):



				"""simple docstring"""
				_lowerCAmelCase  :     str      	=    (
				    (
				        ErnieMModel,
				        ErnieMForMultipleChoice,
				        ErnieMForQuestionAnswering,
				        ErnieMForSequenceClassification,
				        ErnieMForTokenClassification,
				    )
				    if is_torch_available()
				    else ()
				)
				_lowerCAmelCase  :     List[str]      	=    ()
				_lowerCAmelCase  :     int      	=    (
				    {
				        """feature-extraction""": ErnieMModel,
				        """question-answering""": ErnieMForQuestionAnswering,
				        """text-classification""": ErnieMForSequenceClassification,
				        """token-classification""": ErnieMForTokenClassification,
				        """zero-shot""": ErnieMForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				_lowerCAmelCase  :     Dict      	=    False

				def 		snake_case			(					self		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase		,     lowerCAmelCase     ):






										"""simple docstring"""

										if pipeline_test_casse_name == "QAPipelineTests":
																return True

										return False

				def 		snake_case			(					self     ):






										"""simple docstring"""

										snake_case             =      ErnieMModelTester(self     )
										snake_case             =      ConfigTester(self		,     config_class=lowerCAmelCase		,     hidden_size=37     )

				def 		snake_case			(					self     ):






										"""simple docstring"""

										self.config_tester.run_common_tests()

				def 		snake_case			(					self     ):






										"""simple docstring"""

										snake_case             =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*lowerCAmelCase     )

				def 		snake_case			(					self     ):






										"""simple docstring"""

										snake_case             =      self.model_tester.prepare_config_and_inputs()
										for type in ["absolute", "relative_key", "relative_key_query"]:
																snake_case             =      type
																self.model_tester.create_and_check_model(*lowerCAmelCase     )

				def 		snake_case			(					self     ):






										"""simple docstring"""

										snake_case             =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_multiple_choice(*lowerCAmelCase     )

				def 		snake_case			(					self     ):






										"""simple docstring"""

										snake_case             =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase     )

				def 		snake_case			(					self     ):






										"""simple docstring"""

										snake_case             =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase     )

				def 		snake_case			(					self     ):






										"""simple docstring"""

										snake_case             =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_information_extraction(*lowerCAmelCase     )

				def 		snake_case			(					self     ):






										"""simple docstring"""

										snake_case             =      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase     )

				@slow
				def 		snake_case			(					self     ):






										"""simple docstring"""

										for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																snake_case             =      ErnieMModel.from_pretrained(lowerCAmelCase     )
																self.assertIsNotNone(lowerCAmelCase     )





@require_torch
class 	lowerCAmelCase_     (      unittest.TestCase       ):



				"""simple docstring"""

				@slow
				def 		snake_case			(					self     ):






										"""simple docstring"""

										snake_case             =      ErnieMModel.from_pretrained('susnato/ernie-m-base_pytorch'     )
										model.eval()
										snake_case             =      torch.tensor([[0, 1, 2, 3, 4, 5]]     )
										snake_case             =      model(lowerCAmelCase     )[0]

										# TODO Replace vocab size
										snake_case             =      7_68

										snake_case             =      torch.Size((1, 6, hidden_size)     )
										self.assertEqual(output.shape		,     lowerCAmelCase     )

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



"""simple docstring"""
from typing import TYPE_CHECKING

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


SCREAMING_SNAKE_CASE__       =	{
    "configuration_marian": ["MARIAN_PRETRAINED_CONFIG_ARCHIVE_MAP", "MarianConfig", "MarianOnnxConfig"],
}

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

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					SCREAMING_SNAKE_CASE__       =	[
					    "MARIAN_PRETRAINED_MODEL_ARCHIVE_LIST",
					    "MarianForCausalLM",
					    "MarianModel",
					    "MarianMTModel",
					    "MarianPreTrainedModel",
					]

try:
					if not is_tf_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					SCREAMING_SNAKE_CASE__       =	["TFMarianModel", "TFMarianMTModel", "TFMarianPreTrainedModel"]

try:
					if not is_flax_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					SCREAMING_SNAKE_CASE__       =	["FlaxMarianModel", "FlaxMarianMTModel", "FlaxMarianPreTrainedModel"]

if TYPE_CHECKING:
					from .configuration_marian import MARIAN_PRETRAINED_CONFIG_ARCHIVE_MAP, MarianConfig, MarianOnnxConfig

					try:
										if not is_sentencepiece_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_marian import MarianTokenizer

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_marian import (
										    MARIAN_PRETRAINED_MODEL_ARCHIVE_LIST,
										    MarianForCausalLM,
										    MarianModel,
										    MarianMTModel,
										    MarianPreTrainedModel,
										)

					try:
										if not is_tf_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_tf_marian import TFMarianModel, TFMarianMTModel, TFMarianPreTrainedModel

					try:
										if not is_flax_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_flax_marian import FlaxMarianModel, FlaxMarianMTModel, FlaxMarianPreTrainedModel

else:
					import sys

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


