-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_selfrag_no_threshold.py
378 lines (315 loc) · 16.1 KB
/
run_selfrag_no_threshold.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
"""
Code adapted from Self-RAG: https://github.com/AkariAsai/self-rag
"""
from transformers import AutoTokenizer
from vllm import LLM, SamplingParams
import random
import torch
import numpy as np
from tqdm import tqdm
import argparse
import re
from tqdm import tqdm
from utils import load_file, save_file_jsonl, metric_max_over_ground_truths,\
exact_match_score, match, qa_f1_score, save_file_json, \
num_tokens_from_string, load_special_tokens, control_tokens
def postprocess_answer_option_conditioned(answer):
for token in control_tokens:
answer = answer.replace(token, " ")
if "</s>" in answer:
answer = answer.replace("</s>", " ")
if "\n" in answer:
answer = answer.replace("\n", " ")
if "<|endoftext|>" in answer:
answer = answer.replace("<|endoftext|>", " ")
# add space between sentences
sentences = re.split('(?<=[\.\?\!])\s*', answer)
answer = ' '.join(sentences)
return answer
def format_prompt(input, paragraph=None):
prompt = "### Instruction:\n{0}\n\n### Response:\n".format(input)
if paragraph is not None:
prompt += "[Retrieval]<paragraph>{0}</paragraph>".format(paragraph)
return prompt
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--model_name', type=str)
parser.add_argument('--input_data_path', type=str)
parser.add_argument('--output_score_path', type=str, default=None, help='Output json file path')
parser.add_argument('--output_prediction_path', type=str, default=None, help='Output jsonl file path')
parser.add_argument('--limit_input', type=int, default=0)
parser.add_argument('--temperature', type=float, default=0.8)
parser.add_argument('--top_p', type=float, default=0.95)
parser.add_argument('--data_source', type=str, default="retrievalqa")
parser.add_argument('--task', type=str, default=None)
parser.add_argument('--device', type=str, default="cuda")
parser.add_argument('--max_tokens', type=int, default=50)
parser.add_argument("--doc_top_n", type=int, default=5,
help="Number of documents to retrieve per questions")
parser.add_argument("--world_size", type=int, default=1,
help="world size to use multiple GPUs.")
parser.add_argument("--dtype", type=str, default="half",
help="We use bfloat16 for training. If you run inference on GPUs that do not support BF16, please set this to be `half`.")
# Decoding hyperparams
parser.add_argument('--threshold', type=float,
default=None, help="Adaptive threshold.")
parser.add_argument("--use_seqscore", action="store_true")
parser.add_argument("--use_groundness", action="store_true",
help="use ground score")
parser.add_argument(
"--use_utility", action="store_true", help="tree search")
parser.add_argument("--beam_width", type=int,
default=2, help="beam search width")
parser.add_argument("--max_depth", type=int,
default=2, help="tree depth width")
parser.add_argument("--w_rel", type=float, default=1.0,
help="reward weight for document relevance")
parser.add_argument("--w_sup", type=float, default=1.0,
help="reward weight for generation support (attribution)")
parser.add_argument("--w_use", type=float, default=0.5,
help="reward weight for overall completeness / utility.")
parser.add_argument('--retrieval_mode', type=str, help="mode to control retrieval.",
default="default", choices=['adaptive_retrieval', 'no_retrieval', 'always_retrieval'],)
parser.add_argument('--metric', type=str, help="metric to be used during evaluation")
parser.add_argument('--seed', type=int, default=0)
args = parser.parse_args()
seed = args.seed
assert "selfrag" in args.model_name
torch.backends.cudnn.deterministic = True
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
########### load dataset ###########
input_data = load_file(args.input_data_path)
print(f"# total input_data: {len(input_data)}")
if args.data_source != "retrievalqa":
input_data = [item for item in input_data if item["data_source"] == args.data_source]
if args.limit_input > 0:
input_data = input_data[:args.limit_input]
print(f"\nselected data #: {len(input_data)}, data source: {args.data_source}")
print(input_data[0])
model = LLM(args.model_name, dtype="half")
tokenizer = AutoTokenizer.from_pretrained(args.model_name, padding_side="left")
sampling_params = SamplingParams(
temperature=args.temperature,
top_p=args.top_p,
max_tokens=args.max_tokens,
skip_special_tokens=False,
logprobs=32016
)
# Get token ids for reflection tokens.
ret_tokens, rel_tokens, grd_tokens, ut_tokens = load_special_tokens(
tokenizer, use_grounding=args.use_groundness, use_utility=args.use_utility)
preds = []
prompts = []
golds = []
metric_results = []
scores = []
all_results = []
count = 0
total_q_tokens = 0
total_context_tokens = 0
# initial prediction to see any [Retrieval] token exists
for idx in tqdm(range(len(input_data))):
item = input_data[idx]
question = item["question"]
# prepare prompts
prompt_no_retrieval = format_prompt(question, paragraph=None)
results = {}
preds = model.generate([prompt_no_retrieval], sampling_params)
pred_text = preds[0].outputs[0].text
results["no_retrieval"] = pred_text
item["do_retrieve_pred"] = pred_text
do_retrieval = 0
final_prompt = prompt_no_retrieval
# if check_string_exist(pred_text):
if "[Retrieval]" in pred_text:
do_retrieval = 1
evidences = item["context"]
# add paragraph in the format and re-generate
if isinstance(evidences[0], str):
prompt_w_retrieval = [
format_prompt(question, paragraph=evidence) for evidence in evidences
]
else:
prompt_w_retrieval = []
for evidence in evidences:
concat_evidence = ""
if 'title' in evidence:
concat_evidence += evidence["title"] + ". "
if "text" in evidence:
concat_evidence += evidence["text"]
prompt_w_retrieval.append(concat_evidence)
sampling_params = SamplingParams(
temperature=args.temperature, top_p=args.top_p, max_tokens=args.max_tokens, logprobs=5000)
preds = model.generate(prompt_w_retrieval, sampling_params)
### to find the best paragraph option from the top_n_doc
relevance_score_dict = {}
grd_score_dict = {}
ut_score_dict = {}
overall_scores = {}
for p_idx, pred in enumerate(preds):
pred_token_ids = pred.outputs[0].token_ids
pred_text = pred.outputs[0].text
pred_log_probs = pred.outputs[0].logprobs
seq_score = pred.outputs[0].cumulative_logprob / \
max(len(pred.outputs[0].token_ids), 1)
relevance_score_dict.setdefault(p_idx, {})
grd_score_dict.setdefault(p_idx, {})
ut_score_dict.setdefault(p_idx, {})
# Compute reward scores
for tok, id in rel_tokens.items():
prob = pred_log_probs[0][id] if id in pred_log_probs[0] else -100
relevance_score_dict[p_idx][tok] = np.exp(float(prob))
if grd_tokens is not None:
groundness_token_appear_indices = []
for tok_idx, tok in enumerate(pred_token_ids):
if tok in list(grd_tokens.values()):
groundness_token_appear_indices.append(tok_idx)
break
if len(groundness_token_appear_indices) > 0:
idx = groundness_token_appear_indices[0]
for token, token_id in grd_tokens.items():
prob = pred_log_probs[idx][token_id] if token_id in pred_log_probs[idx] else -100
grd_score_dict[p_idx][token] = np.exp(float(prob))
if ut_tokens is not None:
utility_token_appear_indices = []
for tok_idx, tok in enumerate(pred_token_ids):
if tok in list(ut_tokens.values()):
utility_token_appear_indices.append(tok_idx)
if len(utility_token_appear_indices) > 0:
idx = utility_token_appear_indices[0]
for token, token_id in ut_tokens.items():
prob = pred_log_probs[idx][token_id] if token_id in pred_log_probs[idx] else -100
ut_score_dict[p_idx][token] = np.exp(float(prob))
relevance_score = relevance_score_dict[p_idx]["[Relevant]"] / (
np.sum(list(relevance_score_dict[p_idx].values())))
if len(grd_score_dict[p_idx]) == 3:
gt_sum = np.sum(list(grd_score_dict[p_idx].values()))
ground_score = (grd_score_dict[p_idx]["[Fully supported]"] / gt_sum) + 0.5 * (
grd_score_dict[p_idx]["[Partially supported]"] / gt_sum)
else:
ground_score = 0.0
if len(ut_score_dict[p_idx]) == 5:
ut_sum = np.sum(list(ut_score_dict[p_idx].values()))
ut_scores = [-1, -0.5, 0, 0.5, 1]
utility_score = np.sum(
[ut_scores[i] * (ut_score_dict[p_idx]["[Utility:{}]".format(i+1)] / ut_sum) for i in range(len(ut_scores))])
else:
utility_score = 0.0
if args.use_seqscore is True:
final_score = np.exp(seq_score) + args.w_rel * relevance_score + \
args.w_sup * ground_score + args.w_use * utility_score
else:
final_score = args.w_rel * relevance_score + \
args.w_sup * ground_score + args.w_use * utility_score
overall_scores[p_idx] = {"final_score": final_score,
"relevance_score": relevance_score,
"ground_score": ground_score,
"utility_score": utility_score,
"relevance_score_dict": relevance_score_dict,
"grd_score_dict": grd_score_dict,
"ut_score_dict": utility_score}
results["retrieval_{}".format(p_idx)] = {
"pred": pred_text, "score": final_score, "ctx": evidences[p_idx]}
print(f"\n +++++++++++++ do_retrieval = {do_retrieval} +++++++++++++ \n\n")
# Aggregating answers
if len(results) == 1:
postprocessed_pred = postprocess_answer_option_conditioned(pred_text)
best_predict = postprocessed_pred
else:
path2score = {key: item["score"] for key,
item in results.items() if key != "no_retrieval"}
best_path = sorted(path2score.items(),
key=lambda x: x[1], reverse=True)[0][0]
best_option = results[best_path]["pred"]
final_prompt = results[best_path]["ctx"]
best_predict = best_option
# calculate token num
q_tokens = num_tokens_from_string(question)
context_tokens = 0
if do_retrieval == 1:
if isinstance(evidences[0], str):
concat_evidences = [f"{context}" for i, context in enumerate(evidences)]
else:
concat_evidences = [f"{context['title'].strip() if 'title' in context else ''}\n{context['text'].strip() if 'text' in context else ''}" for i, context in enumerate(evidences)]
total_context = "\n".join(concat_evidences)
context_tokens = num_tokens_from_string(total_context)
item["q_token_num"] = q_tokens
item["context_token_num"] = context_tokens
print(f"\n q_tokens: {q_tokens}, context_tokens: {context_tokens}")
if type(best_predict) is str and best_predict[0] == "#" or best_predict[0] == ":":
best_predict = best_predict[1:]
best_predict = postprocess_answer_option_conditioned(best_predict)
prompts.append(final_prompt)
preds.append(best_predict)
all_results.append(results)
if do_retrieval == 1:
count += 1
if args.metric == "match":
metric_result = match(best_predict, item["ground_truth"])
else:
raise NotImplementedError
metric_results.append(metric_result)
item["model_prediction"] = best_predict
item["do_retrieval"] = do_retrieval
########### Calculate metrics ###########
em_total, f1_total, acc_total, match_total = 0, 0, 0, 0
# for item in final_results:
for item in input_data:
pred = item["model_prediction"]
gts = item["ground_truth"]
em_score = 1.0 if metric_max_over_ground_truths(exact_match_score, pred, gts) else 0.0
accuracy_score = 1.0 if gts[0] in pred else 0.0
match_score = match(pred, gts) # loose match
f1_score = metric_max_over_ground_truths(qa_f1_score, pred, gts)
item["em_score"] = em_score
item["accuracy_score"] = accuracy_score
item["match_score"] = match_score
item["f1_score"] = f1_score
em_total += em_score
f1_total += f1_score
acc_total += accuracy_score
match_total += match_score
total_q_tokens = sum([item["q_token_num"] for item in input_data])
total_context_tokens = sum([item["context_token_num"] for item in input_data])
estimate_q_cost = total_q_tokens/1000*0.0005
estimate_context_cost = total_context_tokens/1000*0.0005
estimate_no_retrieval_cost = estimate_q_cost
estimate_always_retrieval_cost = estimate_q_cost + estimate_context_cost
total_retrieval = sum([item["do_retrieval"] for item in input_data])
print(f"\n ======= estimate no retrieval (q) API cost: {estimate_no_retrieval_cost}, total tokens #: {total_q_tokens} ================")
print(f" ======= estimate always retrieval (q+context) API cost: {estimate_always_retrieval_cost}, total tokens #: {total_context_tokens+total_q_tokens} ================")
print(f" ======= total retrieval: [{total_retrieval}/{len(input_data)}] ================\n")
total_score = {
"data_source": args.data_source,
"total_data_count": len(input_data),
"retrieval_frequency": total_retrieval,
"retrieval_rate": round(total_retrieval/len(input_data)*100, 1),
"match_score": round(match_total/len(input_data)*100, 1),
"f1_score": round(f1_total/len(input_data)*100, 1),
"em_score": round(em_total/len(input_data)*100, 1),
"accuracy_score": round(acc_total/len(input_data)*100, 1),
"match_total": match_total,
"f1_total": f1_total,
"em_total": em_total,
"accuracy_total": acc_total,
"total_q_tokens": total_q_tokens,
"total_context_tokens": total_context_tokens,
"total_no_retrieval_tokens": total_q_tokens,
"total_always_retrieval_tokens": total_context_tokens,
"estimate_no_retrieval_cost": estimate_no_retrieval_cost,
"estimate_always_retrieval_cost": estimate_always_retrieval_cost,
'args': vars(args)
}
print()
print(total_score)
# remove 'evidence'
for item in input_data:
if "evidence" in item:
del item["evidence"]
save_file_json(total_score, args.output_score_path)
save_file_jsonl(input_data, args.output_prediction_path)
if __name__ == "__main__":
main()