install_check.py 5.7 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
from .framework import Program, program_guard, unique_name, default_startup_program
J
Jiabin Yang 已提交
16 17 18 19
from .param_attr import ParamAttr
from .initializer import Constant
from . import layers
from . import backward
L
lujun 已提交
20
from .dygraph import Layer, nn
J
Jiabin Yang 已提交
21
from . import executor
22
from . import optimizer
J
Jiabin Yang 已提交
23
from . import core
24 25
from . import compiler
import logging
J
Jiabin Yang 已提交
26
import numpy as np
27
import os
J
Jiabin Yang 已提交
28 29 30 31 32 33 34 35 36

__all__ = ['run_check']


class SimpleLayer(Layer):
    def __init__(self, name_scope):
        super(SimpleLayer, self).__init__(name_scope)
        self._fc1 = nn.FC(self.full_name(),
                          3,
37
                          param_attr=ParamAttr(initializer=Constant(value=0.1)))
J
Jiabin Yang 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50

    def forward(self, inputs):
        x = self._fc1(inputs)
        x = layers.reduce_sum(x)
        return x


def run_check():
    ''' intall check to verify if install is success

    This func should not be called only if you need to verify installation
    '''
    print("Running Verify Fluid Program ... ")
51 52 53
    use_cuda = False if not core.is_compiled_with_cuda() else True
    place = core.CPUPlace() if not core.is_compiled_with_cuda(
    ) else core.CUDAPlace(0)
54 55 56 57 58 59 60
    np_inp = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32)

    if use_cuda:
        if core.get_cuda_device_count() > 1:
            os.environ['CUDA_VISIBLE_DEVICES'] = "0,1"
        else:
            os.environ['CUDA_VISIBLE_DEVICES'] = "0"
61 62 63 64 65 66 67 68 69 70 71 72 73 74

    def test_parallerl_exe():
        train_prog = Program()
        startup_prog = Program()
        scope = core.Scope()
        if not use_cuda:
            os.environ['CPU_NUM'] = "2"
        with executor.scope_guard(scope):
            with program_guard(train_prog, startup_prog):
                with unique_name.guard():
                    places = []
                    build_strategy = compiler.BuildStrategy()
                    build_strategy.enable_inplace = True
                    build_strategy.memory_optimize = True
75 76
                    inp = layers.data(
                        name="inp", shape=[2, 2], append_batch_size=False)
77 78 79 80
                    simple_layer = SimpleLayer("simple_layer")
                    out = simple_layer(inp)
                    exe = executor.Executor(place)
                    if use_cuda:
81
                        places = [core.CUDAPlace(0), core.CUDAPlace(1)]
82 83 84 85 86 87 88 89 90 91 92 93 94 95
                    else:
                        places = [core.CPUPlace(), core.CPUPlace()]
                    loss = layers.mean(out)
                    loss.persistable = True
                    optimizer.SGD(learning_rate=0.01).minimize(loss)
                    startup_prog.random_seed = 1
                    compiled_prog = compiler.CompiledProgram(
                        train_prog).with_data_parallel(
                            build_strategy=build_strategy,
                            loss_name=loss.name,
                            places=places)
                    exe.run(startup_prog)

                    exe.run(compiled_prog,
96
                            feed={inp.name: np_inp},
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
                            fetch_list=[loss.name])

    def test_simple_exe():
        train_prog = Program()
        startup_prog = Program()
        scope = core.Scope()
        if not use_cuda:
            os.environ['CPU_NUM'] = "1"
        with executor.scope_guard(scope):
            with program_guard(train_prog, startup_prog):
                with unique_name.guard():
                    inp0 = layers.data(
                        name="inp", shape=[2, 2], append_batch_size=False)
                    simple_layer0 = SimpleLayer("simple_layer")
                    out0 = simple_layer0(inp0)
                    param_grads = backward.append_backward(
                        out0, parameter_list=[simple_layer0._fc1._w.name])[0]
                    exe0 = executor.Executor(core.CPUPlace()
                                             if not core.is_compiled_with_cuda()
                                             else core.CUDAPlace(0))
                    exe0.run(startup_prog)
118
                    exe0.run(feed={inp0.name: np_inp},
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
                             fetch_list=[out0.name, param_grads[1].name])

    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:"
            "\n 1. There is only 1 GPU visible on your Device;"
            "\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 已提交
138

139 140 141
        print("\n Original Error is: {}".format(e))
        print(
            "Your Paddle Fluid is installed successfully ONLY for SINGLE GPU or CPU! "
142
            "\n Let's start deep Learning with Paddle Fluid now!")