From d52ff0ef2251734b9297b397c1e3e90c901a11ce Mon Sep 17 00:00:00 2001 From: Mauricio Villegas Date: Tue, 24 Aug 2021 07:19:23 +0200 Subject: [PATCH 1/5] LightningCLI multifile save can now be set and is false by default. --- CHANGELOG.md | 3 +++ pytorch_lightning/utilities/cli.py | 9 +++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35404e85a5816..88f32637ee971 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -121,6 +121,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - `Trainer.request_dataloader` now takes a `RunningStage` enum instance ([#8858](https://github.com/PyTorchLightning/pytorch-lightning/pull/8858)) +- `LightningCLI` by default support saving to fsspec filesystem ([#9073](https://github.com/PyTorchLightning/pytorch-lightning/pull/9073)) + + ### Deprecated - Deprecated `LightningModule.summarize()` in favor of `pytorch_lightning.utilities.model_summary.summarize()` diff --git a/pytorch_lightning/utilities/cli.py b/pytorch_lightning/utilities/cli.py index 6cbb12340dc54..3d4693a489ea4 100644 --- a/pytorch_lightning/utilities/cli.py +++ b/pytorch_lightning/utilities/cli.py @@ -161,11 +161,13 @@ def __init__( config: Union[Namespace, Dict[str, Any]], config_filename: str, overwrite: bool = False, + multifile: bool = False, ) -> None: self.parser = parser self.config = config self.config_filename = config_filename self.overwrite = overwrite + self.multifile = multifile def setup(self, trainer: Trainer, pl_module: LightningModule, stage: Optional[str] = None) -> None: # save the config in `setup` because (1) we want it to save regardless of the trainer function run @@ -185,7 +187,7 @@ def setup(self, trainer: Trainer, pl_module: LightningModule, stage: Optional[st # the `log_dir` needs to be created as we rely on the logger to do it usually # but it hasn't logged anything at this point get_filesystem(log_dir).makedirs(log_dir, exist_ok=True) - self.parser.save(self.config, config_path, skip_none=False, overwrite=self.overwrite) + self.parser.save(self.config, config_path, skip_none=False, overwrite=self.overwrite, multifile=self.multifile) def __reduce__(self) -> Tuple[Type["SaveConfigCallback"], Tuple, Dict]: # `ArgumentParser` is un-pickleable. Drop it @@ -202,6 +204,7 @@ def __init__( save_config_callback: Optional[Type[SaveConfigCallback]] = SaveConfigCallback, save_config_filename: str = "config.yaml", save_config_overwrite: bool = False, + save_config_multifile: bool = False, trainer_class: Union[Type[Trainer], Callable[..., Trainer]] = Trainer, trainer_defaults: Optional[Dict[str, Any]] = None, seed_everything_default: Optional[int] = None, @@ -244,6 +247,7 @@ def __init__( save_config_callback: A callback class to save the training config. save_config_filename: Filename for the config file. save_config_overwrite: Whether to overwrite an existing config file. + save_config_multifile: When input is multiple config files, saved config preserves this structure. trainer_class: An optional subclass of the :class:`~pytorch_lightning.trainer.trainer.Trainer` class or a callable which returns a :class:`~pytorch_lightning.trainer.trainer.Trainer` instance when called. trainer_defaults: Set to override Trainer defaults or add persistent callbacks. @@ -267,6 +271,7 @@ def __init__( self.save_config_callback = save_config_callback self.save_config_filename = save_config_filename self.save_config_overwrite = save_config_overwrite + self.save_config_multifile = save_config_multifile self.trainer_class = trainer_class self.trainer_defaults = trainer_defaults or {} self.seed_everything_default = seed_everything_default @@ -371,7 +376,7 @@ 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.save_config_filename, overwrite=self.save_config_overwrite + self.parser, self.config, self.save_config_filename, overwrite=self.save_config_overwrite, multifile=self.save_config_multifile ) config["callbacks"].append(config_callback) return self.trainer_class(**config) From bdf5d67ef1161091f923878c2bd7c9683d6406af Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 24 Aug 2021 05:31:13 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pytorch_lightning/utilities/cli.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pytorch_lightning/utilities/cli.py b/pytorch_lightning/utilities/cli.py index 3d4693a489ea4..e362c06397726 100644 --- a/pytorch_lightning/utilities/cli.py +++ b/pytorch_lightning/utilities/cli.py @@ -187,7 +187,9 @@ def setup(self, trainer: Trainer, pl_module: LightningModule, stage: Optional[st # the `log_dir` needs to be created as we rely on the logger to do it usually # but it hasn't logged anything at this point get_filesystem(log_dir).makedirs(log_dir, exist_ok=True) - self.parser.save(self.config, config_path, skip_none=False, overwrite=self.overwrite, multifile=self.multifile) + self.parser.save( + self.config, config_path, skip_none=False, overwrite=self.overwrite, multifile=self.multifile + ) def __reduce__(self) -> Tuple[Type["SaveConfigCallback"], Tuple, Dict]: # `ArgumentParser` is un-pickleable. Drop it @@ -376,7 +378,11 @@ 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.save_config_filename, overwrite=self.save_config_overwrite, multifile=self.save_config_multifile + self.parser, + self.config, + self.save_config_filename, + overwrite=self.save_config_overwrite, + multifile=self.save_config_multifile, ) config["callbacks"].append(config_callback) return self.trainer_class(**config) From 5868356d43f1e88caeddb6489b041f6c3cf2bc09 Mon Sep 17 00:00:00 2001 From: Mauricio Villegas Date: Fri, 17 Sep 2021 20:51:25 +0200 Subject: [PATCH 3/5] Changes based on review comments --- CHANGELOG.md | 6 +++--- pytorch_lightning/utilities/cli.py | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c223ef674e3a..d5957c5079f22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -136,6 +136,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Added `PL_RECONCILE_PROCESS` environment variable to enable process reconciliation regardless of cluster environment settings ([#9389](https://github.com/PyTorchLightning/pytorch-lightning/pull/9389)) +- Added `multifile` option to `LightningCLI` to enable/disable config save saved to preserve multiple config files structure. ([#9073](https://github.com/PyTorchLightning/pytorch-lightning/pull/9073)) + + ### Changed - `pytorch_lightning.loggers.neptune.NeptuneLogger` is now consistent with new [neptune-client](https://github.com/neptune-ai/neptune-client) API ([#6867](https://github.com/PyTorchLightning/pytorch-lightning/pull/6867)). @@ -187,9 +190,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - `Trainer.request_dataloader` now takes a `RunningStage` enum instance ([#8858](https://github.com/PyTorchLightning/pytorch-lightning/pull/8858)) -- `LightningCLI` by default support saving to fsspec filesystem ([#9073](https://github.com/PyTorchLightning/pytorch-lightning/pull/9073)) - - - Changed `rank_zero_warn` to `NotImplementedError` in the `{train, val, test, predict}_dataloader` hooks that `Lightning(Data)Module` uses ([#9161](https://github.com/PyTorchLightning/pytorch-lightning/pull/9161)) diff --git a/pytorch_lightning/utilities/cli.py b/pytorch_lightning/utilities/cli.py index 62a86731c50ed..caad6754216b8 100644 --- a/pytorch_lightning/utilities/cli.py +++ b/pytorch_lightning/utilities/cli.py @@ -150,6 +150,13 @@ def add_lr_scheduler_args( class SaveConfigCallback(Callback): """Saves a LightningCLI config to the log_dir when training starts. + Args: + parser: The parser object used to parse the configuration. + config: The parsed configuration that will be saved. + config_filename: Filename for the config file. + overwrite: Whether to overwrite an existing config file. + multifile: When input is multiple config files, saved config preserves this structure. + Raises: RuntimeError: If the config file already exists in the directory to avoid overwriting a previous run """ From abb62670b0896a99cbf9ea1a40d307749c1e84a3 Mon Sep 17 00:00:00 2001 From: Mauricio Villegas Date: Fri, 17 Sep 2021 20:52:17 +0200 Subject: [PATCH 4/5] Changes based on review comments --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d5957c5079f22..584d09570a548 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -136,7 +136,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Added `PL_RECONCILE_PROCESS` environment variable to enable process reconciliation regardless of cluster environment settings ([#9389](https://github.com/PyTorchLightning/pytorch-lightning/pull/9389)) -- Added `multifile` option to `LightningCLI` to enable/disable config save saved to preserve multiple config files structure. ([#9073](https://github.com/PyTorchLightning/pytorch-lightning/pull/9073)) +- Added `multifile` option to `LightningCLI` to enable/disable config save to preserve multiple files structure. ([#9073](https://github.com/PyTorchLightning/pytorch-lightning/pull/9073)) ### Changed From 5aa4fcc34ce758dcc1c6e234471994e03a0d6c37 Mon Sep 17 00:00:00 2001 From: Mauricio Villegas Date: Fri, 17 Sep 2021 20:53:11 +0200 Subject: [PATCH 5/5] Changes based on review comments --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 584d09570a548..539e6a0abf8f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -136,7 +136,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Added `PL_RECONCILE_PROCESS` environment variable to enable process reconciliation regardless of cluster environment settings ([#9389](https://github.com/PyTorchLightning/pytorch-lightning/pull/9389)) -- Added `multifile` option to `LightningCLI` to enable/disable config save to preserve multiple files structure. ([#9073](https://github.com/PyTorchLightning/pytorch-lightning/pull/9073)) +- Added `multifile` option to `LightningCLI` to enable/disable config save to preserve multiple files structure ([#9073](https://github.com/PyTorchLightning/pytorch-lightning/pull/9073)) ### Changed