
"""simple docstring"""



import json
import os
import sys
import tempfile
import unittest
from pathlib import Path
from shutil import copyfile

from huggingface_hub import HfFolder, Repository, create_repo, delete_repo
from requests.exceptions import HTTPError

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    PROCESSOR_MAPPING,
    TOKENIZER_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    AutoProcessor,
    AutoTokenizer,
    BertTokenizer,
    ProcessorMixin,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
    WavaVecaProcessor,
)
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test
from transformers.tokenization_utils import TOKENIZER_CONFIG_FILE
from transformers.utils import FEATURE_EXTRACTOR_NAME, is_tokenizers_available


sys.path.append(str(Path(__file__).parent.parent.parent.parent / '''utils'''))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402
from test_module.custom_processing import CustomProcessor  # noqa E402
from test_module.custom_tokenization import CustomTokenizer  # noqa E402


_snake_case          =						get_tests_dir('''fixtures/dummy_feature_extractor_config.json''')
_snake_case          =						get_tests_dir('''fixtures/vocab.json''')
_snake_case          =						get_tests_dir('''fixtures''')
class        _SCREAMING_SNAKE_CASE  (  unittest.TestCase							):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE_:							Tuple							=					["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """bla""", """blou"""]


				def 				__lowerCamelCase	(					self	:			Tuple  )		->  Any:







											"""simple docstring"""




											_lowerCAmelCase  	=			0


				def 				__lowerCamelCase	(					self	:			List[Any]  )		->  str:







											"""simple docstring"""




											_lowerCAmelCase  	=			AutoProcessor.from_pretrained('facebook/wav2vec2-base-960h'  )
											self.assertIsInstance(__lowercase				,     __lowercase  )


				def 				__lowerCamelCase	(					self	:			List[Any]  )		->  Optional[Any]:







											"""simple docstring"""




											with tempfile.TemporaryDirectory() as tmpdirname:
																		_lowerCAmelCase  	=			WavaVecaConfig()
																		_lowerCAmelCase  	=			AutoProcessor.from_pretrained('facebook/wav2vec2-base-960h'  )

																		# save in new folder
																		model_config.save_pretrained(__lowercase  )
																		processor.save_pretrained(__lowercase  )

																		_lowerCAmelCase  	=			AutoProcessor.from_pretrained(__lowercase  )

											self.assertIsInstance(__lowercase				,     __lowercase  )


				def 				__lowerCamelCase	(					self	:			Optional[Any]  )		->  Union[str, Any]:







											"""simple docstring"""




											with tempfile.TemporaryDirectory() as tmpdirname:
																		# copy relevant files
																		copyfile(__lowercase				,     os.path.join(__lowercase				,     __lowercase  )  )
																		copyfile(__lowercase				,     os.path.join(__lowercase				,     'vocab.json'  )  )

																		_lowerCAmelCase  	=			AutoProcessor.from_pretrained(__lowercase  )

											self.assertIsInstance(__lowercase				,     __lowercase  )


				def 				__lowerCamelCase	(					self	:			List[str]  )		->  Optional[int]:







											"""simple docstring"""




											with tempfile.TemporaryDirectory() as tmpdirname:
																		_lowerCAmelCase  	=			WavaVecaFeatureExtractor()
																		_lowerCAmelCase  	=			AutoTokenizer.from_pretrained('facebook/wav2vec2-base-960h'  )

																		_lowerCAmelCase  	=			WavaVecaProcessor(__lowercase				,     __lowercase  )

																		# save in new folder
																		processor.save_pretrained(__lowercase  )

																		# drop `processor_class` in tokenizer
																		with open(os.path.join(__lowercase				,     __lowercase  )				,     'r'  ) as f:
																									_lowerCAmelCase  	=			json.load(__lowercase  )
																									config_dict.pop('processor_class'  )

																		with open(os.path.join(__lowercase				,     __lowercase  )				,     'w'  ) as f:
																									f.write(json.dumps(__lowercase  )  )

																		_lowerCAmelCase  	=			AutoProcessor.from_pretrained(__lowercase  )

											self.assertIsInstance(__lowercase				,     __lowercase  )


				def 				__lowerCamelCase	(					self	:			Dict  )		->  List[str]:







											"""simple docstring"""




											with tempfile.TemporaryDirectory() as tmpdirname:
																		_lowerCAmelCase  	=			WavaVecaFeatureExtractor()
																		_lowerCAmelCase  	=			AutoTokenizer.from_pretrained('facebook/wav2vec2-base-960h'  )

																		_lowerCAmelCase  	=			WavaVecaProcessor(__lowercase				,     __lowercase  )

																		# save in new folder
																		processor.save_pretrained(__lowercase  )

																		# drop `processor_class` in feature extractor
																		with open(os.path.join(__lowercase				,     __lowercase  )				,     'r'  ) as f:
																									_lowerCAmelCase  	=			json.load(__lowercase  )
																									config_dict.pop('processor_class'  )

																		with open(os.path.join(__lowercase				,     __lowercase  )				,     'w'  ) as f:
																									f.write(json.dumps(__lowercase  )  )

																		_lowerCAmelCase  	=			AutoProcessor.from_pretrained(__lowercase  )

											self.assertIsInstance(__lowercase				,     __lowercase  )


				def 				__lowerCamelCase	(					self	:			Dict  )		->  str:







											"""simple docstring"""




											with tempfile.TemporaryDirectory() as tmpdirname:
																		_lowerCAmelCase  	=			WavaVecaConfig(processor_class='Wav2Vec2Processor'  )
																		model_config.save_pretrained(__lowercase  )
																		# copy relevant files
																		copyfile(__lowercase				,     os.path.join(__lowercase				,     'vocab.json'  )  )
																		# create emtpy sample processor
																		with open(os.path.join(__lowercase				,     __lowercase  )				,     'w'  ) as f:
																									f.write('{}'  )

																		_lowerCAmelCase  	=			AutoProcessor.from_pretrained(__lowercase  )

											self.assertIsInstance(__lowercase				,     __lowercase  )


				def 				__lowerCamelCase	(					self	:			List[Any]  )		->  Union[str, Any]:







											"""simple docstring"""




											with self.assertRaises(__lowercase  ):
																		_lowerCAmelCase  	=			AutoProcessor.from_pretrained('hf-internal-testing/test_dynamic_processor'  )
											# If remote code is disabled, we can't load this config.
											with self.assertRaises(__lowercase  ):
																		_lowerCAmelCase  	=			AutoProcessor.from_pretrained(
																		    'hf-internal-testing/test_dynamic_processor'				,     trust_remote_code=__lowercase  )

											_lowerCAmelCase  	=			AutoProcessor.from_pretrained('hf-internal-testing/test_dynamic_processor'				,     trust_remote_code=__lowercase  )
											self.assertTrue(processor.special_attribute_present  )
											self.assertEqual(processor.__class__.__name__				,     'NewProcessor'  )

											_lowerCAmelCase  	=			processor.feature_extractor
											self.assertTrue(feature_extractor.special_attribute_present  )
											self.assertEqual(feature_extractor.__class__.__name__				,     'NewFeatureExtractor'  )

											_lowerCAmelCase  	=			processor.tokenizer
											self.assertTrue(tokenizer.special_attribute_present  )
											if is_tokenizers_available():
																		self.assertEqual(tokenizer.__class__.__name__				,     'NewTokenizerFast'  )

																		# Test we can also load the slow version
																		_lowerCAmelCase  	=			AutoProcessor.from_pretrained(
																		    'hf-internal-testing/test_dynamic_processor'				,     trust_remote_code=__lowercase				,     use_fast=__lowercase  )
																		_lowerCAmelCase  	=			new_processor.tokenizer
																		self.assertTrue(new_tokenizer.special_attribute_present  )
																		self.assertEqual(new_tokenizer.__class__.__name__				,     'NewTokenizer'  )
											else:
																		self.assertEqual(tokenizer.__class__.__name__				,     'NewTokenizer'  )


				def 				__lowerCamelCase	(					self	:			int  )		->  List[str]:







											"""simple docstring"""




											try:
																		AutoConfig.register('custom'				,     __lowercase  )
																		AutoFeatureExtractor.register(__lowercase				,     __lowercase  )
																		AutoTokenizer.register(__lowercase				,     slow_tokenizer_class=__lowercase  )
																		AutoProcessor.register(__lowercase				,     __lowercase  )
																		# Trying to register something existing in the Transformers library will raise an error
																		with self.assertRaises(__lowercase  ):
																									AutoProcessor.register(__lowercase				,     __lowercase  )

																		# Now that the config is registered, it can be used as any other config with the auto-API
																		_lowerCAmelCase  	=			CustomFeatureExtractor.from_pretrained(__lowercase  )

																		with tempfile.TemporaryDirectory() as tmp_dir:
																									_lowerCAmelCase  	=			os.path.join(__lowercase				,     'vocab.txt'  )
																									with open(__lowercase				,     'w'				,     encoding='utf-8'  ) as vocab_writer:
																																vocab_writer.write(''.join([x + '\n' for x in self.vocab_tokens]  )  )
																									_lowerCAmelCase  	=			CustomTokenizer(__lowercase  )

																		_lowerCAmelCase  	=			CustomProcessor(__lowercase				,     __lowercase  )

																		with tempfile.TemporaryDirectory() as tmp_dir:
																									processor.save_pretrained(__lowercase  )
																									_lowerCAmelCase  	=			AutoProcessor.from_pretrained(__lowercase  )
																									self.assertIsInstance(__lowercase				,     __lowercase  )

											finally:
																		if "custom" in CONFIG_MAPPING._extra_content:
																									del CONFIG_MAPPING._extra_content["custom"]
																		if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
																									del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]
																		if CustomConfig in TOKENIZER_MAPPING._extra_content:
																									del TOKENIZER_MAPPING._extra_content[CustomConfig]
																		if CustomConfig in PROCESSOR_MAPPING._extra_content:
																									del PROCESSOR_MAPPING._extra_content[CustomConfig]


				def 				__lowerCamelCase	(					self	:			Dict  )		->  Optional[Any]:







											"""simple docstring"""




											class        _SCREAMING_SNAKE_CASE  (  lowercase__							):






															'''simple docstring'''



															SCREAMING_SNAKE_CASE_:							Union[str, Any]							=					False






											class        _SCREAMING_SNAKE_CASE  (  lowercase__							):






															'''simple docstring'''



															SCREAMING_SNAKE_CASE_:							Dict							=					False






											class        _SCREAMING_SNAKE_CASE  (  lowercase__							):






															'''simple docstring'''



															SCREAMING_SNAKE_CASE_:							Union[str, Any]							=					"""AutoFeatureExtractor"""
															SCREAMING_SNAKE_CASE_:							List[str]							=					"""AutoTokenizer"""
															SCREAMING_SNAKE_CASE_:							Union[str, Any]							=					False

											try:
																		AutoConfig.register('custom'				,     __lowercase  )
																		AutoFeatureExtractor.register(__lowercase				,     __lowercase  )
																		AutoTokenizer.register(__lowercase				,     slow_tokenizer_class=__lowercase  )
																		AutoProcessor.register(__lowercase				,     __lowercase  )
																		# If remote code is not set, the default is to use local classes.
																		_lowerCAmelCase  	=			AutoProcessor.from_pretrained('hf-internal-testing/test_dynamic_processor'  )
																		self.assertEqual(processor.__class__.__name__				,     'NewProcessor'  )
																		self.assertFalse(processor.special_attribute_present  )
																		self.assertFalse(processor.feature_extractor.special_attribute_present  )
																		self.assertFalse(processor.tokenizer.special_attribute_present  )

																		# If remote code is disabled, we load the local ones.
																		_lowerCAmelCase  	=			AutoProcessor.from_pretrained(
																		    'hf-internal-testing/test_dynamic_processor'				,     trust_remote_code=__lowercase  )
																		self.assertEqual(processor.__class__.__name__				,     'NewProcessor'  )
																		self.assertFalse(processor.special_attribute_present  )
																		self.assertFalse(processor.feature_extractor.special_attribute_present  )
																		self.assertFalse(processor.tokenizer.special_attribute_present  )

																		# If remote is enabled, we load from the Hub.
																		_lowerCAmelCase  	=			AutoProcessor.from_pretrained(
																		    'hf-internal-testing/test_dynamic_processor'				,     trust_remote_code=__lowercase  )
																		self.assertEqual(processor.__class__.__name__				,     'NewProcessor'  )
																		self.assertTrue(processor.special_attribute_present  )
																		self.assertTrue(processor.feature_extractor.special_attribute_present  )
																		self.assertTrue(processor.tokenizer.special_attribute_present  )

											finally:
																		if "custom" in CONFIG_MAPPING._extra_content:
																									del CONFIG_MAPPING._extra_content["custom"]
																		if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
																									del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]
																		if CustomConfig in TOKENIZER_MAPPING._extra_content:
																									del TOKENIZER_MAPPING._extra_content[CustomConfig]
																		if CustomConfig in PROCESSOR_MAPPING._extra_content:
																									del PROCESSOR_MAPPING._extra_content[CustomConfig]


				def 				__lowerCamelCase	(					self	:			Optional[Any]  )		->  Optional[int]:







											"""simple docstring"""




											_lowerCAmelCase  	=			AutoProcessor.from_pretrained('hf-internal-testing/tiny-random-bert'  )
											self.assertEqual(processor.__class__.__name__				,     'BertTokenizerFast'  )







				def 				__lowerCamelCase	(					self	:			str  )		->  Any:







											"""simple docstring"""




											_lowerCAmelCase  	=			AutoProcessor.from_pretrained('hf-internal-testing/tiny-random-convnext'  )
											self.assertEqual(processor.__class__.__name__				,     'ConvNextImageProcessor'  )






@is_staging_test
class        _SCREAMING_SNAKE_CASE  (  unittest.TestCase							):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE_:							List[Any]							=					["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """bla""", """blou"""]


				@classmethod
				def 				__lowerCamelCase	(					cls	:			Union[str, Any]  )		->  Dict:







											"""simple docstring"""




											_lowerCAmelCase  	=			TOKEN
											HfFolder.save_token(__lowercase  )


				@classmethod
				def 				__lowerCamelCase	(					cls	:			int  )		->  Optional[Any]:







											"""simple docstring"""




											try:
																		delete_repo(token=cls._token				,     repo_id='test-processor'  )
											except HTTPError:
																		pass

											try:
																		delete_repo(token=cls._token				,     repo_id='valid_org/test-processor-org'  )
											except HTTPError:
																		pass

											try:
																		delete_repo(token=cls._token				,     repo_id='test-dynamic-processor'  )
											except HTTPError:
																		pass


				def 				__lowerCamelCase	(					self	:			Dict  )		->  Tuple:







											"""simple docstring"""




											_lowerCAmelCase  	=			WavaVecaProcessor.from_pretrained(__lowercase  )
											with tempfile.TemporaryDirectory() as tmp_dir:
																		processor.save_pretrained(
																		    os.path.join(__lowercase				,     'test-processor'  )				,     push_to_hub=__lowercase				,     use_auth_token=self._token  )

																		_lowerCAmelCase  	=			WavaVecaProcessor.from_pretrained(F"""{USER}/test-processor"""  )
																		for k, v in processor.feature_extractor.__dict__.items():
																									self.assertEqual(__lowercase				,     getattr(new_processor.feature_extractor				,     __lowercase  )  )
																		self.assertDictEqual(new_processor.tokenizer.get_vocab()				,     processor.tokenizer.get_vocab()  )


				def 				__lowerCamelCase	(					self	:			Tuple  )		->  Optional[int]:







											"""simple docstring"""




											_lowerCAmelCase  	=			WavaVecaProcessor.from_pretrained(__lowercase  )

											with tempfile.TemporaryDirectory() as tmp_dir:
																		processor.save_pretrained(
																		    os.path.join(__lowercase				,     'test-processor-org'  )				,     push_to_hub=__lowercase				,     use_auth_token=self._token				,     organization='valid_org'				,     )

																		_lowerCAmelCase  	=			WavaVecaProcessor.from_pretrained('valid_org/test-processor-org'  )
																		for k, v in processor.feature_extractor.__dict__.items():
																									self.assertEqual(__lowercase				,     getattr(new_processor.feature_extractor				,     __lowercase  )  )
																		self.assertDictEqual(new_processor.tokenizer.get_vocab()				,     processor.tokenizer.get_vocab()  )







				def 				__lowerCamelCase	(					self	:			List[Any]  )		->  Optional[Any]:







											"""simple docstring"""




											CustomFeatureExtractor.register_for_auto_class()
											CustomTokenizer.register_for_auto_class()
											CustomProcessor.register_for_auto_class()

											_lowerCAmelCase  	=			CustomFeatureExtractor.from_pretrained(__lowercase  )

											with tempfile.TemporaryDirectory() as tmp_dir:
																		_lowerCAmelCase  	=			os.path.join(__lowercase				,     'vocab.txt'  )
																		with open(__lowercase				,     'w'				,     encoding='utf-8'  ) as vocab_writer:
																									vocab_writer.write(''.join([x + '\n' for x in self.vocab_tokens]  )  )
																		_lowerCAmelCase  	=			CustomTokenizer(__lowercase  )

											_lowerCAmelCase  	=			CustomProcessor(__lowercase				,     __lowercase  )

											with tempfile.TemporaryDirectory() as tmp_dir:
																		create_repo(F"""{USER}/test-dynamic-processor"""				,     token=self._token  )
																		_lowerCAmelCase  	=			Repository(__lowercase				,     clone_from=F"""{USER}/test-dynamic-processor"""				,     token=self._token  )
																		processor.save_pretrained(__lowercase  )

																		# This has added the proper auto_map field to the feature extractor config
																		self.assertDictEqual(
																		    processor.feature_extractor.auto_map				,     {
																		        'AutoFeatureExtractor': 'custom_feature_extraction.CustomFeatureExtractor',
																		        'AutoProcessor': 'custom_processing.CustomProcessor',
																		    }				,     )

																		# This has added the proper auto_map field to the tokenizer config
																		with open(os.path.join(__lowercase				,     'tokenizer_config.json'  )  ) as f:
																									_lowerCAmelCase  	=			json.load(__lowercase  )
																		self.assertDictEqual(
																		    tokenizer_config['auto_map']				,     {
																		        'AutoTokenizer': ['custom_tokenization.CustomTokenizer', None],
																		        'AutoProcessor': 'custom_processing.CustomProcessor',
																		    }				,     )

																		# The code has been copied from fixtures
																		self.assertTrue(os.path.isfile(os.path.join(__lowercase				,     'custom_feature_extraction.py'  )  )  )
																		self.assertTrue(os.path.isfile(os.path.join(__lowercase				,     'custom_tokenization.py'  )  )  )
																		self.assertTrue(os.path.isfile(os.path.join(__lowercase				,     'custom_processing.py'  )  )  )

																		repo.push_to_hub()

											_lowerCAmelCase  	=			AutoProcessor.from_pretrained(F"""{USER}/test-dynamic-processor"""				,     trust_remote_code=__lowercase  )
											# Can't make an isinstance check because the new_processor is from the CustomProcessor class of a dynamic module
											self.assertEqual(new_processor.__class__.__name__				,     'CustomProcessor'  )


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






				'''simple docstring'''



				def __init__(					self	:			Tuple				,     UpperCAmelCase_	:			Tuple				,     UpperCAmelCase_	:			str=13				,     UpperCAmelCase_	:			Dict=7				,     UpperCAmelCase_	:			Tuple=True				,     UpperCAmelCase_	:			Any=True				,     UpperCAmelCase_	:			Optional[int]=False				,     UpperCAmelCase_	:			Optional[Any]=True				,     UpperCAmelCase_	:			Optional[Any]=99				,     UpperCAmelCase_	:			int=64				,     UpperCAmelCase_	:			Union[str, Any]=5				,     UpperCAmelCase_	:			Union[str, Any]=4				,     UpperCAmelCase_	:			List[Any]=64				,     UpperCAmelCase_	:			List[str]="gelu"				,     UpperCAmelCase_	:			Optional[Any]=0.1				,     UpperCAmelCase_	:			Dict=0.1				,     UpperCAmelCase_	:			Tuple=512				,     UpperCAmelCase_	:			Optional[Any]=16				,     UpperCAmelCase_	:			str=2				,     UpperCAmelCase_	:			int=0.02				,     UpperCAmelCase_	:			List[str]=3				,     UpperCAmelCase_	:			int=4				,     UpperCAmelCase_	:			Tuple=None				,     )		->  Optional[int]:







											"""simple docstring"""




											_lowerCAmelCase  	=			parent
											_lowerCAmelCase  	=			batch_size
											_lowerCAmelCase  	=			seq_length
											_lowerCAmelCase  	=			is_training
											_lowerCAmelCase  	=			use_input_mask
											_lowerCAmelCase  	=			use_token_type_ids
											_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	:			List[str]  )		->  Any:







											"""simple docstring"""




											return MPNetConfig.from_pretrained('microsoft/mpnet-base'  )


				def 				__lowerCamelCase	(					self	:			List[Any]  )		->  Tuple:







											"""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	:			Any  )		->  Tuple:







											"""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 				__lowerCamelCase	(					self	:			Dict				,     UpperCAmelCase_	:			Optional[int]				,     UpperCAmelCase_	:			Dict				,     UpperCAmelCase_	:			Optional[int]				,     UpperCAmelCase_	:			Dict				,     UpperCAmelCase_	:			Tuple				,     UpperCAmelCase_	:			Optional[int]  )		->  Optional[Any]:







											"""simple docstring"""




											_lowerCAmelCase  	=			MPNetModel(config=UpperCAmelCase_  )
											model.to(UpperCAmelCase_  )
											model.eval()
											_lowerCAmelCase  	=			model(UpperCAmelCase_				,     UpperCAmelCase_  )
											_lowerCAmelCase  	=			model(UpperCAmelCase_  )
											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	:			List[str]				,     UpperCAmelCase_	:			Dict				,     UpperCAmelCase_	:			str				,     UpperCAmelCase_	:			List[Any]				,     UpperCAmelCase_	:			str				,     UpperCAmelCase_	:			str				,     UpperCAmelCase_	:			int  )		->  Optional[Any]:







											"""simple docstring"""




											_lowerCAmelCase  	=			MPNetForQuestionAnswering(config=UpperCAmelCase_  )
											model.to(UpperCAmelCase_  )
											model.eval()
											_lowerCAmelCase  	=			model(
											    UpperCAmelCase_				,     attention_mask=UpperCAmelCase_				,     start_positions=UpperCAmelCase_				,     end_positions=UpperCAmelCase_				,     )
											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	:			Dict				,     UpperCAmelCase_	:			str				,     UpperCAmelCase_	:			Tuple				,     UpperCAmelCase_	:			Tuple				,     UpperCAmelCase_	:			Union[str, Any]				,     UpperCAmelCase_	:			Optional[Any]				,     UpperCAmelCase_	:			Union[str, Any]  )		->  Any:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.num_labels
											_lowerCAmelCase  	=			MPNetForSequenceClassification(UpperCAmelCase_  )
											model.to(UpperCAmelCase_  )
											model.eval()
											_lowerCAmelCase  	=			model(UpperCAmelCase_				,     attention_mask=UpperCAmelCase_				,     labels=UpperCAmelCase_  )
											self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_labels)  )


				def 				__lowerCamelCase	(					self	:			int				,     UpperCAmelCase_	:			Dict				,     UpperCAmelCase_	:			Any				,     UpperCAmelCase_	:			Optional[int]				,     UpperCAmelCase_	:			Optional[Any]				,     UpperCAmelCase_	:			Any				,     UpperCAmelCase_	:			List[Any]  )		->  Dict:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.num_choices
											_lowerCAmelCase  	=			MPNetForMultipleChoice(config=UpperCAmelCase_  )
											model.to(UpperCAmelCase_  )
											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(
											    UpperCAmelCase_				,     attention_mask=UpperCAmelCase_				,     labels=UpperCAmelCase_				,     )
											self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_choices)  )


				def 				__lowerCamelCase	(					self	:			Tuple				,     UpperCAmelCase_	:			Tuple				,     UpperCAmelCase_	:			Optional[Any]				,     UpperCAmelCase_	:			Dict				,     UpperCAmelCase_	:			List[str]				,     UpperCAmelCase_	:			str				,     UpperCAmelCase_	:			List[str]  )		->  Union[str, Any]:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.num_labels
											_lowerCAmelCase  	=			MPNetForTokenClassification(config=UpperCAmelCase_  )
											model.to(UpperCAmelCase_  )
											model.eval()
											_lowerCAmelCase  	=			model(UpperCAmelCase_				,     attention_mask=UpperCAmelCase_				,     labels=UpperCAmelCase_  )
											self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.num_labels)  )







				def 				__lowerCamelCase	(					self	:			str  )		->  List[str]:







											"""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        _SCREAMING_SNAKE_CASE  (  UpperCAmelCase      ,				UpperCAmelCase      ,				unittest.TestCase							):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE_:							Union[str, Any]							=					(
				    (
				        MPNetForMaskedLM,
				        MPNetForMultipleChoice,
				        MPNetForQuestionAnswering,
				        MPNetForSequenceClassification,
				        MPNetForTokenClassification,
				        MPNetModel,
				    )
				    if is_torch_available()
				    else ()
				)
				SCREAMING_SNAKE_CASE_:							int							=					(
				    {
				        "feature-extraction": MPNetModel,
				        "fill-mask": MPNetForMaskedLM,
				        "question-answering": MPNetForQuestionAnswering,
				        "text-classification": MPNetForSequenceClassification,
				        "token-classification": MPNetForTokenClassification,
				        "zero-shot": MPNetForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				SCREAMING_SNAKE_CASE_:							Optional[Any]							=					False
				SCREAMING_SNAKE_CASE_:							Dict							=					True


				def 				__lowerCamelCase	(					self	:			str  )		->  List[Any]:







											"""simple docstring"""




											_lowerCAmelCase  	=			MPNetModelTester(self  )
											_lowerCAmelCase  	=			ConfigTester(self				,     config_class=UpperCAmelCase_				,     hidden_size=37  )


				def 				__lowerCamelCase	(					self	:			Dict  )		->  Dict:







											"""simple docstring"""




											self.config_tester.run_common_tests()


				def 				__lowerCamelCase	(					self	:			Tuple  )		->  str:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mpnet_model(*UpperCAmelCase_  )


				def 				__lowerCamelCase	(					self	:			List[Any]  )		->  Union[str, Any]:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mpnet_for_sequence_classification(*UpperCAmelCase_  )


				def 				__lowerCamelCase	(					self	:			Optional[int]  )		->  int:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mpnet_for_multiple_choice(*UpperCAmelCase_  )


				def 				__lowerCamelCase	(					self	:			Any  )		->  List[str]:







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mpnet_for_token_classification(*UpperCAmelCase_  )







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







											"""simple docstring"""




											_lowerCAmelCase  	=			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_mpnet_for_question_answering(*UpperCAmelCase_  )






@require_torch
class        _SCREAMING_SNAKE_CASE  (  unittest.TestCase							):






				'''simple docstring'''



				@slow
				def 				__lowerCamelCase	(					self	:			List[Any]  )		->  List[Any]:







											"""simple docstring"""




											_lowerCAmelCase  	=			MPNetModel.from_pretrained('microsoft/mpnet-base'  )
											_lowerCAmelCase  	=			torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]  )
											_lowerCAmelCase  	=			model(UpperCAmelCase_  )[0]
											_lowerCAmelCase  	=			torch.Size((1, 11, 768)  )
											self.assertEqual(output.shape				,     UpperCAmelCase_  )
											_lowerCAmelCase  	=			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]				,     UpperCAmelCase_				,     atol=1E-4  )  )






