test_protobuf_descs.py 5.5 KB
Newer Older
D
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
#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.
Y
Update  
Yu Yang 已提交
14
import unittest
Q
Qiao Longfei 已提交
15
import paddle.v2.fluid.core as core
Y
Update  
Yu Yang 已提交
16 17


Y
Yu Yang 已提交
18 19
class TestOpDesc(unittest.TestCase):
    def test_op_desc(self):
20
        prog = core.ProgramDesc()
Y
Yu Yang 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
        self.assertIsNotNone(prog)
        block = prog.block(0)
        self.assertIsNotNone(block)
        op = block.append_op()
        self.assertIsNotNone(op)
        op.set_type("test")
        self.assertEqual("test", op.type())
        op.set_input("X", ["a", "b", "c"])
        self.assertEqual(["a", "b", "c"], op.input("X"))
        self.assertEqual(["X"], op.input_names())

        op.set_output("Out", ["z"])
        self.assertEqual(['z'], op.output("Out"))
        self.assertEqual(["Out"], op.output_names())

Y
Yu Yang 已提交
36 37 38
        op.set_attr("int_attr", 1)
        self.assertEqual(1, op.attr("int_attr"))
        self.assertTrue(op.has_attr("int_attr"))
Y
Yu Yang 已提交
39
        self.assertEqual(core.AttrType.INT, op.attr_type("int_attr"))
Y
Yu Yang 已提交
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 66 67 68

        op.set_attr("float_attr", -1.32)
        self.assertAlmostEqual(-1.32, op.attr("float_attr"), delta=1e-4)
        self.assertTrue(op.has_attr("float_attr"))

        op.set_attr("bool_attr", False)
        self.assertFalse(op.attr("bool_attr"))

        op.set_attr("string_attr", "abc")
        self.assertEqual("abc", op.attr("string_attr"))
        self.assertTrue(op.has_attr("string_attr"))

        op.set_attr("ints_attr", [1, 2, 3])
        self.assertEqual([1, 2, 3], op.attr("ints_attr"))

        expected = [1.2, 2.3, 3.4]
        op.set_attr("floats_attr", expected)
        for e, a in zip(expected, op.attr("floats_attr")):
            self.assertAlmostEqual(e, a, delta=1e-4)

        op.set_attr("strings_attr", ["a", "b", "c"])
        self.assertEqual(["a", "b", "c"], op.attr("strings_attr"))

        op.set_attr("bools_attr", [True, False, True])
        self.assertEqual([True, False, True], op.attr("bools_attr"))

        self.assertEqual(8, len(op.attr_names()))

        op.set_block_attr("block_attr", prog.block(0))
F
Fix bug  
fengjiayi 已提交
69
        self.assertEqual(0, op.block_attr("block_attr"))
Y
Yu Yang 已提交
70

F
fengjiayi 已提交
71 72 73 74 75 76
        mul_op = block.append_op()
        mul_op.set_type("mul")
        mul_op.check_attrs()
        self.assertEqual(mul_op.attr("x_num_col_dims"), 1)
        self.assertEqual(mul_op.attr("y_num_col_dims"), 1)

Y
Yu Yang 已提交
77

Y
Update  
Yu Yang 已提交
78 79
class TestProgramDesc(unittest.TestCase):
    def test_instance(self):
80
        program_desc = core.ProgramDesc()
Y
Update  
Yu Yang 已提交
81 82
        self.assertIsNotNone(program_desc)
        del program_desc
83
        program_desc = core.ProgramDesc()
Y
Update  
Yu Yang 已提交
84
        self.assertIsNotNone(program_desc)
Y
Yu Yang 已提交
85
        self.assertIsNotNone(program_desc.block(0))
Y
Update  
Yu Yang 已提交
86 87
        del program_desc

Y
Yu Yang 已提交
88
    def test_append_block(self):
89
        prog_desc = core.ProgramDesc()
Y
Yu Yang 已提交
90
        self.assertIsNotNone(prog_desc)
Y
Yu Yang 已提交
91 92 93
        block_root = prog_desc.block(0)
        self.assertIsNotNone(block_root)
        self.assertEqual(block_root.id, 0)
F
fengjiayi 已提交
94
        block1 = prog_desc.append_block(block_root)
Y
Yu Yang 已提交
95
        block2 = prog_desc.append_block(block1)
Y
Yu Yang 已提交
96 97 98
        self.assertIsNotNone(block1)
        self.assertEqual(block1.id, block2.parent)
        self.assertEqual(block_root.id, block1.parent)
F
fengjiayi 已提交
99
        block3 = prog_desc.append_block(block_root)
Y
Yu Yang 已提交
100 101 102
        self.assertEqual(block3.parent, block_root.id)
        self.assertEqual(prog_desc.block(1).id, 1)
        self.assertEqual(4, prog_desc.num_blocks())
F
fengjiayi 已提交
103 104 105 106


class TestVarDesc(unittest.TestCase):
    def test_shape(self):
107
        program_desc = core.ProgramDesc()
Y
Yu Yang 已提交
108
        block = program_desc.block(0)
D
dongzhihong 已提交
109
        var = block.var('my_var')
Y
Yu Yang 已提交
110
        var.set_type(core.VarDesc.VarType.SELECTED_ROWS)
F
fengjiayi 已提交
111 112 113 114
        src_shape = [3, 2, 10, 8]
        var.set_shape(src_shape)
        res_shape = var.shape()
        self.assertEqual(src_shape, res_shape)
Y
Yu Yang 已提交
115
        self.assertEqual(core.VarDesc.VarType.SELECTED_ROWS, var.type())
Y
Yu Yang 已提交
116

F
fengjiayi 已提交
117
    def test_dtype(self):
118
        program_desc = core.ProgramDesc()
F
fengjiayi 已提交
119
        block = program_desc.block(0)
D
dongzhihong 已提交
120
        var = block.var('my_var')
Y
Yu Yang 已提交
121
        var.set_type(core.VarDesc.VarType.LOD_TENSOR)
F
fengjiayi 已提交
122 123
        var.set_dtype(core.DataType.INT32)
        self.assertEqual(core.DataType.INT32, var.dtype())
Y
Yu Yang 已提交
124
        self.assertEqual(core.VarDesc.VarType.LOD_TENSOR, var.type())
F
fengjiayi 已提交
125 126 127 128


class TestBlockDesc(unittest.TestCase):
    def test_add_var(self):
129
        prog = core.ProgramDesc()
F
fengjiayi 已提交
130 131 132
        self.assertIsNotNone(prog)
        block = prog.block(0)
        self.assertIsNotNone(block)
D
dongzhihong 已提交
133 134 135
        var1 = block.var("var1")
        var2 = block.var("var2")
        var3 = block.var("var3")
F
fengjiayi 已提交
136
        all_vars = block.all_vars()
137
        self.assertEqual(set(all_vars), {var1, var2, var3})
D
Dong Zhihong 已提交
138
        var2_re = block.find_var("var2")
F
fengjiayi 已提交
139 140 141
        self.assertEqual(var2_re, var2)

    def test_add_op(self):
142
        prog = core.ProgramDesc()
F
fengjiayi 已提交
143 144 145 146 147 148
        self.assertIsNotNone(prog)
        block = prog.block(0)
        self.assertIsNotNone(block)
        op1 = block.append_op()
        op2 = block.append_op()
        op0 = block.prepend_op()
149 150 151
        all_ops = []
        for idx in xrange(0, block.op_size()):
            all_ops.append(block.op(idx))
F
fengjiayi 已提交
152 153
        self.assertEqual(all_ops, [op0, op1, op2])

Y
Update  
Yu Yang 已提交
154 155 156

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