





import unittest

from transformers import MPNetConfig, 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 (
					    MPNetForMaskedLM,
					    MPNetForMultipleChoice,
					    MPNetForQuestionAnswering,
					    MPNetForSequenceClassification,
					    MPNetForTokenClassification,
					    MPNetModel,
					)

class 							__SCREAMING_SNAKE_CASE       :






							def __init__(    self	,			__lowerCAmelCase	,			__lowerCAmelCase=13	,			__lowerCAmelCase=7	,			__lowerCAmelCase=True	,			__lowerCAmelCase=True	,			__lowerCAmelCase=False	,			__lowerCAmelCase=True	,			__lowerCAmelCase=99	,			__lowerCAmelCase=64	,			__lowerCAmelCase=5	,			__lowerCAmelCase=4	,			__lowerCAmelCase=64	,			__lowerCAmelCase="gelu"	,			__lowerCAmelCase=0.1	,			__lowerCAmelCase=0.1	,			__lowerCAmelCase=512	,			__lowerCAmelCase=16	,			__lowerCAmelCase=2	,			__lowerCAmelCase=0.02	,			__lowerCAmelCase=3	,			__lowerCAmelCase=4	,			__lowerCAmelCase=None	,			):
														UpperCamelCase__					  =      parent
														UpperCamelCase__					  =      batch_size
														UpperCamelCase__					  =      seq_length
														UpperCamelCase__					  =      is_training
														UpperCamelCase__					  =      use_input_mask
														UpperCamelCase__					  =      use_token_type_ids
														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	):
														return MPNetConfig.from_pretrained("""microsoft/mpnet-base"""	)






							def 					_lowerCamelCase		(    self	):
														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	):
														return MPNetConfig(
														    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	,			initializer_range=self.initializer_range	,			)






							def 					_lowerCamelCase		(    self	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	):
														UpperCamelCase__					  =      MPNetModel(config=__lowerCAmelCase	)
														model.to(__lowerCAmelCase	)
														model.eval()
														UpperCamelCase__					  =      model(__lowerCAmelCase	,			__lowerCAmelCase	)
														UpperCamelCase__					  =      model(__lowerCAmelCase	)
														self.parent.assertEqual(result.last_hidden_state.shape	,			(self.batch_size, self.seq_length, self.hidden_size)	)
														self.parent.assertEqual(result.pooler_output.shape	,			(self.batch_size, self.hidden_size)	)






							def 					_lowerCamelCase		(    self	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	):
														UpperCamelCase__					  =      MPNetForQuestionAnswering(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	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	):
														UpperCamelCase__					  =      self.num_labels
														UpperCamelCase__					  =      MPNetForSequenceClassification(__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	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	):
														UpperCamelCase__					  =      self.num_choices
														UpperCamelCase__					  =      MPNetForMultipleChoice(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	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	):
														UpperCamelCase__					  =      self.num_labels
														UpperCamelCase__					  =      MPNetForTokenClassification(config=__lowerCAmelCase	)
														model.to(__lowerCAmelCase	)
														model.eval()
														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	):
														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 							__SCREAMING_SNAKE_CASE       (  _a ,		_a ,		unittest.TestCase							):
							snake_case  :   Union[str, Any]      =							(
							    (
							        MPNetForMaskedLM,
							        MPNetForMultipleChoice,
							        MPNetForQuestionAnswering,
							        MPNetForSequenceClassification,
							        MPNetForTokenClassification,
							        MPNetModel,
							    )
							    if is_torch_available()
							    else ()
							)
							snake_case  :   Optional[Any]      =							(
							    {
							        """feature-extraction""": MPNetModel,
							        """fill-mask""": MPNetForMaskedLM,
							        """question-answering""": MPNetForQuestionAnswering,
							        """text-classification""": MPNetForSequenceClassification,
							        """token-classification""": MPNetForTokenClassification,
							        """zero-shot""": MPNetForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							snake_case  :   int      =							False
							snake_case  :   Any      =							True






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      MPNetModelTester(self	)
														UpperCamelCase__					  =      ConfigTester(self	,			config_class=__lowerCAmelCase	,			hidden_size=37	)






							def 					_lowerCamelCase		(    self	):
														self.config_tester.run_common_tests()






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_mpnet_model(*__lowerCAmelCase	)






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_mpnet_for_sequence_classification(*__lowerCAmelCase	)






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_mpnet_for_multiple_choice(*__lowerCAmelCase	)






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_mpnet_for_token_classification(*__lowerCAmelCase	)







							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_mpnet_for_question_answering(*__lowerCAmelCase	)






@require_torch
class 							__SCREAMING_SNAKE_CASE       (  unittest.TestCase							):






							@slow
							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      MPNetModel.from_pretrained("""microsoft/mpnet-base"""	)
														UpperCamelCase__					  =      torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]	)
														UpperCamelCase__					  =      model(__lowerCAmelCase	)[0]
														UpperCamelCase__					  =      torch.Size((1, 11, 768)	)
														self.assertEqual(output.shape	,			__lowerCAmelCase	)
														UpperCamelCase__					  =      torch.tensor(
														    [[[-0.0550, 0.1943, -0.0740], [-0.0562, 0.2211, -0.0579], [-0.0437, 0.3337, -0.0641]]]	)
														# compare the actual values for a slice.
														self.assertTrue(torch.allclose(output[:, :3, :3]	,			__lowerCAmelCase	,			atol=1E-4	)	)











import copy

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


UpperCamelCase__           =			logging.get_logger(__name__)

class 							__SCREAMING_SNAKE_CASE       (  _a							):
							snake_case  :   Optional[Any]      =							"""encoder-decoder"""
							snake_case  :   Optional[int]      =							True






							def __init__(    self	,			**__lowerCAmelCase	):
														super().__init__(**__lowerCAmelCase	)
														assert (
														    "encoder" in kwargs and "decoder" in kwargs
														), "Config has to be initialized with encoder and decoder config"
														UpperCamelCase__					  =      kwargs.pop("""encoder"""	)
														UpperCamelCase__					  =      encoder_config.pop("""model_type"""	)
														UpperCamelCase__					  =      kwargs.pop("""decoder"""	)
														UpperCamelCase__					  =      decoder_config.pop("""model_type"""	)

														from ..auto.configuration_auto import AutoConfig

														UpperCamelCase__					  =      AutoConfig.for_model(__lowerCAmelCase	,			**__lowerCAmelCase	)
														UpperCamelCase__					  =      AutoConfig.for_model(__lowerCAmelCase	,			**__lowerCAmelCase	)
														UpperCamelCase__					  =      True






							@classmethod
							def 					_lowerCamelCase		(    cls	,			__lowerCAmelCase	,			__lowerCAmelCase	,			**__lowerCAmelCase	):
														logger.info("""Set `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config"""	)
														UpperCamelCase__					  =      True
														UpperCamelCase__					  =      True

														return cls(encoder=encoder_config.to_dict()	,			decoder=decoder_config.to_dict()	,			**__lowerCAmelCase	)







							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      copy.deepcopy(self.__dict__	)
														UpperCamelCase__					  =      self.encoder.to_dict()
														UpperCamelCase__					  =      self.decoder.to_dict()
														UpperCamelCase__					  =      self.__class__.model_type
														return output




