test_infer_shape.py 1.7 KB
Newer Older
Q
qiaolongfei 已提交
1
import unittest
Y
Yu Yang 已提交
2

Q
qiaolongfei 已提交
3 4 5 6 7 8 9 10 11 12
import paddle.v2.framework.core as core


class TestInferShape(unittest.TestCase):
    def test_sum_op(self):
        prog = core.ProgramDesc.__create_program_desc__()
        self.assertIsNotNone(prog)
        block = prog.block(0)
        self.assertIsNotNone(block)

Q
qiaolongfei 已提交
13 14
        shape = [10, 20]

Q
qiaolongfei 已提交
15 16
        # prepare input/output
        x1 = block.new_var("x1")
Q
qiaolongfei 已提交
17
        x1.set_shape(shape)
Q
qiaolongfei 已提交
18
        x2 = block.new_var("x2")
Q
qiaolongfei 已提交
19
        x2.set_shape(shape)
Q
qiaolongfei 已提交
20 21 22 23 24 25 26 27 28

        out = block.new_var("out")

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

Y
Yu Yang 已提交
29
        sum_op_desc.infer_shape(block)
Q
qiaolongfei 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
        self.assertEqual(out.shape(), shape)

    def test_mul_op(self):
        prog = core.ProgramDesc.__create_program_desc__()
        self.assertIsNotNone(prog)
        block = prog.block(0)
        self.assertIsNotNone(block)

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

        # prepare input/output
        x1 = block.new_var("x")
        x1.set_shape(x_shape)
        x2 = block.new_var("y")
        x2.set_shape(y_shape)

        out = block.new_var("out")

        # 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)

Y
Yu Yang 已提交
58
        mul_op_desc.infer_shape(block)
Q
qiaolongfei 已提交
59 60 61 62 63
        self.assertEqual(out.shape(), [x_shape[0], y_shape[1]])


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