-
Notifications
You must be signed in to change notification settings - Fork 14
/
pruning_utils.py
160 lines (118 loc) · 4.31 KB
/
pruning_utils.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
'''
functions for pruning related operations
'''
import copy
import torch
import torch.nn as nn
import torch.nn.utils.prune as prune
__all__ = ['pruning_model', 'prune_model_custom', 'pruning_model_random', 'remove_prune',
'extract_mask', 'reverse_mask', 'extract_main_weight',
'check_sparsity']
def pruning_model(model, px, conv1=False):
parameters_to_prune =[]
for name,m in model.named_modules():
if isinstance(m, nn.Conv2d):
if name == 'conv1':
if conv1:
parameters_to_prune.append((m,'weight'))
else:
print('skip conv1 for L1 unstructure global pruning')
else:
parameters_to_prune.append((m,'weight'))
parameters_to_prune = tuple(parameters_to_prune)
prune.global_unstructured(
parameters_to_prune,
pruning_method=prune.L1Unstructured,
amount=px,
)
def pruning_model_random(model, px, conv1=False):
parameters_to_prune =[]
for name,m in model.named_modules():
if isinstance(m, nn.Conv2d):
if name == 'conv1':
if conv1:
parameters_to_prune.append((m,'weight'))
else:
print('skip conv1 for L1 unstructure global pruning')
else:
parameters_to_prune.append((m,'weight'))
parameters_to_prune = tuple(parameters_to_prune)
prune.global_unstructured(
parameters_to_prune,
pruning_method=prune.RandomUnstructured,
amount=px,
)
def prune_model_custom(model, mask_dict, conv1=False):
print('start unstructured pruning with custom mask')
for name,m in model.named_modules():
if isinstance(m, nn.Conv2d):
if name == 'conv1':
if conv1:
prune.CustomFromMask.apply(m, 'weight', mask=mask_dict[name+'.weight_mask'])
else:
print('skip conv1 for custom pruning')
else:
prune.CustomFromMask.apply(m, 'weight', mask=mask_dict[name+'.weight_mask'])
def remove_prune(model, conv1=False):
print('remove pruning')
for name,m in model.named_modules():
if isinstance(m, nn.Conv2d):
if name == 'conv1':
if conv1:
prune.remove(m,'weight')
else:
print('skip conv1 for remove pruning')
else:
prune.remove(m,'weight')
def extract_mask(model_dict):
new_dict = {}
for key in model_dict.keys():
if 'mask' in key:
if 'module' in key:
new_key = key[len('module.'):]
else:
new_key = key
new_dict[new_key] = copy.deepcopy(model_dict[key])
return new_dict
def reverse_mask(mask_dict):
new_dict = {}
for key in mask_dict.keys():
new_dict[key] = 1 - mask_dict[key]
return new_dict
def extract_main_weight(model_dict, fc=False, conv1=False):
new_dict = {}
for key in model_dict.keys():
if not 'mask' in key:
if not 'normalize' in key:
if 'module' in key:
new_key = key[len('module.'):]
else:
new_key = key
new_dict[new_key] = copy.deepcopy(model_dict[key])
delete_keys = []
if not fc:
for key in new_dict.keys():
if 'fc' in key:
delete_keys.append(key)
if not conv1:
delete_keys.append('conv1.weight')
for key in delete_keys:
print('delete key = {}'.format(key))
del new_dict[key]
return new_dict
def check_sparsity(model, conv1=True):
sum_list = 0
zero_sum = 0
for name,m in model.named_modules():
if isinstance(m, nn.Conv2d):
if name == 'conv1':
if conv1:
sum_list = sum_list+float(m.weight.nelement())
zero_sum = zero_sum+float(torch.sum(m.weight == 0))
else:
print('skip conv1 for sparsity checking')
else:
sum_list = sum_list+float(m.weight.nelement())
zero_sum = zero_sum+float(torch.sum(m.weight == 0))
print('* remain weight = ', 100*(1-zero_sum/sum_list),'%')
return 100*(1-zero_sum/sum_list)