'''simple docstring'''






import ast
import os
import re
import shutil
import tempfile
import unittest
from unittest import mock

import torch

from accelerate.test_utils.examples import compare_against_test
from accelerate.test_utils.testing import TempDirTestCase, require_trackers, run_command, slow
from accelerate.utils import write_basic_config


# DataLoaders built from `test_samples/MRPC` for quick testing
# Should mock `{script_name}.get_dataloaders` via:
# @mock.patch("{script_name}.get_dataloaders", mocked_dataloaders)

_UpperCAmelCase    :     Union[str, Any]         =       [
    '''cross_validation.py''',
    '''gradient_accumulation.py''',
    '''local_sgd.py''',
    '''multi_process_metrics.py''',
    '''memory.py''',
    '''automatic_gradient_accumulation.py''',
    '''fsdp_with_peak_mem_tracking.py''',
    '''deepspeed_with_config_support.py''',
    '''megatron_lm_gpt_pretraining.py''',
]







class lowercase_       (		unittest.TestCase			):







   """simple docstring"""

   def 	__UpperCAmelCase   (      self      :			Tuple, UpperCamelCase__      :			str, UpperCamelCase__      :			bool, UpperCamelCase__      :			str = None, UpperCamelCase__      :			list = None     )  ->			Any:
        _A    		=  None
        _A    		=  os.path.abspath(os.path.join('examples', 'by_feature'     )     )
        _A    		=  os.path.abspath('examples'     )
        for item in os.listdir(UpperCamelCase__     ):
             if item not in EXCLUDE_EXAMPLES:
                  _A    		=  os.path.join(UpperCamelCase__, UpperCamelCase__     )
                  if os.path.isfile(UpperCamelCase__     ) and ".py" in item_path:
                       with self.subTest(
                           tested_script=UpperCamelCase__, feature_script=UpperCamelCase__, tested_section='main()' if parser_only else 'training_function()', ):
                            _A    		=  compare_against_test(
                                os.path.join(UpperCamelCase__, UpperCamelCase__     ), UpperCamelCase__, UpperCamelCase__, UpperCamelCase__     )
                            _A    		=  '\n'.join(UpperCamelCase__     )
                            if special_strings is not None:
                                 for string in special_strings:
                                      _A    		=  diff.replace(UpperCamelCase__, ''     )
                            self.assertEqual(UpperCamelCase__, ''     )
   def 	__UpperCAmelCase   (      self      :			str     )  ->			Optional[int]:
        self.one_complete_example('complete_nlp_example.py', UpperCamelCase__     )
        self.one_complete_example('complete_nlp_example.py', UpperCamelCase__     )

   def 	__UpperCAmelCase   (      self      :			Any     )  ->			List[str]:
        _A    		=  os.path.abspath(os.path.join('examples', 'cv_example.py'     )     )
        _A    		=  [
            ' ' * 16 + '{\n\n',
            ' ' * 20 + '"accuracy": eval_metric["accuracy"],\n\n',
            ' ' * 20 + '"f1": eval_metric["f1"],\n\n',
            ' ' * 20 + '"train_loss": total_loss.item() / len(train_dataloader),\n\n',
            ' ' * 20 + '"epoch": epoch,\n\n',
            ' ' * 16 + '},\n\n',
            ' ' * 16 + 'step=epoch,\n',
            ' ' * 12,
            ' ' * 8 + 'for step, batch in enumerate(active_dataloader):\n',
        ]
        self.one_complete_example('complete_cv_example.py', UpperCamelCase__, UpperCamelCase__, UpperCamelCase__     )
        self.one_complete_example('complete_cv_example.py', UpperCamelCase__, UpperCamelCase__, UpperCamelCase__     )







@mock.patch.dict(os.environ    ,    {"TESTING_MOCKED_DATALOADERS": "1"}			)
class lowercase_       (		_UpperCamelCase			):







   """simple docstring"""

   __lowerCAmelCase         =   False
   @classmethod
   def 	__UpperCAmelCase   (      cls      :			Optional[int]     )  ->			Dict:
        super().setUpClass()
        _A    		=  tempfile.mkdtemp()
        _A    		=  os.path.join(cls._tmpdir, 'default_config.yml'     )

        write_basic_config(save_location=cls.configPath     )
        _A    		=  ['accelerate', 'launch', '--config_file', cls.configPath]
   @classmethod
   def 	__UpperCAmelCase   (      cls      :			List[str]     )  ->			Tuple:
        super().tearDownClass()
        shutil.rmtree(cls._tmpdir     )
   def 	__UpperCAmelCase   (      self      :			List[Any]     )  ->			str:
        _A    		=  f'\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps epoch\n        --output_dir {self.tmpdir}\n        '.split()
        run_command(self._launch_args + testargs     )
        self.assertTrue(os.path.exists(os.path.join(self.tmpdir, 'epoch_0'     )     )     )
   def 	__UpperCAmelCase   (      self      :			Any     )  ->			Optional[int]:
        _A    		=  f'\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps 1\n        --output_dir {self.tmpdir}\n        '.split()
        _A    		=  run_command(self._launch_args + testargs     )
        self.assertTrue(os.path.exists(os.path.join(self.tmpdir, 'step_2'     )     )     )
   def 	__UpperCAmelCase   (      self      :			Any     )  ->			str:
        _A    		=  f'\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir, "epoch_0"     )}\n        '.split()
        _A    		=  run_command(self._launch_args + testargs, return_stdout=UpperCamelCase__     )
        self.assertNotIn('epoch 0:', UpperCamelCase__     )
        self.assertIn('epoch 1:', UpperCamelCase__     )
   def 	__UpperCAmelCase   (      self      :			Optional[Any]     )  ->			List[str]:
        _A    		=  f'\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir, "step_2"     )}\n        '.split()
        _A    		=  run_command(self._launch_args + testargs, return_stdout=UpperCamelCase__     )
        if torch.cuda.is_available():
             _A    		=  torch.cuda.device_count()
        else:
             _A    		=  1
        if num_processes > 1:
             self.assertNotIn('epoch 0:', UpperCamelCase__     )
             self.assertIn('epoch 1:', UpperCamelCase__     )
        else:
             self.assertIn('epoch 0:', UpperCamelCase__     )
             self.assertIn('epoch 1:', UpperCamelCase__     )
   @slow
   def 	__UpperCAmelCase   (      self      :			List[Any]     )  ->			Optional[int]:
        _A    		=  '\n        examples/by_feature/cross_validation.py\n        --num_folds 2\n        '.split()
        with mock.patch.dict(os.environ, {'TESTING_MOCKED_DATALOADERS': '0'}     ):
             _A    		=  run_command(self._launch_args + testargs, return_stdout=UpperCamelCase__     )
             _A    		=  re.findall('({.+})', UpperCamelCase__     )
             _A    		=  [r for r in results if 'accuracy' in r][-1]
             _A    		=  ast.literal_eval(UpperCamelCase__     )
             self.assertGreaterEqual(results['accuracy'], 0.75     )
   def 	__UpperCAmelCase   (      self      :			Any     )  ->			Dict:
        _A    		=  ['examples/by_feature/multi_process_metrics.py']
        run_command(self._launch_args + testargs     )
   @require_trackers
   @mock.patch.dict(os.environ, {'WANDB_MODE': 'offline'}     )
   def 	__UpperCAmelCase   (      self      :			Tuple     )  ->			Union[str, Any]:
        with tempfile.TemporaryDirectory() as tmpdir:
             _A    		=  f'\n            examples/by_feature/tracking.py\n            --with_tracking\n            --project_dir {tmpdir}\n            '.split()
             run_command(self._launch_args + testargs     )
             self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__, 'tracking'     )     )     )
   def 	__UpperCAmelCase   (      self      :			Optional[Any]     )  ->			Optional[Any]:
        _A    		=  ['examples/by_feature/gradient_accumulation.py']
        run_command(self._launch_args + testargs     )

   def 	__UpperCAmelCase   (      self      :			str     )  ->			Optional[Any]:
        _A    		=  ['examples/by_feature/local_sgd.py']
        run_command(self._launch_args + testargs     )








"""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     UpperCAmelCase    (	unittest.TestCase  ):



   def    __UpperCAmelCase    (       self    :	Any				):



     """simple docstring"""
     _snake_case   	=      0



   def    __UpperCAmelCase    (       self    :	Optional[Any]				):



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



   def    __UpperCAmelCase    (       self    :	List[str]				):



     """simple docstring"""
     _snake_case   	=      AutoConfig.from_pretrained('''bert-base-uncased'''				)
     self.assertIsInstance(__lowerCamelCase							,       __lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	Union[str, Any]				):



     """simple docstring"""
     _snake_case   	=      AutoConfig.from_pretrained(__lowerCamelCase				)
     self.assertIsInstance(__lowerCamelCase							,       __lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	int				):



     """simple docstring"""
     _snake_case   	=      AutoConfig.from_pretrained(__lowerCamelCase				)
     self.assertIsInstance(__lowerCamelCase							,       __lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	Union[str, Any]				):



     """simple docstring"""
     _snake_case   	=      AutoConfig.for_model('''roberta'''				)
     self.assertIsInstance(__lowerCamelCase							,       __lowerCamelCase				)



   def    __UpperCAmelCase    (       self    :	Dict				):



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



   def    __UpperCAmelCase    (       self    :	Optional[Any]				):



     """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
       _snake_case   	=      CustomConfig()
       with tempfile.TemporaryDirectory() as tmp_dir:
         config.save_pretrained(__lowerCamelCase				)
         _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    __UpperCAmelCase    (       self    :	Optional[int]				):



     """simple docstring"""
     with self.assertRaisesRegex(
         __lowerCamelCase							,       '''bert-base is not a local folder and is not a valid model identifier'''				):
       _snake_case   	=      AutoConfig.from_pretrained('''bert-base'''				)



   def    __UpperCAmelCase    (       self    :	Dict				):



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



   def    __UpperCAmelCase    (       self    :	Dict				):



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



   def    __UpperCAmelCase    (       self    :	str				):



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

     _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				)
       _snake_case   	=      AutoConfig.from_pretrained(__lowerCamelCase							,       trust_remote_code=__lowerCamelCase				)
     self.assertEqual(reloaded_config.__class__.__name__							,       '''NewModelConfig'''				)





   def    __UpperCAmelCase    (       self    :	Union[str, Any]				):



     """simple docstring"""



     class     UpperCAmelCase    (	__SCREAMING_SNAKE_CASE  ):
        A__		:	List[Any]           =  '''new-model'''

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





