-
Notifications
You must be signed in to change notification settings - Fork 0
/
all_models.py
372 lines (267 loc) · 12.1 KB
/
all_models.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.autograd as autograd
import numpy as np
def get_constraint(bits, obj):
if bits == 0:
return None
if 'activation' in obj:
lower = 0
upper = 2 ** bits
elif 'swish' in obj:
lower = -1
upper = 2 ** bits - 1
else:
lower = -2 ** (bits - 1) + 1
upper = 2 ** (bits - 1)
constraint = np.arange(lower, upper)
return constraint
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, in_channels, planes, stride=1, downsample=False, constr_activation=None):
super(BasicBlock, self).__init__()
self.quan_activation = constr_activation is not None
self.conv1 = Conv2d(in_channels, planes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(num_features=planes)
self.activation1 = LsqActivation(constr_activation) if self.quan_activation else nn.ReLU(inplace=True)
self.conv2 = Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(num_features=planes)
self.activation2 = LsqActivation(constr_activation) if self.quan_activation else nn.ReLU(inplace=True)
self.downsample = None
if downsample:
conv = Conv2d(in_channels, planes, kernel_size=1, stride=stride, padding=0, bias=False)
bn = nn.BatchNorm2d(num_features=planes)
self.downsample = nn.Sequential(*[conv, bn])
def forward(self, x):
residual = x if self.downsample is None else self.downsample(x)
out = self.conv1(x)
out = self.bn1(out)
out = self.activation1(out)
out = self.conv2(out)
out = self.bn2(out)
out += residual
out = self.activation2(out)
return out
def forward(self, x):
residual = x if self.downsample is None else self.downsample(x)
out = self.conv1(x)
out = self.bn1(out)
out=out+(self.bn1.running_mean*self.bn1.weight/((self.bn1.running_var+0.00001)**0.5)-self.bn1.bias).unsqueeze(
0).unsqueeze(2).unsqueeze(3).expand_as(out)
out = self.activation1(out)
out = self.conv2(out)
out = self.bn2(out)
out += residual
if self.downsample:
for name, module in self.downsample.named_children():
if "1" in name:
out=out+(module.running_mean * module.weight / ((module.running_var+0.00001)**0.5) - module.bias).unsqueeze(
0).unsqueeze(2).unsqueeze(3).expand_as(out)
out = out + (self.bn2.running_mean * self.bn2.weight / ((self.bn2.running_var+0.00001)**0.5) - self.bn2.bias).unsqueeze(
0).unsqueeze(2).unsqueeze(3).expand_as(out)
out = self.activation2(out)
return out
def forward2(self, x):
residual = x if self.downsample is None else self.downsample(x)
out = self.conv1(x)
out = self.bn1(out)
out = self.activation1(out)
out = self.conv2(out)
out = self.bn2(out)
out += residual
out = self.activation2(out)
return out
class Bottleneck(nn.Module):
expansion = 4
def __init__(self, in_channels, planes, stride=1, downsample=None, constr_activation=None):
super(Bottleneck, self).__init__()
self.quan_activation = constr_activation is not None
self.conv1 = Conv2d(in_channels, planes, kernel_size=1, stride=1, padding=0, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.activation1 = LsqActivation(constr_activation) if self.quan_activation else nn.ReLU(inplace=True)
self.conv2 = Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.activation2 = LsqActivation(constr_activation) if self.quan_activation else nn.ReLU(inplace=True)
self.conv3 = Conv2d(planes, planes * 4, kernel_size=1, stride=1, padding=0, bias=False)
self.bn3 = nn.BatchNorm2d(planes * 4)
self.activation3 = LsqActivation(constr_activation) if self.quan_activation else nn.ReLU(inplace=True)
self.downsample = None
if downsample:
conv = Conv2d(in_channels, planes * 4, kernel_size=1, stride=stride, padding=0, bias=False)
bn = nn.BatchNorm2d(num_features=planes * 4)
self.downsample = nn.Sequential(*[conv, bn])
def forward(self, x):
residual = x if self.downsample is None else self.downsample(x)
out = self.conv1(x)
out = self.bn1(out)
out = self.activation1(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.activation2(out)
out = self.conv3(out)
out = self.bn3(out)
out += residual
out = self.activation3(out)
return out
def make_layer(block, in_channels, planes, nblocks, stride=1, constr_activation=None):
layers = list()
downsample = stride != 1 or in_channels != planes * block.expansion
layers.append(block(in_channels, planes, stride, downsample, constr_activation))
in_channels = planes * block.expansion
for i in range(1, nblocks):
layers.append(block(in_channels, planes, constr_activation=constr_activation))
return nn.Sequential(*layers), planes * block.expansion
class ResNet(nn.Module):
def __init__(self, block, layers, num_classes=10, quan_first=False, quan_last=False, constr_activation=None):
super(ResNet, self).__init__()
self.quan_first = quan_first
self.quan_last = quan_last
self.quan_activation = constr_activation is not None
self.constr_activation = constr_activation
if self.quan_first:
self.first_act = LsqActivation(constr_activation) if self.quan_activation else _Identity()
self.conv1 = Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
else:
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
self.bn1 = nn.BatchNorm2d(64)
if self.quan_activation:
self.activation1 = LsqActivation(constr_activation)
else:
self.activation1 = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
in_channels = 64
self.layer1, in_channels = _make_layer(block, in_channels, planes=64, nblocks=layers[0],
stride=1, constr_activation=constr_activation)
self.layer2, in_channels = _make_layer(block, in_channels, planes=128, nblocks=layers[1],
stride=2, constr_activation=constr_activation)
self.layer3, in_channels = _make_layer(block, in_channels, planes=256, nblocks=layers[2],
stride=2, constr_activation=constr_activation)
self.layer4, in_channels = _make_layer(block, in_channels, planes=512, nblocks=layers[3],
stride=2, constr_activation=constr_activation)
self.avgpool = nn.AvgPool2d(7, stride=1)
if self.quan_last:
self.last_act = LsqActivation(constr_activation) if self.quan_activation else _Identity()
self.fc = Linear(512 * block.expansion, num_classes)
else:
self.fc = nn.Linear(512 * block.expansion, num_classes)
self._init_weight()
def _init_weight(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def forward(self, x):
if self.quan_first:
x = self.first_act(x)
x = self.conv1(x)
x = self.bn1(x)
x = self.activation1(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
if self.quan_last:
x = self.last_act(x)
x = self.fc(x)
return x
class LsqActivationFun(autograd.Function):
@staticmethod
def forward(ctx,x,scale,constraint):
ctx.constraint = constraint
x = x
scale = scale
x_scale = torch.div(x, scale)
x_clip = F.hardtanh(x_scale, 0, max_val=float((2**ctx.constraint-1)))
x_round = torch.round(x_clip)
x_restore = torch.mul(x_round, scale)
ctx.save_for_backward(x_clip)
return x_restore
@staticmethod
def backward(ctx, grad_output):
grad_top = grad_output
x_clip = ctx.saved_tensors[0]
internal_flag = ((x_clip > 0) ^ (x_clip >= float((2**ctx.constraint-1))))
# gradient for activation
grad_activation = grad_top * internal_flag
# gradient for scale
grad_one = x_clip * internal_flag
grad_two = torch.round(x_clip)
grad_scale_elem = grad_two - grad_one
grad_scale = (grad_scale_elem * grad_top).sum().view((1,))
grad_scale=grad_scale/((len(x_clip)*float((2**ctx.constraint-1)))**0.5)
return grad_activation, grad_scale, None
class Identity(nn.Module):
def forward(self, x):
return x
class LsqActivation(nn.Module):
def __init__(self, constraint, scale_init=None, skip_bit=None):
super(LsqActivation, self).__init__()
self.constraint = constraint
# scale_init = scale_init if scale_init is not None else torch.ones(1) * 100 / float(2 ** self.constraint - 1)
scale_init = scale_init if scale_init is not None else torch.ones(1)
self.scale = nn.Parameter(scale_init)
self.skip_bit = skip_bit
self.output=None
def extra_repr(self):
return 'constraint=%s' % self.constraint
def forward(self, x):
a=LsqActivationFun.apply(x,self.scale,self.constraint)
self.output=a
return a
class SNNActivationFun(autograd.Function):
def __init__(self, mem, th):
super(SNNActivationFun, self).__init__()
# self.valmin = float(0)
self.mem = mem
self.th=th
def forward(self, *args, **kwargs):
mem = args[0]
# return mem.gt(self.th).float()
return (mem >= self.th).float()
def backward(self, *grad_outputs):
grad_activation=1
grad_scale=1
return None, None
class SNNActivation(nn.Module):
def __init__(self, mem, spike, sum_spike, th, constraint):
super(SNNActivation, self).__init__()
self.mem=mem
self.spike=spike
self.sum_spike=sum_spike
self.th=th
self.constraint = constraint
def forward(self, x):
# self.mem = self.mem - self.spike + x +self.th/18
self.mem = self.mem - self.spike + x
spike = SNNActivationFun(self.mem, self.th)
self.spike=spike.forward(self.mem, self.th)*self.th
# self.spike = self.spike.mul((self.sum_spike<((2**self.constraint-1.5)*self.th)).float())
self.spike = self.spike.float()
# neg_spike=((-self.mem)>0).float().mul((self.sum_spike > (0.5*self.th)).float()) *self.th
# self.spike = self.spike - neg_spike
self.sum_spike=self.sum_spike+self.spike
return self.spike
# def backward(self, *grad_outputs):
class Conv2d(nn.Conv2d):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1,
padding=1, dilation=1, groups=1, bias=False):
super(Conv2d, self).__init__(in_channels, out_channels, kernel_size,stride,
padding, dilation, groups, bias)
self.wquantizer = None
def forward(self, x):
weight = self.weight if self.wquantizer is None else self.wquantizer(self.weight)
return F.conv2d(x, weight, self.bias, self.stride, self.padding, self.dilation, self.groups)
class Linear(nn.Linear):
def __init__(self, in_features, out_features, bias=True):
super(Linear, self).__init__(in_features, out_features, bias)
self.wquantizer = None
def forward(self, x):
weight = self.weight if self.wquantizer is None else self.wquantizer(self.weight)
return F.linear(x, weight, self.bias)