Skip to content
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

Fix LightningCLI failing when both module and data module save hyperparameters #20221

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/lightning/pytorch/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).


## [unreleased] - YYYY-MM-DD

### Added

### Changed

- Merging of hparams when logging now ignores parameter names that begin with underscore `_` ([#20221](https://github.com/Lightning-AI/pytorch-lightning/pull/20221))

### Removed

### Fixed

- Fix LightningCLI failing when both module and data module save hyperparameters due to conflicting internal `_class_path` parameter ([#20221](https://github.com/Lightning-AI/pytorch-lightning/pull/20221))


## [2.4.0] - 2024-08-06

### Added
Expand Down
3 changes: 3 additions & 0 deletions src/lightning/pytorch/loggers/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ def _log_hyperparams(trainer: "pl.Trainer") -> None:
lightning_hparams = pl_module.hparams_initial
inconsistent_keys = []
for key in lightning_hparams.keys() & datamodule_hparams.keys():
if key.startswith("_"):
# Prevent merging LightningCLI's internal hparams and discourage of private param names
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we throw a warning here when a private param is skipped instead of doing it silently?

continue
lm_val, dm_val = lightning_hparams[key], datamodule_hparams[key]
if (
type(lm_val) != type(dm_val)
Expand Down
23 changes: 23 additions & 0 deletions tests/tests_pytorch/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,29 @@ def test_lightning_cli_save_hyperparameters_untyped_module(cleandir):
assert model.kwargs == {"x": 1}


class TestDataSaveHparams(BoringDataModule):
def __init__(self, batch_size: int = 32, num_workers: int = 4):
super().__init__()
self.save_hyperparameters()
self.batch_size = batch_size
self.num_workers = num_workers


def test_lightning_cli_save_hyperparameters_merge(cleandir):
config = {
"model": {
"class_path": f"{__name__}.TestModelSaveHparams",
},
"data": {
"class_path": f"{__name__}.TestDataSaveHparams",
},
}
with mock.patch("sys.argv", ["any.py", "fit", f"--config={json.dumps(config)}", "--trainer.max_epochs=1"]):
cli = LightningCLI(auto_configure_optimizers=False)
assert set(cli.model.hparams) == {"optimizer", "scheduler", "activation", "_instantiator", "_class_path"}
assert set(cli.datamodule.hparams) == {"batch_size", "num_workers", "_instantiator", "_class_path"}


@pytest.mark.parametrize("fn", [fn.value for fn in TrainerFn])
def test_lightning_cli_trainer_fn(fn):
class TestCLI(LightningCLI):
Expand Down
Loading