




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


_UpperCAmelCase	:						str			=     get_tests_dir("fixtures")
_UpperCAmelCase	:						List[str]			=     get_tests_dir("fixtures/dummy_feature_extractor_config.json")
_UpperCAmelCase	:						Any			=     get_tests_dir("fixtures/dummy-config.json")




class    __lowerCAmelCase       (							unittest.TestCase):






       def 					SCREAMING_SNAKE_CASE    (   self:					Dict				):
         lowercase    :int      	=						0






       def 					SCREAMING_SNAKE_CASE    (   self:					Union[str, Any]				):
         lowercase    :Optional[Any]      	=						AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base-960h"				)
         self.assertIsInstance(_lowerCAmelCase       ,					_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Dict				):
         lowercase    :List[str]      	=						AutoFeatureExtractor.from_pretrained(_lowerCAmelCase				)
         self.assertIsInstance(_lowerCAmelCase       ,					_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					int				):
         with tempfile.TemporaryDirectory() as tmpdirname:
           lowercase    :Optional[Any]      	=						WavaVecaConfig()

           # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
           lowercase    :List[Any]      	=						AutoFeatureExtractor.from_pretrained(_lowerCAmelCase				).to_dict()

           config_dict.pop("feature_extractor_type"				)
           lowercase    :Optional[int]      	=						WavaVecaFeatureExtractor(**_lowerCAmelCase				)

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

           lowercase    :List[Any]      	=						AutoFeatureExtractor.from_pretrained(_lowerCAmelCase				)

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

         self.assertIsInstance(_lowerCAmelCase       ,					_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Union[str, Any]				):
         lowercase    :List[str]      	=						AutoFeatureExtractor.from_pretrained(_lowerCAmelCase				)
         self.assertIsInstance(_lowerCAmelCase       ,					_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Union[str, Any]				):
         with self.assertRaisesRegex(
             _lowerCAmelCase       ,					"bert-base is not a local folder and is not a valid model identifier"				):
           lowercase    :List[str]      	=						AutoFeatureExtractor.from_pretrained("bert-base"				)






       def 					SCREAMING_SNAKE_CASE    (   self:					str				):
         with self.assertRaisesRegex(
             _lowerCAmelCase       ,					r"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"				):
           lowercase    :Any      	=						AutoFeatureExtractor.from_pretrained(_lowerCAmelCase       ,					revision="aaaaaa"				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Tuple				):
         with self.assertRaisesRegex(
             _lowerCAmelCase       ,					"hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."       ,					):
           lowercase    :Optional[int]      	=						AutoFeatureExtractor.from_pretrained("hf-internal-testing/config-no-model"				)






       def 					SCREAMING_SNAKE_CASE    (   self:					List[str]				):
         # If remote code is not set, we will time out when asking whether to load the model.
         with self.assertRaises(_lowerCAmelCase				):
           lowercase    :Optional[Any]      	=						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				):
           lowercase    :Tuple      	=						AutoFeatureExtractor.from_pretrained(
               "hf-internal-testing/test_dynamic_feature_extractor"       ,					trust_remote_code=_lowerCAmelCase				)

         lowercase    :Optional[Any]      	=						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				)
           lowercase    :Any      	=						AutoFeatureExtractor.from_pretrained(_lowerCAmelCase       ,					trust_remote_code=_lowerCAmelCase				)
         self.assertEqual(reloaded_feature_extractor.__class__.__name__       ,					"NewFeatureExtractor"				)






       def 					SCREAMING_SNAKE_CASE    (   self:					List[str]				):
         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
           lowercase    :int      	=						CustomFeatureExtractor.from_pretrained(_lowerCAmelCase				)
           with tempfile.TemporaryDirectory() as tmp_dir:
             feature_extractor.save_pretrained(_lowerCAmelCase				)
             lowercase    :List[Any]      	=						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 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]				):




         class    __lowerCAmelCase       (							lowerCAmelCase):
                _a 					=						True

         try:
           AutoConfig.register("custom"       ,					_lowerCAmelCase				)
           AutoFeatureExtractor.register(_lowerCAmelCase       ,					_lowerCAmelCase				)
           # If remote code is not set, the default is to use local
           lowercase    :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.
           lowercase    :Tuple      	=						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
           lowercase    :Dict      	=						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]









_UpperCAmelCase	:						Union[str, Any]			=     "0.21.0"

from .accelerator import Accelerator
from .big_modeling import (
    cpu_offload,
    cpu_offload_with_hook,
    disk_offload,
    dispatch_model,
    init_empty_weights,
    init_on_device,
    load_checkpoint_and_dispatch,
)
from .data_loader import skip_first_batches
from .launchers import debug_launcher, notebook_launcher
from .state import PartialState
from .utils import (
    DeepSpeedPlugin,
    DistributedDataParallelKwargs,
    DistributedType,
    FullyShardedDataParallelPlugin,
    GradScalerKwargs,
    InitProcessGroupKwargs,
    find_executable_batch_size,
    infer_auto_device_map,
    is_rich_available,
    load_checkpoint_in_model,
    synchronize_rng_states,
)


if is_rich_available():
     from .utils import rich



