prelu_fuser.py 5.6 KB
Newer Older
S
SunAhong1993 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
#   Copyright (c) 2020  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 copy
import numpy as np
from collections import OrderedDict
from x2paddle.optimizer.pattern_matcher import FuseBase
from x2paddle.core.program import PaddleGraph, PaddleLayer
from x2paddle.core.util import *


class StaticPReLUFuser(FuseBase):
    def __init__(self):
        super(StaticPReLUFuser, self).__init__(graph_type="static")

    def build_pattern(self):
        """ 描述需要替换的prelu图结构。
        prelu层模式python实现代码示例:
            conv4_alphas = paddle.static.create_parameter(dtype='float32', shape=[128], name='conv4_alphas', default_initializer=paddle.nn.initializer.Constant(value=0.0))
            conv4_mul_1_y = paddle.full(dtype='float32', shape=[1], fill_value=0.5)
            conv4_Relu = paddle.nn.functional.relu(x=conv4_BiasAdd)
            conv4_Abs = paddle.abs(x=conv4_BiasAdd)
            conv4_sub = fluid.layers.elementwise_sub(x=conv4_BiasAdd, y=conv4_Abs)
            conv4_mul = paddle.multiply(x=conv4_alphas, y=conv4_sub)
            conv4_mul_1 = paddle.multiply(x=conv4_mul, y=conv4_mul_1_y)
            conv4_add = paddle.add(x=conv4_Relu, y=conv4_mul_1)
        """

        def gen_name(id):
            return "x" + str(id)

        self.pattern.add_layer(
            "paddle.static.create_parameter",
            inputs={},
            outputs=[gen_name(0)])
        self.pattern.add_layer(
            "paddle.full",
            inputs={},
            outputs=[gen_name(1)],
            shape=[1],
            fill_value=0.5)
        self.pattern.add_layer(
            "paddle.nn.functional.relu",
            inputs={"x": "prelu-input-0"},
            outputs=[gen_name(2)])
        self.pattern.add_layer(
            "paddle.abs",
            inputs={"x": "prelu-input-0"},
            outputs=[gen_name(3)])
        self.pattern.add_layer(
            "fluid.layers.elementwise_sub",
            inputs={"x": "prelu-input-0",
                    "y": gen_name(3)},
            outputs=[gen_name(4)])
        self.pattern.add_layer(
            "paddle.multiply",
            inputs={"x": gen_name(0),
                    "y": gen_name(4)},
            outputs=[gen_name(5)])
        self.pattern.add_layer(
            "paddle.multiply",
            inputs={"x": gen_name(5),
                    "y": gen_name(1)},
            outputs=[gen_name(6)])
        self.pattern.add_layer(
            "paddle.add",
            inputs={"x": gen_name(2),
                    "y": gen_name(6)},
            outputs=[gen_name(7)])
        self.pattern.build(inputs={"input-0": "prelu-input-0", })
        

    def insert_new_layer(self, graph, parameters, matches):
        new_layers, last_layer_id = self.gen_new_layer(matches, parameters, graph)
        matches_copy = copy.deepcopy(matches)
        for layer_id, layer in matches_copy.items():
            for i in range(4):
                if layer_id == new_layers[i].id:
                    matches.pop(new_layers[i].id)
        prefix_layers = OrderedDict()
        mid_layers = OrderedDict()
        suffix_layers = OrderedDict()
        is_need_id = False
        for layer_id, layer in graph.layers.items():
            if is_need_id:
                suffix_layers[layer_id] = layer
            else:
                if layer_id == last_layer_id:
                    for i in range(4):
                        mid_layers[new_layers[i].id] = new_layers[i]
                    is_need_id = True
                prefix_layers[layer_id] = layer
        prefix_layers.update(mid_layers)
        prefix_layers.update(suffix_layers)
        graph.layers = prefix_layers

    def gen_new_layer(self, matches, parameters, graph):
        layer_id_list = list(matches.keys())
        layer_id_list.sort(key = int)
        for layer_id, layer in matches.items():
            if layer.kernel == "paddle.nn.functional.relu":
                input_name = layer.inputs["x"]
            if layer.kernel == "paddle.static.create_parameter":
                param_layer = layer
                param_name = layer.outputs[0]
            if layer.kernel == "paddle.add":
                output_name = layer.outputs[0]
        transpose0 = PaddleLayer(
            id=layer_id_list[-1] + "_1",
            kernel="paddle.transpose",
            inputs={"x": input_name},
            outputs=["{}_transpose_for_prelu".format(input_name)],
            perm=[0, 3, 1, 2])
        param = parameters[param_name]
        c = param.shape[0]
        prelu = PaddleLayer(id=layer_id_list[-1] + "_2",
                            kernel="paddle.nn.functional.prelu",
                            inputs={"x": "{}_transpose_for_prelu".format(input_name),
                                    "weight": param_name},
                            outputs=["{}_prelu".format(input_name)])
        transpose1 = PaddleLayer(
            id=layer_id_list[-1] + "_3",
            kernel="paddle.transpose",
            inputs={"x": "{}_prelu".format(input_name)},
            outputs=[output_name],
            perm=[0, 2, 3, 1])
        return [param_layer, transpose0, prelu, transpose1], layer_id_list[-1]