-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·231 lines (168 loc) · 6.01 KB
/
main.py
File metadata and controls
executable file
·231 lines (168 loc) · 6.01 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
from vllm import LLM, SamplingParams
from transformers import AutoTokenizer
from tqdm import tqdm
import re
import os
import json
import re
from argparse import ArgumentParser
def load_model(model_name):
llm = LLM(model_name, seed=42)
return llm
def load_dataset(demo_type):
with open("data.jsonl", "r") as f:
dataset = [json.loads(line) for line in f]
assert (
demo_type == "all"
or demo_type == "complex"
or demo_type == "easy"
or demo_type == "mid"
)
if demo_type != "all":
dataset = [d for d in dataset if d["demo_type"] == demo_type]
return [d["prompt"] for d in dataset], [d["label"] for d in dataset]
def compress_prompt_0(original_prompt, tokenizer=None):
"""
Compress the given prompt to
"""
# 保留前两个示例
*demonstrations, question = original_prompt.split("\n\n")
demonstrations = demonstrations[:2]
compressed_prompt = "\n\n".join(demonstrations) + "\n\n" + question
return compressed_prompt
def compress_prompt_1(original_prompt, tokenizer=None):
"""
Compress the given prompt to
"""
# 只保留长度最短的两个示例
*demonstrations, question = original_prompt.split("\n\n")
demonstrations = sorted(demonstrations, key=len)[:1]
compressed_prompt = "\n\n".join(demonstrations) + "\n\n" + question
return compressed_prompt
def compress_prompt_2(original_prompt, tokenizer=None):
"""
Compress the given prompt to
"""
# 省略解答过程, 只保留结果
*demonstrations, question = original_prompt.split("\n\n")
for index, value in enumerate(demonstrations):
sentences = value.split("\n")
demonstrations[index] = sentences[0] + "\n" + sentences[-1]
compressed_prompt = "\n\n".join(demonstrations) + "\n\n" + question
return compressed_prompt
def compress_prompt_3(original_prompt, tokenizer=None):
"""
Compress the given prompt to
"""
# 取代人名为 A (标注有 \u2019)
*demonstrations, question = original_prompt.split("\n\n")
demonstrations = [re.sub(r"\b(\w+)\u2019\b", r"A", i) for i in demonstrations]
compressed_prompt = "\n\n".join(demonstrations) + "\n\n" + question
return compressed_prompt
def compress_prompt_4(original_prompt, tokenizer=None):
"""
Compress the given prompt to
"""
# 删除特定的单词和句子
sentences = [
"Let's think step by step",
]
words = [
"first",
"second",
"third",
"then",
"and",
"therefore",
"thus",
"similarly",
"that",
]
*demonstrations, question = original_prompt.split("\n\n")
for index, value in enumerate(demonstrations):
s = value.split("\n")
value = [i for i in s if i not in sentences]
demonstrations[index] = "\n".join(value) + "\n"
words_pattern = r"\b(?:" + "|".join(words) + r")\b"
demonstrations = [
re.sub(words_pattern, "", i, flags=re.IGNORECASE) for i in demonstrations
]
compressed_prompt = "\n\n".join(demonstrations) + "\n\n" + question
return compressed_prompt
def compress_prompt_all(original_prompt, tokenizer=None):
# 综合使用之前的所有方法
compressed_prompt = compress_prompt_4(original_prompt)
compressed_prompt = compress_prompt_3(compressed_prompt)
compressed_prompt = compress_prompt_1(compressed_prompt)
return compressed_prompt
compress_methods = [
compress_prompt_0,
compress_prompt_1,
compress_prompt_2,
compress_prompt_3,
compress_prompt_4,
compress_prompt_all,
]
def generate_answer(prompts, model):
"""Generate answer for each text in texts"""
sampling_params = SamplingParams(temperature=0.3, top_p=0.95, max_tokens=150)
answers = []
for input_text in tqdm(prompts):
output = model.generate(
input_text, use_tqdm=False, sampling_params=sampling_params
)
answer = (
output[0].outputs[0].text.split("\n\n")[0]
) # only keep the first paragraph
answers.append(answer)
return answers
def evaluate_answers(answers, labels):
"""Evaluate the answers"""
scores = []
for answer, label in zip(answers, labels):
numbers = re.findall(r"\d+", answer)
scores.append(any([label == number for number in numbers]))
print("Accuracy: ", sum(scores) / len(scores))
return scores
def test_prompt(prompts, labels, args, compress, case):
print("case: ", case)
p = prompts[0]
ol = len(p)
print("original prompt: ", p)
compressed_prompt = compress(p)
cl = len(compressed_prompt)
print("compressed prompt: ", compressed_prompt)
print(ol, cl, "ratio: ", cl / ol)
def test(prompts, labels, args, compress, case):
print("case: ", case)
original_length = 0
compressed_length = 0
compressed_prompts = []
for p in prompts:
compressed_prompt = compress(p)
compressed_prompts.append(compressed_prompt)
original_length += len(p)
compressed_length += len(compressed_prompt)
print("compress ratio: ", compressed_length / original_length)
"""Conduct Inference"""
answers = generate_answer(compressed_prompts, model)
"""Evaluate the answers"""
scores = evaluate_answers(answers, labels)
if __name__ == "__main__":
os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"
"""Args"""
args = ArgumentParser()
args.add_argument("--model_name", type=str, default="facebook/opt-1.3b")
args.add_argument("--demo_type", type=str, default="all")
args = args.parse_args()
"""Load everything we need"""
model = load_model(args.model_name)
tokenizer = AutoTokenizer.from_pretrained(args.model_name)
prompts, labels = load_dataset(args.demo_type)
# """Compress the prompt"""
original_length = 0
compressed_length = 0
print(args.model_name)
for index, value in enumerate(compress_methods):
# test_prompt(prompts, labels, args, value, index)
test(prompts, labels, args, value, index)