test_ir_pybind.py 6.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
#
# 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.

import unittest

import paddle
18
from paddle import ir
19 20 21 22 23 24 25 26 27 28 29 30 31 32

paddle.enable_static()


def get_ir_program():
    x = paddle.randn([4, 4])
    main_program, start_program = (
        paddle.static.Program(),
        paddle.static.Program(),
    )
    with paddle.static.program_guard(main_program, start_program):
        x_s = paddle.static.data('x', [4, 4], x.dtype)
        x_s.stop_gradient = False
        y_s = paddle.matmul(x_s, x_s)
33 34
        z_s = paddle.add(y_s, y_s)
        k_s = paddle.tanh(z_s)
35
    newir_program = ir.translate_to_new_ir(main_program.desc)
36 37 38 39 40 41
    return newir_program


class TestPybind(unittest.TestCase):
    def test_program(self):
        newir_program = get_ir_program()
42
        print(newir_program)
43

44 45 46 47 48
        block = newir_program.block()
        program = block.get_parent_program()

        self.assertEqual(newir_program, program)

49 50 51 52
    def test_block(self):
        newir_program = get_ir_program()
        block = newir_program.block()
        ops = block.get_ops()
53
        self.assertEqual(
54 55
            len(ops), 4
        )  # ir program add "builtin.get_parameter" by default, so size is 4
56
        block.remove_op(ops[3])
57
        self.assertEqual(len(block.get_ops()), 3)
58 59 60 61 62 63 64

    def test_operation(self):
        newir_program = get_ir_program()
        ops = newir_program.block().get_ops()
        matmul_op = newir_program.block().get_ops()[1]
        add_op = newir_program.block().get_ops()[2]
        tanh_op = newir_program.block().get_ops()[3]
65
        parent_block = tanh_op.get_parent_block()
66
        parent_ops_num = len(parent_block.get_ops())
67 68 69 70 71
        self.assertEqual(parent_ops_num, 4)
        self.assertEqual(tanh_op.num_results(), 1)
        self.assertEqual(len(matmul_op.get_input_names()), 2)
        self.assertEqual(len(matmul_op.get_attr_names()), 2)
        self.assertEqual(len(matmul_op.get_output_names()), 1)
72 73 74 75 76 77

    def test_value(self):
        newir_program = get_ir_program()
        matmul_op = newir_program.block().get_ops()[1]
        add_op = newir_program.block().get_ops()[2]
        tanh_op = newir_program.block().get_ops()[3]
78 79 80 81 82

        self.assertEqual(
            matmul_op.result(0).dtype, paddle.fluid.core.DataType.FLOAT32
        )
        self.assertEqual(matmul_op.result(0).shape, [4, 4])
83
        self.assertEqual(
84 85
            matmul_op.results()[0].get_defining_op().name(), "pd.matmul"
        )
86
        self.assertEqual(
87 88
            matmul_op.result(0).get_defining_op().name(), "pd.matmul"
        )
89 90
        matmul_op.result(0).stop_gradient = True
        self.assertEqual(matmul_op.result(0).stop_gradient, True)
91

X
xiaoguoguo626807 已提交
92
        # test opresult hash
93 94 95
        result_set = set()
        for opresult in matmul_op.results():
            result_set.add(opresult)
X
xiaoguoguo626807 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
        # test opresult hash and hash(opresult) == hash(operesult)
        self.assertTrue(add_op.operands()[0].source() in result_set)
        # test value hash and hash(value) == hash(operesult)
        self.assertTrue(add_op.operands_source()[0] in result_set)
        # test value == value
        self.assertEqual(
            add_op.operands_source()[0], add_op.operands_source()[0]
        )
        # test value == opresult
        self.assertEqual(add_op.operands_source()[0], matmul_op.results()[0])
        # test opresult == value
        self.assertEqual(
            add_op.operands()[0].source(), add_op.operands_source()[0]
        )
        # test opresult == opresult
        self.assertEqual(add_op.operands()[0].source(), matmul_op.results()[0])
112

113 114
        self.assertEqual(
            tanh_op.operands()[0].source().get_defining_op().name(), "pd.add"
115 116 117
        )

        add_op.replace_all_uses_with(matmul_op.results())
118 119
        self.assertEqual(
            tanh_op.operands()[0].source().get_defining_op().name(), "pd.matmul"
120
        )
121

122
        self.assertEqual(add_op.result(0).use_empty(), True)
123 124 125 126 127

    def test_type(self):
        newir_program = get_ir_program()
        matmul_op = newir_program.block().get_ops()[1]
        add_op = newir_program.block().get_ops()[2]
128 129
        print(matmul_op.result(0).type())
        self.assertEqual(
130 131 132
            matmul_op.result(0).type() == add_op.result(0).type(), True
        )

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    def test_attr(self):
        main_program, start_program = (
            paddle.static.Program(),
            paddle.static.Program(),
        )
        with paddle.static.program_guard(main_program, start_program):
            conv_data = paddle.static.data(
                'conv_data', [None, 3, 32, 32], dtype='float32'
            )
            conv2d_out = paddle.static.nn.conv2d(
                input=conv_data,
                num_filters=2,
                filter_size=3,
                stride=3,
                act="relu",
            )
            full_out = paddle.tensor.fill_constant(
                shape=[4, 4], dtype="float32", value=2
            )

        newir_program = ir.translate_to_new_ir(main_program.desc)
        print(newir_program)
        conv_attr = newir_program.block().get_ops()[3].attrs()
        full_attr = newir_program.block().get_ops()[8].attrs()
        self.assertEqual(conv_attr["stop_gradient"], [False])
        self.assertEqual(conv_attr["dilations"], [1, 1])
        self.assertEqual(conv_attr["data_format"], "NCHW")
        self.assertEqual(conv_attr["strides"], [3, 3])
        self.assertEqual(conv_attr["paddings"], [0, 0])
        self.assertEqual(conv_attr["padding_algorithm"], "EXPLICIT")
        self.assertEqual(conv_attr["groups"], 1)
        self.assertEqual(full_attr["dtype"], paddle.fluid.core.DataType.FLOAT32)
        self.assertTrue(isinstance(full_attr["place"], paddle.fluid.core.Place))
166

167 168 169 170 171 172 173 174 175 176 177 178
    def test_operands(self):
        newir_program = get_ir_program()
        matmul_op = newir_program.block().get_ops()[1]
        operands = matmul_op.operands()
        self.assertEqual(len(operands), 2)

    def test_results(self):
        newir_program = get_ir_program()
        matmul_op = newir_program.block().get_ops()[1]
        results = matmul_op.results()
        self.assertEqual(len(results), 1)

179 180 181

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