


"""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    :     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      lowerCamelCase__   (     unittest.TestCase				):







					"""simple docstring"""


					def lowerCamelCase__							(       self    : Dict ,       UpperCamelCase    : List[str] ,       UpperCamelCase    : str ,       UpperCamelCase    : Union[str, Any] = None ,       UpperCamelCase    : str = None  ):
												'''simple docstring'''






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






					def lowerCamelCase__							(       self    : int  ):
												'''simple docstring'''






												self.one_complete_example("""complete_nlp_example.py""" ,       lowerCAmelCase__  )
												self.one_complete_example("""complete_nlp_example.py""" ,       lowerCAmelCase__  )






					def lowerCamelCase__							(       self    : Any  ):
												'''simple docstring'''






												__UpperCAmelCase   :						Optional[Any]      							=					os.path.abspath(os.path.join("""examples""" ,       """cv_example.py"""  )  )
												__UpperCAmelCase   :						List[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""" ,       lowerCAmelCase__ ,       lowerCAmelCase__ ,       lowerCAmelCase__  )
												self.one_complete_example("""complete_cv_example.py""" ,       lowerCAmelCase__ ,       lowerCAmelCase__ ,       lowerCAmelCase__  )






@mock.patch.dict(os.environ     ,    {"""TESTING_MOCKED_DATALOADERS""": """1"""}				)
class      lowerCamelCase__   (     lowerCamelCase_				):







					"""simple docstring"""


					__a	       =							False






					@classmethod
					def lowerCamelCase__							(       cls    : Tuple  ):
												'''simple docstring'''






												super().setUpClass()
												__UpperCAmelCase   :						Optional[Any]      							=					tempfile.mkdtemp()
												__UpperCAmelCase   :						str      							=					os.path.join(cls._tmpdir ,       """default_config.yml"""  )

												write_basic_config(save_location=cls.configPath  )
												__UpperCAmelCase   :						List[str]      							=					["""accelerate""", """launch""", """--config_file""", cls.configPath]






					@classmethod
					def lowerCamelCase__							(       cls    : Any  ):
												'''simple docstring'''






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






					def lowerCamelCase__							(       self    : Optional[Any]  ):
												'''simple docstring'''






												__UpperCAmelCase   :						Optional[int]      							=					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 lowerCamelCase__							(       self    : str  ):
												'''simple docstring'''






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






					def lowerCamelCase__							(       self    : Tuple  ):
												'''simple docstring'''






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






					def lowerCamelCase__							(       self    : Optional[Any]  ):
												'''simple docstring'''






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






					@slow
					def lowerCamelCase__							(       self    : Union[str, Any]  ):
												'''simple docstring'''






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






					def lowerCamelCase__							(       self    : int  ):
												'''simple docstring'''






												__UpperCAmelCase   :						str      							=					["""examples/by_feature/multi_process_metrics.py"""]
												run_command(self._launch_args + testargs  )






					@require_trackers
					@mock.patch.dict(os.environ ,       {"""WANDB_MODE""": """offline"""}  )
					def lowerCamelCase__							(       self    : List[Any]  ):
												'''simple docstring'''






												with tempfile.TemporaryDirectory() as tmpdir:
																			__UpperCAmelCase   :						Any      							=					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(lowerCAmelCase__ ,       """tracking"""  )  )  )






					def lowerCamelCase__							(       self    : List[str]  ):
												'''simple docstring'''






												__UpperCAmelCase   :						Optional[Any]      							=					["""examples/by_feature/gradient_accumulation.py"""]
												run_command(self._launch_args + testargs  )






					def lowerCamelCase__							(       self    : Any  ):
												'''simple docstring'''






												__UpperCAmelCase   :						Union[str, Any]      							=					["""examples/by_feature/local_sgd.py"""]
												run_command(self._launch_args + testargs  )




"""simple docstring"""




__UpperCamelCase						=      frozenset(
    [
        '''prompt''',
        '''height''',
        '''width''',
        '''guidance_scale''',
        '''negative_prompt''',
        '''prompt_embeds''',
        '''negative_prompt_embeds''',
        '''cross_attention_kwargs''',
    ]
)

__UpperCamelCase						=      frozenset(['''prompt''', '''negative_prompt'''])

__UpperCamelCase						=      frozenset([])

__UpperCamelCase						=      frozenset(['''image'''])

__UpperCamelCase						=      frozenset(
    [
        '''image''',
        '''height''',
        '''width''',
        '''guidance_scale''',
    ]
)

__UpperCamelCase						=      frozenset(['''image'''])

__UpperCamelCase						=      frozenset(
    [
        '''prompt''',
        '''image''',
        '''height''',
        '''width''',
        '''guidance_scale''',
        '''negative_prompt''',
        '''prompt_embeds''',
        '''negative_prompt_embeds''',
    ]
)

__UpperCamelCase						=      frozenset(['''prompt''', '''image''', '''negative_prompt'''])

__UpperCamelCase						=      frozenset(
    [
        # Text guided image variation with an image mask
        '''prompt''',
        '''image''',
        '''mask_image''',
        '''height''',
        '''width''',
        '''guidance_scale''',
        '''negative_prompt''',
        '''prompt_embeds''',
        '''negative_prompt_embeds''',
    ]
)

__UpperCamelCase						=      frozenset(['''prompt''', '''image''', '''mask_image''', '''negative_prompt'''])

__UpperCamelCase						=      frozenset(
    [
        # image variation with an image mask
        '''image''',
        '''mask_image''',
        '''height''',
        '''width''',
        '''guidance_scale''',
    ]
)

__UpperCamelCase						=      frozenset(['''image''', '''mask_image'''])

__UpperCamelCase						=      frozenset(
    [
        '''example_image''',
        '''image''',
        '''mask_image''',
        '''height''',
        '''width''',
        '''guidance_scale''',
    ]
)

__UpperCamelCase						=      frozenset(['''example_image''', '''image''', '''mask_image'''])

__UpperCamelCase						=      frozenset(['''class_labels'''])

__UpperCamelCase						=      frozenset(['''class_labels'''])

__UpperCamelCase						=      frozenset(['''batch_size'''])

__UpperCamelCase						=      frozenset([])

__UpperCamelCase						=      frozenset(['''batch_size'''])

__UpperCamelCase						=      frozenset([])

__UpperCamelCase						=      frozenset(
    [
        '''prompt''',
        '''audio_length_in_s''',
        '''guidance_scale''',
        '''negative_prompt''',
        '''prompt_embeds''',
        '''negative_prompt_embeds''',
        '''cross_attention_kwargs''',
    ]
)

__UpperCamelCase						=      frozenset(['''prompt''', '''negative_prompt'''])
__UpperCamelCase						=      frozenset(['''input_tokens'''])

__UpperCamelCase						=      frozenset(['''input_tokens'''])


