-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
338 lines (295 loc) · 9.51 KB
/
main.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
# -*- coding: utf-8 -*-
"""Main script."""
import getpass
import inspect
import logging
import math
import pathlib
from itertools import chain, product
from typing import Iterable, List, Optional, Type
import click
import pandas as pd
import pykeen
import seaborn as sns
import torch.cuda
from docdata import get_docdata
from more_click import force_option, verbose_option
from pykeen.datasets import Dataset, dataset_resolver, get_dataset
from pykeen.losses import loss_resolver
from pykeen.models import model_resolver
from pykeen.sampling import (
NegativeSampler,
PseudoTypedNegativeSampler,
negative_sampler_resolver,
)
from pykeen.sampling.filtering import BloomFilterer, Filterer, filterer_resolver
from pykeen.training import LCWATrainingLoop, NonFiniteLossError, SLCWATrainingLoop
from pykeen.utils import resolve_device
from torch.optim import Adam
from torch.utils.benchmark import Timer
from tqdm import tqdm
from tqdm.contrib.logging import logging_redirect_tqdm
logger = logging.getLogger(__name__)
VERSION = pykeen.get_version()
HERE = pathlib.Path(__file__).resolve().parent
DATA = HERE.joinpath("data", getpass.getuser())
#: Columns in each dataset-specific file
COLUMNS = [
"trainer",
"loss",
"sampler",
"filterer",
"num_negs_per_pos",
"workers",
"epochs",
"time",
"frequency",
]
@click.command()
@click.option("--num-epochs", type=int, default=3, show_default=True)
@click.option("--dataset")
@click.option("--top", type=int, default=2)
@verbose_option
@force_option
@click.option("-b", "--branch-name", type=str, default=None)
def main(
dataset: Optional[str],
num_epochs: int,
top: Optional[int],
force: bool,
branch_name: str,
) -> None:
"""Run the benchmark.
Things to measure:
- time
- memory consumption (secondary)
Things to benchmark across datasets/models
1. LCWA
2. sLCWA
- filtered/unfiltered
- basic/bernoulli/pseudotyped
"""
dfs = []
device = resolve_device()
# branch_name
output_directory = DATA.joinpath(
branch_name or pykeen.get_git_branch(), pykeen.get_git_hash()
)
output_directory.mkdir(exist_ok=True, parents=True)
for dataset_instance in _iterate_datasets(dataset, top=top):
with logging_redirect_tqdm():
df = _generate(
dataset=dataset_instance,
device=device,
num_epochs=num_epochs,
force=force,
output_directory=output_directory,
)
df_columns = df.columns
df["dataset"] = dataset_instance.get_normalized_name()
df = df[["dataset", *df_columns]]
dfs.append(df)
sdf = pd.concat(dfs)
sdf.to_csv(output_directory.joinpath("results.tsv.gz"), sep="\t", index=False)
g = plot(sdf)
g.fig.savefig(output_directory.joinpath("output.svg"))
g.fig.savefig(output_directory.joinpath("output.png"), dpi=300)
def _generate(
*,
dataset: Dataset,
num_epochs: int,
device,
force: bool = False,
output_directory: pathlib.Path,
) -> pd.DataFrame:
path = output_directory.joinpath(dataset.get_normalized_name()).with_suffix(".tsv")
if path.is_file() and not force:
return pd.read_csv(path, sep="\t")
data = []
loss = loss_resolver.make("marginranking")
it = _keys(dataset=dataset)
for i, (
loop_cls,
negative_sampler_cls,
filterer_cls,
num_negs_per_pos,
num_workers,
) in enumerate(it):
model = model_resolver.make(
"distmult",
triples_factory=dataset.training,
random_seed=i,
loss=loss,
).to(device)
optimizer = Adam(model.parameters())
it.set_postfix(
loop=loop_cls.get_normalized_name(),
loss=loss_resolver.normalize_inst(loss),
sampler=negative_sampler_cls and negative_sampler_cls.get_normalized_name(),
filterer=filterer_cls and filterer_resolver.normalize_cls(filterer_cls),
num_negs_per_pos=num_negs_per_pos,
num_workers=num_workers,
)
if loop_cls is SLCWATrainingLoop:
if any(
"mapped_triples" in negative_sampler_resolver.signature(cls).parameters
for cls in inspect.getmro(negative_sampler_cls)
if issubclass(cls, NegativeSampler)
):
kwargs = dict(
mapped_triples=dataset.training.mapped_triples,
num_entities=dataset.num_entities,
num_relations=dataset.num_relations,
)
else:
kwargs = dict(triples_factory=dataset.training)
negative_sampler = negative_sampler_resolver.make(
negative_sampler_cls,
num_negs_per_pos=num_negs_per_pos,
filterer=filterer_cls,
**kwargs,
)
trainer = SLCWATrainingLoop(
model=model,
optimizer=optimizer,
triples_factory=dataset.training,
negative_sampler=negative_sampler,
automatic_memory_optimization=False,
)
else:
trainer = LCWATrainingLoop(
model=model,
optimizer=optimizer,
triples_factory=dataset.training,
automatic_memory_optimization=False,
)
timer = Timer(
stmt="""\
trainer.train(
triples_factory=triples_factory,
num_epochs=num_epochs,
use_tqdm=False,
batch_size=batch_size,
num_workers=num_workers,
)
""",
globals=dict(
trainer=trainer,
triples_factory=dataset.training,
num_epochs=num_epochs,
batch_size=512,
num_workers=num_workers,
),
)
try:
measurement = timer.blocked_autorange()
except NonFiniteLossError as e:
tqdm.write(f"error: {e}")
continue
data.extend(
(
loop_cls.get_normalized_name(),
loss_resolver.normalize_inst(loss),
(
negative_sampler_cls.get_normalized_name()
if negative_sampler_cls
else "none"
),
(
filterer_resolver.normalize_cls(filterer_cls)
if filterer_cls
else "none"
),
num_negs_per_pos,
num_workers,
num_epochs,
t,
num_epochs / t,
)
for t in measurement.raw_times
)
df = pd.DataFrame(data, columns=COLUMNS)
df.to_csv(path, sep="\t", index=False)
return df
def _get_filterer() -> List[Optional[Type[Filterer]]]:
# bloom filter fails on GPU
if torch.cuda.is_available():
return [None]
return [None, BloomFilterer]
def _get_samplers() -> List[NegativeSampler]:
# pseudo-type sampler fails on GPU
res = list(negative_sampler_resolver)
if torch.cuda.is_available():
res.remove(PseudoTypedNegativeSampler)
return res
def _keys(dataset: Dataset):
workers = [0, 2, 8]
num_negs_per_pos_values = [10 ** i for i in range(2)] # just 1 and 10 for now
slcwa_keys = (
[SLCWATrainingLoop],
_get_samplers(),
_get_filterer(),
num_negs_per_pos_values,
workers,
)
lcwa_keys = ([LCWATrainingLoop], [None], [None], [0], workers)
all_keys = (lcwa_keys, slcwa_keys)
it = tqdm(
chain.from_iterable(product(*keys) for keys in all_keys),
desc=dataset.get_normalized_name(),
total=sum(math.prod(len(k) for k in keys) for keys in all_keys),
)
return it
def _make_label(trainer, sampler, filterer):
if trainer == "lcwa":
return trainer
if not filterer or pd.isnull(filterer) or pd.isna(filterer):
filterer = "unfiltered"
return f"{trainer}/{sampler}/{filterer}"
def add_group_label(df, key):
df[key] = [
_make_label(trainer, sampler, filterer)
for trainer, sampler, filterer in df[["trainer", "sampler", "filterer"]].values
]
def plot(df: pd.DataFrame, row=None):
hue_key = "hue"
add_group_label(df, hue_key)
g = sns.relplot(
data=df,
x="num_negs_per_pos",
y="frequency",
hue=hue_key,
kind="line",
col="dataset",
row=row,
height=3.5,
# ci=100,
# estimator=numpy.median,
)
g.set(
xscale="log",
yscale="log",
# xlabel="Batch Size",
ylabel="Epochs per Second",
)
g.tight_layout()
return g
def _iterate_datasets(dataset: Optional[str], top=None) -> Iterable[Dataset]:
if dataset:
_dataset_list = [dataset]
else:
_dataset_list = sorted(dataset_resolver.lookup_dict, key=_triples)
if top:
_dataset_list = _dataset_list[:top]
it = tqdm(_dataset_list, desc="Dataset")
for dataset in it:
it.set_postfix(dataset=dataset)
try:
yield get_dataset(dataset=dataset)
except ValueError as error: # CN3l raises ValueError:
logger.error(f"Could not load {dataset} due to {error}")
continue
def _triples(d: str) -> int:
return get_docdata(dataset_resolver.lookup_dict[d])["statistics"]["triples"]
if __name__ == "__main__":
main()