env.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env python
  2. # Copyright 2022 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. import platform
  18. import subprocess
  19. import numpy as np
  20. import psutil
  21. import torch
  22. from accelerate import __version__ as version
  23. from accelerate.commands.config import default_config_file, load_config_from_file
  24. from ..utils import is_mlu_available, is_musa_available, is_npu_available, is_sdaa_available, is_xpu_available
  25. def env_command_parser(subparsers=None):
  26. if subparsers is not None:
  27. parser = subparsers.add_parser("env")
  28. else:
  29. parser = argparse.ArgumentParser("Accelerate env command")
  30. parser.add_argument(
  31. "--config_file", default=None, help="The config file to use for the default values in the launching script."
  32. )
  33. if subparsers is not None:
  34. parser.set_defaults(func=env_command)
  35. return parser
  36. def env_command(args):
  37. pt_version = torch.__version__
  38. pt_cuda_available = torch.cuda.is_available()
  39. pt_xpu_available = is_xpu_available()
  40. pt_mlu_available = is_mlu_available()
  41. pt_sdaa_available = is_sdaa_available()
  42. pt_musa_available = is_musa_available()
  43. pt_npu_available = is_npu_available()
  44. accelerator = "N/A"
  45. if pt_cuda_available:
  46. accelerator = "CUDA"
  47. elif pt_xpu_available:
  48. accelerator = "XPU"
  49. elif pt_mlu_available:
  50. accelerator = "MLU"
  51. elif pt_sdaa_available:
  52. accelerator = "SDAA"
  53. elif pt_musa_available:
  54. accelerator = "MUSA"
  55. elif pt_npu_available:
  56. accelerator = "NPU"
  57. accelerate_config = "Not found"
  58. # Get the default from the config file.
  59. if args.config_file is not None or os.path.isfile(default_config_file):
  60. accelerate_config = load_config_from_file(args.config_file).to_dict()
  61. # if we can run which, get it
  62. command = None
  63. bash_location = "Not found"
  64. if os.name == "nt":
  65. command = ["where", "accelerate"]
  66. elif os.name == "posix":
  67. command = ["which", "accelerate"]
  68. if command is not None:
  69. bash_location = subprocess.check_output(command, text=True, stderr=subprocess.STDOUT).strip()
  70. info = {
  71. "`Accelerate` version": version,
  72. "Platform": platform.platform(),
  73. "`accelerate` bash location": bash_location,
  74. "Python version": platform.python_version(),
  75. "Numpy version": np.__version__,
  76. "PyTorch version": f"{pt_version}",
  77. "PyTorch accelerator": accelerator,
  78. "System RAM": f"{psutil.virtual_memory().total / 1024**3:.2f} GB",
  79. }
  80. if pt_cuda_available:
  81. info["GPU type"] = torch.cuda.get_device_name()
  82. elif pt_xpu_available:
  83. info["XPU type"] = torch.xpu.get_device_name()
  84. elif pt_mlu_available:
  85. info["MLU type"] = torch.mlu.get_device_name()
  86. elif pt_sdaa_available:
  87. info["SDAA type"] = torch.sdaa.get_device_name()
  88. elif pt_musa_available:
  89. info["MUSA type"] = torch.musa.get_device_name()
  90. elif pt_npu_available:
  91. info["CANN version"] = torch.version.cann
  92. print("\nCopy-and-paste the text below in your GitHub issue\n")
  93. print("\n".join([f"- {prop}: {val}" for prop, val in info.items()]))
  94. print("- `Accelerate` default config:" if args.config_file is None else "- `Accelerate` config passed:")
  95. accelerate_config_str = (
  96. "\n".join([f"\t- {prop}: {val}" for prop, val in accelerate_config.items()])
  97. if isinstance(accelerate_config, dict)
  98. else f"\t{accelerate_config}"
  99. )
  100. print(accelerate_config_str)
  101. info["`Accelerate` configs"] = accelerate_config
  102. return info
  103. def main() -> int:
  104. parser = env_command_parser()
  105. args = parser.parse_args()
  106. env_command(args)
  107. return 0
  108. if __name__ == "__main__":
  109. raise SystemExit(main())