test_infer_shape.py 2.7 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.

15 16
from __future__ import print_function

Q
qiaolongfei 已提交
17
import unittest
Y
Yu Yang 已提交
18

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


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

Q
qiaolongfei 已提交
30 31
        shape = [10, 20]

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

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

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

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

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

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

M
minqiyang 已提交
70
        out = block.var(six.b("out"))
Y
Yu Yang 已提交
71
        out.set_type(core.VarDesc.VarType.LOD_TENSOR)
Q
qiaolongfei 已提交
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"])
W
Wu Yi 已提交
79 80
        mul_op_desc._set_attr("x_num_col_dims", 1)
        mul_op_desc._set_attr("y_num_col_dims", 1)
Q
qiaolongfei 已提交
81

Q
QI JUN 已提交
82
        mul_op_desc.check_attrs()
Y
Yu Yang 已提交
83
        mul_op_desc.infer_shape(block)
Q
qiaolongfei 已提交
84 85 86 87 88
        self.assertEqual(out.shape(), [x_shape[0], y_shape[1]])


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