-
Notifications
You must be signed in to change notification settings - Fork 0
/
quantize.py
147 lines (115 loc) · 4.23 KB
/
quantize.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os
from tqdm.auto import tqdm
from typing import *
import torch
import torch.nn as nn
# for post training quantization
def prepare_ptq(model: nn.Module, backend: str='x86'):
model.eval()
model = model.cpu()
model.qconfig = torch.quantization.get_default_qconfig(backend)
return torch.quantization.prepare(model)
# for quantization aware training
def prepare_qat(model: nn.Module, backend: str='x86'):
model.train()
model = model.cpu()
model.qconfig = torch.quantization.get_default_qat_qconfig(backend)
return torch.quantization.prepare_qat(model)
# after training
def converting_quantization(model: nn.Module):
model.eval()
model = model.cpu()
return torch.quantization.convert(model)
# for serving of ptq model
def ptq_serving(
model: nn.Module,
weight: str, # the path of weight file
backend: str='x86',
):
model = fuse_modules(model, mode='eval')
model = prepare_ptq(model, backend)
model = converting_quantization(model)
model.load_state_dict(torch.load(weight, map_location=torch.device('cpu')))
return model
# for serving of qat model
def qat_serving(
model: nn.Module,
weight: str,
backend: str='x86',
):
model = fuse_modules(model, mode='train')
model = prepare_qat(model, backend)
model.load_state_dict(torch.load(weight, map_location=torch.device('cpu')))
model = converting_quantization(model)
return model
def calibration_for_quantization(
model,
data_loader,
device=torch.device('cpu'),
):
model.eval()
model = model.to(device)
with torch.no_grad():
for image, _ in tqdm(data_loader, total=len(data_loader)):
image = image.to(device)
_ = model(image)
return model
# only apply resnet based model
def fuse_modules(model: nn.Module, mode: str='eval'):
assert mode in ('eval', 'train')
model = model.cpu()
model.eval() if mode == 'eval' else model.train()
modules = [
['conv1', 'bn1'],
['layer1.0.conv1', 'layer1.0.bn1'],
['layer1.0.conv2', 'layer1.0.bn2'],
['layer1.1.conv1', 'layer1.1.bn1'],
['layer1.1.conv2', 'layer1.1.bn2'],
['layer2.0.conv1', 'layer2.0.bn1'],
['layer2.0.conv2', 'layer2.0.bn2'],
['layer2.0.downsample.0', 'layer2.0.downsample.1'],
['layer2.1.conv1', 'layer2.1.bn1'],
['layer2.1.conv2', 'layer2.1.bn2'],
['layer3.0.conv1', 'layer3.0.bn1'],
['layer3.0.conv2', 'layer3.0.bn2'],
['layer3.0.downsample.0', 'layer3.0.downsample.1'],
['layer3.1.conv1', 'layer3.1.bn1'],
['layer3.1.conv2', 'layer3.1.bn2'],
['layer4.0.conv1', 'layer4.0.bn1'],
['layer4.0.conv2', 'layer4.0.bn2'],
['layer4.0.downsample.0', 'layer4.0.downsample.1'],
['layer4.1.conv1', 'layer4.1.bn1'],
['layer4.1.conv2', 'layer4.1.bn2'],
]
try: # resnet based-model
model = torch.quantization.fuse_modules(model, modules)
except: # shufflenet
pass
return model
def print_size_of_model(model, label=''):
torch.save(model.state_dict(), 'temp.p')
size = os.path.getsize('temp.p')
print('model: ', label, ' \t', 'Size (KB):', size / 1e3)
os.remove('temp.p')
return size
def comparison_size_of_models(model_name: str, num_classes: int=33):
if model_name == 'shufflenet':
from ..models.shufflenet import ShuffleNetV2
float_model = ShuffleNetV2(num_classes=33, pre_trained=False, quantize=True)
elif model_name == 'resnet18':
from ..models.resnet import resnet18
float_model = resnet18(num_classes=33, quantize=True)
elif model_name == 'resnet50':
from ..models.resnet import resnet50
float_model = resnet50(num_classes=33, quantize=True)
else:
raise ValueError(f'model name {model_name} does not exists.')
prepared_model = prepare_ptq(float_model)
quantized_model = converting_quantization(prepared_model)
float_model.eval()
float_model = float_model.cpu()
quantized_model.eval()
quantized_model = quantized_model.cpu()
f = print_size_of_model(float_model, 'float32')
q = print_size_of_model(quantized_model, 'int8')
print("{0:.2f} times smaller".format(f / q))