forked from pykeen/pykeen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluation_loop.py
477 lines (410 loc) · 16.8 KB
/
evaluation_loop.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
468
469
470
471
472
473
474
475
476
477
# -*- coding: utf-8 -*-
"""Evaluation loops for KGE models."""
import dataclasses
import logging
from abc import abstractmethod
from collections import defaultdict
from typing import Any, Collection, DefaultDict, Generic, Iterable, List, Mapping, Optional, Tuple, TypeVar, Union, cast
import numpy
import pandas
import torch
from class_resolver import HintOrType, OptionalKwargs
from torch.utils.data import Dataset
from torch.utils.data.dataloader import DataLoader
from torch_max_mem import maximize_memory_utilization
from tqdm.auto import tqdm
from typing_extensions import TypeAlias
from .evaluator import Evaluator, MetricResults, filter_scores_
from ..constants import COLUMN_LABELS, TARGET_TO_INDEX
from ..models import Model
from ..triples import CoreTriplesFactory, get_mapped_triples
from ..typing import LABEL_HEAD, LABEL_TAIL, InductiveMode, MappedTriples, OneOrSequence, Target
from ..utils import upgrade_to_sequence
__all__ = [
"AdditionalFilterTriplesHint",
# Evaluation loops
"EvaluationLoop",
"LCWAEvaluationLoop",
# Evaluation datasets
"LCWAEvaluationDataset",
]
logger = logging.getLogger(__name__)
BatchType = TypeVar("BatchType")
AdditionalFilterTriplesHint: TypeAlias = Optional[OneOrSequence[Union[MappedTriples, CoreTriplesFactory]]]
def _hasher(d: Mapping[str, Any]) -> int:
"""
Calculate hash based on ID of dataset.
This means that we can have separate batch sizes for different evaluation datasets.
:param d:
the dictionary of keyword-based parameters
:return:
the dataset's ID
"""
obj = d["loop"]
assert isinstance(obj, EvaluationLoop)
obj = obj.dataset
return id(obj)
@maximize_memory_utilization(hasher=_hasher)
def _evaluate(
loop: "EvaluationLoop",
batch_size: int,
use_tqdm: bool,
tqdm_kwargs: OptionalKwargs,
**kwargs,
) -> MetricResults:
"""
Run the evaluation loop for a given batch size.
.. note::
this method is wrapped into a `MemoryUtilizationMaximizer` instance to automatically tune the `batch_size`.
:param loop:
the evaluation loop instance.
:param batch_size:
the batch size
:param use_tqdm:
whether to use tqdm progress bar
:param tqdm_kwargs:
additional keyword-based parameters for the progress bar
:param kwargs:
additional keyword-based parameters passed to :meth:`EvaluationLoop.get_loader`
:return:
the evaluation results
"""
loop.model.eval()
loader = loop.get_loader(batch_size=batch_size, **kwargs)
total = len(loader)
if use_tqdm:
loader = tqdm(
loader,
desc="evaluation",
total=total,
unit="batch",
unit_scale=True,
**(tqdm_kwargs or {}),
)
for batch in loader:
loop.process_batch(batch=batch)
return loop.evaluator.finalize()
class EvaluationLoop(Generic[BatchType]):
"""A base class for evaluation loops."""
def __init__(
self,
model: Model,
dataset: Dataset[BatchType],
evaluator: Evaluator,
) -> None:
"""
Initialize the evaluation loop.
:param model:
the model to evaluate.
:param dataset:
the evaluation dataset
:param evaluator:
the evaluator instance
"""
self.model = model
self.evaluator = evaluator
self.dataset = dataset
@abstractmethod
def process_batch(self, batch: BatchType) -> None:
"""
Process a single batch.
:param batch:
one batch of evaluation samples from the dataset.
"""
raise NotImplementedError
def get_collator(self):
"""Get the collator to use for the data loader."""
return None
def get_loader(self, batch_size: int, pin_memory: bool = True, **kwargs) -> DataLoader:
"""
Create a data loader for a single evaluation round.
:param batch_size:
the batch size
:param pin_memory:
whether to pin memory, cf. :meth:`DataLoader.__init__`
:param kwargs:
additional keyword-based parameters passed to :meth:`DataLoader.__init__`
:return:
a dataloader for the evaluation dataset of the given batch size
"""
return DataLoader(
dataset=self.dataset,
batch_size=batch_size,
shuffle=False,
pin_memory=pin_memory,
collate_fn=self.get_collator(),
**kwargs,
)
@torch.inference_mode()
def evaluate(
self,
# batch
batch_size: Optional[int] = None,
# tqdm
use_tqdm: bool = True,
tqdm_kwargs: OptionalKwargs = None,
# data loader
**kwargs,
) -> MetricResults:
"""
Evaluate the loop's model on the loop's dataset.
.. note::
the contained model will be set to evaluation mode.
:param batch_size:
the batch size. If None, enable automatic memory optimization to maximize memory utilization.
:param use_tqdm:
whether to use tqdm progress bar
:param tqdm_kwargs:
additional keyword-based parameters passed to tqdm
:param kwargs:
additional keyword-based parameters passed to :meth:`get_loader`
:return:
the evaluation results.
"""
# set upper limit of batch size for automatic memory optimization
if not batch_size:
if self.model.device.type == "cpu":
batch_size = 32
else:
batch_size = len(self.dataset)
# set model to evaluation mode
self.model.eval()
# delegate to AMO wrapper
return _evaluate(
loop=self,
batch_size=batch_size,
use_tqdm=use_tqdm,
tqdm_kwargs=tqdm_kwargs,
**kwargs,
)
@dataclasses.dataclass
class FilterIndex:
"""An index structure for filtering (roughly following CSR)."""
# The key-id for each triple, shape: (num_triples,)
triple_id_to_key_id: numpy.ndarray
#: the number of targets for each key, shape: (num_unique_keys + 1,)
bounds: numpy.ndarray
#: the concatenation of unique targets for each key (use bounds to select appropriate sub-array)
indices: torch.LongTensor
@classmethod
def from_df(cls, df: pandas.DataFrame, target: Target) -> "FilterIndex":
"""
Create index from dataframe.
:param df:
the dataframe, comprising columns [LABEL_HEAD, LABEL_RELATION, LABEL_TAIL]
:param target:
the prediction target
:raises ValueError:
if some of the expected columns are missing
:return:
a filter index object
"""
# input verification
expected_columns = set(COLUMN_LABELS)
if not expected_columns.issubset(df.columns):
raise ValueError(f"Missing columns: {sorted(expected_columns.difference(df.columns))}")
# group key = everything except the prediction target
key = [c for c in df.columns if c != target]
# initialize data structure
triple_id_to_key_id = numpy.empty_like(df.index)
indices = []
bounds = [0]
# group by key
for key_id, (_, group) in enumerate(df.groupby(by=key)):
unique_targets = group[target].unique()
triple_id_to_key_id[group.index] = key_id
indices.extend(unique_targets)
bounds.append(len(indices))
# convert lists to arrays
indices = cast(torch.LongTensor, torch.as_tensor(indices))
bounds = numpy.asarray(bounds)
# instantiate
return cls(triple_id_to_key_id=triple_id_to_key_id, bounds=bounds, indices=indices)
def __getitem__(self, item: int) -> numpy.ndarray: # noqa: D105
# return indices corresponding to the `item`-th triple
key_id = self.triple_id_to_key_id[item]
low, high = self.bounds[key_id : key_id + 2]
return self.indices[low:high]
class LCWAEvaluationDataset(Dataset[Mapping[Target, Tuple[MappedTriples, Optional[torch.Tensor]]]]):
"""A dataset for link prediction evaluation."""
filter_indices: Optional[Mapping[Target, FilterIndex]]
def __init__(
self,
*,
mapped_triples: Optional[MappedTriples] = None,
factory: Optional[CoreTriplesFactory] = None,
targets: Optional[Collection[Target]] = None,
filtered: bool = True,
additional_filter_triples: AdditionalFilterTriplesHint = None,
) -> None:
"""
Create a PyTorch dataset for link prediction evaluation.
:param mapped_triples: shape: (n, 3)
the ID-based triples
:param factory:
the triples factory. Only used of `mapped_triples` is None
:param targets:
the prediction targets. Defaults to head and tail prediction
:param filtered:
whether to use filtered evaluation, i.e., prepare filter indices
:param additional_filter_triples:
additional filter triples to use for creating the filter
"""
super().__init__()
# input normalization
if targets is None:
targets = [LABEL_HEAD, LABEL_TAIL]
mapped_triples = get_mapped_triples(mapped_triples=mapped_triples, factory=factory)
self.mapped_triples = mapped_triples
self.num_triples = mapped_triples.shape[0]
self.targets = tuple(targets)
# prepare filter indices if required
if filtered:
if not additional_filter_triples:
logger.warning("Enabled filtered evaluation, but not additional filter triples are passed.")
df = pandas.DataFrame(
data=torch.cat(
[
mapped_triples,
*(get_mapped_triples(x) for x in upgrade_to_sequence(additional_filter_triples or [])),
]
),
columns=COLUMN_LABELS,
)
self.filter_indices = {target: FilterIndex.from_df(df=df, target=target) for target in targets}
else:
if additional_filter_triples:
logger.warning("Passed additional filter triples, but filtered evaluation is disabled.")
self.filter_indices = None
@property
def num_targets(self) -> int:
"""Return the number of targets."""
return len(self.targets)
def __len__(self) -> int: # noqa: D105
return self.num_triples * self.num_targets
def __getitem__(self, index: int) -> Tuple[Target, MappedTriples, Optional[torch.LongTensor]]: # noqa: D105
# sorted by target -> most of the batches only have a single target
target_id, index = divmod(index, self.num_triples)
target = self.targets[target_id]
triple = self.mapped_triples[index, :]
nnz = None if self.filter_indices is None else self.filter_indices[target][index]
return target, triple, nnz
@staticmethod
def collate(
batch: Iterable[Tuple[Target, MappedTriples, Optional[torch.LongTensor]]]
) -> Mapping[Target, Tuple[MappedTriples, Optional[torch.Tensor]]]:
"""Collate batches by grouping by target."""
# group by target
triples: DefaultDict[Target, List[torch.LongTensor]] = defaultdict(list)
nnz: DefaultDict[Target, List[torch.LongTensor]] = defaultdict(list)
for target, triple, opt_nnz in batch:
triples[target].append(triple)
if opt_nnz is not None:
nnz[target].append(opt_nnz)
# stack groups into a single tensor
result = {}
for target in triples.keys():
target_triples = cast(MappedTriples, torch.stack(triples[target]))
if target in nnz:
batch_ids = []
target_nnz = nnz[target]
for batch_id, size in enumerate(map(len, target_nnz)):
batch_ids.append(torch.full(size=(size,), fill_value=batch_id, dtype=torch.long))
batch_ids = torch.cat(batch_ids)
target_nnz = torch.cat(target_nnz)
sparse_filter_mask = torch.stack([batch_ids, target_nnz], dim=-1)
else:
sparse_filter_mask = None
result[target] = (target_triples, sparse_filter_mask)
return result
class LCWAEvaluationLoop(EvaluationLoop[Mapping[Target, MappedTriples]]):
r"""
Evaluation loop using 1:n scoring.
For brevity, we only describe evaluation for tail prediction. Let $(h, r, t) \in \mathcal{T}_{eval}$ denote an
evaluation triple. Then, we calculate scores for all triples $(h, r, t')$ with $t' \in \mathcal{E}$, i.e., for
replacing the true tail $t$ by all entities.
"""
def __init__(
self,
triples_factory: CoreTriplesFactory,
evaluator: HintOrType[Evaluator] = None,
evaluator_kwargs: OptionalKwargs = None,
targets: Collection[Target] = (LABEL_HEAD, LABEL_TAIL),
mode: Optional[InductiveMode] = None,
additional_filter_triples: AdditionalFilterTriplesHint = None,
**kwargs,
) -> None:
"""
Initialize the evaluation loop.
:param triples_factory:
the evaluation triples factory
:param evaluator:
the evaluator, or a hint thereof
:param evaluator_kwargs:
additional keyword-based parameters for instantiating the evaluator
:param targets:
the prediction targets.
:param mode:
the inductive mode, or None for transductive evaluation
:param additional_filter_triples:
additional filter triples to use for creating the filter
:param kwargs:
additional keyword-based parameters passed to :meth:`EvaluationLoop.__init__`. Should not contain the keys
`dataset` or `evaluator`.
"""
# avoid cyclic imports
from . import evaluator_resolver
# TODO: it would be better to allow separate batch sizes for entity/relation prediction
evaluator = evaluator_resolver.make(evaluator, pos_kwargs=evaluator_kwargs)
super().__init__(
dataset=LCWAEvaluationDataset(
factory=triples_factory,
targets=targets,
filtered=evaluator.filtered or evaluator.requires_positive_mask,
additional_filter_triples=additional_filter_triples,
),
evaluator=evaluator,
**kwargs,
)
self.targets = targets
self.mode = mode
# docstr-coverage: inherited
def get_collator(self): # noqa: D102
return LCWAEvaluationDataset.collate
# docstr-coverage: inherited
def process_batch(self, batch: Mapping[Target, MappedTriples]) -> None: # noqa: D102
# note: most of the time, this loop will only make a single iteration, since the evaluation dataset typically is
# not shuffled, and contains evaluation ranking tasks sorted by target
for target, (hrt_batch, filter_batch) in batch.items():
# TODO: in theory, we could make a single score calculation for e.g.,
# {(h, r, t1), (h, r, t1), ..., (h, r, tk)}
# predict scores for all candidates
scores = self.model.predict(hrt_batch=hrt_batch, target=target, mode=self.mode)
true_scores = dense_positive_mask = None
# filter scores
if self.evaluator.filtered:
if filter_batch is None:
raise AssertionError("Filter indices are required to filter scores.")
# extract true scores
batch_ids = torch.arange(scores.shape[0], device=scores.device)
target_ids = hrt_batch[:, TARGET_TO_INDEX[target]]
true_scores = scores[batch_ids, target_ids, None]
# replace by nan
scores = filter_scores_(scores=scores, filter_batch=filter_batch)
# rewrite true scores
scores[batch_ids, target_ids] = true_scores[:, 0]
# create dense positive masks
# TODO: afaik, dense positive masks are not used on GPU -> we do not need to move the masks around
elif self.evaluator.requires_positive_mask:
if filter_batch is None:
raise AssertionError("Filter indices are required to create dense positive masks.")
dense_positive_mask = torch.zeros_like(scores, dtype=torch.bool, device=filter_batch.device)
dense_positive_mask[filter_batch[:, 0], filter_batch[:, 0]] = True
# delegate processing of scores to the evaluator
self.evaluator.process_scores_(
hrt_batch=hrt_batch,
target=target,
scores=scores,
true_scores=true_scores,
dense_positive_mask=dense_positive_mask,
)