install_check.py 6.8 KB
Newer Older
J
Jiabin Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2019 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.

15
import os
16
import paddle
17 18 19 20 21 22 23
from .framework import (
    Program,
    program_guard,
    unique_name,
    cuda_places,
    cpu_places,
)
J
Jiabin Yang 已提交
24 25 26 27
from .param_attr import ParamAttr
from .initializer import Constant
from . import layers
from . import backward
L
lujun 已提交
28
from .dygraph import Layer, nn
J
Jiabin Yang 已提交
29
from . import executor
30
from . import optimizer
J
Jiabin Yang 已提交
31
from . import core
32 33
from . import compiler
import logging
J
Jiabin Yang 已提交
34 35 36 37 38 39
import numpy as np

__all__ = ['run_check']


class SimpleLayer(Layer):
40
    def __init__(self, input_size):
41
        super().__init__()
42 43 44 45
        self._linear1 = paddle.nn.Linear(
            input_size,
            3,
            weight_attr=ParamAttr(initializer=Constant(value=0.1)),
46
        )
J
Jiabin Yang 已提交
47 48

    def forward(self, inputs):
49
        x = self._linear1(inputs)
50
        x = paddle.sum(x)
J
Jiabin Yang 已提交
51 52 53 54
        return x


def run_check():
55
    """To check whether install is successful
J
Jiabin Yang 已提交
56
    This func should not be called only if you need to verify installation
57 58

    Examples:
59
        .. code-block:: python
60 61 62 63 64

            import paddle.fluid as fluid
            fluid.install_check.run_check()

            # If installed successfully, output may be
65
            # Running Verify Fluid Program ...
66 67 68 69 70 71
            # W0805 04:24:59.496919 35357 device_context.cc:268] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 10.2, Runtime API Version: 10.1
            # W0805 04:24:59.505594 35357 device_context.cc:276] device: 0, cuDNN Version: 7.6.
            # Your Paddle Fluid works well on SINGLE GPU or CPU.
            # Your Paddle Fluid works well on MUTIPLE GPU or CPU.
            # Your Paddle Fluid is installed successfully! Let's start deep Learning with Paddle Fluid now
    """
P
pangyoki 已提交
72 73
    paddle.enable_static()

J
Jiabin Yang 已提交
74
    print("Running Verify Fluid Program ... ")
75

76 77 78 79 80 81 82
    device_list = []
    if core.is_compiled_with_cuda():
        try:
            core.get_cuda_device_count()
        except Exception as e:
            logging.warning(
                "You are using GPU version Paddle Fluid, But Your CUDA Device is not set properly"
83 84
                "\n Original Error is {}".format(e)
            )
85 86 87 88 89 90 91 92 93 94 95
            return 0
        device_list = cuda_places()
    else:
        device_list = [core.CPUPlace(), core.CPUPlace()]

    np_inp_single = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32)
    inp = []
    for i in range(len(device_list)):
        inp.append(np_inp_single)
    np_inp_muti = np.array(inp)
    np_inp_muti = np_inp_muti.reshape(len(device_list), 2, 2)
96 97 98 99 100 101 102 103 104 105

    def test_parallerl_exe():
        train_prog = Program()
        startup_prog = Program()
        scope = core.Scope()
        with executor.scope_guard(scope):
            with program_guard(train_prog, startup_prog):
                with unique_name.guard():
                    build_strategy = compiler.BuildStrategy()
                    build_strategy.enable_inplace = True
106
                    inp = layers.data(name="inp", shape=[2, 2])
107
                    simple_layer = SimpleLayer(input_size=2)
108
                    out = simple_layer(inp)
109
                    exe = executor.Executor(
110 111 112 113 114
                        core.CUDAPlace(0)
                        if core.is_compiled_with_cuda()
                        and (core.get_cuda_device_count() > 0)
                        else core.CPUPlace()
                    )
115
                    loss = paddle.mean(out)
116 117 118 119
                    loss.persistable = True
                    optimizer.SGD(learning_rate=0.01).minimize(loss)
                    startup_prog.random_seed = 1
                    compiled_prog = compiler.CompiledProgram(
120 121 122 123 124 125
                        train_prog
                    ).with_data_parallel(
                        build_strategy=build_strategy,
                        loss_name=loss.name,
                        places=device_list,
                    )
126 127
                    exe.run(startup_prog)

128 129 130 131 132
                    exe.run(
                        compiled_prog,
                        feed={inp.name: np_inp_muti},
                        fetch_list=[loss.name],
                    )
133 134 135 136 137 138 139 140

    def test_simple_exe():
        train_prog = Program()
        startup_prog = Program()
        scope = core.Scope()
        with executor.scope_guard(scope):
            with program_guard(train_prog, startup_prog):
                with unique_name.guard():
141 142 143
                    inp0 = layers.data(
                        name="inp", shape=[2, 2], append_batch_size=False
                    )
144
                    simple_layer0 = SimpleLayer(input_size=2)
145 146
                    out0 = simple_layer0(inp0)
                    param_grads = backward.append_backward(
147
                        out0,
148 149
                        parameter_list=[simple_layer0._linear1.weight.name],
                    )[0]
150
                    exe0 = executor.Executor(
151 152 153 154 155
                        core.CUDAPlace(0)
                        if core.is_compiled_with_cuda()
                        and (core.get_cuda_device_count() > 0)
                        else core.CPUPlace()
                    )
156
                    exe0.run(startup_prog)
157 158 159 160
                    exe0.run(
                        feed={inp0.name: np_inp_single},
                        fetch_list=[out0.name, param_grads[1].name],
                    )
161 162 163 164 165 166 167 168 169 170 171 172 173

    test_simple_exe()

    print("Your Paddle Fluid works well on SINGLE GPU or CPU.")
    try:
        test_parallerl_exe()
        print("Your Paddle Fluid works well on MUTIPLE GPU or CPU.")
        print(
            "Your Paddle Fluid is installed successfully! Let's start deep Learning with Paddle Fluid now"
        )
    except Exception as e:
        logging.warning(
            "Your Paddle Fluid has some problem with multiple GPU. This may be caused by:"
174
            "\n 1. There is only 1 or 0 GPU visible on your Device;"
175 176 177 178
            "\n 2. No.1 or No.2 GPU or both of them are occupied now"
            "\n 3. Wrong installation of NVIDIA-NCCL2, please follow instruction on https://github.com/NVIDIA/nccl-tests "
            "\n to test your NCCL, or reinstall it following https://docs.nvidia.com/deeplearning/sdk/nccl-install-guide/index.html"
        )
J
Jiabin Yang 已提交
179

180 181 182
        print("\n Original Error is: {}".format(e))
        print(
            "Your Paddle Fluid is installed successfully ONLY for SINGLE GPU or CPU! "
183 184
            "\n Let's start deep Learning with Paddle Fluid now"
        )
P
pangyoki 已提交
185 186

    paddle.disable_static()