





"""simple docstring"""






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 					__UpperCAmelCase							:
				def __init__(		self    :			Optional[Any]	, a_    :			Optional[int]	, a_    :			Dict=13	, a_    :			Any=7	, a_    :			Optional[Any]=True	, a_    :			str=True	, a_    :			Any=False	, a_    :			List[str]=True	, a_    :			Optional[int]=99	, a_    :			str=64	, a_    :			List[Any]=5	, a_    :			Dict=4	, a_    :			List[Any]=64	, a_    :			int="gelu"	, a_    :			Tuple=0.1	, a_    :			List[Any]=0.1	, a_    :			str=5_12	, a_    :			Any=16	, a_    :			Union[str, Any]=2	, a_    :			List[str]=0.02	, a_    :			Optional[int]=3	, a_    :			Dict=4	, a_    :			Any=None	, )	->	Tuple:



							'''simple docstring'''



							a__  :	Dict		 =			parent
							a__  :	int		 =			batch_size
							a__  :	Optional[int]		 =			seq_length
							a__  :	int		 =			is_training
							a__  :	Optional[int]		 =			use_input_mask
							a__  :	Union[str, Any]		 =			use_token_type_ids
							a__  :	Optional[Any]		 =			use_labels
							a__  :	Optional[Any]		 =			vocab_size
							a__  :	int		 =			hidden_size
							a__  :	List[str]		 =			num_hidden_layers
							a__  :	Union[str, Any]		 =			num_attention_heads
							a__  :	Optional[Any]		 =			intermediate_size
							a__  :	Union[str, Any]		 =			hidden_act
							a__  :	List[str]		 =			hidden_dropout_prob
							a__  :	List[Any]		 =			attention_probs_dropout_prob
							a__  :	str		 =			max_position_embeddings
							a__  :	Optional[Any]		 =			type_vocab_size
							a__  :	Union[str, Any]		 =			type_sequence_label_size
							a__  :	int		 =			initializer_range
							a__  :	str		 =			num_labels
							a__  :	str		 =			num_choices
							a__  :	Dict		 =			scope
				def       UpperCAmelCase      (		self    :			str    )	->	Optional[Any]:



							'''simple docstring'''



							return MPNetConfig.from_pretrained("microsoft/mpnet-base"    )
				def       UpperCAmelCase      (		self    :			Dict    )	->	Dict:



							'''simple docstring'''



							a__  :	str		 =			ids_tensor([self.batch_size, self.seq_length]	, self.vocab_size    )

							a__  :	Dict		 =			None
							if self.use_input_mask:
										a__  :	str		 =			random_attention_mask([self.batch_size, self.seq_length]    )

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

							a__  :	List[Any]		 =			self.get_config()
							return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
				def       UpperCAmelCase      (		self    :			List[Any]    )	->	Optional[Any]:



							'''simple docstring'''



							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       UpperCAmelCase      (		self    :			int	, a_    :			int	, a_    :			Optional[Any]	, a_    :			Dict	, a_    :			Optional[Any]	, a_    :			Optional[Any]	, a_    :			Optional[int]    )	->	int:



							'''simple docstring'''



							a__  :	Any		 =			MPNetModel(config=a_    )
							model.to(a_    )
							model.eval()
							a__  :	str		 =			model(a_	, a_    )
							a__  :	int		 =			model(a_    )
							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       UpperCAmelCase      (		self    :			Optional[int]	, a_    :			List[str]	, a_    :			List[str]	, a_    :			Dict	, a_    :			Any	, a_    :			List[Any]	, a_    :			Tuple    )	->	Optional[int]:



							'''simple docstring'''



							a__  :	List[str]		 =			MPNetForQuestionAnswering(config=a_    )
							model.to(a_    )
							model.eval()
							a__  :	Any		 =			model(
							    a_	, attention_mask=a_	, start_positions=a_	, end_positions=a_	, )
							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       UpperCAmelCase      (		self    :			int	, a_    :			List[Any]	, a_    :			int	, a_    :			str	, a_    :			Any	, a_    :			List[str]	, a_    :			Dict    )	->	Any:



							'''simple docstring'''



							a__  :	Optional[Any]		 =			self.num_labels
							a__  :	List[Any]		 =			MPNetForSequenceClassification(a_    )
							model.to(a_    )
							model.eval()
							a__  :	int		 =			model(a_	, attention_mask=a_	, labels=a_    )
							self.parent.assertEqual(result.logits.shape	, (self.batch_size, self.num_labels)    )
				def       UpperCAmelCase      (		self    :			Dict	, a_    :			Optional[Any]	, a_    :			Tuple	, a_    :			Optional[Any]	, a_    :			List[str]	, a_    :			int	, a_    :			int    )	->	Union[str, Any]:



							'''simple docstring'''



							a__  :	int		 =			self.num_choices
							a__  :	int		 =			MPNetForMultipleChoice(config=a_    )
							model.to(a_    )
							model.eval()
							a__  :	Union[str, Any]		 =			input_ids.unsqueeze(1    ).expand(-1	, self.num_choices	, -1    ).contiguous()
							a__  :	int		 =			input_mask.unsqueeze(1    ).expand(-1	, self.num_choices	, -1    ).contiguous()
							a__  :	Any		 =			model(
							    a_	, attention_mask=a_	, labels=a_	, )
							self.parent.assertEqual(result.logits.shape	, (self.batch_size, self.num_choices)    )
				def       UpperCAmelCase      (		self    :			List[str]	, a_    :			str	, a_    :			Union[str, Any]	, a_    :			Union[str, Any]	, a_    :			Tuple	, a_    :			Union[str, Any]	, a_    :			Tuple    )	->	Tuple:



							'''simple docstring'''



							a__  :	Tuple		 =			self.num_labels
							a__  :	List[Any]		 =			MPNetForTokenClassification(config=a_    )
							model.to(a_    )
							model.eval()
							a__  :	List[Any]		 =			model(a_	, attention_mask=a_	, labels=a_    )
							self.parent.assertEqual(result.logits.shape	, (self.batch_size, self.seq_length, self.num_labels)    )






				def       UpperCAmelCase      (		self    :			Optional[int]    )	->	Dict:



							'''simple docstring'''



							a__  :	Any		 =			self.prepare_config_and_inputs()
							((a__) ,	(a__) ,	(a__) ,	(a__) ,	(a__) ,	(a__))  :	List[str]		 =			config_and_inputs
							a__  :	List[Any]		 =			{"input_ids": input_ids, "attention_mask": input_mask}
							return config, inputs_dict



@require_torch
class 					__UpperCAmelCase							(						_UpperCamelCase						,  _UpperCamelCase						,  unittest.TestCase  ):
				__lowerCamelCase				:	Optional[int]				     =				(
				    (
				        MPNetForMaskedLM,
				        MPNetForMultipleChoice,
				        MPNetForQuestionAnswering,
				        MPNetForSequenceClassification,
				        MPNetForTokenClassification,
				        MPNetModel,
				    )
				    if is_torch_available()
				    else ()
				)
				__lowerCamelCase				:	List[Any]				     =				(
				    {
				        "feature-extraction": MPNetModel,
				        "fill-mask": MPNetForMaskedLM,
				        "question-answering": MPNetForQuestionAnswering,
				        "text-classification": MPNetForSequenceClassification,
				        "token-classification": MPNetForTokenClassification,
				        "zero-shot": MPNetForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				__lowerCamelCase				:	List[Any]				     =				False
				__lowerCamelCase				:	Union[str, Any]				     =				True
				def       UpperCAmelCase      (		self    :			Tuple    )	->	Any:



							'''simple docstring'''



							a__  :	List[Any]		 =			MPNetModelTester(self    )
							a__  :	Optional[Any]		 =			ConfigTester(self	, config_class=a_	, hidden_size=37    )
				def       UpperCAmelCase      (		self    :			Any    )	->	int:



							'''simple docstring'''



							self.config_tester.run_common_tests()
				def       UpperCAmelCase      (		self    :			Union[str, Any]    )	->	Optional[int]:



							'''simple docstring'''



							a__  :	Optional[Any]		 =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_model(*a_    )
				def       UpperCAmelCase      (		self    :			Tuple    )	->	List[str]:



							'''simple docstring'''



							a__  :	Union[str, Any]		 =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_for_sequence_classification(*a_    )
				def       UpperCAmelCase      (		self    :			Dict    )	->	Dict:



							'''simple docstring'''



							a__  :	Optional[Any]		 =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_for_multiple_choice(*a_    )
				def       UpperCAmelCase      (		self    :			List[Any]    )	->	Dict:



							'''simple docstring'''



							a__  :	Optional[Any]		 =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_for_token_classification(*a_    )






				def       UpperCAmelCase      (		self    :			Union[str, Any]    )	->	Tuple:



							'''simple docstring'''



							a__  :	Union[str, Any]		 =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_for_question_answering(*a_    )



@require_torch
class 					__UpperCAmelCase							(						unittest.TestCase  ):
				@slow
				def       UpperCAmelCase      (		self    :			List[str]    )	->	Any:



							'''simple docstring'''



							a__  :	Optional[Any]		 =			MPNetModel.from_pretrained("microsoft/mpnet-base"    )
							a__  :	Optional[Any]		 =			torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]    )
							a__  :	Dict		 =			model(a_    )[0]
							a__  :	Tuple		 =			torch.Size((1, 11, 7_68)    )
							self.assertEqual(output.shape	, a_    )
							a__  :	Dict		 =			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]	, a_	, atol=1E-4    )    )






"""simple docstring"""






import pytest

import datasets


# Import fixture modules as plugins
__UpperCAmelCase						=			['''tests.fixtures.files''', '''tests.fixtures.hub''', '''tests.fixtures.fsspec''']






def      lowercase__ (					lowerCAmelCase__   :			Optional[Any]							,			lowerCAmelCase__   :			str			)    ->			str:



			'''simple docstring'''
			# Mark tests as "unit" by default if not marked as "integration" (or already marked as "unit")
			for item in items:
						if any(marker in item.keywords for marker in ["integration", "unit"]			):
									continue
						item.add_marker(pytest.mark.unit			)






def      lowercase__ (					lowerCAmelCase__   :			List[Any]			)    ->			Any:



			'''simple docstring'''
			config.addinivalue_line("markers"							,			"torchaudio_latest: mark test to run with torchaudio>=0.12"			)






@pytest.fixture(autouse=lowerCAmelCase__			)
def      lowercase__ (					lowerCAmelCase__   :			Optional[Any]							,			lowerCAmelCase__   :			Dict			)    ->			List[str]:



			'''simple docstring'''
			# test_hf_cache_home = tmp_path_factory.mktemp("cache")  # TODO: why a cache dir per test function does not work?
			a__  :	Dict		 =			tmp_path_factory.getbasetemp() / "cache"
			a__  :	int		 =			test_hf_cache_home / "datasets"
			a__  :	Tuple		 =			test_hf_cache_home / "metrics"
			a__  :	Any		 =			test_hf_cache_home / "modules"
			monkeypatch.setattr("datasets.config.HF_DATASETS_CACHE"							,			str(lowerCAmelCase__			)			)
			monkeypatch.setattr("datasets.config.HF_METRICS_CACHE"							,			str(lowerCAmelCase__			)			)
			monkeypatch.setattr("datasets.config.HF_MODULES_CACHE"							,			str(lowerCAmelCase__			)			)
			a__  :	Optional[int]		 =			test_hf_datasets_cache / "downloads"
			monkeypatch.setattr("datasets.config.DOWNLOADED_DATASETS_PATH"							,			str(lowerCAmelCase__			)			)
			a__  :	Tuple		 =			test_hf_datasets_cache / "downloads" / "extracted"
			monkeypatch.setattr("datasets.config.EXTRACTED_DATASETS_PATH"							,			str(lowerCAmelCase__			)			)






@pytest.fixture(autouse=lowerCAmelCase__							,			scope="session"			)
def      lowercase__ (					)    ->			Union[str, Any]:



			'''simple docstring'''
			datasets.disable_progress_bar()






@pytest.fixture(autouse=lowerCAmelCase__			)
def      lowercase__ (					lowerCAmelCase__   :			Union[str, Any]			)    ->			Optional[Any]:



			'''simple docstring'''
			# don't take tests into account when counting downloads
			monkeypatch.setattr("datasets.config.HF_UPDATE_DOWNLOAD_COUNTS"							,			lowerCAmelCase__			)






@pytest.fixture
def      lowercase__ (					lowerCAmelCase__   :			Optional[Any]			)    ->			str:



			'''simple docstring'''
			# Required to suppress RemovedIn20Warning when feature(s) are not compatible with SQLAlchemy 2.0
			# To be removed once SQLAlchemy 2.0 supported
			monkeypatch.setattr("sqlalchemy.util.deprecations.SILENCE_UBER_WARNING"							,			lowerCAmelCase__			)