


'''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_					:    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  a     (     unittest.TestCase       ):
 def 							__UpperCAmelCase		( self      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__ = None      ,				__magic_name__ = None						)					->							Any:
      _a			      =							None
      _a			      =							os.path.abspath(os.path.join('examples'      ,				'by_feature'						)						)
      _a			      =							os.path.abspath('examples'						)
      for item in os.listdir(__magic_name__						):
           if item not in EXCLUDE_EXAMPLES:
                _a			      =							os.path.join(__magic_name__      ,				__magic_name__						)
                if os.path.isfile(__magic_name__						) and ".py" in item_path:
                     with self.subTest(
                         tested_script=__magic_name__      ,				feature_script=__magic_name__      ,				tested_section='main()' if parser_only else 'training_function()'      ,				):
                          _a			      =							compare_against_test(
                              os.path.join(__magic_name__      ,				__magic_name__						)      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__						)
                          _a			      =							'\n'.join(__magic_name__						)
                          if special_strings is not None:
                               for string in special_strings:
                                    _a			      =							diff.replace(__magic_name__      ,				''						)
                          self.assertEqual(__magic_name__      ,				''						)
 def 							__UpperCAmelCase		( self						)					->							Any:
      self.one_complete_example('complete_nlp_example.py'      ,				__magic_name__						)
      self.one_complete_example('complete_nlp_example.py'      ,				__magic_name__						)




 def 							__UpperCAmelCase		( self						)					->							int:
      _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'      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__						)
      self.one_complete_example('complete_cv_example.py'      ,				__magic_name__      ,				__magic_name__      ,				__magic_name__						)





@mock.patch.dict(os.environ  ,				{"""TESTING_MOCKED_DATALOADERS""": """1"""}       )
class  a     (     _SCREAMING_SNAKE_CASE       ):
 _lowerCAmelCase 					=	False
 @classmethod
 def 							__UpperCAmelCase		( cls						)					->							Optional[int]:
      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						)					->							Optional[int]:
      super().tearDownClass()
      shutil.rmtree(cls._tmpdir						)
 def 							__UpperCAmelCase		( self						)					->							Optional[Any]:
      _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						)					->							Optional[Any]:
      _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						)					->							Union[str, Any]:
      _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=__magic_name__						)
      self.assertNotIn('epoch 0:'      ,				__magic_name__						)
      self.assertIn('epoch 1:'      ,				__magic_name__						)
 def 							__UpperCAmelCase		( self						)					->							Any:
      _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=__magic_name__						)
      if torch.cuda.is_available():
           _a			      =							torch.cuda.device_count()
      else:
           _a			      =							1
      if num_processes > 1:
           self.assertNotIn('epoch 0:'      ,				__magic_name__						)
           self.assertIn('epoch 1:'      ,				__magic_name__						)
      else:
           self.assertIn('epoch 0:'      ,				__magic_name__						)
           self.assertIn('epoch 1:'      ,				__magic_name__						)
 @slow
 def 							__UpperCAmelCase		( self						)					->							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=__magic_name__						)
           _a			      =							re.findall('({.+})'      ,				__magic_name__						)
           _a			      =							[r for r in results if 'accuracy' in r][-1]
           _a			      =							ast.literal_eval(__magic_name__						)
           self.assertGreaterEqual(results['accuracy']      ,				0.7_5						)
 def 							__UpperCAmelCase		( self						)					->							Union[str, Any]:
      _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:
      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(__magic_name__      ,				'tracking'						)						)						)
 def 							__UpperCAmelCase		( self						)					->							int:
      _a			      =							['examples/by_feature/gradient_accumulation.py']
      run_command(self._launch_args + testargs						)




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





'''simple docstring'''






def 	_A				(lowerCAmelCase__  :Union[str, Any]		)   ->  List[str]:




     '''simple docstring'''



     return [
         {
             0: [1, 2],
             1: [0, 2],
             2: [0, 1, 3, 5],
             3: [2, 4],
             4: [3],
             5: [2, 6, 8],
             6: [5, 7],
             7: [6, 8],
             8: [5, 7],
         },
         {
             0: [6],
             1: [9],
             2: [4, 5],
             3: [4],
             4: [2, 3],
             5: [2],
             6: [0, 7],
             7: [6],
             8: [],
             9: [1],
         },
         {
             0: [4],
             1: [6],
             2: [],
             3: [5, 6, 7],
             4: [0, 6],
             5: [3, 8, 9],
             6: [1, 3, 4, 7],
             7: [3, 6, 8, 9],
             8: [5, 7],
             9: [5, 7],
         },
         {
             0: [1, 3],
             1: [0, 2, 4],
             2: [1, 3, 4],
             3: [0, 2, 4],
             4: [1, 2, 3],
         },
     ][index]






def 	_A				(lowerCAmelCase__  :dict[int, list[int]]		)   ->  list[tuple[int, int]]:




     '''simple docstring'''



     _a			      =							0
     _a			      =							len(lowerCAmelCase__		)  # No of vertices in graph
     _a			      =							[0] * n
     _a			      =							[False] * n

     def dfs(lowerCAmelCase__  :Dict				,						lowerCAmelCase__  :str				,						lowerCAmelCase__  :Dict				,						lowerCAmelCase__  :Union[str, Any]		):
          _a			      =							True
          _a			      =							id_
          id_ += 1
          for to in graph[at]:
               if to == parent:
                    pass
               elif not visited[to]:
                    dfs(lowerCAmelCase__				,						lowerCAmelCase__				,						lowerCAmelCase__				,						id_		)
                    _a			      =							min(low[at]				,						low[to]		)
                    if id_ <= low[to]:
                         bridges.append((at, to) if at < to else (to, at)		)
               else:
                    # This edge is a back edge and cannot be a bridge
                    _a			      =							min(low[at]				,						low[to]		)

     _a			      =							[]
     for i in range(lowerCAmelCase__		):
          if not visited[i]:
               dfs(lowerCAmelCase__				,						-1				,						lowerCAmelCase__				,						id_		)
     return bridges


if __name__ == "__main__":
 import doctest

 doctest.testmod()

