Exemplo n.º 1
0
 def test_instance(self):
     program_desc = core.ProgramDesc()
     self.assertIsNotNone(program_desc)
     del program_desc
     program_desc = core.ProgramDesc()
     self.assertIsNotNone(program_desc)
     self.assertIsNotNone(program_desc.block(0))
     del program_desc
Exemplo n.º 2
0
    def test_sum_op(self):
        prog = core.ProgramDesc()
        self.assertIsNotNone(prog)
        block = prog.block(0)
        self.assertIsNotNone(block)

        shape = [10, 20]

        # prepare input/output
        x1 = block.var("x1")
        x1.set_type(core.VarDesc.VarType.LOD_TENSOR)
        x1.set_shape(shape)
        x2 = block.var("x2")
        x2.set_type(core.VarDesc.VarType.LOD_TENSOR)
        x2.set_shape(shape)

        out = block.var("out")
        out.set_type(core.VarDesc.VarType.LOD_TENSOR)

        # prepare the operator
        sum_op_desc = block.append_op()
        sum_op_desc.set_type("sum")
        sum_op_desc.set_input("X", ["x1", "x2"])
        sum_op_desc.set_output("Out", ["out"])

        sum_op_desc.check_attrs()
        sum_op_desc.infer_shape(block)
        self.assertEqual(out.shape(), shape)
Exemplo n.º 3
0
    def test_mul_op(self):
        prog = core.ProgramDesc()
        self.assertIsNotNone(prog)
        block = prog.block(0)
        self.assertIsNotNone(block)

        x_shape = [10, 20]
        y_shape = [20, 30]

        # prepare input/output
        x1 = block.var("x")
        x1.set_type(core.VarDesc.VarType.LOD_TENSOR)
        x1.set_shape(x_shape)
        x2 = block.var("y")
        x2.set_type(core.VarDesc.VarType.LOD_TENSOR)
        x2.set_shape(y_shape)

        out = block.var("out")
        out.set_type(core.VarDesc.VarType.LOD_TENSOR)

        # prepare the operator
        mul_op_desc = block.append_op()
        mul_op_desc.set_type("mul")
        mul_op_desc.set_input("X", ["x"])
        mul_op_desc.set_input("Y", ["y"])
        mul_op_desc.set_output("Out", ["out"])
        mul_op_desc.set_attr("x_num_col_dims", 1)
        mul_op_desc.set_attr("y_num_col_dims", 1)

        mul_op_desc.check_attrs()
        mul_op_desc.infer_shape(block)
        self.assertEqual(out.shape(), [x_shape[0], y_shape[1]])
Exemplo n.º 4
0
 def test_dtype(self):
     program_desc = core.ProgramDesc()
     block = program_desc.block(0)
     var = block.var('my_var')
     var.set_type(core.VarDesc.VarType.LOD_TENSOR)
     var.set_dtype(core.DataType.INT32)
     self.assertEqual(core.DataType.INT32, var.dtype())
     self.assertEqual(core.VarDesc.VarType.LOD_TENSOR, var.type())
Exemplo n.º 5
0
    def test_op_desc(self):
        prog = core.ProgramDesc()
        self.assertIsNotNone(prog)
        block = prog.block(0)
        self.assertIsNotNone(block)
        op = block.append_op()
        self.assertIsNotNone(op)
        op.set_type("test")
        self.assertEqual("test", op.type())
        op.set_input("X", ["a", "b", "c"])
        self.assertEqual(["a", "b", "c"], op.input("X"))
        self.assertEqual(["X"], op.input_names())

        op.set_output("Out", ["z"])
        self.assertEqual(['z'], op.output("Out"))
        self.assertEqual(["Out"], op.output_names())

        op.set_attr("int_attr", 1)
        self.assertEqual(1, op.attr("int_attr"))
        self.assertTrue(op.has_attr("int_attr"))
        self.assertEqual(core.AttrType.INT, op.attr_type("int_attr"))

        op.set_attr("float_attr", -1.32)
        self.assertAlmostEqual(-1.32, op.attr("float_attr"), delta=1e-4)
        self.assertTrue(op.has_attr("float_attr"))

        op.set_attr("bool_attr", False)
        self.assertFalse(op.attr("bool_attr"))

        op.set_attr("string_attr", "abc")
        self.assertEqual("abc", op.attr("string_attr"))
        self.assertTrue(op.has_attr("string_attr"))

        op.set_attr("ints_attr", [1, 2, 3])
        self.assertEqual([1, 2, 3], op.attr("ints_attr"))

        expected = [1.2, 2.3, 3.4]
        op.set_attr("floats_attr", expected)
        for e, a in zip(expected, op.attr("floats_attr")):
            self.assertAlmostEqual(e, a, delta=1e-4)

        op.set_attr("strings_attr", ["a", "b", "c"])
        self.assertEqual(["a", "b", "c"], op.attr("strings_attr"))

        op.set_attr("bools_attr", [True, False, True])
        self.assertEqual([True, False, True], op.attr("bools_attr"))

        self.assertEqual(8, len(op.attr_names()))

        op.set_block_attr("block_attr", prog.block(0))
        self.assertEqual(0, op.block_attr("block_attr"))

        mul_op = block.append_op()
        mul_op.set_type("mul")
        mul_op.check_attrs()
        self.assertEqual(mul_op.attr("x_num_col_dims"), 1)
        self.assertEqual(mul_op.attr("y_num_col_dims"), 1)
Exemplo n.º 6
0
 def test_shape(self):
     program_desc = core.ProgramDesc()
     block = program_desc.block(0)
     var = block.var('my_var')
     var.set_type(core.VarDesc.VarType.SELECTED_ROWS)
     src_shape = [3, 2, 10, 8]
     var.set_shape(src_shape)
     res_shape = var.shape()
     self.assertEqual(src_shape, res_shape)
     self.assertEqual(core.VarDesc.VarType.SELECTED_ROWS, var.type())
Exemplo n.º 7
0
 def test_add_op(self):
     prog = core.ProgramDesc()
     self.assertIsNotNone(prog)
     block = prog.block(0)
     self.assertIsNotNone(block)
     op1 = block.append_op()
     op2 = block.append_op()
     op0 = block.prepend_op()
     all_ops = []
     for idx in xrange(0, block.op_size()):
         all_ops.append(block.op(idx))
     self.assertEqual(all_ops, [op0, op1, op2])
Exemplo n.º 8
0
 def test_add_var(self):
     prog = core.ProgramDesc()
     self.assertIsNotNone(prog)
     block = prog.block(0)
     self.assertIsNotNone(block)
     var1 = block.var("var1")
     var2 = block.var("var2")
     var3 = block.var("var3")
     all_vars = block.all_vars()
     self.assertEqual(set(all_vars), {var1, var2, var3})
     var2_re = block.find_var("var2")
     self.assertEqual(var2_re, var2)
Exemplo n.º 9
0
 def test_append_block(self):
     prog_desc = core.ProgramDesc()
     self.assertIsNotNone(prog_desc)
     block_root = prog_desc.block(0)
     self.assertIsNotNone(block_root)
     self.assertEqual(block_root.id, 0)
     block1 = prog_desc.append_block(block_root)
     block2 = prog_desc.append_block(block1)
     self.assertIsNotNone(block1)
     self.assertEqual(block1.id, block2.parent)
     self.assertEqual(block_root.id, block1.parent)
     block3 = prog_desc.append_block(block_root)
     self.assertEqual(block3.parent, block_root.id)
     self.assertEqual(prog_desc.block(1).id, 1)
     self.assertEqual(4, prog_desc.num_blocks())