check_nan_inf_base.py 3.6 KB
Newer Older
W
WangXi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# 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.

import os
16

W
WangXi 已提交
17 18 19 20 21 22 23
import numpy as np

os.environ[str("FLAGS_check_nan_inf")] = str("1")
os.environ[str("GLOG_vmodule")] = str("nan_inf_utils_detail=10")

import paddle
import paddle.fluid as fluid
24
import paddle.fluid.core as core
W
WangXi 已提交
25

P
pangyoki 已提交
26 27
paddle.enable_static()

W
WangXi 已提交
28 29 30 31 32 33
np.random.seed(0)


def generator():
    batch_size = 5
    for i in range(5):
34 35 36
        curr_train_x = np.random.randint(
            batch_size, size=(batch_size, 3)
        ).astype("float32")
W
WangXi 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
        if i >= 2:
            curr_train_x[0, :] = np.nan
            curr_train_x[-1, :] = np.inf
        res = []
        for i in range(batch_size):
            y = i % 3
            res.append([y])
        y_label = np.array(res).astype('int64')
        yield [curr_train_x, y_label]


def net():
    x = fluid.layers.data(name="x", shape=[3], dtype='float32')
    y = fluid.layers.data(name="y", shape=[1], dtype='int64')

    # test int64 value
    zero = fluid.layers.fill_constant(shape=[1], dtype='int64', value=0)

    # test float16 value
    fp16_zero = fluid.layers.cast(zero, dtype='float16')

    y = y + zero

    hidden = x

    for i in range(2):
        hidden = fluid.layers.fc(input=hidden, size=400, act="sigmoid")

    hidden = fluid.layers.fc(input=hidden, size=3, act=None)
66
    cost, y_predict = paddle.nn.functional.softmax_with_cross_entropy(
67 68
        hidden, y, return_softmax=True
    )
69
    acc_top1 = paddle.static.accuracy(input=y_predict, label=y, k=1)
70
    avg_cost = paddle.mean(cost)
W
WangXi 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

    sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.05)
    sgd_optimizer.minimize(avg_cost)
    return y_predict, avg_cost, acc_top1


def check(use_cuda):
    main = fluid.Program()
    startup = fluid.Program()
    scope = fluid.core.Scope()

    with fluid.scope_guard(scope):
        with fluid.program_guard(main, startup):
            y_predict, avg_cost, acc_top1 = net()

            place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
            exe = fluid.Executor(place)
            exe.run(startup)

            step = 0.0
            for train_data, y_label in generator():
                outs = exe.run(
                    main,
94 95 96
                    feed={'x': train_data, 'y': y_label},
                    fetch_list=[y_predict.name, avg_cost.name, acc_top1.name],
                )
W
WangXi 已提交
97
                step += 1
98 99 100 101 102
                print(
                    'iter={:.0f},cost={},acc1={}'.format(
                        step, outs[1][0], outs[2][0]
                    )
                )
W
WangXi 已提交
103 104 105


if __name__ == '__main__':
106 107 108 109 110 111 112 113
    try:
        check(use_cuda=False)
        assert False
    except Exception as e:
        print(e)
        print(type(e))
        assert type(e) == RuntimeError

W
WangXi 已提交
114 115 116 117 118 119
    if core.is_compiled_with_cuda():
        try:
            check(use_cuda=True)
            assert False
        except Exception as e:
            print(e)
120 121 122
            print(type(e))
            # Note. Enforce in cuda kernel may not catch in paddle, and
            # Exception type will be RuntimeError
123
            assert type(e) == OSError or type(e) == RuntimeError