summary_env.py 5.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# 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 platform
import subprocess
16 17 18
import sys

import distro
19 20 21 22 23 24

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

OS: {os_info}
G
gouzil 已提交
25 26 27 28
GCC version: {gcc_version}
Clang version: {clang_version}
CMake version: {cmake_version}
Libc version: {libc_version}
29 30 31 32 33
Python version: {python_version}

CUDA version: {cuda_version}
cuDNN version: {cudnn_version}
Nvidia driver version: {nvidia_driver_version}
G
gouzil 已提交
34
Nvidia driver List: {nvidia_gpu_driver}
35 36 37 38 39 40 41 42
"""

envs = {}


def get_paddle_info():
    try:
        import paddle
43

44 45 46
        envs['paddle_version'] = paddle.__version__
        envs['paddle_with_cuda'] = paddle.fluid.core.is_compiled_with_cuda()
    except:
G
gouzil 已提交
47 48
        envs['paddle_version'] = 'N/A'
        envs['paddle_with_cuda'] = 'N/A'
49 50 51 52


def get_os_info():
    if platform.system() == "Darwin":
G
gouzil 已提交
53 54
        plat = "macOS"
        ver = run_shell_command('sw_vers -productVersion').strip('\n')
55
    elif platform.system() == "Linux":
G
gouzil 已提交
56 57
        plat = distro.id()
        ver = distro.version()
58 59 60 61
    elif platform.system() == "Windows":
        plat = "Windows"
        ver = platform.win32_ver()[0]
    else:
G
gouzil 已提交
62 63
        plat = 'N/A'
        ver = 'N/A'
I
iducn 已提交
64
    envs['os_info'] = "{0} {1}".format(plat, ver)
65 66


G
gouzil 已提交
67 68 69 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 95 96 97 98 99 100 101 102 103 104
def get_gcc_version():
    try:
        envs['gcc_version'] = (
            run_shell_command("gcc --version").split('\n')[0].split("gcc ")[1]
        )
    except:
        envs['gcc_version'] = 'N/A'


def get_clang_version():
    try:
        envs['clang_version'] = (
            run_shell_command("clang --version")
            .split('\n')[0]
            .split("clang version ")[1]
        )
    except:
        envs['clang_version'] = 'N/A'


def get_cmake_version():
    try:
        envs['cmake_version'] = (
            run_shell_command("cmake --version")
            .split('\n')[0]
            .split("cmake ")[1]
        )
    except:
        envs['cmake_version'] = 'N/A'


def get_libc_version():
    if platform.system() == "Linux":
        envs['libc_version'] = ' '.join(platform.libc_ver())
    else:
        envs['libc_version'] = 'N/A'


105 106 107 108 109
def get_python_info():
    envs['python_version'] = sys.version.split(' ')[0]


def run_shell_command(cmd):
110 111 112
    out, err = subprocess.Popen(
        cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True
    ).communicate()
113 114 115 116 117 118 119 120 121 122 123
    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:
G
gouzil 已提交
124
        envs['cuda_version'] = 'N/A'
125 126 127 128 129 130 131 132


def get_cudnn_info():
    def _get_cudnn_ver(cmd):
        out = run_shell_command(cmd)
        if out:
            return out.split(' ')[-1].strip()
        else:
G
gouzil 已提交
133
            return 'N/A'
134 135 136 137

    if platform.system() == "Windows":
        cudnn_dll_path = run_shell_command('where cudnn*')
        if cudnn_dll_path:
138 139 140
            cudnn_header_path = (
                cudnn_dll_path.split('bin')[0] + r'include\cudnn.h'
            )
I
iducn 已提交
141
            cmd = 'type "{0}" | findstr "{1}" | findstr /v "CUDNN_VERSION"'
142
        else:
G
gouzil 已提交
143
            envs['cudnn_version'] = 'N/A'
144 145 146
            return
    else:
        cudnn_header_path = run_shell_command(
147
            'whereis "cudnn.h" | awk \'{print $2}\''
G
gouzil 已提交
148
        ).strip('\n')
149
        if cudnn_header_path:
I
iducn 已提交
150
            cmd = 'cat "{0}" | grep "{1}" | grep -v "CUDNN_VERSION"'
G
gouzil 已提交
151 152 153 154 155
            if _get_cudnn_ver(cmd.format(cudnn_header_path, 'CUDNN_MAJOR')):
                cudnn_header_path = run_shell_command(
                    'whereis "cudnn_version.h" | awk \'{print $2}\''
                ).strip('\n')

156
        else:
G
gouzil 已提交
157
            envs['cudnn_version'] = 'N/A'
158 159 160 161 162
            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(
163 164
        cmd.format(cudnn_header_path, 'CUDNN_PATCHLEVEL')
    )
165

G
gouzil 已提交
166 167 168 169
    if major != 'N/A':
        envs['cudnn_version'] = "{0}.{1}.{2}".format(major, minor, patch_level)
    else:
        envs['cudnn_version'] = 'N/A'
170 171 172 173 174


def get_driver_info():
    driver_ver = run_shell_command('nvidia-smi')
    if driver_ver:
175 176 177
        driver_ver = (
            driver_ver.split('Driver Version:')[1].strip().split(' ')[0]
        )
178
    else:
G
gouzil 已提交
179
        driver_ver = 'N/A'
180 181 182
    envs['nvidia_driver_version'] = driver_ver


G
gouzil 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
def get_nvidia_gpu_driver():
    if platform.system() != "Windows" and platform.system() != "Linux":
        envs['nvidia_gpu_driver'] = 'N/A'
        return
    try:
        nvidia_smi = 'nvidia-smi'
        gpu_list = run_shell_command(nvidia_smi + " -L")
        result = "\n"
        for gpu_info in gpu_list.split("\n"):
            result += gpu_info.split(" (UUID:")[0] + "\n"
        envs['nvidia_gpu_driver'] = result
    except:
        envs['nvidia_gpu_driver'] = 'N/A'


198 199 200
def main():
    get_paddle_info()
    get_os_info()
G
gouzil 已提交
201 202 203 204
    get_gcc_version()
    get_clang_version()
    get_cmake_version()
    get_libc_version()
205 206 207 208
    get_python_info()
    get_cuda_info()
    get_cudnn_info()
    get_driver_info()
G
gouzil 已提交
209
    get_nvidia_gpu_driver()
I
iducn 已提交
210
    print('*' * 40 + envs_template.format(**envs) + '*' * 40)
211 212 213 214


if __name__ == '__main__':
    main()