-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauto_quant.py
More file actions
133 lines (104 loc) · 4.23 KB
/
auto_quant.py
File metadata and controls
133 lines (104 loc) · 4.23 KB
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
# (C) Copyright 2020 EdgeCortix Inc.
#
# 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 torch
import torch.nn as nn
from torch.quantization import QuantStub, DeQuantStub
from torch.quantization import default_qconfig, quantize, default_eval_fn
from torch.quantization._quantize_script import quantize_script, script_qconfig
from torchvision import models
from torchvision.models.quantization import resnet as qresnet
import numpy as np
class ConvModel(torch.nn.Module):
def __init__(self):
super(ConvModel, self).__init__()
self.conv = torch.nn.Conv2d(3, 3, 3, bias=False).to(dtype=torch.float)
def forward(self, x):
x = self.conv(x)
return x
class AnnotatedConvModel(torch.nn.Module):
def __init__(self):
super(AnnotatedConvModel, self).__init__()
self.qconfig = default_qconfig
self.conv = torch.nn.Conv2d(3, 3, 3, bias=False).to(dtype=torch.float)
self.quant = QuantStub()
self.dequant = DeQuantStub()
def forward(self, x):
x = self.quant(x)
x = self.conv(x)
x = self.dequant(x)
return x
def fuse_model(self):
pass
class ConvBNReLU(nn.Sequential):
def __init__(self, in_planes, out_planes, kernel_size=3, stride=1, groups=1):
padding = (kernel_size - 1) // 2
super().__init__(
nn.Conv2d(in_planes, out_planes, kernel_size, stride, padding, groups=groups, bias=False),
nn.BatchNorm2d(out_planes, momentum=0.1),
nn.ReLU(inplace=False)
)
class AnnotatedConvBnModel(torch.nn.Module):
def __init__(self):
super(AnnotatedConvBnModel, self).__init__()
self.qconfig = default_qconfig
self.block = ConvBNReLU(3, 3)
self.quant = QuantStub()
self.dequant = DeQuantStub()
def forward(self, x):
x = self.quant(x)
x = self.block(x)
x = self.dequant(x)
return x
def fuse_model(self):
torch.quantization.fuse_modules(self.block, ['0', '1', '2'], inplace=True)
def quantize_model(model, inp, per_channel=False, dummy=True):
model.fuse_model()
model.qconfig = default_qconfig
torch.quantization.prepare(model, inplace=True)
model(inp)
torch.quantization.convert(model, inplace=True)
def quantize_and_run(model_eager, raw_model, img_data, do_eager=False):
qconfig_dict = {'': default_qconfig}
model_traced = torch.jit.trace(raw_model, img_data[0][0])
torch._C._jit_pass_inline(model_traced.graph)
model_quantized = quantize_script(
model_traced,
qconfig_dict,
default_eval_fn,
[img_data],
inplace=False)
result_traced = model_quantized(img_data[0][0])
torch._C._jit_pass_inline(model_quantized.graph)
print(model_quantized.graph)
if do_eager:
quantize_model(model_eager, img_data[0][0])
result_eager = model_eager(img_data[0][0])
np.allclose(result_traced.numpy(), result_eager.numpy())
def test_conv():
img_data = [(torch.rand(2, 3, 10, 10, dtype=torch.float),
torch.randint(0, 1, (2,), dtype=torch.long))
for _ in range(2)]
annotated_conv_model = AnnotatedConvModel().eval()
conv_model = ConvModel().eval()
conv_model.conv.weight = torch.nn.Parameter(annotated_conv_model.conv.weight.detach())
quantize_and_run(annotated_conv_model, conv_model, img_data, True)
def test_resnet():
img_data = [(torch.rand(1, 3, 224, 224, dtype=torch.float),
torch.randint(0, 1, (2,), dtype=torch.long))
for _ in range(5)]
annotated_model = qresnet.resnet18(pretrained=True).eval()
raw_model = models.resnet.resnet18(pretrained=True).eval()
quantize_and_run(annotated_model, raw_model, img_data, True)
test_conv()
test_resnet()