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

import paddle.v2.framework.core as core
Y
Yu Yang 已提交
4 5 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
from paddle.v2.framework.graph import g_program


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)

36
    def test_append_backward(self):
37 38 39 40 41
        prog = core.ProgramDesc.__create_program_desc__()
        self.assertIsNotNone(prog)
        block = prog.block(0)
        self.assertIsNotNone(block)

42 43 44 45 46 47
        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"])

48
        sum_op_desc = block.append_op()
49 50 51 52
        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"])
53

54 55 56 57 58 59 60 61 62
        expect_ops = [
            "mul", "elementwise_add", "elementwise_add_grad", "mul_grad"
        ]
        actual_ops = []
        prog.append_backward(set())
        for op in block.all_ops():
            actual_ops.append(op.type())
        print(actual_ops)
        self.assertEqual(actual_ops, expect_ops)
63

Y
Yu Yang 已提交
64 65 66

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