-
Notifications
You must be signed in to change notification settings - Fork 3
/
run.py
92 lines (81 loc) · 2.45 KB
/
run.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
import os
import copy
from datetime import datetime
from pathlib import Path
import pytorch_lightning as pl
from pytorch_lightning.loggers import WandbLogger
from pytorch_lightning.callbacks import (
ModelCheckpoint,
LearningRateMonitor,
EarlyStopping,
)
import wandb
from chemeleon.config import ex
from chemeleon.datamodule import DataModule
from chemeleon.modules.chemeleon import Chemeleon
@ex.automain
def main(_config):
_config = copy.deepcopy(_config)
if _config["sweep"]:
wandb.init()
_config.update(wandb.config)
pl.seed_everything(_config["seed"])
project_name = _config["project_name"]
current_time = datetime.now().strftime("%Y-%m-%d")
exp_name = (
f"test_{_config['exp_name']}_{current_time}"
if _config["test_only"]
else f"{_config['exp_name']}_{current_time}"
)
log_dir = Path(_config["log_dir"], _config["dataset_name"])
os.environ["WANDB_DIR"] = str(log_dir)
offline = _config["offline"]
# set datamodule
dm = DataModule(_config)
# set model
module = Chemeleon(_config)
print(module)
# set checkpoint callback
checkpoint_callback = ModelCheckpoint(
monitor="val/loss",
save_top_k=1,
save_last=True,
mode="min",
filename="best-{epoch}",
)
lr_callback = LearningRateMonitor(logging_interval="step")
early_stop_callback = EarlyStopping(
monitor="val/loss",
patience=_config["early_stopping"],
verbose=True,
mode="min",
)
callbacks = [
checkpoint_callback,
lr_callback,
early_stop_callback,
]
# set logger
logger = WandbLogger(
project=project_name,
name=exp_name,
offline=offline,
save_dir=log_dir,
log_model=True if not offline else False,
group=(f"{_config['group_name']}"),
)
# set trainer
trainer = pl.Trainer(
num_nodes=_config["num_nodes"],
devices=_config["devices"],
accelerator=_config["accelerator"],
max_epochs=_config["max_epochs"],
strategy="ddp_find_unused_parameters_true",
deterministic=_config["deterministic"],
gradient_clip_val=_config["gradient_clip_val"],
limit_test_batches=_config["limit_test_batches"],
accumulate_grad_batches=_config["accumulate_grad_batches"],
callbacks=callbacks,
logger=logger,
)
trainer.fit(module, dm, ckpt_path=_config["resume_from"])