config.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python
  2. # Copyright 2021 The HuggingFace Team. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import argparse
  16. import os
  17. from accelerate.utils import ComputeEnvironment
  18. from .cluster import get_cluster_input
  19. from .config_args import cache_dir, default_config_file, default_yaml_config_file, load_config_from_file # noqa: F401
  20. from .config_utils import _ask_field, _ask_options, _convert_compute_environment # noqa: F401
  21. from .sagemaker import get_sagemaker_input
  22. description = "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"
  23. def get_user_input():
  24. compute_environment = _ask_options(
  25. "In which compute environment are you running?",
  26. ["This machine", "AWS (Amazon SageMaker)"],
  27. _convert_compute_environment,
  28. )
  29. if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
  30. config = get_sagemaker_input()
  31. else:
  32. config = get_cluster_input()
  33. return config
  34. def config_command_parser(subparsers=None):
  35. if subparsers is not None:
  36. parser = subparsers.add_parser("config", description=description)
  37. else:
  38. parser = argparse.ArgumentParser("Accelerate config command", description=description)
  39. parser.add_argument(
  40. "--config_file",
  41. default=None,
  42. help=(
  43. "The path to use to store the config file. Will default to a file named default_config.yaml in the cache "
  44. "location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have "
  45. "such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed "
  46. "with 'huggingface'."
  47. ),
  48. )
  49. if subparsers is not None:
  50. parser.set_defaults(func=config_command)
  51. return parser
  52. def config_command(args):
  53. config = get_user_input()
  54. if args.config_file is not None:
  55. config_file = args.config_file
  56. else:
  57. if not os.path.isdir(cache_dir):
  58. os.makedirs(cache_dir)
  59. config_file = default_yaml_config_file
  60. if config_file.endswith(".json"):
  61. config.to_json_file(config_file)
  62. else:
  63. config.to_yaml_file(config_file)
  64. print(f"accelerate configuration saved at {config_file}")
  65. def main():
  66. parser = config_command_parser()
  67. args = parser.parse_args()
  68. config_command(args)
  69. if __name__ == "__main__":
  70. main()