test_program.py 2.9 KB
Newer Older
Y
Yu Yang 已提交
1
import unittest
2 3

import paddle.v2.framework.core as core
Q
Qiao Longfei 已提交
4
from paddle.v2.framework.framework import Program
5
from paddle.v2.framework.framework import g_program
Y
Yu Yang 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36


class TestProgram(unittest.TestCase):
    def test_program(self):
        b = g_program.current_block()
        self.assertEqual(-1, b.parent_idx)
        self.assertEqual(0, b.idx)

        b = g_program.create_block()
        self.assertEqual(1, b.idx)
        self.assertEqual(0, b.parent_idx)

        b = g_program.create_block()
        self.assertEqual(2, b.idx)
        self.assertEqual(1, b.parent_idx)

        g_program.rollback()

        b = g_program.current_block()
        self.assertEqual(1, b.idx)
        self.assertEqual(0, b.parent_idx)

        b = g_program.create_block()
        self.assertEqual(3, b.idx)
        self.assertEqual(1, b.parent_idx)

        g_program.rollback()
        b = g_program.current_block()
        self.assertEqual(1, b.idx)
        self.assertEqual(0, b.parent_idx)

Q
Qiao Longfei 已提交
37
    def test_desc_append_backward(self):
38 39 40 41 42
        prog = core.ProgramDesc.__create_program_desc__()
        self.assertIsNotNone(prog)
        block = prog.block(0)
        self.assertIsNotNone(block)

43 44 45 46 47 48
        mul_op_desc = block.append_op()
        mul_op_desc.set_type("mul")
        mul_op_desc.set_input("X", ["x1"])
        mul_op_desc.set_input("Y", ["y1"])
        mul_op_desc.set_output("Out", ["out1"])

49
        sum_op_desc = block.append_op()
50 51 52 53
        sum_op_desc.set_type("elementwise_add")
        sum_op_desc.set_input("X", ["out1"])
        sum_op_desc.set_input("Y", ["b1"])
        sum_op_desc.set_output("Out", ["out2"])
54

D
Dong Zhihong 已提交
55
        target = block.var("out2")
56

57
        expect_ops = [
58 59
            "mul", "elementwise_add", "fill_constant", "elementwise_add_grad",
            "mul_grad"
60
        ]
Q
qiaolongfei 已提交
61 62 63 64

        def grad_name(name):
            return name + "@GRAD"

65
        actual_ops = []
Q
qiaolongfei 已提交
66 67 68 69 70
        param_to_grad = prog.append_backward(target, set())
        for var_name in ("x1", "y1", "out1", "b1"):
            self.assertEqual(param_to_grad[var_name][0], grad_name(var_name))
            self.assertEqual(param_to_grad[var_name][1], 0)

71 72 73
        for op in block.all_ops():
            actual_ops.append(op.type())
        self.assertEqual(actual_ops, expect_ops)
74

Q
Qiao Longfei 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    def test_append_backward(self):
        prog = Program.instance()
        block = prog.global_block()

        mul_x = block.create_parameter(
            dtype="float32", shape=[5, 10], lod_level=0, name="mul.x")
        mul_y = block.create_var(
            dtype="float32", shape=[10, 8], lod_level=0, name="mul.y")
        mul_out = block.create_var(
            dtype="float32", shape=[5, 8], lod_level=0, name="mul.out")
        mul_op = block.append_op(
            type="mul",
            inputs={"X": [mul_x],
                    "Y": mul_y},
            outputs={"Out": [mul_out]},
            attrs={"x_num_col_dims": 1})
        param_to_grad = prog.append_backward(mul_out, set())

Y
Yu Yang 已提交
93 94 95

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