-
Notifications
You must be signed in to change notification settings - Fork 92
/
prune_utils.py
227 lines (186 loc) · 7.67 KB
/
prune_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
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
import torch
import torch.nn as nn
from layerwrapper import WrappedLayer
def find_layers(module, layers=[nn.Linear], name=''):
if type(module) in layers:
return {name: module}
res = {}
for name1, child in module.named_children():
res.update(find_layers(
child, layers=layers, name=name + '.' + name1 if name != '' else name1
))
return res
def check_sparsity(model):
subset = find_layers(model, layers=[nn.Linear])
zero_cnt = 0
fc_params = 0
for name in subset:
W = subset[name].weight.data
if W.shape[0] == 1000:
continue
zero_cnt += (W==0).sum().item()
fc_params += W.numel()
return float(zero_cnt) / fc_params
def compute_mask(W_metric, prune_granularity, sparsity):
if prune_granularity == "layer":
thres = torch.sort(W_metric.flatten().cuda())[0][int(W_metric.numel() * sparsity)].cpu()
W_mask = (W_metric <= thres)
return W_mask
elif prune_granularity == "row":
W_mask = (torch.zeros_like(W_metric)==1)
sort_res = torch.sort(W_metric, dim=-1, stable=True)
indices = sort_res[1][:,:int(W_metric.shape[1]*sparsity)]
W_mask.scatter_(1, indices, True)
return W_mask
def prune_deit(args, model, calib_data, device):
inps = calib_data
bs = inps.shape[0]
require_forward = (args.prune_metric in ["wanda"])
metric_stats = []
for blk in model.blocks:
subset = find_layers(blk)
res_per_layer = {}
for name in subset:
res_per_layer[name] = torch.abs(subset[name].weight.data)
metric_stats.append(res_per_layer)
thresh = None
#####################################
inps = model.patch_embed(inps)
cls_tokens = model.cls_token.expand(bs, -1, -1) # stole cls_tokens impl from Phil Wang, thanks
dist_token = model.dist_token.expand(bs, -1, -1)
inps = torch.cat((cls_tokens, dist_token, inps), dim=1)
inps = inps + model.pos_embed
inps = model.pos_drop(inps)
for block_id, blk in enumerate(model.blocks):
subset = find_layers(blk)
if require_forward:
wrapped_layers = {}
for name in subset:
wrapped_layers[name] = WrappedLayer(subset[name])
def add_batch(name):
def tmp(_, inp, out):
wrapped_layers[name].add_batch(inp[0].data, out.data)
return tmp
handles = []
for name in wrapped_layers:
handles.append(subset[name].register_forward_hook(add_batch(name)))
if bs > 256:
tmp_res = []
for i1 in range(0, bs, 256):
j1 = min(i1+256, bs)
tmp_res.append(blk(inps[i1:j1]))
inps = torch.cat(tmp_res, dim=0)
else:
inps = blk(inps)
for h in handles:
h.remove()
################# pruning ###################
for name in subset:
if args.prune_metric == "wanda":
metric_stats[block_id][name] *= torch.sqrt(wrapped_layers[name].scaler_row.reshape((1,-1)))
W_mask = compute_mask(metric_stats[block_id][name], args.prune_granularity, args.sparsity)
subset[name].weight.data[W_mask] = 0
def prune_vit(args, model, calib_data, device):
inps = calib_data
bs = inps.shape[0]
require_forward = (args.prune_metric in ["wanda"])
metric_stats = []
for blk in model.blocks:
subset = find_layers(blk)
res_per_layer = {}
for name in subset:
res_per_layer[name] = torch.abs(subset[name].weight.data)
metric_stats.append(res_per_layer)
thresh = None
#####################################
inps = model.patch_embed(inps)
cls_tokens = model.cls_token.expand(bs, -1, -1) # stole cls_tokens impl from Phil Wang, thanks
inps = torch.cat((cls_tokens, inps), dim=1)
inps = inps + model.pos_embed
inps = model.pos_drop(inps)
for block_id, blk in enumerate(model.blocks):
print(f"block {block_id}")
subset = find_layers(blk)
if require_forward:
wrapped_layers = {}
for name in subset:
wrapped_layers[name] = WrappedLayer(subset[name])
def add_batch(name):
def tmp(_, inp, out):
wrapped_layers[name].add_batch(inp[0].data, out.data)
return tmp
handles = []
for name in wrapped_layers:
handles.append(subset[name].register_forward_hook(add_batch(name)))
if bs > 256:
tmp_res = []
for i1 in range(0, bs, 256):
j1 = min(i1+256, bs)
tmp_res.append(blk(inps[i1:j1]))
inps = torch.cat(tmp_res, dim=0)
else:
inps = blk(inps)
for h in handles:
h.remove()
################# pruning ###################
for name in subset:
if args.prune_metric == "wanda":
metric_stats[block_id][name] *= torch.sqrt(wrapped_layers[name].scaler_row.reshape((1,-1)))
W_mask = compute_mask(metric_stats[block_id][name], args.prune_granularity, args.sparsity)
subset[name].weight.data[W_mask] = 0
##############################################
def prune_convnext(args, model, calib_data, device):
inps = calib_data
bs = inps.shape[0]
require_forward = (args.prune_metric in ["wanda"])
##############################################################
metric_stats = []
for block_id in range(4):
subset = find_layers(model.stages[block_id])
res_per_layer = {}
for name in subset:
res_per_layer[name] = torch.abs(subset[name].weight.data)
metric_stats.append(res_per_layer)
##############################################################
thresh = None
for block_id in range(4):
print(f"block {block_id}")
subset = find_layers(model.stages[block_id])
if require_forward:
layer = model.downsample_layers[block_id]
if bs > 1024:
tmp_res = []
for i1 in range(0, bs, 512):
j1 = min(i1+512, bs)
tmp_res.append(layer(inps[i1:j1]))
inps = torch.cat(tmp_res, dim=0)
else:
inps = layer(inps)
wrapped_layers = {}
for name in subset:
wrapped_layers[name] = WrappedLayer(subset[name])
def add_batch(name):
def tmp(_, inp, out):
wrapped_layers[name].add_batch(inp[0].data, out.data)
return tmp
handles = []
for name in wrapped_layers:
handles.append(subset[name].register_forward_hook(add_batch(name)))
layer = model.stages[block_id]
if bs > 1024:
tmp_res = []
for i1 in range(0, bs, 512):
j1 = min(i1+512, bs)
tmp_res.append(layer(inps[i1:j1]))
inps = torch.cat(tmp_res, dim=0)
else:
inps = layer(inps)
for h in handles:
h.remove()
################# pruning ###################
for name in subset:
if args.prune_metric == "wanda":
metric_stats[block_id][name] *= torch.sqrt(wrapped_layers[name].scaler_row.reshape((1,-1)))
W_mask = compute_mask(metric_stats[block_id][name], args.prune_granularity, args.sparsity)
subset[name].weight.data[W_mask] = 0
##############################################