
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						      =				get_tests_dir("fixtures")
_A						      =				get_tests_dir("fixtures/dummy_feature_extractor_config.json")
_A						      =				get_tests_dir("fixtures/dummy-config.json")







class   lowerCamelCase	(						unittest.TestCase						):





     def 	UpperCAmelCase(self   :					Dict     )		->		List[Any]:
         snake_case				  =      0





     def 	UpperCAmelCase(self   :					Optional[Any]     )		->		Any:
         snake_case				  =      AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base-960h"     )
         self.assertIsInstance(_A       ,					_A     )





     def 	UpperCAmelCase(self   :					int     )		->		List[str]:
         snake_case				  =      AutoFeatureExtractor.from_pretrained(_A     )
         self.assertIsInstance(_A       ,					_A     )





     def 	UpperCAmelCase(self   :					Tuple     )		->		Dict:
         with tempfile.TemporaryDirectory() as tmpdirname:
             snake_case				  =      WavaVecaConfig()

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

             config_dict.pop("feature_extractor_type"     )
             snake_case				  =      WavaVecaFeatureExtractor(**_A     )

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

             snake_case				  =      AutoFeatureExtractor.from_pretrained(_A     )

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

         self.assertIsInstance(_A       ,					_A     )





     def 	UpperCAmelCase(self   :					Tuple     )		->		List[str]:
         snake_case				  =      AutoFeatureExtractor.from_pretrained(_A     )
         self.assertIsInstance(_A       ,					_A     )





     def 	UpperCAmelCase(self   :					int     )		->		Any:
         with self.assertRaisesRegex(
             _A       ,					"bert-base is not a local folder and is not a valid model identifier"     ):
             snake_case				  =      AutoFeatureExtractor.from_pretrained("bert-base"     )





     def 	UpperCAmelCase(self   :					Optional[int]     )		->		Tuple:
         with self.assertRaisesRegex(
             _A       ,					r"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"     ):
             snake_case				  =      AutoFeatureExtractor.from_pretrained(_A       ,					revision="aaaaaa"     )





     def 	UpperCAmelCase(self   :					List[str]     )		->		Dict:
         with self.assertRaisesRegex(
             _A       ,					"hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."       ,					):
             snake_case				  =      AutoFeatureExtractor.from_pretrained("hf-internal-testing/config-no-model"     )





     def 	UpperCAmelCase(self   :					List[Any]     )		->		List[Any]:
         # If remote code is not set, we will time out when asking whether to load the model.
         with self.assertRaises(_A     ):
             snake_case				  =      AutoFeatureExtractor.from_pretrained(
                 "hf-internal-testing/test_dynamic_feature_extractor"     )
         # If remote code is disabled, we can't load this config.
         with self.assertRaises(_A     ):
             snake_case				  =      AutoFeatureExtractor.from_pretrained(
                 "hf-internal-testing/test_dynamic_feature_extractor"       ,					trust_remote_code=_A     )

         snake_case				  =      AutoFeatureExtractor.from_pretrained(
             "hf-internal-testing/test_dynamic_feature_extractor"       ,					trust_remote_code=_A     )
         self.assertEqual(feature_extractor.__class__.__name__       ,					"NewFeatureExtractor"     )

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





     def 	UpperCAmelCase(self   :					Dict     )		->		Optional[int]:
         try:
             AutoConfig.register("custom"       ,					_A     )
             AutoFeatureExtractor.register(_A       ,					_A     )
             # Trying to register something existing in the Transformers library will raise an error
             with self.assertRaises(_A     ):
                 AutoFeatureExtractor.register(_A       ,					_A     )

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

         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 	UpperCAmelCase(self   :					str     )		->		Tuple:







         class   lowerCamelCase	(						A_						):
              UpperCAmelCase__       :							Tuple     =	True

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

             # If remote is enabled, we load from the Hub
             snake_case				  =      AutoFeatureExtractor.from_pretrained(
                 "hf-internal-testing/test_dynamic_feature_extractor"       ,					trust_remote_code=_A     )
             self.assertEqual(feature_extractor.__class__.__name__       ,					"NewFeatureExtractor"     )
             self.assertTrue(not hasattr(_A       ,					"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 ...configuration_utils import PretrainedConfig
from ...utils import logging


_A						      =				logging.get_logger(__name__)

_A						      =				{
    "google/canine-s": "https://huggingface.co/google/canine-s/resolve/main/config.json",
    # See all CANINE models at https://huggingface.co/models?filter=canine
}







class   lowerCamelCase	(						A_						):
     UpperCAmelCase__       :							Dict     =	"canine"





     def __init__(self   :					List[Any]       ,					_A   :					Union[str, Any]=7_6_8       ,					_A   :					Any=1_2       ,					_A   :					List[Any]=1_2       ,					_A   :					List[Any]=3_0_7_2       ,					_A   :					Dict="gelu"       ,					_A   :					Optional[Any]=0.1       ,					_A   :					Tuple=0.1       ,					_A   :					str=1_6_3_8_4       ,					_A   :					Union[str, Any]=1_6       ,					_A   :					Any=0.02       ,					_A   :					List[str]=1E-12       ,					_A   :					Union[str, Any]=0       ,					_A   :					Dict=0Xe0_00       ,					_A   :					List[Any]=0Xe0_01       ,					_A   :					int=4       ,					_A   :					str=4       ,					_A   :					Optional[int]=8       ,					_A   :					Optional[Any]=1_6_3_8_4       ,					_A   :					Optional[Any]=1_2_8       ,					**_A   :					Union[str, Any]       ,					)		->		Union[str, Any]:
         super().__init__(pad_token_id=_A       ,					bos_token_id=_A       ,					eos_token_id=_A       ,					**_A     )

         snake_case				  =      max_position_embeddings
         snake_case				  =      hidden_size
         snake_case				  =      num_hidden_layers
         snake_case				  =      num_attention_heads
         snake_case				  =      intermediate_size
         snake_case				  =      hidden_act
         snake_case				  =      hidden_dropout_prob
         snake_case				  =      attention_probs_dropout_prob
         snake_case				  =      initializer_range
         snake_case				  =      type_vocab_size
         snake_case				  =      layer_norm_eps

         # Character config:
         snake_case				  =      downsampling_rate
         snake_case				  =      upsampling_kernel_size
         snake_case				  =      num_hash_functions
         snake_case				  =      num_hash_buckets
         snake_case				  =      local_transformer_stride

