test_infer_shape.py 2.6 KB
Newer Older
D
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
Q
qiaolongfei 已提交
14
import unittest
Y
Yu Yang 已提交
15

Q
Qiao Longfei 已提交
16
import paddle.v2.fluid.core as core
Q
qiaolongfei 已提交
17 18 19 20


class TestInferShape(unittest.TestCase):
    def test_sum_op(self):
21
        prog = core.ProgramDesc()
Q
qiaolongfei 已提交
22 23 24 25
        self.assertIsNotNone(prog)
        block = prog.block(0)
        self.assertIsNotNone(block)

Q
qiaolongfei 已提交
26 27
        shape = [10, 20]

Q
qiaolongfei 已提交
28
        # prepare input/output
D
dongzhihong 已提交
29
        x1 = block.var("x1")
Y
Yu Yang 已提交
30
        x1.set_type(core.VarDesc.VarType.LOD_TENSOR)
Q
qiaolongfei 已提交
31
        x1.set_shape(shape)
D
dongzhihong 已提交
32
        x2 = block.var("x2")
Y
Yu Yang 已提交
33
        x2.set_type(core.VarDesc.VarType.LOD_TENSOR)
Q
qiaolongfei 已提交
34
        x2.set_shape(shape)
Q
qiaolongfei 已提交
35

D
dongzhihong 已提交
36
        out = block.var("out")
Y
Yu Yang 已提交
37
        out.set_type(core.VarDesc.VarType.LOD_TENSOR)
Q
qiaolongfei 已提交
38 39 40 41 42 43 44

        # 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 已提交
45
        sum_op_desc.check_attrs()
Y
Yu Yang 已提交
46
        sum_op_desc.infer_shape(block)
Q
qiaolongfei 已提交
47 48 49
        self.assertEqual(out.shape(), shape)

    def test_mul_op(self):
50
        prog = core.ProgramDesc()
Q
qiaolongfei 已提交
51 52 53 54 55 56 57 58
        self.assertIsNotNone(prog)
        block = prog.block(0)
        self.assertIsNotNone(block)

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

        # prepare input/output
D
dongzhihong 已提交
59
        x1 = block.var("x")
Y
Yu Yang 已提交
60
        x1.set_type(core.VarDesc.VarType.LOD_TENSOR)
Q
qiaolongfei 已提交
61
        x1.set_shape(x_shape)
D
dongzhihong 已提交
62
        x2 = block.var("y")
Y
Yu Yang 已提交
63
        x2.set_type(core.VarDesc.VarType.LOD_TENSOR)
Q
qiaolongfei 已提交
64 65
        x2.set_shape(y_shape)

D
dongzhihong 已提交
66
        out = block.var("out")
Y
Yu Yang 已提交
67
        out.set_type(core.VarDesc.VarType.LOD_TENSOR)
Q
qiaolongfei 已提交
68 69 70 71 72 73 74 75 76 77

        # 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 已提交
78
        mul_op_desc.check_attrs()
Y
Yu Yang 已提交
79
        mul_op_desc.infer_shape(block)
Q
qiaolongfei 已提交
80 81 82 83 84
        self.assertEqual(out.shape(), [x_shape[0], y_shape[1]])


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