-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
builders.py
193 lines (169 loc) 路 5.96 KB
/
builders.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
# -*- coding: utf-8 -*-
"""Functions for building magical KGE model CLIs."""
import inspect
import json
import logging
import sys
from typing import Optional, Type, Union
import click
from torch import nn
from . import options
from .options import CLI_OPTIONS
from ..base import Model
from ...regularizers import Regularizer, _REGULARIZER_SUFFIX, regularizers
from ...utils import normalize_string
__all__ = [
'build_cli_from_cls',
]
logger = logging.getLogger(__name__)
_OPTIONAL_MAP = {Optional[int]: int, Optional[str]: str}
_SKIP_ARGS = {
'return',
'triples_factory',
'preferred_device',
'regularizer',
}
_SKIP_ANNOTATIONS = {
Optional[nn.Embedding],
Optional[nn.Parameter],
Optional[nn.Module],
}
def build_cli_from_cls(model: Type[Model]) -> click.Command: # noqa: D202
"""Build a :mod:`click` command line interface for a KGE model.
Allows users to specify all of the (hyper)parameters to the
model via command line options using :class:`click.Option`.
"""
signature = inspect.signature(model.__init__)
def _decorate_model_kwargs(command: click.Command) -> click.Command:
for name, annotation in model.__init__.__annotations__.items():
if name in _SKIP_ARGS or annotation in _SKIP_ANNOTATIONS:
continue
if annotation == Union[None, str, Regularizer]: # a model that has preset regularization
parameter = signature.parameters[name]
option = click.option(
'--regularizer',
type=str,
default=parameter.default,
show_default=True,
help=f'The name of the regularizer preset for {model.__name__}',
)
elif name in CLI_OPTIONS:
option = CLI_OPTIONS[name]
elif annotation in {Optional[int], Optional[str]}:
option = click.option(f'--{name.replace("_", "-")}', type=_OPTIONAL_MAP[annotation])
else:
parameter = signature.parameters[name]
if parameter.default is None:
logger.warning(
f'Missing handler in {model.__name__} for {name}: '
f'type={annotation} default={parameter.default}',
)
continue
option = click.option(f'--{name.replace("_", "-")}', type=annotation, default=parameter.default)
try:
command = option(command)
except AttributeError:
logger.warning(f'Unable to handle parameter in {model.__name__}: {name}')
continue
return command
regularizer_option = click.option(
'--regularizer',
type=click.Choice(regularizers),
help=f'The name of the regularizer. Defaults to'
f' {normalize_string(model.regularizer_default.__name__, suffix=_REGULARIZER_SUFFIX)}',
)
@click.command(help=f'CLI for {model.__name__}', name=model.__name__.lower())
@options.device_option
@options.dataset_option
@options.training_option
@options.testing_option
@options.valiadation_option
@options.optimizer_option
@regularizer_option
@options.training_loop_option
@options.automatic_memory_optimization_option
@options.number_epochs_option
@options.batch_size_option
@options.learning_rate_option
@options.evaluator_option
@options.stopper_option
@options.mlflow_uri_option
@options.title_option
@options.num_workers_option
@options.random_seed_option
@_decorate_model_kwargs
@click.option('--silent', is_flag=True)
@click.option('--output', type=click.File('w'), default=sys.stdout, help='Where to dump the metric results')
def main(
*,
device,
training_loop,
optimizer,
regularizer,
number_epochs,
batch_size,
learning_rate,
evaluator,
stopper,
output,
mlflow_tracking_uri,
title,
dataset,
automatic_memory_optimization,
training_triples_factory,
testing_triples_factory,
validation_triples_factory,
num_workers,
random_seed,
silent: bool,
**model_kwargs,
):
"""CLI for PyKEEN."""
click.echo(
f'Training {model.__name__} with '
f'{training_loop.__name__[:-len("TrainingLoop")]} using '
f'{optimizer.__name__} and {evaluator.__name__}',
)
from ...pipeline import pipeline
if mlflow_tracking_uri:
result_tracker = 'mlflow'
result_tracker_kwargs = {
'tracking_uri': mlflow_tracking_uri,
}
else:
result_tracker = None
result_tracker_kwargs = None
pipeline_result = pipeline(
device=device,
model=model,
model_kwargs=model_kwargs,
regularizer=regularizer,
dataset=dataset,
training=training_triples_factory,
testing=testing_triples_factory or training_triples_factory,
validation=validation_triples_factory,
optimizer=optimizer,
optimizer_kwargs=dict(
lr=learning_rate,
),
training_loop=training_loop,
evaluator=evaluator,
training_kwargs=dict(
num_epochs=number_epochs,
batch_size=batch_size,
num_workers=num_workers,
),
stopper=stopper,
result_tracker=result_tracker,
result_tracker_kwargs=result_tracker_kwargs,
metadata=dict(
title=title,
),
random_seed=random_seed,
automatic_memory_optimization=automatic_memory_optimization,
)
if not silent:
json.dump(pipeline_result.metric_results.to_dict(), output, indent=2)
click.echo('')
return sys.exit(0)
return main