




'''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       A   :







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


														"""simple docstring"""


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







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


														"""simple docstring"""


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







							def        __lowerCAmelCase      (  self	:  Optional[Any] )      ->							Tuple:


														"""simple docstring"""


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

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

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

														_a     					=    self.get_config()
														return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels







							def        __lowerCAmelCase      (  self	:  str )      ->							str:


														"""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	:  List[str]							,      lowerCAmelCase_	:  List[str]							,      lowerCAmelCase_	:  Tuple							,      lowerCAmelCase_	:  int							,      lowerCAmelCase_	:  Optional[Any]							,      lowerCAmelCase_	:  Tuple							,      lowerCAmelCase_	:  Dict )      ->							str:


														"""simple docstring"""


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


														"""simple docstring"""


														_a     					=    MPNetForQuestionAnswering(config=lowerCAmelCase_ )
														model.to(lowerCAmelCase_ )
														model.eval()
														_a     					=    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	:  Union[str, Any]							,      lowerCAmelCase_	:  List[str]							,      lowerCAmelCase_	:  Union[str, Any]							,      lowerCAmelCase_	:  Optional[int]							,      lowerCAmelCase_	:  Union[str, Any]							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  str )      ->							str:


														"""simple docstring"""


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







							def        __lowerCAmelCase      (  self	:  List[str]							,      lowerCAmelCase_	:  List[str]							,      lowerCAmelCase_	:  Optional[Any]							,      lowerCAmelCase_	:  Optional[int]							,      lowerCAmelCase_	:  Union[str, Any]							,      lowerCAmelCase_	:  Dict							,      lowerCAmelCase_	:  Any )      ->							Any:


														"""simple docstring"""


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







							def        __lowerCAmelCase      (  self	:  Tuple							,      lowerCAmelCase_	:  Tuple							,      lowerCAmelCase_	:  Optional[int]							,      lowerCAmelCase_	:  Tuple							,      lowerCAmelCase_	:  Optional[Any]							,      lowerCAmelCase_	:  Tuple							,      lowerCAmelCase_	:  Dict )      ->							List[Any]:


														"""simple docstring"""


														_a     					=    self.num_labels
														_a     					=    MPNetForTokenClassification(config=lowerCAmelCase_ )
														model.to(lowerCAmelCase_ )
														model.eval()
														_a     					=    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	:  List[str] )      ->							Any:


														"""simple docstring"""


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



@require_torch
class       A   (		_a					,_a					,unittest.TestCase   ):
							lowercase_          =    (
							    (
							        MPNetForMaskedLM,
							        MPNetForMultipleChoice,
							        MPNetForQuestionAnswering,
							        MPNetForSequenceClassification,
							        MPNetForTokenClassification,
							        MPNetModel,
							    )
							    if is_torch_available()
							    else ()
							)
							lowercase_          =    (
							    {
							        'feature-extraction': MPNetModel,
							        'fill-mask': MPNetForMaskedLM,
							        'question-answering': MPNetForQuestionAnswering,
							        'text-classification': MPNetForSequenceClassification,
							        'token-classification': MPNetForTokenClassification,
							        'zero-shot': MPNetForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							lowercase_          =    False
							lowercase_          =    True







							def        __lowerCAmelCase      (  self	:  List[str] )      ->							Tuple:


														"""simple docstring"""


														_a     					=    MPNetModelTester(self )
														_a     					=    ConfigTester(self							,      config_class=lowerCAmelCase_							,      hidden_size=37 )







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


														"""simple docstring"""


														self.config_tester.run_common_tests()







							def        __lowerCAmelCase      (  self	:  str )      ->							Any:


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_mpnet_model(*lowerCAmelCase_ )







							def        __lowerCAmelCase      (  self	:  str )      ->							int:


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_mpnet_for_sequence_classification(*lowerCAmelCase_ )







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


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_mpnet_for_multiple_choice(*lowerCAmelCase_ )







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


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_mpnet_for_token_classification(*lowerCAmelCase_ )







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


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_mpnet_for_question_answering(*lowerCAmelCase_ )



@require_torch
class       A   (		unittest.TestCase   ):







							@slow
							def        __lowerCAmelCase      (  self	:  int )      ->							str:


														"""simple docstring"""


														_a     					=    MPNetModel.from_pretrained('''microsoft/mpnet-base''' )
														_a     					=    torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]] )
														_a     					=    model(lowerCAmelCase_ )[0]
														_a     					=    torch.Size((1, 11, 7_68) )
														self.assertEqual(output.shape							,      lowerCAmelCase_ )
														_a     					=    torch.tensor(
														    [[[-0.0_5_5_0, 0.1_9_4_3, -0.0_7_4_0], [-0.0_5_6_2, 0.2_2_1_1, -0.0_5_7_9], [-0.0_4_3_7, 0.3_3_3_7, -0.0_6_4_1]]] )
														# compare the actual values for a slice.
														self.assertTrue(torch.allclose(output[:, :3, :3]							,      lowerCAmelCase_							,      atol=1e-4 ) )







'''simple docstring'''


import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


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


_snake_case				:				int       							=		get_tests_dir('fixtures')
_snake_case				:				Tuple       							=		get_tests_dir('fixtures/dummy_feature_extractor_config.json')
_snake_case				:				Optional[int]       							=		get_tests_dir('fixtures/dummy-config.json')

class       A   (		unittest.TestCase   ):







							def        __lowerCAmelCase      (  self	:  int )      ->							List[Any]:


														"""simple docstring"""


														_a     					=    0







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


														"""simple docstring"""


														_a     					=    AutoFeatureExtractor.from_pretrained('''facebook/wav2vec2-base-960h''' )
														self.assertIsInstance(lowerCAmelCase_							,      lowerCAmelCase_ )







							def        __lowerCAmelCase      (  self	:  str )      ->							Tuple:


														"""simple docstring"""


														_a     					=    AutoFeatureExtractor.from_pretrained(lowerCAmelCase_ )
														self.assertIsInstance(lowerCAmelCase_							,      lowerCAmelCase_ )







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


														"""simple docstring"""


														with tempfile.TemporaryDirectory() as tmpdirname:
																					_a     					=    WavaVecaConfig()

																					# remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
																					_a     					=    AutoFeatureExtractor.from_pretrained(lowerCAmelCase_ ).to_dict()

																					config_dict.pop('''feature_extractor_type''' )
																					_a     					=    WavaVecaFeatureExtractor(**lowerCAmelCase_ )

																					# save in new folder
																					model_config.save_pretrained(lowerCAmelCase_ )
																					config.save_pretrained(lowerCAmelCase_ )

																					_a     					=    AutoFeatureExtractor.from_pretrained(lowerCAmelCase_ )

																					# make sure private variable is not incorrectly saved
																					_a     					=    json.loads(config.to_json_string() )
																					self.assertTrue('''_processor_class''' not in dict_as_saved )

														self.assertIsInstance(lowerCAmelCase_							,      lowerCAmelCase_ )







							def        __lowerCAmelCase      (  self	:  Optional[Any] )      ->							Optional[Any]:


														"""simple docstring"""


														_a     					=    AutoFeatureExtractor.from_pretrained(lowerCAmelCase_ )
														self.assertIsInstance(lowerCAmelCase_							,      lowerCAmelCase_ )







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


														"""simple docstring"""


														with self.assertRaisesRegex(
														    lowerCAmelCase_							,      '''bert-base is not a local folder and is not a valid model identifier''' ):
																					_a     					=    AutoFeatureExtractor.from_pretrained('''bert-base''' )







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


														"""simple docstring"""


														with self.assertRaisesRegex(
														    lowerCAmelCase_							,      R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)''' ):
																					_a     					=    AutoFeatureExtractor.from_pretrained(lowerCAmelCase_							,      revision='''aaaaaa''' )







							def        __lowerCAmelCase      (  self	:  Any )      ->							Dict:


														"""simple docstring"""


														with self.assertRaisesRegex(
														    lowerCAmelCase_							,      '''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'''							,      ):
																					_a     					=    AutoFeatureExtractor.from_pretrained('''hf-internal-testing/config-no-model''' )







							def        __lowerCAmelCase      (  self	:  List[Any] )      ->							Any:


														"""simple docstring"""


														with self.assertRaises(lowerCAmelCase_ ):
																					_a     					=    AutoFeatureExtractor.from_pretrained(
																					    '''hf-internal-testing/test_dynamic_feature_extractor''' )
														# If remote code is disabled, we can't load this config.
														with self.assertRaises(lowerCAmelCase_ ):
																					_a     					=    AutoFeatureExtractor.from_pretrained(
																					    '''hf-internal-testing/test_dynamic_feature_extractor'''							,      trust_remote_code=lowerCAmelCase_ )

														_a     					=    AutoFeatureExtractor.from_pretrained(
														    '''hf-internal-testing/test_dynamic_feature_extractor'''							,      trust_remote_code=lowerCAmelCase_ )
														self.assertEqual(feature_extractor.__class__.__name__							,      '''NewFeatureExtractor''' )

														# Test feature extractor can be reloaded.
														with tempfile.TemporaryDirectory() as tmp_dir:
																					feature_extractor.save_pretrained(lowerCAmelCase_ )
																					_a     					=    AutoFeatureExtractor.from_pretrained(lowerCAmelCase_							,      trust_remote_code=lowerCAmelCase_ )
														self.assertEqual(reloaded_feature_extractor.__class__.__name__							,      '''NewFeatureExtractor''' )







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


														"""simple docstring"""


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

																					# Now that the config is registered, it can be used as any other config with the auto-API
																					_a     					=    CustomFeatureExtractor.from_pretrained(lowerCAmelCase_ )
																					with tempfile.TemporaryDirectory() as tmp_dir:
																												feature_extractor.save_pretrained(lowerCAmelCase_ )
																												_a     					=    AutoFeatureExtractor.from_pretrained(lowerCAmelCase_ )
																												self.assertIsInstance(lowerCAmelCase_							,      lowerCAmelCase_ )

														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]







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


														"""simple docstring"""


														class       A   (		_a   ):
																					lowercase_          =    True

														try:
																					AutoConfig.register('''custom'''							,      lowerCAmelCase_ )
																					AutoFeatureExtractor.register(lowerCAmelCase_							,      lowerCAmelCase_ )
																					# If remote code is not set, the default is to use local
																					_a     					=    AutoFeatureExtractor.from_pretrained(
																					    '''hf-internal-testing/test_dynamic_feature_extractor''' )
																					self.assertEqual(feature_extractor.__class__.__name__							,      '''NewFeatureExtractor''' )
																					self.assertTrue(feature_extractor.is_local )

																					# If remote code is disabled, we load the local one.
																					_a     					=    AutoFeatureExtractor.from_pretrained(
																					    '''hf-internal-testing/test_dynamic_feature_extractor'''							,      trust_remote_code=lowerCAmelCase_ )
																					self.assertEqual(feature_extractor.__class__.__name__							,      '''NewFeatureExtractor''' )
																					self.assertTrue(feature_extractor.is_local )

																					# If remote is enabled, we load from the Hub
																					_a     					=    AutoFeatureExtractor.from_pretrained(
																					    '''hf-internal-testing/test_dynamic_feature_extractor'''							,      trust_remote_code=lowerCAmelCase_ )
																					self.assertEqual(feature_extractor.__class__.__name__							,      '''NewFeatureExtractor''' )
																					self.assertTrue(not hasattr(lowerCAmelCase_							,      '''is_local''' ) )

														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]

