Skip to content

Commit

Permalink
Remove the deprecated pl.utilities.cli module (#16116)
Browse files Browse the repository at this point in the history
  • Loading branch information
carmocca committed Dec 20, 2022
1 parent 222562a commit f0662ca
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 220 deletions.
4 changes: 3 additions & 1 deletion examples/pl_basics/backbone_image_classifier.py
Expand Up @@ -124,7 +124,9 @@ def predict_dataloader(self):


def cli_main():
cli = LightningCLI(LitClassifier, MyDataModule, seed_everything_default=1234, save_config_overwrite=True, run=False)
cli = LightningCLI(
LitClassifier, MyDataModule, seed_everything_default=1234, save_config_kwargs={"overwrite": True}, run=False
)
cli.trainer.fit(cli.model, datamodule=cli.datamodule)
cli.trainer.test(ckpt_path="best", datamodule=cli.datamodule)
predictions = cli.trainer.predict(ckpt_path="best", datamodule=cli.datamodule)
Expand Down
5 changes: 4 additions & 1 deletion examples/pl_basics/profiler_example.py
Expand Up @@ -107,7 +107,10 @@ def cli_main():
sys.argv += DEFAULT_CMD_LINE

LightningCLI(
ModelToProfile, CIFAR10DataModule, save_config_overwrite=True, trainer_defaults={"profiler": PyTorchProfiler()}
ModelToProfile,
CIFAR10DataModule,
save_config_kwargs={"overwrite": True},
trainer_defaults={"profiler": PyTorchProfiler()},
)


Expand Down
2 changes: 1 addition & 1 deletion examples/pl_domain_templates/imagenet.py
Expand Up @@ -190,5 +190,5 @@ def test_dataloader(self):
],
},
seed_everything_default=42,
save_config_overwrite=True,
save_config_kwargs={"overwrite": True},
)
2 changes: 1 addition & 1 deletion examples/pl_hpu/mnist_sample.py
Expand Up @@ -66,7 +66,7 @@ def configure_optimizers(self):
"plugins": lazy_instance(HPUPrecisionPlugin, precision=16),
},
run=False,
save_config_overwrite=True,
save_config_kwargs={"overwrite": True},
)

# Run the model ⚡
Expand Down
4 changes: 3 additions & 1 deletion examples/pl_integrations/dali_image_classifier.py
Expand Up @@ -194,7 +194,9 @@ def cli_main():
if not _DALI_AVAILABLE:
return

cli = LightningCLI(LitClassifier, MyDataModule, seed_everything_default=1234, save_config_overwrite=True, run=False)
cli = LightningCLI(
LitClassifier, MyDataModule, seed_everything_default=1234, save_config_kwargs={"overwrite": True}, run=False
)
cli.trainer.fit(cli.model, datamodule=cli.datamodule)
cli.trainer.test(ckpt_path="best", datamodule=cli.datamodule)

Expand Down
2 changes: 1 addition & 1 deletion examples/pl_servable_module/production.py
Expand Up @@ -109,7 +109,7 @@ def cli_main():
ProductionReadyModel,
CIFAR10DataModule,
seed_everything_default=42,
save_config_overwrite=True,
save_config_kwargs={"overwrite": True},
run=False,
trainer_defaults={
"callbacks": [ServableModuleValidator()],
Expand Down
3 changes: 3 additions & 0 deletions src/pytorch_lightning/CHANGELOG.md
Expand Up @@ -101,6 +101,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Removed the deprecated `pytorch_lightning.profiler.*` classes in favor of `pytorch_lightning.profilers` ([#16059](https://github.com/PyTorchLightning/pytorch-lightning/pull/16059))


- Removed the deprecated `pytorch_lightning.utilities.cli` module in favor of `pytorch_lightning.cli` ([#16116](https://github.com/PyTorchLightning/pytorch-lightning/pull/16116))


### Fixed

- Enhanced `reduce_boolean_decision` to accommodate `any`-analogous semantics expected by the `EarlyStopping` callback ([#15253](https://github.com/Lightning-AI/lightning/pull/15253))
Expand Down
1 change: 1 addition & 0 deletions src/pytorch_lightning/_graveyard/__init__.py
Expand Up @@ -14,6 +14,7 @@

import pytorch_lightning._graveyard.accelerator
import pytorch_lightning._graveyard.callbacks
import pytorch_lightning._graveyard.cli
import pytorch_lightning._graveyard.core
import pytorch_lightning._graveyard.legacy_import_unpickler
import pytorch_lightning._graveyard.loggers
Expand Down
45 changes: 45 additions & 0 deletions src/pytorch_lightning/_graveyard/cli.py
@@ -0,0 +1,45 @@
import sys
from typing import Any


def _patch_sys_modules() -> None:
# TODO: Remove in v2.0.0
self = sys.modules[__name__]
sys.modules["pytorch_lightning.utilities.cli"] = self


class LightningCLI:
# TODO: Remove in v2.0.0
def __init__(self, *_: Any, **__: Any) -> None:
raise NotImplementedError(
"`pytorch_lightning.utilities.cli.LightningCLI` was deprecated in v1.7.0 and is no"
" longer supported as of v1.9.0. Please use `pytorch_lightning.cli.LightningCLI` instead"
)


class SaveConfigCallback:
# TODO: Remove in v2.0.0
def __init__(self, *_: Any, **__: Any) -> None:
raise NotImplementedError(
"`pytorch_lightning.utilities.cli.SaveConfigCallback` was deprecated in v1.7.0 and is no"
" longer supported as of v1.9.0. Please use `pytorch_lightning.cli.SaveConfigCallback` instead"
)


class LightningArgumentParser:
# TODO: Remove in v2.0.0
def __init__(self, *_: Any, **__: Any) -> None:
raise NotImplementedError(
"`pytorch_lightning.utilities.cli.LightningArgumentParser` was deprecated in v1.7.0 and is no"
" longer supported as of v1.9.0. Please use `pytorch_lightning.cli.LightningArgumentParser` instead"
)


def instantiate_class(*_: Any, **__: Any) -> None:
raise NotImplementedError(
"`pytorch_lightning.utilities.cli.instantiate_class` was deprecated in v1.7.0 and is no"
" longer supported as of v1.9.0. Please use `pytorch_lightning.cli.instantiate_class` instead"
)


_patch_sys_modules()
23 changes: 8 additions & 15 deletions src/pytorch_lightning/cli.py
Expand Up @@ -279,7 +279,6 @@ def __init__(
args: ArgsType = None,
run: bool = True,
auto_configure_optimizers: bool = True,
auto_registry: bool = False,
**kwargs: Any, # Remove with deprecations of v1.10
) -> None:
"""Receives as input pytorch-lightning classes (or callables which return pytorch-lightning classes), which
Expand Down Expand Up @@ -323,7 +322,6 @@ def __init__(
``dict`` or ``jsonargparse.Namespace``.
run: Whether subcommands should be added to run a :class:`~pytorch_lightning.trainer.trainer.Trainer`
method. If set to ``False``, the trainer and model classes will be instantiated only.
auto_registry: Whether to automatically fill up the registries with all defined subclasses.
"""
self.save_config_callback = save_config_callback
self.save_config_kwargs = save_config_kwargs or {}
Expand All @@ -345,10 +343,6 @@ def __init__(
self._datamodule_class = datamodule_class or LightningDataModule
self.subclass_mode_data = (datamodule_class is None) or subclass_mode_data

from pytorch_lightning.utilities.cli import _populate_registries

_populate_registries(auto_registry)

main_kwargs, subparser_kwargs = self._setup_parser_kwargs(self.parser_kwargs)
self.setup_parser(run, main_kwargs, subparser_kwargs)
self.parse_arguments(self.parser, args)
Expand All @@ -371,15 +365,14 @@ def _handle_deprecated_params(self, kwargs: dict) -> None:
)
self.seed_everything_default = False

for name in ["save_config_filename", "save_config_overwrite", "save_config_multifile"]:
if name in kwargs:
value = kwargs.pop(name)
key = name.replace("save_config_", "").replace("filename", "config_filename")
self.save_config_kwargs[key] = value
rank_zero_deprecation(
f"LightningCLI's {name!r} init parameter is deprecated from v1.8 and will "
f"be removed in v1.10. Use `save_config_kwargs={{'{key}': ...}}` instead."
)
for name in kwargs.keys() & ["save_config_filename", "save_config_overwrite", "save_config_multifile"]:
value = kwargs.pop(name)
key = name.replace("save_config_", "").replace("filename", "config_filename")
self.save_config_kwargs[key] = value
rank_zero_deprecation(
f"LightningCLI's {name!r} init parameter is deprecated from v1.8 and will "
f"be removed in v1.10. Use `save_config_kwargs={{'{key}': ...}}` instead."
)

for name in kwargs.keys() & ["description", "env_prefix", "env_parse"]:
value = kwargs.pop(name)
Expand Down
165 changes: 0 additions & 165 deletions src/pytorch_lightning/utilities/cli.py

This file was deleted.

34 changes: 1 addition & 33 deletions tests/tests_pytorch/deprecated_api/test_remove_1-9.py
Expand Up @@ -13,16 +13,13 @@
# limitations under the License.

from unittest import mock
from unittest.mock import Mock

import pytest

import pytorch_lightning.loggers.base as logger_base
import pytorch_lightning.utilities.cli as old_cli
from pytorch_lightning import Trainer
from pytorch_lightning.cli import LightningCLI, SaveConfigCallback
from pytorch_lightning.cli import LightningCLI
from pytorch_lightning.core.module import LightningModule
from pytorch_lightning.demos.boring_classes import BoringModel
from pytorch_lightning.utilities.rank_zero import rank_zero_only


Expand Down Expand Up @@ -136,32 +133,3 @@ def test_deprecated_dataloader_reset():
trainer = Trainer()
with pytest.deprecated_call(match="reset_train_val_dataloaders` has been deprecated in v1.7"):
trainer.reset_train_val_dataloaders()


def test_lightningCLI_registries_register():
with pytest.deprecated_call(match=old_cli._deprecate_registry_message):

@old_cli.CALLBACK_REGISTRY
class CustomCallback(SaveConfigCallback):
pass


def test_lightningCLI_registries_register_automatically():
with pytest.deprecated_call(match=old_cli._deprecate_auto_registry_message):
with mock.patch("sys.argv", ["any.py"]):
LightningCLI(BoringModel, run=False, auto_registry=True)


def test_lightningCLI_old_module_deprecation():
with pytest.deprecated_call(match=r"LightningCLI.*deprecated in v1.7.*Use the equivalent class"):
with mock.patch("sys.argv", ["any.py"]):
old_cli.LightningCLI(BoringModel, run=False)

with pytest.deprecated_call(match=r"SaveConfigCallback.*deprecated in v1.7.*Use the equivalent class"):
old_cli.SaveConfigCallback(Mock(), Mock(), Mock())

with pytest.deprecated_call(match=r"LightningArgumentParser.*deprecated in v1.7.*Use the equivalent class"):
old_cli.LightningArgumentParser()

with pytest.deprecated_call(match=r"instantiate_class.*deprecated in v1.7.*Use the equivalent function"):
assert isinstance(old_cli.instantiate_class(tuple(), {"class_path": "pytorch_lightning.Trainer"}), Trainer)

0 comments on commit f0662ca

Please sign in to comment.