


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


A   :					Dict	=      get_tests_dir("fixtures")
A   :					str	=      get_tests_dir("fixtures/dummy_feature_extractor_config.json")
A   :					Any	=      get_tests_dir("fixtures/dummy-config.json")







class     _lowercase				(							unittest.TestCase):


       """simple docstring"""




       def 		lowerCAmelCase  (     self							:    Any ):


             '''simple docstring'''



             lowerCamelCase__	:       List[Any]					=						0





       def 		lowerCAmelCase  (     self							:    Optional[Any] ):


             '''simple docstring'''



             lowerCamelCase__	:       Optional[int]					=						AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base-960h" )
             self.assertIsInstance(snake_case_      ,   snake_case_ )





       def 		lowerCAmelCase  (     self							:    Union[str, Any] ):


             '''simple docstring'''



             lowerCamelCase__	:       Optional[Any]					=						AutoFeatureExtractor.from_pretrained(snake_case_ )
             self.assertIsInstance(snake_case_      ,   snake_case_ )





       def 		lowerCAmelCase  (     self							:    str ):


             '''simple docstring'''



             with tempfile.TemporaryDirectory() as tmpdirname:
                   lowerCamelCase__	:       Dict					=						WavaVecaConfig()

                   # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
                   lowerCamelCase__	:       str					=						AutoFeatureExtractor.from_pretrained(snake_case_ ).to_dict()

                   config_dict.pop("feature_extractor_type" )
                   lowerCamelCase__	:       Optional[int]					=						WavaVecaFeatureExtractor(**snake_case_ )

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

                   lowerCamelCase__	:       List[str]					=						AutoFeatureExtractor.from_pretrained(snake_case_ )

                   # make sure private variable is not incorrectly saved
                   lowerCamelCase__	:       List[str]					=						json.loads(config.to_json_string() )
                   self.assertTrue("_processor_class" not in dict_as_saved )

             self.assertIsInstance(snake_case_      ,   snake_case_ )





       def 		lowerCAmelCase  (     self							:    List[str] ):


             '''simple docstring'''



             lowerCamelCase__	:       List[Any]					=						AutoFeatureExtractor.from_pretrained(snake_case_ )
             self.assertIsInstance(snake_case_      ,   snake_case_ )





       def 		lowerCAmelCase  (     self							:    str ):


             '''simple docstring'''



             with self.assertRaisesRegex(
                 snake_case_      ,   "bert-base is not a local folder and is not a valid model identifier" ):
                   lowerCamelCase__	:       int					=						AutoFeatureExtractor.from_pretrained("bert-base" )





       def 		lowerCAmelCase  (     self							:    Dict ):


             '''simple docstring'''



             with self.assertRaisesRegex(
                 snake_case_      ,   R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)" ):
                   lowerCamelCase__	:       List[Any]					=						AutoFeatureExtractor.from_pretrained(snake_case_      ,   revision="aaaaaa" )





       def 		lowerCAmelCase  (     self							:    str ):


             '''simple docstring'''



             with self.assertRaisesRegex(
                 snake_case_      ,   "hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."      ,   ):
                   lowerCamelCase__	:       Optional[Any]					=						AutoFeatureExtractor.from_pretrained("hf-internal-testing/config-no-model" )





       def 		lowerCAmelCase  (     self							:    str ):


             '''simple docstring'''



             with self.assertRaises(snake_case_ ):
                   lowerCamelCase__	:       List[str]					=						AutoFeatureExtractor.from_pretrained(
                       "hf-internal-testing/test_dynamic_feature_extractor" )
             # If remote code is disabled, we can't load this config.
             with self.assertRaises(snake_case_ ):
                   lowerCamelCase__	:       List[str]					=						AutoFeatureExtractor.from_pretrained(
                       "hf-internal-testing/test_dynamic_feature_extractor"      ,   trust_remote_code=snake_case_ )

             lowerCamelCase__	:       Optional[int]					=						AutoFeatureExtractor.from_pretrained(
                 "hf-internal-testing/test_dynamic_feature_extractor"      ,   trust_remote_code=snake_case_ )
             self.assertEqual(feature_extractor.__class__.__name__      ,   "NewFeatureExtractor" )

             # Test feature extractor can be reloaded.
             with tempfile.TemporaryDirectory() as tmp_dir:
                   feature_extractor.save_pretrained(snake_case_ )
                   lowerCamelCase__	:       List[str]					=						AutoFeatureExtractor.from_pretrained(snake_case_      ,   trust_remote_code=snake_case_ )
             self.assertEqual(reloaded_feature_extractor.__class__.__name__      ,   "NewFeatureExtractor" )





       def 		lowerCAmelCase  (     self							:    List[Any] ):


             '''simple docstring'''



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

                   # Now that the config is registered, it can be used as any other config with the auto-API
                   lowerCamelCase__	:       Any					=						CustomFeatureExtractor.from_pretrained(snake_case_ )
                   with tempfile.TemporaryDirectory() as tmp_dir:
                         feature_extractor.save_pretrained(snake_case_ )
                         lowerCamelCase__	:       Union[str, Any]					=						AutoFeatureExtractor.from_pretrained(snake_case_ )
                         self.assertIsInstance(snake_case_      ,   snake_case_ )

             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							:    str ):


             '''simple docstring'''



             class     _lowercase				(							_a):


                    """simple docstring"""




                    A__      		=    True

             try:
                   AutoConfig.register("custom"      ,   snake_case_ )
                   AutoFeatureExtractor.register(snake_case_      ,   snake_case_ )
                   # If remote code is not set, the default is to use local
                   lowerCamelCase__	:       Union[str, Any]					=						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.
                   lowerCamelCase__	:       str					=						AutoFeatureExtractor.from_pretrained(
                       "hf-internal-testing/test_dynamic_feature_extractor"      ,   trust_remote_code=snake_case_ )
                   self.assertEqual(feature_extractor.__class__.__name__      ,   "NewFeatureExtractor" )
                   self.assertTrue(feature_extractor.is_local )

                   # If remote is enabled, we load from the Hub
                   lowerCamelCase__	:       Union[str, Any]					=						AutoFeatureExtractor.from_pretrained(
                       "hf-internal-testing/test_dynamic_feature_extractor"      ,   trust_remote_code=snake_case_ )
                   self.assertEqual(feature_extractor.__class__.__name__      ,   "NewFeatureExtractor" )
                   self.assertTrue(not hasattr(snake_case_      ,   "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]






from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


A   :					Dict	=      logging.get_logger(__name__)

A   :					Union[str, Any]	=      {
    "roberta-base": "https://huggingface.co/roberta-base/resolve/main/config.json",
    "roberta-large": "https://huggingface.co/roberta-large/resolve/main/config.json",
    "roberta-large-mnli": "https://huggingface.co/roberta-large-mnli/resolve/main/config.json",
    "distilroberta-base": "https://huggingface.co/distilroberta-base/resolve/main/config.json",
    "roberta-base-openai-detector": "https://huggingface.co/roberta-base-openai-detector/resolve/main/config.json",
    "roberta-large-openai-detector": "https://huggingface.co/roberta-large-openai-detector/resolve/main/config.json",
}







class     _lowercase				(							lowercase__):


       """simple docstring"""




       A__      		=    "roberta"





       def __init__(     self							:    int      ,   __lowerCamelCase							:    Dict=50265      ,   __lowerCamelCase							:    Optional[int]=768      ,   __lowerCamelCase							:    Optional[Any]=12      ,   __lowerCamelCase							:    Optional[int]=12      ,   __lowerCamelCase							:    int=3072      ,   __lowerCamelCase							:    Dict="gelu"      ,   __lowerCamelCase							:    Union[str, Any]=0.1      ,   __lowerCamelCase							:    int=0.1      ,   __lowerCamelCase							:    Tuple=512      ,   __lowerCamelCase							:    List[str]=2      ,   __lowerCamelCase							:    Any=0.0_2      ,   __lowerCamelCase							:    Optional[int]=1E-1_2      ,   __lowerCamelCase							:    List[Any]=1      ,   __lowerCamelCase							:    int=0      ,   __lowerCamelCase							:    Any=2      ,   __lowerCamelCase							:    Tuple="absolute"      ,   __lowerCamelCase							:    Tuple=True      ,   __lowerCamelCase							:    str=None      ,   **__lowerCamelCase							:    Optional[Any]      ,   ):


             '''simple docstring'''



             super().__init__(pad_token_id=__lowerCamelCase      ,   bos_token_id=__lowerCamelCase      ,   eos_token_id=__lowerCamelCase      ,   **__lowerCamelCase )

             lowerCamelCase__	:       List[Any]					=						vocab_size
             lowerCamelCase__	:       str					=						hidden_size
             lowerCamelCase__	:       int					=						num_hidden_layers
             lowerCamelCase__	:       Optional[Any]					=						num_attention_heads
             lowerCamelCase__	:       Optional[Any]					=						hidden_act
             lowerCamelCase__	:       Any					=						intermediate_size
             lowerCamelCase__	:       Tuple					=						hidden_dropout_prob
             lowerCamelCase__	:       Any					=						attention_probs_dropout_prob
             lowerCamelCase__	:       Tuple					=						max_position_embeddings
             lowerCamelCase__	:       int					=						type_vocab_size
             lowerCamelCase__	:       Any					=						initializer_range
             lowerCamelCase__	:       Dict					=						layer_norm_eps
             lowerCamelCase__	:       int					=						position_embedding_type
             lowerCamelCase__	:       Any					=						use_cache
             lowerCamelCase__	:       int					=						classifier_dropout







class     _lowercase				(							lowercase__):


       """simple docstring"""




       @property
       def 		lowerCAmelCase  (     self							:    Union[str, Any] ):


             '''simple docstring'''



             if self.task == "multiple-choice":
                   lowerCamelCase__	:       int					=						{0: "batch", 1: "choice", 2: "sequence"}
             else:
                   lowerCamelCase__	:       Optional[Any]					=						{0: "batch", 1: "sequence"}
             return OrderedDict(
                 [
                     ("input_ids", dynamic_axis),
                     ("attention_mask", dynamic_axis),
                 ] )


