


'''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 :  Union[str, 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        __snake_case	(       unittest.TestCase):







				"""simple docstring"""



				def __lowercase       (       self   :  List[str]   ,						lowerCamelCase   :  str   ,						lowerCamelCase   :  bool   ,						lowerCamelCase   :  str = None   ,						lowerCamelCase   :  list = None		)       ->  Optional[int]:
						lowerCAmelCase_      :				Any      =       None
						lowerCAmelCase_      :				Optional[int]      =       os.path.abspath(os.path.join("""examples"""   ,						"""by_feature"""		)		)
						lowerCAmelCase_      :				List[str]      =       os.path.abspath("""examples"""		)
						for item in os.listdir(_lowerCAmelCase		):
								if item not in EXCLUDE_EXAMPLES:
										lowerCAmelCase_      :				Tuple      =       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()"""   ,						):
														lowerCAmelCase_      :				Optional[Any]      =       compare_against_test(
														    os.path.join(_lowerCAmelCase   ,						_lowerCAmelCase		)   ,						_lowerCAmelCase   ,						_lowerCAmelCase   ,						_lowerCAmelCase		)
														lowerCAmelCase_      :				Any      =       """\n""".join(_lowerCAmelCase		)
														if special_strings is not None:
																for string in special_strings:
																		lowerCAmelCase_      :				Optional[int]      =       diff.replace(_lowerCAmelCase   ,						""""""		)
														self.assertEqual(_lowerCAmelCase   ,						""""""		)

				def __lowercase       (       self   :  int		)       ->  List[Any]:
						self.one_complete_example("""complete_nlp_example.py"""   ,						_lowerCAmelCase		)
						self.one_complete_example("""complete_nlp_example.py"""   ,						_lowerCAmelCase		)





				def __lowercase       (       self   :  Union[str, Any]		)       ->  Tuple:
						lowerCAmelCase_      :				List[str]      =       os.path.abspath(os.path.join("""examples"""   ,						"""cv_example.py"""		)		)
						lowerCAmelCase_      :				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"""   ,						_lowerCAmelCase   ,						_lowerCAmelCase   ,						_lowerCAmelCase		)
						self.one_complete_example("""complete_cv_example.py"""   ,						_lowerCAmelCase   ,						_lowerCAmelCase   ,						_lowerCAmelCase		)







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







				"""simple docstring"""



				lowercase       			= False

				@classmethod
				def __lowercase       (       cls   :  Any		)       ->  Optional[Any]:
						super().setUpClass()
						lowerCAmelCase_      :				int      =       tempfile.mkdtemp()
						lowerCAmelCase_      :				Dict      =       os.path.join(cls._tmpdir   ,						"""default_config.yml"""		)

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

				@classmethod
				def __lowercase       (       cls   :  List[str]		)       ->  int:
						super().tearDownClass()
						shutil.rmtree(cls._tmpdir		)

				def __lowercase       (       self   :  str		)       ->  Dict:
						lowerCAmelCase_      :				List[Any]      =       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 __lowercase       (       self   :  Tuple		)       ->  List[Any]:
						lowerCAmelCase_      :				Dict      =       F'\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps 1\n        --output_dir {self.tmpdir}\n        '.split()
						lowerCAmelCase_      :				Dict      =       run_command(self._launch_args + testargs		)
						self.assertTrue(os.path.exists(os.path.join(self.tmpdir   ,						"""step_2"""		)		)		)

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

				def __lowercase       (       self   :  str		)       ->  Dict:
						lowerCAmelCase_      :				List[Any]      =       F'\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir   ,						"step_2"		)}\n        '.split()
						lowerCAmelCase_      :				Optional[Any]      =       run_command(self._launch_args + testargs   ,						return_stdout=_lowerCAmelCase		)
						if torch.cuda.is_available():
								lowerCAmelCase_      :				List[str]      =       torch.cuda.device_count()
						else:
								lowerCAmelCase_      :				int      =       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 __lowercase       (       self   :  Dict		)       ->  Optional[int]:
						lowerCAmelCase_      :				List[Any]      =       """\n        examples/by_feature/cross_validation.py\n        --num_folds 2\n        """.split()
						with mock.patch.dict(os.environ   ,						{"""TESTING_MOCKED_DATALOADERS""": """0"""}		):
								lowerCAmelCase_      :				int      =       run_command(self._launch_args + testargs   ,						return_stdout=_lowerCAmelCase		)
								lowerCAmelCase_      :				str      =       re.findall("""({.+})"""   ,						_lowerCAmelCase		)
								lowerCAmelCase_      :				List[str]      =       [r for r in results if """accuracy""" in r][-1]
								lowerCAmelCase_      :				List[str]      =       ast.literal_eval(_lowerCAmelCase		)
								self.assertGreaterEqual(results["""accuracy"""]   ,						0.75		)

				def __lowercase       (       self   :  int		)       ->  Any:
						lowerCAmelCase_      :				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   :  Tuple		)       ->  Optional[Any]:

						with tempfile.TemporaryDirectory() as tmpdir:
								lowerCAmelCase_      :				Dict      =       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 __lowercase       (       self   :  int		)       ->  Tuple:
						lowerCAmelCase_      :				Optional[Any]      =       ["""examples/by_feature/gradient_accumulation.py"""]
						run_command(self._launch_args + testargs		)





				def __lowercase       (       self   :  Any		)       ->  Optional[int]:
						lowerCAmelCase_      :				str      =       ["""examples/by_feature/local_sgd.py"""]
						run_command(self._launch_args + testargs		)







'''simple docstring'''






# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import os

from accelerate.utils import ComputeEnvironment

from .cluster import get_cluster_input
from .config_args import cache_dir, default_config_file, default_yaml_config_file, load_config_from_file  # noqa: F401
from .config_utils import _ask_field, _ask_options, _convert_compute_environment  # noqa: F401
from .sagemaker import get_sagemaker_input


__A :  str    							=				"Launches a series of prompts to create and save a `default_config.yaml` configuration file for your training system. Should always be ran first on your machine"







def     UpperCamelCase_      (   ):
  '''simple docstring'''





  lowerCAmelCase_      :				List[Any]      =       _ask_options(
      """In which compute environment are you running?"""							,   ["""This machine""", """AWS (Amazon SageMaker)"""]							,   _convert_compute_environment							,   )
  if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
    lowerCAmelCase_      :				str      =       get_sagemaker_input()
  else:
    lowerCAmelCase_      :				Optional[int]      =       get_cluster_input()
  return config







def     UpperCamelCase_      (   A__ :       Optional[Any]=None ):
  '''simple docstring'''





  if subparsers is not None:
    lowerCAmelCase_      :				List[str]      =       subparsers.add_parser("""config"""							,   description=A__ )
  else:
    lowerCAmelCase_      :				Optional[int]      =       argparse.ArgumentParser("""Accelerate config command"""							,   description=A__ )

  parser.add_argument(
      """--config_file"""							,   default=A__							,   help=(
          """The path to use to store the config file. Will default to a file named default_config.yaml in the cache """
          """location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have """
          """such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed """
          """with 'huggingface'."""
      )							,   )

  if subparsers is not None:
    parser.set_defaults(func=A__ )
  return parser







def     UpperCamelCase_      (   A__ :       Any ):
  '''simple docstring'''





  lowerCAmelCase_      :				Dict      =       get_user_input()
  if args.config_file is not None:
    lowerCAmelCase_      :				List[str]      =       args.config_file
  else:
    if not os.path.isdir(A__ ):
      os.makedirs(A__ )
    lowerCAmelCase_      :				List[Any]      =       default_yaml_config_file

  if config_file.endswith(""".json""" ):
    config.to_json_file(A__ )
  else:
    config.to_yaml_file(A__ )
  print(f'accelerate configuration saved at {config_file}' )







def     UpperCamelCase_      (   ):
  '''simple docstring'''





  lowerCAmelCase_      :				str      =       config_command_parser()
  lowerCAmelCase_      :				Tuple      =       parser.parse_args()
  config_command(A__ )


if __name__ == "__main__":
  main()



