trace_fc_fuser.py 4.9 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
#   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 numpy as np
from x2paddle.optimizer.pattern_matcher import FuseBase
from x2paddle.core.program import PaddleGraph, PaddleLayer
from x2paddle.core.util import *


class TraceFcFuser(FuseBase):
    def __init__(self):
        self.linear_index = 0
S
SunAhong1993 已提交
24
        super(TraceFcFuser, self).__init__()
S
SunAhong1993 已提交
25
        self.patterns = list()
S
SunAhong1993 已提交
26

S
SunAhong1993 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    def build_pattern(self):
        """ 描述需要替换的fc图结构。
        fc层模式python实现代码示例:
           模式一:
           encoder_layer_8_attention_self_key_weight = self.encoder_layer_8_attention_self_key_weight
           x748 = paddle.transpose(x=encoder_layer_8_attention_self_key_weight, perm=[1, 0])
           x749 = paddle.matmul(x=x732, y=x748)
           encoder_layer_8_attention_self_key_bias = self.encoder_layer_8_attention_self_key_bias
           x750 = x749 + 1 * encoder_layer_8_attention_self_key_bias
           模式二:
           x13 = self.x13
           x14 = paddle.transpose(x=x13, perm=[1, 0])
           x15 = self.x15
           x16 = paddle.addmm(input=x15, x=x12, y=x14, beta=1, alpha=1)
        """

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

S
SunAhong1993 已提交
46
        pattern = PaddleGraph()
S
SunAhong1993 已提交
47
        pattern.add_layer(
S
SunAhong1993 已提交
48
            "self.create_parameter", inputs={}, outputs=[gen_name(0)])
S
SunAhong1993 已提交
49
        pattern.add_layer(
S
SunAhong1993 已提交
50
            "paddle.transpose",
S
SunAhong1993 已提交
51 52 53 54 55 56 57 58 59
            inputs={"x": gen_name(0)},
            outputs=[gen_name(1)],
            perm=[1, 0])
        pattern.add_layer(
            "paddle.matmul",
            inputs={"x": "fc-input-0",
                    "y": gen_name(1)},
            outputs=[gen_name(2)])
        pattern.add_layer(
S
SunAhong1993 已提交
60
            "self.create_parameter", inputs={}, outputs=[gen_name(3)])
S
SunAhong1993 已提交
61 62 63 64 65 66 67 68
        pattern.add_layer(
            "prim.add_",
            inputs={"x": gen_name(2),
                    "y": gen_name(3)},
            outputs=[gen_name(4)],
            alpha=1)
        pattern.build(inputs={"input-0": "fc-input-0"})
        self.patterns.append(pattern)
S
SunAhong1993 已提交
69 70

        pattern = PaddleGraph()
S
SunAhong1993 已提交
71
        pattern.add_layer(
S
SunAhong1993 已提交
72
            "self.create_parameter", inputs={}, outputs=[gen_name(0)])
S
SunAhong1993 已提交
73 74 75 76 77 78
        pattern.add_layer(
            "paddle.transpose",
            inputs={"x": gen_name(0)},
            outputs=[gen_name(1)],
            perm=[1, 0])
        pattern.add_layer(
S
SunAhong1993 已提交
79
            "self.create_parameter", inputs={}, outputs=[gen_name(2)])
S
SunAhong1993 已提交
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
        pattern.add_layer(
            "paddle.addmm",
            inputs={"input": gen_name(2),
                    "x": "fc-input-0",
                    "y": gen_name(1)},
            outputs=[gen_name(4)],
            alpha=1,
            beta=1)
        pattern.build(inputs={"input-0": "fc-input-0"})
        self.patterns.append(pattern)

    def insert_new_layer(self, graph, parameters, matches):
        new_layer = self.gen_new_layer(parameters, matches)
        new_layer_id = list(matches.keys())[0]
        graph.layers[new_layer_id] = new_layer
        matches.pop(new_layer_id)

    def gen_new_layer(self, parameters, matches):
        layers_id = list(matches.keys())
        if len(layers_id) == 5:
            layer = matches[layers_id[2]]
        else:
            layer = matches[layers_id[-1]]
        input_name = layer.inputs["x"]
        scope_name = layer.scope_name
        layer = matches[layers_id[-1]]
        output_name = layer.outputs[0]
        layer = matches[layers_id[0]]
        weight_name = layer.outputs[0]
        layer = matches[layers_id[-2]]
        bias_name = layer.outputs[0]
        attrs = dict()
        attrs["in_features"] = parameters[weight_name].shape[1]
        attrs["out_features"] = parameters[weight_name].shape[0]
        linear_name = "linear{}".format(self.linear_index)
        self.linear_index += 1
W
wjj19950828 已提交
116
        weight_numpy = parameters[weight_name]
W
wjj19950828 已提交
117 118
        parameters["{}.weight".format(linear_name)] = weight_numpy.transpose(
            (1, 0))
W
wjj19950828 已提交
119 120
        self.rm_params.add(weight_name)
        bias_numpy = parameters[bias_name]
W
WJJ1995 已提交
121 122 123
        if len(bias_numpy.shape) == 2:
            bias_numpy = np.squeeze(bias_numpy)
        parameters["{}.bias".format(linear_name)] = bias_numpy
W
wjj19950828 已提交
124
        self.rm_params.add(bias_name)
S
SunAhong1993 已提交
125 126 127 128 129 130 131 132
        new_layer = PaddleLayer(
            layers_id[0],
            "paddle.nn.Linear",
            inputs={"input": input_name},
            outputs=[linear_name, output_name],
            scope_name=scope_name,
            **attrs)
        return new_layer