-
Notifications
You must be signed in to change notification settings - Fork 2
/
quantize_gptq_deepseek_eval.py
531 lines (421 loc) · 19.3 KB
/
quantize_gptq_deepseek_eval.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
import sys
sys.path.append("/home/LeiFeng/pingzhi/moe_quantize/optimum/") # Add the path to Python's search path
# print(sys.path)
import os
os.environ['HF_HOME'] = '/home/LeiFeng/pingzhi/moe_quantize/hf_cache'
os.makedirs(os.environ['HF_HOME'], exist_ok=True)
from transformers import AutoModelForCausalLM, AutoTokenizer
from optimum.gptq import GPTQQuantizer, load_quantized_model, GPTQQuantizer_deepseek
import torch
import random
from argparse import ArgumentParser
from transformers import AutoTokenizer, TextGenerationPipeline
import logging
from datasets import load_dataset
from auto_gptq import AutoGPTQForCausalLM, BaseQuantizeConfig, AutoGPTQForCausalLM_mixed_precision, BaseQuantizeConfig_mixed_precision
import argparse
import os
import torch
from transformers import AutoTokenizer
from auto_gptq.utils import Perplexity
def get_wikitext2(tokenizer, seqlen: int, nsamples: int, split: str = "train"):
if split == "train":
data = load_dataset("wikitext", "wikitext-2-raw-v1", split="train")
elif split == "validation":
data = load_dataset("wikitext", "wikitext-2-raw-v1", split="test")
# length of 288059 should be enough
text = "".join([" \n" if s == "" else s for s in data["text"][:1000]])
enc = tokenizer(text, return_tensors="pt")
dataset = []
for _ in range(nsamples):
i = random.randint(0, enc.input_ids.shape[1] - seqlen - 1)
j = i + seqlen
inp = enc.input_ids[:, i:j]
attention_mask = torch.ones_like(inp)
dataset.append({"input_ids": inp, "attention_mask": attention_mask})
return dataset
def moe_quantize_config(args):
if args.bits == 'all_4':
moe_block_bit_dict = {}
for i in range(4):
key = f"self_attn.{['q_proj', 'k_proj', 'v_proj', 'o_proj'][i]}"
moe_block_bit_dict[key] = 4
for i in range(64):
for part in ['gate_proj', 'up_proj', 'down_proj']:
key = f"mlp.experts.{i}.{part}"
moe_block_bit_dict[key] = 4
for part in ['gate_proj', 'up_proj', 'down_proj']:
key = f"mlp.shared_experts.{part}"
moe_block_bit_dict[key] = 4
deeepseek_bit = {
'model.layers.0.self_attn.q_proj': 4,
'model.layers.0.self_attn.k_proj': 4,
'model.layers.0.self_attn.v_proj': 4,
'model.layers.0.self_attn.o_proj': 4,
'model.layers.0.mlp.gate_proj': 4,
'model.layers.0.mlp.up_proj': 4,
'model.layers.0.mlp.down_proj': 4
}
for block_num in range(1, 28):
for layer in moe_block_bit_dict:
key = f'model.layers.{block_num}' + '.' + layer
deeepseek_bit[key] = moe_block_bit_dict[layer]
return deeepseek_bit
if args.bits == 'all_2':
moe_block_bit_dict = {}
for i in range(4):
key = f"self_attn.{['q_proj', 'k_proj', 'v_proj', 'o_proj'][i]}"
moe_block_bit_dict[key] = 2
for i in range(64):
for part in ['gate_proj', 'up_proj', 'down_proj']:
key = f"mlp.experts.{i}.{part}"
moe_block_bit_dict[key] = 2
for part in ['gate_proj', 'up_proj', 'down_proj']:
key = f"mlp.shared_experts.{part}"
moe_block_bit_dict[key] = 2
deeepseek_bit = {
'model.layers.0.self_attn.q_proj': 2,
'model.layers.0.self_attn.k_proj': 2,
'model.layers.0.self_attn.v_proj': 2,
'model.layers.0.self_attn.o_proj': 2,
'model.layers.0.mlp.gate_proj': 2,
'model.layers.0.mlp.up_proj': 2,
'model.layers.0.mlp.down_proj': 2
}
for block_num in range(1, 28):
for layer in moe_block_bit_dict:
key = f'model.layers.{block_num}' + '.' + layer
deeepseek_bit[key] = moe_block_bit_dict[layer]
return deeepseek_bit
if args.bits == 'all_8':
moe_block_bit_dict = {}
for i in range(4):
key = f"self_attn.{['q_proj', 'k_proj', 'v_proj', 'o_proj'][i]}"
moe_block_bit_dict[key] = 8
for i in range(64):
for part in ['gate_proj', 'up_proj', 'down_proj']:
key = f"mlp.experts.{i}.{part}"
moe_block_bit_dict[key] = 8
for part in ['gate_proj', 'up_proj', 'down_proj']:
key = f"mlp.shared_experts.{part}"
moe_block_bit_dict[key] = 8
deeepseek_bit = {
'model.layers.0.self_attn.q_proj': 8,
'model.layers.0.self_attn.k_proj': 8,
'model.layers.0.self_attn.v_proj': 8,
'model.layers.0.self_attn.o_proj': 8,
'model.layers.0.mlp.gate_proj': 8,
'model.layers.0.mlp.up_proj': 8,
'model.layers.0.mlp.down_proj': 8
}
for block_num in range(1, 28):
for layer in moe_block_bit_dict:
key = f'model.layers.{block_num}' + '.' + layer
deeepseek_bit[key] = moe_block_bit_dict[layer]
return deeepseek_bit
if args.bits == 'moe.all_mlp.2+other_block.4':
moe_block_bit_dict = {}
for i in range(4):
key = f"self_attn.{['q_proj', 'k_proj', 'v_proj', 'o_proj'][i]}"
moe_block_bit_dict[key] = 4
for i in range(64):
for part in ['gate_proj', 'up_proj', 'down_proj']:
key = f"mlp.experts.{i}.{part}"
moe_block_bit_dict[key] = 2
for part in ['gate_proj', 'up_proj', 'down_proj']:
key = f"mlp.shared_experts.{part}"
moe_block_bit_dict[key] = 2
deeepseek_bit = {
'model.layers.0.self_attn.q_proj': 4,
'model.layers.0.self_attn.k_proj': 4,
'model.layers.0.self_attn.v_proj': 4,
'model.layers.0.self_attn.o_proj': 4,
'model.layers.0.mlp.gate_proj': 4,
'model.layers.0.mlp.up_proj': 4,
'model.layers.0.mlp.down_proj': 4
}
for block_num in range(1, 28):
for layer in moe_block_bit_dict:
key = f'model.layers.{block_num}' + '.' + layer
deeepseek_bit[key] = moe_block_bit_dict[layer]
return deeepseek_bit
if args.bits == 'moe.shared_4.other.2+other_block_4':
moe_block_bit_dict = {}
for i in range(4):
key = f"self_attn.{['q_proj', 'k_proj', 'v_proj', 'o_proj'][i]}"
moe_block_bit_dict[key] = 4
for i in range(64):
for part in ['gate_proj', 'up_proj', 'down_proj']:
key = f"mlp.experts.{i}.{part}"
moe_block_bit_dict[key] = 2
for part in ['gate_proj', 'up_proj', 'down_proj']:
key = f"mlp.shared_experts.{part}"
moe_block_bit_dict[key] = 4
deeepseek_bit = {
'model.layers.0.self_attn.q_proj': 4,
'model.layers.0.self_attn.k_proj': 4,
'model.layers.0.self_attn.v_proj': 4,
'model.layers.0.self_attn.o_proj': 4,
'model.layers.0.mlp.gate_proj': 4,
'model.layers.0.mlp.up_proj': 4,
'model.layers.0.mlp.down_proj': 4
}
for block_num in range(1, 28):
for layer in moe_block_bit_dict:
key = f'model.layers.{block_num}' + '.' + layer
deeepseek_bit[key] = moe_block_bit_dict[layer]
return deeepseek_bit
if args.bits == "moe.shared_2.other.4+other_block_4":
moe_block_bit_dict = {}
for i in range(4):
key = f"self_attn.{['q_proj', 'k_proj', 'v_proj', 'o_proj'][i]}"
moe_block_bit_dict[key] = 4
for i in range(64):
for part in ['gate_proj', 'up_proj', 'down_proj']:
key = f"mlp.experts.{i}.{part}"
moe_block_bit_dict[key] = 4
for part in ['gate_proj', 'up_proj', 'down_proj']:
key = f"mlp.shared_experts.{part}"
moe_block_bit_dict[key] = 2
deeepseek_bit = {
'model.layers.0.self_attn.q_proj': 4,
'model.layers.0.self_attn.k_proj': 4,
'model.layers.0.self_attn.v_proj': 4,
'model.layers.0.self_attn.o_proj': 4,
'model.layers.0.mlp.gate_proj': 4,
'model.layers.0.mlp.up_proj': 4,
'model.layers.0.mlp.down_proj': 4
}
for block_num in range(1, 28):
for layer in moe_block_bit_dict:
key = f'model.layers.{block_num}' + '.' + layer
deeepseek_bit[key] = moe_block_bit_dict[layer]
return deeepseek_bit
if args.bits == "moe.all_mlp.4+other_block.8":
moe_block_bit_dict = {}
for i in range(4):
key = f"self_attn.{['q_proj', 'k_proj', 'v_proj', 'o_proj'][i]}"
moe_block_bit_dict[key] = 8
for i in range(64):
for part in ['gate_proj', 'up_proj', 'down_proj']:
key = f"mlp.experts.{i}.{part}"
moe_block_bit_dict[key] = 4
for part in ['gate_proj', 'up_proj', 'down_proj']:
key = f"mlp.shared_experts.{part}"
moe_block_bit_dict[key] = 4
deeepseek_bit = {
'model.layers.0.self_attn.q_proj': 8,
'model.layers.0.self_attn.k_proj': 8,
'model.layers.0.self_attn.v_proj': 8,
'model.layers.0.self_attn.o_proj': 8,
'model.layers.0.mlp.gate_proj': 8,
'model.layers.0.mlp.up_proj': 8,
'model.layers.0.mlp.down_proj': 8
}
for block_num in range(1, 28):
for layer in moe_block_bit_dict:
key = f'model.layers.{block_num}' + '.' + layer
deeepseek_bit[key] = moe_block_bit_dict[layer]
return deeepseek_bit
if args.bits == 'moe.shared_4.other.2+other_block.8':
moe_block_bit_dict = {}
for i in range(4):
key = f"self_attn.{['q_proj', 'k_proj', 'v_proj', 'o_proj'][i]}"
moe_block_bit_dict[key] = 8
for i in range(64):
for part in ['gate_proj', 'up_proj', 'down_proj']:
key = f"mlp.experts.{i}.{part}"
moe_block_bit_dict[key] = 4
for part in ['gate_proj', 'up_proj', 'down_proj']:
key = f"mlp.shared_experts.{part}"
moe_block_bit_dict[key] = 2
deeepseek_bit = {
'model.layers.0.self_attn.q_proj': 8,
'model.layers.0.self_attn.k_proj': 8,
'model.layers.0.self_attn.v_proj': 8,
'model.layers.0.self_attn.o_proj': 8,
'model.layers.0.mlp.gate_proj': 8,
'model.layers.0.mlp.up_proj': 8,
'model.layers.0.mlp.down_proj': 8
}
for block_num in range(1, 28):
for layer in moe_block_bit_dict:
key = f'model.layers.{block_num}' + '.' + layer
deeepseek_bit[key] = moe_block_bit_dict[layer]
return deeepseek_bit
if args.bits == 'moe.shared_2.other.4+other_block.8':
moe_block_bit_dict = {}
for i in range(4):
key = f"self_attn.{['q_proj', 'k_proj', 'v_proj', 'o_proj'][i]}"
moe_block_bit_dict[key] = 8
for i in range(64):
for part in ['gate_proj', 'up_proj', 'down_proj']:
key = f"mlp.experts.{i}.{part}"
moe_block_bit_dict[key] = 2
for part in ['gate_proj', 'up_proj', 'down_proj']:
key = f"mlp.shared_experts.{part}"
moe_block_bit_dict[key] = 4
deeepseek_bit = {
'model.layers.0.self_attn.q_proj': 8,
'model.layers.0.self_attn.k_proj': 8,
'model.layers.0.self_attn.v_proj': 8,
'model.layers.0.self_attn.o_proj': 8,
'model.layers.0.mlp.gate_proj': 8,
'model.layers.0.mlp.up_proj': 8,
'model.layers.0.mlp.down_proj': 8
}
for block_num in range(1, 28):
for layer in moe_block_bit_dict:
key = f'model.layers.{block_num}' + '.' + layer
deeepseek_bit[key] = moe_block_bit_dict[layer]
return deeepseek_bit
raise ValueError("Invalid bits")
def main():
parser = ArgumentParser()
parser.add_argument("--bits", type=str)
parser.add_argument("--model_name", type=str, default=None)
parser.add_argument("--quantized_model_file_base_name", type=str, default=None)
parser.add_argument("--quant_path", type=str, default=None)
parser.add_argument("--nsamples", type=int, default=512)
parser.add_argument("--group_size", type=int, default=128)
args = parser.parse_args()
logging.basicConfig(
format="%(asctime)s %(levelname)s [%(name)s] %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
filename=f"/home/LeiFeng/pingzhi/moe_quantize/quantize_gptq_deepseek_{args.bits}.log"
)
args_dict = vars(args)
logging.info("Command-line arguments: %s", args_dict)
model_name = args.model_name
quant_path = f'autogptq_{model_name}-gptq_w_bit_{args.bits}'
quantized_model_file_base_name = f'{model_name.split("/")[-1]}-gptq_w_bit_{args.bits}'
deeepseek_bit = moe_quantize_config(args)
quantize_config = BaseQuantizeConfig_mixed_precision(
bits=deeepseek_bit, # quantize model to 4-bit
group_size=args.group_size, # it is recommended to set the value to 128
desc_act=False, # set to False can significantly speed up inference but the perplexity may slightly bad
model_file_base_name = quantized_model_file_base_name
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoGPTQForCausalLM_mixed_precision.from_pretrained(model_name, quantize_config, torch_dtype=torch.float16, trust_remote_code=True)
quantization_dataset = get_wikitext2(tokenizer=tokenizer, seqlen=4096, nsamples=args.nsamples, split="train")
logging.info(f"Quantization dataset loaded with {args.nsamples} samples")
logging.info(f"Quantizing model to {args.bits}-bit")
logging.info(f"Quantization config: {deeepseek_bit}")
model.quantize(quantization_dataset)
model.save_quantized(quant_path)
logging.info(f"Quantized model saved to {quant_path}")
def eval():
"""
Example usage.
Default usage with GPT2 model:
python examples/benchmark/perplexity.py
Specify GPTQ quantized model:
python examples/benchmark/perplexity.py \
--model_name TheBloke/open-llama-7b-open-instruct-GPTQ \
--model_basename gptq_model-4bit-128g \
--is_quantized
Change your dataset:
python examples/benchmark/perplexity.py --dataset_path tiny_shakespeare
"""
parser = argparse.ArgumentParser(description="Calculate Perplexity for a model.")
parser.add_argument("--model_name", type=str, default='deepseek-ai/deepseek-moe-16b-chat')
parser.add_argument("--quant_model_path", type=str)
parser.add_argument("--bits", type=str)
# parser.add_argument("--model_basename", type=str, default=None, help="Model file's basename.")
parser.add_argument("--n_ctx", type=int, default=512, help="Context size.")
parser.add_argument("--n_batch", type=int, default=512, help="Batch size.")
parser.add_argument("--dataset_path", type=str, default="wikitext", help="Path to the dataset.")
parser.add_argument("--dataset_name", type=str, default=None, help="Name of the dataset.")
parser.add_argument("--split", type=str, default="test", help="Dataset split to use.")
parser.add_argument(
"--text_column",
type=str,
default="text",
help="Column in the dataset containing the text.",
)
parser.add_argument(
"--per_gpu_max_memory",
type=int,
default=None,
help="Max memory used in each GPU.",
)
parser.add_argument("--cpu_max_memory", type=int, default=None, help="Mx memory used in CPU.")
parser.add_argument("--is_quantized", action="store_true", help="Is the model GPTQ quantized?")
parser.add_argument(
"--use_safetensors",
action="store_true",
help="Whether to use safetensors model file",
)
parser.add_argument("--use_fast_tokenizer", action="store_true", help="Wheter to use fast tokenizer")
parser.add_argument("--trust_remote_code", action="store_true", help="Whether to use remote code")
parser.add_argument(
"--disable_exllama",
action="store_true",
help="Whether to use disable exllama kernel",
)
args = parser.parse_args()
if args.is_quantized:
args.quantized_model_file_base_name = f'{args.model_name.split("/")[-1]}-gptq_w_bit_{args.bits}'
os.environ["TOKENIZERS_PARALLELISM"] = "false"
tokenizer = AutoTokenizer.from_pretrained(args.model_name, use_fast=args.use_fast_tokenizer)
if not tokenizer.pad_token_id:
tokenizer.pad_token_id = tokenizer.eos_token_id
max_memory = {}
if args.per_gpu_max_memory is not None and args.per_gpu_max_memory > 0:
if torch.cuda.is_available():
max_memory.update({i: f"{args.per_gpu_max_memory}GIB" for i in range(torch.cuda.device_count())})
if args.cpu_max_memory is not None and args.cpu_max_memory > 0 and max_memory:
max_memory["cpu"] = f"{args.cpu_max_memory}GIB"
if not max_memory:
max_memory = None
if args.use_safetensors:
print(
"The argument --use_safetensors is deprecrated and will be removed in the next release. It is now the default behavior."
)
model = AutoGPTQForCausalLM_mixed_precision.from_quantized(
args.quant_model_path,
low_cpu_mem_usage=True,
device_map="auto",
max_memory=max_memory,
model_basename=args.quantized_model_file_base_name,
use_safetensors=True,
trust_remote_code=True,
inject_fused_mlp=False,
inject_fused_attention=False,
# disable_exllama=args.disable_exllama,
)
else:
model = AutoModelForCausalLM.from_pretrained(
args.model_name,
device_map="auto",
trust_remote_code=True,
torch_dtype=torch.float16,
)
tokenizer = AutoTokenizer.from_pretrained(args.model_name, use_fast=args.use_fast_tokenizer)
if not tokenizer.pad_token_id:
tokenizer.pad_token_id = tokenizer.eos_token_id
ppl = Perplexity(
model,
tokenizer,
args.dataset_path,
args.dataset_name,
args.split,
args.text_column,
)
all_perplexity = ppl.calculate_perplexity(args.n_ctx, args.n_batch)
average_ppl = sum(all_perplexity) / len(all_perplexity)
if args.is_quantized:
ppl_log = f'{args.quant_model_path} | {args.bits}'
else:
ppl_log = f'{args.model_name} | full precision'
file_path = 'perplexity_results.txt' # Specify your desired file path here
with open(file_path, 'a') as file: # Open file in append mode
file.write(f'{ppl_log} | {average_ppl}\n') # Write all_perplexity to the file, followed by a newline
print(f'Perplexity value {average_ppl} has been added to {file_path}.')
if __name__ == "__main__":
eval()
# /home/LeiFeng/pingzhi/smoothquant/smoothquant/lm_eval
# cp -r /home/LeiFeng/pingzhi/smoothquant/smoothquant/lm_eval /home/LeiFeng/pingzhi/moe_quantize
# cp -r /home/LeiFeng/pingzhi/smoothquant/smoothquant/lm_eval.py /home/LeiFeng/pingzhi/moe_quantize