

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



			def 	__magic_name__   (							self							:					Any						):
					UpperCAmelCase	:							Optional[int]						=			0



			def 	__magic_name__   (							self							:					List[Any]						):
					self.assertIsNotNone(transformers.models.auto.__spec__						)
					self.assertIsNotNone(importlib.util.find_spec('''transformers.models.auto'''						)						)



			def 	__magic_name__   (							self							:					Tuple						):
					UpperCAmelCase	:							Dict						=			AutoConfig.from_pretrained('''bert-base-uncased'''						)
					self.assertIsInstance(__A,		__A						)



			def 	__magic_name__   (							self							:					Any						):
					UpperCAmelCase	:							List[str]						=			AutoConfig.from_pretrained(__A						)
					self.assertIsInstance(__A,		__A						)



			def 	__magic_name__   (							self							:					Union[str, Any]						):
					UpperCAmelCase	:							Dict						=			AutoConfig.from_pretrained(__A						)
					self.assertIsInstance(__A,		__A						)



			def 	__magic_name__   (							self							:					str						):
					UpperCAmelCase	:							Any						=			AutoConfig.for_model('''roberta'''						)
					self.assertIsInstance(__A,		__A						)



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



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

							# 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(__A						)
									UpperCAmelCase	:							Optional[Any]						=			AutoConfig.from_pretrained(__A						)
									self.assertIsInstance(__A,		__A						)

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



			def 	__magic_name__   (							self							:					Optional[Any]						):
					with self.assertRaisesRegex(
					    __A,		'''bert-base is not a local folder and is not a valid model identifier'''						):
							UpperCAmelCase	:							Any						=			AutoConfig.from_pretrained('''bert-base'''						)



			def 	__magic_name__   (							self							:					List[Any]						):
					with self.assertRaisesRegex(
					    __A,		R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''						):
							UpperCAmelCase	:							Any						=			AutoConfig.from_pretrained(__A,		revision='''aaaaaa'''						)



			def 	__magic_name__   (							self							:					int						):
					with self.assertRaisesRegex(
					    __A,		'''hf-internal-testing/no-config-test-repo does not appear to have a file named config.json.''',		):
							UpperCAmelCase	:							List[str]						=			AutoConfig.from_pretrained('''hf-internal-testing/no-config-test-repo'''						)



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

					UpperCAmelCase	:							Union[str, Any]						=			AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model''',		trust_remote_code=__A						)
					self.assertEqual(config.__class__.__name__,		'''NewModelConfig'''						)

					# Test config can be reloaded.
					with tempfile.TemporaryDirectory() as tmp_dir:
							config.save_pretrained(__A						)
							UpperCAmelCase	:							Any						=			AutoConfig.from_pretrained(__A,		trust_remote_code=__A						)
					self.assertEqual(reloaded_config.__class__.__name__,		'''NewModelConfig'''						)





			def 	__magic_name__   (							self							:					Optional[Any]						):





					class  __UpperCAmelCase					(	lowerCamelCase__						):
								UpperCamelCase       					=					"""new-model"""

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

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

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








def       a__					(		UpperCAmelCase							:      int      )  ->						bool:
  if not isinstance(UpperCAmelCase    ,			UpperCAmelCase      ):
    UpperCAmelCase	:							List[str]						=			f'''Input value of [number={number}] must be an integer'''
    raise TypeError(UpperCAmelCase      )
  if number < 0:
    return False
  UpperCAmelCase	:							List[str]						=			number * number
  while number > 0:
    if number % 10 != number_square % 10:
      return False
    number //= 10
    number_square //= 10
  return True


if __name__ == "__main__":
     import doctest

     doctest.testmod()





