Skip to content

Commit

Permalink
Merge branch 'main' into bugfix_data_config_yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
joeloskarsson committed May 29, 2024
2 parents 54c44cd + 879cfec commit 7774fcc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Robust restoration of optimizer and scheduler using `ckpt_path`
[\#17](https://github.com/mllam/neural-lam/pull/17)
@sadamov

- Updated scripts and modules to use `data_config.yaml` instead of `constants.py`
[\#31](https://github.com/joeloskarsson/neural-lam/pull/31)
@sadamov
Expand Down
10 changes: 5 additions & 5 deletions neural_lam/models/ar_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ def __init__(self, args):
if self.output_std:
self.test_metrics["output_std"] = [] # Treat as metric

# For making restoring of optimizer state optional (slight hack)
self.opt_state = None
# For making restoring of optimizer state optional
self.restore_opt = args.restore_opt

# For example plotting
self.n_example_pred = args.n_example_pred
Expand All @@ -97,9 +97,6 @@ def configure_optimizers(self):
opt = torch.optim.AdamW(
self.parameters(), lr=self.args.lr, betas=(0.9, 0.95)
)
if self.opt_state:
opt.load_state_dict(self.opt_state)

return opt

@property
Expand Down Expand Up @@ -597,3 +594,6 @@ def on_load_checkpoint(self, checkpoint):
)
loaded_state_dict[new_key] = loaded_state_dict[old_key]
del loaded_state_dict[old_key]
if not self.restore_opt:
opt = self.configure_optimizers()
checkpoint["optimizer_states"] = [opt.state_dict()]
12 changes: 3 additions & 9 deletions train_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,14 +268,7 @@ def main():

# Load model parameters Use new args for model
model_class = MODELS[args.model]
if args.load:
model = model_class.load_from_checkpoint(args.load, args=args)
if args.restore_opt:
# Save for later
# Unclear if this works for multi-GPU
model.opt_state = torch.load(args.load)["optimizer_states"][0]
else:
model = model_class(args)
model = model_class(args)

prefix = "subset-" if args.subset_ds else ""
if args.eval:
Expand Down Expand Up @@ -330,13 +323,14 @@ def main():
)

print(f"Running evaluation on {args.eval}")
trainer.test(model=model, dataloaders=eval_loader)
trainer.test(model=model, dataloaders=eval_loader, ckpt_path=args.load)
else:
# Train model
trainer.fit(
model=model,
train_dataloaders=train_loader,
val_dataloaders=val_loader,
ckpt_path=args.load,
)


Expand Down

0 comments on commit 7774fcc

Please sign in to comment.