pass_base.py 10.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 15
# 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.

from abc import ABC, abstractmethod
16

17
from paddle.framework import _apply_pass as _apply_cpp_pass
18 19 20 21 22


class PassContext:
    def __init__(self):
        self._applied_passes = []
S
sneaxiy 已提交
23
        self._attrs = {}
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

    def set_attr(self, key, value):
        self._attrs[key] = value

    def get_attr(self, key, default=None):
        return self._attrs.get(key, default)

    @property
    def passes(self):
        return self._applied_passes

    def _add_pass(self, pass_obj):
        self._applied_passes.append(pass_obj)

    def _pop_pass(self):
        del self._applied_passes[-1]


42 43 44 45 46 47 48 49
class PassType:
    UNKNOWN = 0
    COMM_OPT = 1
    CALC_OPT = 2
    PARALLEL_OPT = 3
    FUSION_OPT = 4


50 51 52
class PassBase(ABC):
    _REGISTERED_PASSES = {}
    _COMMON_RULES = []
S
sneaxiy 已提交
53 54 55

    _BEFORE_WHITE_LISTS_DICT = {}
    _AFTER_WHITE_LISTS_DICT = {}
56
    _PASS_PROCESS_ORDER_LIST = []
57 58

    name = None
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

    @staticmethod
    def _register(pass_name, pass_class):
        assert issubclass(pass_class, PassBase)
        PassBase._REGISTERED_PASSES[pass_name] = pass_class

    def __init__(self):
        self._attrs = {}

    def set_attr(self, key, value):
        self._attrs[key] = value
        return self

    def get_attr(self, key, default=None):
        return self._attrs.get(key, default)

    @abstractmethod
    def _check_self(self):
        pass

    @abstractmethod
    def _check_conflict(self, other_pass):
        pass

83 84 85
    def _type(self):
        return PassType.UNKNOWN

86 87
    def _check_conflict_including_common_rules(self, other_pass):
        return self._check_conflict(other_pass) and all(
88 89
            [r(other_pass, self) for r in PassBase._COMMON_RULES]
        )
90 91 92 93 94 95 96 97

    def apply(self, main_programs, startup_programs, context=None):
        if context is None:
            context = PassContext()

        if not self._check_self():
            return context

98 99
        if not all(
            [
100 101
                self._check_conflict_including_common_rules(p)
                for p in context.passes
102 103
            ]
        ):
104 105 106 107 108 109 110 111 112 113
            return context

        assert isinstance(main_programs, list)
        assert isinstance(startup_programs, list)
        assert len(main_programs) == len(startup_programs)
        self._apply_impl(main_programs, startup_programs, context)
        context._add_pass(self)
        return context

    def _apply_impl(self, main_programs, startup_programs, context):
114 115 116
        for main_program, startup_program in zip(
            main_programs, startup_programs
        ):
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
            self._apply_single_impl(main_program, startup_program, context)

    @abstractmethod
    def _apply_single_impl(self, main_program, startup_program, context):
        pass


def register_pass(name):
    def impl(cls):
        PassBase._register(name, cls)
        cls.name = name
        return cls

    return impl


def new_pass(name, pass_attrs={}):
    pass_class = PassBase._REGISTERED_PASSES.get(name)
    assert pass_class is not None, "Pass {} is not registered".format(name)
    pass_obj = pass_class()
    for k, v in pass_attrs.items():
        pass_obj.set_attr(k, v)
    return pass_obj


class CPPPassWrapper(PassBase):
    def __init__(self):
144
        super().__init__()
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

    @property
    def cpp_name(self):
        raise NotImplementedError()

    @property
    def cpp_attr_types(self):
        return {}

    def _check_self(self):
        return True

    def _check_conflict(self, other_pass):
        return True

    def _apply_single_impl(self, main_program, startup_program, context):
161 162 163 164 165 166 167
        _apply_cpp_pass(
            main_program,
            startup_program,
            self.cpp_name,
            self._attrs,
            self.cpp_attr_types,
        )
168 169


170
def _fusion_opt_last_rule(pass_before, pass_after):
171 172 173 174
    if (
        pass_before._type() == PassType.FUSION_OPT
        and pass_after._type() != PassType.FUSION_OPT
    ):
175 176
        return False
    else:
177 178 179
        return True


180 181 182 183 184 185 186 187 188 189
def _fusion_opt_list_rule(pass_before, pass_after):
    if (
        pass_before._type() == PassType.FUSION_OPT
        and pass_after._type() == PassType.FUSION_OPT
    ):
        return _get_list_index(pass_before) < _get_list_index(pass_after)
    else:
        return True


190 191 192
def _make_rule_from_white_lists_dict(
    before_white_lists_dict, after_white_lists_dict
):
S
sneaxiy 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    def collect_pass_names(white_lists_dict, result):
        for k, v in white_lists_dict.items():
            result.add(k)
            assert isinstance(v, (list, tuple))
            for pass_name in v:
                assert isinstance(pass_name, (bytes, str))
                result.add(pass_name)

    all_pass_names = set()
    collect_pass_names(before_white_lists_dict, all_pass_names)
    collect_pass_names(after_white_lists_dict, all_pass_names)

    compatible_pass_dict = {}
    for pass_name in all_pass_names:
        compatible_pass_dict[pass_name] = set()

    for k, v in before_white_lists_dict.items():
        for pass_name in v:
            compatible_pass_dict[k].add(pass_name)

    for k, v in after_white_lists_dict.items():
        for pass_name in v:
            compatible_pass_dict[pass_name].add(k)

    def rule(pass_before, pass_after):
        all_passes_after = compatible_pass_dict.get(pass_before.name)
219 220 221 222
        if (
            all_passes_after is None
            or pass_after.name not in compatible_pass_dict
        ):
S
sneaxiy 已提交
223 224 225 226 227 228 229
            return True
        else:
            return pass_after.name in all_passes_after

    return rule


230 231 232 233 234 235 236
def _get_list_index(in_pass):
    assert (
        in_pass.name in PassBase._PASS_PROCESS_ORDER_LIST
    ), "Pass {} is not in _PASS_PROCESS_ORDER_LIST".format(in_pass.name)
    return PassBase._PASS_PROCESS_ORDER_LIST.index(in_pass.name)


237 238
# The key-value pair (k, [v1, v2, ..., vn]) means the pass k can be
# applied before any of pass [v1, v2, ..., vn] is applied
S
sneaxiy 已提交
239 240 241 242 243 244 245 246
PassBase._BEFORE_WHITE_LISTS_DICT = {
    "fuse_gradient_merge": ["fuse_all_reduce"],
    # Add more white lists here
}

# The key-value pair (k, [v1, v2, ..., vn]) means the pass k can be
# applied after any of pass [v1, v2, ..., vn] is applied
PassBase._AFTER_WHITE_LISTS_DICT = {
247
    # Add more white lists here
S
sneaxiy 已提交
248 249
}

250 251 252 253 254 255 256 257 258 259
# The index of pass in this list represent the order in which the pass is processed.
PassBase._PASS_PROCESS_ORDER_LIST = [
    "fuse_relu_depthwise_conv",
    "fuse_bn_add_act",
    "fuse_bn_act",
    "fused_attention",
    "fuse_gemm_epilogue",
    "fuse_optimizer",
]

260
PassBase._COMMON_RULES = [
261
    _fusion_opt_last_rule,
262
    _fusion_opt_list_rule,
263
    lambda pass_before, pass_after: type(pass_before) != type(pass_after),
264 265 266
    _make_rule_from_white_lists_dict(
        PassBase._BEFORE_WHITE_LISTS_DICT, PassBase._AFTER_WHITE_LISTS_DICT
    ),
267
    # Add more common rules here
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
]


def _find_longest_path(edges):
    n = len(edges)
    paths = [None] * n
    dists = [None] * n

    min_path = []
    min_dist = 0
    for i in range(n):
        paths[i] = [None] * n
        dists[i] = [None] * n
        for j in range(n):
            assert isinstance(edges[i][j], bool)
            if not edges[i][j]:
                dists[i][j] = n  # inf
                paths[i][j] = []
            else:
                assert edges[i][j] is True
                dists[i][j] = -1
                paths[i][j] = [i, j]
                if dists[i][j] < min_dist:
                    min_dist = -1
                    min_path = paths[i][j]

    for k in range(n):
        for i in range(n):
            for j in range(n):
                if dists[i][j] > dists[i][k] + dists[k][j]:
                    dists[i][j] = dists[i][k] + dists[k][j]
299 300 301 302 303 304 305 306 307 308 309
                    if paths[i][k]:
                        assert paths[i][k][-1] == k
                    else:
                        continue
                    if paths[k][j]:
                        assert paths[k][j][0] == k
                    else:
                        continue
                    paths[i][j] = (
                        paths[i][k] + paths[k][j][1:] if paths[k][j] else []
                    )
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
                    if dists[i][j] < min_dist:
                        min_dist = dists[i][j]
                        min_path = paths[i][j]

    return min_path if min_path else [0]


def _solve_pass_conflict(passes, context):
    passes = [p for p in passes if p._check_self()]
    if not passes:
        return []

    old_passes = passes
    passes = []
    for p in old_passes:
325 326
        if all(
            [
327 328
                p._check_conflict_including_common_rules(applied_p)
                for applied_p in context.passes
329 330
            ]
        ):
331 332 333 334 335 336 337 338 339 340 341 342 343
            passes.append(p)

    if not passes:
        return []

    n = len(passes)
    adjacent_matrix = []
    for _ in range(n):
        adjacent_matrix.append([None] * n)

    for i in range(n):
        for j in range(n):
            adjacent_matrix[i][j] = passes[
344 345
                j
            ]._check_conflict_including_common_rules(passes[i])
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374

    longest_path = _find_longest_path(adjacent_matrix)
    return [passes[idx] for idx in longest_path]


class PassManager:
    def __init__(self, passes, context=None, auto_solve_conflict=True):
        if context is None:
            context = PassContext()
        self._context = context

        if auto_solve_conflict:
            self._passes = _solve_pass_conflict(passes, context)
        else:
            self._passes = list(passes)

    def apply(self, main_programs, startup_programs):
        context = self._context
        for p in self._passes:
            context = p.apply(main_programs, startup_programs, context)
        self._context = context
        return context

    @property
    def context(self):
        return self._context

    @property
    def names(self):
S
sneaxiy 已提交
375 376 377 378 379
        return [p.name for p in self.passes]

    @property
    def passes(self):
        return tuple(self._passes)