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

Q
qiaolongfei 已提交
3 4 5 6 7
import paddle.v2.framework.core as core


class TestInferShape(unittest.TestCase):
    def test_sum_op(self):
8
        prog = core.ProgramDesc()
Q
qiaolongfei 已提交
9 10 11 12
        self.assertIsNotNone(prog)
        block = prog.block(0)
        self.assertIsNotNone(block)

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

Q
qiaolongfei 已提交
15
        # prepare input/output
D
dongzhihong 已提交
16
        x1 = block.var("x1")
Y
Yu Yang 已提交
17
        x1.set_type(core.VarDesc.VarType.LOD_TENSOR)
Q
qiaolongfei 已提交
18
        x1.set_shape(shape)
D
dongzhihong 已提交
19
        x2 = block.var("x2")
Y
Yu Yang 已提交
20
        x2.set_type(core.VarDesc.VarType.LOD_TENSOR)
Q
qiaolongfei 已提交
21
        x2.set_shape(shape)
Q
qiaolongfei 已提交
22

D
dongzhihong 已提交
23
        out = block.var("out")
Y
Yu Yang 已提交
24
        out.set_type(core.VarDesc.VarType.LOD_TENSOR)
Q
qiaolongfei 已提交
25 26 27 28 29 30 31

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

Q
QI JUN 已提交
32
        sum_op_desc.check_attrs()
Y
Yu Yang 已提交
33
        sum_op_desc.infer_shape(block)
Q
qiaolongfei 已提交
34 35 36
        self.assertEqual(out.shape(), shape)

    def test_mul_op(self):
37
        prog = core.ProgramDesc()
Q
qiaolongfei 已提交
38 39 40 41 42 43 44 45
        self.assertIsNotNone(prog)
        block = prog.block(0)
        self.assertIsNotNone(block)

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

        # prepare input/output
D
dongzhihong 已提交
46
        x1 = block.var("x")
Y
Yu Yang 已提交
47
        x1.set_type(core.VarDesc.VarType.LOD_TENSOR)
Q
qiaolongfei 已提交
48
        x1.set_shape(x_shape)
D
dongzhihong 已提交
49
        x2 = block.var("y")
Y
Yu Yang 已提交
50
        x2.set_type(core.VarDesc.VarType.LOD_TENSOR)
Q
qiaolongfei 已提交
51 52
        x2.set_shape(y_shape)

D
dongzhihong 已提交
53
        out = block.var("out")
Y
Yu Yang 已提交
54
        out.set_type(core.VarDesc.VarType.LOD_TENSOR)
Q
qiaolongfei 已提交
55 56 57 58 59 60 61 62 63 64

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

Q
QI JUN 已提交
65
        mul_op_desc.check_attrs()
Y
Yu Yang 已提交
66
        mul_op_desc.infer_shape(block)
Q
qiaolongfei 已提交
67 68 69 70 71
        self.assertEqual(out.shape(), [x_shape[0], y_shape[1]])


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