test_operator.py 5.8 KB
Newer Older
Y
Yu Yang 已提交
1
import unittest
Y
Yu Yang 已提交
2
import paddle.v2.framework.op as op
3
import paddle.v2.framework.core as core
Y
Yu Yang 已提交
4
import paddle.v2.framework.proto.framework_pb2 as framework_pb2
Y
Yu Yang 已提交
5 6


7 8
class TestGetAllProtos(unittest.TestCase):
    def test_all(self):
Y
Yu Yang 已提交
9
        all_protos = op.get_all_op_protos()
Y
Yu Yang 已提交
10 11 12 13 14 15
        self.assertNotEqual(0, len(all_protos))

        for each in all_protos:
            self.assertTrue(each.IsInitialized())


16 17
class TestOpDescCreationMethod(unittest.TestCase):
    def test_plain_input_output(self):
Y
Yu Yang 已提交
18
        op_proto = framework_pb2.OpProto()
Y
Yu Yang 已提交
19 20
        op_proto.type = "test"
        ipt = op_proto.inputs.add()
21 22 23
        ipt.name = "X"
        ipt.comment = "not matter"

Y
Yu Yang 已提交
24
        ipt = op_proto.inputs.add()
25 26 27
        ipt.name = "Y"
        ipt.comment = "not matter"

Y
Yu Yang 已提交
28
        opt = op_proto.outputs.add()
29 30 31
        opt.name = "Z"
        opt.comment = "not matter"

Y
Yu Yang 已提交
32
        op_proto.comment = "not matter"
33

Y
Yu Yang 已提交
34
        self.assertTrue(op_proto.IsInitialized())
35

Y
Yu Yang 已提交
36
        method = op.OpDescCreationMethod(op_proto)
37
        output = method(X="a", Y="b", Z="c")
Y
Yu Yang 已提交
38
        expected = framework_pb2.OpDesc()
39
        expected.type = "test"
Y
Yu Yang 已提交
40 41 42 43 44 45 46 47 48 49
        ipt_0 = expected.inputs.add()
        ipt_0.parameter = "X"
        ipt_0.arguments.extend(["a"])
        ipt_1 = expected.inputs.add()
        ipt_1.parameter = 'Y'
        ipt_1.arguments.extend(['b'])
        opt = expected.outputs.add()
        opt.parameter = "Z"
        opt.arguments.extend(["c"])

50 51 52
        self.assertEqual(expected, output)

    def test_multiple_input_plain_output(self):
Y
Yu Yang 已提交
53
        op_proto = framework_pb2.OpProto()
Y
Yu Yang 已提交
54 55
        op_proto.type = "fc"
        ipt = op_proto.inputs.add()
56 57
        ipt.name = "X"
        ipt.comment = ""
Y
Yu Yang 已提交
58
        ipt.duplicable = True
59

Y
Yu Yang 已提交
60
        ipt = op_proto.inputs.add()
61 62
        ipt.name = "W"
        ipt.comment = ""
Y
Yu Yang 已提交
63
        ipt.duplicable = True
64

Y
Yu Yang 已提交
65
        ipt = op_proto.inputs.add()
66 67 68
        ipt.name = "b"
        ipt.comment = ""

Y
Yu Yang 已提交
69
        out = op_proto.outputs.add()
70 71 72
        out.name = "Y"
        out.comment = ""

Y
Yu Yang 已提交
73 74 75
        op_proto.comment = ""
        self.assertTrue(op_proto.IsInitialized())
        method = op.OpDescCreationMethod(op_proto)
76 77

        generated1 = method(X="x", W="w", b="b", Y="y")
Y
Yu Yang 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        expected1 = framework_pb2.OpDesc()
        tmp = expected1.inputs.add()
        tmp.parameter = "X"
        tmp.arguments.extend(['x'])

        tmp = expected1.inputs.add()
        tmp.parameter = 'W'
        tmp.arguments.extend(['w'])

        tmp = expected1.inputs.add()
        tmp.parameter = 'b'
        tmp.arguments.extend(['b'])

        tmp = expected1.outputs.add()
        tmp.parameter = 'Y'
        tmp.arguments.extend(['y'])
94 95 96 97 98
        expected1.type = 'fc'
        self.assertEqual(expected1, generated1)

        generated2 = method(
            X=['x1', 'x2', 'x3'], b='b', W=['w1', 'w2', 'w3'], Y='y')
Y
Yu Yang 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        expected2 = framework_pb2.OpDesc()

        tmp = expected2.inputs.add()
        tmp.parameter = "X"
        tmp.arguments.extend(['x1', 'x2', 'x3'])

        tmp = expected2.inputs.add()
        tmp.parameter = 'W'
        tmp.arguments.extend(['w1', 'w2', 'w3'])

        tmp = expected2.inputs.add()
        tmp.parameter = 'b'
        tmp.arguments.extend(['b'])

        tmp = expected2.outputs.add()
        tmp.parameter = 'Y'
        tmp.arguments.extend(['y'])

117 118 119 120
        expected2.type = 'fc'
        self.assertEqual(expected2, generated2)

    def test_attrs(self):
Y
Yu Yang 已提交
121
        op_proto = framework_pb2.OpProto()
Y
Yu Yang 已提交
122 123
        op_proto.type = "test"
        ipt = op_proto.inputs.add()
124 125 126 127
        ipt.name = 'X'
        ipt.comment = ""

        def __add_attr__(name, type):
Y
Yu Yang 已提交
128
            attr = op_proto.attrs.add()
129 130 131 132
            attr.name = name
            attr.comment = ""
            attr.type = type

Y
Yu Yang 已提交
133 134 135 136 137 138
        __add_attr__("int_attr", framework_pb2.INT)
        __add_attr__("float_attr", framework_pb2.FLOAT)
        __add_attr__("string_attr", framework_pb2.STRING)
        __add_attr__("ints_attr", framework_pb2.INTS)
        __add_attr__("floats_attr", framework_pb2.FLOATS)
        __add_attr__("strings_attr", framework_pb2.STRINGS)
139

Y
Yu Yang 已提交
140 141
        op_proto.comment = ""
        self.assertTrue(op_proto.IsInitialized())
142

Y
Yu Yang 已提交
143
        method = op.OpDescCreationMethod(op_proto)
144 145 146 147 148 149 150 151 152 153

        generated = method(
            X="a",
            int_attr=10,
            float_attr=3.2,
            string_attr="test_str",
            ints_attr=[0, 1, 2, 3, 4],
            floats_attr=[0.2, 3.2, 4.5],
            strings_attr=["a", "b", "c"])

Y
Yu Yang 已提交
154
        expected = framework_pb2.OpDesc()
155
        expected.type = "test"
Y
Yu Yang 已提交
156 157 158 159 160

        ipt = expected.inputs.add()
        ipt.parameter = "X"
        ipt.arguments.extend(['a'])

161 162
        attr = expected.attrs.add()
        attr.name = "int_attr"
Y
Yu Yang 已提交
163
        attr.type = framework_pb2.INT
164 165 166 167
        attr.i = 10

        attr = expected.attrs.add()
        attr.name = "float_attr"
Y
Yu Yang 已提交
168
        attr.type = framework_pb2.FLOAT
169 170 171 172
        attr.f = 3.2

        attr = expected.attrs.add()
        attr.name = "string_attr"
Y
Yu Yang 已提交
173
        attr.type = framework_pb2.STRING
174 175 176 177
        attr.s = "test_str"

        attr = expected.attrs.add()
        attr.name = "ints_attr"
Y
Yu Yang 已提交
178
        attr.type = framework_pb2.INTS
179 180 181 182
        attr.ints.extend([0, 1, 2, 3, 4])

        attr = expected.attrs.add()
        attr.name = 'floats_attr'
Y
Yu Yang 已提交
183
        attr.type = framework_pb2.FLOATS
184 185 186 187
        attr.floats.extend([0.2, 3.2, 4.5])

        attr = expected.attrs.add()
        attr.name = 'strings_attr'
Y
Yu Yang 已提交
188
        attr.type = framework_pb2.STRINGS
189 190 191 192 193 194 195
        attr.strings.extend(['a', 'b', 'c'])

        self.assertEqual(expected, generated)


class TestOpCreations(unittest.TestCase):
    def test_all(self):
196
        add_op = op.Operator("add", X="a", Y="b", Out="z")
197 198
        self.assertIsNotNone(add_op)
        # Invoke C++ DebugString()
199
        self.assertEqual('Op(add), inputs:{X[a], Y[b]}, outputs:{Out[z]}.',
200 201 202
                         str(add_op))


Y
Yu Yang 已提交
203 204
if __name__ == "__main__":
    unittest.main()