From d42711f22f86b901203bdb2dd77c20b578ced1a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20W=C3=A4lchli?= Date: Fri, 15 Jul 2022 19:18:55 +0200 Subject: [PATCH] Remove deprecated `Strategy.post_dispatch` (#13461) * Remove deprecated Strategy.post_dispatch * changelog * remove unused imports --- src/pytorch_lightning/CHANGELOG.md | 3 ++ src/pytorch_lightning/strategies/strategy.py | 15 ---------- src/pytorch_lightning/trainer/trainer.py | 1 - .../deprecated_api/test_remove_1-7.py | 29 ------------------- 4 files changed, 3 insertions(+), 45 deletions(-) delete mode 100644 tests/tests_pytorch/deprecated_api/test_remove_1-7.py diff --git a/src/pytorch_lightning/CHANGELOG.md b/src/pytorch_lightning/CHANGELOG.md index 9fc6cc720a..218d3c6fe9 100644 --- a/src/pytorch_lightning/CHANGELOG.md +++ b/src/pytorch_lightning/CHANGELOG.md @@ -281,6 +281,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Removed the need to explicitly load habana module ([#13338](https://github.com/PyTorchLightning/pytorch-lightning/pull/13338)) +- Removed the deprecated `Strategy.post_dispatch()` hook ([#13461](https://github.com/PyTorchLightning/pytorch-lightning/pull/13461)) + + - Removed deprecated `pytorch_lightning.callbacks.lr_monitor.LearningRateMonitor.lr_sch_names` ([#13353](https://github.com/Lightning-AI/lightning/pull/13353)) diff --git a/src/pytorch_lightning/strategies/strategy.py b/src/pytorch_lightning/strategies/strategy.py index 3890a739ed..2cbf14760f 100644 --- a/src/pytorch_lightning/strategies/strategy.py +++ b/src/pytorch_lightning/strategies/strategy.py @@ -30,10 +30,8 @@ from pytorch_lightning.plugins.io.checkpoint_plugin import CheckpointIO from pytorch_lightning.plugins.precision import PrecisionPlugin from pytorch_lightning.strategies.launchers.base import _Launcher from pytorch_lightning.trainer.states import TrainerFn -from pytorch_lightning.utilities import rank_zero_deprecation from pytorch_lightning.utilities.apply_func import move_data_to_device from pytorch_lightning.utilities.distributed import ReduceOp -from pytorch_lightning.utilities.model_helpers import is_overridden from pytorch_lightning.utilities.optimizer import optimizer_to_device, optimizers_to_device from pytorch_lightning.utilities.types import _PATH, LRSchedulerConfig, STEP_OUTPUT @@ -61,11 +59,6 @@ class Strategy(ABC): self._lightning_optimizers: Dict[int, LightningOptimizer] = {} self.lr_scheduler_configs: List[LRSchedulerConfig] = [] self.optimizer_frequencies: List[int] = [] - if is_overridden("post_dispatch", self, parent=Strategy): - rank_zero_deprecation( - f"`{self.__class__.__name__}.post_dispatch()` has been deprecated in v1.6 and will be removed in v1.7." - f" Move your implementation to `{self.__class__.__name__}.teardown()` instead." - ) @property def launcher(self) -> Optional[_Launcher]: @@ -506,11 +499,3 @@ class Strategy(ABC): def __setstate__(self, state: Dict) -> None: self.__dict__ = state self.optimizers = self.optimizers # re-create the `_lightning_optimizers` - - def post_dispatch(self, trainer: "pl.Trainer") -> None: - r""" - .. deprecated:: - v1.6 This method has been deprecated in v1.6 and will be removed in v1.7. Use :meth:`teardown` instead. - - Hook to do something after the training/evaluation/prediction finishes. - """ diff --git a/src/pytorch_lightning/trainer/trainer.py b/src/pytorch_lightning/trainer/trainer.py index 02cb951e1e..25357578ea 100644 --- a/src/pytorch_lightning/trainer/trainer.py +++ b/src/pytorch_lightning/trainer/trainer.py @@ -1225,7 +1225,6 @@ class Trainer( def _teardown(self): """This is the Trainer's internal teardown, unrelated to the `teardown` hooks in LightningModule and Callback; those are handled by :meth:`_call_teardown_hook`.""" - self.strategy.post_dispatch(self) self.strategy.teardown() loop = self._active_loop # loop should never be `None` here but it can because we don't know the trainer stage with `ddp_spawn` diff --git a/tests/tests_pytorch/deprecated_api/test_remove_1-7.py b/tests/tests_pytorch/deprecated_api/test_remove_1-7.py deleted file mode 100644 index bf6ffdc33e..0000000000 --- a/tests/tests_pytorch/deprecated_api/test_remove_1-7.py +++ /dev/null @@ -1,29 +0,0 @@ -# Copyright The PyTorch Lightning team. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""Test deprecated functionality which will be removed in v1.7.0.""" -from re import escape - -import pytest -import torch - -from pytorch_lightning.strategies import SingleDeviceStrategy - - -def test_v1_7_0_post_dispatch_hook(): - class CustomPlugin(SingleDeviceStrategy): - def post_dispatch(self, trainer): - pass - - with pytest.deprecated_call(match=escape("`CustomPlugin.post_dispatch()` has been deprecated in v1.6")): - CustomPlugin(torch.device("cpu"))