-
Notifications
You must be signed in to change notification settings - Fork 0
/
DETR_PTQ.py
370 lines (287 loc) · 13.1 KB
/
DETR_PTQ.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
import argparse
from transformers import DetrForObjectDetection
from datasets.coco import build as build_dataset
from gptq.gptq import *
from gptq.modelutils import *
from gptq.quant import *
@torch.no_grad()
def detr_sequential(args, model, dataloader, dev):
print('Starting ...')
layers = torch.nn.ModuleList()
backbone_idx = -1
input_projection_idx = -1
encoder_idx = -1
decoder_idx = -1
label_classifier_idx = -1
bbox_predictor_idx = -1
# Input encoder
inps_encoder = [None] * args.nsamples
inps_attention_mask = [None] * args.nsamples
inps_object_queries = [None] * args.nsamples
# Input backbone
inps_pixel = [None] * args.nsamples
inps_pixel_mask = [None] * args.nsamples
# Input output head
inps_output_head = [None] * args.nsamples
cache = {'i': 0, "queries": None, "query_position_embeddings": None}
if args.transformer:
print('Transformers inputs')
layers = model.model.encoder.layers + model.model.decoder.layers
encoder_idx = 0
decoder_idx = 6
class CatcherTransformer(nn.Module):
def __init__(self, module):
super().__init__()
self.module = module
def forward(self, inp, attention_mask, **kwargs):
if 'query_position_embeddings' in kwargs.keys(): # Decoder
cache["queries"] = inp
cache['query_position_embeddings'] = kwargs['query_position_embeddings']
raise ValueError
inps_encoder[cache['i']] = inp.cpu()
inps_attention_mask[cache['i']] = attention_mask.cpu()
inps_object_queries[cache['i']] = kwargs['object_queries'].cpu()
cache['i'] += 1
raise ValueError
## Encoder
layers[encoder_idx] = CatcherTransformer(layers[encoder_idx])
model.model.encoder.layers[0] = layers[encoder_idx]
for batch in dataloader:
try:
model(batch[0].to(dev))
except ValueError:
pass
model.model.encoder.layers[0] = layers[encoder_idx].module
layers[encoder_idx] = layers[encoder_idx].module
torch.cuda.empty_cache()
## Decoder
layers[decoder_idx] = CatcherTransformer(layers[decoder_idx])
model.model.decoder.layers[0] = layers[decoder_idx]
for batch in dataloader:
try:
model(batch[0].to(dev))
except ValueError:
pass
model.model.decoder.layers[0] = layers[decoder_idx].module
layers[decoder_idx] = layers[decoder_idx].module
torch.cuda.empty_cache()
if args.backbone:
model.model.backbone.to(dev)
model.model.input_projection.to(dev)
backbone_idx = 0
input_projection_idx = 1
if args.transformer:
encoder_idx += 2
decoder_idx += 2
# Add input projection to layers
layers.insert(0, model.model.input_projection)
print('Backbone inputs')
# Add backbone to layers
layers.insert(0, model.model.backbone)
cache['i'] = 0
class CatcherBackbone(nn.Module):
def __init__(self, module):
super().__init__()
self.module = module
def forward(self, pixel_values, pixel_mask, **kwargs):
inps_pixel[cache['i']] = pixel_values.cpu()
inps_pixel_mask[cache['i']] = pixel_mask.cpu()
cache['i'] += 1
raise ValueError
##Backbone
layers[backbone_idx] = CatcherBackbone(layers[backbone_idx])
model.model.backbone = layers[backbone_idx]
for batch in dataloader:
try:
model(batch[0].to(dev))
except ValueError:
pass
model.model.backbone = layers[backbone_idx].module
layers[backbone_idx] = layers[backbone_idx].module
torch.cuda.empty_cache()
if args.output_head:
layers = layers.append(model.class_labels_classifier)
layers = layers.append(model.bbox_predictor)
label_classifier_idx = 0
bbox_predictor_idx = 1
if args.backbone:
label_classifier_idx += 2
bbox_predictor_idx += 2
if args.transformer:
label_classifier_idx += 12
bbox_predictor_idx += 12
if not args.transformer:
print('Output head inputs')
cache['i'] = 0
class CatcherHead(nn.Module):
def __init__(self, module):
super().__init__()
self.module = module
def forward(self, inp, **kwargs):
inps_output_head[cache['i']] = inp.cpu()
cache['i'] += 1
raise ValueError
layers[label_classifier_idx] = CatcherHead(layers[label_classifier_idx])
model.class_labels_classifier = layers[label_classifier_idx]
for batch in dataloader:
try:
model(batch[0].to(dev))
except ValueError:
pass
model.class_labels_classifier = layers[label_classifier_idx].module
layers[label_classifier_idx] = layers[label_classifier_idx].module
torch.cuda.empty_cache()
if args.backbone:
inps = inps_pixel
elif args.transformer:
inps = inps_encoder
elif args.output_head:
inps = inps_output_head
else:
raise ValueError("Can not quantize nothing")
outs = [None] * args.nsamples
inps_encoder_hidden_states = [None] * args.nsamples
errors = {}
model.cpu()
print('Ready.')
quantizers = {}
for i in range(len(layers)):
layer = layers[i].to(dev)
subset = find_layers(layer)
gptq = {}
for name in subset:
print(i, name)
gptq[name] = GPTQ(subset[name])
gptq[name].quantizer = Quantizer()
gptq[name].quantizer.configure(
args.wbits, perchannel=True, sym=False, mse=False)
def add_batch(name):
def tmp(_, inp, out):
gptq[name].add_batch(inp[0].data, out.data)
return tmp
handles = []
for name in subset:
handles.append(subset[name].register_forward_hook(add_batch(name)))
for j in range(args.nsamples):
if i == backbone_idx: # Backbone
outs[j] = layer(inps[j].to(dev), inps_pixel_mask[j].to(dev))[0]
elif i == input_projection_idx or i == label_classifier_idx or i == bbox_predictor_idx: # Input projection
outs[j] = layer(inps[j].to(dev))
elif i >= decoder_idx: # Decoder
outs[j] = \
layer(inps[j].to(dev), encoder_hidden_states=inps_encoder_hidden_states[j].to(dev), attention_mask=None,
object_queries=inps_object_queries[j].to(dev),
query_position_embeddings=cache['query_position_embeddings'])[0]
else: # Encoder
outs[j] = layer(inps[j].to(dev), attention_mask=inps_attention_mask[j].to(dev),
object_queries=inps_object_queries[j].to(dev))[0]
for h in handles:
h.remove()
for name in subset:
print(i, name)
print('Quantizing ...')
error = gptq[name].fasterquant(
percdamp=args.percdamp, groupsize=args.groupsize, actorder=args.act_order,
static_groups=args.static_groups
)
quantizers['model.decoder.layers.%d.%s' % (i, name)] = gptq[name].quantizer
gptq[name].free()
s = ""
if i == backbone_idx:
s = f"Backbone_{name}"
elif i == input_projection_idx:
s = "Input_projection"
elif i == label_classifier_idx:
s = "Label_classifier"
elif i == bbox_predictor_idx:
s = f"Bbox_predictor_{name}"
elif i >= decoder_idx:
s = f"Decoder_{i - decoder_idx}_{name}"
else:
s = f"Encoder_{i - encoder_idx}_{name}"
errors[s] = error
for j in range(args.nsamples):
if i == backbone_idx: # Backbone
outs[j] = layer(inps[j].to(dev), inps_pixel_mask[j].to(dev))[0]
elif i == input_projection_idx or i == label_classifier_idx or i == bbox_predictor_idx: # Input projection
outs[j] = layer(inps[j].to(dev))
elif i >= decoder_idx: # Decoder
outs[j] = \
layer(inps[j].to(dev), encoder_hidden_states=inps_encoder_hidden_states[j].to(dev), attention_mask=None,
object_queries=inps_object_queries[j].to(dev),
query_position_embeddings=cache['query_position_embeddings'])[0]
else: # Encoder
outs[j] = layer(inps[j].to(dev), attention_mask=inps_attention_mask[j].to(dev),
object_queries=inps_object_queries[j].to(dev))[0]
layers[i] = layer.cpu()
del layer
del gptq
torch.cuda.empty_cache()
if i == backbone_idx: # Keep backbone outputs
for k in range(args.nsamples):
outs[k] = outs[k][0][0]
if i == input_projection_idx:
if args.transformer:
outs = inps_encoder # Encoder inputs
else:
outs = inps_output_head # Output_head inputs
if i == decoder_idx - 1: # Decoder inputs
for k in range(args.nsamples):
inps_encoder_hidden_states[k] = outs[k].clone()
outs[k] = cache['queries']
if i == label_classifier_idx: # Keep decoder outputs for bbox_predictor
for k in range(args.nsamples):
outs[k] = inps[k].clone()
inps, outs = outs, inps
print("------------------")
for k in errors.keys():
print(k, errors[k])
print("------------------")
for k, v in sorted(errors.items(), key=lambda item: -item[1]):
print(k, v)
print("------------------")
name = f"detr_gptq{'_transformer' if args.transformer else ''}{'_backbone' if args.backbone else ''}{'_output_head' if args.output_head else ''}_{args.nsamples}samples_{args.wbits}bits"
with open(args.root + "errors/" + name + ".csv", 'w') as f:
f.write("Layer, Error\n")
for k, v in errors.items():
f.write(f"{k}, {v:.5f}\n")
torch.save(model.state_dict(), args.root+"model/"+name+".bin")
print("Model name : ", name)
return quantizers
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--backbone', action='store_true',
help='Whether to quantize the backbone. Quantize by default.')
parser.add_argument('--transformer', action='store_true',
help='Whether to quantize the transformer. Quantize by default.')
parser.add_argument('--output_head', action='store_true',
help='Whether to quantize the output head. Quantize by default.')
parser.add_argument('--seed', type=int, default=0,
help='Seed for sampling the calibration data.')
parser.add_argument('--nsamples', type=int, default=100,
help='Number of calibration data samples.')
parser.add_argument('--wbits', type=int, default=8, choices=[2, 3, 4, 5, 6, 7, 8],
help='#bits to use for quantization; use 16 for evaluating base model.')
parser.add_argument('--percdamp', type=float, default=.01,
help='Percent of the average Hessian diagonal to use for dampening.')
parser.add_argument('--groupsize', type=int, default=-1,
help='Groupsize to use for quantization; default uses full row.')
parser.add_argument('--act-order', action='store_false',
help='Whether to apply the activation order GPTQ heuristic')
parser.add_argument('--static-groups', action='store_false',
help='Whether to use static groups; recommended when using `--actorder` for more efficient inference.')
parser.add_argument('--root', type=str, default='')
args = parser.parse_args()
if not args.backbone and not args.transformer and not args.output_head:
args.backbone = True
args.transformer = True
args.output_head = True
dev = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", revision="no_timm").to(dev)
model = model.eval()
dataset_val = build_dataset(image_set='val', coco_path=args.root + "coco") # Can replace 'val' with 'train' to sample training data
indices = torch.randperm(len(dataset_val), generator=torch.Generator().manual_seed(args.seed))[:args.nsamples]
dataset_val = torch.utils.data.Subset(dataset_val, indices)
sampler_val = torch.utils.data.SequentialSampler(dataset_val)
dataloader = torch.utils.data.DataLoader(dataset_val, 1, sampler=sampler_val, drop_last=False)
detr_sequential(args, model, dataloader, dev)