

'''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        a__  (							unittest.TestCase  ):


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				List[Any]      					=       0


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





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


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				int      					=       AutoConfig.from_pretrained("bert-base-uncased"  )
      self.assertIsInstance(_UpperCamelCase      ,    _UpperCamelCase  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Optional[Any]      					=       AutoConfig.from_pretrained(_UpperCamelCase  )
      self.assertIsInstance(_UpperCamelCase      ,    _UpperCamelCase  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Union[str, Any]      					=       AutoConfig.from_pretrained(_UpperCamelCase  )
      self.assertIsInstance(_UpperCamelCase      ,    _UpperCamelCase  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				str      					=       AutoConfig.for_model("roberta"  )
      self.assertIsInstance(_UpperCamelCase      ,    _UpperCamelCase  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





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


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





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

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

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


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      with self.assertRaisesRegex(
          _UpperCamelCase      ,    "bert-base is not a local folder and is not a valid model identifier"  ):
         _lowercase      :				Dict      					=       AutoConfig.from_pretrained("bert-base"  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





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


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      with self.assertRaisesRegex(
          _UpperCamelCase      ,    "hf-internal-testing/no-config-test-repo does not appear to have a file named config.json."      ,    ):
         _lowercase      :				List[str]      					=       AutoConfig.from_pretrained("hf-internal-testing/no-config-test-repo"  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





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

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

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






   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      class        a__  (							lowerCamelCase_  ):
         _SCREAMING_SNAKE_CASE   :					Dict     	=							'new-model'

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

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

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









'''simple docstring'''
import os
import sys
import unittest


_snake_case         =  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
_snake_case         =  os.path.join(git_repo_path, 'src', 'transformers')

_snake_case         =  '\n{0} = None\n'

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


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


class        a__  (							unittest.TestCase  ):


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Optional[int]      					=       find_backend("    _import_structure[\"models.albert\"].append(\"AlbertTokenizerFast\")"  )
      self.assertIsNone(_UpperCamelCase  )

      _lowercase      :				Dict      					=       find_backend("    if not is_tokenizers_available():"  )
      self.assertEqual(_UpperCamelCase      ,    "tokenizers"  )

      _lowercase      :				str      					=       find_backend("    if not is_tensorflow_text_available():"  )
      self.assertEqual(_UpperCamelCase      ,    "tensorflow_text"  )

      _lowercase      :				Optional[Any]      					=       find_backend("    if not (is_sentencepiece_available() and is_tokenizers_available()):"  )
      self.assertEqual(_UpperCamelCase      ,    "sentencepiece_and_tokenizers"  )

      _lowercase      :				List[str]      					=       find_backend(
          "    if not (is_sentencepiece_available() and is_tensorflow_text_available()):"  )
      self.assertEqual(_UpperCamelCase      ,    "sentencepiece_and_tensorflow_text"  )

      _lowercase      :				Optional[Any]      					=       find_backend(
          "    if not (is_sentencepiece_available() and is_tokenizers_available() and is_vision_available()):"  )
      self.assertEqual(_UpperCamelCase      ,    "sentencepiece_and_tokenizers_and_vision"  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				List[Any]      					=       read_init()
      # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects
      self.assertIn("torch"      ,    _UpperCamelCase  )
      self.assertIn("tensorflow_text"      ,    _UpperCamelCase  )
      self.assertIn("sentencepiece_and_tokenizers"      ,    _UpperCamelCase  )

      # 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 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				int      					=       create_dummy_object("CONSTANT"      ,    "'torch'"  )
      self.assertEqual(_UpperCamelCase      ,    "\nCONSTANT = None\n"  )

      _lowercase      :				Optional[Any]      					=       create_dummy_object("function"      ,    "'torch'"  )
      self.assertEqual(
          _UpperCamelCase      ,    "\ndef function(*args, **kwargs):\n    requires_backends(function, 'torch')\n"  )

      _lowercase      :				Union[str, Any]      					=       "\nclass FakeClass(metaclass=DummyObject):\n    _backends = 'torch'\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, 'torch')\n"
      _lowercase      :				List[str]      					=       create_dummy_object("FakeClass"      ,    "'torch'"  )
      self.assertEqual(_UpperCamelCase      ,    _UpperCamelCase  )






   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Optional[int]      					=       "# This file is autogenerated by the command `make fix-copies`, do not edit.\nfrom ..utils import DummyObject, requires_backends\n\n\nCONSTANT = None\n\n\ndef function(*args, **kwargs):\n    requires_backends(function, [\"torch\"])\n\n\nclass FakeClass(metaclass=DummyObject):\n    _backends = [\"torch\"]\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, [\"torch\"])\n"
      _lowercase      :				str      					=       create_dummy_files({"torch": ["CONSTANT", "function", "FakeClass"]}  )
      self.assertEqual(dummy_files["torch"]      ,    _UpperCamelCase  )






