-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqa_add_distractor.py
More file actions
403 lines (352 loc) · 14.6 KB
/
qa_add_distractor.py
File metadata and controls
403 lines (352 loc) · 14.6 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
import os
import re
import json
import argparse
from pathlib import Path
from tqdm import tqdm
import random
import numpy as np
from tokenizer import select_tokenizer
parser = argparse.ArgumentParser()
# Basic Configurations
parser.add_argument(
"--save_dir", type=Path, required=True, help="dataset folder to save dataset"
)
parser.add_argument(
"--save_name", type=str, required=True, help="name of the save dataset jsonl file"
)
parser.add_argument(
"--subset", type=str, default="validation", help="Options: validation or test"
)
parser.add_argument(
"--tokenizer_path", type=str, required=True, help="path to the tokenizer model"
)
parser.add_argument(
"--tokenizer_type", type=str, default="nemo", help="[Options] hf, openai."
)
parser.add_argument(
"--max_seq_length",
type=int,
required=True,
help="max sequence length including all input tokens and generated tokens.",
)
parser.add_argument(
"--tokens_to_generate",
type=int,
required=True,
help="expected generated token amount.",
)
parser.add_argument(
"--num_samples", type=int, required=True, help="number of samples to generate"
)
parser.add_argument(
"--pre_samples", type=int, default=0, help="number of samples are already generated"
)
parser.add_argument("--random_seed", type=int, default=42)
parser.add_argument("--template", type=str, required=True, help="prompt template")
parser.add_argument(
"--remove_newline_tab",
action="store_true",
help="remove `\n` and `\t` in all strings.",
)
# Complexity Configurations
parser.add_argument("--dataset", type=str, required=True, help="dataset file")
args = parser.parse_args()
random.seed(args.random_seed)
np.random.seed(args.random_seed)
TOKENIZER = select_tokenizer(args.tokenizer_type, args.tokenizer_path)
# Read SQuAD QA dataset
def read_squad(file):
with open(file) as f:
data = json.load(f)
total_docs = [p["context"] for d in data["data"] for p in d["paragraphs"]]
total_docs = sorted(list(set(total_docs)))
total_docs_dict = {c: idx for idx, c in enumerate(total_docs)}
total_qas = []
for d in data["data"]:
more_docs = [total_docs_dict[p["context"]] for p in d["paragraphs"]]
for p in d["paragraphs"]:
for qas in p["qas"]:
if not qas["is_impossible"]:
total_qas.append(
{
"query": qas["question"],
"outputs": [a["text"] for a in qas["answers"]],
"context": [total_docs_dict[p["context"]]],
"more_context": [
idx
for idx in more_docs
if idx != total_docs_dict[p["context"]]
],
}
)
return total_qas, total_docs
# Read Hotpot QA dataset
def read_hotpotqa(file):
with open(file) as f:
data = json.load(f)
total_docs = [f"{t}\n{''.join(p)}" for d in data for t, p in d["context"]]
total_docs = sorted(list(set(total_docs)))
total_docs_dict = {c: idx for idx, c in enumerate(total_docs)}
total_qas = []
for d in data:
total_qas.append(
{
"query": d["question"],
"outputs": [d["answer"]],
"context": [
total_docs_dict[f"{t}\n{''.join(p)}"] for t, p in d["context"]
],
}
)
return total_qas, total_docs
def read_musqiue(file):
with open(file) as f:
# read jsonl file
data = [json.loads(line) for line in f]
total_docs = []
for d in data:
for p in d["paragraphs"]:
total_docs.append(f"{p['title']}\n{p['paragraph_text']}")
print(len(total_docs))
total_docs = sorted(list(set(total_docs)))
total_docs_dict = {c: idx for idx, c in enumerate(total_docs)}
total_qas = []
for d in data:
# This only deals with questions that are answerable given the context
if d["answerable"]:
total_qas.append(
{
"query": d["question"],
"outputs": [d["answer"]],
"context": [
total_docs_dict[f"{p['title']}\n{p['paragraph_text']}"]
for p in d["paragraphs"]
],
}
)
return total_qas, total_docs
def read_2wikimqa(file):
with open(file) as f:
data = json.load(f)
total_docs = [f"{t}\n{''.join(p)}" for d in data for t, p in d["context"]]
total_docs = sorted(list(set(total_docs)))
total_docs_dict = {c: idx for idx, c in enumerate(total_docs)}
total_qas = []
for d in data:
total_qas.append(
{
"query": d["question"],
"outputs": [d["answer"]],
"context": [
total_docs_dict[f"{t}\n{''.join(p)}"] for t, p in d["context"]
],
}
)
return total_qas, total_docs
DOCUMENT_PROMPT = "Passage {i}:\n{document}"
if "hotpot" in args.dataset:
QAS, DOCS = read_hotpotqa(args.dataset)
elif "musique" in args.dataset:
QAS, DOCS = read_musqiue(args.dataset)
elif "2wikimqa" in args.dataset:
QAS, DOCS = read_2wikimqa(args.dataset)
else:
raise NotImplementedError(f"{args.dataset} is not implemented.")
def generate_input_output(index, num_docs):
curr_q = QAS[index]["query"]
curr_a = QAS[index]["outputs"]
curr_docs = QAS[index]["context"]
curr_more = QAS[index].get("more_context", [])
if num_docs < len(DOCS):
# If we have more documents than the number of documents we want to use
if len(curr_docs) > num_docs:
all_docs = random.sample(curr_docs, num_docs)
elif (num_docs - len(curr_docs)) > len(curr_more):
addition_docs = [
i for i, d in enumerate(DOCS) if i not in curr_docs + curr_more
]
all_docs = (
curr_docs
+ curr_more
+ random.sample(
addition_docs, max(0, num_docs - len(curr_docs) - len(curr_more))
)
)
else:
all_docs = curr_docs + random.sample(curr_more, num_docs - len(curr_docs))
all_docs = [DOCS[idx] for idx in all_docs]
else:
all_docs = DOCS
random.Random(args.random_seed).shuffle(all_docs)
context = "\n\n".join(
[DOCUMENT_PROMPT.format(i=i + 1, document=d) for i, d in enumerate(all_docs)]
)
input_text = args.template.format(
context=context,
)
return input_text, curr_a, curr_q
def generate_samples(
num_samples: int, max_seq_length: int, save_dir: str, incremental: int = 10
):
write_jsons = []
tokens_to_generate = args.tokens_to_generate
# Find the perfect num_docs
num_docs = incremental
total_tokens = 0 # Track the total tokens generated for this example
while total_tokens + tokens_to_generate < max_seq_length:
input_text, answer, question = generate_input_output(0, num_docs)
# Calculate the number of tokens in the example
total_tokens = len(TOKENIZER.text_to_tokens(input_text + f" {answer}"))
print(
f"Max length {max_seq_length} | Current length {total_tokens + tokens_to_generate} | Docs: {num_docs}"
)
if total_tokens + tokens_to_generate > max_seq_length:
num_docs -= incremental
break
num_docs += incremental
if num_docs > len(DOCS):
num_docs = len(DOCS)
break
print("Number of documents:", num_docs)
# Generate samples
for index in tqdm(range(num_samples)):
used_docs = num_docs
while True:
try:
input_text, answer, question = generate_input_output(
index + args.pre_samples, used_docs
)
length = len(TOKENIZER.text_to_tokens(input_text)) + tokens_to_generate
assert length <= max_seq_length, f"{length} exceeds max_seq_length."
break
except:
if used_docs > incremental:
used_docs -= incremental
if args.remove_newline_tab:
input_text = " ".join(
input_text.replace("\n", " ").replace("\t", " ").strip().split()
)
formatted_output = {
"index": index,
"input": question,
"context": input_text,
"answers": answer,
"length": length,
}
write_jsons.append(formatted_output)
return write_jsons
def main():
save_file = args.save_dir / f"{args.save_name}" / f"{args.subset}-{os.path.basename(args.tokenizer_path)}-num_sample_{args.num_samples}-max_seq_{args.max_seq_length}.jsonl"
dataset_name =""
if "hotpotqa" in args.save_dir:
dataset_name = "hotpotqa"
elif "musique" in args.save_dir:
dataset_name = "musique"
elif "2wikimqa" in args.save_dir:
dataset_name = "2wikimqa"
else:
raise NotImplementedError(f"{args.save_dir} is not implemented.")
# read the save file to write_json
with open(save_file, 'r') as f:
write_jsons = [json.loads(line) for line in f]
distract_questions=args.max_seq_length // 1024 * 16 if args.max_seq_length // 1024 > 0 else 16
if distract_questions>=0:
for item in write_jsons:
# Add distractor questions to the dataset
# add one more entry list named distract_questions
# sampled from all the questions in the dataset excluding the current question
if len(QAS) <= distract_questions:
continue
distract_qas = random.sample(
[q for i, q in enumerate(QAS) if i != item["index"]],
min(distract_questions, len(QAS) - 1)
)
distract_questions_list = []
# only keep the questions in the distractors
for distract_q in distract_qas:
distract_questions_list.append(distract_q["query"])
item["distract_questions"] = distract_questions_list
distractor_type = "chain"
chain_distractor_config = {
"num_chains": args.max_seq_length // 1024, # number of chains to generate, this should be a small number
"num_uuids": 4,
}
import uuid_test
for item in write_jsons:
if distractor_type == "chain":
chain_list = [ uuid_test.generate_uuid_chain(chain_distractor_config['num_uuids']) for _ in range(chain_distractor_config["num_chains"])]
chain_string_list = []
insert_input = True
for index, chain in enumerate(chain_list):
# Generate a string representation of the chain
if insert_input:
chain_string_list.append(
uuid_test.generate_uuid_string_from_chain(
uuids=chain,
end_with=item['input']
)
)
# get the head of the chain with the question
chain_head_with_question = chain[0]
insert_input = False
else:
chain_string_list.append(
uuid_test.generate_uuid_string_from_chain(
uuids=chain,
end_with=item['distract_questions'][index]
)
)
# Flatten the list of strings
flat_chain_string_list = sum(chain_string_list, [])
# shuffle the flat list to mix the distractors
random.shuffle(flat_chain_string_list)
# find all the occurrences of sentence stoppers, i.e., '.' or '?' or '\n' in the context
# randomly insert the distractor strings into the context
distractor_inserted_context = insert_distractor_into_context(
context=item["context"],
distractor_strings=flat_chain_string_list
)
item['distractor_context'] = distractor_inserted_context
item['chain_head_with_question'] = chain_head_with_question
resave_file = args.save_dir / f"{args.save_name}" / f"{args.subset}-{args.save_name}-dis_{distract_questions}-{os.path.basename(args.tokenizer_path)}-num_sample_{args.num_samples}-max_seq_{args.max_seq_length}.jsonl"
with open(resave_file, "w") as f:
for item in write_jsons:
f.write(json.dumps(item) + "\n")
def insert_distractor_into_context(context, distractor_strings):
"""
Insert distractor strings into the context at random positions.
"""
if not distractor_strings:
return context
# find all the sentences stoppers in the context
sentence_stoppers = [match.start() for match in re.finditer(r'[\n.?\n]', context)]
if not sentence_stoppers:
raise ValueError("No sentence stoppers found in the context to insert distractors.")
if len(sentence_stoppers) < len(distractor_strings):
raise ValueError(
f"Not enough sentence stoppers in the context to insert all distractors. Found {len(sentence_stoppers)} but need {len(distractor_strings)}."
)
# insert distractor strings after random sentence stoppers in the context
insertion_position = random.sample(
range(len(sentence_stoppers)),
len(distractor_strings)
)
insertion_position = [sentence_stoppers[i] for i in insertion_position]
assert len(insertion_position) == len(distractor_strings), \
f"Mismatch in insertion positions and distractor strings length: {len(insertion_position)} vs {len(distractor_strings)}"
distractor_string_tupple_list = [(pos, distractor) for pos, distractor in zip(insertion_position, distractor_strings)]
# sort the tupple list by position to insert in order of from big to small
distractor_string_tupple_list = sorted(distractor_string_tupple_list, key=lambda x: x[0], reverse=True)
distractor_inserted_context = context
for pos, distractor in distractor_string_tupple_list:
# insert the distractor string after the position of the sentence stopper
insertion_extra_char = ' ' if distractor_inserted_context[pos] != '\n' else '' # ensure readability
distractor_inserted_context = (
distractor_inserted_context[:pos + 1] # +1 to include the stopper
+ f"{insertion_extra_char}{distractor}." # add space around the distractor for readability
+ distractor_inserted_context[pos + 1:]
)
return distractor_inserted_context
if __name__ == "__main__":
main()