


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







				"""simple docstring"""



				def 					_snake_case  (self       ,			__lowercase       ,			__lowercase       ,			__lowercase = None       ,			__lowercase = None		):
										__lowerCAmelCase							       =				None
										__lowerCAmelCase							       =				os.path.abspath(os.path.join('''examples'''       ,			'''by_feature'''		)		)
										__lowerCAmelCase							       =				os.path.abspath('''examples'''		)
										for item in os.listdir(__lowercase		):
																if item not in EXCLUDE_EXAMPLES:
																						__lowerCAmelCase							       =				os.path.join(__lowercase       ,			__lowercase		)
																						if os.path.isfile(__lowercase		) and ".py" in item_path:
																												with self.subTest(
																												    tested_script=__lowercase       ,			feature_script=__lowercase       ,			tested_section='''main()''' if parser_only else '''training_function()'''       ,			):
																																		__lowerCAmelCase							       =				compare_against_test(
																																		    os.path.join(__lowercase       ,			__lowercase		)       ,			__lowercase       ,			__lowercase       ,			__lowercase		)
																																		__lowerCAmelCase							       =				'''\n'''.join(__lowercase		)
																																		if special_strings is not None:
																																								for string in special_strings:
																																														__lowerCAmelCase							       =				diff.replace(__lowercase       ,			''''''		)
																																		self.assertEqual(__lowercase       ,			''''''		)




				def 					_snake_case  (self		):
										self.one_complete_example('''complete_nlp_example.py'''       ,			__lowercase		)
										self.one_complete_example('''complete_nlp_example.py'''       ,			__lowercase		)




				def 					_snake_case  (self		):
										__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'''       ,			__lowercase       ,			__lowercase       ,			__lowercase		)
										self.one_complete_example('''complete_cv_example.py'''       ,			__lowercase       ,			__lowercase       ,			__lowercase		)


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







				"""simple docstring"""



				__UpperCamelCase    :		Union[str, Any]	=	False




				@classmethod
				def 					_snake_case  (cls		):
										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 					_snake_case  (cls		):
										super().tearDownClass()
										shutil.rmtree(cls._tmpdir		)




				def 					_snake_case  (self		):
										__lowerCAmelCase							       =				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 					_snake_case  (self		):
										__lowerCAmelCase							       =				F"""
        examples/by_feature/checkpointing.py
        --checkpointing_steps 1
        --output_dir {self.tmpdir}
        """.split()
										__lowerCAmelCase							       =				run_command(self._launch_args + testargs		)
										self.assertTrue(os.path.exists(os.path.join(self.tmpdir       ,			'''step_2'''		)		)		)




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




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




				@slow
				def 					_snake_case  (self		):
										__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=__lowercase		)
																__lowerCAmelCase							       =				re.findall('''({.+})'''       ,			__lowercase		)
																__lowerCAmelCase							       =				[r for r in results if '''accuracy''' in r][-1]
																__lowerCAmelCase							       =				ast.literal_eval(__lowercase		)
																self.assertGreaterEqual(results['''accuracy''']       ,			0.7_5		)




				def 					_snake_case  (self		):
										__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 					_snake_case  (self		):
										with tempfile.TemporaryDirectory() as tmpdir:
																__lowerCAmelCase							       =				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(__lowercase       ,			'''tracking'''		)		)		)




				def 					_snake_case  (self		):
										__lowerCAmelCase							       =				['''examples/by_feature/gradient_accumulation.py''']
										run_command(self._launch_args + testargs		)




				def 					_snake_case  (self		):
										__lowerCAmelCase							       =				['''examples/by_feature/local_sgd.py''']
										run_command(self._launch_args + testargs		)





'''simple docstring'''

from manim import *


class       a__				( __A      ):







				"""simple docstring"""



				def 					_snake_case  (self		):
										__lowerCAmelCase							       =				Rectangle(height=0.5       ,			width=0.5		)
										__lowerCAmelCase							       =				Rectangle(height=0.4_6       ,			width=0.4_6		).set_stroke(width=0		)

										__lowerCAmelCase							       =				[mem.copy() for i in range(6		)]
										__lowerCAmelCase							       =				[mem.copy() for i in range(6		)]
										__lowerCAmelCase							       =				VGroup(*__lowercase		).arrange(__lowercase       ,			buff=0		)
										__lowerCAmelCase							       =				VGroup(*__lowercase		).arrange(__lowercase       ,			buff=0		)
										__lowerCAmelCase							       =				VGroup(__lowercase       ,			__lowercase		).arrange(__lowercase       ,			buff=0		)
										__lowerCAmelCase							       =				Text('''CPU'''       ,			font_size=24		)
										__lowerCAmelCase							       =				Group(__lowercase       ,			__lowercase		).arrange(__lowercase       ,			buff=0.5       ,			aligned_edge=__lowercase		)
										cpu.move_to([-2.5, -0.5, 0]		)
										self.add(__lowercase		)

										__lowerCAmelCase							       =				[mem.copy() for i in range(1		)]
										__lowerCAmelCase							       =				VGroup(*__lowercase		).arrange(__lowercase       ,			buff=0		)
										__lowerCAmelCase							       =				Text('''GPU'''       ,			font_size=24		)
										__lowerCAmelCase							       =				Group(__lowercase       ,			__lowercase		).arrange(__lowercase       ,			buff=0.5       ,			aligned_edge=__lowercase		)
										gpu.align_to(__lowercase       ,			__lowercase		)
										gpu.set_x(gpu.get_x() - 1		)

										self.add(__lowercase		)

										__lowerCAmelCase							       =				[mem.copy() for i in range(6		)]
										__lowerCAmelCase							       =				VGroup(*__lowercase		).arrange(__lowercase       ,			buff=0		)

										__lowerCAmelCase							       =				Text('''Model'''       ,			font_size=24		)
										__lowerCAmelCase							       =				Group(__lowercase       ,			__lowercase		).arrange(__lowercase       ,			buff=0.5       ,			aligned_edge=__lowercase		)
										model.move_to([3, -1.0, 0]		)

										self.play(
										    Create(__lowercase       ,			run_time=1		)       ,			Create(__lowercase       ,			run_time=1		)       ,			Create(__lowercase       ,			run_time=1		)       ,			)

										__lowerCAmelCase							       =				MarkupText(
										    F"""First, an empty model skeleton is loaded\ninto <span fgcolor='{YELLOW}'>memory</span> without using much RAM."""       ,			font_size=24       ,			)

										__lowerCAmelCase							       =				Square(side_length=2.2		)
										key.move_to([-5, 2, 0]		)

										__lowerCAmelCase							       =				MarkupText(
										    F"""<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model"""       ,			font_size=18       ,			)

										key_text.move_to([-5, 2.4, 0]		)

										step_a.move_to([2, 2, 0]		)
										self.play(Write(__lowercase       ,			run_time=2.5		)       ,			Write(__lowercase		)       ,			Write(__lowercase		)		)

										self.add(__lowercase		)

										__lowerCAmelCase							       =				[]
										__lowerCAmelCase							       =				[]
										__lowerCAmelCase							       =				[]
										for i, rect in enumerate(__lowercase		):
																__lowerCAmelCase							       =				Rectangle(height=0.4_6       ,			width=0.4_6		).set_stroke(width=0.0		).set_fill(__lowercase       ,			opacity=0.7		)
																cpu_target.move_to(__lowercase		)
																cpu_target.generate_target()
																__lowerCAmelCase							       =				0.4_6 / 4
																__lowerCAmelCase							       =				0.4_6 / 3

																if i == 0:
																						cpu_target.target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT		)       ,			buff=0.0_2       ,			direction=__lowercase		)
																						cpu_target.target.set_x(cpu_target.target.get_x() + 0.1		)
																elif i == 3:
																						cpu_target.target.next_to(cpu_targs[0].target       ,			direction=__lowercase       ,			buff=0.0		)
																else:
																						cpu_target.target.next_to(cpu_targs[i - 1].target       ,			direction=__lowercase       ,			buff=0.0		)
																cpu_targs.append(__lowercase		)

																first_animations.append(rect.animate(run_time=0.5		).set_stroke(__lowercase		)		)
																second_animations.append(MoveToTarget(__lowercase       ,			run_time=1.5		)		)

										self.play(*__lowercase		)
										self.play(*__lowercase		)

										self.wait()

