tf_optimizer.py 8.6 KB
Newer Older
J
jiangjiajun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2019  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.

# TODO useless node remove
J
jiangjiajun 已提交
16 17
from x2paddle.op_mapper.tf_op_mapper import TFOpMapper
from x2paddle.core.util import *
J
jiangjiajun 已提交
18 19


J
jiangjiajun 已提交
20 21 22 23 24 25 26 27 28
class TFOptimizer(object):
    activation_ops = {
        'Relu': 'relu',
        'Sigmoid': 'sigmoid',
        'Relu6': 'relu6',
        'swish_f32': 'swish'
    }
    layers_with_act = [
        'Conv2D', 'BiasAdd', 'DepthwiseConv2dNative', 'Conv2DBackpropInput',
29 30
        'FusedBatchNorm', 'conv2d', 'elementwise_add', 'conv2d_transpose',
        'batch_norm'
J
jiangjiajun 已提交
31 32
    ]
    layers_with_bias = [
33 34
        'Conv2D', 'DepthwiseConv2dNative', 'Conv2DBackpropInput', 'conv2d',
        'conv2d_transpose'
J
jiangjiajun 已提交
35
    ]
36

J
jiangjiajun 已提交
37 38 39 40 41 42 43 44
    def __init__(self, op_mapper):
        self.op_mapper = op_mapper
        self.graph = op_mapper.graph

    def delete_redundance_code(self):
        for node_name in self.graph.topo_sort:
            if node_name in self.op_mapper.omit_nodes:
                node = self.graph.get_node(node_name)
J
jiangjiajun 已提交
45 46
                if node is None:
                    continue
J
jiangjiajun 已提交
47 48 49 50
                omit_freq = self.op_mapper.omit_nodes.count(node_name)
                if len(node.outputs) <= omit_freq:
                    node.fluid_code.clear()

J
jiangjiajun 已提交
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
                    # remove node from graph
                    input_names = node.inputs
                    output_names = node.outputs
                    for in_name in input_names:
                        in_node = self.graph.get_node(in_name)
                        index = in_node.outputs.index(node_name)
                        del in_node.outputs[index]
                    for out_name in output_names:
                        out_node = self.graph.get_node(out_name)
                        index = out_node.inputs.index(node_name)
                        del out_node.inputs[index]
                    del self.graph.node_map[node_name]

    def strip_graph(self):
        visited_nodes = set()

        def visit(node_name):
            if node_name in visited_nodes:
                return
            visited_nodes.add(node_name)
            input_names = self.graph.get_node(node_name).inputs
            for in_name in input_names:
                visit(in_name)

        for node_name in self.graph.output_nodes:
            visit(node_name)

        for i, node_name in enumerate(self.graph.topo_sort):
            if node_name not in visited_nodes:
                node = self.graph.get_node(node_name)
                if node is None:
                    continue
                input_names = node.inputs
                output_names = node.outputs
                for in_name in input_names:
                    in_node = self.graph.get_node(in_name)
                    index = in_node.outputs.index(node_name)
                    del in_node.outputs[index]
                for out_name in output_names:
                    out_node = self.graph.get_node(out_name)
                    index = out_node.inputs.index(node_name)
                    del out_node.inputs[index]
                del self.graph.node_map[node_name]

J
jiangjiajun 已提交
95 96 97 98 99
    # TODO activation merge
    def merge_activation(self):
        act_nodes = list()
        for node_name in self.graph.topo_sort:
            node = self.graph.get_node(node_name)
J
jiangjiajun 已提交
100 101
            if node is None:
                continue
J
jiangjiajun 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
            if node.layer_type in self.activation_ops:
                act_nodes.append(node_name)

        for act_node_name in act_nodes:
            node = self.graph.get_node(act_node_name)
            input = self.graph.get_node(node.inputs[0])
            if input.layer_type not in self.layers_with_act:
                continue
            if len(input.fluid_code.layers) == 0:
                continue
            if 'act' in input.fluid_code.layers[
                    -1].param_attr and input.fluid_code.layers[-1].param_attr[
                        'act'] is not None:
                continue
            if len(input.outputs) != 1:
                continue
118 119 120 121 122 123
            index = -1
            for i in range(len(input.fluid_code.layers)):
                if input.fluid_code.layers[i].op in self.layers_with_act:
                    index = i
                    break
            input.fluid_code.layers[index].param_attr['act'] = string(
J
jiangjiajun 已提交
124 125 126 127 128 129 130 131 132
                self.activation_ops[node.layer_type])
            input.fluid_code.layers[-1].output = node.fluid_code.layers[
                0].output
            self.graph.remove_node(act_node_name)

    # TODO bias merge
    def merge_bias(self):
        for node_name in self.graph.topo_sort:
            node = self.graph.get_node(node_name)
J
jiangjiajun 已提交
133 134
            if node is None:
                continue
J
jiangjiajun 已提交
135 136 137 138 139 140 141 142 143 144 145 146
            if node.layer_type == "BiasAdd":
                input = self.graph.get_node(node.inputs[0])
                if input.layer_type not in self.layers_with_bias:
                    continue
                if len(input.outputs) != 1:
                    continue
                if len(input.fluid_code.layers) == 0:
                    continue
                bias_with_act = False
                if 'act' in node.fluid_code.layers[-1].param_attr:
                    bias_with_act = True
                layer_with_act = False
147 148 149 150 151
                index = -1
                for i in range(len(input.fluid_code.layers)):
                    if input.fluid_code.layers[i].op in self.layers_with_bias:
                        index = i
                        break
J
jiangjiajun 已提交
152
                if 'act' in input.fluid_code.layers[
153 154
                        index].param_attr and input.fluid_code.layers[
                            index].param_attr['act'] is not None:
J
jiangjiajun 已提交
155 156 157 158
                    layer_with_act = True

                if bias_with_act and layer_with_act:
                    continue
159
                if not input.fluid_code.layers[index].param_attr['bias_attr']:
J
jiangjiajun 已提交
160
                    bias_name = node.inputs[1]
161
                    input.fluid_code.layers[index].param_attr[
J
jiangjiajun 已提交
162 163 164 165
                        'bias_attr'] = string(bias_name)
                    input.fluid_code.layers[-1].output = node.fluid_code.layers[
                        0].output
                    if bias_with_act:
166
                        input.fluid_code.layers[index].param_attr[
J
jiangjiajun 已提交
167 168 169
                            'act'] = node.fluid_code.layers[-1].param_attr[
                                'act']
                    node.fluid_code.clear()
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
                    self.graph.remove_node(node.layer_name)

    def remove_transpose(self):
        optimize_ops = [
            'Conv2D', 'MaxPool', 'FusedBatchNorm', 'DepthwiseConv2dNative',
            'AvgPool', 'Pad', 'Conv2DBackpropInput', 'ResizeNearestNeighbor',
            'ResizeBilinear'
        ]
        for node_name in self.graph.topo_sort:
            node = self.graph.get_node(node_name)
            if node is None:
                continue
            if node.layer_type not in optimize_ops:
                continue
            if node.fluid_code.layers[
                    -1].op != "transpose" or node.fluid_code.layers[
                        -1].param_attr["perm"] != [0, 2, 3, 1]:
                continue
            output_names = node.outputs
            can_be_removed = True
            for out_name in output_names:
                out_node = self.graph.get_node(out_name)
                if out_node.layer_type == "BiasAdd":
                    can_be_removed = True
                if out_node.fluid_code.layers[
                        0].op != "transpose" or out_node.fluid_code.layers[
                            0].param_attr["perm"] != [0, 3, 1, 2]:
                    can_be_removed = False
                    break

            if can_be_removed and len(output_names) > 0:
                last_out = node.fluid_code.layers[-1].inputs
                del node.fluid_code.layers[-1]
                for out_name in output_names:
                    out_node = self.graph.get_node(out_name)
                    if out_node.layer_type == "BiasAdd":
                        del out_node.fluid_code.layers[0]
                        out_node.fluid_code.layers[0].inputs['x'] = last_out
                    else:
                        del out_node.fluid_code.layers[0]
                        out_node.fluid_code.layers[0].inputs = last_out