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

__lowerCAmelCase         =    [
    """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	):





  """simple docstring"""

  def   __lowercase		(		self      :     Tuple							,_a      :     str							,_a      :     bool							,_a      :     str = None							,_a      :     list = None       ):


     '''simple docstring'''






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


  def   __lowercase		(		self      :     Optional[Any]       ):


     '''simple docstring'''






     self.one_complete_example('complete_nlp_example.py'							,_a       )
     self.one_complete_example('complete_nlp_example.py'							,_a       )


  def   __lowercase		(		self      :     Dict       ):


     '''simple docstring'''






     _a				:	Optional[Any]			=   os.path.abspath(os.path.join('examples'							,'cv_example.py'       )       )
     _a				:	str			=   [
         ' ' * 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'							,_a							,_a							,_a       )
     self.one_complete_example('complete_cv_example.py'							,_a							,_a							,_a       )







@mock.patch.dict(os.environ    ,				{'''TESTING_MOCKED_DATALOADERS''': '''1'''}	)
class UpperCAmelCase__	( lowercase__	):





  """simple docstring"""

  __UpperCAmelCase				:				Union[str, Any]   =					False


  @classmethod
  def   __lowercase		(		cls      :     int       ):


     '''simple docstring'''






     super().setUpClass()
     _a				:	int			=   tempfile.mkdtemp()
     _a				:	Union[str, Any]			=   os.path.join(cls._tmpdir							,'default_config.yml'       )

     write_basic_config(save_location=cls.configPath       )
     _a				:	int			=   ['accelerate', 'launch', '--config_file', cls.configPath]


  @classmethod
  def   __lowercase		(		cls      :     str       ):


     '''simple docstring'''






     super().tearDownClass()
     shutil.rmtree(cls._tmpdir       )


  def   __lowercase		(		self      :     Optional[Any]       ):


     '''simple docstring'''






     _a				:	List[Any]			=   F"""
        examples/by_feature/checkpointing.py
        --checkpointing_steps epoch
        --output_dir {self.tmpdir}
        """.split()
     run_command(self._launch_args + testargs       )
     self.assertTrue(os.path.exists(os.path.join(self.tmpdir							,'epoch_0'       )       )       )


  def   __lowercase		(		self      :     int       ):


     '''simple docstring'''






     _a				:	Optional[int]			=   F"""
        examples/by_feature/checkpointing.py
        --checkpointing_steps 1
        --output_dir {self.tmpdir}
        """.split()
     _a				:	Dict			=   run_command(self._launch_args + testargs       )
     self.assertTrue(os.path.exists(os.path.join(self.tmpdir							,'step_2'       )       )       )


  def   __lowercase		(		self      :     Dict       ):


     '''simple docstring'''






     _a				:	List[str]			=   F"""
        examples/by_feature/checkpointing.py
        --resume_from_checkpoint {os.path.join(self.tmpdir							,'epoch_0'       )}
        """.split()
     _a				:	Optional[int]			=   run_command(self._launch_args + testargs							,return_stdout=_a       )
     self.assertNotIn('epoch 0:'							,_a       )
     self.assertIn('epoch 1:'							,_a       )


  def   __lowercase		(		self      :     List[str]       ):


     '''simple docstring'''






     _a				:	Any			=   F"""
        examples/by_feature/checkpointing.py
        --resume_from_checkpoint {os.path.join(self.tmpdir							,'step_2'       )}
        """.split()
     _a				:	Any			=   run_command(self._launch_args + testargs							,return_stdout=_a       )
     if torch.cuda.is_available():
        _a				:	Dict			=   torch.cuda.device_count()
     else:
        _a				:	Any			=   1
     if num_processes > 1:
        self.assertNotIn('epoch 0:'							,_a       )
        self.assertIn('epoch 1:'							,_a       )
     else:
        self.assertIn('epoch 0:'							,_a       )
        self.assertIn('epoch 1:'							,_a       )


  @slow
  def   __lowercase		(		self      :     List[str]       ):


     '''simple docstring'''






     _a				:	int			=   '\n        examples/by_feature/cross_validation.py\n        --num_folds 2\n        '.split()
     with mock.patch.dict(os.environ							,{'TESTING_MOCKED_DATALOADERS': '0'}       ):
        _a				:	int			=   run_command(self._launch_args + testargs							,return_stdout=_a       )
        _a				:	Dict			=   re.findall('({.+})'							,_a       )
        _a				:	Optional[Any]			=   [r for r in results if 'accuracy' in r][-1]
        _a				:	Optional[Any]			=   ast.literal_eval(_a       )
        self.assertGreaterEqual(results['accuracy']							,0.75       )


  def   __lowercase		(		self      :     List[Any]       ):


     '''simple docstring'''






     _a				:	Optional[int]			=   ['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      :     int       ):


     '''simple docstring'''






     with tempfile.TemporaryDirectory() as tmpdir:
        _a				:	Any			=   F"""
            examples/by_feature/tracking.py
            --with_tracking
            --project_dir {tmpdir}
            """.split()
        run_command(self._launch_args + testargs       )
        self.assertTrue(os.path.exists(os.path.join(_a							,'tracking'       )       )       )


  def   __lowercase		(		self      :     Tuple       ):


     '''simple docstring'''






     _a				:	str			=   ['examples/by_feature/gradient_accumulation.py']
     run_command(self._launch_args + testargs       )


  def   __lowercase		(		self      :     List[Any]       ):


     '''simple docstring'''






     _a				:	str			=   ['examples/by_feature/local_sgd.py']
     run_command(self._launch_args + testargs       )





'''simple docstring'''







class UpperCAmelCase__	:





  """simple docstring"""

  def __init__(		self      :     Dict       ):


     '''simple docstring'''






     _a				:	Dict			=   {}


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


     '''simple docstring'''






     print(self.vertex       )
     for i in self.vertex:
        print(_a							,' -> '							,' -> '.join([str(_a       ) for j in self.vertex[i]]       )       )


  def   __lowercase		(		self      :     Dict							,_a      :     int							,_a      :     int       ):


     '''simple docstring'''






     if from_vertex in self.vertex:
        self.vertex[from_vertex].append(_a       )
     else:
        # else make a new vertex
        _a				:	int			=   [to_vertex]


  def   __lowercase		(		self      :     Optional[int]       ):


     '''simple docstring'''






     _a				:	Tuple			=   [False] * len(self.vertex       )

     # call the recursive helper function
     for i in range(len(self.vertex       )       ):
        if not visited[i]:
           self.dfs_recursive(_a							,_a       )


  def   __lowercase		(		self      :     Union[str, Any]							,_a      :     int							,_a      :     list       ):


     '''simple docstring'''






     _a				:	List[Any]			=   True

     print(_a							,end=' '       )

     # Recur for all the vertices that are adjacent to this node
     for i in self.vertex:
        if not visited[i]:
           self.dfs_recursive(_a							,_a       )


if __name__ == "__main__":
  __lowerCAmelCase         =    Graph()
  g.add_edge(0, 1)
  g.add_edge(0, 2)
  g.add_edge(1, 2)
  g.add_edge(2, 0)
  g.add_edge(2, 3)
  g.add_edge(3, 3)

  g.print_graph()
  print("""DFS:""")
  g.dfs()

  # OUTPUT:
  # 0  ->  1 -> 2
  # 1  ->  2
  # 2  ->  0 -> 3
  # 3  ->  3
  # DFS:
  #  0 1 2 3



