-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrun_longbench.py
More file actions
1330 lines (1202 loc) · 68.3 KB
/
run_longbench.py
File metadata and controls
1330 lines (1202 loc) · 68.3 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
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import json
import random
import argparse
import sys
import numpy as np
from tqdm import tqdm
from dataclasses import dataclass, field
from scipy.spatial.distance import cdist
from sklearn.cluster import AgglomerativeClustering
import pandas as pd
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
import subprocess
from accelerate import Accelerator
from accelerate.utils import gather_object
from transformers import AutoModelForCausalLM, AutoTokenizer
from statistics import mean
import torch, time, json
import re
from uncomp.utils.logger import Logger
import logging
# 创建logger实例
accelerator = Accelerator()
logger = Logger()
# datasets = ["narrativeqa", "qasper", "multifieldqa_en", "hotpotqa", "2wikimqa", "musique", \
# "trec", "triviaqa","passage_count", "passage_retrieval_en", \
# "qmsum","samsum","lcc", "repobench-p","gov_report","multi_news"]
datasets = ["multifieldqa_en"]
dataset2maxlen = {
"narrativeqa": 128,
"qasper": 128,
"qasper_new": 128,
"multifieldqa_en": 64,
"multifieldqa_en_e": 64,
"multifieldqa_zh": 64,
"hotpotqa": 32,
"hotpotqa_new": 32,
"2wikimqa": 32,
"2wikimqa_new": 32,
"musique": 32,
# "dureader": 128,
"gov_report": 512,
"qmsum": 512,
"multi_news": 512,
# "vcsum": 512,
"trec": 64,
"trec_new": 64,
"triviaqa": 32,
"triviaqa_new": 32,
# "triviaqa_e": 32,
"samsum": 128,
"samsum_new": 128,
# "lsht": 64,
"passage_count": 32,
"passage_retrieval_en": 32,
"passage_retrieval_en_new": 32,
# "passage_retrieval_zh": 32,
"lcc": 64,
"repobench-p": 64,
"repobench-p_new": 64
}
model2prompt = {
"narrativeqa": "You are given a story, which can be either a novel or a movie script, and a question. Answer the question asconcisely as you can, using a single phrase if possible. Do not provide any explanation.\n\nStory: {context}\n\nNow, answer the question based on the story asconcisely as you can, using a single phrase if possible. Do not provide any explanation.\n\nQuestion: {input}\n\nAnswer:",
"qasper": "You are given a scientific article and a question. Answer the question as concisely as you can, using a single phrase or sentence if possible. If the question cannot be answered based on the information in the article, write \"unanswerable\". If the question is a yes/no question, answer \"yes\", \"no\", or \"unanswerable\". Do not provide any explanation.\n\nArticle: {context}\n\n Answer the question based on the above article as concisely as you can, using a single phrase or sentence if possible. If the question cannot be answered based on the information in the article, write \"unanswerable\". If the question is a yes/no question, answer \"yes\", \"no\", or \"unanswerable\". Do not provide any explanation.\n\nQuestion: {input}\n\nAnswer:",
"qasper_new": "You are given a scientific article and a question. Answer the question as concisely as you can, using a single phrase or sentence if possible. If the question cannot be answered based on the information in the article, write \"unanswerable\". If the question is a yes/no question, answer \"yes\", \"no\", or \"unanswerable\". Do not provide any explanation.\n\nArticle: {context}\n\n Answer the question based on the above article as concisely as you can, using a single phrase or sentence if possible. If the question cannot be answered based on the information in the article, write \"unanswerable\". If the question is a yes/no question, answer \"yes\", \"no\", or \"unanswerable\". Do not provide any explanation.\n\nQuestion: {input}\n\nAnswer:",
"multifieldqa_en": "Read the following text and answer briefly.\n\n{context}\n\nNow, answer the following question based on the above text, only give me the answer and do not output any other words.\n\nQuestion: {input}\nAnswer:",
"multifieldqa_en_e": "Read the following text and answer briefly.\n\n{context}\n\nNow, answer the following question based on the above text, only give me the answer and do not output any other words.\n\nQuestion: {input}\nAnswer:",
"multifieldqa_zh": "阅读以下文字并用中文简短回答:\n\n{context}\n\n现在请基于上面的文章回答下面的问题,只告诉我答案,不要输出任何其他字词。\n\n问题:{input}\n回答:",
"hotpotqa": "Answer the question based on the given passages. Only give me the answer and do not output any other words.\n\nThe following are given passages.\n{context}\n\nAnswer the question based on the given passages. Only give me the answer and do not output any other words.\n\nQuestion: {input}\nAnswer:",
"hotpotqa_new": "Answer the question based on the given passages. Only give me the answer and do not output any other words.\n\nThe following are given passages.\n{context}\n\nAnswer the question based on the given passages. Only give me the answer and do not output any other words.\n\nQuestion: {input}\nAnswer:",
"2wikimqa": "Answer the question based on the given passages. Only give me the answer and do not output any other words.\n\nThe following are given passages.\n{context}\n\nAnswer the question based on the given passages. Only give me the answer and do not output any other words.\n\nQuestion: {input}\nAnswer:",
"2wikimqa_new": "Answer the question based on the given passages. Only give me the answer and do not output any other words.\n\nThe following are given passages.\n{context}\n\nAnswer the question based on the given passages. Only give me the answer and do not output any other words.\n\nQuestion: {input}\nAnswer:",
"musique": "Answer the question based on the given passages. Only give me the answer and do not output any other words.\n\nThe following are given passages.\n{context}\n\nAnswer the question based on the given passages. Only give me the answer and do not output any other words.\n\nQuestion: {input}\nAnswer:",
"dureader": "请基于给定的文章回答下述问题。\n\n文章:{context}\n\n请基于上述文章回答下面的问题。\n\n问题:{input}\n回答:",
"gov_report": "You are given a report by a government agency. Write a one-page summary of the report.\n\nReport:\n{context}\n\nNow, write a one-page summary of the report.\n\nSummary:",
"qmsum": "You are given a meeting transcript and a query containing a question or instruction. Answer the query in one or more sentences.\n\nTranscript:\n{context}\n\nNow, answer the query based on the above meeting transcript in one or more sentences.\n\nQuery: {input}\nAnswer:",
"multi_news": "You are given several news passages. Write a one-page summary of all news. \n\nNews:\n{context}\n\nNow, write a one-page summary of all the news.\n\nSummary:",
"vcsum": "下面有一段会议记录,请你阅读后,写一段总结,总结会议的内容。\n会议记录:\n{context}\n\n会议总结:",
"trec": "Please determine the type of the question below. Here are some examples of questions.\n\n{context}\n{input}",
"trec_new": "Please determine the type of the question below. Here are some examples of questions.\n\n{context}\n{input}",
"triviaqa": "Answer the question based on the given passage. Only give me the answer and do not output any other words. The following are some examples.\n\n{context}\n\n{input}",
# "triviaqa_e": "Answer the question based on the given passage. Only give me the answer and do not output any other words. The following are some examples.\n\n{context}\n\n{input}",
"triviaqa_new": "Answer the question based on the given passage. Only give me the answer and do not output any other words. The following are some examples.\n\n{context}\n\n{input}",
"samsum": "Summarize the dialogue into a few short sentences. The following are some examples.\n\n{context}\n\n{input}",
"samsum_new": "Summarize the dialogue into a few short sentences. The following are some examples.\n\n{context}\n\n{input}",
"lsht": "请判断给定新闻的类别,下面是一些例子。\n\n{context}\n{input}",
"passage_count": "There are some paragraphs below sourced from Wikipedia. Some of them may be duplicates. Please carefully read these paragraphs and determine how many unique paragraphs there are after removing duplicates. In other words, how many non-repeating paragraphs are there in total?\n\n{context}\n\nPlease enter the final count of unique paragraphs after removing duplicates. The output format should only contain the number, such as 1, 2, 3, and so on.\n\nThe final answer is: ",
"passage_retrieval_en": "Here are 30 paragraphs from Wikipedia, along with an abstract. Please determine which paragraph the abstract is from.\n\n{context}\n\nThe following is an abstract.\n\n{input}\n\nPlease enter the number of the paragraph that the abstract is from. The answer format must be like \"Paragraph 1\", \"Paragraph 2\", etc.\n\nThe answer is: ",
"passage_retrieval_en_new": "Here are 30 paragraphs from Wikipedia, along with an abstract. Please determine which paragraph the abstract is from.\n\n{context}\n\nThe following is an abstract.\n\n{input}\n\nPlease enter the number of the paragraph that the abstract is from. The answer format must be like \"Paragraph 1\", \"Paragraph 2\", etc.\n\nThe answer is: ",
"passage_retrieval_zh": "以下是若干段落文字,以及其中一个段落的摘要。请确定给定的摘要出自哪一段。\n\n{context}\n\n下面是一个摘要\n\n{input}\n\n请输入摘要所属段落的编号。答案格式必须是\"段落1\",\"段落2\"等格式\n\n答案是:",
"lcc": "Please complete the code given below. \n{context}Next line of code:\n",
"repobench-p": "Please complete the code given below. \n{context}{input}Next line of code:\n",
"repobench-p_new": "Please complete the code given below. \n{context}{input}Next line of code:\n",
}
model2maxlen = {
"llama2": 3950,
"llama-2": 3950,
"llama3": 7950,
"llama-3": 7950,
"mistral": 3950,
"mistral": 12000,
"tinyllama": 1800,
"qwen2": 8000,
}
def set_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
def build_chat(prompt):
prompt = f"[INST] {prompt} [/INST]"
return prompt
def main(args,manager):
logger.info("Loading data...")
test_data = []
prompts_all = []
inputs = []
contexts = []
answerss = []
lengths = []
datasets = []
languages = []
all_classess = []
_ids = []
input_max_len = 0
model_path = args.model_path.lower()
for key in model2maxlen:
if key in model_path:
model_max_len = model2maxlen[key]
output_max_len = dataset2maxlen[args.dataset]
with open(args.data_file) as fp:
for line in fp:
example = json.loads(line)
length = example["length"]
if length > input_max_len: input_max_len = length
template = model2prompt[args.dataset]
prompt = template.format(**example)
if "llama2" in args.model_path.lower():
prompt = build_chat(prompt)
example["prompt"] = prompt
test_data.append(example)
logger.info(f"Max Length is {input_max_len}")
for example in test_data:
prompts_all.append(example["prompt"])
inputs.append(example["input"])
contexts.append(example["context"])
answerss.append(example["answers"])
lengths.append(example["length"])
datasets.append(example["dataset"])
languages.append(example["language"])
all_classess.append(example["all_classes"])
_ids.append(example["_id"])
logger.info("Finish loading model and tokenizer")
combined_data = [
{
"prompt": p, "input": i, "context": c, "answers": a,
"length": l, "dataset": d, "language": lang,
"all_classes": ac, "_id": id
}
for p, i, c, a, l, d, lang, ac, id in zip(
prompts_all, inputs, contexts, answerss, lengths,
datasets, languages, all_classess, _ids
)
]
if manager.method_name in manager.rope_correlation:
combined_data = combined_data[:1]
output_max_len = 1
manager.rope_correlation_dict["dataname"] = args.dataset
manager.max_used = 0
manager.min_used = torch.iinfo(torch.long).max
start=time.time()
with accelerator.split_between_processes(combined_data) as split_data:
results=dict(outputs=[], num_tokens=0, first_token_time=0)
manager.sum1 = 0
split_data = list(split_data)
for i in tqdm(range(0, len(split_data), args.eval_batch_size)):
batch_data = split_data[i:i+args.eval_batch_size]
batch_prompts = [item["prompt"] for item in batch_data]
batch_inputs = [item["input"] for item in batch_data]
batch_contexts = [item["context"] for item in batch_data]
batch_answerss = [item["answers"] for item in batch_data]
batch_lengths = [item["length"] for item in batch_data]
batch_datasets = [item["dataset"] for item in batch_data]
batch_languages = [item["language"] for item in batch_data]
batch_all_classess = [item["all_classes"] for item in batch_data]
batch__ids = [item["_id"] for item in batch_data]
tokenized_prompts = tokenizer(batch_prompts,
padding="longest",
return_tensors="pt",
add_special_tokens=True,
).to('cuda')
batch_input_ids = tokenized_prompts.input_ids
attention_mask = tokenized_prompts.attention_mask
actual_lengths = attention_mask.sum(dim=1)
max_len = actual_lengths.max().item()
padding_len = max_len - actual_lengths
if args.eval_batch_size == 1:
if len(batch_input_ids[0]) > model_max_len:
half = int(model_max_len/2)
prompt = [tokenizer.decode(batch_input_ids[i][padding_len[i]:padding_len[i]+half], skip_special_tokens=True)+tokenizer.decode(batch_input_ids[i][-half:], skip_special_tokens=True) for i in range(len(batch_input_ids))]
messages = [
{"role": "user", "content": prompt[0]}
]
if "qwen2" in manager.model_path.lower():
prompt = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
tokenized_prompts = tokenizer(prompt, padding="longest", return_tensors="pt", add_special_tokens=True).to('cuda')
batch_input_ids = tokenized_prompts.input_ids
attention_mask = tokenized_prompts.attention_mask
else:
prompt = [tokenizer.decode(batch_input_ids[i], skip_special_tokens=True) for i in range(len(batch_input_ids))]
messages = [
{"role": "user", "content": prompt[0]}
]
if "qwen2" in manager.model_path.lower():
prompt = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
tokenized_prompts = tokenizer(prompt, padding="longest", return_tensors="pt", add_special_tokens=True).to('cuda')
batch_input_ids = tokenized_prompts.input_ids
attention_mask = tokenized_prompts.attention_mask
else:
if len(batch_input_ids[0]) > model_max_len:
new_batch_input_ids = torch.zeros((len(batch_input_ids), model_max_len), dtype=torch.long).to('cuda')
new_attention_mask = torch.zeros((len(attention_mask), model_max_len), dtype=torch.long).to('cuda')
half = int(model_max_len/2)
for i in range(len(batch_input_ids)):
new_batch_input_ids[i] = torch.cat([batch_input_ids[i,padding_len[i]:padding_len[i]+half],batch_input_ids[i,-half:]],dim=0)
new_attention_mask[i] = torch.cat([attention_mask[i,padding_len[i]:padding_len[i]+half],attention_mask[i,-half:]],dim=0)
batch_input_ids = new_batch_input_ids
attention_mask = new_attention_mask
if args.max_capacity_prompts != -1:
max_capacity_prompts = args.max_capacity_prompts
if args.method != "FullKV":
window_sizes = 8
if "wind" in args.method_name:
match = re.search(r'wind(\d+)', args.method_name)
window_sizes = int(match.group(1))
kernel_sizes = 7
pooling = "maxpool"
layers = len(model.model.layers)
if not isinstance(window_sizes, list):
window_sizes = [window_sizes] * layers
if not isinstance(max_capacity_prompts, list):
max_capacity_prompts = [max_capacity_prompts] * layers
if not isinstance(kernel_sizes, list):
kernel_sizes = [kernel_sizes] * layers
for i in range(layers):
model.model.layers[i].self_attn.config.window_size = window_sizes[i]
model.model.layers[i].self_attn.config.max_capacity_prompt = max_capacity_prompts[i]
model.model.layers[i].self_attn.config.kernel_size = kernel_sizes[i]
model.model.layers[i].self_attn.config.pooling = pooling
context_length = batch_input_ids.shape[-1]
if manager.calib_label:
manager.sample_time = 0
datas = manager.calib_datas
manager.presuppose = [0,0,0]
manager.presuppose_sum = [0,0,0]
manager.pearson_correlations = 0
for i in range(len(datas)):
model.generate(
**datas[i],
output_attentions = args.output_attentions,
max_new_tokens=1,
num_beams=1,
do_sample=False,
temperature=1.0,
min_length=4096+1,
eos_token_id=[tokenizer.eos_token_id]
)
manager.sample_time+=1
print("manager.sample_time",manager.sample_time)
sys.exit(0)
# if manager.method_name in manager.draw_picture_set:
# output_max_len = 1
# manager.dataset = args.dataset
output = model.generate(
input_ids=batch_input_ids,
attention_mask=attention_mask,
output_attentions = args.output_attentions,
max_new_tokens=output_max_len,
num_beams=1,
do_sample=False,
temperature=1.0,
min_length=context_length+1,
eos_token_id=[tokenizer.eos_token_id]
)
torch.cuda.empty_cache()
func_utils_instance = manager.func_utils_instance
get_memory_info = func_utils_instance.get_memory_info
bytes_to_gb = func_utils_instance.bytes_to_gb
reserved, allocated = get_memory_info()
print(f"Memory freed: reserved={bytes_to_gb(reserved)} GB, allocated={bytes_to_gb(allocated)} GB")
# assert 1==0
batch_outputs =tokenizer.batch_decode(output[:,context_length:], skip_special_tokens=True)
print("\nbatch_outputs is ", batch_outputs)
batch_generations = batch_outputs
for j in range(len(batch_prompts)):
example = {}
example["prompt"] = batch_prompts[j]
example["input"] = batch_inputs[j]
example["context"] = batch_contexts[j]
example["answers"] = batch_answerss[j]
example["pred"] = batch_generations[j]
example["length"] = batch_lengths[j]
example["dataset"] = batch_datasets[j]
example["language"] = batch_languages[j]
example["all_classes"] = batch_all_classess[j]
example["_id"] = batch__ids[j]
results["outputs"].append(example)
results["num_tokens"] += len(batch_generations[j])
# if manager.method_name in manager.draw_picture_set:
# break
def get_rocm_memory():
result = subprocess.run(
['rocm-smi','--showmeminfo', 'vram'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
lines = result.stdout.splitlines()
for i, line in enumerate(lines):
if "GPU[2]" in line and "VRAM Total Used Memory (B)" in line:
value = int(line.split(": ")[2].strip().split()[0])
value = value /(1024**2)
# print(f"GPU 2 VRAM Total Memory : {value :.2f} MB")
return value
used = get_rocm_memory()
if manager.max_used < used:
manager.max_used = used
if manager.min_used > used:
manager.min_used = used
print(f'Max used: {manager.max_used:.2f} MB, Min used: {manager.min_used:.2f} MB')
results = [results]
sums = [manager.sum1]
results_gathered = gather_object(results)
accelerator.wait_for_everyone()
sum_gathered = gather_object(sums)
if accelerator.is_main_process:
timediff=time.time()-start
num_tokens=sum([r["num_tokens"] for r in results_gathered ])
model_name = model_path.split("/")[-1]
os.makedirs(os.path.join(args.save_dir, f"{model_name}_{args.max_capacity_prompts}", args.dataset), exist_ok=True)
fout = open(os.path.join(args.save_dir, f"{model_name}_{args.max_capacity_prompts}", args.dataset, f"{args.method}.json"), "w")
for result_list in results_gathered:
for example in result_list["outputs"]:
fout.write(json.dumps(example) + "\n")
print("sum(sum_gathered)", sum(sum_gathered))
print("mean compress", sum(sum_gathered)/len(combined_data)/(manager.num_hidden_layers*manager.num_attention_heads))
print(f"all sec/token: {timediff / num_tokens}, time {timediff}, total tokens {num_tokens}, total prompts {len(prompts_all)}")
def calib_main(args,manager):
logger.info("Loading data...")
for key in model2maxlen:
if key in model_path:
model_max_len = model2maxlen[key]
model_max_lens = [2048,8192]
test_data = manager.calib_datas
manager.max_used = 0
manager.min_used = torch.iinfo(torch.long).max
start=time.time()
with accelerator.split_between_processes(test_data) as split_data:
results=dict(outputs=[], num_tokens=0, first_token_time=0, prompt = 0)
manager.sum1 = 0
split_data = list(split_data)
logger.debug(f"len(split_data): {len(split_data)}")
for i in tqdm(range(0, len(split_data), args.eval_batch_size)):
batch_data = split_data[i:i+args.eval_batch_size]
tokenized_prompts = tokenizer(batch_data,
padding="longest",
return_tensors="pt",
add_special_tokens=True,
).to('cuda')
batch_input_ids = tokenized_prompts.input_ids
attention_mask = tokenized_prompts.attention_mask
print("attnention_mask.dtype",attention_mask.dtype)
print("batch_input_ids.dtype",batch_input_ids.dtype)
actual_lengths = attention_mask.sum(dim=1)
max_len = actual_lengths.max().item()
padding_len = max_len - actual_lengths
if args.eval_batch_size == 1:
if len(batch_input_ids[0]) > model_max_lens[0]:
half = int(model_max_lens[0]/2)
prompt = [tokenizer.decode(batch_input_ids[i][padding_len[i]:padding_len[i]+half], skip_special_tokens=True)+tokenizer.decode(batch_input_ids[i][-half:], skip_special_tokens=True) for i in range(len(batch_input_ids))]
tokenized_prompts = tokenizer(prompt, padding="longest", return_tensors="pt", add_special_tokens=True).to('cuda')
batch_input_ids = tokenized_prompts.input_ids
attention_mask = tokenized_prompts.attention_mask
else:
if len(batch_input_ids[0]) > model_max_lens[0]:
new_batch_input_ids = torch.zeros((len(batch_input_ids), model_max_lens[0]), dtype=torch.long).to('cuda')
new_attention_mask = torch.zeros((len(attention_mask), model_max_lens[0]), dtype=torch.long).to('cuda')
half = int(model_max_lens[0]/2)
for i in range(len(batch_input_ids)):
new_batch_input_ids[i] = torch.cat([batch_input_ids[i,padding_len[i]:padding_len[i]+half],batch_input_ids[i,-half:]],dim=0)
new_attention_mask[i] = torch.cat([attention_mask[i,padding_len[i]:padding_len[i]+half],attention_mask[i,-half:]],dim=0)
batch_input_ids = new_batch_input_ids
attention_mask = new_attention_mask
max_capacity_prompts = args.max_capacity_prompts
if args.method != "FullKV":
window_sizes = 8
kernel_sizes = 7
pooling = "maxpool"
layers = len(model.model.layers)
if not isinstance(window_sizes, list):
window_sizes = [window_sizes] * layers
if not isinstance(max_capacity_prompts, list):
max_capacity_prompts = [max_capacity_prompts] * layers
if not isinstance(kernel_sizes, list):
kernel_sizes = [kernel_sizes] * layers
for i in range(layers):
model.model.layers[i].self_attn.config.window_size = window_sizes[i]
model.model.layers[i].self_attn.config.max_capacity_prompt = max_capacity_prompts[i]
model.model.layers[i].self_attn.config.kernel_size = kernel_sizes[i]
model.model.layers[i].self_attn.config.pooling = pooling
context_length = batch_input_ids.shape[-1]
print("tokenized_prompts.input_ids.shape", tokenized_prompts.input_ids.shape)
manager.sample_time = 0
manager.presuppose = [0,0,0]
manager.presuppose_sum = [0,0,0]
manager.pearson_correlations = 0
manager.bsz = batch_input_ids.shape[0]
output = model.generate(
input_ids=batch_input_ids,
attention_mask=attention_mask,
output_attentions = args.output_attentions,
max_new_tokens=model_max_lens[1],
num_beams=1,
do_sample=False,
temperature=1.0,
min_length=model_max_lens[0]+model_max_lens[1],
eos_token_id=[tokenizer.eos_token_id]
)
logger.debug(f"output.shape: {output.shape}")
results["num_tokens"] += output[:,context_length:].shape[-1]*batch_input_ids.shape[0]
results["prompt"] += batch_input_ids.shape[-1]*batch_input_ids.shape[0]
def get_rocm_memory():
result = subprocess.run(
['rocm-smi','--showmeminfo', 'vram'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
lines = result.stdout.splitlines()
for i, line in enumerate(lines):
if "GPU[2]" in line and "VRAM Total Used Memory (B)" in line:
value = int(line.split(": ")[2].strip().split()[0])
value = value /(1024**2)
print(f"GPU 2 VRAM Total Memory : {value :.2f} MB")
return value
used = get_rocm_memory()
if manager.max_used < used:
manager.max_used = used
if manager.min_used > used:
manager.min_used = used
print(f'Max used: {manager.max_used:.2f} MB, Min used: {manager.min_used:.2f} MB')
results = [results]
sums = [manager.sum1]
results_gathered = gather_object(results)
accelerator.wait_for_everyone()
sum_gathered = gather_object(sums)
if accelerator.is_main_process:
timediff=time.time()-start
num_tokens=sum([r["num_tokens"] for r in results_gathered ])
prompts = sum([r["prompt"] for r in results_gathered ])
model_name = model_path.split("/")[-1]
os.makedirs(os.path.join(args.save_dir, f"{model_name}_{args.max_capacity_prompts}", args.dataset), exist_ok=True)
fout = open(os.path.join(args.save_dir, f"{model_name}_{args.max_capacity_prompts}", args.dataset, f"{args.method}.json"), "w")
for result_list in results_gathered:
for example in result_list["outputs"]:
fout.write(json.dumps(example) + "\n")
print(f"\n\nall sec/token: {timediff / num_tokens*1000} ms, time {timediff}, total tokens {num_tokens}, total prompts {prompts}")
@dataclass
class Manager:
last_attn = None
head_datas = [0]*100
head_granularity,head_set,last_process,delet_head_set,group_sampling = [],[],[],[],[]
chai_layers_llama2: list = field(default_factory=lambda: [
32,32,30,30,
30,30,30,30,
24,30,24,30,
28,28,32,32,
18,18,20,28,
18,18,18,20,
18,18,18,18,
18,18,24,32,
])
chai_layers_llama2_2: list = field(default_factory=lambda: [
32,32,32,32,
28,28,28,28,
20,20,20,20,
20,20,20,20,
8,8,8,8,
8,8,8,8,
8,8,8,8,
12,12,12,12,
])
chai_layers_llama2_13B: list = field(default_factory=lambda: [
32,32,30,30,
32,32,30,30,
32,32,30,30,
30,30,30,30,
24,30,24,30,
28,28,32,32,
18,18,20,28,
18,18,18,20,
18,18,18,18,
18,18,24,32,
])
chai_layers_llama3: list = field(default_factory=lambda: [
32,32,30,30,
32,32,32,32,
24,30,24,30,
28,28,32,32,
26,26,26,26,
26,26,26,26,
26,26,26,26,
26,26,26,30,
])
pass
from datasets import load_dataset
def get_calib_data(name, tokenizer, model_id, nsamples, seqlen=4096, seed=43):
logger.info(f"get_calib_data {name}, nsamples={nsamples}, seqlen={seqlen}, seed={seed}")
if name == "wikitext2":
traindata = load_dataset("wikitext", "wikitext-2-raw-v1", split="train")
tot_text = "\n\n".join(traindata["text"])
logger.info(f"traindata[\"text\"] {type(traindata['text'])}")
logger.info(f"tot_text={len(tot_text)}")
traindataset = []
for _ in range(nsamples):
i = random.randint(0, len(tot_text) - seqlen - 1)
j = i + seqlen * 10
trainenc = tokenizer(tot_text[i:j], padding="longest", return_tensors="pt", add_special_tokens=True).to('cuda')
inp = trainenc.input_ids[:, :seqlen]
attention_mask = torch.ones_like(inp)
traindataset.append({"input_ids": inp, "attention_mask": attention_mask})
return traindataset
class func_utils:
def init_manager(self,manager):
model_path = manager.model_path
if "llama-3" in model_path:
manager.chai_layers = manager.chai_layers_llama3
manager.max_token = 8192
manager.num_hidden_layers = 32
manager.num_attention_heads = 32
elif "llama-2" in model_path and "13b" in model_path:
manager.chai_layers = manager.chai_layers_llama2_13B
manager.num_hidden_layers = 40
manager.num_attention_heads = 40
manager.max_token = 4096
elif "tinyllama" in model_path:
print("tinyllama detected")
manager.max_token = 1800
manager.num_attention_heads = 32
manager.num_hidden_layers = 22
elif "qwen2" and "7b" in model_path:
logger.info("qwen2 detected")
manager.max_token = 8192
manager.num_attention_heads = 28
manager.num_hidden_layers = 28
elif "qwen2" and "3b" in model_path:
logger.info("qwen2 3b detected")
manager.max_token = 8192
manager.num_attention_heads = 16
manager.num_hidden_layers = 36
else:
manager.chai_layers = manager.chai_layers_llama2
manager.max_token = 4096
manager.num_attention_heads = 32
manager.num_hidden_layers = 32
def last_process(self,manager):
manager.search=["pearson_correlation_survey_1"]
manager.last_process.extend(["pearson_correlation_survey_1","pearson_correlation_survey"])
def rope_survey(self,manager):
manager.rope=[]
manager.rope_survey = []
manager.rope_clip_layer_single_layer = []
manager.rope_clip_layer_multi_layer = []
manager.rope_correlation = []
manager.rope_position_ids_control = []
def get_last_number(string):
match = re.search(r'(\d+)(?!.*\d)', string)
if match:
last_number = int(match.group(1))
print(f"The last number in the string is: {last_number}")
else:
print("No number found in the string.")
return last_number
def get_all_numbers(string):
matches = re.findall(r'\d+(?:\.\d+)?', string)
if matches:
numbers = [float(num) for num in matches]
print(f"All numbers in the string are: {numbers}")
else:
print("No numbers found in the string.")
return numbers
def get_last_float_number(string):
match = re.search(r'(\d+(\.\d+)?)(?!.*\d)', string)
if match:
last_number = float(match.group(1))
print(f"The last number in the string is: {last_number}")
else:
print("No number found in the string.")
return last_number
def not_update(self,manager):
manager.not_update = []
if "not_update" in manager.method_name:
logger.warning("kv cache is not maintained in decoding stage")
manager.not_update.append(manager.method_name)
def other_methods(self,manager):
manager.chai = ["chai"]
if "pyramidkv_generate" in manager.method_name or "snapkv" in manager.method_name:
manager.group_sampling.append(manager.method_name)
def calib_sets(self,manager):
manager.calib = ["head_type_search_2","head_type_search_2_nosvd",
"head_type_search_3",
"head_type_search_4","head_type_search_4_nosvd",
"head_type_search_5",
"head_type_search_8", "head_type_search_8_nosvd",
"head_type_search_32","output_entropy",
"head_type_search_2_variance"]
if "calib" in manager.method_name:
manager.calib.append(manager.method_name)
def get_memory_info(self):
stats = torch.cuda.memory_stats()
reserved = stats.get("reserved_bytes.all.current", 0)
allocated = stats.get("allocated_bytes.all.current", 0)
return reserved, allocated
def bytes_to_gb(self,x):
return round(x / (1024 ** 3), 4)
def uncomp_sets(self,manager):
manager.ahead_500_similar = []
manager.ahead_500 = []
manager.delete_head_equal_code = []
if "uncomp_delete_head" in manager.method_name:
logger.info("uncomp_delete_head")
manager.delete_head_equal_code.append(manager.method_name)
manager.extreme_compressibility_equal_code = []
if "uncomp_extreme_compressibility" in manager.method_name:
logger.info("uncomp_extreme_compressibility")
manager.extreme_compressibility_equal_code.append(manager.method_name)
manager.hidden_delete_stage_and_ours = []
manager.ahead_500_equal_code = []
if "stage" in manager.method_name:
logger.info("uncomp_stage")
manager.hidden_delete_stage_and_ours.append(manager.method_name)
if "group" not in manager.method_name and "stage_only" not in manager.method_name:
manager.ahead_500.append(manager.method_name)
manager.ahead_500_equal_code.append(manager.method_name)
if "uncomp" in manager.method_name and "extreme_compressibility" not in manager.method_name \
and "group" not in manager.method_name and "delete_head" not in manager.method_name \
and "pyramidkv" not in manager.method_name:
logger.info("uncomp")
manager.ahead_500_equal_code.append(manager.method_name)
manager.ahead_500.append(manager.method_name)
lists_to_extend = [manager.extreme_compressibility_equal_code,
manager.delete_head_equal_code,
manager.ahead_500_equal_code,
]
for lst in lists_to_extend:
manager.head_set.extend(lst)
manager.head_granularity.extend(lst)
def uncomp_extend(self,manager):
manager.head_differ_recent_n = []
if "uncomp_head_group_differ_recent_n" in manager.method_name:
logger.info("uncomp_head_group_differ_recent_n")
manager.head_differ_recent_n.append(manager.method_name)
manager.head_set.extend(manager.head_differ_recent_n)
manager.head_granularity.extend(manager.head_differ_recent_n)
manager.ahead_500.extend(manager.head_differ_recent_n)
manager.layer_differ_recent_n = []
if "uncomp_layer_group_differ_recent_n" in manager.method_name:
logger.info("uncomp_layer_group_differ_recent_n")
if "set1" in manager.method_name:
manager.layer_window = list(range(1, 33))
elif "set2" in manager.method_name:
manager.layer_window = list(range(32, 0, -1))
manager.layer_differ_recent_n.append(manager.method_name)
manager.head_set.extend(manager.layer_differ_recent_n)
manager.head_granularity.extend(manager.layer_differ_recent_n)
manager.ahead_500.extend(manager.layer_differ_recent_n)
def multi_group_sets(self,manager):
manager.ahead_500_equal_code_little_size = []
if "uncomp_little_size" in manager.method_name and "group" not in manager.method_name:
manager.ahead_500.append(manager.method_name)
manager.ahead_500_equal_code_little_size.append(manager.method_name)
if "hidden_stage_uncomp_little_size" in manager.method_name and "group" not in manager.method_name:
manager.hidden_delete_stage_and_ours.append(manager.method_name)
lists_to_extend = [manager.ahead_500_equal_code_little_size]
for lst in lists_to_extend:
manager.head_set.extend(lst)
manager.head_granularity.extend(lst)
manager.ahead_500_group3 = []
if "uncomp_group3" in manager.method_name and "uncomp_group32" not in manager.method_name:
manager.ahead_500_group3.append(manager.method_name)
manager.ahead_500_equal_code_group3=[]
lists_to_extend = [manager.ahead_500_group3]
for lst in lists_to_extend:
manager.head_set.extend(lst)
manager.head_granularity.extend(lst)
manager.ahead_500_equal_code_group3.extend(lst)
manager.ahead_500_group4 = []
if "uncomp_group4" in manager.method_name:
manager.ahead_500_group4.append(manager.method_name)
manager.ahead_500_equal_code_group4=[]
lists_to_extend = [manager.ahead_500_group4]
for lst in lists_to_extend:
manager.head_set.extend(lst)
manager.head_granularity.extend(lst)
manager.ahead_500_equal_code_group4.extend(lst)
manager.ahead_500_group5 = []
if "uncomp_group5" in manager.method_name:
manager.ahead_500_group5.append(manager.method_name)
manager.ahead_500_equal_code_group5=[]
lists_to_extend = [manager.ahead_500_group5]
for lst in lists_to_extend:
manager.head_set.extend(lst)
manager.head_granularity.extend(lst)
manager.ahead_500_equal_code_group5.extend(lst)
manager.ahead_500_group8 = []
if "uncomp_group8" in manager.method_name:
manager.ahead_500_group8.append(manager.method_name)
manager.ahead_500_equal_code_group8=[]
lists_to_extend = [manager.ahead_500_group8]
for lst in lists_to_extend:
manager.head_set.extend(lst)
manager.head_granularity.extend(lst)
manager.ahead_500_equal_code_group8.extend(lst)
manager.ahead_500_group32 = []
if "uncomp_group32" in manager.method_name:
manager.ahead_500_group32.append(manager.method_name)
manager.layer_groups = [2,2,2,2,2,2,2,2,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
2,2,2,2,2,2,2,2,]
manager.ahead_500_equal_code_group32=[]
lists_to_extend = [manager.ahead_500_group32]
for lst in lists_to_extend:
manager.head_set.extend(lst)
manager.head_granularity.extend(lst)
manager.ahead_500_equal_code_group32.extend(lst)
manager.pyramidkv_uncomp = []
if "pyramidkv" in manager.method_name and "uncomp" in manager.method_name and "stage" not in manager.method_name:
logger.info("pyramidkv_uncomp")
if "group4" in manager.method_name:
manager.ahead_500_group4.append(manager.method_name)
manager.pyramidkv_uncomp_new = manager.ahead_500_group4
elif "group8" in manager.method_name:
manager.ahead_500_group8.append(manager.method_name)
manager.pyramidkv_uncomp_new = manager.ahead_500_group8
elif "group32" in manager.method_name:
manager.layer_groups = [8,8,8,8,8,8,8,8,
4,4,4,4,4,4,4,4,
2,2,2,2,2,2,2,2,
1,1,1,1,1,1,1,1]
manager.ahead_500_group32.append(manager.method_name)
manager.pyramidkv_uncomp_new = manager.ahead_500_group32
else:
manager.ahead_500.append(manager.method_name)
manager.pyramidkv_uncomp_new = manager.ahead_500
manager.pyramidkv_uncomp.append(manager.method_name)
else:
manager.pyramidkv_uncomp_new= manager.ahead_500
lists_to_extend = [manager.pyramidkv_uncomp_new]
for lst in lists_to_extend:
manager.head_set.extend(lst)
manager.head_granularity.extend(lst)
manager.ahead_500_equal_code_group32.extend(lst)
manager.ahead_500.extend(manager.extreme_compressibility_equal_code)
manager.ahead_500_similar.extend(manager.delete_head_equal_code)
manager.delet_head_set.extend(manager.delete_head_equal_code)
def determine_head_type(self,manager,device):
if manager.method_name in manager.ahead_500:
data_all_layers = []
data_all_layers_2 = []
logger.info(f"args.model_path is {args.model_path}")
for i in range(manager.num_hidden_layers):
if "llama-3" in manager.model_path:
logger.info("llama3")
filename = f"./search/llama3-instruct/2_groups/svd32/head_type_search_layer" + str(i) + ".csv"
elif "tinyllama" in manager.model_path:
logger.info("tinyllama")
filename = f"./search/TinyLlama/2_groups/query/svd32/head_type_search_layer" + str(i) + ".csv"
elif "qwen2" in manager.model_path:
logger.info("qwen2")
filename = f"./search/qwen2/svd32/head_type_search_layer" + str(i) + ".csv"
elif "variance" in manager.method_name:
filename = "./search/512/llama2-chat/variance/head_type_search_layer" + str(i) + ".csv"
logger.info(f"filename is {filename}")
elif "uncomp" in manager.method_name:
if "nosvd" in manager.method_name:
filename = f"./search/llama2-chat/2_groups_nosvd/svd128/head_type_search_layer" + str(i) + ".csv"
else:
filename = "./search/512/llama2-chat/query/head_type_search_layer" + str(i) + ".csv"
data_layers = []
if os.path.isfile(filename):
import csv
with open(filename, 'r', newline='') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
data_layers.append([int(value) for value in row])
else:
logger.error("load error")
raise ValueError
data_layers = np.array(data_layers)
data_layers = data_layers.sum(axis=0)
num_heads = manager.num_attention_heads // 2
top_half_indices = np.argpartition(data_layers, -num_heads)[-num_heads:]
down_half_indices = np.argpartition(data_layers, -num_heads)[:num_heads]
indices = torch.cat([torch.tensor(down_half_indices).sort()[0],torch.tensor(top_half_indices).sort()[0]])
if "reverse" in manager.method_name:
indices = torch.cat([torch.tensor(top_half_indices).sort()[0],torch.tensor(down_half_indices).sort()[0]])
data_all_layers.append([top_half_indices,down_half_indices])
data_all_layers_2.append(indices)
manager.head_datas = torch.from_numpy(np.array(data_all_layers_2)).to(device)
elif manager.method_name in manager.ahead_500_group3:
data_all_layers_2 = []
for i in range(manager.num_hidden_layers):
numbers = 32
filename = f"./search/llama2-chat/3_groups/svd{numbers}/head_type_search_layer" + str(i) + ".csv"
data_layers = []
if os.path.isfile(filename):
import csv
with open(filename, 'r', newline='') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
data_layers.append([int(value) for value in row])
else:
logger.error("load error")
raise ValueError
data_layers = np.array(data_layers)
counts = np.array([np.bincount(data_layers[:, i], minlength=3) for i in range(32)]).T
zero_indices = np.argsort(counts[0])[-11:][::-1]
remaining_indices = np.setdiff1d(np.arange(32), zero_indices)
one_indices = remaining_indices[np.argsort(counts[1, remaining_indices])[-10:][::-1]]
final_indices = np.setdiff1d(np.arange(32), np.concatenate((zero_indices, one_indices)))
indices = torch.cat([torch.from_numpy(zero_indices.copy()).sort()[0],torch.from_numpy(one_indices.copy()).sort()[0],torch.from_numpy(final_indices.copy()).sort()[0]])
data_all_layers_2.append(indices)
manager.head_datas = torch.from_numpy(np.array(data_all_layers_2)).to(device)
elif manager.method_name in manager.ahead_500_group4:
data_all_layers_2 = []
for i in range(manager.num_hidden_layers):
logger.info(f"manager.model_path is {manager.model_path}")
if "llama-3" in manager.model_path:
logger.info("llama3")
filename = f"./search/llama3-instruct/4_groups/svd32/head_type_search_layer" + str(i) + ".csv"
elif "nosvd" in manager.method_name:
filename = f"./search/llama2-chat/4_groups_nosvd/svd128/head_type_search_layer" + str(i) + ".csv"
else:
filename = f"./search/llama2-chat/4_groups/svd{numbers}/head_type_search_layer" + str(i) + ".csv"
data_layers = []
if os.path.isfile(filename):
import csv
with open(filename, 'r', newline='') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
data_layers.append([int(value) for value in row])
else:
logger.error("load error")
raise ValueError
data_layers = np.array(data_layers)
def count_occurrences(arr):
counts = [[0, 0, 0, 0] for _ in range(32)]
for row in arr:
for i, value in enumerate(row):
counts[i][value] += 1
return counts
def get_top_indices(counts, n, value):
indexed_counts = list(enumerate(counts))
sorted_counts = sorted(indexed_counts, key=lambda x: x[1][value], reverse=True)
return [index for index, _ in sorted_counts[:n]]
counts = count_occurrences(data_layers)
remaining_indices = list(range(32))
results = []
for value in range(4):
top_indices = get_top_indices([counts[i] for i in remaining_indices], 8, value)
selected_indices = [remaining_indices[i] for i in top_indices]
results.append(selected_indices)
if value < 3:
remaining_indices = [i for i in remaining_indices if i not in selected_indices]
indices = torch.cat([
torch.tensor(results[0]).sort()[0],
torch.tensor(results[1]).sort()[0],
torch.tensor(results[2]).sort()[0],
torch.tensor(results[3]).sort()[0]
])
if "reverse" in manager.method_name:
indices = torch.cat([
torch.tensor(results[3]).sort()[0],
torch.tensor(results[2]).sort()[0],
torch.tensor(results[1]).sort()[0],
torch.tensor(results[0]).sort()[0]
])
data_all_layers_2.append(indices)
manager.head_datas = torch.from_numpy(np.array(data_all_layers_2)).to(device)
elif manager.method_name in manager.ahead_500_group5: