From acf512321a73997a53caa452b289d77b5a057448 Mon Sep 17 00:00:00 2001 From: otaj Date: Fri, 26 Aug 2022 10:22:11 +0200 Subject: [PATCH 1/2] remove deprecated callback hook --- pyproject.toml | 1 - src/pytorch_lightning/CHANGELOG.md | 3 + .../trainer/callback_hook.py | 670 ------------------ src/pytorch_lightning/trainer/trainer.py | 2 - .../deprecated_api/test_remove_1-8.py | 97 --- 5 files changed, 3 insertions(+), 770 deletions(-) delete mode 100644 src/pytorch_lightning/trainer/callback_hook.py diff --git a/pyproject.toml b/pyproject.toml index 60c031dfe920b..4c95eb4d4f7e8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -55,7 +55,6 @@ module = [ "pytorch_lightning.profilers.base", "pytorch_lightning.profilers.pytorch", "pytorch_lightning.strategies.sharded", - "pytorch_lightning.trainer.callback_hook", "pytorch_lightning.trainer.connectors.data_connector", "pytorch_lightning.trainer.supporters", "pytorch_lightning.trainer.trainer", diff --git a/src/pytorch_lightning/CHANGELOG.md b/src/pytorch_lightning/CHANGELOG.md index 32303d6babb5d..394585c4179f7 100644 --- a/src/pytorch_lightning/CHANGELOG.md +++ b/src/pytorch_lightning/CHANGELOG.md @@ -73,6 +73,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Removed the experimental `pytorch_lightning.utiltiies.meta` functions in favor of built-in https://github.com/pytorch/torchdistx support ([#13868](https://github.com/Lightning-AI/lightning/pull/13868)) +- Removed the deprecated class `CallBackHookMixin` ([](https://github.com/Lightning-AI/lightning/)) + + ### Fixed - Fixed an assertion error when using a `ReduceOnPlateau` scheduler with the Horovod strategy ([#14215](https://github.com/Lightning-AI/lightning/pull/14215)) diff --git a/src/pytorch_lightning/trainer/callback_hook.py b/src/pytorch_lightning/trainer/callback_hook.py deleted file mode 100644 index 1e455b3424606..0000000000000 --- a/src/pytorch_lightning/trainer/callback_hook.py +++ /dev/null @@ -1,670 +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. -from abc import ABC -from copy import deepcopy -from typing import Any, Dict, List, Optional, Type, Union - -from packaging.version import Version -from torch import Tensor - -import pytorch_lightning as pl -from pytorch_lightning.callbacks import Callback -from pytorch_lightning.utilities.rank_zero import rank_zero_deprecation, rank_zero_warn -from pytorch_lightning.utilities.types import STEP_OUTPUT - - -class TrainerCallbackHookMixin(ABC): - r""" - .. deprecated:: v1.6 - The `TrainerCallbackHookMixin` class was deprecated in v1.6 and will be removed in v1.8. - """ - - # this is just a summary on variables used in this abstract class, - # the proper values/initialisation should be done in child class - callbacks: List[Callback] = [] - lightning_module: "pl.LightningModule" - - def on_before_accelerator_backend_setup(self) -> None: - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_before_accelerator_backend_setup` was deprecated in v1.6 - and will be removed in v1.8. - - Called at the beginning of fit (train + validate), validate, test, or predict, or tune. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_before_accelerator_backend_setup` was deprecated in v1.6 " - "and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_before_accelerator_backend_setup(self, self.lightning_module) - - def on_configure_sharded_model(self) -> None: - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_configure_sharded_model` was deprecated in v1.6 and will be removed in v1.8. - - Called at the beginning of fit (train + validate), validate, test, or predict, or tune. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_configure_sharded_model` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_configure_sharded_model(self, self.lightning_module) - - def setup(self, stage: Optional[str]) -> None: - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.setup` was deprecated in v1.6 and will be removed in v1.8. - - Called at the beginning of fit (train + validate), validate, test, or predict, or tune. - """ - rank_zero_deprecation("`TrainerCallbackHookMixin.setup` was deprecated in v1.6 and will be removed in v1.8.") - for callback in self.callbacks: - callback.setup(self, self.lightning_module, stage=stage) - - def teardown(self, stage: Optional[str] = None) -> None: - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.teardown` was deprecated in v1.6 and will be removed in v1.8. - - Called at the end of fit (train + validate), validate, test, or predict, or tune. - """ - rank_zero_deprecation("`TrainerCallbackHookMixin.teardown` was deprecated in v1.6 and will be removed in v1.8.") - for callback in self.callbacks: - callback.teardown(self, self.lightning_module, stage=stage) - - def on_init_start(self): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_init_start` was deprecated in v1.6 and will be removed in v1.8. - - Called when the trainer initialization begins, model has not yet been set. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_init_start` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_init_start(self) - - def on_init_end(self): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_init_end` was deprecated in v1.6 and will be removed in v1.8. - - Called when the trainer initialization ends, model has not yet been set. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_init_end` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_init_end(self) - - def on_fit_start(self): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_fit_start` was deprecated in v1.6 and will be removed in v1.8. - - Called when fit begins. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_fit_start` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_fit_start(self, self.lightning_module) - - def on_fit_end(self): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_fit_end` was deprecated in v1.6 and will be removed in v1.8. - - Called when fit ends. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_fit_end` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_fit_end(self, self.lightning_module) - - def on_sanity_check_start(self): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_sanity_check_start` was deprecated in v1.6 and will be removed in v1.8. - - Called when the validation sanity check starts. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_sanity_check_start` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_sanity_check_start(self, self.lightning_module) - - def on_sanity_check_end(self): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_sanity_check_end` was deprecated in v1.6 and will be removed in v1.8. - - Called when the validation sanity check ends. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_sanity_check_end` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_sanity_check_end(self, self.lightning_module) - - def on_train_epoch_start(self): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_train_epoch_start` was deprecated in v1.6 and will be removed in v1.8. - - Called when the epoch begins. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_train_epoch_start` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_train_epoch_start(self, self.lightning_module) - - def on_train_epoch_end(self): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_train_epoch_end` was deprecated in v1.6 and will be removed in v1.8. - - Called when the epoch ends. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_train_epoch_end` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_train_epoch_end(self, self.lightning_module) - - def on_validation_epoch_start(self): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_validation_epoch_start` was deprecated in v1.6 and will be removed in v1.8. - - Called when the epoch begins. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_validation_epoch_start` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_validation_epoch_start(self, self.lightning_module) - - def on_validation_epoch_end(self): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_validation_epoch_end` was deprecated in v1.6 and will be removed in v1.8. - - Called when the validation epoch ends. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_validation_epoch_end` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_validation_epoch_end(self, self.lightning_module) - - def on_test_epoch_start(self): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_test_epoch_start` was deprecated in v1.6 and will be removed in v1.8. - - Called when the epoch begins. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_test_epoch_start` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_test_epoch_start(self, self.lightning_module) - - def on_test_epoch_end(self): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_test_epoch_end` was deprecated in v1.6 and will be removed in v1.8. - - Called when the test epoch ends. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_test_epoch_end` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_test_epoch_end(self, self.lightning_module) - - def on_predict_epoch_start(self) -> None: - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_predict_epoch_start` was deprecated in v1.6 and will be removed in v1.8. - - Called when the epoch begins. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_predict_epoch_start` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_predict_epoch_start(self, self.lightning_module) - - def on_predict_epoch_end(self, outputs: List[Any]) -> None: - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_predict_epoch_end` was deprecated in v1.6 and will be removed in v1.8. - - Called when the epoch ends. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_predict_epoch_end` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_predict_epoch_end(self, self.lightning_module, outputs) - - def on_epoch_start(self): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_epoch_start` was deprecated in v1.6 and will be removed in v1.8. - - Called when either of train/val/test epoch begins. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_epoch_start` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_epoch_start(self, self.lightning_module) - - def on_epoch_end(self): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_epoch_end` was deprecated in v1.6 and will be removed in v1.8. - - Called when either of train/val/test epoch ends. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_epoch_end` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_epoch_end(self, self.lightning_module) - - def on_train_start(self): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_train_start` was deprecated in v1.6 and will be removed in v1.8. - - Called when the train begins. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_train_start` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_train_start(self, self.lightning_module) - - def on_train_end(self): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_train_end` was deprecated in v1.6 and will be removed in v1.8. - - Called when the train ends. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_train_end` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_train_end(self, self.lightning_module) - - def on_pretrain_routine_start(self) -> None: - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_pretrain_routine_start` was deprecated in v1.6 and will be removed in v1.8. - - Called when the pre-train routine begins. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_pretrain_routine_start` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_pretrain_routine_start(self, self.lightning_module) - - def on_pretrain_routine_end(self) -> None: - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_pretrain_routine_end` was deprecated in v1.6 and will be removed in v1.8. - - Called when the pre-train routine ends. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_pretrain_routine_end` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_pretrain_routine_end(self, self.lightning_module) - - def on_batch_start(self): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_batch_start` was deprecated in v1.6 and will be removed in v1.8. - - Called when the training batch begins. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_batch_start` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_batch_start(self, self.lightning_module) - - def on_batch_end(self): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_batch_end` was deprecated in v1.6 and will be removed in v1.8. - - Called when the training batch ends. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_batch_end` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_batch_end(self, self.lightning_module) - - def on_train_batch_start(self, batch, batch_idx): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_train_batch_start` was deprecated in v1.6 and will be removed in v1.8. - - Called when the training batch begins. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_train_batch_start` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_train_batch_start(self, self.lightning_module, batch, batch_idx) - - def on_train_batch_end(self, outputs: STEP_OUTPUT, batch, batch_idx): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_train_batch_end` was deprecated in v1.6 and will be removed in v1.8. - - Called when the training batch ends. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_train_batch_end` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_train_batch_end(self, self.lightning_module, outputs, batch, batch_idx) - - def on_validation_batch_start(self, batch, batch_idx, dataloader_idx): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_validation_batch_start` was deprecated in v1.6 and will be removed in v1.8. - - Called when the validation batch begins. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_validation_batch_start` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_validation_batch_start(self, self.lightning_module, batch, batch_idx, dataloader_idx) - - def on_validation_batch_end(self, outputs: STEP_OUTPUT, batch, batch_idx, dataloader_idx): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_validation_batch_end` was deprecated in v1.6 and will be removed in v1.8. - - Called when the validation batch ends. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_validation_batch_end` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_validation_batch_end(self, self.lightning_module, outputs, batch, batch_idx, dataloader_idx) - - def on_test_batch_start(self, batch, batch_idx, dataloader_idx): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_test_batch_start` was deprecated in v1.6 and will be removed in v1.8. - - Called when the test batch begins. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_test_batch_start` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_test_batch_start(self, self.lightning_module, batch, batch_idx, dataloader_idx) - - def on_test_batch_end(self, outputs: STEP_OUTPUT, batch, batch_idx, dataloader_idx): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_test_batch_end` was deprecated in v1.6 and will be removed in v1.8. - - Called when the test batch ends. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_test_batch_end` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_test_batch_end(self, self.lightning_module, outputs, batch, batch_idx, dataloader_idx) - - def on_predict_batch_start(self, batch: Any, batch_idx: int, dataloader_idx: int) -> None: - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_predict_batch_start` was deprecated in v1.6 and will be removed in v1.8. - - Called when the predict batch begins. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_predict_batch_start` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_predict_batch_start(self, self.lightning_module, batch, batch_idx, dataloader_idx) - - def on_predict_batch_end(self, outputs: STEP_OUTPUT, batch: Any, batch_idx: int, dataloader_idx: int) -> None: - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_predict_batch_end` was deprecated in v1.6 and will be removed in v1.8. - - Called when the predict batch ends. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_predict_batch_end` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_predict_batch_end(self, self.lightning_module, outputs, batch, batch_idx, dataloader_idx) - - def on_validation_start(self): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_validation_start` was deprecated in v1.6 and will be removed in v1.8. - - Called when the validation loop begins. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_validation_start` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_validation_start(self, self.lightning_module) - - def on_validation_end(self): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_validation_end` was deprecated in v1.6 and will be removed in v1.8. - - Called when the validation loop ends. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_validation_end` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_validation_end(self, self.lightning_module) - - def on_test_start(self): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_test_start` was deprecated in v1.6 and will be removed in v1.8. - - Called when the test begins. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_test_start` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_test_start(self, self.lightning_module) - - def on_test_end(self): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_test_end` was deprecated in v1.6 and will be removed in v1.8. - - Called when the test ends. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_test_end` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_test_end(self, self.lightning_module) - - def on_predict_start(self) -> None: - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_predict_start` was deprecated in v1.6 and will be removed in v1.8. - - Called when predict begins. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_predict_start` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_predict_start(self, self.lightning_module) - - def on_predict_end(self) -> None: - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_predict_end` was deprecated in v1.6 and will be removed in v1.8. - - Called when predict ends. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_predict_end` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_predict_end(self, self.lightning_module) - - def on_exception(self, exception: BaseException) -> None: - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_exception` was deprecated in v1.6 and will be removed in v1.8. - - Called when any trainer execution is interrupted by an exception. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_exception` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_exception(self, self.lightning_module, exception) - - def on_save_checkpoint(self, checkpoint: Dict[str, Any]) -> Dict[str, dict]: - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_save_checkpoint` was deprecated in v1.6 and will be removed in v1.8. - - Called when saving a model checkpoint. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_save_checkpoint` was deprecated in v1.6 and will be removed in v1.8." - ) - callback_states = {} - for callback in self.callbacks: - state = callback.on_save_checkpoint(self, self.lightning_module, checkpoint) - if state: - callback_states[callback.state_key] = state - return callback_states - - def on_load_checkpoint(self, checkpoint: Dict[str, Any]) -> None: - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_load_checkpoint` was deprecated in v1.6 and will be removed in v1.8. - - Called when loading a model checkpoint. - """ - # Todo: the `callback_states` are dropped with TPUSpawn as they - # can't be saved using `xm.save` - # https://github.com/pytorch/xla/issues/2773 - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_load_checkpoint` was deprecated in v1.6 and will be removed in v1.8." - ) - callback_states: Dict[Union[Type, str], Dict] = checkpoint.get("callbacks") - - if callback_states is None: - return - - is_legacy_ckpt = Version(checkpoint["pytorch-lightning_version"]) < Version("1.5.0dev") - current_callbacks_keys = {cb._legacy_state_key if is_legacy_ckpt else cb.state_key for cb in self.callbacks} - difference = callback_states.keys() - current_callbacks_keys - if difference: - rank_zero_warn( - "Be aware that when using `ckpt_path`," - " callbacks used to create the checkpoint need to be provided during `Trainer` instantiation." - f" Please add the following callbacks: {list(difference)}.", - ) - - for callback in self.callbacks: - state = callback_states.get(callback.state_key, callback_states.get(callback._legacy_state_key)) - if state: - state = deepcopy(state) - callback.on_load_checkpoint(self, self.lightning_module, state) - - def on_before_backward(self, loss: Tensor) -> None: - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_before_backward` was deprecated in v1.6 and will be removed in v1.8. - - Called before ``loss.backward()``. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_before_backward` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_before_backward(self, self.lightning_module, loss) - - def on_after_backward(self): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_after_backward` was deprecated in v1.6 and will be removed in v1.8. - - Called after loss.backward() and before optimizers do anything. - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_after_backward` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_after_backward(self, self.lightning_module) - - def on_before_optimizer_step(self, optimizer, optimizer_idx): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_before_optimizer_step` was deprecated in v1.6 and will be removed in v1.8. - - Called after on_after_backward() once the gradient is accumulated and before optimizer.step(). - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_before_optimizer_step` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_before_optimizer_step(self, self.lightning_module, optimizer, optimizer_idx) - - def on_before_zero_grad(self, optimizer): - r""" - .. deprecated:: v1.6 - `TrainerCallbackHookMixin.on_before_zero_grad` was deprecated in v1.6 and will be removed in v1.8. - - Called after optimizer.step() and before optimizer.zero_grad(). - """ - rank_zero_deprecation( - "`TrainerCallbackHookMixin.on_before_zero_grad` was deprecated in v1.6 and will be removed in v1.8." - ) - for callback in self.callbacks: - callback.on_before_zero_grad(self, self.lightning_module, optimizer) diff --git a/src/pytorch_lightning/trainer/trainer.py b/src/pytorch_lightning/trainer/trainer.py index 8bee0ac6dfb7f..9d3fa1215a003 100644 --- a/src/pytorch_lightning/trainer/trainer.py +++ b/src/pytorch_lightning/trainer/trainer.py @@ -70,7 +70,6 @@ XLAProfiler, ) from pytorch_lightning.strategies import ParallelStrategy, Strategy -from pytorch_lightning.trainer.callback_hook import TrainerCallbackHookMixin from pytorch_lightning.trainer.configuration_validator import verify_loop_configurations from pytorch_lightning.trainer.connectors.accelerator_connector import _LITERAL_WARN, AcceleratorConnector from pytorch_lightning.trainer.connectors.callback_connector import CallbackConnector @@ -127,7 +126,6 @@ class Trainer( - TrainerCallbackHookMixin, # TODO: Remove in v1.8 TrainerOptimizersMixin, # TODO: Remove in v1.8 TrainerDataLoadingMixin, # TODO: Remove in v1.8 ): diff --git a/tests/tests_pytorch/deprecated_api/test_remove_1-8.py b/tests/tests_pytorch/deprecated_api/test_remove_1-8.py index a69071fd67610..89877b2ea02e1 100644 --- a/tests/tests_pytorch/deprecated_api/test_remove_1-8.py +++ b/tests/tests_pytorch/deprecated_api/test_remove_1-8.py @@ -20,7 +20,6 @@ import numpy as np import pytest import torch -from torch import optim import pytorch_lightning from pytorch_lightning import Callback, Trainer @@ -178,102 +177,6 @@ def test_v1_8_0_trainer_optimizers_mixin(): trainer.convert_to_lightning_optimizers() -def test_v1_8_0_deprecate_trainer_callback_hook_mixin(): - methods_with_self = [ - "on_before_accelerator_backend_setup", - "on_configure_sharded_model", - "on_init_start", - "on_init_end", - "on_fit_start", - "on_fit_end", - "on_sanity_check_start", - "on_sanity_check_end", - "on_train_epoch_start", - "on_train_epoch_end", - "on_validation_epoch_start", - "on_validation_epoch_end", - "on_test_epoch_start", - "on_test_epoch_end", - "on_predict_epoch_start", - "on_epoch_start", - "on_epoch_end", - "on_train_start", - "on_train_end", - "on_pretrain_routine_start", - "on_pretrain_routine_end", - "on_batch_start", - "on_batch_end", - "on_validation_start", - "on_validation_end", - "on_test_start", - "on_test_end", - "on_predict_start", - "on_predict_end", - "on_after_backward", - ] - methods_with_stage = [ - "setup", - "teardown", - ] - methods_with_batch_batch_idx_dataloader_idx = [ - "on_validation_batch_start", - "on_test_batch_start", - "on_predict_batch_start", - ] - methods_with_outputs_batch_batch_idx_dataloader_idx = [ - "on_validation_batch_end", - "on_test_batch_end", - "on_predict_batch_end", - ] - methods_with_checkpoint = ["on_save_checkpoint", "on_load_checkpoint"] - trainer = Trainer( - max_epochs=1, - limit_val_batches=0.1, - limit_train_batches=0.2, - enable_progress_bar=False, - logger=False, - ) - model = BoringModel() - # need to attach model to trainer for testing of `on_pretrain_routine_start` - trainer.strategy.connect(model) - for method_name in methods_with_self: - fn = getattr(trainer, method_name, None) - with pytest.deprecated_call(match="was deprecated in v1.6 and will be removed in v1.8"): - fn() - for method_name in methods_with_stage: - fn = getattr(trainer, method_name) - with pytest.deprecated_call(match="was deprecated in v1.6 and will be removed in v1.8"): - fn(stage="test") - for method_name in methods_with_batch_batch_idx_dataloader_idx: - fn = getattr(trainer, method_name) - with pytest.deprecated_call(match="was deprecated in v1.6 and will be removed in v1.8"): - fn(batch={}, batch_idx=0, dataloader_idx=0) - for method_name in methods_with_outputs_batch_batch_idx_dataloader_idx: - fn = getattr(trainer, method_name) - with pytest.deprecated_call(match="was deprecated in v1.6 and will be removed in v1.8"): - fn(outputs=torch.tensor([[1.0, -1.0], [1.0, -1.0]]), batch={}, batch_idx=0, dataloader_idx=0) - for method_name in methods_with_checkpoint: - fn = getattr(trainer, method_name) - with pytest.deprecated_call(match="was deprecated in v1.6 and will be removed in v1.8"): - fn(checkpoint={}) - with pytest.deprecated_call(match="was deprecated in v1.6 and will be removed in v1.8"): - trainer.on_train_batch_start(batch={}, batch_idx=0) - with pytest.deprecated_call(match="was deprecated in v1.6 and will be removed in v1.8"): - trainer.on_train_batch_end(outputs=torch.tensor([[1.0, -1.0], [1.0, -1.0]]), batch={}, batch_idx=0) - with pytest.deprecated_call(match="was deprecated in v1.6 and will be removed in v1.8"): - trainer.on_predict_epoch_end(outputs=torch.tensor([[1.0, -1.0], [1.0, -1.0]])) - with pytest.deprecated_call(match="was deprecated in v1.6 and will be removed in v1.8"): - trainer.on_exception(exception=Exception) - with pytest.deprecated_call(match="was deprecated in v1.6 and will be removed in v1.8"): - trainer.on_before_backward(loss=torch.tensor([[1.0, -1.0], [1.0, -1.0]])) - with pytest.deprecated_call(match="was deprecated in v1.6 and will be removed in v1.8"): - trainer.on_before_optimizer_step( - optimizer=optim.SGD(model.parameters(), lr=0.01, momentum=0.9), optimizer_idx=0 - ) - with pytest.deprecated_call(match="was deprecated in v1.6 and will be removed in v1.8"): - trainer.on_before_zero_grad(optimizer=optim.SGD(model.parameters(), lr=0.01, momentum=0.9)) - - def test_v1_8_0_deprecate_trainer_data_loading_mixin(): trainer = Trainer(max_epochs=1) model = BoringModel() From 2e46bcc3a8fb73d5f088eba9bc90cb4ed77f54f9 Mon Sep 17 00:00:00 2001 From: otaj Date: Fri, 26 Aug 2022 10:25:15 +0200 Subject: [PATCH 2/2] changelog --- src/pytorch_lightning/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pytorch_lightning/CHANGELOG.md b/src/pytorch_lightning/CHANGELOG.md index 394585c4179f7..90ec8a00ea163 100644 --- a/src/pytorch_lightning/CHANGELOG.md +++ b/src/pytorch_lightning/CHANGELOG.md @@ -73,7 +73,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Removed the experimental `pytorch_lightning.utiltiies.meta` functions in favor of built-in https://github.com/pytorch/torchdistx support ([#13868](https://github.com/Lightning-AI/lightning/pull/13868)) -- Removed the deprecated class `CallBackHookMixin` ([](https://github.com/Lightning-AI/lightning/)) +- Removed the deprecated class `TrainerCallbackHookMixin` ([#14401](https://github.com/Lightning-AI/lightning/14401)) ### Fixed