-
Notifications
You must be signed in to change notification settings - Fork 0
/
final_Lab4.py
481 lines (363 loc) · 16.9 KB
/
final_Lab4.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
import tqdm
import torch
from torch import nn
from transformers import AutoModelForCausalLM, AutoTokenizer
from datasets import load_dataset
from functools import partial
import gc
def evaluate(model, tokenizer):
testenc = load_dataset('wikitext', 'wikitext-2-raw-v1', split='test')
testenc = tokenizer("\n\n".join(testenc['text']), return_tensors='pt')
testenc = testenc.input_ids.to(model.device)
nsamples = 40
model = model.eval()
nlls = []
for i in tqdm.tqdm(range(nsamples), desc="evaluating..."):
batch = testenc[:, (i * 2048):((i + 1) * 2048)].to(model.device)
with torch.no_grad():
lm_logits = model(batch).logits
shift_logits = lm_logits[:, :-1, :].contiguous().float()
shift_labels = testenc[:, (i * 2048):((i + 1) * 2048)][:, 1:]
loss_fct = nn.CrossEntropyLoss()
loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))
neg_log_likelihood = loss.float() * 2048
nlls.append(neg_log_likelihood)
return torch.exp(torch.stack(nlls).sum() / (nsamples * 2048))
def get_model_size(model: nn.Module, data_width=16, group_size=-1):
if group_size != -1:
data_width += (16 + 4) / group_size
num_elements = 0
for param in model.parameters():
num_elements += param.numel()
return num_elements * data_width
Byte = 8
KiB = 1024 * Byte
MiB = 1024 * KiB
GiB = 1024 * MiB
model_path = "facebook/opt-1.3b"
# model_path = "/home/wplf/.cache/huggingface/hub/models--facebook--opt-1.3b/"
tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False)
model = AutoModelForCausalLM.from_pretrained(model_path, device_map="auto")
# # Evaluate the model
model_perplexity = evaluate(model, tokenizer)
model_size = get_model_size(model, data_width=32, group_size=128)
print("#" * 20 + "evaluating float perplexity and model size")
print(f"\nmodel perplexity: {model_perplexity:.2f}")
print(f"model size: {model_size/MiB:.2f} MiB")
# core quantization method (simulated quantization)
def pseudo_quantize_tensor(w, n_bit=4, q_group_size=-1):
org_w_shape = w.shape
if q_group_size > 0:
assert org_w_shape[-1] % q_group_size == 0
w = w.reshape(-1, q_group_size)
assert w.dim() == 2
# Calculate the maximum (\alpha) and minimum values (\beta) in the tensor.
max_val = w.amax(dim=1, keepdim=True)
assert max_val.dim() == 2 and max_val.size(0) == w.size(0) and max_val.size(1) == 1
min_val = w.amin(dim=1, keepdim=True)
assert min_val.dim() == 2 and min_val.size(0) == w.size(0) and min_val.size(1) == 1
# Calculate the scale factor and zero point. (Formula 1 & 2)
max_int = 2 ** n_bit - 1
scales = (max_val - min_val).clamp(min=1e-5) / max_int
assert scales.shape == max_val.shape
zeros = (-torch.round(min_val / scales)).clamp_(0, max_int)
assert scales.shape == min_val.shape
assert torch.isnan(scales).sum() == 0
assert torch.isnan(w).sum() == 0
# Quantize W: Map values in the range [\beta, \alpha] to lie within [0, 2^b - 1] (Formula 3)
w = torch.clamp(torch.round(w / scales) + zeros, 0, max_int)
assert w.dim() == 2 and w.size(0) == scales.size(0) and w.size(1) == q_group_size
# Dequantize W (pseudo quantization, the inverse transformation of Formula 3)
w = (w - zeros) * scales
assert w.dim() == 2 and w.size(0) == scales.size(0) and w.size(1) == q_group_size
assert torch.isnan(w).sum() == 0
w = w.reshape(org_w_shape)
return w
@torch.no_grad()
def pseudo_quantize_model_weight(
model, w_bit, q_group_size,
):
for n, m in model.named_modules():
if isinstance(m, nn.Linear):
m.weight.data = pseudo_quantize_tensor(m.weight.data, n_bit=w_bit, q_group_size=q_group_size)
del model
gc.collect()
torch.cuda.empty_cache()
model = AutoModelForCausalLM.from_pretrained(model_path, device_map="auto")
pseudo_quantize_model_weight(model, w_bit=3, q_group_size=128)
# # Evaluate the model
model_perplexity = evaluate(model, tokenizer)
model_size = get_model_size(model, data_width=3, group_size=128)
print("#" * 20 + "evaluating 4 bit model perplexity and model size (weight only), and use pseudo quantization (quantize and dequantize)")
print(f"\nmodel perplexity: {model_perplexity:.2f}")
print(f"model size: {model_size/MiB:.2f} MiB")
def get_calib_dataset(tokenizer=None, n_samples=256, block_size=512):
dataset = load_dataset("mit-han-lab/pile-val-backup", split="validation")
dataset = dataset.shuffle(seed=42)
samples = []
n_run = 0
for data in dataset:
line = data["text"]
line = line.strip()
line_encoded = tokenizer.encode(line)
if len(line_encoded) > block_size:
continue
sample = torch.tensor([line_encoded])
if sample.numel() == 0:
continue
samples.append(sample)
n_run += 1
if n_run == n_samples:
break
# now concatenate all samples and split according to block size
cat_samples = torch.cat(samples, dim=1)
n_split = cat_samples.shape[1] // block_size
print(f" * Split into {n_split} blocks")
return [cat_samples[:, i*block_size:(i+1)*block_size] for i in range(n_split)]
@torch.no_grad()
def get_calib_feat(model, tokenizer):
input_dict = dict()
def stat_input_max_hook(m, x, y, name):
if isinstance(x, tuple):
x = x[0]
x_max = x.view(-1, x.shape[-1]).abs().mean(dim=0).cpu().detach() # 每个隐藏层维度取最大值,保留的向量为【dim】
if name not in input_dict:
input_dict[name] = [x_max]
else:
input_dict[name] += [x_max]
hooks = []
for name, m in model.named_modules():
if isinstance(m, nn.Linear):
hooks.append(
m.register_forward_hook(
partial(stat_input_max_hook, name=name)))
print("Collecting activation scales...")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
samples = get_calib_dataset(tokenizer)
pbar = tqdm.tqdm(samples)
for input_ids in pbar:
input_ids = input_ids.to(device)
model(input_ids)
for hook in hooks:
hook.remove()
return input_dict
del model
gc.collect()
torch.cuda.empty_cache()
model = AutoModelForCausalLM.from_pretrained(model_path, device_map="auto")
input_feat = get_calib_feat(model, tokenizer)
@torch.no_grad()
def pseudo_quantize_model_salient_weight_fp16(
model, w_bit, q_group_size, input_feat
):
for n, m in model.named_modules():
if isinstance(m, nn.Linear):
importance = sum(input_feat[n]).float()
############### YOUR CODE STARTS HERE ###############
# Step 1: Find 1% of the salient weight channels according to importance (hint: use torch.topk())
# import pdb; pdb.set_trace()
outlier_indices = torch.topk(importance, k = round(importance.shape[0] * 0.01)).indices
assert outlier_indices.dim() == 1
############### YOUR CODE ENDS HERE #################
# Back up the values of the salient weight channels
outlier = m.weight.data[:, outlier_indices].clone()
m.weight.data = pseudo_quantize_tensor(m.weight.data, n_bit=w_bit, q_group_size=q_group_size)
############### YOUR CODE STARTS HERE ###############
# Step 2: Restore the 1% salient weight channels to their original FP16 values
m.weight.data[:, outlier_indices] = outlier
############### YOUR CODE ENDS HERE #################
del model
gc.collect()
torch.cuda.empty_cache()
model = AutoModelForCausalLM.from_pretrained(model_path, device_map="auto")
pseudo_quantize_model_salient_weight_fp16(model, w_bit=3, q_group_size=128, input_feat=input_feat)
# Evaluate the model
model_perplexity = evaluate(model, tokenizer)
print(f"Processing 1% salient weight channels, and model perplexity is {model_perplexity}")
@torch.no_grad()
def pseudo_quantize_model_random_weight_fp16(
model, w_bit, q_group_size, input_feat
):
for n, m in model.named_modules():
if isinstance(m, nn.Linear):
importance = sum(input_feat[n]).float()
############### YOUR CODE STARTS HERE ###############
# Step 1: Randomly choose 1% of the weight channels
outlier_mask = torch.randperm(importance.shape[0])[:round(importance.shape[0] * 0.01)]
assert outlier_mask.dim() == 1
############### YOUR CODE ENDS HERE #################
# Back up the values of the selected weight channels
outlier = m.weight.data[:, outlier_mask].clone()
m.weight.data = pseudo_quantize_tensor(m.weight.data, n_bit=w_bit, q_group_size=q_group_size)
############### YOUR CODE STARTS HERE ###############
# Step 2: Restore the 1% selected weight channels to their original FP16 values
m.weight.data[:, outlier_mask] = outlier
############### YOUR CODE ENDS HERE #################
del model
gc.collect()
torch.cuda.empty_cache()
model = AutoModelForCausalLM.from_pretrained(model_path, device_map="auto")
pseudo_quantize_model_random_weight_fp16(model, w_bit=3, q_group_size=128, input_feat=input_feat)
# Evaluate the model
model_perplexity = evaluate(model, tokenizer)
print(f"Processing random weight quantization, and model perplexity is {model_perplexity}")
@torch.no_grad()
def pseudo_quantize_model_weight_scaleup(
model, w_bit, q_group_size, input_feat, scale_factor
):
for n, m in model.named_modules():
if isinstance(m, nn.Linear):
importance = sum(input_feat[n]).float()
############### YOUR CODE STARTS HERE ###############
# Step 1: Find 1% of the salient weight channels
outlier_mask = torch.topk(importance, k=int(importance.shape[0] * 0.01)).indices
assert outlier_mask.dim() == 1
############### YOUR CODE ENDS HERE #################
# To simulate applying the scale factor, we can simply multiply it before quantization, and then divide by the scale factor after quantization.
# Scale up the values of the salient weight channels
m.weight.data[:, outlier_mask] *= scale_factor
m.weight.data = pseudo_quantize_tensor(m.weight.data, n_bit=w_bit, q_group_size=q_group_size)
############### YOUR CODE STARTS HERE ###############
# Step 2: Scale back down the values of the salient weight channels
m.weight.data[:, outlier_mask] /= scale_factor
############### YOUR CODE ENDS HERE #################
for s in [1,2,3,4]:
print("#" * 20, "processing scale factor: ", s)
del model
gc.collect()
torch.cuda.empty_cache()
model = AutoModelForCausalLM.from_pretrained(model_path, device_map="auto")
pseudo_quantize_model_weight_scaleup(model, w_bit=3, q_group_size=128, input_feat=input_feat, scale_factor=s)
# Evaluate the model
model_perplexity = evaluate(model, tokenizer)
model_size = get_model_size(model, data_width=3, group_size=128)
print(f"\n Processing AWQ weight quantization, and model perplexity: {model_perplexity:.2f}")
print(f"model size: {model_size/MiB:.2f} MiB")
@torch.no_grad()
def scale_ln_fcs(ln, fcs, scales):
if not isinstance(fcs, list):
fcs = [fcs]
scales = scales.to(ln.weight.device)
ln.weight.div_(scales)
if hasattr(ln, 'bias') and ln.bias is not None:
ln.bias.div_(scales)
for fc in fcs:
fc.weight.mul_(scales.view(1, -1))
for p in ln.parameters():
assert torch.isnan(p).sum() == 0
for fc in fcs:
for p in fc.parameters():
assert torch.isnan(p).sum() == 0
@torch.no_grad()
def scale_fc_fc(fc1, fc2, scales):
assert isinstance(fc1, nn.Linear)
assert isinstance(fc2, nn.Linear)
scales = scales.to(fc1.weight.device)
# fc1.weight.div_(scales.view(-1, 1))
fc1.weight[-scales.size(0):].div_(scales.view(-1, 1))
if fc1.bias is not None:
fc1.bias.div_(scales.view(-1))
fc2.weight.mul_(scales.view(1, -1))
for p in fc1.parameters():
assert torch.isnan(p).sum() == 0
for p in fc2.parameters():
assert torch.isnan(p).sum() == 0
@torch.no_grad()
def auto_scale_block(module, name, w_bit,
q_group_size,
input_feat):
# find the best scale ratio
def _search_module_scale(block, linears2scale: list, x, kwargs={}):
x = x.to(next(block.parameters()).device)
with torch.no_grad():
org_out = block(x, **kwargs)
if isinstance(org_out, tuple):
org_out = org_out[0]
s_x = x.view(-1, x.shape[-1]).abs().mean(0)
############### YOUR CODE STARTS HERE ###############
# Step 1: Initialize the best_error, best_ratio and best_scales
best_error = float('inf')
best_ratio = 1
best_scales = 1
############### YOUR CODE ENDS HERE #################
n_grid = 20
history = []
org_sd = {k: v.cpu() for k, v in block.state_dict().items()}
for ratio in range(n_grid):
# ratio is the \alpha in the formula
ratio = ratio * 1 / n_grid
############### YOUR CODE STARTS HERE ###############
# Step 2: Calculate the scales by the formula: scales = s_x^ratio
scales = s_x ** ratio
# import pdb; pdb.set_trace()
assert scales.shape == s_x.shape
############### YOUR CODE ENDS HERE #################
scales = scales / (scales.max() * scales.min()).sqrt().view(1, -1)
for fc in linears2scale:
scales = scales.to(fc.weight.device)
# Scale up the values of the weight channels
fc.weight.mul_(scales)
fc.weight.data = pseudo_quantize_tensor(fc.weight.data, w_bit, q_group_size)
############### YOUR CODE STARTS HERE ###############
# Step 3: Scale back down the values of the weight channels
fc.weight.div_(scales)
############### YOUR CODE ENDS HERE #################
out = block(x, **kwargs)
if isinstance(out, tuple):
out = out[0]
loss = (org_out - out).float().pow(2).mean().item() # float prevents overflow
history.append(loss)
is_best = loss < best_error
if is_best:
best_error = loss
best_ratio = ratio
best_scales = scales
block.load_state_dict(org_sd)
if best_ratio == -1:
print(history)
raise Exception
best_scales = best_scales.view(-1)
assert torch.isnan(best_scales).sum() == 0, best_scales
return best_scales.detach()
# attention input
inp = input_feat[name + '.self_attn.out_proj']
inp = torch.cat([x.unsqueeze(0) for x in inp], dim=0).unsqueeze(0)
qkv = [module.self_attn.q_proj, module.self_attn.k_proj, module.self_attn.v_proj]
final_scales = _search_module_scale(module.self_attn, qkv, inp)
scale_ln_fcs(module.self_attn_layer_norm, qkv, final_scales)
# attn out
inp = input_feat[name + '.self_attn.out_proj']
inp = torch.cat([x.unsqueeze(0) for x in inp], dim=0)
final_scales = _search_module_scale(module.self_attn.out_proj, [module.self_attn.out_proj], inp)
scale_fc_fc(module.self_attn.v_proj, module.self_attn.out_proj, final_scales)
# fc1
inp = input_feat[name + '.fc1']
inp = torch.cat([x.unsqueeze(0) for x in inp], dim=0)
final_scales = _search_module_scale(module.fc1, [module.fc1], inp)
scale_ln_fcs(module.final_layer_norm, module.fc1, final_scales)
# fc2
inp = input_feat[name + '.fc2']
inp = torch.cat([x.unsqueeze(0) for x in inp], dim=0)
final_scales = _search_module_scale(module.fc2, [module.fc2], inp)
scale_fc_fc(module.fc1, module.fc2, final_scales)
@torch.no_grad()
def pseudo_quantize_model_weight_auto_scale(
model, w_bit, q_group_size, input_feat
):
from transformers.models.opt.modeling_opt import OPTDecoderLayer
for name, module in model.named_modules():
if isinstance(module, OPTDecoderLayer):
auto_scale_block(module, name, w_bit, q_group_size, input_feat)
for n, m in model.named_modules():
if isinstance(m, nn.Linear):
m.weight.data = pseudo_quantize_tensor(m.weight.data, n_bit=w_bit, q_group_size=q_group_size)
# del model
# gc.collect()
# torch.cuda.empty_cache()
# model = AutoModelForCausalLM.from_pretrained(model_path, device_map="auto")
# pseudo_quantize_model_weight_auto_scale(model, w_bit=3, q_group_size=128, input_feat=input_feat)
# # Evaluate the model
# model_perplexity = evaluate(model, tokenizer)
# model_size = get_model_size(model, data_width=3, group_size=128)
# print(f"\nmodel perplexity: {model_perplexity:.2f}")
# print(f"model size: {model_size/MiB:.2f} MiB")