test_parallel_op.py 1.3 KB
Newer Older
Y
Yang Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import unittest

import paddle.v2.fluid.layers as layers
import paddle.v2.fluid as fluid
from paddle.v2.fluid.framework import Program
from paddle.v2.fluid.executor import Executor
from paddle.v2.fluid.backward import append_backward_ops
import numpy as np
import paddle.v2.fluid.core as core


class ParallelOpTest(unittest.TestCase):
    def setUp(self):
        x = layers.data(
Y
Yang Yang 已提交
15 16 17 18 19
            shape=[-1, 3, 4],
            dtype='float32',
            name='x',
            append_batch_size=False,
            stop_gradient=False)
Y
Yang Yang 已提交
20 21 22 23 24 25 26 27 28

        places = fluid.default_main_program().global_block().create_var()
        pd = layers.ParallelDo(places=places)

        with pd.do():
            data = pd.read_input(x)
            hidden = layers.fc(input=data, size=7)
            pd.write_output(hidden)
        data = pd()
Y
Yang Yang 已提交
29 30 31 32 33 34 35 36 37 38
        loss = layers.mean(x=data)
        append_backward_ops(loss)

        exe = fluid.Executor(fluid.CPUPlace())
        exe.run(fluid.default_startup_program())
        exe.run(fluid.default_main_program(),
                feed={
                    x.name: np.random.uniform(0.1, 0.6,
                                              (2, 3, 4)).astype("float32")
                })
Y
Yang Yang 已提交
39 40 41 42 43 44 45

    def test_forward(self):
        pass


if __name__ == '__main__':
    unittest.main()