-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
410 lines (325 loc) · 17.6 KB
/
utils.py
File metadata and controls
410 lines (325 loc) · 17.6 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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import torch
import torch.nn as nn
import pruning
from typing import Union, Dict, Any
from torch.utils.data import DataLoader
import hardening
import handlers
import logging
class prune_utils():
def __init__(self,
model: nn.Module,
trainloader: DataLoader,
classes_count: int,
pruning_method: str,
device: Union[torch.device, str] = torch.device("cuda")) -> None:
if not isinstance(pruning_method, str):
raise TypeError(f"Expected 'pruning_method' to be a string, got {type(pruning_method).__name__}")
self.model = model
self.trainloader = trainloader
self.classes_count = classes_count
self.pruning_method = pruning_method #hm: homogeneous, ht: heterogeneous
self.conv_count = 0
self.conv_layers = []
self.bn_layers = []
self.fc_layers = []
self.importance = {}
self.device = device
def set_pruning_ratios(self,
pruning_params: Union[float, list]) -> None:
if not isinstance(pruning_params, list) and not isinstance(pruning_params, float):
raise TypeError(f"Expected 'pruning_params' to be a float, got {type(pruning_params).__name__}")
if self.pruning_method == "hm":
self.conv_pruning_ratio = pruning_params
#self.fc_pruning_ratio = pruning_params[1]
elif self.pruning_method == "ht":
self.conv_count = 0
self._conv_layer_counter(self.model)
assert self.conv_count != 0
self.conv_pruning_ratio = pruning_params[:self.conv_count]
#self.fc_pruning_ratio = pruning_params[self.conv_count:]
def _conv_layer_counter(self,
model: nn.Module) -> None:
for _, layer in model.named_children():
if list(layer.children()) == []:
if isinstance(layer, nn.Conv2d):
self.conv_count += 1
else:
self._conv_layer_counter(layer)
def _get_separated_layers(self,
model: nn.Module,
name: str = '') -> None:
for name1, layer in model.named_children():
if list(layer.children()) == []:
if isinstance(layer, nn.Conv2d):
name_ = name + name1
self.conv_layers.append([name_, layer])
elif isinstance(layer, nn.BatchNorm2d):
self.bn_layers.append(layer)
elif isinstance(layer, nn.Linear):
self.fc_layers.append(layer)
elif isinstance(layer, nn.Module) and hasattr(layer, 'conv1') and hasattr(layer, 'conv2'): #BasicBlocks in ResNet
name_ = name + name1
self.conv_layers.append([name_, layer])
else:
name += name1 + "."
self._get_separated_layers(layer, name)
def _get_separated_layers_reversed(self,
model: nn.Module,
name: str = '') -> None:
for name1, layer in reversed(list(model.named_children())):
if list(layer.children()) == []:
if isinstance(layer, nn.Conv2d):
name_ = name + name1
self.conv_layers.append([name_, layer])
elif isinstance(layer, nn.BatchNorm2d):
self.bn_layers.append(layer)
elif isinstance(layer, nn.Linear):
self.fc_layers.append(layer)
elif isinstance(layer, nn.Module) and hasattr(layer, 'conv1') and hasattr(layer, 'conv2'): #BasicBlocks in ResNet
name_ = name + name1
self.conv_layers.append([name_, layer])
else:
name += name1 + "."
self._get_separated_layers_reversed(layer, name)
def _reset_params(self):
self.conv_count = 0
self.conv_layers = []
self.bn_layers = []
self.fc_layers = []
def _conv_prune(self,
conv_layer: nn.Conv2d,
next_conv: Union[nn.Conv2d, nn.Linear]) -> None:
new_out_channel = conv_layer.out_channels - int(conv_layer.out_channels * self.conv_pruning_ratio)
conv_layer.weight.set_(conv_layer.weight.detach()[:new_out_channel])
if conv_layer.bias is not None:
conv_layer.bias.set_(conv_layer.bias.detach()[:new_out_channel])
conv_layer.out_channels = new_out_channel
if next_conv is not None:
if isinstance(next_conv, nn.Conv2d):
next_conv.weight.set_(next_conv.weight.detach()[:, :new_out_channel])
next_conv.in_channels = new_out_channel
elif isinstance(next_conv, nn.Linear):
next_conv.weight.set_(next_conv.weight.detach()[:, :new_out_channel])
next_conv.in_features = new_out_channel
def _bn_prune(self, bn_layer: nn.BatchNorm2d) -> None:
new_out_channel = bn_layer.num_features - int(bn_layer.num_features * self.conv_pruning_ratio)
bn_layer.weight.set_(bn_layer.weight.detach()[:new_out_channel])
bn_layer.bias.set_(bn_layer.bias.detach()[:new_out_channel])
bn_layer.running_mean.set_(bn_layer.running_mean.detach()[:new_out_channel])
bn_layer.running_var.set_(bn_layer.running_var.detach()[:new_out_channel])
bn_layer.num_features = new_out_channel
'''
iterates over the model,
applies homogeneous pruning to conv layers,
returns the pruned model
'''
def homogeneous_prune(self,
model: nn.Module) -> nn.Module:
self._reset_params()
self._get_separated_layers(model)
assert len(self.conv_layers) != 0
assert len(self.fc_layers) != 0
with torch.no_grad():
for i in range(len(self.conv_layers)): # - 1):
conv_layer = self.conv_layers[i][1]
if i + 1 < len(self.conv_layers):
next_conv = self.conv_layers[i+1][1]
else:
next_conv = self.fc_layers[0]
if "BasicBlock" in type(conv_layer).__name__:
self._conv_prune(conv_layer.conv1, conv_layer.conv2)
self._bn_prune(conv_layer.bn1)
if "BasicBlock" in type(next_conv).__name__:
self._conv_prune(conv_layer.conv2, next_conv.conv1)
self._bn_prune(conv_layer.bn2)
else:
self._conv_prune(conv_layer.conv2, next_conv)
self._bn_prune(conv_layer.bn2)
if conv_layer.downsample:
for _, sub_layer in conv_layer.downsample.named_children():
if isinstance(sub_layer, nn.Conv2d):
sub_layer.weight.set_(sub_layer.weight.detach()[:, :conv_layer.conv1.in_channels])
sub_layer.in_channels = conv_layer.conv1.in_channels
self._conv_prune(sub_layer, None)
elif isinstance(sub_layer, nn.BatchNorm2d):
self._bn_prune(sub_layer)
else:
if "BasicBlock" in type(next_conv).__name__:
self._conv_prune(conv_layer, next_conv.conv1)
self._bn_prune(self.bn_layers[i])
else:
self._conv_prune(conv_layer, next_conv)
self._bn_prune(self.bn_layers[i])
return model
'''
sorts channels in conv layers based on an importance metric
'''
def channel_sorting(self,
model: nn.Module,
handler: handlers.AnalysisHandler,
logger: logging.Logger,
command: str="l1-norm",
pruning_ratio: float=0,
hardening_ratio: float=0,
) -> nn.Module:
self._reset_params()
if command == "l1-norm":
sort_index_conv_dict = handler.execute(command, model, ...)
self._get_separated_layers(model)
elif command == "vul-gain":
sort_index_conv_dict = handler.execute(command, model, self.trainloader, self.classes_count, self.device)
self._get_separated_layers(model)
elif command == "salience":
sort_index_conv_dict = handler.execute(command, model, self.classes_count, self.device)
self._get_separated_layers_reversed(model)
self.conv_layers.reverse()
self.bn_layers.reverse()
self.fc_layers.reverse()
elif command == "deepvigor":
for data in self.trainloader:
inputs = data[0].to(self.device)
break
torch.cuda.empty_cache()
sort_index_conv_dict = handler.execute(command, model, inputs, self.classes_count, self.device, logger, pruning_ratio, hardening_ratio)
self._get_separated_layers(model)
else:
raise Exception(f"Unexpected analysis command is given: {command}")
assert len(self.conv_layers) != 0
assert len(self.fc_layers) != 0
with torch.no_grad():
for i in range(len(self.conv_layers)):
conv_name = self.conv_layers[i][0]
conv_layer = self.conv_layers[i][1]
if i + 1 < len(self.conv_layers):
next_conv = self.conv_layers[i+1][1]
else:
next_conv = self.fc_layers[0]
if "BasicBlock" in type(conv_layer).__name__: #for resnet
layer_name = conv_name + ".conv1"
sort_index_conv = sort_index_conv_dict[layer_name]
pruning.out_channel_sorting(conv_layer.conv1, sort_index_conv)
pruning.batchnorm_sorting(conv_layer.bn1, sort_index_conv)
pruning.in_channel_sorting(conv_layer.conv2, sort_index_conv)
if conv_layer.downsample:
layer_name = conv_name + ".conv2"
sort_index_conv = sort_index_conv_dict[layer_name]
pruning.out_channel_sorting(conv_layer.conv2, sort_index_conv)
pruning.batchnorm_sorting(conv_layer.bn2, sort_index_conv)
for _, sub_layer in conv_layer.downsample.named_children():
if isinstance(sub_layer, nn.Conv2d):
pruning.in_channel_sorting(sub_layer, last_sort_index_conv)
pruning.out_channel_sorting(sub_layer, sort_index_conv)
elif isinstance(sub_layer, nn.BatchNorm2d):
pruning.batchnorm_sorting(sub_layer, sort_index_conv)
if "BasicBlock" in type(next_conv).__name__:
pruning.in_channel_sorting(next_conv.conv1, sort_index_conv)
else:
pruning.in_channel_sorting(next_conv, sort_index_conv)
last_sort_index_conv = sort_index_conv
else:
pruning.out_channel_sorting(conv_layer.conv2, last_sort_index_conv)
pruning.batchnorm_sorting(conv_layer.bn2, last_sort_index_conv)
if "BasicBlock" in type(next_conv).__name__:
pruning.in_channel_sorting(next_conv.conv1, last_sort_index_conv)
else:
pruning.in_channel_sorting(next_conv, last_sort_index_conv)
else:
sort_index_conv = sort_index_conv_dict[conv_name]
pruning.out_channel_sorting(conv_layer, sort_index_conv)
pruning.batchnorm_sorting(self.bn_layers[i], sort_index_conv)
if "BasicBlock" in type(next_conv).__name__:
pruning.in_channel_sorting(next_conv.conv1, sort_index_conv)
else:
pruning.in_channel_sorting(next_conv, sort_index_conv)
last_sort_index_conv = sort_index_conv
logger.info("channels are sorted")
return model
class hardening_utils():
def __init__(self,
hardening_ratio: float,
clipping_command: str="ranger") -> None:
self.hardening_ratio = hardening_ratio
self.clipping = clipping_command
self.relu_thresholds = {}
def conv_replacement(self, model: nn.Module):
if self.hardening_ratio != 0:
for name, layer in model.named_children():
if list(layer.children()) == []:
if isinstance(layer, nn.Conv2d):
hardened_layer = hardening.HardenedConv2d(
in_channels=layer.in_channels,
out_channels=layer.out_channels,
kernel_size=layer.kernel_size,
stride=layer.stride,
padding=layer.padding,
dilation=layer.dilation,
groups=layer.groups,
bias=(layer.bias is not None),
padding_mode=layer.padding_mode,
)
hardened_layer.weight.data = layer.weight.data.clone()
if layer.bias is not None:
hardened_layer.bias.data = layer.bias.data.clone()
hardened_layer = self.hardening_conv(hardened_layer)
# Replace the module in the model
setattr(model, name, hardened_layer)
else:
self.conv_replacement(layer)
return model
# duplicating the weights' output channels based on hardening ratio
# the model is already sorted, so the first hardening_ratio% should be replicated
def hardening_conv(self, layer: nn.Conv2d):
duplication_count = int(layer.out_channels * self.hardening_ratio)
duplication_count = 1 if duplication_count == 0 else duplication_count
layer.duplicated_channels = duplication_count
new_out_channels = layer.out_channels + duplication_count
_, b, c, d = layer.weight.size()
new_weight = torch.zeros(new_out_channels, b, c, d)
new_bias = torch.zeros(new_out_channels)
# duplicating the channels
with torch.no_grad():
for i in range(duplication_count):
new_weight[i] = layer.weight[i]
new_weight[duplication_count + i] = layer.weight[i]
if layer.bias is not None:
new_bias[i] = layer.bias[i]
new_bias[duplication_count + i] = layer.bias[i]
new_weight[2*duplication_count:] = layer.weight[duplication_count:]
if layer.bias is not None:
new_bias[2*duplication_count:] = layer.bias[duplication_count:]
layer.weight = nn.Parameter(new_weight)
if layer.bias is not None:
layer.bias = nn.Parameter(new_bias)
return layer
def thresholds_extraction(self,
model: nn.Module,
handler: handlers.ClippingHandler,
clipping_command: str,
dataloader: DataLoader,
device: Union[torch.device, str],
logger: logging.Logger) -> None:
self.relu_thresholds = {}
if clipping_command == "ranger":
self.relu_thresholds = handler.execute(clipping_command, model, dataloader, device, logger)
def relu_replacement(self,
model: nn.Module,
name: str = '') -> nn.Module:
for name1, layer in model.named_children():
if list(layer.children()) == []:
if isinstance(layer, nn.ReLU):
hardened_relu = hardening.RangerReLU(
inplace=layer.inplace
)
name_ = name + name1
hardened_relu = self.hardening_relu(hardened_relu, self.relu_thresholds[name_].item())
# Replace the module in the model
setattr(model, name1, hardened_relu)
else:
name += name1 + "."
self.relu_replacement(layer, name)
return model
def hardening_relu(self, relu: nn.ReLU, threshold: float):
hardened_relu = hardening.RangerReLU(inplace=relu.inplace)
hardened_relu.clipping_threshold = threshold
return hardened_relu