test_infer_shape.py 2.6 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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 已提交
15
import unittest
Y
Yu Yang 已提交
16

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


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

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

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

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

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

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

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

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

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

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


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