From 124b529088575183e0bbe1e8d18d6aec1f8b93c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mochol=C3=AD?= Date: Thu, 7 Apr 2022 14:22:51 +0200 Subject: [PATCH] Add version header to CLI config files (#12532) --- CHANGELOG.md | 2 +- pytorch_lightning/utilities/cli.py | 1 + pytorch_lightning/utilities/imports.py | 2 +- requirements/extra.txt | 2 +- tests/utilities/test_cli.py | 8 ++++++-- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4037dd3632379..ba38e83b77508 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - -- +- Include the `pytorch_lightning` version as a header in the CLI config files ([#12532](https://github.com/PyTorchLightning/pytorch-lightning/pull/12532)) - diff --git a/pytorch_lightning/utilities/cli.py b/pytorch_lightning/utilities/cli.py index 2e8d629a88cb8..0e6b0edba39eb 100644 --- a/pytorch_lightning/utilities/cli.py +++ b/pytorch_lightning/utilities/cli.py @@ -575,6 +575,7 @@ def _setup_parser_kwargs( def init_parser(self, **kwargs: Any) -> LightningArgumentParser: """Method that instantiates the argument parser.""" + kwargs.setdefault("dump_header", [f"pytorch_lightning=={pl.__version__}"]) return LightningArgumentParser(**kwargs) def setup_parser( diff --git a/pytorch_lightning/utilities/imports.py b/pytorch_lightning/utilities/imports.py index aadc419b778d9..5a57446dc6a40 100644 --- a/pytorch_lightning/utilities/imports.py +++ b/pytorch_lightning/utilities/imports.py @@ -103,7 +103,7 @@ def _compare_version(package: str, op: Callable, version: str, use_base_version: _HOROVOD_AVAILABLE = _module_available("horovod.torch") _HYDRA_AVAILABLE = _package_available("hydra") _HYDRA_EXPERIMENTAL_AVAILABLE = _module_available("hydra.experimental") -_JSONARGPARSE_AVAILABLE = _package_available("jsonargparse") and _compare_version("jsonargparse", operator.ge, "4.3.0") +_JSONARGPARSE_AVAILABLE = _package_available("jsonargparse") and _compare_version("jsonargparse", operator.ge, "4.5.0") _KINETO_AVAILABLE = _TORCH_GREATER_EQUAL_1_8_1 and torch.profiler.kineto_available() _NEPTUNE_AVAILABLE = _package_available("neptune") _NEPTUNE_GREATER_EQUAL_0_9 = _NEPTUNE_AVAILABLE and _compare_version("neptune", operator.ge, "0.9.0") diff --git a/requirements/extra.txt b/requirements/extra.txt index b44d68f24f723..602605759a194 100644 --- a/requirements/extra.txt +++ b/requirements/extra.txt @@ -5,6 +5,6 @@ horovod>=0.21.2,!=0.24.0 # no need to install with [pytorch] as pytorch is alre torchtext>=0.9.* omegaconf>=2.0.5 hydra-core>=1.0.5 -jsonargparse[signatures]>=4.3.0 +jsonargparse[signatures]>=4.5.0 gcsfs>=2021.5.0 rich>=10.2.2,!=10.15.* diff --git a/tests/utilities/test_cli.py b/tests/utilities/test_cli.py index 710f2f3be1d80..441067afa3244 100644 --- a/tests/utilities/test_cli.py +++ b/tests/utilities/test_cli.py @@ -31,7 +31,7 @@ from torch.optim import SGD from torch.optim.lr_scheduler import ReduceLROnPlateau, StepLR -from pytorch_lightning import Callback, LightningDataModule, LightningModule, Trainer +from pytorch_lightning import __version__, Callback, LightningDataModule, LightningModule, Trainer from pytorch_lightning.callbacks import LearningRateMonitor, ModelCheckpoint from pytorch_lightning.loggers import LightningLoggerBase, TensorBoardLogger from pytorch_lightning.plugins.environments import SLURMEnvironment @@ -458,7 +458,11 @@ def test_lightning_cli_print_config(): with mock.patch("sys.argv", cli_args), redirect_stdout(out), pytest.raises(SystemExit): any_model_any_data_cli() - outval = yaml.safe_load(out.getvalue()) + text = out.getvalue() + # test dump_header + assert text.startswith(f"# pytorch_lightning=={__version__}") + + outval = yaml.safe_load(text) assert outval["seed_everything"] == 1234 assert outval["model"]["class_path"] == "tests.helpers.BoringModel" assert outval["data"]["class_path"] == "tests.helpers.BoringDataModule"