





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

A__							:  Tuple    =[
    '''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 					UpperCAmelCase	(     unittest.TestCase  ):




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




      def 							lowercase__       (     self     :					List[str]    )							-> List[str]:
            self.one_complete_example("""complete_nlp_example.py"""     ,     __snake_case    )
            self.one_complete_example("""complete_nlp_example.py"""     ,     __snake_case    )




      def 							lowercase__       (     self     :					Tuple    )							-> List[Any]:
            _lowerCAmelCase					=				os.path.abspath(os.path.join("""examples"""     ,     """cv_example.py"""    )    )
            _lowerCAmelCase					=				[
                """ """ * 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"""     ,     __snake_case     ,     __snake_case     ,     __snake_case    )
            self.one_complete_example("""complete_cv_example.py"""     ,     __snake_case     ,     __snake_case     ,     __snake_case    )




@mock.patch.dict(os.environ				,						{'''TESTING_MOCKED_DATALOADERS''': '''1'''}  )
class 					UpperCAmelCase	(     snake_case_  ):
      _lowercase:     Optional[int]			=  False




      @classmethod
      def 							lowercase__       (     cls     :					Any    )							-> List[Any]:
            super().setUpClass()
            _lowerCAmelCase					=				tempfile.mkdtemp()
            _lowerCAmelCase					=				os.path.join(cls._tmpdir     ,     """default_config.yml"""    )

            write_basic_config(save_location=cls.configPath    )
            _lowerCAmelCase					=				["""accelerate""", """launch""", """--config_file""", cls.configPath]




      @classmethod
      def 							lowercase__       (     cls     :					Dict    )							-> str:
            super().tearDownClass()
            shutil.rmtree(cls._tmpdir    )




      def 							lowercase__       (     self     :					Tuple    )							-> List[str]:
            _lowerCAmelCase					=				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 							lowercase__       (     self     :					Optional[Any]    )							-> Union[str, Any]:
            _lowerCAmelCase					=				f"\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps 1\n        --output_dir {self.tmpdir}\n        ".split()
            _lowerCAmelCase					=				run_command(self._launch_args + testargs    )
            self.assertTrue(os.path.exists(os.path.join(self.tmpdir     ,     """step_2"""    )    )    )




      def 							lowercase__       (     self     :					int    )							-> Optional[int]:
            _lowerCAmelCase					=				f"\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir     ,     'epoch_0'    )}\n        ".split()
            _lowerCAmelCase					=				run_command(self._launch_args + testargs     ,     return_stdout=__snake_case    )
            self.assertNotIn("""epoch 0:"""     ,     __snake_case    )
            self.assertIn("""epoch 1:"""     ,     __snake_case    )




      def 							lowercase__       (     self     :					Union[str, Any]    )							-> str:
            _lowerCAmelCase					=				f"\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir     ,     'step_2'    )}\n        ".split()
            _lowerCAmelCase					=				run_command(self._launch_args + testargs     ,     return_stdout=__snake_case    )
            if torch.cuda.is_available():
                  _lowerCAmelCase					=				torch.cuda.device_count()
            else:
                  _lowerCAmelCase					=				1
            if num_processes > 1:
                  self.assertNotIn("""epoch 0:"""     ,     __snake_case    )
                  self.assertIn("""epoch 1:"""     ,     __snake_case    )
            else:
                  self.assertIn("""epoch 0:"""     ,     __snake_case    )
                  self.assertIn("""epoch 1:"""     ,     __snake_case    )




      @slow
      def 							lowercase__       (     self     :					Optional[Any]    )							-> Any:
            _lowerCAmelCase					=				"""
        examples/by_feature/cross_validation.py
        --num_folds 2
        """.split()
            with mock.patch.dict(os.environ     ,     {"""TESTING_MOCKED_DATALOADERS""": """0"""}    ):
                  _lowerCAmelCase					=				run_command(self._launch_args + testargs     ,     return_stdout=__snake_case    )
                  _lowerCAmelCase					=				re.findall("""({.+})"""     ,     __snake_case    )
                  _lowerCAmelCase					=				[r for r in results if """accuracy""" in r][-1]
                  _lowerCAmelCase					=				ast.literal_eval(__snake_case    )
                  self.assertGreaterEqual(results["""accuracy"""]     ,     0.75    )




      def 							lowercase__       (     self     :					Optional[int]    )							-> Tuple:
            _lowerCAmelCase					=				["""examples/by_feature/multi_process_metrics.py"""]
            run_command(self._launch_args + testargs    )




      @require_trackers
      @mock.patch.dict(os.environ     ,     {"""WANDB_MODE""": """offline"""}    )
      def 							lowercase__       (     self     :					List[Any]    )							-> Dict:
            with tempfile.TemporaryDirectory() as tmpdir:
                  _lowerCAmelCase					=				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(__snake_case     ,     """tracking"""    )    )    )




      def 							lowercase__       (     self     :					Dict    )							-> Optional[int]:
            _lowerCAmelCase					=				["""examples/by_feature/gradient_accumulation.py"""]
            run_command(self._launch_args + testargs    )




      def 							lowercase__       (     self     :					Optional[Any]    )							-> Dict:
            _lowerCAmelCase					=				["""examples/by_feature/local_sgd.py"""]
            run_command(self._launch_args + testargs    )













'''simple docstring'''






import os


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_doctest_list.py
A__							:  Optional[int]    ='''.'''


if __name__ == "__main__":
      A__							:  int    =os.path.join(REPO_PATH, '''utils/documentation_tests.txt''')
      A__							:  Optional[int]    =[]
      A__							:  Tuple    =[]
      with open(doctest_file_path) as fp:
            for line in fp:
                  A__							:  Optional[Any]    =line.strip()
                  A__							:  Union[str, Any]    =os.path.join(REPO_PATH, line)
                  if not (os.path.isfile(path) or os.path.isdir(path)):
                        non_existent_paths.append(line)
                  all_paths.append(path)
      if len(non_existent_paths) > 0:
            A__							:  List[Any]    ='''\n'''.join(non_existent_paths)
            raise ValueError(F"""`utils/documentation_tests.txt` contains non-existent paths:\n{non_existent_paths}""")
      if all_paths != sorted(all_paths):
            raise ValueError('''Files in `utils/documentation_tests.txt` are not in alphabetical order.''')






