-
Notifications
You must be signed in to change notification settings - Fork 8
/
SVDLLM.py
580 lines (557 loc) · 30.1 KB
/
SVDLLM.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
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#coding:utf8
import os
import sys
import argparse
import torch.jit
from tqdm import tqdm
import torch
import torch.nn as nn
from utils.data_utils import *
from component.svd_llama import SVD_LlamaAttention, SVD_LlamaMLP
from component.svd_mistral import SVD_MistralAttention, SVD_MistralMLP
from component.svd_opt import SVDOPTDecoderLayer
from utils.model_utils import *
from evaluater import *
current_path = os.path.dirname(os.path.abspath(__file__))
parent_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(current_path)
@torch.no_grad()
def profle_svdllm(name, model, calib_loader, dev):
if "llama" in name or "mistral" in name or "vicuna" in name:
layers = model.model.layers
elif "opt" in name:
layers = model.model.decoder.layers
model = model.to(dev)
print("Start obtaining the whitening matrix...")
def hook(module, input, output):
inp = input[0].detach().float()
if inp.dim() == 2: # for opt
inp = inp.unsqueeze(0)
adds = torch.matmul(inp.transpose(1,2), inp)
adds_sum = torch.sum(adds, dim=0)
module.raw_scaling_diag_matrix += adds_sum
del inp, adds, adds_sum
torch.cuda.empty_cache()
for name, module in model.named_modules():
if isinstance(module, nn.Linear):
module.raw_scaling_diag_matrix = 0
module.register_forward_hook(hook)
for batch in tqdm(calib_loader):
batch = {k: v.to(dev) for k, v in batch.items()}
model(**batch)
for name, module in model.named_modules():
if isinstance(module, nn.Linear):
module._forward_hooks.clear()
torch.cuda.empty_cache()
model = model.cpu()
for i in range(len(layers)):
subset = find_layers(layers[i])
for name in subset:
subset[name].raw_scaling_diag_matrix = subset[name].raw_scaling_diag_matrix.cpu()
profiling_mat = {}
print("Start Cholesky Decomposition...")
for i in tqdm(range(len(layers))):
layer_profile = {}
subset = find_layers(layers[i])
for name in subset:
raw_scaling_diag_matrix = subset[name].raw_scaling_diag_matrix.double().to(dev)
try:
scaling_diag_matrix = torch.linalg.cholesky(raw_scaling_diag_matrix)
except Exception as e:
print("Warning: eigen scaling_diag_matrix is not positive!")
eigenvalues = torch.linalg.eigvalsh(raw_scaling_diag_matrix)
raw_scaling_diag_matrix += (- eigenvalues[0] + 1e-6) * torch.eye(raw_scaling_diag_matrix.shape[0]).to(dev)
scaling_diag_matrix = torch.linalg.cholesky(raw_scaling_diag_matrix)
eigenvalues = None
del eigenvalues
layer_profile[name] = scaling_diag_matrix.cpu()
scaling_diag_matrix = raw_scaling_diag_matrix = subset[name].raw_scaling_diag_matrix = None
del scaling_diag_matrix, raw_scaling_diag_matrix, subset[name].raw_scaling_diag_matrix
torch.cuda.empty_cache()
profiling_mat[i] = layer_profile
return profiling_mat
@torch.no_grad()
def profle_svdllm_low_resource(model_name, model, calib_loader, dev):
if "opt" in model_name:
layers = model.model.decoder.layers
model.model.decoder.embed_tokens = model.model.decoder.embed_tokens.to(dev)
model.model.decoder.final_layer_norm = model.model.decoder.final_layer_norm.to(dev)
model.model.decoder.embed_positions = model.model.decoder.embed_positions.to(dev)
else:
layers = model.model.layers
model.model.embed_tokens = model.model.embed_tokens.to(dev)
model.model.norm = model.model.norm.to(dev)
layers[0] = layers[0].to(dev)
dtype = next(iter(model.parameters())).dtype
inps = torch.zeros(
(len(calib_loader), model.seqlen, model.config.hidden_size), dtype=dtype, device=dev
)
cache = {'i': 0, 'attention_mask': None, "position_ids": None}
class Catcher(nn.Module):
def __init__(self, module):
super().__init__()
self.module = module
def forward(self, inp, **kwargs):
inps[cache['i']] = inp.cpu()
cache['i'] += 1
if cache['attention_mask'] is None:
cache['attention_mask'] = kwargs['attention_mask'].cpu()
if "opt" not in model_name:
cache['position_ids'] = kwargs['position_ids'].cpu()
else:
cache['attention_mask'] = torch.cat((cache['attention_mask'], kwargs['attention_mask'].cpu()), dim=0)
if "opt" not in model_name:
cache['position_ids'] = torch.cat((cache['position_ids'], kwargs['position_ids'].cpu()), dim=0)
raise ValueError
layers[0] = Catcher(layers[0])
for batch in calib_loader:
try:
batch = {k: v.to(dev) for k, v in batch.items()}
model(**batch)
except ValueError:
pass
layers[0] = layers[0].module
layers[0] = layers[0].cpu()
if "opt" in model_name:
model.model.decoder.embed_tokens = model.model.decoder.embed_tokens.cpu()
model.model.decoder.final_layer_norm = model.model.decoder.final_layer_norm.cpu()
model.model.decoder.embed_positions = model.model.decoder.embed_positions.cpu()
else:
model.model.embed_tokens = model.model.embed_tokens.cpu()
model.model.norm = model.model.norm.cpu()
torch.cuda.empty_cache()
outs = torch.zeros_like(inps)
attention_masks = cache['attention_mask']
if "opt" not in model_name:
position_ids = cache['position_ids']
profiling_mat = {}
for i in tqdm(range(len(layers))):
layer_profile = {}
layer = layers[i].to(dev)
subset = find_layers(layer)
def hook(module, input, output):
inp = input[0].detach().float()
if inp.dim() == 2: # for opt
inp = inp.unsqueeze(0)
adds = torch.matmul(inp.transpose(1,2), inp)
adds_sum = torch.sum(adds, dim=0)
module.scaling_diag_matrix += adds_sum
del inp, adds, adds_sum, output
torch.cuda.empty_cache()
handles = []
for name in subset:
subset[name].scaling_diag_matrix = 0
handles.append(subset[name].register_forward_hook(hook))
for j in range(inps.shape[0]):
if "opt" not in model_name:
outs[j] = layer(inps[j].unsqueeze(0), attention_mask=attention_masks[j].unsqueeze(0).to(dev), position_ids=position_ids[j].unsqueeze(0).to(dev))[0]
else:
outs[j] = layer(inps[j].unsqueeze(0), attention_mask=attention_masks[j].unsqueeze(0).to(dev))[0]
for h in handles:
h.remove()
layer = layer.cpu()
for name in subset:
subset[name].scaling_diag_matrix = subset[name].scaling_diag_matrix.cpu()
torch.cuda.empty_cache()
for name in subset:
raw_scaling_diag_matrix = subset[name].scaling_diag_matrix.double().to(dev)
try:
scaling_diag_matrix = torch.linalg.cholesky(raw_scaling_diag_matrix)
except Exception as e:
print("Warning: eigen scaling_diag_matrix is not positive!")
eigenvalues = torch.linalg.eigvalsh(raw_scaling_diag_matrix)
raw_scaling_diag_matrix += (- eigenvalues[0] + 1e-6) * torch.eye(raw_scaling_diag_matrix.shape[0]).to(dev)
scaling_diag_matrix = torch.linalg.cholesky(raw_scaling_diag_matrix)
eigenvalues = None
del eigenvalues
layer_profile[name] = scaling_diag_matrix.cpu()
scaling_diag_matrix = raw_scaling_diag_matrix = subset[name].raw_scaling_diag_matrix = None
del scaling_diag_matrix, raw_scaling_diag_matrix, subset[name].raw_scaling_diag_matrix
torch.cuda.empty_cache()
layers[i] = layer.cpu()
profiling_mat[i] = layer_profile
inps = outs
torch.cuda.empty_cache()
return profiling_mat
@torch.no_grad()
def whitening(model_name, model, profiling_mat, ratio, dev):
model.eval()
if 'opt' in model_name:
layers = model.model.decoder.layers
else:
layers = model.model.layers
print("Start SVD decomposition after whitening...")
for i in tqdm(range(len(layers))):
layer = layers[i]
subset = find_layers(layer)
#### Replace Attn, MLP ####
if "llama" in model_name or "vicuna" in model_name:
svd_attn = SVD_LlamaAttention(config=model.config, ratio=ratio)
svd_mlp = SVD_LlamaMLP(hidden_size=layer.hidden_size, intermediate_size=model.config.intermediate_size, hidden_act=model.config.hidden_act, ratio=ratio)
elif "mistral" in model_name:
svd_attn = SVD_MistralAttention(config=model.config, ratio=ratio)
svd_mlp = SVD_MistralMLP(config=model.config, ratio=ratio)
elif 'opt' in model_name:
svd_decoder = SVDOPTDecoderLayer(model.config, ratio=ratio)
#### Replace Attn, MLP ####
for name in subset:
W = subset[name].weight.data.float().to(dev)
dtype = W.dtype
scaling_diag_matrix = profiling_mat[i][name].to(dev)
try:
scaling_matrix_inv = torch.linalg.inv(scaling_diag_matrix)
except Exception as e:
print("Warning: scaling_diag_matrix is not full rank!")
scaling_diag_matrix += 1e-6 * torch.eye(scaling_diag_matrix.shape[0]).to(dev)
scaling_matrix_inv = torch.linalg.inv(scaling_diag_matrix)
scaling_diag_matrix = scaling_diag_matrix.float()
scaling_matrix_inv = scaling_matrix_inv.float()
W_scale = torch.matmul(W, scaling_diag_matrix)
U, S, VT = torch.linalg.svd(W_scale, full_matrices=False)
num_s_after_trunc = int(W.shape[0] * W.shape[1] * ratio / (W.shape[0] + W.shape[1]))
truc_s = S[:num_s_after_trunc]
truc_u = U[:, :num_s_after_trunc]
truc_v = torch.matmul(VT[:num_s_after_trunc, :], scaling_matrix_inv)
truc_sigma = torch.diag(truc_s)
#### Replace Attn, MLP ####
sqrtSigma = torch.sqrt(truc_sigma)
svd_u = torch.matmul(truc_u, sqrtSigma).cpu().to(dtype)
svd_v = torch.matmul(sqrtSigma, truc_v).cpu().to(dtype)
if 'opt' in model_name:
if "q_proj" in name:
svd_decoder.self_attn.q_u_proj.weight.data = svd_u
svd_decoder.self_attn.q_v_proj.weight.data = svd_v
svd_decoder.self_attn.q_u_proj.bias.data = layer.self_attn.q_proj.bias.data # the linear layer in OPT has bias, which is different from LLaMA and Mistral
elif "k_proj" in name:
svd_decoder.self_attn.k_u_proj.weight.data = svd_u
svd_decoder.self_attn.k_v_proj.weight.data = svd_v
svd_decoder.self_attn.k_u_proj.bias.data = layer.self_attn.k_proj.bias.data
elif "v_proj" in name:
svd_decoder.self_attn.v_u_proj.weight.data = svd_u
svd_decoder.self_attn.v_v_proj.weight.data = svd_v
svd_decoder.self_attn.v_u_proj.bias.data = layer.self_attn.v_proj.bias.data
elif "out_proj" in name:
svd_decoder.self_attn.out_u_proj.weight.data = svd_u
svd_decoder.self_attn.out_v_proj.weight.data = svd_v
svd_decoder.self_attn.out_u_proj.bias.data = layer.self_attn.out_proj.bias.data
elif "fc1" in name:
svd_decoder.fc1_u_proj.weight.data = svd_u
svd_decoder.fc1_v_proj.weight.data = svd_v
svd_decoder.fc1_u_proj.bias.data = layer.fc1.bias.data
elif "fc2" in name:
svd_decoder.fc2_u_proj.weight.data = svd_u
svd_decoder.fc2_v_proj.weight.data = svd_v
svd_decoder.fc2_u_proj.bias.data = layer.fc2.bias.data
svd_decoder.self_attn_layer_norm = layer.self_attn_layer_norm
svd_decoder.final_layer_norm = layer.final_layer_norm
layers[i] = svd_decoder
else:
if "q_proj" in name:
svd_attn.q_u_proj.weight.data = svd_u
svd_attn.q_v_proj.weight.data = svd_v
elif "k_proj" in name:
svd_attn.k_u_proj.weight.data = svd_u
svd_attn.k_v_proj.weight.data = svd_v
elif "v_proj" in name:
svd_attn.v_u_proj.weight.data = svd_u
svd_attn.v_v_proj.weight.data = svd_v
elif "o_proj" in name:
svd_attn.o_u_proj.weight.data = svd_u
svd_attn.o_v_proj.weight.data = svd_v
layer.self_attn = svd_attn
elif "gate_proj" in name:
svd_mlp.gate_u_proj.weight.data = svd_u
svd_mlp.gate_v_proj.weight.data = svd_v
elif "down_proj" in name:
svd_mlp.down_u_proj.weight.data = svd_u
svd_mlp.down_v_proj.weight.data = svd_v
elif "up_proj" in name:
svd_mlp.up_u_proj.weight.data = svd_u
svd_mlp.up_v_proj.weight.data = svd_v
layer.mlp = svd_mlp
W = W_scale = scaling_matrix_inv = scaling_diag_matrix = U = S = VT = truc_s = truc_u = truc_v = sqrtSigma = None
del W, W_scale, scaling_matrix_inv, scaling_diag_matrix, U, S, VT, truc_s, truc_u, truc_v, sqrtSigma
del layer
torch.cuda.empty_cache()
@torch.no_grad()
def whitening_local_update(model_name, model, dataloader, profiling_mat, ratio, dev, direct_update=False):
print("Start SVD decomposition then update...")
use_cache = model.config.use_cache
model.config.use_cache = False
if "opt" in model_name:
layers = model.model.decoder.layers
model.model.decoder.embed_tokens = model.model.decoder.embed_tokens.to(dev)
model.model.decoder.final_layer_norm = model.model.decoder.final_layer_norm.to(dev)
model.model.decoder.embed_positions = model.model.decoder.embed_positions.to(dev)
else:
layers = model.model.layers
model.model.embed_tokens = model.model.embed_tokens.to(dev)
model.model.norm = model.model.norm.to(dev)
model.model.norm = model.model.norm.to(dev)
layers[0] = layers[0].to(dev)
dtype = next(iter(model.parameters())).dtype
inps = torch.zeros(
(len(dataloader), model.seqlen, model.config.hidden_size), dtype=dtype, device=dev
)
cache = {'i': 0, 'attention_mask': None, "position_ids": None}
class Catcher(nn.Module):
def __init__(self, module):
super().__init__()
self.module = module
def forward(self, inp, **kwargs):
inps[cache['i']] = inp
cache['i'] += 1
if cache['attention_mask'] is None:
cache['attention_mask'] = kwargs['attention_mask']
if "opt" not in model_name:
cache['position_ids'] = kwargs['position_ids']
else:
cache['attention_mask'] = torch.cat((cache['attention_mask'], kwargs['attention_mask']), dim=0)
if "opt" not in model_name:
cache['position_ids'] = torch.cat((cache['position_ids'], kwargs['position_ids']), dim=0)
raise ValueError
layers[0] = Catcher(layers[0])
for batch in dataloader:
try:
model(batch[0].to(dev))
except ValueError:
pass
layers[0] = layers[0].module
layers[0] = layers[0].cpu()
model.model.embed_tokens = model.model.embed_tokens.cpu()
model.model.norm = model.model.norm.cpu()
torch.cuda.empty_cache()
outs = torch.zeros_like(inps)
attention_masks = cache['attention_mask']
if "opt" not in model_name:
position_ids = cache['position_ids']
for i in tqdm(range(len(layers))):
layer = layers[i].to(dev)
subset = find_layers(layer)
gpts = {}
if "llama" in model_name or "vicuna" in model_name:
svd_attn = SVD_LlamaAttention(config=model.config, ratio=ratio)
svd_mlp = SVD_LlamaMLP(hidden_size=layer.hidden_size, intermediate_size=model.config.intermediate_size, hidden_act=model.config.hidden_act, ratio=ratio)
elif "mistral" in model_name:
svd_attn = SVD_MistralAttention(config=model.config, ratio=ratio)
svd_mlp = SVD_MistralMLP(config=model.config, ratio=ratio)
elif 'opt' in model_name:
svd_decoder = SVDOPTDecoderLayer(model.config, ratio=ratio)
for name in subset:
if profiling_mat is not None:
scaling_diag_matrix = profiling_mat[i][name].to(dev)
else:
scaling_diag_matrix = None
gpts[name] = local_update(subset[name], scaling_diag_matrix = scaling_diag_matrix, ratio=ratio, name=name, direct_update=direct_update)
def add_batch(name):
def tmp(_, inp, out):
gpts[name].add_batch_update_u(inp[0].data, out.data)
return tmp
handles = []
for name in gpts:
handles.append(subset[name].register_forward_hook(add_batch(name)))
if "opt" not in model_name:
outs = layer(inps, attention_mask=attention_masks, position_ids=position_ids)[0]
else:
outs = layer(inps, attention_mask=attention_masks)[0]
for h in handles:
h.remove()
for name in gpts:
svd_u, svd_v = gpts[name].fasterprune()
svd_u, svd_v = svd_u.to(dtype), svd_v.to(dtype)
if 'opt' in model_name:
if "q_proj" in name:
svd_decoder.self_attn.q_u_proj.weight.data = svd_u
svd_decoder.self_attn.q_v_proj.weight.data = svd_v
svd_decoder.self_attn.q_u_proj.bias.data = layer.self_attn.q_proj.bias.data # the linear layer in OPT has bias, which is different from LLaMA and Mistral
elif "k_proj" in name:
svd_decoder.self_attn.k_u_proj.weight.data = svd_u
svd_decoder.self_attn.k_v_proj.weight.data = svd_v
svd_decoder.self_attn.k_u_proj.bias.data = layer.self_attn.k_proj.bias.data
elif "v_proj" in name:
svd_decoder.self_attn.v_u_proj.weight.data = svd_u
svd_decoder.self_attn.v_v_proj.weight.data = svd_v
svd_decoder.self_attn.v_u_proj.bias.data = layer.self_attn.v_proj.bias.data
elif "out_proj" in name:
svd_decoder.self_attn.out_u_proj.weight.data = svd_u
svd_decoder.self_attn.out_v_proj.weight.data = svd_v
svd_decoder.self_attn.out_u_proj.bias.data = layer.self_attn.out_proj.bias.data
elif "fc1" in name:
svd_decoder.fc1_u_proj.weight.data = svd_u
svd_decoder.fc1_v_proj.weight.data = svd_v
svd_decoder.fc1_u_proj.bias.data = layer.fc1.bias.data
elif "fc2" in name:
svd_decoder.fc2_u_proj.weight.data = svd_u
svd_decoder.fc2_v_proj.weight.data = svd_v
svd_decoder.fc2_u_proj.bias.data = layer.fc2.bias.data
svd_decoder.self_attn_layer_norm = layer.self_attn_layer_norm
svd_decoder.final_layer_norm = layer.final_layer_norm
layers[i] = svd_decoder
else:
if "q_proj" in name:
svd_attn.q_u_proj.weight.data = svd_u
svd_attn.q_v_proj.weight.data = svd_v
elif "k_proj" in name:
svd_attn.k_u_proj.weight.data = svd_u
svd_attn.k_v_proj.weight.data = svd_v
elif "v_proj" in name:
svd_attn.v_u_proj.weight.data = svd_u
svd_attn.v_v_proj.weight.data = svd_v
elif "o_proj" in name:
svd_attn.o_u_proj.weight.data = svd_u
svd_attn.o_v_proj.weight.data = svd_v
layer.self_attn = svd_attn
elif "gate_proj" in name:
svd_mlp.gate_u_proj.weight.data = svd_u
svd_mlp.gate_v_proj.weight.data = svd_v
elif "down_proj" in name:
svd_mlp.down_u_proj.weight.data = svd_u
svd_mlp.down_v_proj.weight.data = svd_v
elif "up_proj" in name:
svd_mlp.up_u_proj.weight.data = svd_u
svd_mlp.up_v_proj.weight.data = svd_v
layer.mlp = svd_mlp
layer = layer.to(dev)
if "opt" not in model_name:
outs = layer(inps, attention_mask=attention_masks, position_ids=position_ids)[0]
else:
outs = layer(inps, attention_mask=attention_masks)[0]
layers[i] = layer.cpu()
del gpts
torch.cuda.empty_cache()
inps = outs
outs = None
del outs
model.config.use_cache = use_cache
class local_update:
def __init__(self, layer, scaling_diag_matrix, ratio, name, direct_update=False):
self.layer = layer
self.name = name
self.dev = self.layer.weight.device
# W = layer.weight.data.clone()
W = layer.weight.data.clone()
self.rows = W.shape[0]
self.columns = W.shape[1]
if direct_update:
self.U, self.S, self.VT = torch.linalg.svd(W.data, full_matrices=False)
else:
try:
scaling_matrix_inv = torch.linalg.inv(scaling_diag_matrix)
except Exception as e:
print("Warning: scaling_diag_matrix is not full rank!")
scaling_diag_matrix += 1e-6 * torch.eye(scaling_diag_matrix.shape[0])
scaling_matrix_inv = torch.linalg.inv(scaling_diag_matrix)
scaling_diag_matrix = scaling_diag_matrix.float()
scaling_matrix_inv = scaling_matrix_inv.float()
W_scale = torch.matmul(W, scaling_diag_matrix)
self.U, self.S, self.VT = torch.linalg.svd(W_scale, full_matrices=False)
# trucation SVD
num_s_after_trunc = int(W.shape[0] * W.shape[1] * ratio / (W.shape[0] + W.shape[1]))
self.truc_s = self.S[:num_s_after_trunc].cuda()
self.truc_u = self.U[:, :num_s_after_trunc].cuda()
if direct_update:
self.truc_v = self.VT[:num_s_after_trunc, :].cuda()
else:
self.truc_v = torch.matmul(self.VT[:num_s_after_trunc, :].cuda(), scaling_matrix_inv)
self.truc_sigma = torch.diag(self.truc_s)
self.new_w = torch.matmul(self.truc_u, torch.matmul(self.truc_sigma, self.truc_v[:num_s_after_trunc, :]))
# intialize H for close form solution
self.updated_err = self.error = 0
def add_batch_update_u(self, inp, out):
inps = inp.view(inp.shape[0] * inp.shape[1], inp.shape[2])
outs = out.view(out.shape[0] * out.shape[1], out.shape[2])
new_w = torch.matmul(self.truc_u, torch.matmul(self.truc_sigma, self.truc_v))
new_output = inps.matmul(new_w.t())
self.error = torch.sqrt(torch.sum((outs - new_output)**2)).item() / torch.norm(outs, p='fro').item()
# print(f"truncted error: {self.error}")
x = torch.matmul(torch.matmul(inps, self.truc_v.T), self.truc_sigma)
self.updated_uT = torch.linalg.lstsq(x,outs).solution
updated_output = torch.matmul(torch.matmul(torch.matmul(inps, self.truc_v.T), self.truc_sigma), self.updated_uT)
self.updated_error = torch.sqrt(torch.sum((outs - updated_output)**2)).item() / torch.norm(outs, p='fro').item()
# print(f"updated error: {self.updated_error}")
inps = outs = new_output = updated_output = x = new_w = None
del inps, outs, new_output, updated_output, x, new_w
torch.cuda.empty_cache()
# print(f"Finish {self.name}"
def fasterprune(self):
sqrtSigma = torch.sqrt(self.truc_sigma)
self.appendU = self.updated_uT.t().matmul(sqrtSigma)
self.appendV = sqrtSigma.matmul(self.truc_v)
return self.appendU, self.appendV
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, default='jeffwan/llama-7b-hf', help='LLaMA model to load, pass `jeffwan/llama-7b-hf`')
parser.add_argument('--model_path', type=str, default=None, help='local compressed model path or whitening information path')
parser.add_argument('--ratio', type=float, default=0.2, help='Target compression ratio,(0,1), default=0.2, means only keeping about 20% of the params.')
parser.add_argument('--run_low_resource', action='store_true', help='whether to run whitening in low resource, exp, compress LLaMA-7B below 15G gpu')
parser.add_argument('--dataset', type=str, default='wikitext2',help='Where to extract calibration data from [wikitext2, ptb, c4]')
parser.add_argument('--whitening_nsamples', type=int, default=256, help='Number of calibration data samples for whitening.')
parser.add_argument('--updating_nsamples', type=int, default=16, help='Number of calibration data samples for udpating.')
parser.add_argument('--save_path', type=str, default=None, help='the path to save the compressed model checkpoints.`')
parser.add_argument('--profiling_mat_path', type=str, default=None, help='Local path to load the profiling matrices`')
parser.add_argument('--seed',type=int, default=0, help='Seed for sampling the calibration data')
parser.add_argument('--DEV', type=str, default="cuda", help='device')
parser.add_argument('--model_seq_len', type=int, default=2048, help='the default sequence length of the LLM')
parser.add_argument('--eval_batch_size', type=int, default=4, help='inference bactch size')
parser.add_argument('--gen_seq_len', type=int, default=1024, help='generated sequence len for efficiency evaluation')
parser.add_argument('--step', type=int, default=4, help='the step to run the compression')
parser.add_argument('--lora', type=str, default=None, help='the lora updated weight path to run the accuracy evaluation')
args = parser.parse_args()
args.ratio = 1- args.ratio
if args.step == 1:
model, tokenizer = get_model_from_huggingface(model_id=args.model)
model = model.eval()
if args.profiling_mat_path is None:
cali_white_data = get_calib_train_data(args.dataset, tokenizer, args.whitening_nsamples, seqlen=args.model_seq_len)
profiling_mat = profle_svdllm_low_resource(args.model, model, cali_white_data, args.DEV)
if args.save_path is not None:
torch.save(profiling_mat, args.save_path + "/" + args.model.replace("/", "_").replace("-", "_") + '_profiling_'+ args.dataset + '_' + str(args.whitening_nsamples) + '_' + str(args.seed)+ '.pt')
else:
profiling_mat = torch.load(args.profiling_mat_path)
whitening(args.model, model, profiling_mat, args.ratio, args.DEV)
if args.save_path is not None:
torch.save({'model': model, 'tokenizer': tokenizer}, args.save_path + "/" + args.model.replace("/", "_").replace("-", "_") +'_whitening_only_' + str(args.ratio) + '.pt') # fp32
elif args.step == 2:
model, tokenizer = get_model_from_huggingface(model_id=args.model)
dataloader, _ = get_loaders(args.dataset, nsamples=args.updating_nsamples, seed=args.seed, tokenizer=tokenizer, seqlen=args.model_seq_len)
model = model.eval()
model = model.float() # need to set to float
if args.profiling_mat_path is None:
cali_white_data = get_calib_train_data(args.dataset, tokenizer, args.whitening_nsamples, seqlen=args.model_seq_len)
profiling_mat = profle_svdllm_low_resource(args.model, model, cali_white_data, args.DEV)
if args.save_path is not None:
torch.save(profiling_mat, args.save_path + "/" + args.model.replace("/", "_").replace("-", "_") + '_profiling_'+ args.dataset + '_' + str(args.whitening_nsamples) + '_' + str(args.seed)+ '.pt')
else:
profiling_mat = torch.load(args.profiling_mat_path)
whitening_local_update(args.model, model, dataloader, profiling_mat, args.ratio, args.DEV)
if args.save_path is not None:
torch.save({'model': model, 'tokenizer': tokenizer}, args.save_path + "/" + args.model.replace("/", "_").replace("-", "_") +'_whitening_then_update_' + str(args.ratio) + '.pt') # fp32
elif args.step == 3:
model, tokenizer = get_model_from_huggingface(args.model)
model = model.eval()
model = model.float()
dataloader, _ = get_loaders(args.dataset, nsamples=args.updating_nsamples, seed=args.seed, tokenizer=tokenizer, seqlen=args.model_seq_len)
whitening_local_update(model_name=args.model, model=model, dataloader=dataloader, profiling_mat=None, ratio=args.ratio, dev=args.DEV, direct_update=True)
if args.save_path is not None:
torch.save({'model': model, 'tokenizer': tokenizer}, args.save_path + "/" + args.model.replace("/", "_").replace("-", "_") +'_update_only_' + str(args.ratio) + '.pt') # fp32
elif args.step >= 4:
print(f"evaluating {args.model_path}...")
if args.model_path == "original":
model, tokenizer = get_model_from_huggingface(args.model)
else:
model, tokenizer = get_model_from_local(args.model_path)
if args.lora is not None:
from utils.peft import PeftModel
model = PeftModel.from_pretrained(
model,
args.lora,
torch_dtype=torch.float16,
)
model = model.merge_and_unload()
model.eval()
model = model.float()
model = model.to(args.DEV)
if args.step == 4:
ppl_eval(model, tokenizer, datasets=['wikitext2'], model_seq_len=args.model_seq_len, batch_size=args.eval_batch_size, device=args.DEV)
elif args.step == 5:
eff_eval(model, tokenizer, generated_len=args.gen_seq_len, batch_size=args.eval_batch_size, device=args.DEV)