env_report.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # Copyright (c) Microsoft Corporation.
  2. # SPDX-License-Identifier: Apache-2.0
  3. # DeepSpeed Team
  4. import os
  5. import torch
  6. import deepspeed
  7. import subprocess
  8. import argparse
  9. from .ops.op_builder.all_ops import ALL_OPS
  10. from .git_version_info import installed_ops, torch_info
  11. from deepspeed.accelerator import get_accelerator
  12. GREEN = '\033[92m'
  13. RED = '\033[91m'
  14. YELLOW = '\033[93m'
  15. END = '\033[0m'
  16. SUCCESS = f"{GREEN} [SUCCESS] {END}"
  17. OKAY = f"{GREEN}[OKAY]{END}"
  18. WARNING = f"{YELLOW}[WARNING]{END}"
  19. FAIL = f'{RED}[FAIL]{END}'
  20. INFO = '[INFO]'
  21. color_len = len(GREEN) + len(END)
  22. okay = f"{GREEN}[OKAY]{END}"
  23. warning = f"{YELLOW}[WARNING]{END}"
  24. def op_report(verbose=True):
  25. max_dots = 23
  26. max_dots2 = 11
  27. h = ["op name", "installed", "compatible"]
  28. print("-" * (max_dots + max_dots2 + len(h[0]) + len(h[1])))
  29. print("DeepSpeed C++/CUDA extension op report")
  30. print("-" * (max_dots + max_dots2 + len(h[0]) + len(h[1])))
  31. print("NOTE: Ops not installed will be just-in-time (JIT) compiled at\n"
  32. " runtime if needed. Op compatibility means that your system\n"
  33. " meet the required dependencies to JIT install the op.")
  34. print("-" * (max_dots + max_dots2 + len(h[0]) + len(h[1])))
  35. print("JIT compiled ops requires ninja")
  36. ninja_status = OKAY if ninja_installed() else FAIL
  37. print('ninja', "." * (max_dots - 5), ninja_status)
  38. print("-" * (max_dots + max_dots2 + len(h[0]) + len(h[1])))
  39. print(h[0], "." * (max_dots - len(h[0])), h[1], "." * (max_dots2 - len(h[1])), h[2])
  40. print("-" * (max_dots + max_dots2 + len(h[0]) + len(h[1])))
  41. installed = f"{GREEN}[YES]{END}"
  42. no = f"{YELLOW}[NO]{END}"
  43. for op_name, builder in ALL_OPS.items():
  44. dots = "." * (max_dots - len(op_name))
  45. is_compatible = OKAY if builder.is_compatible(verbose) else no
  46. is_installed = installed if installed_ops.get(op_name, False) else no
  47. dots2 = '.' * ((len(h[1]) + (max_dots2 - len(h[1]))) - (len(is_installed) - color_len))
  48. print(op_name, dots, is_installed, dots2, is_compatible)
  49. print("-" * (max_dots + max_dots2 + len(h[0]) + len(h[1])))
  50. def ninja_installed():
  51. try:
  52. import ninja # noqa: F401 # type: ignore
  53. except ImportError:
  54. return False
  55. return True
  56. def nvcc_version():
  57. import torch.utils.cpp_extension
  58. cuda_home = torch.utils.cpp_extension.CUDA_HOME
  59. if cuda_home is None:
  60. return f"{RED} [FAIL] cannot find CUDA_HOME via torch.utils.cpp_extension.CUDA_HOME={torch.utils.cpp_extension.CUDA_HOME} {END}"
  61. try:
  62. output = subprocess.check_output([cuda_home + "/bin/nvcc", "-V"], universal_newlines=True)
  63. except FileNotFoundError:
  64. return f"{RED} [FAIL] nvcc missing {END}"
  65. output_split = output.split()
  66. release_idx = output_split.index("release")
  67. release = output_split[release_idx + 1].replace(',', '').split(".")
  68. return ".".join(release)
  69. def installed_cann_path():
  70. if "ASCEND_HOME_PATH" in os.environ or os.path.exists(os.environ["ASCEND_HOME_PATH"]):
  71. return os.environ["ASCEND_HOME_PATH"]
  72. return None
  73. def installed_cann_version():
  74. import re
  75. ascend_path = installed_cann_path()
  76. if ascend_path is None:
  77. return f"CANN_HOME does not exist, unable to compile NPU op(s)"
  78. cann_version = ""
  79. for dirpath, _, filenames in os.walk(os.path.realpath(ascend_path)):
  80. if cann_version:
  81. break
  82. install_files = [file for file in filenames if re.match(r"ascend_.*_install\.info", file)]
  83. if install_files:
  84. filepath = os.path.join(dirpath, install_files[0])
  85. with open(filepath, "r") as f:
  86. for line in f:
  87. if line.find("version") != -1:
  88. cann_version = line.strip().split("=")[-1]
  89. break
  90. return cann_version
  91. def get_shm_size():
  92. try:
  93. shm_stats = os.statvfs('/dev/shm')
  94. except (OSError, FileNotFoundError, ValueError):
  95. return "UNKNOWN", None
  96. shm_size = shm_stats.f_frsize * shm_stats.f_blocks
  97. shm_hbytes = human_readable_size(shm_size)
  98. warn = []
  99. if shm_size < 512 * 1024**2:
  100. warn.append(
  101. f" {YELLOW} [WARNING] /dev/shm size might be too small, if running in docker increase to at least --shm-size='1gb' {END}"
  102. )
  103. if get_accelerator().communication_backend_name() == "nccl":
  104. warn.append(
  105. f" {YELLOW} [WARNING] see more details about NCCL requirements: https://docs.nvidia.com/deeplearning/nccl/user-guide/docs/troubleshooting.html#sharing-data {END}"
  106. )
  107. return shm_hbytes, warn
  108. def human_readable_size(size):
  109. units = ['B', 'KB', 'MB', 'GB', 'TB']
  110. i = 0
  111. while size >= 1024 and i < len(units) - 1:
  112. size /= 1024
  113. i += 1
  114. return f'{size:.2f} {units[i]}'
  115. def debug_report():
  116. max_dots = 33
  117. report = [("torch install path", torch.__path__), ("torch version", torch.__version__),
  118. ("deepspeed install path", deepspeed.__path__),
  119. ("deepspeed info", f"{deepspeed.__version__}, {deepspeed.__git_hash__}, {deepspeed.__git_branch__}")]
  120. if get_accelerator().device_name() == 'cuda':
  121. hip_version = getattr(torch.version, "hip", None)
  122. report.extend([("torch cuda version", torch.version.cuda), ("torch hip version", hip_version),
  123. ("nvcc version", (None if hip_version else nvcc_version())),
  124. ("deepspeed wheel compiled w.", f"torch {torch_info['version']}, " +
  125. (f"hip {torch_info['hip_version']}" if hip_version else f"cuda {torch_info['cuda_version']}"))
  126. ])
  127. elif get_accelerator().device_name() == 'npu':
  128. import torch_npu
  129. report.extend([("deepspeed wheel compiled w.", f"torch {torch_info['version']}"),
  130. ("torch_npu install path", torch_npu.__path__), ("torch_npu version", torch_npu.__version__),
  131. ("ascend_cann version", installed_cann_version())])
  132. else:
  133. report.extend([("deepspeed wheel compiled w.", f"torch {torch_info['version']} ")])
  134. report.append(("shared memory (/dev/shm) size", get_shm_size()))
  135. print("DeepSpeed general environment info:")
  136. for name, value in report:
  137. warns = []
  138. if isinstance(value, tuple):
  139. value, warns = value
  140. print(name, "." * (max_dots - len(name)), value)
  141. if warns:
  142. for warn in warns:
  143. print(warn)
  144. def parse_arguments():
  145. parser = argparse.ArgumentParser()
  146. parser.add_argument('--hide_operator_status',
  147. action='store_true',
  148. help='Suppress display of installation and compatibility statuses of DeepSpeed operators. ')
  149. parser.add_argument('--hide_errors_and_warnings', action='store_true', help='Suppress warning and error messages.')
  150. args = parser.parse_args()
  151. return args
  152. def main(hide_operator_status=False, hide_errors_and_warnings=False):
  153. if not hide_operator_status:
  154. op_report(verbose=not hide_errors_and_warnings)
  155. debug_report()
  156. def cli_main():
  157. args = parse_arguments()
  158. main(hide_operator_status=args.hide_operator_status, hide_errors_and_warnings=args.hide_errors_and_warnings)
  159. if __name__ == "__main__":
  160. main()