-
Notifications
You must be signed in to change notification settings - Fork 275
test: verify datastore is omitted from checkpoint hparams (#148) #232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sadamov
merged 1 commit into
mllam:main
from
Jayant-kernel:fix/148-datastore-checkpoint-regression
Jun 6, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # Standard library | ||
| from pathlib import Path | ||
|
|
||
| # Third-party | ||
| import pytorch_lightning as pl | ||
| import torch | ||
|
|
||
| # First-party | ||
| from neural_lam import config as nlconfig | ||
| from neural_lam.create_graph import create_graph_from_datastore | ||
| from neural_lam.models import ARForecaster, ForecasterModule, GraphLAM | ||
| from tests.dummy_datastore import DummyDatastore | ||
|
|
||
|
|
||
| def test_saved_checkpoint_excludes_datastore_and_forecaster(tmp_path): | ||
| """ | ||
| Regression check for issue #148: heavy non-pickle-safe objects | ||
| (`datastore`, `forecaster`) must be excluded from the saved Lightning | ||
| hyperparameters so that checkpoints stay small and portable, and so | ||
| that `load_from_checkpoint` requires them to be passed in explicitly. | ||
| """ | ||
| datastore = DummyDatastore() | ||
|
|
||
| # Build the minimum graph the GraphLAM predictor needs. | ||
| graph_dir_path = Path(datastore.root_path) / "graph" / "1level" | ||
| if not graph_dir_path.exists(): | ||
| create_graph_from_datastore( | ||
| datastore=datastore, | ||
| output_root_path=str(graph_dir_path), | ||
| n_max_levels=1, | ||
| ) | ||
|
|
||
| config = nlconfig.NeuralLAMConfig( | ||
| datastore=nlconfig.DatastoreSelection( | ||
| kind=datastore.SHORT_NAME, | ||
| config_path=datastore.root_path, | ||
| ), | ||
| ) | ||
|
|
||
| predictor = GraphLAM( | ||
| datastore=datastore, | ||
| graph_name="1level", | ||
| hidden_dim=4, | ||
| hidden_layers=1, | ||
| processor_layers=1, | ||
| mesh_aggr="sum", | ||
| num_past_forcing_steps=0, | ||
| num_future_forcing_steps=0, | ||
| output_std=False, | ||
| output_clamping_lower=config.training.output_clamping.lower, | ||
| output_clamping_upper=config.training.output_clamping.upper, | ||
| ) | ||
| forecaster = ARForecaster(predictor, datastore) | ||
| model = ForecasterModule( | ||
| forecaster=forecaster, | ||
| config=config, | ||
| datastore=datastore, | ||
| loss="mse", | ||
| lr=1.0e-3, | ||
| n_example_pred=1, | ||
| val_steps_to_log=[1], | ||
| ) | ||
|
|
||
| # Lightning's in-memory hparams must already drop these. | ||
| assert "datastore" not in model.hparams | ||
| assert "forecaster" not in model.hparams | ||
|
|
||
| # And the on-disk checkpoint round-trip must agree. | ||
| trainer = pl.Trainer( | ||
| default_root_dir=tmp_path, | ||
| accelerator="cpu", | ||
| max_epochs=0, | ||
| logger=False, | ||
| enable_checkpointing=False, | ||
| ) | ||
| trainer.strategy.connect(model) | ||
|
|
||
| ckpt_path = tmp_path / "test.ckpt" | ||
| trainer.save_checkpoint(ckpt_path, weights_only=False) | ||
|
|
||
| # In-process checkpoint, trusted source. | ||
| ckpt = torch.load(ckpt_path, map_location="cpu", weights_only=False) | ||
| assert "hyper_parameters" in ckpt | ||
| assert "datastore" not in ckpt["hyper_parameters"] | ||
| assert "forecaster" not in ckpt["hyper_parameters"] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.