ptq_registry.py 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#   Copyright (c) 2021 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 paddle

__all__ = ['PTQRegistry']


20
class LayerInfo:
21 22 23 24 25
    """
    Store the argnames of the inputs and outputs.
    """

    def __init__(self, layer, input_names, weight_names, output_names):
26
        super().__init__()
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
        self.layer = layer
        self.input_names = input_names
        self.weight_names = weight_names
        self.output_names = output_names


PTQ_LAYERS_INFO = [
    LayerInfo(paddle.nn.Conv2D, ['Input'], ['Filter'], ['Output']),
    LayerInfo(paddle.nn.Linear, ['X'], ['Y'], ['Out']),
    LayerInfo(paddle.nn.BatchNorm2D, ['X'], [], ['Y']),
    LayerInfo(paddle.nn.AdaptiveMaxPool2D, ['X'], [], ['Out']),
    LayerInfo(paddle.nn.AdaptiveAvgPool2D, ['X'], [], ['Out']),
    LayerInfo(paddle.nn.AvgPool2D, ['X'], [], ['Out']),
    LayerInfo(paddle.nn.MaxPool2D, ['X'], [], ['Out']),
    LayerInfo(paddle.nn.ReLU, ['X'], [], ['Out']),
    LayerInfo(paddle.nn.ReLU6, ['X'], [], ['Out']),
    LayerInfo(paddle.nn.Hardswish, ['X'], [], ['Out']),
44
    LayerInfo(paddle.nn.Swish, ['X'], [], ['Out']),
45 46 47 48 49 50
    LayerInfo(paddle.nn.Sigmoid, ['X'], [], ['Out']),
    LayerInfo(paddle.nn.Softmax, ['X'], [], ['Out']),
    LayerInfo(paddle.nn.Tanh, ['X'], [], ['Out']),
    LayerInfo(paddle.nn.quant.add, ['X', 'Y'], [], ['Out']),
]

51
QUANT_LAYERS_INFO = [
52 53 54 55 56 57 58 59 60
    LayerInfo(
        paddle.nn.quant.quant_layers.QuantizedConv2D,
        ['Input'],
        ['Filter'],
        ['Output'],
    ),
    LayerInfo(
        paddle.nn.quant.quant_layers.QuantizedLinear, ['X'], ['Y'], ['Out']
    ),
61 62 63 64
]

SIMULATED_LAYERS = [paddle.nn.Conv2D, paddle.nn.Linear]

65

66
class PTQRegistry:
67 68 69
    """
    Register the supported layers for PTQ and provide layers info.
    """
70

71
    supported_layers_map = {}
72
    registered_layers_map = {}
73 74 75
    is_inited = False

    def __init__(self):
76
        super().__init__()
77 78 79 80 81 82

    @classmethod
    def _init(cls):
        if not cls.is_inited:
            for layer_info in PTQ_LAYERS_INFO:
                cls.supported_layers_map[layer_info.layer] = layer_info
83 84 85 86

            all_layers_info = PTQ_LAYERS_INFO + QUANT_LAYERS_INFO
            for layer_info in all_layers_info:
                cls.registered_layers_map[layer_info.layer] = layer_info
87 88 89 90 91 92
        cls.is_inited = True

    @classmethod
    def is_supported_layer(cls, layer):
        """
        Analyze whether the layer supports quantization.
93 94 95 96
        Args:
            layer(Layer): The input layer can be a python class or an instance.
        Returns:
            flag(bool): Whther the layer is supported.
97 98
        """
        cls._init()
99 100 101
        return layer in cls.supported_layers_map or isinstance(
            layer, tuple(cls.supported_layers_map.keys())
        )
102

103 104 105 106 107 108 109 110 111 112
    @classmethod
    def is_registered_layer(cls, layer):
        """
        Analyze whether the layer is register layer_info.
        Args:
            layer(Layer): The input layer can be a python class or an instance.
        Returns:
            flag(bool): Wether the layer is register layer_info.
        """
        cls._init()
113 114 115
        return layer in cls.registered_layers_map or isinstance(
            layer, tuple(cls.registered_layers_map.keys())
        )
116 117 118 119 120 121 122 123 124 125

    @classmethod
    def is_simulated_quant_layer(cls, layer):
        """
        Analyze whether the layer is simulated quant layer.
        Args:
            layer(Layer): The input layer can be a python class or an instance.
        Returns:
            flag(bool): Whther the layer is supported.
        """
126 127 128
        return layer in SIMULATED_LAYERS or isinstance(
            layer, tuple(SIMULATED_LAYERS)
        )
129 130

    @classmethod
131 132
    def layer_info(cls, layer):
        """
133 134 135 136 137
        Get the infomation for the layer.
        Args:
            layer(Layer): The input layer can be a python class or an instance.
        Returns:
            layer_info(LayerInfo): The layer info of the input layer.
138
        """
139 140 141
        assert cls.is_registered_layer(
            layer
        ), "The input layer is not register."
142

143
        for layer_key, layer_info in cls.registered_layers_map.items():
144 145
            if layer == layer_key or isinstance(layer, layer_key):
                return layer_info