

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


_a       =							get_tests_dir('fixtures/dummy-config.json')




class 					_lowerCAmelCase  (	unittest.TestCase   ):







 """simple docstring"""







 def 					_lowercase (			self							:			List[str]	):
    __lowercase								=						0


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


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


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


 def 					_lowercase (			self							:			List[str]	):
    __lowercase								=						AutoConfig.from_pretrained(UpperCAmelCase__	)
    self.assertIsInstance(UpperCAmelCase__,					UpperCAmelCase__	)


 def 					_lowercase (			self							:			Union[str, Any]	):
    __lowercase								=						AutoConfig.for_model("roberta"	)
    self.assertIsInstance(UpperCAmelCase__,					UpperCAmelCase__	)


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


 def 					_lowercase (			self							:			Union[str, Any]	):
    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								=						CustomConfig()
       with tempfile.TemporaryDirectory() as tmp_dir:
          config.save_pretrained(UpperCAmelCase__	)
          __lowercase								=						AutoConfig.from_pretrained(UpperCAmelCase__	)
          self.assertIsInstance(UpperCAmelCase__,					UpperCAmelCase__	)

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


 def 					_lowercase (			self							:			Union[str, Any]	):
    with self.assertRaisesRegex(
        UpperCAmelCase__,					"bert-base is not a local folder and is not a valid model identifier"	):
       __lowercase								=						AutoConfig.from_pretrained("bert-base"	)


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


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


 def 					_lowercase (			self							:			Tuple	):
    # If remote code is not set, we will time out when asking whether to load the model.
    with self.assertRaises(UpperCAmelCase__	):
       __lowercase								=						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								=						AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model",					trust_remote_code=UpperCAmelCase__	)

    __lowercase								=						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								=						AutoConfig.from_pretrained(UpperCAmelCase__,					trust_remote_code=UpperCAmelCase__	)
    self.assertEqual(reloaded_config.__class__.__name__,					"NewModelConfig"	)



 def 					_lowercase (			self							:			Optional[Any]	):




    class 					_lowerCAmelCase  (	lowercase   ):







     """simple docstring"""







     __UpperCAmelCase	:					Optional[Any]   		=	"new-model"

    try:
       AutoConfig.register("new-model",					UpperCAmelCase__	)
       # If remote code is not set, the default is to use local
       __lowercase								=						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								=						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								=						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"""





def   _A  (  UpperCamelCase_     :       list[int])	->						float:
   '''simple docstring'''


   if not nums:  # Makes sure that the list is not empty
      raise ValueError("List is empty")

   __lowercase								=						sum(UpperCamelCase_) / len(UpperCamelCase_)  # Calculate the average
   return sum(abs(x - average) for x in nums) / len(UpperCamelCase_)


if __name__ == "__main__":
   import doctest

   doctest.testmod()




