bn_scale_fuser.py 5.2 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
#   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 *


S
renam  
SunAhong1993 已提交
21
class DygraphBNScaleFuser(FuseBase):
S
SunAhong1993 已提交
22
    def __init__(self):
S
renam  
SunAhong1993 已提交
23
        super(DygraphBNScaleFuser, self).__init__(graph_type="dygraph")
S
SunAhong1993 已提交
24
        patterns = list()
S
SunAhong1993 已提交
25 26 27 28

    def build_pattern(self):
        """ 描述需要替换的batchnorm2d图结构。
        batchnorm2d层模式python实现代码示例:
S
SunAhong1993 已提交
29
            模式一:
S
SunAhong1993 已提交
30 31 32 33
            bn_conv1 = self.batchnorm0(conv1)
            scale_conv1_cparam1 = self.scale_conv1_cparam1
            scale_conv1_mul = paddle.multiply(x=bn_conv1, y=scale_conv1_cparam1, axis=1)
            scale_conv1_cparam2 = self.scale_conv1_cparam2
S
SunAhong1993 已提交
34 35 36 37 38 39 40 41
            scale_conv1 = paddle.add(x=scale_conv1_mul, y=scale_conv1_cparam2, axis=1)
            模式二:
            bn_conv1 = self.batchnorm0(conv1)
            scale_conv1_cparam1 = self.scale_conv1_cparam1
            scale_conv1_mul = paddle.multiply(x=bn_conv1, y=scale_conv1_cparam1, axis=1)
            scale_conv1_cparam2 = self.scale_conv1_cparam2
            scale_conv1_cparam2 = paddle.reshape(x=scale_conv1_cparam2, shape=[32, 1, 1])
            scale_conv1 = paddle.add(x=scale_conv1_mul, y=scale_conv1_cparam2, axis=1)
S
SunAhong1993 已提交
42 43 44 45 46
        """

        def gen_name(id):
            return "x" + str(id)
        
S
SunAhong1993 已提交
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
        pattern = PaddleGraph(graph_type="dygraph")
        pattern.add_layer(
            "paddle.nn.BatchNorm2D",
            inputs={"input": "bn-input-0"},
            outputs=[gen_name(0)])
        pattern.add_layer(
            "self.create_parameter",
            inputs={},
            outputs=[gen_name(1)])
        inputs_dict = {}
        inputs_dict['x'] = gen_name(0)
        inputs_dict['y'] = gen_name(1)
        pattern.add_layer(
            "paddle.multiply",
            inputs=inputs_dict,
            outputs=[gen_name(2)])
        pattern.add_layer(
            "self.create_parameter",
            inputs={},
            outputs=[gen_name(3)])
        inputs_dict = {}
        inputs_dict['x'] = gen_name(2)
        inputs_dict['y'] = gen_name(3)
        pattern.add_layer(
            "paddle.add",
            inputs=inputs_dict,
            outputs=[gen_name(4)])
        pattern.build(inputs={"input-0": "bn-input-0"})
        self.patterns.append(pattern)
        
        pattern = PaddleGraph(graph_type="dygraph")
        pattern.add_layer(
S
SunAhong1993 已提交
79 80 81
            "paddle.nn.BatchNorm2D",
            inputs={"input": "bn-input-0"},
            outputs=[gen_name(0)])
S
SunAhong1993 已提交
82
        pattern.add_layer(
S
SunAhong1993 已提交
83 84 85 86 87 88
            "self.create_parameter",
            inputs={},
            outputs=[gen_name(1)])
        inputs_dict = {}
        inputs_dict['x'] = gen_name(0)
        inputs_dict['y'] = gen_name(1)
S
SunAhong1993 已提交
89
        pattern.add_layer(
S
SunAhong1993 已提交
90 91 92
            "paddle.multiply",
            inputs=inputs_dict,
            outputs=[gen_name(2)])
S
SunAhong1993 已提交
93
        pattern.add_layer(
S
SunAhong1993 已提交
94 95 96
            "self.create_parameter",
            inputs={},
            outputs=[gen_name(3)])
S
SunAhong1993 已提交
97 98 99 100
        pattern.add_layer(
            "paddle.reshape",
            inputs={"x": gen_name(3)},
            outputs=[gen_name(3)])
S
SunAhong1993 已提交
101 102 103
        inputs_dict = {}
        inputs_dict['x'] = gen_name(2)
        inputs_dict['y'] = gen_name(3)
S
SunAhong1993 已提交
104 105
        pattern.add_layer(
            "paddle.add",
S
SunAhong1993 已提交
106 107
            inputs=inputs_dict,
            outputs=[gen_name(4)])
S
SunAhong1993 已提交
108 109 110 111
        pattern.build(inputs={"input-0": "bn-input-0"})
        self.patterns.append(pattern)
        
        
S
SunAhong1993 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

    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())
        layer = matches[layers_id[0]]
        layer_inputs = layer.inputs
        bn_name = layer.outputs[0]
        layer_attrs = layer.attrs
        layer_attrs.pop("weight_attr")
        layer_attrs.pop("bias_attr")
S
SunAhong1993 已提交
128
        layer = matches[layers_id[-1]]
S
SunAhong1993 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        layer_outputs = [bn_name] + layer.outputs
        layer = matches[layers_id[1]]
        data0_name = layer.outputs[0]
        data0_numpy = parameters.pop(data0_name)
        parameters["{}.weight".format(layer_outputs[0])] = data0_numpy
        layer = matches[layers_id[3]]
        data1_name = layer.outputs[0]
        data1_numpy = parameters.pop(data1_name)
        parameters["{}.bias".format(layer_outputs[0])] = data1_numpy
        new_layer = PaddleLayer(
            layers_id[0],
            "paddle.nn.BatchNorm2D",
            inputs=layer_inputs,
            outputs=layer_outputs,
            **layer_attrs)
        return new_layer