-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
315 lines (235 loc) · 7.17 KB
/
config.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
import copy
import importlib
from dataclasses import dataclass
from functools import partial
from pathlib import Path
from typing import Any, Optional
import omegaconf
from hydra.core.config_store import ConfigStore
from omegaconf import MISSING, OmegaConf
###############################################
# SECTION: AUXILIARY DEFINITIONS
###############################################
@dataclass
class SafeCallable:
callable: Any
_target_: str = f"{__name__}.SafeCallable"
def __call__(self, *args: Any, **kwds: Any) -> Any:
return self.callable(*args, **kwds)
def __str__(self):
return str(self.callable.__name__)
def fqn_import(fqn: str, *args):
module_name, _, function_name = fqn.rpartition(".")
fn = getattr(importlib.import_module(module_name), function_name)
if len(args) != 0:
fn = partial(fn, *args)
return SafeCallable(fn)
CALLABLE_RESOLVER = "callable"
OmegaConf.register_new_resolver(CALLABLE_RESOLVER, fqn_import)
###############################################
# SECTION: OPTIMIZERS
###############################################
@dataclass
class OptimizerConfig:
_target_: str
lr: float = 0.001
@dataclass
class AdamConfig(OptimizerConfig):
_target_: str = "torch.optim.Adam"
@dataclass
class SGDConfig(OptimizerConfig):
_target_: str = "torch.optim.SGD"
@dataclass
class RMSpropConfig(OptimizerConfig):
_target_: str = "torch.optim.RMSprop"
###############################################
# SECTION: SCHEDULERS
###############################################
@dataclass
class SchedulerConfig:
_target_: str
@dataclass
class StepLRConfig(SchedulerConfig):
_target_: str = "torch.optim.lr_scheduler.StepLR"
step_size: int = 50
gamma: float = 0.5
@dataclass
class MultiStepLRConfig(SchedulerConfig):
_target_: str = "torch.optim.lr_scheduler.MultiStepLR"
milestones: Any = MISSING
gamma: float = 0.5
@dataclass
class ConstantLRConfig(SchedulerConfig):
_target_: str = "torch.optim.lr_scheduler.ConstantLR"
factor: int = 1
@dataclass
class ReduceLROnPlateauConfig(SchedulerConfig):
_target_: str = "torch.optim.lr_scheduler.ReduceLROnPlateau"
mode: str = "min"
factor: float = 0.5
patience: int = 40
@dataclass
class LambdaLRConfig(SchedulerConfig):
_target_: str = "data_utils.get_linear_schedule_with_warmup"
num_warmup_epochs: int = 10
num_training_steps: Optional[int] = None
last_epoch: int = -1
###############################################
# SECTION: SCHED AND OPTIM BASE CLASS
###############################################
@dataclass
class OptimConfig:
optimizer: OptimizerConfig
scheduler: SchedulerConfig
num_epochs: int
###############################################
# SECTION: FEATURE ENCODERS
###############################################
@dataclass
class FeatureEncoderConfig:
_target_: str
@dataclass
class Identity(FeatureEncoderConfig):
k: int = MISSING
in_dim: int = MISSING
emb_dim: int = MISSING
_target_: str = "feature_encoders.Identity"
@dataclass
class ZincAtomEncoder(FeatureEncoderConfig):
k: int = MISSING
in_dim: int = MISSING
emb_dim: int = MISSING
_target_: str = "feature_encoders.ZincAtomEncoder"
@dataclass
class MyAtomEncoder(FeatureEncoderConfig):
k: int = MISSING
in_dim: int = MISSING
emb_dim: int = MISSING
_target_: str = "feature_encoders.MyAtomEncoder"
@dataclass
class LinearEncoder(FeatureEncoderConfig):
k: int = MISSING
in_dim: int = MISSING
emb_dim: int = MISSING
_target_: str = "feature_encoders.LinearEncoder"
@dataclass
###############################################
# SECTION: DS
###############################################
@dataclass
class DSNetworkArgs:
_target_: str = "builtins.dict"
# Task defined options
num_tasks: Optional[int] = MISSING
final_reductions: Any = MISSING
return_x: bool = False
@dataclass
class DSNetworkBackbone:
_target_: str = "models.DSnetworkBackbone"
# Task defined options
in_dim: int = MISSING
feature_encoder: Any = MISSING
# Decent defaults
num_layers: int = 6
emb_dim: int = 128
GNNConv: SafeCallable = f"${{{CALLABLE_RESOLVER}:torch_geometric.nn.GraphConv}}"
layernorm: bool = False
add_residual: bool = False
track_running_stats: bool = True
model_drop_ratio: float = 0.0
@dataclass
class GNNNetworkBackbone:
_target_: str = "models.GNNnetworkBackbone"
# Task defined options
in_dim: int = MISSING
feature_encoder: Any = MISSING
# Decent defaults
num_layers: int = 6
emb_dim: int = 128
GNNConv: SafeCallable = f"${{{CALLABLE_RESOLVER}:torch_geometric.nn.GraphConv}}"
layernorm: bool = False
add_residual: bool = False
track_running_stats: bool = True
@dataclass
class DSNetworkConfig(DSNetworkArgs):
_target_: str = "models.DSnetwork"
@dataclass
class GNNNetworkConfig:
_target_: str = "models.GNNnetwork"
# Task defined options
in_dim: int = MISSING
feature_encoder: Any = MISSING
# Decent defaults
num_layers: int = 6
emb_dim: int = 128
GNNConv: SafeCallable = f"${{{CALLABLE_RESOLVER}:torch_geometric.nn.GraphConv}}"
layernorm: bool = False
add_residual: bool = False
track_running_stats: bool = True
@dataclass
class TaskConfig:
dataroot: Path
dataset_name: str
num_tasks: int
num_hops: Optional[int]
in_dim: int
model_feature_encoder: FeatureEncoderConfig
selection_feature_encoder: FeatureEncoderConfig
metric: SafeCallable
loss: SafeCallable
@dataclass
class ShouldCheckpoint:
init: float
is_better: SafeCallable
@dataclass
class TrainConfig:
task: TaskConfig
model_backbone: DSNetworkBackbone
model: DSNetworkConfig
selection_backbone: DSNetworkBackbone
selection_model: DSNetworkConfig
opt: OptimConfig
selection_opt: OptimConfig
should_checkpoint: ShouldCheckpoint
batch_size: int
selection_type: str
num_marked: int
num_subgraphs: Optional[int]
gumbel_softmax_tau: float = 2.0 / 3.0
drop_ratio: float = 0.0
seed: int = 1337
device: str = "cuda"
data_device: str = "cuda"
split: int = 0
cs = ConfigStore.instance()
for node in [
SGDConfig,
RMSpropConfig,
AdamConfig,
StepLRConfig,
MultiStepLRConfig,
ConstantLRConfig,
ReduceLROnPlateauConfig,
LambdaLRConfig,
]:
cs.store(group="optim", name=node.__name__, node=node)
for node in [
DSNetworkArgs,
DSNetworkConfig,
DSNetworkBackbone,
GNNNetworkConfig,
GNNNetworkBackbone,
]:
cs.store(group="models", name=node.__name__, node=node)
for node in [Identity, ZincAtomEncoder, MyAtomEncoder, LinearEncoder]:
cs.store(group="feature_encoders", name=node.__name__, node=node)
cs.store(group="task", name="TaskConfig", node=TaskConfig)
cs.store(name="TrainConfig", node=TrainConfig)
def resolve(config: omegaconf.dictconfig.DictConfig) -> omegaconf.dictconfig.DictConfig:
config_copy = copy.deepcopy(config)
config_copy._set_flag(
flags=["allow_objects", "struct", "readonly"], values=[True, False, False]
)
config_copy._set_parent(config._get_parent())
OmegaConf.resolve(config_copy)
return config_copy