cpp_pass.py 5.2 KB
Newer Older
1
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2
#
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
6
#
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
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 paddle.framework import _apply_pass as _apply_cpp_pass
from paddle.framework import core
17
from paddle.static import Executor
18 19

from .pass_base import CPPPassWrapper, PassType, register_pass
20 21 22 23 24


@register_pass("fuse_elewise_add_act")
class FuseElementwiseAddActPass(CPPPassWrapper):
    def __init__(self):
25
        super().__init__()
26 27 28 29

    @property
    def cpp_name(self):
        return "fuse_elewise_add_act_pass"
30 31 32

    def _type(self):
        return PassType.FUSION_OPT
33 34 35 36 37


@register_pass("fuse_bn_act")
class FuseBatchNormActPass(CPPPassWrapper):
    def __init__(self):
38
        super().__init__()
39 40 41 42 43 44 45

    @property
    def cpp_name(self):
        return "fuse_bn_act_pass"

    def _type(self):
        return PassType.FUSION_OPT
46 47 48 49 50


@register_pass("fuse_bn_add_act")
class FuseBatchNormAddActPass(CPPPassWrapper):
    def __init__(self):
51
        super().__init__()
52 53 54 55 56 57 58

    @property
    def cpp_name(self):
        return "fuse_bn_add_act_pass"

    def _type(self):
        return PassType.FUSION_OPT
59 60 61 62 63


@register_pass("fuse_relu_depthwise_conv")
class FuseReluDepthwiseConvPass(CPPPassWrapper):
    def __init__(self):
64
        super().__init__()
65 66 67 68 69 70 71

    @property
    def cpp_name(self):
        return "fuse_relu_depthwise_conv_pass"

    def _type(self):
        return PassType.FUSION_OPT
72 73


74 75 76 77 78 79 80 81 82 83 84 85 86
@register_pass("fused_attention")
class FusedAttentionPass(CPPPassWrapper):
    def __init__(self):
        super().__init__()

    @property
    def cpp_name(self):
        return "fused_attention_pass"

    def _type(self):
        return PassType.FUSION_OPT


S
Shijie 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99
@register_pass("fuse_gemm_epilogue")
class FuseGemmEpiloguePass(CPPPassWrapper):
    def __init__(self):
        super().__init__()

    @property
    def cpp_name(self):
        return "fuse_gemm_epilogue_pass"

    def _type(self):
        return PassType.FUSION_OPT


100 101 102
@register_pass("fuse_optimizer")
class FuseOptimizerPass(CPPPassWrapper):
    def __init__(self):
103
        super().__init__()
104 105 106 107

    @property
    def cpp_name(self):
        return [
108 109 110
            "fuse_adam_op_pass",
            "fuse_sgd_op_pass",
            "fuse_momentum_op_pass",
111 112 113 114
        ]

    def _type(self):
        return PassType.FUSION_OPT
115 116 117 118 119


@register_pass("inplace_addto_op")
class InplaceAddtoOpPass(CPPPassWrapper):
    def __init__(self):
120
        super().__init__()
121 122 123 124 125 126 127

    @property
    def cpp_name(self):
        return "inplace_addto_op_pass"

    def _type(self):
        return PassType.CALC_OPT
S
sneaxiy 已提交
128 129


130 131 132 133 134 135 136
def _set_cinn_op_flag(flag_name, extra_ops):
    values = core.globals()[flag_name]
    values = [v.strip() for v in values.split(";") if v.strip()]
    values.extend(extra_ops)
    core.globals()[flag_name] = ";".join(values)


S
sneaxiy 已提交
137 138 139
@register_pass("build_cinn")
class BuildCINNPass(CPPPassWrapper):
    def __init__(self):
140
        super().__init__()
S
sneaxiy 已提交
141 142 143 144 145 146 147 148 149 150 151 152
        self.set_attr("allow_ops", [])
        self.set_attr("deny_ops", [])

    @property
    def cpp_name(self):
        return "build_cinn_pass"

    def _type(self):
        return PassType.CALC_OPT

    def _apply_single_impl(self, main_program, startup_program, context):

153 154
        assert (
            'FLAGS_allow_cinn_ops' in core.globals()
S
sneaxiy 已提交
155 156 157 158
        ), "PaddlePaddle is not compiled with CINN support"
        old_allow_ops = core.globals()['FLAGS_allow_cinn_ops']
        old_deny_ops = core.globals()['FLAGS_deny_cinn_ops']
        try:
159 160 161
            _set_cinn_op_flag(
                'FLAGS_allow_cinn_ops', self.get_attr("allow_ops")
            )
162 163 164 165 166 167 168 169
            _set_cinn_op_flag('FLAGS_deny_cinn_ops', self.get_attr("deny_ops"))

            feed = self.get_attr('feed', [])
            fetch_list = self.get_attr('fetch_list', [])
            prune_program = self.get_attr('prune_program', True)

            if prune_program:
                tmp_main_program = Executor._prune_program(
170 171
                    main_program, feed, fetch_list, []
                )
172 173

                tmp_main_program = Executor._add_fetch_ops(
174 175
                    tmp_main_program, fetch_list, 'fetch'
                )
176 177 178 179

            else:

                tmp_main_program = Executor._add_fetch_ops(
180 181 182 183 184 185 186 187 188 189
                    main_program, fetch_list, 'fetch'
                )

            _apply_cpp_pass(
                tmp_main_program,
                startup_program,
                self.cpp_name,
                {},
                self.cpp_attr_types,
            )
190 191 192 193 194 195 196

            tmp_main_program = Executor._remove_fetch_ops(tmp_main_program)

            tmp_main_program = core.ProgramDesc(tmp_main_program.desc)

            main_program._rebuild_from_desc(tmp_main_program)

S
sneaxiy 已提交
197 198 199
        finally:
            core.globals()['FLAGS_allow_cinn_ops'] = old_allow_ops
            core.globals()['FLAGS_deny_cinn_ops'] = old_deny_ops