test_operator.py 6.8 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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.

15 16
from __future__ import print_function

Y
Yu Yang 已提交
17
import unittest
Q
qiaolongfei 已提交
18

19 20
import numpy as np

21 22
import paddle.fluid.op as op
import paddle.fluid.proto.framework_pb2 as framework_pb2
Y
Yu Yang 已提交
23 24


25
class TestGetAllProtos(unittest.TestCase):
26

27
    def test_all(self):
Y
Yu Yang 已提交
28
        all_protos = op.get_all_op_protos()
Y
Yu Yang 已提交
29 30 31 32 33 34
        self.assertNotEqual(0, len(all_protos))

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


35
class TestOpDescCreationMethod(unittest.TestCase):
36

37
    def test_plain_input_output(self):
Y
Yu Yang 已提交
38
        op_proto = framework_pb2.OpProto()
Y
Yu Yang 已提交
39 40
        op_proto.type = "test"
        ipt = op_proto.inputs.add()
41 42 43
        ipt.name = "X"
        ipt.comment = "not matter"

Y
Yu Yang 已提交
44
        ipt = op_proto.inputs.add()
45 46 47
        ipt.name = "Y"
        ipt.comment = "not matter"

Y
Yu Yang 已提交
48
        opt = op_proto.outputs.add()
49 50 51
        opt.name = "Z"
        opt.comment = "not matter"

Y
Yu Yang 已提交
52
        op_proto.comment = "not matter"
53

Y
Yu Yang 已提交
54
        self.assertTrue(op_proto.IsInitialized())
55

Y
Yu Yang 已提交
56
        method = op.OpDescCreationMethod(op_proto)
57
        output = method(X="a", Y="b", Z="c")
Y
Yu Yang 已提交
58
        expected = framework_pb2.OpDesc()
59
        expected.type = "test"
Y
Yu Yang 已提交
60 61 62 63 64 65 66 67 68 69
        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"])

70 71 72
        self.assertEqual(expected, output)

    def test_multiple_input_plain_output(self):
Y
Yu Yang 已提交
73
        op_proto = framework_pb2.OpProto()
Y
Yu Yang 已提交
74 75
        op_proto.type = "fc"
        ipt = op_proto.inputs.add()
76 77
        ipt.name = "X"
        ipt.comment = ""
Y
Yu Yang 已提交
78
        ipt.duplicable = True
79

Y
Yu Yang 已提交
80
        ipt = op_proto.inputs.add()
81 82
        ipt.name = "W"
        ipt.comment = ""
Y
Yu Yang 已提交
83
        ipt.duplicable = True
84

Y
Yu Yang 已提交
85
        ipt = op_proto.inputs.add()
86 87 88
        ipt.name = "b"
        ipt.comment = ""

Y
Yu Yang 已提交
89
        out = op_proto.outputs.add()
90 91 92
        out.name = "Y"
        out.comment = ""

Y
Yu Yang 已提交
93 94 95
        op_proto.comment = ""
        self.assertTrue(op_proto.IsInitialized())
        method = op.OpDescCreationMethod(op_proto)
96 97

        generated1 = method(X="x", W="w", b="b", Y="y")
Y
Yu Yang 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
        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'])
114 115 116
        expected1.type = 'fc'
        self.assertEqual(expected1, generated1)

117 118 119 120
        generated2 = method(X=['x1', 'x2', 'x3'],
                            b='b',
                            W=['w1', 'w2', 'w3'],
                            Y='y')
Y
Yu Yang 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        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'])

139 140 141 142
        expected2.type = 'fc'
        self.assertEqual(expected2, generated2)

    def test_attrs(self):
Y
Yu Yang 已提交
143
        op_proto = framework_pb2.OpProto()
Y
Yu Yang 已提交
144 145
        op_proto.type = "test"
        ipt = op_proto.inputs.add()
146 147 148 149
        ipt.name = 'X'
        ipt.comment = ""

        def __add_attr__(name, type):
Y
Yu Yang 已提交
150
            attr = op_proto.attrs.add()
151 152 153 154
            attr.name = name
            attr.comment = ""
            attr.type = type

Y
Yu Yang 已提交
155 156
        __add_attr__("int_attr", framework_pb2.INT)
        __add_attr__("float_attr", framework_pb2.FLOAT)
157
        __add_attr__("float64_attr", framework_pb2.FLOAT64)
Y
Yu Yang 已提交
158 159 160 161
        __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)
162

Y
Yu Yang 已提交
163 164
        op_proto.comment = ""
        self.assertTrue(op_proto.IsInitialized())
165

Y
Yu Yang 已提交
166
        method = op.OpDescCreationMethod(op_proto)
167

168 169 170
        generated = method(X="a",
                           int_attr=10,
                           float_attr=3.2,
171
                           float64_attr=np.finfo("float64").max,
172 173 174 175
                           string_attr="test_str",
                           ints_attr=[0, 1, 2, 3, 4],
                           floats_attr=[0.2, 3.2, 4.5],
                           strings_attr=["a", "b", "c"])
176

Y
Yu Yang 已提交
177
        expected = framework_pb2.OpDesc()
178
        expected.type = "test"
Y
Yu Yang 已提交
179 180 181 182 183

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

184 185
        attr = expected.attrs.add()
        attr.name = "int_attr"
Y
Yu Yang 已提交
186
        attr.type = framework_pb2.INT
187 188 189 190
        attr.i = 10

        attr = expected.attrs.add()
        attr.name = "float_attr"
Y
Yu Yang 已提交
191
        attr.type = framework_pb2.FLOAT
192 193
        attr.f = 3.2

194 195 196 197 198
        attr = expected.attrs.add()
        attr.name = "float64_attr"
        attr.type = framework_pb2.FLOAT64
        attr.float64 = np.finfo("float64").max

199 200
        attr = expected.attrs.add()
        attr.name = "string_attr"
Y
Yu Yang 已提交
201
        attr.type = framework_pb2.STRING
202 203 204 205
        attr.s = "test_str"

        attr = expected.attrs.add()
        attr.name = "ints_attr"
Y
Yu Yang 已提交
206
        attr.type = framework_pb2.INTS
207 208 209 210
        attr.ints.extend([0, 1, 2, 3, 4])

        attr = expected.attrs.add()
        attr.name = 'floats_attr'
Y
Yu Yang 已提交
211
        attr.type = framework_pb2.FLOATS
212 213 214 215
        attr.floats.extend([0.2, 3.2, 4.5])

        attr = expected.attrs.add()
        attr.name = 'strings_attr'
Y
Yu Yang 已提交
216
        attr.type = framework_pb2.STRINGS
217 218 219 220 221 222
        attr.strings.extend(['a', 'b', 'c'])

        self.assertEqual(expected, generated)


class TestOpCreations(unittest.TestCase):
223

224
    def test_all(self):
Y
Fix CI  
Yu Yang 已提交
225
        add_op = op.Operator("sum", X=["a", "b"], Out="z")
226 227
        self.assertIsNotNone(add_op)
        # Invoke C++ DebugString()
Y
Fix CI  
Yu Yang 已提交
228
        self.assertEqual('Op(sum), inputs:{X[a, b]}, outputs:{Out[z]}.',
229 230 231
                         str(add_op))


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