-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathptq_main.py
122 lines (109 loc) · 5.09 KB
/
ptq_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# Copyright (2023) Bytedance Ltd. and/or its affiliates
# 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 numpy as np
import argparse, sys
from data.imagenet import load_data
from models import load_model
from utils import parse_config, seed_all, evaluate
from MRECG import ptq_reconstruction
from mqbench.prepare_by_platform import prepare_by_platform, BackendType
from mqbench.convert_deploy import convert_deploy
backend_dict = {
'Academic': BackendType.Academic,
'Tensorrt': BackendType.Tensorrt,
'SNPE': BackendType.SNPE,
'PPLW8A16': BackendType.PPLW8A16,
'NNIE': BackendType.NNIE,
'Vitis': BackendType.Vitis,
'ONNX_QNN': BackendType.ONNX_QNN,
'PPLCUDA': BackendType.PPLCUDA,
}
def load_calibrate_data(train_loader, cali_batchsize):
cali_data = []
for i, batch in enumerate(train_loader):
cali_data.append(batch[0])
if i + 1 == cali_batchsize:
break
return cali_data
def get_quantize_model(model, config):
backend_type = BackendType.Academic if not hasattr(
config.quantize, 'backend') else backend_dict[config.quantize.backend]
extra_prepare_dict = {} if not hasattr(
config, 'extra_prepare_dict') else config.extra_prepare_dict
return prepare_by_platform(
model, backend_type, extra_prepare_dict)
def deploy(model, config):
backend_type = BackendType.Academic if not hasattr(
config.quantize, 'backend') else backend_dict[config.quantize.backend]
output_path = './' if not hasattr(
config.quantize, 'deploy') else config.quantize.deploy.output_path
model_name = config.quantize.deploy.model_name
deploy_to_qlinear = False if not hasattr(
config.quantize.deploy, 'deploy_to_qlinear') else config.quantize.deploy.deploy_to_qlinear
convert_deploy(model, backend_type, {
'input': [1, 3, 224, 224]}, output_path=output_path, model_name=model_name, deploy_to_qlinear=deploy_to_qlinear)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='ImageNet Solver')
parser.add_argument('--config', required=True, type=str)
args = parser.parse_args()
config = parse_config(args.config)
# # seed first
# seed_all(config.process.seed)
# load_model
model = load_model(config.model)
if hasattr(config, 'quantize'):
model = get_quantize_model(model, config)
model.cuda()
# load_data
train_loader, val_loader = load_data(**config.data)
# evaluate
if not hasattr(config, 'quantize'):
evaluate(val_loader, model)
elif config.quantize.quantize_type == 'advanced_ptq':
print('begin calibration now!')
cali_data = load_calibrate_data(train_loader, cali_batchsize=config.quantize.cali_batchsize)
from mqbench.utils.state import enable_quantization, enable_calibration_woquantization
# do activation and weight calibration seperately for quick MSE per-channel for weight one
model.eval()
import torch
with torch.no_grad():
enable_calibration_woquantization(model, quantizer_type='act_fake_quant')
for batch in cali_data:
model(batch.cuda())
enable_calibration_woquantization(model, quantizer_type='weight_fake_quant')
model(cali_data[0].cuda())
print('begin advanced PTQ now!')
if hasattr(config.quantize, 'reconstruction'):
model = ptq_reconstruction(
model, cali_data, config.quantize.reconstruction)
enable_quantization(model)
evaluate(val_loader, model)
if hasattr(config.quantize, 'deploy'):
deploy(model, config)
elif config.quantize.quantize_type == 'naive_ptq':
print('begin calibration now!')
cali_data = load_calibrate_data(train_loader, cali_batchsize=config.quantize.cali_batchsize)
from mqbench.utils.state import enable_quantization, enable_calibration_woquantization
# do activation and weight calibration seperately for quick MSE per-channel for weight one
model.eval()
enable_calibration_woquantization(model, quantizer_type='act_fake_quant')
for batch in cali_data:
model(batch.cuda())
enable_calibration_woquantization(model, quantizer_type='weight_fake_quant')
model(cali_data[0].cuda())
print('begin quantization now!')
enable_quantization(model)
evaluate(val_loader, model)
if hasattr(config.quantize, 'deploy'):
deploy(model, config)
else:
print("The quantize_type must in 'naive_ptq' or 'advanced_ptq',")
print("and 'advanced_ptq' need reconstruction configration.")