bias.py 3.3 KB
Newer Older
J
jiangjiajun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import copy


class BiasOpt:
    def __init__(self):
        self.conv_layers = [
            'fluid.layers.conv2d', 'fluid.layers.conv2d_transpose'
        ]
        self.act_layers = [
            'fluid.layers.relu', 'fluid.layers.relu6', 'fluid.layers.sigmoid',
            'fluid.layers.exp', 'fluid.layers.tanh', 'fluid.layers.softplus',
            'fluid.layers.leaky_relu'
        ]

    def run(self, graph):
J
jiangjiajun 已提交
16
        print("Optimize: BiasOpt...")
J
jiangjiajun 已提交
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
        layers = copy.deepcopy(graph.layers)
        for layer_id, layer in layers.items():
            if layer.kernel in self.conv_layers or layer.kernel == "fluid.layers.transpose":
                if len(graph.edges_out.get(layer_id, [])) > 1:
                    continue
                if layer.outputs[0] in graph.outputs:
                    continue

                out_layer_id = graph.edges_out[layer_id][0]
                if graph.layers[
                        out_layer_id].kernel != "fluid.layers.elementwise_add":
                    continue
                if graph.layers[out_layer_id].attrs.get('axis', -1) != -1:
                    continue

                in_layer_id = graph.edges_in[out_layer_id]
                bias_layer_id = in_layer_id[1 - in_layer_id.index(layer_id)]
                if graph.layers[
                        bias_layer_id].kernel != "fluid.layers.create_parameter":
                    continue

                bias_layer = graph.layers[bias_layer_id]
                if len(bias_layer.attrs['shape']) != 1:
                    continue
                if len(graph.edges_out[bias_layer_id]) != 1:
                    continue

                if layer.kernel == "fluid.layers.transpose":
                    if layer.attrs['perm'] != [0, 2, 3, 1]:
                        continue
                    in_layer_id = graph.edges_in[layer_id][0]
                    if graph.layers[in_layer_id].kernel not in self.conv_layers:
                        continue
                    if graph.layers[in_layer_id].attrs['bias_attr'] != False:
                        continue
                    if graph.layers[in_layer_id].outputs[0] in graph.outputs:
                        continue
                    if len(graph.edges_out[in_layer_id]) != 1:
                        continue
                    graph.layers[in_layer_id].attrs[
                        'bias_attr'] = bias_layer.attrs['name']
                else:
                    graph.layers[layer_id].attrs[
                        'bias_attr'] = bias_layer.attrs['name']
                bias_add_outs = graph.edges_out.get(out_layer_id, [])
                bias_add_output = graph.layers[out_layer_id].outputs[0]
                graph.del_layer(bias_layer_id)
                graph.del_layer(out_layer_id)

                for out in bias_add_outs:
                    for k, v in graph.layers[out].inputs.items():
                        if v == layer.outputs[0]:
                            graph.layers[out].inputs[k] = bias_add_output
                graph.layers[layer_id].outputs[0] = bias_add_output

                if layer.kernel == "fluid.layers.transpose":
                    in_layer_id = graph.edges_in[layer_id][0]
                    graph.layers[in_layer_id].outputs[0] = bias_add_output
                    graph.layers[layer_id].inputs['x'] = bias_add_output