
'''simple docstring'''

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


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




class    lowerCAmelCase       (     unittest.TestCase  ):


 def    _A	(    self  : str    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Optional[Any]     		=	0


 def    _A	(    self  : int    ):
  '''simple docstring'''

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


 def    _A	(    self  : Optional[int]    ):
  '''simple docstring'''

  lowerCAmelCase__     :   str     		=	AutoConfig.from_pretrained("bert-base-uncased"    )
  self.assertIsInstance(a__			,							a__    )


 def    _A	(    self  : Dict    ):
  '''simple docstring'''

  lowerCAmelCase__     :   int     		=	AutoConfig.from_pretrained(a__    )
  self.assertIsInstance(a__			,							a__    )


 def    _A	(    self  : Optional[Any]    ):
  '''simple docstring'''

  lowerCAmelCase__     :   str     		=	AutoConfig.from_pretrained(a__    )
  self.assertIsInstance(a__			,							a__    )


 def    _A	(    self  : int    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Any     		=	AutoConfig.for_model("roberta"    )
  self.assertIsInstance(a__			,							a__    )


 def    _A	(    self  : str    ):
  '''simple docstring'''

  with tempfile.TemporaryDirectory() as tmp_dir:
   # This model name contains bert and roberta, but roberta ends up being picked.
   lowerCAmelCase__     :   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({}    )    )
   lowerCAmelCase__     :   int     		=	AutoConfig.from_pretrained(a__    )
   self.assertEqual(type(a__    )			,							a__    )


 def    _A	(    self  : Any    ):
  '''simple docstring'''

  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
   lowerCAmelCase__     :   List[Any]     		=	CustomConfig()
   with tempfile.TemporaryDirectory() as tmp_dir:
    config.save_pretrained(a__    )
    lowerCAmelCase__     :   str     		=	AutoConfig.from_pretrained(a__    )
    self.assertIsInstance(a__			,							a__    )

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


 def    _A	(    self  : Optional[Any]    ):
  '''simple docstring'''

  with self.assertRaisesRegex(
      a__			,							"bert-base is not a local folder and is not a valid model identifier"    ):
   lowerCAmelCase__     :   Optional[int]     		=	AutoConfig.from_pretrained("bert-base"    )


 def    _A	(    self  : List[str]    ):
  '''simple docstring'''

  with self.assertRaisesRegex(
      a__			,							r"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"    ):
   lowerCAmelCase__     :   List[Any]     		=	AutoConfig.from_pretrained(a__			,							revision="aaaaaa"    )


 def    _A	(    self  : List[Any]    ):
  '''simple docstring'''

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


 def    _A	(    self  : str    ):
  '''simple docstring'''

  with self.assertRaises(a__    ):
   lowerCAmelCase__     :   Optional[Any]     		=	AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"    )
  # If remote code is disabled, we can't load this config.
  with self.assertRaises(a__    ):
   lowerCAmelCase__     :   Optional[int]     		=	AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"			,							trust_remote_code=a__    )

  lowerCAmelCase__     :   Tuple     		=	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__    )
   lowerCAmelCase__     :   Optional[int]     		=	AutoConfig.from_pretrained(a__			,							trust_remote_code=a__    )
  self.assertEqual(reloaded_config.__class__.__name__			,							"NewModelConfig"    )


 def    _A	(    self  : List[Any]    ):
  '''simple docstring'''

  class    lowerCAmelCase       (     UpperCamelCase_  ):
   A_				:				Tuple	=			"""new-model"""

  try:
   AutoConfig.register("new-model"			,							a__    )
   # If remote code is not set, the default is to use local
   lowerCAmelCase__     :   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.
   lowerCAmelCase__     :   Optional[int]     		=	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
   lowerCAmelCase__     :   Dict     		=	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"]







'''simple docstring'''

def        UpperCAmelCase_ (  lowerCamelCase_			):







 """simple docstring"""


 return number & 1 == 0


if __name__ == "__main__":
     import doctest

     doctest.testmod()





