

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


SCREAMING_SNAKE_CASE_        =      get_tests_dir('fixtures')
SCREAMING_SNAKE_CASE_        =      get_tests_dir('fixtures/dummy_feature_extractor_config.json')
SCREAMING_SNAKE_CASE_        =      get_tests_dir('fixtures/dummy-config.json')

class 				a  (	unittest.TestCase				):


						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										_UpperCAmelCase       :		int 		=    0


						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										_UpperCAmelCase       :		List[Any] 		=    AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base-960h"	)
										self.assertIsInstance(__lowercase						,					__lowercase	)


						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										_UpperCAmelCase       :		List[str] 		=    AutoFeatureExtractor.from_pretrained(__lowercase	)
										self.assertIsInstance(__lowercase						,					__lowercase	)


						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										with tempfile.TemporaryDirectory() as tmpdirname:
														_UpperCAmelCase       :		List[str] 		=    WavaVecaConfig()

														# remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
														_UpperCAmelCase       :		int 		=    AutoFeatureExtractor.from_pretrained(__lowercase	).to_dict()

														config_dict.pop("feature_extractor_type"	)
														_UpperCAmelCase       :		Any 		=    WavaVecaFeatureExtractor(**__lowercase	)

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

														_UpperCAmelCase       :		Optional[Any] 		=    AutoFeatureExtractor.from_pretrained(__lowercase	)

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

										self.assertIsInstance(__lowercase						,					__lowercase	)


						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										_UpperCAmelCase       :		List[Any] 		=    AutoFeatureExtractor.from_pretrained(__lowercase	)
										self.assertIsInstance(__lowercase						,					__lowercase	)


						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


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


						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										with self.assertRaisesRegex(
										    __lowercase						,					R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"	):
														_UpperCAmelCase       :		str 		=    AutoFeatureExtractor.from_pretrained(__lowercase						,					revision="aaaaaa"	)


						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


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


						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


										with self.assertRaises(__lowercase	):
														_UpperCAmelCase       :		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(__lowercase	):
														_UpperCAmelCase       :		Optional[int] 		=    AutoFeatureExtractor.from_pretrained(
														    "hf-internal-testing/test_dynamic_feature_extractor"						,					trust_remote_code=__lowercase	)

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

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


						def        _UpperCAmelCase				(       self	):







										'''simple docstring'''


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

														# Now that the config is registered, it can be used as any other config with the auto-API
														_UpperCAmelCase       :		Tuple 		=    CustomFeatureExtractor.from_pretrained(__lowercase	)
														with tempfile.TemporaryDirectory() as tmp_dir:
																		feature_extractor.save_pretrained(__lowercase	)
																		_UpperCAmelCase       :		Optional[int] 		=    AutoFeatureExtractor.from_pretrained(__lowercase	)
																		self.assertIsInstance(__lowercase						,					__lowercase	)

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







										'''simple docstring'''


										class 				a  (	lowercase__				):
																_lowercase    						=			True

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

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


SCREAMING_SNAKE_CASE_        =      logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_        =      {
    'facebook/timesformer': 'https://huggingface.co/facebook/timesformer/resolve/main/config.json',
}

class 				a  (	UpperCAmelCase				):
						_lowercase    						=			"timesformer"


						def __init__(       self						,					A_=224						,					A_=16						,					A_=3						,					A_=8						,					A_=768						,					A_=12						,					A_=12						,					A_=3072						,					A_="gelu"						,					A_=0.0						,					A_=0.0						,					A_=0.02						,					A_=1e-6						,					A_=True						,					A_="divided_space_time"						,					A_=0						,					**A_						,					):







										'''simple docstring'''


										super().__init__(**A_	)

										_UpperCAmelCase       :		Union[str, Any] 		=    image_size
										_UpperCAmelCase       :		Tuple 		=    patch_size
										_UpperCAmelCase       :		Any 		=    num_channels
										_UpperCAmelCase       :		List[Any] 		=    num_frames

										_UpperCAmelCase       :		str 		=    hidden_size
										_UpperCAmelCase       :		Any 		=    num_hidden_layers
										_UpperCAmelCase       :		Dict 		=    num_attention_heads
										_UpperCAmelCase       :		Optional[int] 		=    intermediate_size
										_UpperCAmelCase       :		Union[str, Any] 		=    hidden_act
										_UpperCAmelCase       :		Optional[Any] 		=    hidden_dropout_prob
										_UpperCAmelCase       :		Tuple 		=    attention_probs_dropout_prob
										_UpperCAmelCase       :		Union[str, Any] 		=    initializer_range
										_UpperCAmelCase       :		Optional[Any] 		=    layer_norm_eps
										_UpperCAmelCase       :		str 		=    qkv_bias

										_UpperCAmelCase       :		Tuple 		=    attention_type
										_UpperCAmelCase       :		List[Any] 		=    drop_path_rate


