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

[CLI] Save only the configuration used #11532

Merged
merged 4 commits into from
Jan 20, 2022
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed wrong typehint for `Trainer.lightning_optimizers` ([#11155](https://github.com/PyTorchLightning/pytorch-lightning/pull/11155))


- Fixed the format of the configuration saved automatically by the CLI's `SaveConfigCallback` ([#11532](https://github.com/PyTorchLightning/pytorch-lightning/pull/11532))


- Fixed type promotion when tensors of higher category than float are logged ([#11401](https://github.com/PyTorchLightning/pytorch-lightning/pull/11401))


Expand Down
8 changes: 3 additions & 5 deletions pytorch_lightning/utilities/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,8 +699,8 @@ def _instantiate_trainer(self, config: Dict[str, Any], callbacks: List[Callback]
config["callbacks"].append(self.trainer_defaults["callbacks"])
if self.save_config_callback and not config["fast_dev_run"]:
config_callback = self.save_config_callback(
self.parser,
self.config,
self._parser(self.subcommand),
self.config.get(str(self.subcommand), self.config),
self.save_config_filename,
overwrite=self.save_config_overwrite,
multifile=self.save_config_multifile,
Expand Down Expand Up @@ -798,9 +798,7 @@ def get_automatic(

def _get(self, config: Dict[str, Any], key: str, default: Optional[Any] = None) -> Any:
"""Utility to get a config value which might be inside a subcommand."""
if self.subcommand is not None:
return config[self.subcommand].get(key, default)
return config.get(key, default)
return config.get(str(self.subcommand), config).get(key, default)

def _run_subcommand(self, subcommand: str) -> None:
"""Run the chosen subcommand."""
Expand Down
12 changes: 8 additions & 4 deletions tests/utilities/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,7 @@ def test_lightning_cli_args(tmpdir):
with open(config_path) as f:
loaded_config = yaml.safe_load(f.read())

loaded_config = loaded_config["fit"]
cli_config = cli.config["fit"].as_dict()

assert cli_config["seed_everything"] == 1234
assert "model" not in loaded_config and "model" not in cli_config # no arguments to include
assert loaded_config["data"] == cli_config["data"]
Expand Down Expand Up @@ -405,9 +403,7 @@ def test_lightning_cli_config_and_subclass_mode(tmpdir):
with open(config_path) as f:
loaded_config = yaml.safe_load(f.read())

loaded_config = loaded_config["fit"]
cli_config = cli.config["fit"].as_dict()

assert loaded_config["model"] == cli_config["model"]
assert loaded_config["data"] == cli_config["data"]
assert loaded_config["trainer"] == cli_config["trainer"]
Expand Down Expand Up @@ -1251,6 +1247,10 @@ def test_lightning_cli_config_before_subcommand():
test_mock.assert_called_once_with(cli.trainer, model=cli.model, verbose=True, ckpt_path="foobar")
assert cli.trainer.limit_test_batches == 1

save_config_callback = cli.trainer.callbacks[0]
assert save_config_callback.config.trainer.limit_test_batches == 1
assert save_config_callback.parser.subcommand == "test"

with mock.patch("sys.argv", ["any.py", f"--config={config}", "validate"]), mock.patch(
"pytorch_lightning.Trainer.validate", autospec=True
) as validate_mock:
Expand All @@ -1259,6 +1259,10 @@ def test_lightning_cli_config_before_subcommand():
validate_mock.assert_called_once_with(cli.trainer, cli.model, verbose=False, ckpt_path="barfoo")
assert cli.trainer.limit_val_batches == 1

save_config_callback = cli.trainer.callbacks[0]
assert save_config_callback.config.trainer.limit_val_batches == 1
assert save_config_callback.parser.subcommand == "validate"


def test_lightning_cli_config_before_subcommand_two_configs():
config1 = {"validate": {"trainer": {"limit_val_batches": 1}, "verbose": False, "ckpt_path": "barfoo"}}
Expand Down