summary_env.py 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2020 PaddlePaddle Authors. 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 sys
15
import distro
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
import platform
import subprocess

envs_template = """
Paddle version: {paddle_version}
Paddle With CUDA: {paddle_with_cuda}

OS: {os_info}
Python version: {python_version}

CUDA version: {cuda_version}
cuDNN version: {cudnn_version}
Nvidia driver version: {nvidia_driver_version}
"""

envs = {}


def get_paddle_info():
    try:
        import paddle
37

38 39 40 41 42 43 44 45 46 47 48 49 50
        envs['paddle_version'] = paddle.__version__
        envs['paddle_with_cuda'] = paddle.fluid.core.is_compiled_with_cuda()
    except:
        envs['paddle_version'] = None
        envs['paddle_with_cuda'] = None


def get_os_info():
    plat = platform.system()
    if platform.system() == "Darwin":
        plat = "macOs"
        ver = platform.mac_ver()[0]
    elif platform.system() == "Linux":
51 52
        plat = distro.linux_distribution()[0]
        ver = distro.linux_distribution()[1]
53 54 55 56 57 58
    elif platform.system() == "Windows":
        plat = "Windows"
        ver = platform.win32_ver()[0]
    else:
        plat = None
        ver = None
I
iducn 已提交
59
    envs['os_info'] = "{0} {1}".format(plat, ver)
60 61 62 63 64 65 66


def get_python_info():
    envs['python_version'] = sys.version.split(' ')[0]


def run_shell_command(cmd):
67 68 69
    out, err = subprocess.Popen(
        cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True
    ).communicate()
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    if err:
        return None
    else:
        return out.decode('utf-8')


def get_cuda_info():
    out = run_shell_command('nvcc --version')
    if out:
        envs['cuda_version'] = out.split('V')[-1].strip()
    else:
        envs['cuda_version'] = None


def get_cudnn_info():
    def _get_cudnn_ver(cmd):
        out = run_shell_command(cmd)
        if out:
            return out.split(' ')[-1].strip()
        else:
            return None

    if platform.system() == "Windows":
        cudnn_dll_path = run_shell_command('where cudnn*')
        if cudnn_dll_path:
95 96 97
            cudnn_header_path = (
                cudnn_dll_path.split('bin')[0] + r'include\cudnn.h'
            )
I
iducn 已提交
98
            cmd = 'type "{0}" | findstr "{1}" | findstr /v "CUDNN_VERSION"'
99 100 101 102 103
        else:
            envs['cudnn_version'] = None
            return
    else:
        cudnn_header_path = run_shell_command(
104 105
            'whereis "cudnn.h" | awk \'{print $2}\''
        )
106 107
        if cudnn_header_path:
            cudnn_header_path = cudnn_header_path.strip()
I
iducn 已提交
108
            cmd = 'cat "{0}" | grep "{1}" | grep -v "CUDNN_VERSION"'
109 110 111 112 113 114 115
        else:
            envs['cudnn_version'] = None
            return

    major = _get_cudnn_ver(cmd.format(cudnn_header_path, 'CUDNN_MAJOR'))
    minor = _get_cudnn_ver(cmd.format(cudnn_header_path, 'CUDNN_MINOR'))
    patch_level = _get_cudnn_ver(
116 117
        cmd.format(cudnn_header_path, 'CUDNN_PATCHLEVEL')
    )
118

I
iducn 已提交
119
    envs['cudnn_version'] = "{0}.{1}.{2}".format(major, minor, patch_level)
120 121 122 123 124


def get_driver_info():
    driver_ver = run_shell_command('nvidia-smi')
    if driver_ver:
125 126 127
        driver_ver = (
            driver_ver.split('Driver Version:')[1].strip().split(' ')[0]
        )
128 129 130 131 132 133 134 135 136 137 138 139
    else:
        driver_ver = None
    envs['nvidia_driver_version'] = driver_ver


def main():
    get_paddle_info()
    get_os_info()
    get_python_info()
    get_cuda_info()
    get_cudnn_info()
    get_driver_info()
I
iducn 已提交
140
    print('*' * 40 + envs_template.format(**envs) + '*' * 40)
141 142 143 144


if __name__ == '__main__':
    main()