




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


a													=			get_tests_dir("fixtures/dummy-config.json")




class       _A				(		unittest.TestCase     ):






		def 				UpperCAmelCase (    self			):
								_UpperCAmelCase      =			0






		def 				UpperCAmelCase (    self			):
								self.assertIsNotNone(transformers.models.auto.__spec__			)
								self.assertIsNotNone(importlib.util.find_spec("""transformers.models.auto"""			)			)






		def 				UpperCAmelCase (    self			):
								_UpperCAmelCase      =			AutoConfig.from_pretrained("""bert-base-uncased"""			)
								self.assertIsInstance(_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			)






		def 				UpperCAmelCase (    self			):
								_UpperCAmelCase      =			AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE			)
								self.assertIsInstance(_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			)






		def 				UpperCAmelCase (    self			):
								_UpperCAmelCase      =			AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE			)
								self.assertIsInstance(_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			)






		def 				UpperCAmelCase (    self			):
								_UpperCAmelCase      =			AutoConfig.for_model("""roberta"""			)
								self.assertIsInstance(_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			)






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






		def 				UpperCAmelCase (    self			):
								try:
														AutoConfig.register("""custom"""						,		_SCREAMING_SNAKE_CASE			)
														# Wrong model type will raise an error
														with self.assertRaises(_SCREAMING_SNAKE_CASE			):
																				AutoConfig.register("""model"""						,		_SCREAMING_SNAKE_CASE			)
														# Trying to register something existing in the Transformers library will raise an error
														with self.assertRaises(_SCREAMING_SNAKE_CASE			):
																				AutoConfig.register("""bert"""						,		_SCREAMING_SNAKE_CASE			)

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

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






		def 				UpperCAmelCase (    self			):
								with self.assertRaisesRegex(
								    _SCREAMING_SNAKE_CASE						,		"""bert-base is not a local folder and is not a valid model identifier"""			):
														_UpperCAmelCase      =			AutoConfig.from_pretrained("""bert-base"""			)






		def 				UpperCAmelCase (    self			):
								with self.assertRaisesRegex(
								    _SCREAMING_SNAKE_CASE						,		r"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""			):
														_UpperCAmelCase      =			AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE						,		revision="""aaaaaa"""			)






		def 				UpperCAmelCase (    self			):
								with self.assertRaisesRegex(
								    _SCREAMING_SNAKE_CASE						,		"""hf-internal-testing/no-config-test-repo does not appear to have a file named config.json."""						,		):
														_UpperCAmelCase      =			AutoConfig.from_pretrained("""hf-internal-testing/no-config-test-repo"""			)






		def 				UpperCAmelCase (    self			):
								# If remote code is not set, we will time out when asking whether to load the model.
								with self.assertRaises(_SCREAMING_SNAKE_CASE			):
														_UpperCAmelCase      =			AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""			)
								# If remote code is disabled, we can't load this config.
								with self.assertRaises(_SCREAMING_SNAKE_CASE			):
														_UpperCAmelCase      =			AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""						,		trust_remote_code=_SCREAMING_SNAKE_CASE			)

								_UpperCAmelCase      =			AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""						,		trust_remote_code=_SCREAMING_SNAKE_CASE			)
								self.assertEqual(config.__class__.__name__						,		"""NewModelConfig"""			)

								# Test config can be reloaded.
								with tempfile.TemporaryDirectory() as tmp_dir:
														config.save_pretrained(_SCREAMING_SNAKE_CASE			)
														_UpperCAmelCase      =			AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE						,		trust_remote_code=_SCREAMING_SNAKE_CASE			)
								self.assertEqual(reloaded_config.__class__.__name__						,		"""NewModelConfig"""			)






		def 				UpperCAmelCase (    self			):




								class       _A				(		__lowercase     ):
										__a					    =       """new-model"""

								try:
														AutoConfig.register("""new-model"""						,		_SCREAMING_SNAKE_CASE			)
														# If remote code is not set, the default is to use local
														_UpperCAmelCase      =			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      =			AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""						,		trust_remote_code=_SCREAMING_SNAKE_CASE			)
														self.assertEqual(config.__class__.__name__						,		"""NewModelConfigLocal"""			)

														# If remote is enabled, we load from the Hub
														_UpperCAmelCase      =			AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""						,		trust_remote_code=_SCREAMING_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
import sys
import unittest


a													=			os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, "utils"))

import check_dummies  # noqa: E402
from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init  # noqa: E402


# Align TRANSFORMERS_PATH in check_dummies with the current path
a													=			os.path.join(git_repo_path, "src", "transformers")

a													=			"\n{0} = None\n"

a													=			"\nclass {0}(metaclass=DummyObject):\n    _backends = {1}\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, {1})\n"


a													=			"\ndef {0}(*args, **kwargs):\n    requires_backends({0}, {1})\n"




class       _A				(		unittest.TestCase     ):






		def 				UpperCAmelCase (    self			):
								_UpperCAmelCase      =			find_backend("""    _import_structure[\"models.albert\"].append(\"AlbertTokenizerFast\")"""			)
								self.assertIsNone(_SCREAMING_SNAKE_CASE			)

								_UpperCAmelCase      =			find_backend("""    if not is_tokenizers_available():"""			)
								self.assertEqual(_SCREAMING_SNAKE_CASE						,		"""tokenizers"""			)

								_UpperCAmelCase      =			find_backend("""    if not is_tensorflow_text_available():"""			)
								self.assertEqual(_SCREAMING_SNAKE_CASE						,		"""tensorflow_text"""			)

								_UpperCAmelCase      =			find_backend("""    if not (is_sentencepiece_available() and is_tokenizers_available()):"""			)
								self.assertEqual(_SCREAMING_SNAKE_CASE						,		"""sentencepiece_and_tokenizers"""			)

								_UpperCAmelCase      =			find_backend(
								    """    if not (is_sentencepiece_available() and is_tensorflow_text_available()):"""			)
								self.assertEqual(_SCREAMING_SNAKE_CASE						,		"""sentencepiece_and_tensorflow_text"""			)

								_UpperCAmelCase      =			find_backend(
								    """    if not (is_sentencepiece_available() and is_tokenizers_available() and is_vision_available()):"""			)
								self.assertEqual(_SCREAMING_SNAKE_CASE						,		"""sentencepiece_and_tokenizers_and_vision"""			)






		def 				UpperCAmelCase (    self			):
								_UpperCAmelCase      =			read_init()
								# We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects
								self.assertIn("""torch"""						,		_SCREAMING_SNAKE_CASE			)
								self.assertIn("""tensorflow_text"""						,		_SCREAMING_SNAKE_CASE			)
								self.assertIn("""sentencepiece_and_tokenizers"""						,		_SCREAMING_SNAKE_CASE			)

								# Likewise, we can't assert on the exact content of a key
								self.assertIn("""BertModel"""						,		objects["""torch"""]			)
								self.assertIn("""TFBertModel"""						,		objects["""tf"""]			)
								self.assertIn("""FlaxBertModel"""						,		objects["""flax"""]			)
								self.assertIn("""BertModel"""						,		objects["""torch"""]			)
								self.assertIn("""TFBertTokenizer"""						,		objects["""tensorflow_text"""]			)
								self.assertIn("""convert_slow_tokenizer"""						,		objects["""sentencepiece_and_tokenizers"""]			)






		def 				UpperCAmelCase (    self			):
								_UpperCAmelCase      =			create_dummy_object("""CONSTANT"""						,		"""'torch'"""			)
								self.assertEqual(_SCREAMING_SNAKE_CASE						,		"""\nCONSTANT = None\n"""			)

								_UpperCAmelCase      =			create_dummy_object("""function"""						,		"""'torch'"""			)
								self.assertEqual(
								    _SCREAMING_SNAKE_CASE						,		"""\ndef function(*args, **kwargs):\n    requires_backends(function, 'torch')\n"""			)

								_UpperCAmelCase      =			"""
class FakeClass(metaclass=DummyObject):
    _backends = 'torch'

    def __init__(self, *args, **kwargs):
        requires_backends(self, 'torch')
"""
								_UpperCAmelCase      =			create_dummy_object("""FakeClass"""						,		"""'torch'"""			)
								self.assertEqual(_SCREAMING_SNAKE_CASE						,		_SCREAMING_SNAKE_CASE			)






		def 				UpperCAmelCase (    self			):
								_UpperCAmelCase      =			"""# This file is autogenerated by the command `make fix-copies`, do not edit.
from ..utils import DummyObject, requires_backends


CONSTANT = None


def function(*args, **kwargs):
    requires_backends(function, [\"torch\"])


class FakeClass(metaclass=DummyObject):
    _backends = [\"torch\"]

    def __init__(self, *args, **kwargs):
        requires_backends(self, [\"torch\"])
"""
								_UpperCAmelCase      =			create_dummy_files({"""torch""": ["""CONSTANT""", """function""", """FakeClass"""]}			)
								self.assertEqual(dummy_files["""torch"""]						,		_SCREAMING_SNAKE_CASE			)