





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   :

			'''simple docstring'''







			def __init__(   self	,  __lowerCAmelCase	,  __lowerCAmelCase=1_3	,  __lowerCAmelCase=7	,  __lowerCAmelCase=True	,  __lowerCAmelCase=True	,  __lowerCAmelCase=True	,  __lowerCAmelCase=9_9	,  __lowerCAmelCase=3_2	,  __lowerCAmelCase=5	,  __lowerCAmelCase=4	,  __lowerCAmelCase=3_7	,  __lowerCAmelCase="gelu"	,  __lowerCAmelCase=0.1	,  __lowerCAmelCase=0.1	,  __lowerCAmelCase=5_1_2	,  __lowerCAmelCase=1_6	,  __lowerCAmelCase=2	,  __lowerCAmelCase=0.02	,  __lowerCAmelCase=3	,  __lowerCAmelCase=4	,  __lowerCAmelCase=None	,  ):



				'''simple docstring'''







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





			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







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

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

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

				lowerCamelCase__        =		self.get_config()

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





			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







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

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

				return config, input_ids, input_mask





			def 		__lowerCamelCase    (   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 		__lowerCamelCase    (   self	,  __lowerCAmelCase	,  __lowerCAmelCase	,  __lowerCAmelCase	,  __lowerCAmelCase	,  __lowerCAmelCase	,  __lowerCAmelCase							):



				'''simple docstring'''







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



				'''simple docstring'''







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



				'''simple docstring'''







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



				'''simple docstring'''







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





			def 		__lowerCamelCase    (   self	,  __lowerCAmelCase	,  __lowerCAmelCase	,  __lowerCAmelCase	,  __lowerCAmelCase	,  __lowerCAmelCase	,  __lowerCAmelCase							):



				'''simple docstring'''







				lowerCamelCase__        =		self.num_labels
				lowerCamelCase__        =		ErnieMForTokenClassification(config=__lowerCAmelCase							)
				model.to(__lowerCAmelCase							)
				model.eval()
				input_ids.to(__lowerCAmelCase							)
				input_mask.to(__lowerCAmelCase							)
				token_labels.to(__lowerCAmelCase							)

				lowerCamelCase__        =		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	,  __lowerCAmelCase	,  __lowerCAmelCase	,  __lowerCAmelCase	,  __lowerCAmelCase	,  __lowerCAmelCase	,  __lowerCAmelCase							):



				'''simple docstring'''







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





			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







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






@require_torch
class  __A   (   lowerCAmelCase      ,    lowerCAmelCase      ,    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 		__lowerCamelCase    (   self	,  __lowerCAmelCase	,  __lowerCAmelCase	,  __lowerCAmelCase	,  __lowerCAmelCase	,  __lowerCAmelCase							):



				'''simple docstring'''







				if pipeline_test_casse_name == "QAPipelineTests":
					return True

				return False





			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







				lowerCamelCase__        =		ErnieMModelTester(self							)
				lowerCamelCase__        =		ConfigTester(self	,  config_class=__lowerCAmelCase	,  hidden_size=3_7							)





			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







				self.config_tester.run_common_tests()





			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







				lowerCamelCase__        =		self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*__lowerCAmelCase							)





			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







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





			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







				lowerCamelCase__        =		self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_multiple_choice(*__lowerCAmelCase							)





			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







				lowerCamelCase__        =		self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_question_answering(*__lowerCAmelCase							)





			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







				lowerCamelCase__        =		self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_sequence_classification(*__lowerCAmelCase							)





			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







				lowerCamelCase__        =		self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_information_extraction(*__lowerCAmelCase							)





			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







				lowerCamelCase__        =		self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_token_classification(*__lowerCAmelCase							)





			@slow
			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







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






@require_torch
class  __A   (   unittest.TestCase			):

			'''simple docstring'''







			@slow
			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







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

				# TODO Replace vocab size
				lowerCamelCase__        =		7_6_8

				lowerCamelCase__        =		torch.Size((1, 6, hidden_size)							)
				self.assertEqual(output.shape	,  __lowerCAmelCase							)

				lowerCamelCase__        =		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							)							)










from ...configuration_utils import PretrainedConfig
from ...utils import logging


_a									=				logging.get_logger(__name__)

_a									=				{
    "google/pegasus-x-base": "https://huggingface.co/google/pegasus-x-base/resolve/main/config.json",
    "google/pegasus-x-large": "https://huggingface.co/google/pegasus-x-large/resolve/main/config.json",
    # See all PEGASUS-X models at https://huggingface.co/models?filter=pegasus-x
}






class  __A   (   lowerCAmelCase			):

			'''simple docstring'''







			lowerCAmelCase_												=						"""pegasus_x"""
			lowerCAmelCase_												=						["""past_key_values"""]
			lowerCAmelCase_												=						{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}





			def __init__(   self	,  __lowerCAmelCase=9_6_1_0_3	,  __lowerCAmelCase=1_6_3_8_4	,  __lowerCAmelCase=1_6	,  __lowerCAmelCase=4_0_9_6	,  __lowerCAmelCase=1_6	,  __lowerCAmelCase=1_6	,  __lowerCAmelCase=4_0_9_6	,  __lowerCAmelCase=1_6	,  __lowerCAmelCase=0.0	,  __lowerCAmelCase=0.0	,  __lowerCAmelCase=True	,  __lowerCAmelCase=True	,  __lowerCAmelCase="gelu"	,  __lowerCAmelCase=1_0_2_4	,  __lowerCAmelCase=0.1	,  __lowerCAmelCase=0.0	,  __lowerCAmelCase=0.0	,  __lowerCAmelCase=0.02	,  __lowerCAmelCase=0	,  __lowerCAmelCase=True	,  __lowerCAmelCase=0	,  __lowerCAmelCase=1	,  __lowerCAmelCase=1	,  __lowerCAmelCase=3_2	,  __lowerCAmelCase=5_1_2	,  __lowerCAmelCase=True	,  **__lowerCAmelCase	,  ):



				'''simple docstring'''







				lowerCamelCase__        =		vocab_size
				lowerCamelCase__        =		max_position_embeddings
				lowerCamelCase__        =		d_model
				lowerCamelCase__        =		encoder_ffn_dim
				lowerCamelCase__        =		encoder_layers
				lowerCamelCase__        =		encoder_attention_heads
				lowerCamelCase__        =		decoder_ffn_dim
				lowerCamelCase__        =		decoder_layers
				lowerCamelCase__        =		decoder_attention_heads
				lowerCamelCase__        =		dropout
				lowerCamelCase__        =		attention_dropout
				lowerCamelCase__        =		activation_dropout
				lowerCamelCase__        =		activation_function
				lowerCamelCase__        =		init_std
				lowerCamelCase__        =		encoder_layerdrop
				lowerCamelCase__        =		decoder_layerdrop
				lowerCamelCase__        =		use_cache
				lowerCamelCase__        =		encoder_layers
				lowerCamelCase__        =		scale_embedding  # scale factor will be sqrt(d_model) if True

				lowerCamelCase__        =		num_global_tokens
				lowerCamelCase__        =		block_size
				lowerCamelCase__        =		stagger_local_blocks

				super().__init__(
				    pad_token_id=__lowerCAmelCase	,  eos_token_id=__lowerCAmelCase	,  is_encoder_decoder=__lowerCAmelCase	,  decoder_start_token_id=__lowerCAmelCase	,  forced_eos_token_id=__lowerCAmelCase	,  **__lowerCAmelCase	,  )





			@property
			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







				return self.encoder_attention_heads





			@property
			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







				return self.d_model



