-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathrun_training.py
204 lines (184 loc) · 6.9 KB
/
run_training.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
# MIT License
#
# Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES, University of Washington. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
from typing import Optional, Any, Dict, List
from pathlib import Path
import sys
import os
from datetime import timedelta
import pytorch_lightning as pl
from pytorch_lightning.loggers import WandbLogger
from pytorch_lightning.strategies import DDPStrategy
from pytorch_lightning.callbacks import ModelCheckpoint, Callback
from termcolor import colored
import argparse
import yaml
import uuid
PROJECT_ROOT = str(Path(__file__).resolve().parent.parent)
sys.path.insert(0, PROJECT_ROOT)
from mpinets.data_loader import DataModule
from mpinets.model import TrainingMotionPolicyNetwork
def setup_trainer(
gpus: int,
test: bool,
should_checkpoint: bool,
logger: Optional[WandbLogger],
checkpoint_interval: int,
checkpoint_dir: str,
validation_interval: float,
) -> pl.Trainer:
"""
Creates the Pytorch Lightning trainer object
:param gpus int: The number of GPUs (if more than 1, uses DDP)
:param test bool: Whether to use a test dataset
:param should_checkpoint bool: Whether to save checkpoints
:param logger Optional[WandbLogger]: The logger object, set to None if logging is disabled
:param checkpoint_interval int: The number of minutes between checkpoints
:param checkpoint_dir str: The directory in which to save checkpoints (a subdirectory will
be created according to the experiment ID)
:param validation_interval float: How often to run the validation step, either as a proportion
of the training epoch or as a number of batches
:rtype pl.Trainer: The trainer object
"""
args: Dict[str, Any] = {}
if test:
args = {**args, "limit_train_batches": 10, "limit_val_batches": 3}
validation_interval = 2 # Overwritten to be an appropriate size for test
if (isinstance(gpus, list) and len(gpus) > 1) or (
isinstance(gpus, int) and gpus > 1
):
args = {
**args,
"strategy": DDPStrategy(find_unused_parameters=False),
}
if validation_interval is not None:
args = {**args, "val_check_interval": validation_interval}
callbacks: List[Callback] = []
if logger is not None:
experiment_id = str(logger.experiment.id)
else:
experiment_id = str(uuid.uuid1())
if should_checkpoint:
if checkpoint_dir is not None:
dirpath = Path(checkpoint_dir).resolve() / experiment_id
else:
dirpath = PROJECT_ROOT / "checkpoints" / experiment_id
pl.utilities.rank_zero_info(f"Saving checkpoints to {dirpath}")
every_n_checkpoint = ModelCheckpoint(
monitor="val_loss",
save_last=True,
dirpath=dirpath,
train_time_interval=timedelta(minutes=checkpoint_interval),
)
epoch_end_checkpoint = ModelCheckpoint(
monitor="val_loss",
save_last=True,
dirpath=dirpath,
save_on_train_epoch_end=True,
)
epoch_end_checkpoint.CHECKPOINT_NAME_LAST = "epoch-{epoch}-end"
callbacks.extend([every_n_checkpoint, epoch_end_checkpoint])
trainer = pl.Trainer(
enable_checkpointing=should_checkpoint,
callbacks=callbacks,
max_epochs=1 if test else 500,
gradient_clip_val=1.0,
gpus=gpus,
precision=16,
logger=False if logger is None else logger,
**args,
)
return trainer
def setup_logger(
should_log: bool, experiment_name: str, config_values: Dict[str, Any]
) -> Optional[WandbLogger]:
if not should_log:
pl.utilities.rank_zero_info("Disabling all logs")
return None
logger = WandbLogger(
name=experiment_name,
project="mpinets",
log_model=True,
)
logger.log_hyperparams(config_values)
return logger
def parse_args_and_configuration():
"""
Checks the command line arguments and merges them with the configuration yaml file
"""
parser = argparse.ArgumentParser()
parser.add_argument("yaml_config", type=str)
parser.add_argument(
"--test",
action="store_true",
help="Test with only a few batches (disables logging)",
)
parser.add_argument(
"--no-logging", action="store_true", help="Don't log to weights and biases"
)
parser.add_argument(
"--no-checkpointing", action="store_true", help="Don't checkpoint"
)
args = parser.parse_args()
if args.test:
args.no_logging = True
with open(args.yaml_config) as f:
configuration = yaml.safe_load(f)
return {
"training_node_name": os.uname().nodename,
**configuration,
**vars(args),
}
def run():
"""
Runs the training procedure
"""
config = parse_args_and_configuration()
color_name = colored(config["experiment_name"], "green")
pl.utilities.rank_zero_info(f"Experiment name: {color_name}")
logger = setup_logger(
not config["no_logging"],
config["experiment_name"],
config,
)
trainer = setup_trainer(
config["gpus"],
config["test"],
should_checkpoint=not config["no_checkpointing"],
logger=logger,
checkpoint_interval=config["checkpoint_interval"],
checkpoint_dir=config["save_checkpoint_dir"],
validation_interval=config["validation_interval"],
)
dm = DataModule(
batch_size=config["batch_size"],
**(config["shared_parameters"] or {}),
**(config["data_module_parameters"] or {}),
)
mdl = TrainingMotionPolicyNetwork(
**(config["shared_parameters"] or {}),
**(config["training_model_parameters"] or {}),
)
if logger is not None:
logger.watch(mdl, log="gradients", log_freq=100)
trainer.fit(model=mdl, datamodule=dm)
if __name__ == "__main__":
run()