test_operator_desc.py 3.3 KB
Newer Older
F
fengjiayi 已提交
1
import unittest
Q
Qiao Longfei 已提交
2 3
from paddle.v2.fluid.framework import Variable, Program, g_main_program
import paddle.v2.fluid.core as core
F
fengjiayi 已提交
4 5 6 7


class TestOperator(unittest.TestCase):
    def test_error_type(self):
8
        block = g_main_program.create_block()
F
fengjiayi 已提交
9 10 11 12 13 14 15
        try:
            block.append_op()
            self.assertFail()
        except ValueError as v_err:
            self.assertEqual(
                v_err.message,
                "`type` to initilized an Operator can not be None.")
F
fengjiayi 已提交
16 17 18
        try:
            block.append_op(type="no_such_op")
            self.assertFail()
Y
Yu Yang 已提交
19
        except ValueError as a_err:
F
fengjiayi 已提交
20
            self.assertEqual(a_err.message,
F
fengjiayi 已提交
21
                             "Operator \"no_such_op\" has not been registered.")
F
fengjiayi 已提交
22

F
fengjiayi 已提交
23
    def test_op_desc_creation(self):
24 25
        program = Program()
        block = program.current_block()
F
fengjiayi 已提交
26 27 28 29 30 31 32 33 34 35
        mul_x = block.create_var(
            dtype="float32", shape=[5, 10], lod_level=0, name="mul.x")
        mul_y = block.create_var(
            dtype="float32", shape=[10, 8], lod_level=0, name="mul.y")
        mul_out = block.create_var(
            dtype="float32", shape=[5, 8], lod_level=0, name="mul.out")
        mul_op = block.append_op(
            type="mul",
            inputs={"X": [mul_x],
                    "Y": mul_y},
F
fengjiayi 已提交
36 37
            outputs={"Out": [mul_out]},
            attrs={"x_num_col_dims": 1})
38 39

        self.assertNotEqual(str(mul_op), "")
F
fengjiayi 已提交
40 41
        self.assertEqual(mul_op.type, "mul")
        self.assertEqual(mul_op.input_names, ["X", "Y"])
F
fengjiayi 已提交
42 43
        self.assertEqual(mul_op.input("X"), ["mul.x"])
        self.assertEqual(mul_op.input("Y"), ["mul.y"])
F
fengjiayi 已提交
44
        self.assertEqual(mul_op.output_names, ["Out"])
F
fengjiayi 已提交
45
        self.assertEqual(mul_op.output("Out"), ["mul.out"])
46 47
        self.assertEqual(
            set(mul_op.attr_names), set(["x_num_col_dims", "y_num_col_dims"]))
F
fengjiayi 已提交
48 49 50
        self.assertEqual(mul_op.has_attr("x_num_col_dims"), True)
        self.assertEqual(mul_op.attr_type("x_num_col_dims"), core.AttrType.INT)
        self.assertEqual(mul_op.attr("x_num_col_dims"), 1)
51 52 53
        self.assertEqual(mul_op.has_attr("y_num_col_dims"), True)
        self.assertEqual(mul_op.attr_type("y_num_col_dims"), core.AttrType.INT)
        self.assertEqual(mul_op.attr("y_num_col_dims"), 1)
54
        self.assertEqual(mul_op.idx, 0)
F
fengjiayi 已提交
55
        self.assertEqual(mul_out.op, mul_op)
F
fengjiayi 已提交
56 57

    def test_mult_input(self):
58 59
        program = Program()
        block = program.current_block()
F
fengjiayi 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73
        sum_x1 = block.create_var(
            dtype="int", shape=[3, 4], lod_level=0, name="sum.x1")
        sum_x2 = block.create_var(
            dtype="int", shape=[3, 4], lod_level=0, name="sum.x2")
        sum_x3 = block.create_var(
            dtype="int", shape=[3, 4], lod_level=0, name="sum.x3")
        sum_out = block.create_var(
            dtype="int", shape=[3, 4], lod_level=0, name="sum.out")
        sum_op = block.append_op(
            type="sum",
            inputs={"X": [sum_x1, sum_x2, sum_x3]},
            outputs={"Out": sum_out})
        self.assertEqual(sum_op.type, "sum")
        self.assertEqual(sum_op.input_names, ["X"])
F
fengjiayi 已提交
74
        self.assertEqual(sum_op.input("X"), ["sum.x1", "sum.x2", "sum.x3"])
F
fengjiayi 已提交
75
        self.assertEqual(sum_op.output_names, ["Out"])
F
fengjiayi 已提交
76
        self.assertEqual(sum_op.output("Out"), ["sum.out"])
77
        self.assertEqual(sum_op.idx, 0)
F
fengjiayi 已提交
78
        self.assertEqual(sum_out.op, sum_op)
F
fengjiayi 已提交
79 80 81 82


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