





'''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


__lowerCAmelCase							:	str			=get_tests_dir("fixtures/dummy-config.json")






class    UpperCAmelCase	(       unittest.TestCase				):
 def      UpperCAmelCase_							(				self      :Tuple							)->							Dict:
     A__				=			0
 def      UpperCAmelCase_							(				self      :Any							)->							List[str]:
     self.assertIsNotNone(transformers.models.auto.__spec__							)
     self.assertIsNotNone(importlib.util.find_spec("transformers.models.auto"							)							)
 def      UpperCAmelCase_							(				self      :int							)->							Tuple:
     A__				=			AutoConfig.from_pretrained("bert-base-uncased"							)
     self.assertIsInstance(lowercase_						,       lowercase_							)
 def      UpperCAmelCase_							(				self      :List[str]							)->							Dict:
     A__				=			AutoConfig.from_pretrained(lowercase_							)
     self.assertIsInstance(lowercase_						,       lowercase_							)
 def      UpperCAmelCase_							(				self      :Dict							)->							Tuple:
     A__				=			AutoConfig.from_pretrained(lowercase_							)
     self.assertIsInstance(lowercase_						,       lowercase_							)
 def      UpperCAmelCase_							(				self      :Tuple							)->							int:
     A__				=			AutoConfig.for_model("roberta"							)
     self.assertIsInstance(lowercase_						,       lowercase_							)
 def      UpperCAmelCase_							(				self      :Union[str, Any]							)->							List[Any]:
     with tempfile.TemporaryDirectory() as tmp_dir:
         # This model name contains bert and roberta, but roberta ends up being picked.
         A__				=			os.path.join(lowercase_						,       "fake-roberta"							)
         os.makedirs(lowercase_						,       exist_ok=lowercase_							)
         with open(os.path.join(lowercase_						,       "config.json"							)						,       "w"							) as f:
             f.write(json.dumps({}							)							)
         A__				=			AutoConfig.from_pretrained(lowercase_							)
         self.assertEqual(type(lowercase_							)						,       lowercase_							)
 def      UpperCAmelCase_							(				self      :Tuple							)->							int:
     try:
         AutoConfig.register("custom"						,       lowercase_							)
         # Wrong model type will raise an error
         with self.assertRaises(lowercase_							):
             AutoConfig.register("model"						,       lowercase_							)
         # Trying to register something existing in the Transformers library will raise an error
         with self.assertRaises(lowercase_							):
             AutoConfig.register("bert"						,       lowercase_							)

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

     finally:
         if "custom" in CONFIG_MAPPING._extra_content:
             del CONFIG_MAPPING._extra_content["custom"]
 def      UpperCAmelCase_							(				self      :int							)->							List[Any]:
     with self.assertRaisesRegex(
         lowercase_						,       "bert-base is not a local folder and is not a valid model identifier"							):
         A__				=			AutoConfig.from_pretrained("bert-base"							)
 def      UpperCAmelCase_							(				self      :int							)->							int:
     with self.assertRaisesRegex(
         lowercase_						,       R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"							):
         A__				=			AutoConfig.from_pretrained(lowercase_						,       revision="aaaaaa"							)
 def      UpperCAmelCase_							(				self      :Tuple							)->							Optional[int]:
     with self.assertRaisesRegex(
         lowercase_						,       "hf-internal-testing/no-config-test-repo does not appear to have a file named config.json."						,       ):
         A__				=			AutoConfig.from_pretrained("hf-internal-testing/no-config-test-repo"							)
 def      UpperCAmelCase_							(				self      :int							)->							Tuple:
     # If remote code is not set, we will time out when asking whether to load the model.
     with self.assertRaises(lowercase_							):
         A__				=			AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"							)
     # If remote code is disabled, we can't load this config.
     with self.assertRaises(lowercase_							):
         A__				=			AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"						,       trust_remote_code=lowercase_							)

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

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



 def      UpperCAmelCase_							(				self      :List[Any]							)->							int:






     class    UpperCAmelCase	(       UpperCamelCase__				):
      __lowercase   		=				"""new-model"""

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

         # If remote is enabled, we load from the Hub
         A__				=			AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"						,       trust_remote_code=lowercase_							)
         self.assertEqual(config.__class__.__name__						,       "NewModelConfig"							)

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










'''simple docstring'''

from ..utils import DummyObject, requires_backends






class    UpperCAmelCase	(       metaclass=UpperCamelCase__				):
 __lowercase   		=				["""note_seq"""]
 def __init__(				self      :Optional[Any]						,       *lowercase_      :List[Any]						,       **lowercase_      :List[str]							)->							int:
     requires_backends(self						,       ["note_seq"]							)
 @classmethod
 def      UpperCAmelCase_							(				cls      :str						,       *lowercase_      :Union[str, Any]						,       **lowercase_      :Any							)->							Optional[int]:
     requires_backends(cls						,       ["note_seq"]							)



 @classmethod
 def      UpperCAmelCase_							(				cls      :Dict						,       *lowercase_      :Tuple						,       **lowercase_      :List[Any]							)->							Optional[Any]:
     requires_backends(cls						,       ["note_seq"]							)



