





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

       '''simple docstring'''

       def __init__( self					:				Tuple       ,     a__					:				Tuple       ,     a__					:				Optional[Any]=13       ,     a__					:				Tuple=7       ,     a__					:				Dict=True       ,     a__					:				Dict=True       ,     a__					:				List[Any]=False       ,     a__					:				int=True       ,     a__					:				Optional[Any]=99       ,     a__					:				Union[str, Any]=64       ,     a__					:				Optional[int]=5       ,     a__					:				Union[str, Any]=4       ,     a__					:				Any=64       ,     a__					:				Optional[int]="gelu"       ,     a__					:				Optional[int]=0.1       ,     a__					:				Dict=0.1       ,     a__					:				Optional[Any]=512       ,     a__					:				Optional[Any]=16       ,     a__					:				Dict=2       ,     a__					:				int=0.02       ,     a__					:				int=3       ,     a__					:				str=4       ,     a__					:				List[str]=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 			__snake_case      ( self					:				List[str]						):
              return MPNetConfig.from_pretrained('''microsoft/mpnet-base'''						)

       def 			__snake_case      ( self					:				str						):
              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 			__snake_case      ( self					:				Union[str, Any]						):
              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 			__snake_case      ( self					:				List[Any]       ,     a__					:				Dict       ,     a__					:				List[Any]       ,     a__					:				List[Any]       ,     a__					:				List[str]       ,     a__					:				str       ,     a__					:				str						):
              UpperCAmelCase  =						MPNetModel(config=a__						)
              model.to(a__						)
              model.eval()
              UpperCAmelCase  =						model(a__       ,     a__						)
              UpperCAmelCase  =						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 			__snake_case      ( self					:				str       ,     a__					:				Tuple       ,     a__					:				List[str]       ,     a__					:				int       ,     a__					:				List[Any]       ,     a__					:				List[Any]       ,     a__					:				Union[str, Any]						):
              UpperCAmelCase  =						MPNetForQuestionAnswering(config=a__						)
              model.to(a__						)
              model.eval()
              UpperCAmelCase  =						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 			__snake_case      ( self					:				Union[str, Any]       ,     a__					:				Any       ,     a__					:				Optional[int]       ,     a__					:				Any       ,     a__					:				Union[str, Any]       ,     a__					:				int       ,     a__					:				Optional[Any]						):
              UpperCAmelCase  =						self.num_labels
              UpperCAmelCase  =						MPNetForSequenceClassification(a__						)
              model.to(a__						)
              model.eval()
              UpperCAmelCase  =						model(a__       ,     attention_mask=a__       ,     labels=a__						)
              self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.num_labels)						)

       def 			__snake_case      ( self					:				Any       ,     a__					:				int       ,     a__					:				Any       ,     a__					:				str       ,     a__					:				Dict       ,     a__					:				Tuple       ,     a__					:				List[Any]						):
              UpperCAmelCase  =						self.num_choices
              UpperCAmelCase  =						MPNetForMultipleChoice(config=a__						)
              model.to(a__						)
              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(
                  a__       ,     attention_mask=a__       ,     labels=a__       ,     )
              self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.num_choices)						)

       def 			__snake_case      ( self					:				List[str]       ,     a__					:				Dict       ,     a__					:				List[Any]       ,     a__					:				List[Any]       ,     a__					:				int       ,     a__					:				Dict       ,     a__					:				Any						):
              UpperCAmelCase  =						self.num_labels
              UpperCAmelCase  =						MPNetForTokenClassification(config=a__						)
              model.to(a__						)
              model.eval()
              UpperCAmelCase  =						model(a__       ,     attention_mask=a__       ,     labels=a__						)
              self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.seq_length, self.num_labels)						)



       def 			__snake_case      ( self					:				str						):
              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     lowerCAmelCase__ (				UpperCAmelCase_      , UpperCAmelCase_      , unittest.TestCase    ):

       '''simple docstring'''
       _lowerCamelCase								=(
           (
               MPNetForMaskedLM,
               MPNetForMultipleChoice,
               MPNetForQuestionAnswering,
               MPNetForSequenceClassification,
               MPNetForTokenClassification,
               MPNetModel,
           )
           if is_torch_available()
           else ()
       )
       _lowerCamelCase								=(
           {
               "feature-extraction": MPNetModel,
               "fill-mask": MPNetForMaskedLM,
               "question-answering": MPNetForQuestionAnswering,
               "text-classification": MPNetForSequenceClassification,
               "token-classification": MPNetForTokenClassification,
               "zero-shot": MPNetForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       _lowerCamelCase								=False
       _lowerCamelCase								=True

       def 			__snake_case      ( self					:				Optional[int]						):
              UpperCAmelCase  =						MPNetModelTester(self						)
              UpperCAmelCase  =						ConfigTester(self       ,     config_class=a__       ,     hidden_size=37						)

       def 			__snake_case      ( self					:				Optional[int]						):
              self.config_tester.run_common_tests()

       def 			__snake_case      ( self					:				List[str]						):
              UpperCAmelCase  =						self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_mpnet_model(*a__						)

       def 			__snake_case      ( self					:				Dict						):
              UpperCAmelCase  =						self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_mpnet_for_sequence_classification(*a__						)

       def 			__snake_case      ( self					:				Any						):
              UpperCAmelCase  =						self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_mpnet_for_multiple_choice(*a__						)

       def 			__snake_case      ( self					:				Tuple						):
              UpperCAmelCase  =						self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_mpnet_for_token_classification(*a__						)



       def 			__snake_case      ( self					:				int						):
              UpperCAmelCase  =						self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_mpnet_for_question_answering(*a__						)






@require_torch
class     lowerCAmelCase__ (				unittest.TestCase    ):

       '''simple docstring'''

       @slow
       def 			__snake_case      ( self					:				Tuple						):
              UpperCAmelCase  =						MPNetModel.from_pretrained('''microsoft/mpnet-base'''						)
              UpperCAmelCase  =						torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]						)
              UpperCAmelCase  =						model(a__						)[0]
              UpperCAmelCase  =						torch.Size((1, 11, 768)						)
              self.assertEqual(output.shape       ,     a__						)
              UpperCAmelCase  =						torch.tensor(
                  [[[-0.0_550, 0.1_943, -0.0_740], [-0.0_562, 0.2_211, -0.0_579], [-0.0_437, 0.3_337, -0.0_641]]]						)
              # compare the actual values for a slice.
              self.assertTrue(torch.allclose(output[:, :3, :3]       ,     a__       ,     atol=1e-4						)						)






import sys
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

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

from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test


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

from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


__snake_case   							=						get_tests_dir('''fixtures''')



class 	__lowerCamelCase			(unittest.TestCase						):







       def 		snake_case_	(			self: int					):






            '''simple docstring'''




            __UpperCamelCase =			mock.Mock()
            __UpperCamelCase =			500
            __UpperCamelCase =			{}
            __UpperCamelCase =			HTTPError
            __UpperCamelCase =			{}

            # Download this model to make sure it's in the cache.
            __UpperCamelCase =			WavaVecaFeatureExtractor.from_pretrained('hf-internal-testing/tiny-random-wav2vec2'					)
            # Under the mock environment we get a 500 error when trying to reach the model.
            with mock.patch('requests.Session.request',return_value=A_					) as mock_head:
                 __UpperCamelCase =			WavaVecaFeatureExtractor.from_pretrained('hf-internal-testing/tiny-random-wav2vec2'					)
                 # This check we did call the fake head request
                 mock_head.assert_called()







       def 		snake_case_	(			self: Dict					):






            '''simple docstring'''




            __UpperCamelCase =			WavaVecaFeatureExtractor.from_pretrained(
                'https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json'					)



@is_staging_test
class 	__lowerCamelCase			(unittest.TestCase						):







       @classmethod
       def 		snake_case_	(			cls: Tuple					):






            '''simple docstring'''




            __UpperCamelCase =			TOKEN
            HfFolder.save_token(A_					)







       @classmethod
       def 		snake_case_	(			cls: Tuple					):






            '''simple docstring'''




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

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

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







       def 		snake_case_	(			self: Tuple					):






            '''simple docstring'''




            __UpperCamelCase =			WavaVecaFeatureExtractor.from_pretrained(A_					)
            feature_extractor.push_to_hub('test-feature-extractor',use_auth_token=self._token					)

            __UpperCamelCase =			WavaVecaFeatureExtractor.from_pretrained(F'''{USER}/test-feature-extractor'''					)
            for k, v in feature_extractor.__dict__.items():
                 self.assertEqual(A_,getattr(A_,A_					)					)

            # Reset repo
            delete_repo(token=self._token,repo_id='test-feature-extractor'					)

            # Push to hub via save_pretrained
            with tempfile.TemporaryDirectory() as tmp_dir:
                 feature_extractor.save_pretrained(
                     A_,repo_id='test-feature-extractor',push_to_hub=A_,use_auth_token=self._token					)

            __UpperCamelCase =			WavaVecaFeatureExtractor.from_pretrained(F'''{USER}/test-feature-extractor'''					)
            for k, v in feature_extractor.__dict__.items():
                 self.assertEqual(A_,getattr(A_,A_					)					)







       def 		snake_case_	(			self: List[str]					):






            '''simple docstring'''




            __UpperCamelCase =			WavaVecaFeatureExtractor.from_pretrained(A_					)
            feature_extractor.push_to_hub('valid_org/test-feature-extractor',use_auth_token=self._token					)

            __UpperCamelCase =			WavaVecaFeatureExtractor.from_pretrained('valid_org/test-feature-extractor'					)
            for k, v in feature_extractor.__dict__.items():
                 self.assertEqual(A_,getattr(A_,A_					)					)

            # Reset repo
            delete_repo(token=self._token,repo_id='valid_org/test-feature-extractor'					)

            # Push to hub via save_pretrained
            with tempfile.TemporaryDirectory() as tmp_dir:
                 feature_extractor.save_pretrained(
                     A_,repo_id='valid_org/test-feature-extractor-org',push_to_hub=A_,use_auth_token=self._token					)

            __UpperCamelCase =			WavaVecaFeatureExtractor.from_pretrained('valid_org/test-feature-extractor-org'					)
            for k, v in feature_extractor.__dict__.items():
                 self.assertEqual(A_,getattr(A_,A_					)					)







       def 		snake_case_	(			self: int					):






            '''simple docstring'''




            CustomFeatureExtractor.register_for_auto_class()
            __UpperCamelCase =			CustomFeatureExtractor.from_pretrained(A_					)

            feature_extractor.push_to_hub('test-dynamic-feature-extractor',use_auth_token=self._token					)

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

            __UpperCamelCase =			AutoFeatureExtractor.from_pretrained(
                F'''{USER}/test-dynamic-feature-extractor''',trust_remote_code=A_					)
            # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module
            self.assertEqual(new_feature_extractor.__class__.__name__,'CustomFeatureExtractor'					)


