utils.py 5.5 KB
Newer Older
C
cc 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   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.

15
import math
16
import numpy as np
17 18

import paddle
19
from paddle.distributed import fleet
20
import paddle.nn.quant.quant_layers as quant_layers
21

22
from ..utils import _get_op_input_var_names, _get_op_output_var_names, _get_output_name_index, _get_input_name_index
C
cc 已提交
23

24
layer_name_map = {
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
    'Conv2DTranspose':
    paddle.nn.Conv2DTranspose,
    'Conv2D':
    paddle.nn.Conv2D,
    'Linear':
    paddle.nn.Linear,
    'AdaptiveAvgPool2D':
    paddle.nn.AdaptiveAvgPool2D,
    'AdaptiveMaxPool2D':
    paddle.nn.AdaptiveMaxPool2D,
    'AvgPool2D':
    paddle.nn.AvgPool2D,
    'MaxPool2D':
    paddle.nn.MaxPool2D,
    'Hardswish':
    paddle.nn.Hardswish,
    'LeakyReLU':
    paddle.nn.LeakyReLU,
    'PReLU':
    paddle.nn.PReLU,
    'ReLU':
    paddle.nn.ReLU,
    'ReLU6':
    paddle.nn.ReLU6,
    'Sigmoid':
    paddle.nn.Sigmoid,
    'Softmax':
    paddle.nn.Softmax,
    'Swish':
    paddle.nn.Swish,
    'Tanh':
    paddle.nn.Tanh,
    'Hardswish':
    paddle.nn.Hardswish,
    'BatchNorm':
    paddle.nn.BatchNorm,
    'GroupNorm':
    paddle.nn.GroupNorm,
    'LayerNorm':
    paddle.nn.LayerNorm,
    'ColumnParallelLinear':
    fleet.meta_parallel.parallel_layers.mp_layers.ColumnParallelLinear,
    'RowParallelLinear':
    fleet.meta_parallel.parallel_layers.mp_layers.RowParallelLinear
C
cc 已提交
69
}
70

71
# Apply fake quant for the inputs of these layers
72
fake_quant_input_layers = [
73 74 75
    paddle.nn.Conv2D, paddle.nn.Linear, paddle.nn.Conv2DTranspose,
    fleet.meta_parallel.RowParallelLinear,
    fleet.meta_parallel.ColumnParallelLinear
76
]
77 78 79 80 81 82 83 84 85 86

# Apply fake quant for the output of these layers
# TODO(jc): fix the problem of adding duplicate fake_quant ops
# paddle.nn.AdaptiveAvgPool2D, paddle.nn.AvgPool2D, paddle.nn.ReLU,paddle.nn.LeakyReLU
fake_quant_output_layers = [
    paddle.nn.quant.add, paddle.nn.quant.subtract, paddle.nn.quant.multiply,
    paddle.nn.quant.divide
]

fake_quant_leaf_layers = [
87 88 89 90
    quant_layers.FakeQuantAbsMax,
    quant_layers.FakeQuantChannelWiseAbsMax,
    quant_layers.FakeQuantMovingAverageAbsMax,
    quant_layers.MovingAverageAbsMaxScale,
G
guofei 已提交
91 92
]

93
fake_quant_wrap_layers = [
94
    quant_layers.QuantizedConv2D, quant_layers.QuantizedLinear,
95 96 97
    quant_layers.QuantizedConv2DTranspose,
    quant_layers.QuantizedColumnParallelLinear,
    quant_layers.QuantizedRowParallelLinear
98
]
99

100
# The weight format of these layers is Cin * Cout * H * W
101
spec_channel_axis_layers = [paddle.nn.Conv2DTranspose, paddle.nn.Linear]
102

103 104 105 106
weight_op_types = [
    "conv2d", "depthwise_conv2d", "matmul", "conv2d_transpose",
    "depthwise_conv2d_transpose"
]
107

108 109 110 111 112 113
fake_quantize_dequantize_op_types = [
    "fake_quantize_dequantize_abs_max",
    "fake_channel_wise_quantize_dequantize_abs_max",
    "fake_quantize_dequantize_moving_average_abs_max"
]

114 115

def load_variable_data(scope, var_name):
116
    """
117
    Load variable value from scope
118
    """
119 120 121 122 123 124 125 126 127 128 129 130 131
    var_node = scope.find_var(var_name)
    assert var_node is not None, \
        "Can not find " + var_name + " in the scope."
    return np.array(var_node.get_tensor())


def find_previous_op(block, var_name):
    """
    Find the previous op for the input variable.
    """
    for op in block.ops:
        if var_name in op.output_arg_names:
            return op
132
    return None
133 134 135 136 137 138 139 140 141 142 143


def find_next_ops(block, var_name):
    """
    Find all followed ops for the input variable.
    """
    res_ops = []
    for op in block.ops:
        if var_name in op.input_arg_names:
            res_ops.append(op)
    return res_ops
144 145 146 147 148 149 150 151


def find_parent_layer_and_sub_name(model, name):
    """
    Given the model and the name of a layer, find the parent layer and
    the sub_name of the layer.
    For example, if name is 'block_1/convbn_1/conv_1', the parent layer is
    'block_1/convbn_1' and the sub_name is `conv_1`.
152 153 154 155 156 157
    Args:
        model(paddle.nn.Layer): the model to be quantized.
        name(string): the name of a layer

    Returns:
        parent_layer, subname
158
    """
159
    assert isinstance(model, paddle.nn.Layer), \
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
            "The model must be the instance of paddle.nn.Layer."
    assert len(name) > 0, "The input (name) should not be empty."

    last_idx = 0
    idx = 0
    parent_layer = model
    while idx < len(name):
        if name[idx] == '.':
            sub_name = name[last_idx:idx]
            if hasattr(parent_layer, sub_name):
                parent_layer = getattr(parent_layer, sub_name)
                last_idx = idx + 1
        idx += 1
    sub_name = name[last_idx:idx]
    return parent_layer, sub_name


177 178 179 180 181 182 183 184 185 186 187
def program_all_ops(program):
    """
    Return all ops for the input program.
    """
    all_ops = []
    for block in program.blocks:
        for op in block.ops:
            all_ops.append(op)
    return all_ops


188 189 190 191
def is_leaf_layer(layer):
    """
    Whether the layer is leaf layer.
    """
192
    return isinstance(layer, paddle.nn.Layer) \
193
        and len(layer.sublayers()) == 0
194 195


196
def fp_numpy_to_naive(x_np):
197
    """
198
    Convert numpy to float or list.
199
    """
200 201 202 203
    if x_np.size == 1:
        return float(x_np)
    else:
        return x_np.tolist()