-
Notifications
You must be signed in to change notification settings - Fork 0
/
PokeMQA-turbo_n_edited.py
276 lines (204 loc) · 9.58 KB
/
PokeMQA-turbo_n_edited.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
import os
import json
import random
from tqdm import tqdm
import torch
import transformers
import logging
from sklearn.model_selection import StratifiedKFold
import openai
import numpy as np
from arguments import arg_parse
from train_predetector import train_detector
from train_disambiguator import train_disambiguator
import argparse
openai.api_key = os.getenv("OPENAI_API_KEY")
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
args = arg_parse()
edited_num = args.edited_num
dataset_name = args.dataset_name
retrain_detector = args.retraining_detector
cls_name = args.cls_name
retrain_disambiguator = args.retraining_disambiguator
seq_name = args.seq_name
use_kgprompt = args.activate_kgprompt
logging.basicConfig(filename=f'PokeMQA_result.log', level=logging.INFO, format='%(asctime)s - %(message)s')
logging.info(f'PokeMQA on GPT-3.5-turbo-instruct')
def call_gpt(cur_prompt, stop):
ans = openai.Completion.create(
model="gpt-3.5-turbo-instruct",
prompt= cur_prompt,
temperature=0,
stop=stop,
max_tokens = 200
)
returned = ans['choices'][0]['text']
return returned
def verify_subquestion_path(prompt,hops,path):
# Checking the correctness of reasoning path i.e. Calculating Hop-Acc
# subquestion verification
if not len(prompt.strip().split('Subquestion:')) == hops+1:
return 1
# reasoning path verification
sub_prompt = prompt.strip().split('Subquestion:')
for idx in range(1,len(sub_prompt)):
inter_ans = sub_prompt[idx].strip().split(': ')[-1]
print(inter_ans)
if inter_ans != path[idx-1]["answer"] and inter_ans not in path[idx-1]["answer_alias"]:
return 2
return False
# train or load scope detector
model_name = "distilbert-base-cased"
tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
if retrain_detector:
cls_name = train_detector() # negative_sample_num = 20
if retrain_disambiguator:
seq_name = train_disambiguator() # negative_sample_num = 1
torch.cuda.empty_cache()
classifier = transformers.AutoModel.from_pretrained(f"detector-checkpoint/{cls_name}").to(device)
seq_cls = transformers.AutoModelForSequenceClassification.from_pretrained(f"detector-checkpoint/{seq_name}").to(device)
# loading dataset
with open(f'datasets/{dataset_name}.json', 'r') as f:
dataset = json.load(f)
# construct edit batches of different sizes
samples = 3000 if dataset_name=='MQuAKE-CF-3k' else 1868
dataset_splits = samples // edited_num
hop_labels = [i//1000 for i in range(3000)]
if edited_num == 1 or edited_num == samples:
hop_labels = [0 for i in range(samples)]
if edited_num == 1:
skf = StratifiedKFold(n_splits= dataset_splits, shuffle=False)
elif edited_num != samples:
skf = StratifiedKFold(n_splits= dataset_splits, shuffle=True, random_state=42)
subsets = []
if edited_num == samples:
subsets.append([i for i in range(samples)])
else:
for _, test_index in skf.split(dataset, hop_labels):
subsets.append(test_index)
dataset_batch = []
edits_batch = []
embs_batch = []
for batch in subsets:
sub_dataset = [dataset[index] for index in batch]
new_facts = set()
for d in sub_dataset:
for r in d["requested_rewrite"]:
if f'{r["prompt"].format(r["subject"])} {r["target_new"]["str"]}' not in new_facts:
new_facts.add(f'{r["prompt"].format(r["subject"])} {r["target_new"]["str"]}')
new_facts = list(new_facts)
with torch.no_grad():
facts_input = tokenizer(new_facts, padding=True, truncation=True, max_length=256, return_tensors='pt').to(device)
facts_emb = classifier(**facts_input).last_hidden_state[:,0]
dataset_batch.append(sub_dataset)
embs_batch.append(facts_emb)
edits_batch.append(new_facts)
# load inference prompts
with open('prompts/KGprompt.txt','r') as f:
task_wkg_prompt = f.read()
with open('prompts/woKGprompt.txt','r') as f:
task_wokg_prompt = f.read()
# load pregenerated knowledge prompt
if use_kgprompt:
with open(f'kgprompt/{dataset_name}-kgprompt.json','r') as f:
pre_kgprompt = json.loads(f.read())
# stop token
stop = ["Generated answer:","\n\n"]
# Inference stage
print(f"PokeMQA inference start (edit batch : {edited_num} instance):")
logging.info(f"PokeMQA inference start (edit batch : {edited_num} instance):")
T = 3000
cor = 0
ver_cor = 0
cor_list = [0,0,0]
ver_cor_list = [0,0,0]
tot = 0
for no in range(dataset_splits):
for d in tqdm(dataset_batch[no]):
tot += 1
have_cor = False
if use_kgprompt:
kg_existing = pre_kgprompt[d["case_id"]-1]["existing"]
kg_mapping = pre_kgprompt[d["case_id"]-1]["mapping"]
for q in d["questions"]:
found_ans = False
prompt = task_wokg_prompt + "\n\nQuestion: "+ q
if use_kgprompt and kg_existing[q]:
prompt = task_wkg_prompt + "\n\nQuestion: "+ q + "\n" + "Entity of Question: " + kg_mapping[q]
for i in range(5):
# prompt the model to identify the subquestion
gen = call_gpt(prompt, stop)
last_sent = gen.strip().split('\n')[-1]
# if final answer is there, get the answer and exit
if last_sent.startswith('Final answer: '):
found_ans = True
ans = last_sent[len("Final answer: "):]
prompt = prompt + gen
break
# otherwise, extract the generated subquestion
if len(gen.strip().split('\n')) < 1 or len(gen.strip().split('\n')) > 3:
prompt = prompt + gen
break # failed case
subquestion = gen.strip().split('\n')[-1]
if not subquestion.startswith('Subquestion: '):
prompt = prompt + gen
break # failed case
subquestion = subquestion[len("Subquestion: "):]
# conflict detection
with torch.no_grad():
subquestion_input = tokenizer(subquestion, padding=True, truncation=True, max_length=256, return_tensors='pt').to(device)
subquestion_emb = classifier(**subquestion_input).last_hidden_state[:,0]
log_prob = (embs_batch[no]-subquestion_emb).norm(2,-1)
log_prob = -log_prob**2
prob = log_prob.exp()
if prob.max() < 0.5:
# prompt = prompt + gen +'Intermediate answer:'
prompt = prompt + gen + "Generated answer"
else:
idxs = prob >= 0.5
edits_mini = [edits_batch[no][i] for i in range(len(idxs)) if idxs[i]==True]
if len(edits_mini)>1:
input_batch = []
for can_edit in edits_mini:
input_batch.append(can_edit + tokenizer.sep_token + subquestion)
with torch.no_grad():
batch_input = tokenizer(input_batch, padding=True, truncation=True, max_length=256, return_tensors="pt").to(device)
batch_logits = seq_cls(**batch_input).logits
seq_prob = torch.softmax(batch_logits,-1)[:,0]
if seq_prob.max() < 0.5:
prompt = prompt + gen + 'Generated answer'
else:
value ,index = seq_prob.max(0)
# prompt = prompt + gen[:-remove_length] + edip ts_batch[no][index] + '.\nIntermediate answer:'
prompt = prompt + gen + 'Generated answer: '+edits_mini[index] + '.'
else:
value , index = prob.max(0)
prompt = prompt + gen + 'Generated answer: '+edits_batch[no][index] + '.'
if not found_ans:
continue
prompt = prompt.strip().split('\n\n')[-1]
# if the answer is correct -> positive instance for Acc
if ans == d["new_answer"] or ans in d["new_answer_alias"]:
instance_type = verify_subquestion_path(prompt,len(d["single_hops"]),d["new_single_hops"])
print('id:{} ans:{}'.format(d['case_id'],ans))
logging.info('id:{} ans:{} progress:{}/{}/{}'.format(d['case_id'],ans,cor,cor_list[len(d["single_hops"])-2],tot))
if not have_cor:
cor += 1
cor_list[len(d["single_hops"])-2] += 1
have_cor = True
if not instance_type: # positive instance for Hop-Acc
ver_cor += 1
ver_cor_list[len(d["single_hops"])-2] += 1
print('verification passed sum:{} hop:{}'.format(ver_cor,ver_cor_list[len(d["single_hops"])-2]))
logging.info('passVerification sum:{} hop:{}'.format(ver_cor,ver_cor_list[len(d["single_hops"])-2]))
break
print(f'Acc = {cor / tot} ({cor} / {tot})')
print(f'Hop-Acc = {ver_cor / tot} ({ver_cor} / {tot})')
logging.info(f'Multi-hop Acc = {cor / tot} ({cor} / {tot})')
logging.info(f'2-hop = {cor_list[0]}')
logging.info(f'3-hop = {cor_list[1]}')
logging.info(f'4-hop = {cor_list[2]}')
logging.info(f'Hop-wise Acc (Hop-Acc) = {ver_cor / tot} ({ver_cor} / {tot})')
logging.info(f'2-hop = {ver_cor_list[0]}')
logging.info(f'3-hop = {ver_cor_list[1]}')
logging.info(f'4-hop = {ver_cor_list[2]}')