-
Notifications
You must be signed in to change notification settings - Fork 10
/
quant_model.py
64 lines (60 loc) · 2.31 KB
/
quant_model.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
# *
# @file Different utility functions
# Copyright (c) Yaohui Cai, Zhewei Yao, Zhen Dong, Amir Gholami
# All rights reserved.
# This file is part of ZeroQ repository.
#
# ZeroQ is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ZeroQ is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ZeroQ repository. If not, see <http://www.gnu.org/licenses/>.
# *
import torch
import torch.nn as nn
import copy
from quant_modules import TensorQuantizer, Conv2dQuantizer, LinearQuantizer, ActivationQuantizer
from quant_utils import quant_args
def quantize_model(model):
"""
Recursively quantize a pretrained single-precision model to int8 quantized model
model: pretrained single-precision model
"""
# quantize convolutional and linear layers to 8-bit
if type(model) == nn.Conv2d:
quant_mod = Conv2dQuantizer(**quant_args)
quant_mod.set_param(model)
return quant_mod
elif type(model) == nn.Linear:
quant_mod = LinearQuantizer(**quant_args)
quant_mod.set_param(model)
return quant_mod
# recursively use the quantized module to replace the single-precision module
elif type(model) == nn.Sequential:
mods = []
for n, m in model.named_children():
mods.append(quantize_model(m))
return nn.Sequential(*mods)
else:
q_model = copy.deepcopy(model)
for attr in dir(model):
mod = getattr(model, attr)
if isinstance(mod, nn.Module) and 'norm' not in attr:
setattr(q_model, attr, quantize_model(mod))
return q_model
def set_first_last_layer(model):
module_list = []
for m in model.modules():
if isinstance(m, Conv2dQuantizer):
module_list += [m]
if isinstance(m, LinearQuantizer):
module_list += [m]
module_list[0].quant_input.is_enable = False
module_list[-1].quant_input.bit = torch.tensor(8)