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

M
minqiyang 已提交
17
import six
18
import paddle.fluid.core as core
Q
qiaolongfei 已提交
19 20 21 22


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

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

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

M
minqiyang 已提交
38
        out = block.var(six.b("out"))
Y
Yu Yang 已提交
39
        out.set_type(core.VarDesc.VarType.LOD_TENSOR)
Q
qiaolongfei 已提交
40 41 42 43 44 45 46

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

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

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

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

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

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


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