-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDETR_QuIP.py
More file actions
467 lines (374 loc) · 18.4 KB
/
DETR_QuIP.py
File metadata and controls
467 lines (374 loc) · 18.4 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import argparse
from tqdm import tqdm
from transformers import DetrForObjectDetection
from QuIP.bal import Balance
from QuIP.gptq import *
from QuIP.near import Nearest
from QuIP.quant import *
from datasets.coco import build as build_dataset
from gptq.modelutils import *
@torch.no_grad()
def detr_sequential(model, dataloader, dev, args):
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 = {}
times = []
for i in tqdm(range(len(layers))):
layer = layers[i].to(dev)
subset = find_layers(layer)
quant_method = {}
for name in subset:
print(i, name)
# if isinstance(subset[name], nn.Conv2d): ## Conv2d is not supported by ldlq
# quant_method[name] = GPTQ(subset[name])
# quant_method[name].quantizer = Quantizer()
# quant_method[name].quantizer.configure(args.wbits,
# perchannel=True,
# sym=False,
# qfn='a',
# mse=False)
if args.quant == 'gptq':
quant_method[name] = GPTQ(subset[name])
quant_method[name].quantizer = Quantizer()
quant_method[name].quantizer.configure(args.wbits,
perchannel=True,
sym=False,
qfn=args.qfn,
mse=False)
elif args.quant == 'near':
quant_method[name] = Nearest(subset[name])
quant_method[name].quantizer = Quantizer()
quant_method[name].quantizer.configure(args.wbits,
perchannel=True,
sym=False,
qfn=args.qfn,
mse=False)
elif args.quant in ['allbal','ldlq','ldlqRG','ldlbal_admm']:
quant_method[name] = Balance(subset[name])
quant_method[name].configure(
args.quant,
args.wbits,
args.npasses,
unbiased=args.unbiased)
quant_method[name].quantizer = Quantizer()
quant_method[name].quantizer.configure(args.wbits,
perchannel=True,
sym=False,
qfn=args.qfn,
mse=False)
def add_batch(name):
def tmp(_, inp, out):
quant_method[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:
quant_method[name].post_batch()
for name in subset:
print(i, name)
print('Quantizing ...')
# if isinstance(subset[name], nn.Conv2d):
# quant_method[name].preproc(preproc_gptqH=True,
# percdamp=args.percdamp,
# preproc_rescale=False,
# preproc_proj=False,
# preproc_proj_extra=0)
# else:
quant_method[name].preproc(preproc_gptqH=args.pre_gptqH,
percdamp=args.percdamp,
preproc_rescale=args.pre_rescale,
preproc_proj=args.pre_proj,
preproc_proj_extra=args.pre_proj_extra)
if args.quant == 'gptq':# or isinstance(subset[name], nn.Conv2d):
quant_method[name].fasterquant(groupsize=args.groupsize)
elif args.quant in ['allbal','ldlq','ldlqRG','ldlbal_admm']:
quant_method[name].fasterquant(lazy_batch=args.lazy_batch)
elif args.quant == 'near':
quant_method[name].fasterquant()
times.append(quant_method[name].time)
quantizers['model.decoder.layers.%d.%s' % (i, name)] = quant_method[name].quantizer
quant_method[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] = quant_method[name].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 quant_method
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("------------------")
print(f'Total quant time: {sum(times):.2f}s')
name = f"detr_{args.quant}{'_IP' if args.incoh_processing else ''}{'_unbiased' if args.unbiased else ''}{'_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, errors
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('--pre_gptqH', action='store_true',
help='preprocessing')
parser.add_argument('--pre_rescale', action='store_true',
help='preprocessing')
parser.add_argument('--pre_proj', action='store_true',
help='preprocessing')
parser.add_argument('--pre_proj_extra', type=int,default=0, choices=[0, 1, 2],
help='Extra options to control pre_proj step.')
parser.add_argument('--qfn', type=str, default='a',
help='qfn: a is default, b is sym incoherent based')
parser.add_argument('--unbiased', action='store_true',
help='unbiased')
parser.add_argument('--incoh_processing', action='store_true',
help='incoherence processing')
parser.add_argument('--npasses', type=int, default=0,
help='number passes to repeat balance loop over 1-d.')
parser.add_argument('--lazy_batch', action='store_true',
help='lazy batch updates in blocks as used in OPTQ')
parser.add_argument('--quant', choices=['allbal', 'ldlq', 'ldlqRG', 'ldlbal_admm', 'near', 'gptq'], default='gptq',
help='Which quantization method to use.')
parser.add_argument('--root', type=str)
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
if args.incoh_processing:
args.pre_gptqH = True
args.pre_rescale = True
args.pre_proj = True
args.proj_extra = 1
args.qfn = 'b'
if args.qfn=='b': assert args.pre_proj is True
print(f"Preprocessing flags: gptqH:{args.pre_gptqH}, rescale:{args.pre_rescale}, proj:{args.pre_proj}, proj_extra:{args.pre_proj_extra}, qfn:{args.qfn}")
print(f"using lazy_batch updates: {args.lazy_batch}")
# LDL checks
if ('ldl' in args.quant) and args.unbiased and (args.npasses > 0):
print(f"LDL NOTE: unbiased + {args.npasses} npasses. NOT TRULY UNBIASED.")
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)
np.random.seed(args.seed)
torch.random.manual_seed(args.seed)
detr_sequential(model, dataloader, dev, args)