test_operator.py 6.2 KB
Newer Older
Y
Yu Yang 已提交
1
import unittest
Y
Yu Yang 已提交
2
import paddle.v2.framework.op as op
3 4 5
import paddle.v2.framework.core as core
import paddle.v2.framework.proto.op_proto_pb2 as op_proto_pb2
import paddle.v2.framework.proto.op_desc_pb2 as op_desc_pb2
Y
Yu Yang 已提交
6
import paddle.v2.framework.proto.attribute_pb2 as attribute_pb2
Y
Yu Yang 已提交
7 8


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

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


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

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

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

Y
Yu Yang 已提交
34
        op_proto.comment = "not matter"
35

Y
Yu Yang 已提交
36
        self.assertTrue(op_proto.IsInitialized())
37

Y
Yu Yang 已提交
38
        method = op.OpDescCreationMethod(op_proto)
39 40 41 42 43 44 45 46 47
        output = method(X="a", Y="b", Z="c")

        expected = op_desc_pb2.OpDesc()
        expected.type = "test"
        expected.inputs.extend(["a", "b"])
        expected.outputs.append("c")
        self.assertEqual(expected, output)

    def test_multiple_input_plain_output(self):
Y
Yu Yang 已提交
48 49 50
        op_proto = op_proto_pb2.OpProto()
        op_proto.type = "fc"
        ipt = op_proto.inputs.add()
51 52 53 54
        ipt.name = "X"
        ipt.comment = ""
        ipt.multiple = True

Y
Yu Yang 已提交
55
        ipt = op_proto.inputs.add()
56 57 58 59
        ipt.name = "W"
        ipt.comment = ""
        ipt.multiple = True

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

Y
Yu Yang 已提交
64
        out = op_proto.outputs.add()
65 66 67
        out.name = "Y"
        out.comment = ""

Y
Yu Yang 已提交
68 69 70
        op_proto.comment = ""
        self.assertTrue(op_proto.IsInitialized())
        method = op.OpDescCreationMethod(op_proto)
71 72 73 74 75 76

        generated1 = method(X="x", W="w", b="b", Y="y")
        expected1 = op_desc_pb2.OpDesc()
        expected1.inputs.extend(['x', 'w', 'b'])
        expected1.outputs.extend(['y'])
        expected1.type = 'fc'
77
        # the input_format can be removed after testing
78 79
        attr = expected1.attrs.add()
        attr.name = 'input_format'
Y
Yu Yang 已提交
80
        attr.type = attribute_pb2.INTS
81 82 83 84 85 86 87 88 89
        attr.ints.extend([0, 1, 2, 3])
        self.assertEqual(expected1, generated1)

        generated2 = method(
            X=['x1', 'x2', 'x3'], b='b', W=['w1', 'w2', 'w3'], Y='y')
        expected2 = op_desc_pb2.OpDesc()
        expected2.inputs.extend(['x1', 'x2', 'x3', 'w1', 'w2', 'w3', 'b'])
        expected2.outputs.extend(['y'])
        expected2.type = 'fc'
90
        # the input_format can be removed after testing
91 92
        attr = expected2.attrs.add()
        attr.name = 'input_format'
Y
Yu Yang 已提交
93
        attr.type = attribute_pb2.INTS
94 95 96 97
        attr.ints.extend([0, 3, 6, 7])
        self.assertEqual(expected2, generated2)

    def test_attrs(self):
Y
Yu Yang 已提交
98 99 100
        op_proto = op_proto_pb2.OpProto()
        op_proto.type = "test"
        ipt = op_proto.inputs.add()
101 102 103 104
        ipt.name = 'X'
        ipt.comment = ""

        def __add_attr__(name, type):
Y
Yu Yang 已提交
105
            attr = op_proto.attrs.add()
106 107 108 109
            attr.name = name
            attr.comment = ""
            attr.type = type

Y
Yu Yang 已提交
110 111 112 113 114 115
        __add_attr__("int_attr", attribute_pb2.INT)
        __add_attr__("float_attr", attribute_pb2.FLOAT)
        __add_attr__("string_attr", attribute_pb2.STRING)
        __add_attr__("ints_attr", attribute_pb2.INTS)
        __add_attr__("floats_attr", attribute_pb2.FLOATS)
        __add_attr__("strings_attr", attribute_pb2.STRINGS)
116

Y
Yu Yang 已提交
117 118
        op_proto.comment = ""
        self.assertTrue(op_proto.IsInitialized())
119

Y
Yu Yang 已提交
120
        method = op.OpDescCreationMethod(op_proto)
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

        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"])

        expected = op_desc_pb2.OpDesc()
        expected.type = "test"
        expected.inputs.extend(['a'])
        attr = expected.attrs.add()
        attr.name = "int_attr"
Y
Yu Yang 已提交
136
        attr.type = attribute_pb2.INT
137 138 139 140
        attr.i = 10

        attr = expected.attrs.add()
        attr.name = "float_attr"
Y
Yu Yang 已提交
141
        attr.type = attribute_pb2.FLOAT
142 143 144 145
        attr.f = 3.2

        attr = expected.attrs.add()
        attr.name = "string_attr"
Y
Yu Yang 已提交
146
        attr.type = attribute_pb2.STRING
147 148 149 150
        attr.s = "test_str"

        attr = expected.attrs.add()
        attr.name = "ints_attr"
Y
Yu Yang 已提交
151
        attr.type = attribute_pb2.INTS
152 153 154 155
        attr.ints.extend([0, 1, 2, 3, 4])

        attr = expected.attrs.add()
        attr.name = 'floats_attr'
Y
Yu Yang 已提交
156
        attr.type = attribute_pb2.FLOATS
157 158 159 160
        attr.floats.extend([0.2, 3.2, 4.5])

        attr = expected.attrs.add()
        attr.name = 'strings_attr'
Y
Yu Yang 已提交
161
        attr.type = attribute_pb2.STRINGS
162 163 164 165 166
        attr.strings.extend(['a', 'b', 'c'])

        self.assertEqual(expected, generated)

    def test_input_temporary_output(self):
Y
Yu Yang 已提交
167 168 169
        op_proto = op_proto_pb2.OpProto()
        op_proto.type = "test"
        out = op_proto.outputs.add()
170 171 172
        out.name = "OUT"
        out.comment = ""

Y
Yu Yang 已提交
173
        out = op_proto.outputs.add()
174 175 176 177
        out.name = "TMP"
        out.comment = ""
        out.temporary = True

Y
Yu Yang 已提交
178
        out = op_proto.outputs.add()
179 180
        out.name = "OUT2"
        out.comment = ""
Y
Yu Yang 已提交
181
        op_proto.comment = ""
182

Y
Yu Yang 已提交
183
        method = op.OpDescCreationMethod(op_proto)
184 185 186 187 188 189
        generated = method(OUT="a", OUT2="b")
        desc = op_desc_pb2.OpDesc()
        desc.outputs.extend(["a", core.var_names.temp(), "b"])
        desc.type = "test"
        attr = desc.attrs.add()
        attr.name = "temporary_index"
Y
Yu Yang 已提交
190
        attr.type = attribute_pb2.INTS
191 192 193 194 195 196
        attr.ints.append(2)
        self.assertEqual(generated, desc)


class TestOpCreations(unittest.TestCase):
    def test_all(self):
Y
Yu Yang 已提交
197
        add_op = op.Operator("add_two", X="a", Y="b", Out="z")
198 199 200 201 202 203
        self.assertIsNotNone(add_op)
        # Invoke C++ DebugString()
        self.assertEqual('Op(add_two), inputs:(a, b), outputs:(z).',
                         str(add_op))


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