



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

snake_case_			=     [
    """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 				lowercase__  (self : Union[str, Any],					__UpperCAmelCase : str,					__UpperCAmelCase : bool,					__UpperCAmelCase : str = None,					__UpperCAmelCase : list = None      )     ->			Tuple:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						int      					= None
					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= os.path.abspath(os.path.join('''examples''',					'''by_feature'''      )      )
					SCREAMING_SNAKE_CASE  :						Tuple      					= os.path.abspath('''examples'''      )
					for item in os.listdir(__UpperCAmelCase      ):
									if item not in EXCLUDE_EXAMPLES:
													SCREAMING_SNAKE_CASE  :						Dict      					= 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()''',					):
																					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= compare_against_test(
																					    os.path.join(__UpperCAmelCase,					__UpperCAmelCase      ),					__UpperCAmelCase,					__UpperCAmelCase,					__UpperCAmelCase      )
																					SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= '''\n'''.join(__UpperCAmelCase      )
																					if special_strings is not None:
																									for string in special_strings:
																													SCREAMING_SNAKE_CASE  :						List[str]      					= diff.replace(__UpperCAmelCase,					''''''      )
																					self.assertEqual(__UpperCAmelCase,					''''''      )


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

					"""simple docstring"""

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






	def 				lowercase__  (self : Optional[Any]      )     ->			List[Any]:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Optional[int]      					= os.path.abspath(os.path.join('''examples''',					'''cv_example.py'''      )      )
					SCREAMING_SNAKE_CASE  :						Dict      					= [
					    ''' ''' * 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 	a__     (				_lowercase	):
	__magic_name__		:    int	    =     False


	@classmethod
	def 				lowercase__  (cls : str      )     ->			int:

					"""simple docstring"""

					super().setUpClass()
					SCREAMING_SNAKE_CASE  :						Tuple      					= tempfile.mkdtemp()
					SCREAMING_SNAKE_CASE  :						int      					= os.path.join(cls._tmpdir,					'''default_config.yml'''      )

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


	@classmethod
	def 				lowercase__  (cls : Any      )     ->			str:

					"""simple docstring"""

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


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

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						str      					= 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 : Dict      )     ->			Dict:

					"""simple docstring"""

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


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

					"""simple docstring"""

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


	def 				lowercase__  (self : Optional[int]      )     ->			str:

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						List[Any]      					= F'''
        examples/by_feature/checkpointing.py
        --resume_from_checkpoint {os.path.join(self.tmpdir,					"step_2"      )}
        '''.split()
					SCREAMING_SNAKE_CASE  :						int      					= run_command(self._launch_args + testargs,					return_stdout=__UpperCAmelCase      )
					if torch.cuda.is_available():
									SCREAMING_SNAKE_CASE  :						int      					= torch.cuda.device_count()
					else:
									SCREAMING_SNAKE_CASE  :						Optional[Any]      					= 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 				lowercase__  (self : Optional[Any]      )     ->			Optional[Any]:

					"""simple docstring"""

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


	def 				lowercase__  (self : Optional[int]      )     ->			str:

					"""simple docstring"""

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

					"""simple docstring"""

					with tempfile.TemporaryDirectory() as tmpdir:
									SCREAMING_SNAKE_CASE  :						int      					= 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(__UpperCAmelCase,					'''tracking'''      )      )      )


	def 				lowercase__  (self : List[Any]      )     ->			List[Any]:

					"""simple docstring"""

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






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

					"""simple docstring"""

					SCREAMING_SNAKE_CASE  :						Optional[Any]      					= ['''examples/by_feature/local_sgd.py''']
					run_command(self._launch_args + testargs      )









'''simple docstring'''

from collections import deque






def       __lowercase  (_SCREAMING_SNAKE_CASE      :List[str]       ):
				SCREAMING_SNAKE_CASE  :						Optional[Any]      					= len(_SCREAMING_SNAKE_CASE       )
				SCREAMING_SNAKE_CASE  :						List[str]      					= deque()
				SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= [False for _ in range(_SCREAMING_SNAKE_CASE       )]
				SCREAMING_SNAKE_CASE  :						Any      					= [-1 for _ in range(_SCREAMING_SNAKE_CASE       )]
				SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= index_of[:]

				def strong_connect(_SCREAMING_SNAKE_CASE      :List[str]    ,	_SCREAMING_SNAKE_CASE      :Any    ,	_SCREAMING_SNAKE_CASE      :List[str]       ):
								SCREAMING_SNAKE_CASE  :						Any      					= index  # the number when this node is seen
								SCREAMING_SNAKE_CASE  :						str      					= index  # lowest rank node reachable from here
								index += 1
								stack.append(_SCREAMING_SNAKE_CASE       )
								SCREAMING_SNAKE_CASE  :						Optional[int]      					= True

								for w in g[v]:
												if index_of[w] == -1:
																SCREAMING_SNAKE_CASE  :						List[str]      					= strong_connect(_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE       )
																SCREAMING_SNAKE_CASE  :						Optional[int]      					= (
																    lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v]
																)
												elif on_stack[w]:
																SCREAMING_SNAKE_CASE  :						List[str]      					= (
																    lowlink_of[w] if lowlink_of[w] < lowlink_of[v] else lowlink_of[v]
																)

								if lowlink_of[v] == index_of[v]:
												SCREAMING_SNAKE_CASE  :						str      					= []
												SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= stack.pop()
												SCREAMING_SNAKE_CASE  :						Union[str, Any]      					= False
												component.append(_SCREAMING_SNAKE_CASE       )
												while w != v:
																SCREAMING_SNAKE_CASE  :						int      					= stack.pop()
																SCREAMING_SNAKE_CASE  :						Tuple      					= False
																component.append(_SCREAMING_SNAKE_CASE       )
												components.append(_SCREAMING_SNAKE_CASE       )
								return index

				SCREAMING_SNAKE_CASE  :						int      					= []
				for v in range(_SCREAMING_SNAKE_CASE       ):
								if index_of[v] == -1:
												strong_connect(_SCREAMING_SNAKE_CASE    ,	0    ,	_SCREAMING_SNAKE_CASE       )

				return components






def       __lowercase  (_SCREAMING_SNAKE_CASE      :List[str]    ,	_SCREAMING_SNAKE_CASE      :Optional[int]       ):
				SCREAMING_SNAKE_CASE  :						Tuple      					= [[] for _ in range(_SCREAMING_SNAKE_CASE       )]
				for u, v in edges:
								g[u].append(_SCREAMING_SNAKE_CASE       )
				return g


if __name__ == "__main__":
	# Test
	snake_case_			=     7
	snake_case_			=     [0, 0, 1, 2, 3, 3, 4, 4, 6]
	snake_case_			=     [1, 3, 2, 0, 1, 4, 5, 6, 5]
	snake_case_			=     [(u, v) for u, v in zip(source, target)]
	snake_case_			=     create_graph(n_vertices, edges)

	assert [[5], [6], [4], [3, 2, 1, 0]] == tarjan(g)




