
import importlib
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
import transformers.models.auto
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
from transformers.models.bert.configuration_bert import BertConfig
from transformers.models.roberta.configuration_roberta import RobertaConfig
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


lowerCamelCase_						: Optional[int]         =  get_tests_dir("""fixtures/dummy-config.json""")

class        _UpperCamelCase				(					unittest.TestCase  ):






			'''simple docstring'''

			def       lowerCAmelCase__	(       self :       List[str]      ):
							UpperCamelCase_:							Tuple	=						0

			def       lowerCAmelCase__	(       self :       Optional[int]      ):
							self.assertIsNotNone(transformers.models.auto.__spec__      )
							self.assertIsNotNone(importlib.util.find_spec("""transformers.models.auto"""      )      )

			def       lowerCAmelCase__	(       self :       List[Any]      ):
							UpperCamelCase_:							int	=						AutoConfig.from_pretrained("""bert-base-uncased"""      )
							self.assertIsInstance(snake_case_						,	snake_case_      )

			def       lowerCAmelCase__	(       self :       Optional[Any]      ):
							UpperCamelCase_:							int	=						AutoConfig.from_pretrained(snake_case_      )
							self.assertIsInstance(snake_case_						,	snake_case_      )

			def       lowerCAmelCase__	(       self :       Any      ):
							UpperCamelCase_:							Union[str, Any]	=						AutoConfig.from_pretrained(snake_case_      )
							self.assertIsInstance(snake_case_						,	snake_case_      )

			def       lowerCAmelCase__	(       self :       Dict      ):
							UpperCamelCase_:							Tuple	=						AutoConfig.for_model("""roberta"""      )
							self.assertIsInstance(snake_case_						,	snake_case_      )

			def       lowerCAmelCase__	(       self :       Any      ):
							with tempfile.TemporaryDirectory() as tmp_dir:
											# This model name contains bert and roberta, but roberta ends up being picked.
											UpperCamelCase_:							Any	=						os.path.join(snake_case_						,	"""fake-roberta"""      )
											os.makedirs(snake_case_						,	exist_ok=snake_case_      )
											with open(os.path.join(snake_case_						,	"""config.json"""      )						,	"""w"""      ) as f:
															f.write(json.dumps({}      )      )
											UpperCamelCase_:							Tuple	=						AutoConfig.from_pretrained(snake_case_      )
											self.assertEqual(type(snake_case_      )						,	snake_case_      )

			def       lowerCAmelCase__	(       self :       Optional[Any]      ):
							try:
											AutoConfig.register("""custom"""						,	snake_case_      )
											# Wrong model type will raise an error
											with self.assertRaises(snake_case_      ):
															AutoConfig.register("""model"""						,	snake_case_      )
											# Trying to register something existing in the Transformers library will raise an error
											with self.assertRaises(snake_case_      ):
															AutoConfig.register("""bert"""						,	snake_case_      )

											# Now that the config is registered, it can be used as any other config with the auto-API
											UpperCamelCase_:							Any	=						CustomConfig()
											with tempfile.TemporaryDirectory() as tmp_dir:
															config.save_pretrained(snake_case_      )
															UpperCamelCase_:							List[Any]	=						AutoConfig.from_pretrained(snake_case_      )
															self.assertIsInstance(snake_case_						,	snake_case_      )

							finally:
											if "custom" in CONFIG_MAPPING._extra_content:
															del CONFIG_MAPPING._extra_content["custom"]

			def       lowerCAmelCase__	(       self :       List[Any]      ):
							with self.assertRaisesRegex(
							    snake_case_						,	"""bert-base is not a local folder and is not a valid model identifier"""      ):
											UpperCamelCase_:							Any	=						AutoConfig.from_pretrained("""bert-base"""      )

			def       lowerCAmelCase__	(       self :       Optional[int]      ):
							with self.assertRaisesRegex(
							    snake_case_						,	R"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""      ):
											UpperCamelCase_:							str	=						AutoConfig.from_pretrained(snake_case_						,	revision="""aaaaaa"""      )

			def       lowerCAmelCase__	(       self :       Tuple      ):
							with self.assertRaisesRegex(
							    snake_case_						,	"""hf-internal-testing/no-config-test-repo does not appear to have a file named config.json."""						,	):
											UpperCamelCase_:							Any	=						AutoConfig.from_pretrained("""hf-internal-testing/no-config-test-repo"""      )

			def       lowerCAmelCase__	(       self :       Optional[Any]      ):
							# If remote code is not set, we will time out when asking whether to load the model.
							with self.assertRaises(snake_case_      ):
											UpperCamelCase_:							Optional[int]	=						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""      )
							# If remote code is disabled, we can't load this config.
							with self.assertRaises(snake_case_      ):
											UpperCamelCase_:							Union[str, Any]	=						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""						,	trust_remote_code=snake_case_      )

							UpperCamelCase_:							str	=						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""						,	trust_remote_code=snake_case_      )
							self.assertEqual(config.__class__.__name__						,	"""NewModelConfig"""      )

							# Test config can be reloaded.
							with tempfile.TemporaryDirectory() as tmp_dir:
											config.save_pretrained(snake_case_      )
											UpperCamelCase_:							Optional[Any]	=						AutoConfig.from_pretrained(snake_case_						,	trust_remote_code=snake_case_      )
							self.assertEqual(reloaded_config.__class__.__name__						,	"""NewModelConfig"""      )




			def       lowerCAmelCase__	(       self :       str      ):

							class        _UpperCamelCase				(					_A  ):






										'''simple docstring'''

										__UpperCamelCase      :						Tuple					      = """new-model"""

							try:
											AutoConfig.register("""new-model"""						,	snake_case_      )
											# If remote code is not set, the default is to use local
											UpperCamelCase_:							Tuple	=						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""      )
											self.assertEqual(config.__class__.__name__						,	"""NewModelConfigLocal"""      )

											# If remote code is disabled, we load the local one.
											UpperCamelCase_:							Optional[int]	=						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""						,	trust_remote_code=snake_case_      )
											self.assertEqual(config.__class__.__name__						,	"""NewModelConfigLocal"""      )

											# If remote is enabled, we load from the Hub
											UpperCamelCase_:							List[Any]	=						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""						,	trust_remote_code=snake_case_      )
											self.assertEqual(config.__class__.__name__						,	"""NewModelConfig"""      )

							finally:
											if "new-model" in CONFIG_MAPPING._extra_content:
															del CONFIG_MAPPING._extra_content["new-model"]








import os

from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE, hf_cache_home


lowerCamelCase_						: Optional[int]         =  HUGGINGFACE_HUB_CACHE


lowerCamelCase_						: List[str]         =  """config.json"""
lowerCamelCase_						: Any         =  """diffusion_pytorch_model.bin"""
lowerCamelCase_						: Union[str, Any]         =  """diffusion_flax_model.msgpack"""
lowerCamelCase_						: Dict         =  """model.onnx"""
lowerCamelCase_						: List[Any]         =  """diffusion_pytorch_model.safetensors"""
lowerCamelCase_						: Optional[Any]         =  """weights.pb"""
lowerCamelCase_						: Optional[Any]         =  """https://huggingface.co"""
lowerCamelCase_						: Union[str, Any]         =  default_cache_path
lowerCamelCase_						: Tuple         =  """diffusers_modules"""
lowerCamelCase_						: Optional[Any]         =  os.getenv("""HF_MODULES_CACHE""", os.path.join(hf_cache_home, """modules"""))
lowerCamelCase_						: str         =  ["""fp16""", """non-ema"""]
lowerCamelCase_						: List[Any]         =  """.self_attn"""






