sys_env.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import glob
  15. import os
  16. import platform
  17. import subprocess
  18. import sys
  19. import cv2
  20. import paddle
  21. IS_WINDOWS = sys.platform == 'win32'
  22. def _find_cuda_home():
  23. '''Finds the CUDA install path. It refers to the implementation of
  24. pytorch <https://github.com/pytorch/pytorch/blob/master/torch/utils/cpp_extension.py>.
  25. '''
  26. # Guess #1
  27. cuda_home = os.environ.get('CUDA_HOME') or os.environ.get('CUDA_PATH')
  28. if cuda_home is None:
  29. # Guess #2
  30. try:
  31. which = 'where' if IS_WINDOWS else 'which'
  32. nvcc = subprocess.check_output([which,
  33. 'nvcc']).decode().rstrip('\r\n')
  34. cuda_home = os.path.dirname(os.path.dirname(nvcc))
  35. except Exception:
  36. # Guess #3
  37. if IS_WINDOWS:
  38. cuda_homes = glob.glob(
  39. 'C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v*.*')
  40. if len(cuda_homes) == 0:
  41. cuda_home = ''
  42. else:
  43. cuda_home = cuda_homes[0]
  44. else:
  45. cuda_home = '/usr/local/cuda'
  46. if not os.path.exists(cuda_home):
  47. cuda_home = None
  48. return cuda_home
  49. def _get_nvcc_info(cuda_home):
  50. if cuda_home is not None and os.path.isdir(cuda_home):
  51. try:
  52. nvcc = os.path.join(cuda_home, 'bin/nvcc')
  53. nvcc = subprocess.check_output(
  54. "{} -V".format(nvcc), shell=True).decode()
  55. nvcc = nvcc.strip().split('\n')[-1]
  56. except subprocess.SubprocessError:
  57. nvcc = "Not Available"
  58. else:
  59. nvcc = "Not Available"
  60. return nvcc
  61. def _get_gpu_info():
  62. try:
  63. gpu_info = subprocess.check_output(['nvidia-smi',
  64. '-L']).decode().strip()
  65. gpu_info = gpu_info.split('\n')
  66. for i in range(len(gpu_info)):
  67. gpu_info[i] = ' '.join(gpu_info[i].split(' ')[:4])
  68. except:
  69. gpu_info = ' Can not get GPU information. Please make sure CUDA have been installed successfully.'
  70. return gpu_info
  71. def get_sys_env():
  72. """collect environment information"""
  73. env_info = {}
  74. env_info['platform'] = platform.platform()
  75. env_info['Python'] = sys.version.replace('\n', '')
  76. # TODO is_compiled_with_cuda() has not been moved
  77. compiled_with_cuda = paddle.is_compiled_with_cuda()
  78. env_info['Paddle compiled with cuda'] = compiled_with_cuda
  79. if compiled_with_cuda:
  80. cuda_home = _find_cuda_home()
  81. env_info['NVCC'] = _get_nvcc_info(cuda_home)
  82. # refer to https://github.com/PaddlePaddle/Paddle/blob/release/2.0-rc/paddle/fluid/platform/device_context.cc#L327
  83. v = paddle.get_cudnn_version()
  84. v = str(v // 1000) + '.' + str(v % 1000 // 100)
  85. env_info['cudnn'] = v
  86. if 'gpu' in paddle.get_device():
  87. gpu_nums = paddle.distributed.ParallelEnv().nranks
  88. else:
  89. gpu_nums = 0
  90. env_info['GPUs used'] = gpu_nums
  91. env_info['CUDA_VISIBLE_DEVICES'] = os.environ.get(
  92. 'CUDA_VISIBLE_DEVICES')
  93. if gpu_nums == 0:
  94. os.environ['CUDA_VISIBLE_DEVICES'] = ''
  95. env_info['GPU'] = _get_gpu_info()
  96. try:
  97. gcc = subprocess.check_output(['gcc', '--version']).decode()
  98. gcc = gcc.strip().split('\n')[0]
  99. env_info['GCC'] = gcc
  100. except:
  101. pass
  102. env_info['PaddlePaddle'] = paddle.__version__
  103. env_info['OpenCV'] = cv2.__version__
  104. return env_info