-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodel_service.py
More file actions
276 lines (245 loc) · 10.5 KB
/
model_service.py
File metadata and controls
276 lines (245 loc) · 10.5 KB
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
import logging
from collections.abc import Iterable
from pathlib import Path, PosixPath
from typing import TYPE_CHECKING, cast
import hydra
import torch
from lightning import Callback, Trainer
from lightning.fabric.utilities import suggested_max_num_workers
from lightning.pytorch.callbacks import ModelCheckpoint
from omegaconf import DictConfig, OmegaConf
from wandb.sdk.lib.runid import generate_id
from icenet_mp.callbacks import UnconditionalCheckpoint
from icenet_mp.data_loaders import CommonDataModule
from icenet_mp.models.base_model import BaseModel
from icenet_mp.types import SupportsMetadata
from icenet_mp.utils import get_device_name, get_timestamp, get_wandb_run
if TYPE_CHECKING:
from lightning.pytorch.loggers import Logger as LightningLogger
logger = logging.getLogger(__name__)
class ModelService:
def __init__(self, config: DictConfig) -> None:
"""Initialize the model service."""
self.config_ = config
self.data_module_: CommonDataModule | None = None
self.model_: BaseModel | None = None
self.trainer_: Trainer | None = None
self.extra_callbacks_: list[Callback] = []
self.extra_loggers_: list[LightningLogger] = []
self.run_directory_: Path | None = None
@classmethod
def from_config(cls, config: DictConfig) -> "ModelService":
"""Build a new ModelService by loading a model from a configuration file."""
# Load the model configuration
builder = cls(config)
# Construct the model
builder.model_ = hydra.utils.instantiate(
dict(
{
"hemisphere": builder.data_module.hemisphere,
"input_spaces": [
s.to_dict() for s in builder.data_module.input_spaces
],
"n_forecast_steps": builder.data_module.n_forecast_steps,
"n_history_steps": builder.data_module.n_history_steps,
"output_space": builder.data_module.output_space.to_dict(),
"optimizer": config["train"]["optimizer"],
"scheduler": config["train"]["scheduler"],
},
**config["model"],
),
_recursive_=False,
_convert_="object",
)
# Return the builder
return builder
@classmethod
def from_checkpoint(
cls, config: DictConfig, checkpoint_path: Path
) -> "ModelService":
"""Build a new ModelService by loading a model from a checkpoint."""
# Verify the checkpoint path
if checkpoint_path.is_file():
logger.debug("Found checkpoint at %s.", checkpoint_path)
else:
msg = f"Checkpoint file {checkpoint_path} does not exist."
raise FileNotFoundError(msg)
# Build a combined model configuration where the command line config takes
# precedence except for the "model", "predict" and "train" keys which are
# related to training the model.
config_path = checkpoint_path.parent.parent / "files" / "model_config.yaml"
try:
# Load the model configuration from the checkpoint directory
ckpt_config = DictConfig(OmegaConf.load(config_path))
logger.debug("Loaded checkpoint configuration from %s.", config_path)
combined_cfg = DictConfig(OmegaConf.merge(ckpt_config, config))
for key in ("model", "predict", "train"):
combined_cfg[key] = OmegaConf.merge(
combined_cfg.get(key, {}), ckpt_config.get(key, {})
)
except (NotADirectoryError, FileNotFoundError):
combined_cfg = config
logger.debug(
"Could not load checkpoint configuration from %s.", config_path
)
# Load the model from checkpoint
builder = cls(combined_cfg)
model_cls: type[BaseModel] = hydra.utils.get_class(
builder.config["model"]["_target_"]
)
with torch.serialization.safe_globals([PosixPath]):
builder.model_ = model_cls.load_from_checkpoint(
checkpoint_path=checkpoint_path
)
return builder
@property
def config(self) -> DictConfig:
"""Get the model configuration."""
if not self.config_:
msg = "Model config has not been initialised."
raise AttributeError(msg)
return self.config_
@property
def data_module(self) -> CommonDataModule:
"""Get the data module instance."""
if not self.data_module_:
self.data_module_ = CommonDataModule(self.config)
return self.data_module_
@property
def model(self) -> BaseModel:
"""Get the model instance."""
if not self.model_:
msg = "Model has not been initialised."
raise AttributeError(msg)
return self.model_
@property
def run_directory(self) -> Path:
"""Get run directory from Wandb or generate one in the same format."""
if not self.run_directory_:
# Get the run directory from Wandb if it exists
wandb_run = get_wandb_run(self.trainer)
if wandb_run:
self.run_directory_ = Path(wandb_run._settings.sync_dir)
# Otherwise generate a new run directory
if not self.run_directory_:
self.run_directory_ = (
self.data_module.base_path
/ "training"
/ "local"
/ f"run-{get_timestamp()}-{generate_id()}"
)
# Ensure the run directory exists
logger.debug("Set run directory to %s.", self.run_directory_)
self.run_directory_.mkdir(parents=True, exist_ok=True)
return self.run_directory_
@property
def trainer(self) -> Trainer:
"""Create a new Trainer or return the existing one."""
if not self.trainer_:
# Create a new Trainer
logger.debug("Instantiating lightning trainer.")
self.trainer_ = cast(
"Trainer",
hydra.utils.instantiate(
dict(
{
"callbacks": self.extra_callbacks_,
"logger": self.extra_loggers_,
},
**self.config["train"]["trainer"],
)
),
)
# Assign workers for data loading
self.data_module.assign_workers(
suggested_max_num_workers(self.trainer_.num_devices)
)
return self.trainer_
def add_callbacks(self, callback_configs: Iterable[DictConfig]) -> None:
"""Add extra lightning callbacks."""
self.extra_callbacks_ += [
hydra.utils.instantiate(callback_config)
for callback_config in callback_configs
]
def add_loggers(self, overrides: dict[str, str]) -> None:
"""Add extra lightning loggers."""
self.extra_loggers_ += [
hydra.utils.instantiate(dict(**logger_config) | overrides)
for logger_config in self.config.get("loggers", {}).values()
]
def configure_trainer(
self,
*,
job_type: str,
) -> None:
"""Configure the trainer with callbacks and loggers."""
# Setup callbacks first
callback_configs = self.config.get(job_type, {}).get("callbacks", {}).values()
self.add_callbacks(callback_configs)
if not self.extra_callbacks_:
logger.warning("No callbacks have been set for the trainer.")
# Setup lightning loggers
logger_overrides = {
"job_type": job_type,
"project": job_type,
}
self.add_loggers(logger_overrides)
if not self.extra_loggers_:
logger.warning("No loggers have been set for the trainer.")
# Additional configuration for callbacks
for callback in cast("list[Callback]", self.trainer.callbacks): # type: ignore[attr-defined]
logger.debug("Configuring callback %s.", callback.__class__.__name__)
# Set metadata for supported callbacks
if isinstance(callback, SupportsMetadata):
logger.debug("Setting metadata for %s.", callback.__class__.__name__)
callback.set_metadata(self.config, self.model.__class__.__name__)
# Set checkpoint run directory for supported callbacks
if isinstance(callback, (ModelCheckpoint, UnconditionalCheckpoint)):
logger.debug(
"Setting run_directory for %s to %s.",
callback.__class__.__name__,
self.run_directory / "checkpoints",
)
callback.dirpath = self.run_directory / "checkpoints"
# Save model config to the run directory
model_config_path = self.run_directory / "files" / "model_config.yaml"
if self.trainer.is_global_zero:
model_config_path.parent.mkdir(parents=True, exist_ok=True)
OmegaConf.save(self.config, model_config_path)
if wandb_run := get_wandb_run(self.trainer):
wandb_run.save(model_config_path, base_path=model_config_path.parent)
def evaluate(self) -> None:
"""Evaluate a trained model."""
# Configure the trainer with evaluation callbacks and loggers
logger.info("Configuring model for evaluation.")
self.configure_trainer(job_type="evaluate")
# Log evaluation details
logger.info(
"Starting evaluation using %d threads across %d %s device(s).",
torch.get_num_threads(),
self.trainer.num_devices,
get_device_name(self.trainer.accelerator.name()),
)
# Evaluate the model
self.trainer.test(
model=self.model,
datamodule=self.data_module,
)
def train(self) -> None:
"""Train a model."""
# Configure the trainer with training callbacks and loggers
logger.info("Configuring model for training.")
self.configure_trainer(job_type="train")
# Log training details
logger.info(
"Starting training for %d epochs using %d threads across %d %s device(s).",
self.trainer.max_epochs,
torch.get_num_threads(),
self.trainer.num_devices,
get_device_name(self.trainer.accelerator.name()),
)
# Train the model
self.trainer.fit(
model=self.model,
datamodule=self.data_module,
)