forked from facebookresearch/PyTorch-BigGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.py
266 lines (215 loc) · 9.35 KB
/
eval.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
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE.txt file in the root directory of this source tree.
import argparse
import logging
import time
from functools import partial
from typing import Callable, Generator, List, Optional, Tuple
import torch
from torchbiggraph.batching import AbstractBatchProcessor, call, process_in_batches
from torchbiggraph.bucket_scheduling import create_buckets_ordered_lexicographically
from torchbiggraph.checkpoint_manager import CheckpointManager
from torchbiggraph.config import add_to_sys_path, ConfigFileLoader, ConfigSchema
from torchbiggraph.edgelist import EdgeList
from torchbiggraph.graph_storages import EDGE_STORAGES
from torchbiggraph.losses import LOSS_FUNCTIONS
from torchbiggraph.model import make_model, MultiRelationEmbedder, Scores
from torchbiggraph.stats import average_of_sums, Stats
from torchbiggraph.types import Bucket, EntityName, Partition, UNPARTITIONED
from torchbiggraph.util import (
compute_randomized_auc,
create_pool,
EmbeddingHolder,
get_async_result,
get_num_workers,
set_logging_verbosity,
setup_logging,
split_almost_equally,
SubprocessInitializer,
tag_logs_with_process_name,
)
logger = logging.getLogger("torchbiggraph")
class RankingEvaluator(AbstractBatchProcessor):
def _process_one_batch(
self, model: MultiRelationEmbedder, batch_edges: EdgeList
) -> Stats:
with torch.no_grad():
scores, _ = model(batch_edges)
self._adjust_scores(scores, batch_edges)
batch_size = len(batch_edges)
loss = self.calc_loss(scores, batch_edges)
ranks = []
aucs = []
if scores.lhs_neg.nelement() > 0:
lhs_rank = (scores.lhs_neg >= scores.lhs_pos.unsqueeze(1)).sum(1) + 1
lhs_auc = compute_randomized_auc(scores.lhs_pos, scores.lhs_neg, batch_size)
ranks.append(lhs_rank)
aucs.append(lhs_auc)
if scores.rhs_neg.nelement() > 0:
rhs_rank = (scores.rhs_neg >= scores.rhs_pos.unsqueeze(1)).sum(1) + 1
rhs_auc = compute_randomized_auc(scores.rhs_pos, scores.rhs_neg, batch_size)
ranks.append(rhs_rank)
aucs.append(rhs_auc)
return Stats(
loss=float(loss),
pos_rank=average_of_sums(*ranks),
mrr=average_of_sums(*(rank.float().reciprocal() for rank in ranks)),
r1=average_of_sums(*(rank.le(1) for rank in ranks)),
r10=average_of_sums(*(rank.le(10) for rank in ranks)),
r50=average_of_sums(*(rank.le(50) for rank in ranks)),
# At the end the AUC will be averaged over count.
auc=batch_size * sum(aucs) / len(aucs),
count=batch_size,
)
def _adjust_scores(self, scores: Scores, batch_edges: EdgeList):
# This is a hook for the filtered evaluator to do the filtering
# of true edges
pass
def do_eval_and_report_stats(
config: ConfigSchema,
model: Optional[MultiRelationEmbedder] = None,
evaluator: Optional[AbstractBatchProcessor] = None,
subprocess_init: Optional[Callable[[], None]] = None,
) -> Generator[Tuple[Optional[int], Optional[Bucket], Stats], None, None]:
"""Computes eval metrics (mr/mrr/r1/r10/r50) for a checkpoint with trained
embeddings.
"""
tag_logs_with_process_name(f"Evaluator")
if evaluator is None:
evaluator = RankingEvaluator(
loss_fn=LOSS_FUNCTIONS.get_class(config.loss_fn)(margin=config.margin),
relation_weights=[relation.weight for relation in config.relations],
)
if config.verbose > 0:
import pprint
pprint.PrettyPrinter().pprint(config.to_dict())
checkpoint_manager = CheckpointManager(config.checkpoint_path)
def load_embeddings(entity: EntityName, part: Partition) -> torch.nn.Parameter:
embs, _ = checkpoint_manager.read(entity, part)
assert embs.is_shared()
return torch.nn.Parameter(embs)
holder = EmbeddingHolder(config)
num_workers = get_num_workers(config.workers)
pool = create_pool(
num_workers, subprocess_name="EvalWorker", subprocess_init=subprocess_init
)
if model is None:
model = make_model(config)
model.share_memory()
state_dict, _ = checkpoint_manager.maybe_read_model()
if state_dict is not None:
model.load_state_dict(state_dict, strict=False)
model.eval()
for entity in holder.lhs_unpartitioned_types | holder.rhs_unpartitioned_types:
embs = load_embeddings(entity, UNPARTITIONED)
holder.unpartitioned_embeddings[entity] = embs
all_stats: List[Stats] = []
for edge_path_idx, edge_path in enumerate(config.edge_paths):
logger.info(
f"Starting edge path {edge_path_idx + 1} / {len(config.edge_paths)} "
f"({edge_path})"
)
edge_storage = EDGE_STORAGES.make_instance(edge_path)
all_edge_path_stats = []
# FIXME This order assumes higher affinity on the left-hand side, as it's
# the one changing more slowly. Make this adaptive to the actual affinity.
for bucket in create_buckets_ordered_lexicographically(
holder.nparts_lhs, holder.nparts_rhs
):
tic = time.perf_counter()
# logger.info(f"{bucket}: Loading entities")
old_parts = set(holder.partitioned_embeddings.keys())
new_parts = {(e, bucket.lhs) for e in holder.lhs_partitioned_types} | {
(e, bucket.rhs) for e in holder.rhs_partitioned_types
}
for entity, part in old_parts - new_parts:
del holder.partitioned_embeddings[entity, part]
for entity, part in new_parts - old_parts:
embs = load_embeddings(entity, part)
holder.partitioned_embeddings[entity, part] = embs
model.set_all_embeddings(holder, bucket)
# logger.info(f"{bucket}: Loading edges")
edges = edge_storage.load_edges(bucket.lhs, bucket.rhs)
num_edges = len(edges)
load_time = time.perf_counter() - tic
tic = time.perf_counter()
# logger.info(f"{bucket}: Launching and waiting for workers")
future_all_bucket_stats = pool.map_async(
call,
[
partial(
process_in_batches,
batch_size=config.batch_size,
model=model,
batch_processor=evaluator,
edges=edges[s],
)
for s in split_almost_equally(num_edges, num_parts=num_workers)
],
)
all_bucket_stats = get_async_result(future_all_bucket_stats, pool)
compute_time = time.perf_counter() - tic
logger.info(
f"{bucket}: Processed {num_edges} edges in {compute_time:.2g} s "
f"({num_edges / compute_time / 1e6:.2g}M/sec); "
f"load time: {load_time:.2g} s"
)
total_bucket_stats = Stats.sum(all_bucket_stats)
all_edge_path_stats.append(total_bucket_stats)
mean_bucket_stats = total_bucket_stats.average()
logger.info(
f"Stats for edge path {edge_path_idx + 1} / {len(config.edge_paths)}, "
f"bucket {bucket}: {mean_bucket_stats}"
)
model.clear_all_embeddings()
yield edge_path_idx, bucket, mean_bucket_stats
total_edge_path_stats = Stats.sum(all_edge_path_stats)
all_stats.append(total_edge_path_stats)
mean_edge_path_stats = total_edge_path_stats.average()
logger.info("")
logger.info(
f"Stats for edge path {edge_path_idx + 1} / {len(config.edge_paths)}: "
f"{mean_edge_path_stats}"
)
logger.info("")
yield edge_path_idx, None, mean_edge_path_stats
mean_stats = Stats.sum(all_stats).average()
logger.info("")
logger.info(f"Stats: {mean_stats}")
logger.info("")
yield None, None, mean_stats
pool.close()
pool.join()
def do_eval(
config: ConfigSchema,
model: Optional[MultiRelationEmbedder] = None,
evaluator: Optional[AbstractBatchProcessor] = None,
subprocess_init: Optional[Callable[[], None]] = None,
) -> None:
# Create and run the generator until exhaustion.
for _ in do_eval_and_report_stats(config, model, evaluator, subprocess_init):
pass
def main():
setup_logging()
config_help = "\n\nConfig parameters:\n\n" + "\n".join(ConfigSchema.help())
parser = argparse.ArgumentParser(
epilog=config_help,
# Needed to preserve line wraps in epilog.
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("config", help="Path to config file")
parser.add_argument("-p", "--param", action="append", nargs="*")
opt = parser.parse_args()
loader = ConfigFileLoader()
config = loader.load_config(opt.config, opt.param)
set_logging_verbosity(config.verbose)
subprocess_init = SubprocessInitializer()
subprocess_init.register(setup_logging, config.verbose)
subprocess_init.register(add_to_sys_path, loader.config_dir.name)
do_eval(config, subprocess_init=subprocess_init)
if __name__ == "__main__":
main()