-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.py
467 lines (407 loc) · 15.8 KB
/
utils.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
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
import argparse
from dataclasses import dataclass
import json
import os
import random
from typing import Any, Dict, List, Optional
MAX_HITS = 10
@dataclass
class HitsMetric:
total: int = 0
hit1: int = 0
hit3: int = 0
hit10: int = 0
def update(self, rank):
if rank <= 1:
self.hit1 += 1
if rank <= 3:
self.hit3 += 1
if rank <= 10:
self.hit10 += 1
def dump(self):
return {
"total": self.total,
"hit1": self.hit1 / self.total,
"hit3": self.hit3 / self.total,
"hit10": self.hit10 / self.total,
}
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("--model", default="gpt2", type=str)
parser.add_argument(
"--dataset",
choices=["ICEWS14", "ICEWS18", "WIKI", "YAGO"],
default="ICEWS18",
type=str,
)
parser.add_argument(
"--multi_step", default=False, action="store_true"
) # inference in multi_step
# History Modeling
parser.add_argument(
"--history_type", choices=["entity", "pair"], default="entity", type=str
) # history type
parser.add_argument(
"--history_direction", choices=["uni", "bi"], default="uni", type=str
) # history type
parser.add_argument("--history_len", default=0, type=int) # length of history
parser.add_argument("--history_top_k", default=1, type=int) # length of targets from history
# Prompt Construction
parser.add_argument("--label", default=False, action="store_true") # express prompt with label
parser.add_argument(
"--text_style", default=False, action="store_true"
) # express prompt in text
parser.add_argument(
"--no_entity", default=False, action="store_true"
) # express prompt without entity
parser.add_argument("--sys_instruction", default="", type=str) # system instcution for ChatGPT
parser.add_argument(
"--no_time", default=False, action="store_true"
) # express prompt without time
parser.add_argument("--shuffle_history", default=False, action="store_true") # shuffle history
# Hyperparameter
parser.add_argument("--top_k", default=100, type=int) # number of predictions to store
parser.add_argument(
"--dec_cand", default=5, type=int
) # number of candidates to decode at each step
parser.add_argument("--max_length", default=1, type=int) # max decoding length
parser.add_argument("--world_size", default=1, type=int) # number of chunks
parser.add_argument("--rank", default=0, type=int) # rankd of the executor
parser.add_argument(
"--tokenizer_revision", default="main", type=str
) # change tokenizer revision (for llama)
parser.add_argument(
"--fp16", default=False, action="store_true"
) # use float16 instead of float32
parser.add_argument("--verbose", default=False, action="store_true") # print extra information
# Evaluation
parser.add_argument(
"--eval_filter",
choices=["none", "static", "time-aware"],
type=str,
default="none",
)
args = parser.parse_args()
assert args.label or not args.no_entity
return args
# Read entity2id, relation2id
def load_dictionary(in_path: str, file_name: str) -> Dict[int, str]:
_dict = {}
with open(os.path.join(in_path, file_name), "r", encoding="utf-8") as fr:
for line in fr:
line_split = line.split("\t")
node = line_split[0]
index = int(line_split[1])
_dict[index] = node
return _dict
# Read train, valid data to construct search space
def load_quadruples(
search_dictionary: Dict[Any, Dict[Any, Dict[Any, List[Any]]]],
in_path: str,
file_name: str,
entity_dictionary: Optional[Dict[int, str]] = None,
relation_dictionary: Optional[Dict[int, str]] = None,
query: str = "head",
):
discard_line, total_line = 0, 0
with open(os.path.join(in_path, file_name), "r", encoding="utf-8") as fr:
for line in fr:
total_line += 1
line_split = line.split()
if entity_dictionary and relation_dictionary:
if (
int(line_split[0]) not in entity_dictionary
or int(line_split[2]) not in entity_dictionary
or int(line_split[1]) not in relation_dictionary
):
print(line)
discard_line += 1
continue
head = entity_dictionary[int(line_split[0])]
tail = entity_dictionary[int(line_split[2])]
rel = relation_dictionary[int(line_split[1])]
else:
head = int(line_split[0])
tail = int(line_split[2])
rel = int(line_split[1])
time = int(line_split[3])
if query == "head":
if head not in search_dictionary:
search_dictionary[head] = {}
if time not in search_dictionary[head]:
search_dictionary[head][time] = {}
if rel not in search_dictionary[head][time]:
search_dictionary[head][time][rel] = []
search_dictionary[head][time][rel].append(tail)
elif query == "tail":
if tail not in search_dictionary:
search_dictionary[tail] = {}
if time not in search_dictionary[tail]:
search_dictionary[tail][time] = {}
if rel not in search_dictionary[tail][time]:
search_dictionary[tail][time][rel] = []
search_dictionary[tail][time][rel].append(head)
print(f"# line discarded due to index issue: {discard_line} / {total_line}")
# Read test data to inferencee
def load_quadruples_for_test(
in_path: str,
file_name: str,
entity_dictionary: Optional[Dict[int, str]] = None,
relation_dictionary: Optional[Dict[int, str]] = None,
) -> List[List[Any]]:
test_instances = []
with open(os.path.join(in_path, file_name), "r", encoding="utf-8") as fr:
for line in fr:
line_split = line.split()
if entity_dictionary and relation_dictionary:
if (
int(line_split[0]) not in entity_dictionary
or int(line_split[2]) not in entity_dictionary
or int(line_split[1]) not in relation_dictionary
):
print(line)
continue
head = entity_dictionary[int(line_split[0])]
tail = entity_dictionary[int(line_split[2])]
rel = relation_dictionary[int(line_split[1])]
else:
head = int(line_split[0])
tail = int(line_split[2])
rel = int(line_split[1])
time = int(line_split[3])
test_instances.append((head, rel, tail, time))
return test_instances
def format_data(data):
tail_prediction, head_prediction = {}, {}
for head, rel, tail, time in data:
tail_key = (head, rel, time)
if tail_key not in tail_prediction:
tail_prediction[tail_key] = []
tail_prediction[tail_key].append(tail)
head_key = (tail, rel, time)
if head_key not in head_prediction:
head_prediction[head_key] = []
head_prediction[head_key].append(head)
formatted_data = list(
sorted(
[([k[0], k[1], list(set(v)), k[2]], "tail") for k, v in tail_prediction.items()]
+ [([k[0], k[1], list(set(v)), k[2]], "head") for k, v in head_prediction.items()],
key=lambda x: x[0][3],
)
)
return formatted_data
def load_data(args: argparse.Namespace):
entity_dictionary, relation_dictionary = None, None
if args.text_style:
entity_dictionary = load_dictionary("data", os.path.join(args.dataset, "entity2id.txt"))
relation_dictionary = load_dictionary("data", os.path.join(args.dataset, "relation2id.txt"))
head_search_space = {}
load_quadruples(
head_search_space,
"data",
os.path.join(args.dataset, "train.txt"),
entity_dictionary,
relation_dictionary,
query="head",
)
load_quadruples(
head_search_space,
"data",
os.path.join(args.dataset, "valid.txt"),
entity_dictionary,
relation_dictionary,
query="head",
)
tail_search_space = {}
load_quadruples(
tail_search_space,
"data",
os.path.join(args.dataset, "train.txt"),
entity_dictionary,
relation_dictionary,
query="tail",
)
load_quadruples(
tail_search_space,
"data",
os.path.join(args.dataset, "valid.txt"),
entity_dictionary,
relation_dictionary,
query="tail",
)
if args.history_direction == "bi":
head_search_space.update(tail_search_space)
tail_search_space = head_search_space
test_data = load_quadruples_for_test(
"data",
os.path.join(args.dataset, "test.txt"),
entity_dictionary,
relation_dictionary,
)
formatted_test_data = format_data(test_data)
return formatted_test_data, head_search_space, tail_search_space
def adjust_top_k(test_data, args):
max_targets_len = max([len(x[0][2]) for x in test_data])
args.top_k = max(args.top_k, MAX_HITS, max_targets_len + MAX_HITS)
if args.verbose:
print(f"max targets len: {max_targets_len}")
print(f"adjusted top k: {args.top_k}")
def get_filename(args: argparse.Namespace, is_eval: bool = False):
model_name = args.model.split("/")[-1]
filename_args = "_".join(
[
model_name,
args.dataset,
f"multi_step_{args.multi_step}",
f"history_len_{args.history_len}",
f"history_type_{args.history_type}",
f"history_direction_{args.history_direction}",
f"no_time_{args.no_time}",
f"shuffle_history_{args.shuffle_history}",
f"label_{args.label}",
f"text_style_{args.text_style}",
f"no_entity_{args.no_entity}",
f'world_size_{"*" if is_eval else args.world_size}',
f'rank_{"*" if is_eval else args.rank}',
]
)
filename = f"outputs/{filename_args}.jsonl"
print(f"output file: {filename}")
return filename
def construct_history_by_search(
search_space: Dict[str, Any], entity: str, relation: str, history_type: str
):
if entity not in search_space:
return {}
search_graph = {entity: {}}
if history_type == "entity":
search_graph[entity] = search_space[entity]
elif history_type == "pair":
search_graph[entity] = {
k: {relation: v[relation]} for k, v in search_space[entity].items() if relation in v
}
return search_graph
def format_history(
history_graph: Dict[str, Any],
history_len: int,
question: List[str],
args: argparse.Namespace,
return_prompt: bool = True,
):
quadruples = []
for entity in history_graph:
for time in history_graph[entity]:
if time >= question[0]:
continue
for relation in history_graph[entity][time]:
for target in history_graph[entity][time][relation]:
quadruples.append([entity, relation, target, time])
candidates_stats = {}
if args.model == "recency":
for x in quadruples[-history_len:]:
if x[2] not in candidates_stats:
candidates_stats[x[2]] = -1
candidates_stats[x[2]] = max(candidates_stats[x[2]], x[3])
else:
for x in quadruples[-history_len:]:
if x[2] not in candidates_stats:
candidates_stats[x[2]] = 0
candidates_stats[x[2]] += 1
candidates_stats_sorted = list(
sorted(candidates_stats.items(), key=lambda item: item[1], reverse=True)
)
candidates_mapping = {}
for i, (entity, _) in enumerate(candidates_stats_sorted):
candidates_mapping[entity] = i
if (args.label or args.no_entity) and args.model not in ["recency", "frequency"]:
candidates = {v: k for k, v in candidates_mapping.items()} # label --> entity
else:
candidates = {k: k for k, _ in candidates_mapping.items()} # entity --> entity
if return_prompt:
prompt = ""
history = quadruples[-history_len:]
if args.shuffle_history:
random.shuffle(history)
for x in history:
entity, relation, target, time = x[0], x[1], x[2], x[3]
if not args.no_time:
prompt += f"{time}:"
if args.no_entity:
prompt += f"[{entity},{relation},{candidates_mapping[target]}]\n"
elif args.label:
prompt += f"[{entity},{relation},{candidates_mapping[target]}.{target}]\n"
else:
prompt += f"[{entity},{relation},{target}]\n"
if not args.no_time:
prompt += f"{question[0]}:"
prompt += f"[{question[1]},{question[2]},"
return prompt, candidates
else:
return candidates_stats_sorted, candidates
def prepare_input(x, entity_search_space, args, return_prompt: bool = True):
entity, relation, time = x[0], x[1], x[3]
entity_history = construct_history_by_search(
entity_search_space,
entity=entity,
relation=relation,
history_type=args.history_type,
)
history_input, candidates = format_history(
entity_history,
args.history_len,
[time, entity, relation],
args=args,
return_prompt=return_prompt,
)
if args.verbose:
print(f"input:\n{history_input}\ncandidates:\n{candidates}")
if entity not in entity_search_space:
entity_search_space[entity] = {}
if time not in entity_search_space[entity]:
entity_search_space[entity][time] = {}
if relation not in entity_search_space[entity][time]:
entity_search_space[entity][time][relation] = []
return history_input, candidates
def update_history(x, entity_search_space, predictions, candidates, args):
entity, relation, targets, time = x[0], x[1], x[2], x[3]
if args.verbose:
print(
f"search space:\n{entity},{relation},{time} --> {entity_search_space[entity][time][relation]}"
)
if args.multi_step:
filtered_predictions = [candidates[x[0]] for x in predictions if x[0] in candidates]
targets = filtered_predictions[: args.history_top_k]
entity_search_space[entity][time][relation] += targets
if args.verbose:
print(f"history:\n{entity},{relation},{time} --> {targets}")
print(
f"search space:\n{entity},{relation},{time} --> {entity_search_space[entity][time][relation]}"
)
def write_results(x, predictions, candidates, direction, writer, args):
entity, relation, targets, time = x[0], x[1], x[2], x[3]
example = {
"timestamp": time,
"entity": entity,
"relation": relation,
"targets": targets,
"direction": direction,
"predictions": [candidates[x[0]] for x in predictions if x[0] in candidates],
}
writer.write(json.dumps(example) + "\n")
if args.verbose:
print(f"example:\n{json.dumps(example, indent=2)}")
return example
def update_metric(example, metric, args):
if args.verbose:
print(f'predictions: {example["predictions"]}')
for target in example["targets"]:
metric.total += 1
index = example["predictions"].index(target) if target in example["predictions"] else -1
if index >= 0:
_predictions = [
x for x in example["predictions"][:index] if x not in example["targets"]
]
rank = len(_predictions) + 1
if args.verbose:
print(f"target: {target} --> rank: {rank}")
metric.update(rank)