test_py_func_op.py 7.2 KB
Newer Older
S
sneaxiy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2018 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.

S
sneaxiy 已提交
15
import os
S
sneaxiy 已提交
16
import unittest
17

S
sneaxiy 已提交
18 19
import numpy as np

20 21 22 23
import paddle
import paddle.fluid as fluid
from paddle.fluid import compiler

S
sneaxiy 已提交
24 25 26 27 28
dev_cnt = 2
if fluid.core.is_compiled_with_cuda():
    dev_cnt = fluid.core.get_cuda_device_count()
os.environ['CPU_NUM'] = str(dev_cnt)

S
sneaxiy 已提交
29

S
sneaxiy 已提交
30
def dummy_func_with_no_input():
S
sneaxiy 已提交
31
    return np.array([0], dtype='float32')
S
sneaxiy 已提交
32 33 34 35 36 37


def dummy_func_with_no_output(x):
    pass


38 39 40 41
def dummy_func_with_multi_input_output(x, y):
    return np.array(x), np.array(y)


S
sneaxiy 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55
def tanh(x):
    return np.tanh(x)


def tanh_grad(y, dy):
    return np.array(dy) * (1 - np.square(np.array(y)))


def cross_entropy(logits, labels):
    logits = np.array(logits)
    labels = np.array(labels)
    M = logits.shape[0]
    N = logits.shape[1]
    ret = np.ndarray([M, 1]).astype(logits.dtype)
56
    for idx in range(M):
S
sneaxiy 已提交
57 58 59 60 61 62 63 64 65 66 67
        ret[idx][0] = -np.log(logits[idx][labels[idx][0]])
    return ret


def cross_entropy_grad(logits, labels, bwd_dout):
    logits = np.array(logits)
    labels = np.array(labels)
    bwd_dout = np.array(bwd_dout)
    M = logits.shape[0]
    N = logits.shape[1]
    dlogits = np.zeros([M, N]).astype(logits.dtype)
68
    for idx in range(M):
69 70 71
        dlogits[idx][labels[idx][0]] = (
            -bwd_dout[idx] / logits[idx][labels[idx][0]]
        )
S
sneaxiy 已提交
72 73 74 75 76 77
    return dlogits, None


def simple_fc_net(img, label, use_py_func_op):
    hidden = img
    for idx in range(4):
C
Charles-hit 已提交
78
        hidden = paddle.static.nn.fc(
S
sneaxiy 已提交
79 80
            hidden,
            size=200,
81
            bias_attr=fluid.ParamAttr(
82
                initializer=paddle.nn.initializer.Constant(value=1.0)
83 84
            ),
        )
S
sneaxiy 已提交
85
        if not use_py_func_op:
86
            hidden = paddle.tanh(hidden)
S
sneaxiy 已提交
87
        else:
88 89 90 91 92 93 94 95 96
            new_hidden = (
                fluid.default_main_program()
                .current_block()
                .create_var(
                    name='hidden_{}'.format(idx),
                    dtype='float32',
                    shape=hidden.shape,
                )
            )
97
            hidden = paddle.static.py_func(
98 99 100 101 102 103
                func=tanh,
                x=hidden,
                out=new_hidden,
                backward_func=tanh_grad,
                skip_vars_in_backward_input=hidden,
            )
S
sneaxiy 已提交
104

C
Charles-hit 已提交
105
    prediction = paddle.static.nn.fc(hidden, size=10, activation='softmax')
S
sneaxiy 已提交
106
    if not use_py_func_op:
107 108 109
        loss = paddle.nn.functional.cross_entropy(
            input=prediction, label=label, reduction='none', use_softmax=False
        )
S
sneaxiy 已提交
110
    else:
111 112 113 114 115
        loss = (
            fluid.default_main_program()
            .current_block()
            .create_var(name='loss', dtype='float32', shape=[-1, 1])
        )
116
        loss = paddle.static.py_func(
117 118 119 120 121 122 123 124 125 126 127 128
            func=cross_entropy,
            x=[prediction, label],
            out=loss,
            backward_func=cross_entropy_grad,
            skip_vars_in_backward_input=loss,
        )

        dummy_var = (
            fluid.default_main_program()
            .current_block()
            .create_var(name='test_tmp_var', dtype='float32', shape=[1])
        )
129
        paddle.static.py_func(
130 131
            func=dummy_func_with_no_input, x=None, out=dummy_var
        )
S
sneaxiy 已提交
132
        loss += dummy_var
133
        paddle.static.py_func(func=dummy_func_with_no_output, x=loss, out=None)
S
sneaxiy 已提交
134

135 136 137 138 139 140 141 142 143 144
        loss_out = (
            fluid.default_main_program()
            .current_block()
            .create_var(dtype='float32', shape=[-1, 1])
        )
        dummy_var_out = (
            fluid.default_main_program()
            .current_block()
            .create_var(dtype='float32', shape=[1])
        )
145
        paddle.static.py_func(
146 147 148 149 150 151 152 153
            func=dummy_func_with_multi_input_output,
            x=(loss, dummy_var),
            out=(loss_out, dummy_var_out),
        )
        assert (
            loss == loss_out and dummy_var == dummy_var_out
        ), "py_func failed with multi input and output"

154
        paddle.static.py_func(
155 156 157 158 159 160 161
            func=dummy_func_with_multi_input_output,
            x=[loss, dummy_var],
            out=[loss_out, dummy_var_out],
        )
        assert (
            loss == loss_out and dummy_var == dummy_var_out
        ), "py_func failed with multi input and output"
162

163
    loss = paddle.mean(loss)
S
sneaxiy 已提交
164 165 166 167
    return loss


def reader():
168
    for _ in range(dev_cnt * 100):
169 170 171
        yield np.random.random([784]), np.random.random_integers(
            size=[1], low=0, high=9
        )
S
sneaxiy 已提交
172 173


S
sneaxiy 已提交
174
def test_main(use_cuda, use_py_func_op, use_parallel_executor):
S
sneaxiy 已提交
175 176 177 178 179
    if use_cuda and not fluid.core.is_compiled_with_cuda():
        return None

    with fluid.program_guard(fluid.Program(), fluid.Program()):
        with fluid.scope_guard(fluid.core.Scope()):
C
cnn 已提交
180
            gen = paddle.seed(1)
S
sneaxiy 已提交
181
            np.random.seed(1)
G
GGBond8488 已提交
182 183 184 185 186 187
            img = paddle.static.data(
                name='image', shape=[-1, 784], dtype='float32'
            )
            label = paddle.static.data(
                name='label', shape=[-1, 1], dtype='int64'
            )
S
sneaxiy 已提交
188 189 190 191 192 193 194 195 196 197
            loss = simple_fc_net(img, label, use_py_func_op)
            optimizer = fluid.optimizer.SGD(learning_rate=1e-3)
            optimizer.minimize(loss)

            place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
            feeder = fluid.DataFeeder(feed_list=[img, label], place=place)
            r = paddle.batch(reader, batch_size=10)

            exe = fluid.Executor(place)
            exe.run(fluid.default_startup_program())
198

C
chengduo 已提交
199 200
            train_cp = fluid.default_main_program()

S
sneaxiy 已提交
201
            if use_parallel_executor:
202
                train_cp = compiler.CompiledProgram(
203 204
                    fluid.default_main_program()
                )
S
sneaxiy 已提交
205 206 207 208
                fetch_list = [loss.name]
            else:
                fetch_list = [loss]

S
sneaxiy 已提交
209
            ret = []
210
            for epoch_id in range(2):
S
sneaxiy 已提交
211
                for d in r():
212 213 214
                    (L,) = exe.run(
                        train_cp, feed=feeder.feed(d), fetch_list=fetch_list
                    )
S
sneaxiy 已提交
215
                    ret.append(L)
S
sneaxiy 已提交
216 217 218
            return np.array(ret)


S
sneaxiy 已提交
219 220 221 222
class TestPyFuncOpUseExecutor(unittest.TestCase):
    def setUp(self):
        self.use_parallel_executor = False

S
sneaxiy 已提交
223 224
    def test_loss_diff(self):
        for use_cuda in [True, False]:
L
Leo Chen 已提交
225
            losses = []
S
sneaxiy 已提交
226
            for use_py_func_op in [True, False]:
227 228 229
                L = test_main(
                    use_cuda, use_py_func_op, self.use_parallel_executor
                )
S
sneaxiy 已提交
230 231 232
                if L is not None:
                    losses.append(L)

233
                for idx in range(len(losses) - 1):
L
Leo Chen 已提交
234 235
                    max_diff = np.max(np.abs(losses[idx] - losses[0]))
                    self.assertAlmostEqual(max_diff, 0, delta=1e-3)
S
sneaxiy 已提交
236 237


S
sneaxiy 已提交
238
class TestPyFuncOpUseParallelExecutor(TestPyFuncOpUseExecutor):
S
sneaxiy 已提交
239 240 241 242
    def setUp(self):
        self.use_parallel_executor = True


S
sneaxiy 已提交
243 244
if __name__ == '__main__':
    unittest.main()