|
|
|
@ -1,9 +1,7 @@
|
|
|
|
|
import unittest
|
|
|
|
|
import paddle.v2.framework.op as op
|
|
|
|
|
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
|
|
|
|
|
import paddle.v2.framework.proto.attribute_pb2 as attribute_pb2
|
|
|
|
|
import paddle.v2.framework.proto.framework_pb2 as framework_pb2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestGetAllProtos(unittest.TestCase):
|
|
|
|
@ -17,7 +15,7 @@ class TestGetAllProtos(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
class TestOpDescCreationMethod(unittest.TestCase):
|
|
|
|
|
def test_plain_input_output(self):
|
|
|
|
|
op_proto = op_proto_pb2.OpProto()
|
|
|
|
|
op_proto = framework_pb2.OpProto()
|
|
|
|
|
op_proto.type = "test"
|
|
|
|
|
ipt = op_proto.inputs.add()
|
|
|
|
|
ipt.name = "X"
|
|
|
|
@ -37,25 +35,32 @@ class TestOpDescCreationMethod(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
method = op.OpDescCreationMethod(op_proto)
|
|
|
|
|
output = method(X="a", Y="b", Z="c")
|
|
|
|
|
|
|
|
|
|
expected = op_desc_pb2.OpDesc()
|
|
|
|
|
expected = framework_pb2.OpDesc()
|
|
|
|
|
expected.type = "test"
|
|
|
|
|
expected.inputs.extend(["a", "b"])
|
|
|
|
|
expected.outputs.append("c")
|
|
|
|
|
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"])
|
|
|
|
|
|
|
|
|
|
self.assertEqual(expected, output)
|
|
|
|
|
|
|
|
|
|
def test_multiple_input_plain_output(self):
|
|
|
|
|
op_proto = op_proto_pb2.OpProto()
|
|
|
|
|
op_proto = framework_pb2.OpProto()
|
|
|
|
|
op_proto.type = "fc"
|
|
|
|
|
ipt = op_proto.inputs.add()
|
|
|
|
|
ipt.name = "X"
|
|
|
|
|
ipt.comment = ""
|
|
|
|
|
ipt.multiple = True
|
|
|
|
|
ipt.duplicable = True
|
|
|
|
|
|
|
|
|
|
ipt = op_proto.inputs.add()
|
|
|
|
|
ipt.name = "W"
|
|
|
|
|
ipt.comment = ""
|
|
|
|
|
ipt.multiple = True
|
|
|
|
|
ipt.duplicable = True
|
|
|
|
|
|
|
|
|
|
ipt = op_proto.inputs.add()
|
|
|
|
|
ipt.name = "b"
|
|
|
|
@ -70,32 +75,50 @@ class TestOpDescCreationMethod(unittest.TestCase):
|
|
|
|
|
method = op.OpDescCreationMethod(op_proto)
|
|
|
|
|
|
|
|
|
|
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 = 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'])
|
|
|
|
|
expected1.type = 'fc'
|
|
|
|
|
# the input_format can be removed after testing
|
|
|
|
|
attr = expected1.attrs.add()
|
|
|
|
|
attr.name = 'input_format'
|
|
|
|
|
attr.type = attribute_pb2.INTS
|
|
|
|
|
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 = 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'])
|
|
|
|
|
|
|
|
|
|
expected2.type = 'fc'
|
|
|
|
|
# the input_format can be removed after testing
|
|
|
|
|
attr = expected2.attrs.add()
|
|
|
|
|
attr.name = 'input_format'
|
|
|
|
|
attr.type = attribute_pb2.INTS
|
|
|
|
|
attr.ints.extend([0, 3, 6, 7])
|
|
|
|
|
self.assertEqual(expected2, generated2)
|
|
|
|
|
|
|
|
|
|
def test_attrs(self):
|
|
|
|
|
op_proto = op_proto_pb2.OpProto()
|
|
|
|
|
op_proto = framework_pb2.OpProto()
|
|
|
|
|
op_proto.type = "test"
|
|
|
|
|
ipt = op_proto.inputs.add()
|
|
|
|
|
ipt.name = 'X'
|
|
|
|
@ -107,12 +130,12 @@ class TestOpDescCreationMethod(unittest.TestCase):
|
|
|
|
|
attr.comment = ""
|
|
|
|
|
attr.type = type
|
|
|
|
|
|
|
|
|
|
__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)
|
|
|
|
|
__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)
|
|
|
|
|
|
|
|
|
|
op_proto.comment = ""
|
|
|
|
|
self.assertTrue(op_proto.IsInitialized())
|
|
|
|
@ -128,76 +151,52 @@ class TestOpDescCreationMethod(unittest.TestCase):
|
|
|
|
|
floats_attr=[0.2, 3.2, 4.5],
|
|
|
|
|
strings_attr=["a", "b", "c"])
|
|
|
|
|
|
|
|
|
|
expected = op_desc_pb2.OpDesc()
|
|
|
|
|
expected = framework_pb2.OpDesc()
|
|
|
|
|
expected.type = "test"
|
|
|
|
|
expected.inputs.extend(['a'])
|
|
|
|
|
|
|
|
|
|
ipt = expected.inputs.add()
|
|
|
|
|
ipt.parameter = "X"
|
|
|
|
|
ipt.arguments.extend(['a'])
|
|
|
|
|
|
|
|
|
|
attr = expected.attrs.add()
|
|
|
|
|
attr.name = "int_attr"
|
|
|
|
|
attr.type = attribute_pb2.INT
|
|
|
|
|
attr.type = framework_pb2.INT
|
|
|
|
|
attr.i = 10
|
|
|
|
|
|
|
|
|
|
attr = expected.attrs.add()
|
|
|
|
|
attr.name = "float_attr"
|
|
|
|
|
attr.type = attribute_pb2.FLOAT
|
|
|
|
|
attr.type = framework_pb2.FLOAT
|
|
|
|
|
attr.f = 3.2
|
|
|
|
|
|
|
|
|
|
attr = expected.attrs.add()
|
|
|
|
|
attr.name = "string_attr"
|
|
|
|
|
attr.type = attribute_pb2.STRING
|
|
|
|
|
attr.type = framework_pb2.STRING
|
|
|
|
|
attr.s = "test_str"
|
|
|
|
|
|
|
|
|
|
attr = expected.attrs.add()
|
|
|
|
|
attr.name = "ints_attr"
|
|
|
|
|
attr.type = attribute_pb2.INTS
|
|
|
|
|
attr.type = framework_pb2.INTS
|
|
|
|
|
attr.ints.extend([0, 1, 2, 3, 4])
|
|
|
|
|
|
|
|
|
|
attr = expected.attrs.add()
|
|
|
|
|
attr.name = 'floats_attr'
|
|
|
|
|
attr.type = attribute_pb2.FLOATS
|
|
|
|
|
attr.type = framework_pb2.FLOATS
|
|
|
|
|
attr.floats.extend([0.2, 3.2, 4.5])
|
|
|
|
|
|
|
|
|
|
attr = expected.attrs.add()
|
|
|
|
|
attr.name = 'strings_attr'
|
|
|
|
|
attr.type = attribute_pb2.STRINGS
|
|
|
|
|
attr.type = framework_pb2.STRINGS
|
|
|
|
|
attr.strings.extend(['a', 'b', 'c'])
|
|
|
|
|
|
|
|
|
|
self.assertEqual(expected, generated)
|
|
|
|
|
|
|
|
|
|
def test_input_temporary_output(self):
|
|
|
|
|
op_proto = op_proto_pb2.OpProto()
|
|
|
|
|
op_proto.type = "test"
|
|
|
|
|
out = op_proto.outputs.add()
|
|
|
|
|
out.name = "OUT"
|
|
|
|
|
out.comment = ""
|
|
|
|
|
|
|
|
|
|
out = op_proto.outputs.add()
|
|
|
|
|
out.name = "TMP"
|
|
|
|
|
out.comment = ""
|
|
|
|
|
out.temporary = True
|
|
|
|
|
|
|
|
|
|
out = op_proto.outputs.add()
|
|
|
|
|
out.name = "OUT2"
|
|
|
|
|
out.comment = ""
|
|
|
|
|
op_proto.comment = ""
|
|
|
|
|
|
|
|
|
|
method = op.OpDescCreationMethod(op_proto)
|
|
|
|
|
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"
|
|
|
|
|
attr.type = attribute_pb2.INTS
|
|
|
|
|
attr.ints.append(2)
|
|
|
|
|
self.assertEqual(generated, desc)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestOpCreations(unittest.TestCase):
|
|
|
|
|
def test_all(self):
|
|
|
|
|
add_op = op.Operator("add_two", X="a", Y="b", Out="z")
|
|
|
|
|
self.assertIsNotNone(add_op)
|
|
|
|
|
# Invoke C++ DebugString()
|
|
|
|
|
self.assertEqual('Op(add_two), inputs:(a, b), outputs:(z).',
|
|
|
|
|
self.assertEqual('Op(add_two), inputs:{X[a], Y[b]}, outputs:{Out[z]}.',
|
|
|
|
|
str(add_op))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|