auto_strategy.py 9.5 KB
Newer Older
C
ceci3 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#   Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserve.
#
# 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 os
import logging
import platform
from ..common import get_logger
19
from .utils.predict import predict_compressed_model, with_variable_shape
C
ceci3 已提交
20
from .strategy_config import *
W
whs 已提交
21
from paddleslim.analysis import TableLatencyPredictor
C
ceci3 已提交
22 23 24 25 26 27 28

_logger = get_logger(__name__, level=logging.INFO)

__all__ = [
    "prepare_strategy", "create_strategy_config", "get_final_quant_config"
]

29
# config tester to test the loss of quant_post
C
ceci3 已提交
30
hpo_config_tester = {
C
ceci3 已提交
31
    "ptq_algo": ["avg", "mse", "KL"],
C
ceci3 已提交
32 33
    "weight_quantize_type": ['channel_wise_abs_max', 'abs_max'],
    "bias_correct": [False],
34
    "batch_num": [5],
C
ceci3 已提交
35 36 37
    "max_quant_count": 1,
}

38
# default hpo config
C
ceci3 已提交
39 40 41 42 43 44 45 46 47
default_hpo_config = {
    "ptq_algo": ["KL", "hist", "avg", "mse"],
    "weight_quantize_type": ['channel_wise_abs_max', 'abs_max'],
    "bias_correct": [True, False],
    "hist_percent": [0.98, 0.999],
    "batch_num": [10, 30],
    "max_quant_count": 20,
}

48
# default quant config, can be used by ptq&hpo and qat&distillation
C
ceci3 已提交
49
default_quant_config = {
50 51 52 53
    'quantize_op_types': [
        'conv2d', 'depthwise_conv2d', 'conv2d_transpose', 'mul', 'matmul',
        'matmul_v2'
    ],
C
ceci3 已提交
54
    'weight_bits': 8,
55 56
    'activation_bits': 8,
    "is_full_quantize": False,
C
ceci3 已提交
57 58
    "activation_quantize_type": 'moving_average_abs_max',
    "weight_quantize_type": 'channel_wise_abs_max',
59 60 61 62 63 64 65 66
    "not_quant_pattern": ["skip_quant"],
}

# default train config
DefaultTrainConfig = {
    "epochs": 1,
    "eval_iter": 500,
    "learning_rate": 0.0001,
C
ceci3 已提交
67 68 69 70
    "optimizer_builder": {
        "optimizer": {
            "type": "Momentum",
        },
71
        "weight_decay": 4.0e-05
C
ceci3 已提交
72
    }
C
ceci3 已提交
73 74 75 76 77 78 79
}

EXPERIENCE_STRATEGY_WITHOUT_LOSS = [
    'sparse_0.75_fp32', 'prune_0.3_fp32', 'origin_int8', 'sparse_0.75_int8',
    'prune_0.3_int8'
]
MAGIC_SPARSE_RATIO = 0.75
C
ceci3 已提交
80
### TODO: 0.02 threshold maybe not suitable, need to check
C
ceci3 已提交
81
### NOTE: reduce magic data to choose quantization aware training.
C
ceci3 已提交
82 83
MAGIC_MAX_EMD_DISTANCE = 0.00002  #0.02
MAGIC_MIN_EMD_DISTANCE = 0.00001  #0.01
C
ceci3 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96

DEFAULT_TRANSFORMER_STRATEGY = 'prune_0.25_int8'
DEFAULT_STRATEGY = 'origin_int8'
DEFAULT_QUANT_SPEEDUP = 0.7


def create_strategy_config(strategy_str, model_type):
    """ create config according to string"""
    tmp_s = strategy_str.split('_')
    configs = []

    dis_config = Distillation()
    if len(tmp_s) == 3:
C
ceci3 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109
        ### TODO(ceci3): choose prune algo automatically
        if 'prune' in tmp_s[0]:
            ### default prune config
            default_prune_config = {
                'pruned_ratio': float(tmp_s[1]),
                'criterion': 'l1_norm'
            }
        else:
            ### default unstruture prune config
            default_prune_config = {
                'prune_strategy':
                'gmp',  ### default unstruture prune strategy is gmp
                'prune_mode': 'ratio',
C
ceci3 已提交
110
                'ratio': float(tmp_s[1]),
C
ceci3 已提交
111 112 113
                'local_sparsity': True,
                'prune_params_type': 'conv1x1_only'
            }
C
ceci3 已提交
114 115 116 117 118
        if model_type == 'transformer':
            tmp_s[0] = tmp_s[0].replace('prune', 'TransformerPrune')
            default_prune_config = {'pruned_ratio': float(tmp_s[1])}
        else:
            tmp_s[0] = tmp_s[0].replace('prune', 'Prune')
C
ceci3 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        tmp_s[0] = tmp_s[0].replace('sparse', 'UnstructurePrune')
        prune_config = eval(tmp_s[0])(**default_prune_config)
        configs.append({tmp_s[0]: prune_config, 'Distillation': dis_config})

    ### TODO(ceci3): support skip some layer and full quant
    if tmp_s[-1] == 'int8':
        ### only platform is linux can use smac to do hyperparameter optimization
        ### choose quant_aware to do quantization in other platform
        if platform.system().lower() == 'linux':
            quant_config = Quantization(**default_quant_config)
            hpo_config = HyperParameterOptimization(**hpo_config_tester)
            configs.append({
                'Quantization': quant_config,
                'HyperParameterOptimization': hpo_config
            })
        else:
            quant_config = Quantization(**default_quant_config)
            dis_config = Distillation()
            configs.append({
                'Quantization': quant_config,
                'Distillation': dis_config
            })

    return configs


145 146 147 148 149 150
def create_train_config(strategy_str, model_type):
    # TDOD: support more strategy and model_type
    train_config = TrainConfig(**DefaultTrainConfig)
    return train_config


C
ceci3 已提交
151 152 153
def prepare_strategy(executor,
                     places,
                     model_dir,
C
ceci3 已提交
154 155 156 157 158 159 160 161
                     model_filename,
                     params_filename,
                     target_speedup=None,
                     deploy_hardware=None,
                     model_type=None):
    """ prepare compression config automatically """
    final_strategy = None

162 163
    ### use hardware latency tabel if support
    if not with_variable_shape(
164 165
            model_dir,
            model_filename=model_filename,
166 167 168
            params_filename=params_filename) and (
                deploy_hardware in TableLatencyPredictor.hardware_list):

C
ceci3 已提交
169
        compressed_time_dict = predict_compressed_model(
C
ceci3 已提交
170 171
            executor,
            places,
C
ceci3 已提交
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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
            model_dir,
            model_filename,
            params_filename,
            hardware=deploy_hardware)

        baseline = compressed_time_dict['origin_fp32']
        speedup_ratio = {}
        for strategy, latency in compressed_time_dict.items():
            speedup_ratio[strategy] = 1.0 - float(latency) / baseline

        sorted_speedup_ratio = sorted(speedup_ratio.items(), key=lambda x: x[1])

        ### if target speedup is None, choose strategy by experience.
        if target_speedup is None:
            max_speedup = -1.0
            for s in EXPERIENCE_STRATEGY_WITHOUT_LOSS:
                if s not in speedup_ratio:
                    _logger.info(f"cannot get the speed up of strategy {s}")
                    continue

                if speedup_ratio[s] > max_speedup:
                    max_speedup = speedup_ratio[s]
                    final_strategy = s
        else:
            candidate_s = []
            pre_s = None
            for strategy, ratio in sorted_speedup_ratio:
                if abs(ratio - target_speedup) <= 0.1:
                    candidate_s.append(strategy)
                ### if there is no strategy satisfy target speedup
                ### choose the most recent speedup 
                if ratio > target_speedup and len(candidate_s) == 0:
                    if pre_s is not None:
                        candidate_s.append(pre_s)
                    candidate_s.append(strategy)
                pre_s = strategy

            if 'origin_int8' in candidate_s:
                final_strategy = candidate_s
            else:
                candidate_s = sorted(candidate_s, key=lambda x: x.split('_')[1])
                for c in candidate_s:
                    if c.startswith('sparse') and float(c.split('_')[
                            1]) <= MAGIC_SPARSE_RATIO:
                        final_strategy = c

                if final_strategy is None:
                    final_strategy = candidate_s[0]

    else:
        ### default speedup ratio of quantization is 70% compare to fp32
        ### TODO(ceci3): full quant or skip some layer later
        if target_speedup is None:
            if model_type == 'transformer':
                final_strategy = DEFAULT_TRANSFORMER_STRATEGY
            else:
                final_strategy = DEFAULT_STRATEGY

        elif target_speedup > DEFAULT_QUANT_SPEEDUP:
            prune_ratio = target_speedup - DEFAULT_QUANT_SPEEDUP
            if prune_ratio > 1.0:
                raise NotImplementedError(
                    "target_speedup {} is improper".format(target_speedup))
            final_strategy = 'prune_{}_int8'.format(str(prune_ratio))
        else:
            raise NotImplementedError("target_speedup {} is improper".format(
                target_speedup))

    strategy_config = create_strategy_config(final_strategy, model_type)
    return strategy_config


C
ceci3 已提交
244
def get_final_quant_config(ptq_loss, model_type=None):
C
ceci3 已提交
245
    """ transform quantization tester config to real quantization config """
C
ceci3 已提交
246 247 248 249 250
    ### if emd loss less than MAGIC_MIN_EMD_DISTANCE, final compress.
    if ptq_loss < MAGIC_MIN_EMD_DISTANCE:
        return None
    ### if emd loss less than MAGIC_MAX_EMD_DISTANCE, select quant_post & hpo.
    elif ptq_loss < MAGIC_MAX_EMD_DISTANCE:
C
ceci3 已提交
251 252 253 254 255 256 257
        quant_config = Quantization(**default_quant_config)
        hpo_config = HyperParameterOptimization(**default_hpo_config)
        configs = [{
            'Quantization': quant_config,
            'HyperParameterOptimization': hpo_config
        }]

C
ceci3 已提交
258 259
    ### if emd loss greater than MAGIC_MAX_EMD_DISTANCE, select qat & dist.
    else:
C
ceci3 已提交
260 261 262
        quant_config = Quantization(**default_quant_config)
        dis_config = Distillation()
        configs = [{'Quantization': quant_config, 'Distillation': dis_config}]
263
        _logger.info("Start Quantization and Distillation Training.")
C
ceci3 已提交
264 265 266 267 268 269

    return configs


if __name__ == '__main__':
    create_strategy_config('sparse_0.75_int8', 'transformer')