





import inspect
import logging
import os
import random
import shutil
import tempfile
import unittest

import pytest
import torch
from torch import nn
from torch.utils.data import DataLoader, TensorDataset

from accelerate import Accelerator
from accelerate.test_utils import execute_subprocess_async, require_cuda
from accelerate.utils import ProjectConfiguration, set_seed


SCREAMING_SNAKE_CASE_						   =       logging.getLogger(__name__)



def    __lowercase   (      _SCREAMING_SNAKE_CASE=2 ,						_SCREAMING_SNAKE_CASE=3 ,						_SCREAMING_SNAKE_CASE=16 ,						_SCREAMING_SNAKE_CASE = 10 ,						_SCREAMING_SNAKE_CASE = 2	) ->   str:


      '''simple docstring'''


      def get_dataset(_SCREAMING_SNAKE_CASE	):
            SCREAMING_SNAKE_CASE		=						torch.randn(batch_size * n_batches ,						1	)
            return TensorDataset(_SCREAMING_SNAKE_CASE ,						a * x + b + 0.1 * torch.randn(batch_size * n_batches ,						1	)	)

      SCREAMING_SNAKE_CASE		=						get_dataset(_SCREAMING_SNAKE_CASE	)
      SCREAMING_SNAKE_CASE		=						get_dataset(_SCREAMING_SNAKE_CASE	)
      SCREAMING_SNAKE_CASE		=						DataLoader(_SCREAMING_SNAKE_CASE ,						shuffle=_SCREAMING_SNAKE_CASE ,						batch_size=_SCREAMING_SNAKE_CASE ,						num_workers=4	)
      SCREAMING_SNAKE_CASE		=						DataLoader(_SCREAMING_SNAKE_CASE ,						shuffle=_SCREAMING_SNAKE_CASE ,						batch_size=_SCREAMING_SNAKE_CASE ,						num_workers=4	)
      return (train_dataloader, valid_dataloader)



def    __lowercase   (      _SCREAMING_SNAKE_CASE ,						_SCREAMING_SNAKE_CASE ,						_SCREAMING_SNAKE_CASE ,						_SCREAMING_SNAKE_CASE ,						_SCREAMING_SNAKE_CASE ,						_SCREAMING_SNAKE_CASE=None	) ->   Optional[int]:


      '''simple docstring'''


      SCREAMING_SNAKE_CASE		=						[]
      for epoch in range(_SCREAMING_SNAKE_CASE	):
            # Train quickly
            model.train()
            for batch in dataloader:
                  SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE		=						batch
                  SCREAMING_SNAKE_CASE		=						model(_SCREAMING_SNAKE_CASE	)
                  SCREAMING_SNAKE_CASE		=						torch.nn.functional.mse_loss(_SCREAMING_SNAKE_CASE ,						_SCREAMING_SNAKE_CASE	)
                  accelerator.backward(_SCREAMING_SNAKE_CASE	)
                  optimizer.step()
                  optimizer.zero_grad()
            rands.append(random.random()	)  # Introduce some randomness
            if scheduler is not None:
                  scheduler.step()
      return rands

class 		UpperCamelCase__ (      nn.Module   ):



     '''simple docstring'''

     def __init__(							self						:    int       )     ->		Optional[int]:




           '''simple docstring'''


           super().__init__()
           SCREAMING_SNAKE_CASE		=						nn.Parameter(torch.randn(1       )       )
           SCREAMING_SNAKE_CASE		=						nn.Parameter(torch.randn(1       )       )






     def 				SCREAMING_SNAKE_CASE__  (							self						:    int						,lowerCamelCase__						:    Optional[Any]       )     ->		Dict:




           '''simple docstring'''


           return x * self.a + self.b

class 		UpperCamelCase__ (      unittest.TestCase   ):



     '''simple docstring'''

     def 				SCREAMING_SNAKE_CASE__  (							self						:    List[str]       )     ->		Any:




           '''simple docstring'''


           with tempfile.TemporaryDirectory() as tmpdir:
                 set_seed(42       )
                 SCREAMING_SNAKE_CASE		=						DummyModel()
                 SCREAMING_SNAKE_CASE		=						torch.optim.Adam(params=model.parameters()						,lr=1e-3       )
                 SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE		=						dummy_dataloaders()
                 SCREAMING_SNAKE_CASE		=						ProjectConfiguration(total_limit=1						,project_dir=lowerCamelCase__						,automatic_checkpoint_naming=lowerCamelCase__       )
                 # Train baseline
                 SCREAMING_SNAKE_CASE		=						Accelerator(project_config=lowerCamelCase__       )
                 SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE		=						accelerator.prepare(
                     lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__       )
                 # Save initial
                 accelerator.save_state()

                 # Save second state
                 accelerator.save_state()
                 self.assertEqual(len(os.listdir(accelerator.project_dir       )       )						,1       )






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Union[str, Any]       )     ->		Optional[Any]:




           '''simple docstring'''


           with tempfile.TemporaryDirectory() as tmpdir:
                 set_seed(42       )
                 SCREAMING_SNAKE_CASE		=						DummyModel()
                 SCREAMING_SNAKE_CASE		=						torch.optim.Adam(params=model.parameters()						,lr=1e-3       )
                 SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE		=						dummy_dataloaders()
                 # Train baseline
                 SCREAMING_SNAKE_CASE		=						Accelerator()
                 SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE		=						accelerator.prepare(
                     lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__       )
                 # Save initial
                 SCREAMING_SNAKE_CASE		=						os.path.join(lowerCamelCase__						,"""initial"""       )
                 accelerator.save_state(lowerCamelCase__       )
                 ((SCREAMING_SNAKE_CASE),					(SCREAMING_SNAKE_CASE))		=						model.a.item(), model.b.item()
                 SCREAMING_SNAKE_CASE		=						optimizer.state_dict()
                 SCREAMING_SNAKE_CASE		=						train(3						,lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__       )
                 ((SCREAMING_SNAKE_CASE),					(SCREAMING_SNAKE_CASE))		=						model.a.item(), model.b.item()
                 SCREAMING_SNAKE_CASE		=						optimizer.state_dict()

                 # Train partially
                 set_seed(42       )
                 SCREAMING_SNAKE_CASE		=						DummyModel()
                 SCREAMING_SNAKE_CASE		=						torch.optim.Adam(params=model.parameters()						,lr=1e-3       )
                 SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE		=						dummy_dataloaders()
                 SCREAMING_SNAKE_CASE		=						Accelerator()
                 SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE		=						accelerator.prepare(
                     lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__       )
                 accelerator.load_state(lowerCamelCase__       )
                 ((SCREAMING_SNAKE_CASE),					(SCREAMING_SNAKE_CASE))		=						model.a.item(), model.b.item()
                 SCREAMING_SNAKE_CASE		=						optimizer.state_dict()
                 self.assertEqual(lowerCamelCase__						,lowerCamelCase__       )
                 self.assertEqual(lowerCamelCase__						,lowerCamelCase__       )
                 self.assertEqual(lowerCamelCase__						,lowerCamelCase__       )

                 SCREAMING_SNAKE_CASE		=						train(2						,lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__       )
                 # Save everything
                 SCREAMING_SNAKE_CASE		=						os.path.join(lowerCamelCase__						,"""checkpoint"""       )
                 accelerator.save_state(lowerCamelCase__       )

                 # Load everything back in and make sure all states work
                 accelerator.load_state(lowerCamelCase__       )
                 test_rands += train(1						,lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__       )
                 ((SCREAMING_SNAKE_CASE),					(SCREAMING_SNAKE_CASE))		=						model.a.item(), model.b.item()
                 SCREAMING_SNAKE_CASE		=						optimizer.state_dict()
                 self.assertEqual(lowerCamelCase__						,lowerCamelCase__       )
                 self.assertEqual(lowerCamelCase__						,lowerCamelCase__       )
                 self.assertEqual(lowerCamelCase__						,lowerCamelCase__       )
                 self.assertEqual(lowerCamelCase__						,lowerCamelCase__       )






     def 				SCREAMING_SNAKE_CASE__  (							self						:    List[Any]       )     ->		Union[str, Any]:




           '''simple docstring'''


           with tempfile.TemporaryDirectory() as tmpdir:
                 set_seed(42       )
                 SCREAMING_SNAKE_CASE		=						DummyModel()
                 SCREAMING_SNAKE_CASE		=						torch.optim.Adam(params=model.parameters()						,lr=1e-3       )
                 SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE		=						dummy_dataloaders()
                 SCREAMING_SNAKE_CASE		=						ProjectConfiguration(automatic_checkpoint_naming=lowerCamelCase__       )

                 # Train baseline
                 SCREAMING_SNAKE_CASE		=						Accelerator(project_dir=lowerCamelCase__						,project_config=lowerCamelCase__       )
                 SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE		=						accelerator.prepare(
                     lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__       )
                 # Save initial
                 accelerator.save_state()
                 ((SCREAMING_SNAKE_CASE),					(SCREAMING_SNAKE_CASE))		=						model.a.item(), model.b.item()
                 SCREAMING_SNAKE_CASE		=						optimizer.state_dict()
                 SCREAMING_SNAKE_CASE		=						train(3						,lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__       )
                 ((SCREAMING_SNAKE_CASE),					(SCREAMING_SNAKE_CASE))		=						model.a.item(), model.b.item()
                 SCREAMING_SNAKE_CASE		=						optimizer.state_dict()

                 # Train partially
                 set_seed(42       )
                 SCREAMING_SNAKE_CASE		=						DummyModel()
                 SCREAMING_SNAKE_CASE		=						torch.optim.Adam(params=model.parameters()						,lr=1e-3       )
                 SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE		=						dummy_dataloaders()
                 SCREAMING_SNAKE_CASE		=						ProjectConfiguration(iteration=1						,automatic_checkpoint_naming=lowerCamelCase__       )
                 SCREAMING_SNAKE_CASE		=						Accelerator(project_dir=lowerCamelCase__						,project_config=lowerCamelCase__       )
                 SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE		=						accelerator.prepare(
                     lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__       )
                 accelerator.load_state(os.path.join(lowerCamelCase__						,"""checkpoints"""						,"""checkpoint_0"""       )       )
                 ((SCREAMING_SNAKE_CASE),					(SCREAMING_SNAKE_CASE))		=						model.a.item(), model.b.item()
                 SCREAMING_SNAKE_CASE		=						optimizer.state_dict()
                 self.assertEqual(lowerCamelCase__						,lowerCamelCase__       )
                 self.assertEqual(lowerCamelCase__						,lowerCamelCase__       )
                 self.assertEqual(lowerCamelCase__						,lowerCamelCase__       )

                 SCREAMING_SNAKE_CASE		=						train(2						,lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__       )
                 # Save everything
                 accelerator.save_state()

                 # Load everything back in and make sure all states work
                 accelerator.load_state(os.path.join(lowerCamelCase__						,"""checkpoints"""						,"""checkpoint_1"""       )       )
                 test_rands += train(1						,lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__       )
                 ((SCREAMING_SNAKE_CASE),					(SCREAMING_SNAKE_CASE))		=						model.a.item(), model.b.item()
                 SCREAMING_SNAKE_CASE		=						optimizer.state_dict()
                 self.assertEqual(lowerCamelCase__						,lowerCamelCase__       )
                 self.assertEqual(lowerCamelCase__						,lowerCamelCase__       )
                 self.assertEqual(lowerCamelCase__						,lowerCamelCase__       )
                 self.assertEqual(lowerCamelCase__						,lowerCamelCase__       )






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Union[str, Any]       )     ->		List[str]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						torch.tensor([1, 2, 3]       )
           SCREAMING_SNAKE_CASE		=						torch.tensor([2, 3, 4]       )
           SCREAMING_SNAKE_CASE		=						DummyModel()
           SCREAMING_SNAKE_CASE		=						torch.optim.Adam(net.parameters()       )
           SCREAMING_SNAKE_CASE		=						Accelerator()
           with self.assertRaises(lowerCamelCase__       ) as ve:
                 accelerator.register_for_checkpointing(lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__       )
           SCREAMING_SNAKE_CASE		=						str(ve.exception       )
           self.assertTrue("""Item at index 0""" in message       )
           self.assertTrue("""Item at index 1""" in message       )
           self.assertFalse("""Item at index 2""" in message       )
           self.assertFalse("""Item at index 3""" in message       )






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Optional[int]       )     ->		Dict:




           '''simple docstring'''


           with tempfile.TemporaryDirectory() as tmpdir:
                 set_seed(42       )
                 SCREAMING_SNAKE_CASE		=						DummyModel()
                 SCREAMING_SNAKE_CASE		=						torch.optim.Adam(params=model.parameters()						,lr=1e-3       )
                 SCREAMING_SNAKE_CASE		=						torch.optim.lr_scheduler.StepLR(lowerCamelCase__						,step_size=1						,gamma=0.99       )
                 SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE		=						dummy_dataloaders()
                 SCREAMING_SNAKE_CASE		=						ProjectConfiguration(automatic_checkpoint_naming=lowerCamelCase__       )
                 # Train baseline
                 SCREAMING_SNAKE_CASE		=						Accelerator(project_dir=lowerCamelCase__						,project_config=lowerCamelCase__       )
                 SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE		=						accelerator.prepare(
                     lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__       )
                 # Save initial
                 accelerator.save_state()
                 SCREAMING_SNAKE_CASE		=						scheduler.state_dict()
                 train(3						,lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__						,lowerCamelCase__       )
                 self.assertNotEqual(lowerCamelCase__						,scheduler.state_dict()       )

                 # Load everything back in and make sure all states work
                 accelerator.load_state(os.path.join(lowerCamelCase__						,"""checkpoints"""						,"""checkpoint_0"""       )       )
                 self.assertEqual(lowerCamelCase__						,scheduler.state_dict()       )






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Dict       )     ->		Optional[Any]:




           '''simple docstring'''


           with tempfile.TemporaryDirectory() as tmpdir:
                 set_seed(42       )
                 SCREAMING_SNAKE_CASE		=						DummyModel()
                 SCREAMING_SNAKE_CASE		=						ProjectConfiguration(automatic_checkpoint_naming=lowerCamelCase__						,total_limit=2       )
                 # Train baseline
                 SCREAMING_SNAKE_CASE		=						Accelerator(project_dir=lowerCamelCase__						,project_config=lowerCamelCase__       )
                 SCREAMING_SNAKE_CASE		=						accelerator.prepare(lowerCamelCase__       )
                 # Save 3 states:
                 for _ in range(11       ):
                       accelerator.save_state()
                 self.assertTrue(not os.path.exists(os.path.join(lowerCamelCase__						,"""checkpoints"""						,"""checkpoint_0"""       )       )       )
                 self.assertTrue(os.path.exists(os.path.join(lowerCamelCase__						,"""checkpoints"""						,"""checkpoint_9"""       )       )       )
                 self.assertTrue(os.path.exists(os.path.join(lowerCamelCase__						,"""checkpoints"""						,"""checkpoint_10"""       )       )       )






     @require_cuda
     def 				SCREAMING_SNAKE_CASE__  (							self						:    Tuple       )     ->		Optional[int]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						["""torchrun""", F"""--nproc_per_node={torch.cuda.device_count()}""", inspect.getfile(self.__class__       )]
           execute_subprocess_async(lowerCamelCase__						,env=os.environ.copy()       )


if __name__ == "__main__":
      SCREAMING_SNAKE_CASE_						   =       """/tmp/accelerate/state_checkpointing"""
      SCREAMING_SNAKE_CASE_						   =       DummyModel()
      SCREAMING_SNAKE_CASE_						   =       torch.optim.Adam(params=model.parameters(), lr=1e-3)
      SCREAMING_SNAKE_CASE_						   =       torch.optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=0.99)
      SCREAMING_SNAKE_CASE_   ,						SCREAMING_SNAKE_CASE_						   =       dummy_dataloaders()
      SCREAMING_SNAKE_CASE_						   =       ProjectConfiguration(automatic_checkpoint_naming=True)
      # Train baseline
      SCREAMING_SNAKE_CASE_						   =       Accelerator(project_dir=savedir, project_config=project_config, mixed_precision="""no""")
      if accelerator.process_index == 0:
            if os.path.exists(savedir):
                  shutil.rmtree(savedir)
            os.makedirs(savedir)
      SCREAMING_SNAKE_CASE_   ,						SCREAMING_SNAKE_CASE_   ,						SCREAMING_SNAKE_CASE_   ,						SCREAMING_SNAKE_CASE_   ,						SCREAMING_SNAKE_CASE_						   =       accelerator.prepare(
          model, optimizer, train_dataloader, valid_dataloader, scheduler
      )
      SCREAMING_SNAKE_CASE_   ,						SCREAMING_SNAKE_CASE_						   =       accelerator.prepare(model, optimizer)
      train(3, model, train_dataloader, optimizer, accelerator, scheduler)
      # Check that the intial optimizer is loaded on the GPU
      for group in optimizer.param_groups:
            SCREAMING_SNAKE_CASE_						   =       group["""params"""][0].device
            break
      assert param_device.type == accelerator.device.type
      SCREAMING_SNAKE_CASE_						   =       model.cpu()
      accelerator.wait_for_everyone()
      accelerator.save_state()
      accelerator.wait_for_everyone()

      # Check CPU state
      accelerator.load_state(os.path.join(savedir, """checkpoints""", """checkpoint_0"""), map_location="""cpu""")
      for group in optimizer.param_groups:
            SCREAMING_SNAKE_CASE_						   =       group["""params"""][0].device
            break
      assert (
          param_device.type == torch.device("""cpu""").type
      ), F"Loaded optimizer states did not match, expected to be loaded on the CPU but got {param_device}"

      # Check device state
      model.to(accelerator.device)
      accelerator.load_state(os.path.join(savedir, """checkpoints""", """checkpoint_0"""), map_location="""on_device""")
      for group in optimizer.param_groups:
            SCREAMING_SNAKE_CASE_						   =       group["""params"""][0].device
            break
      assert (
          param_device.type == accelerator.device.type
      ), F"Loaded optimizer states did not match, expected to be loaded on {accelerator.device} but got {param_device}"

      # Check error
      with pytest.raises(TypeError, match="""Unsupported optimizer map location passed"""):
            accelerator.load_state(os.path.join(savedir, """checkpoints""", """checkpoint_0"""), map_location="""invalid""")
      accelerator.wait_for_everyone()
      if accelerator.process_index == 0:
            shutil.rmtree(savedir)
      accelerator.wait_for_everyone()









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


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

class 		UpperCamelCase__ (      unittest.TestCase   ):



     '''simple docstring'''

     def 				SCREAMING_SNAKE_CASE__  (							self						:    Optional[Any]       )     ->		Optional[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						0






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Any       )     ->		str:




           '''simple docstring'''


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






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Union[str, Any]       )     ->		List[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						AutoConfig.from_pretrained("""bert-base-uncased"""       )
           self.assertIsInstance(lowerCamelCase__						,lowerCamelCase__       )






     def 				SCREAMING_SNAKE_CASE__  (							self						:    int       )     ->		Optional[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						AutoConfig.from_pretrained(lowerCamelCase__       )
           self.assertIsInstance(lowerCamelCase__						,lowerCamelCase__       )






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Union[str, Any]       )     ->		List[Any]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						AutoConfig.from_pretrained(lowerCamelCase__       )
           self.assertIsInstance(lowerCamelCase__						,lowerCamelCase__       )






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Tuple       )     ->		Optional[int]:




           '''simple docstring'''


           SCREAMING_SNAKE_CASE		=						AutoConfig.for_model("""roberta"""       )
           self.assertIsInstance(lowerCamelCase__						,lowerCamelCase__       )






     def 				SCREAMING_SNAKE_CASE__  (							self						:    int       )     ->		int:




           '''simple docstring'''


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






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Dict       )     ->		str:




           '''simple docstring'''


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

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

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






     def 				SCREAMING_SNAKE_CASE__  (							self						:    str       )     ->		Dict:




           '''simple docstring'''


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






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Dict       )     ->		str:




           '''simple docstring'''


           with self.assertRaisesRegex(
               lowerCamelCase__						,R"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""       ):
                 SCREAMING_SNAKE_CASE		=						AutoConfig.from_pretrained(lowerCamelCase__						,revision="""aaaaaa"""       )






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Tuple       )     ->		List[Any]:




           '''simple docstring'''


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






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Optional[int]       )     ->		Union[str, Any]:




           '''simple docstring'''


           with self.assertRaises(lowerCamelCase__       ):
                 SCREAMING_SNAKE_CASE		=						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""       )
           # If remote code is disabled, we can't load this config.
           with self.assertRaises(lowerCamelCase__       ):
                 SCREAMING_SNAKE_CASE		=						AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""						,trust_remote_code=lowerCamelCase__       )

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

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






     def 				SCREAMING_SNAKE_CASE__  (							self						:    Dict       )     ->		Union[str, Any]:




           '''simple docstring'''


           class 		UpperCamelCase__ (      lowerCAmelCase_   ):



                '''simple docstring'''

                __snake_case    :	Union[str, Any]			=			"new-model"

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

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

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


