From 02426d65cb254c713ccd64ac623786472695cb46 Mon Sep 17 00:00:00 2001 From: nitinramvelraj Date: Thu, 23 Jun 2022 07:32:14 -0700 Subject: [PATCH 01/10] remove on_keyboard_interrupt --- src/lightning_app/utilities/introspection.py | 1 - src/pytorch_lightning/callbacks/callback.py | 8 -------- src/pytorch_lightning/callbacks/lambda_function.py | 1 - src/pytorch_lightning/trainer/callback_hook.py | 10 ---------- .../trainer/configuration_validator.py | 5 ----- .../connectors/logger_connector/fx_validator.py | 1 - src/pytorch_lightning/trainer/trainer.py | 1 - 7 files changed, 27 deletions(-) diff --git a/src/lightning_app/utilities/introspection.py b/src/lightning_app/utilities/introspection.py index d31d0f896f642..03f160a5ab47e 100644 --- a/src/lightning_app/utilities/introspection.py +++ b/src/lightning_app/utilities/introspection.py @@ -200,7 +200,6 @@ class LightningCallbackVisitor(LightningVisitor): "on_validation_end", "on_test_start", "on_test_end", - "on_keyboard_interrupt", "on_save_checkpoint", "on_load_checkpoint", } diff --git a/src/pytorch_lightning/callbacks/callback.py b/src/pytorch_lightning/callbacks/callback.py index ed106076e41a7..892bd0fdfbf8b 100644 --- a/src/pytorch_lightning/callbacks/callback.py +++ b/src/pytorch_lightning/callbacks/callback.py @@ -272,14 +272,6 @@ def on_predict_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule def on_predict_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: """Called when predict ends.""" - def on_keyboard_interrupt(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None: - r""" - .. deprecated:: v1.5 - This callback hook was deprecated in v1.5 in favor of `on_exception` and will be removed in v1.7. - - Called when any trainer execution is interrupted by KeyboardInterrupt. - """ - def on_exception(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", exception: BaseException) -> None: """Called when any trainer execution is interrupted by an exception.""" diff --git a/src/pytorch_lightning/callbacks/lambda_function.py b/src/pytorch_lightning/callbacks/lambda_function.py index b9fc8f01385b9..a37122cb2aa04 100644 --- a/src/pytorch_lightning/callbacks/lambda_function.py +++ b/src/pytorch_lightning/callbacks/lambda_function.py @@ -74,7 +74,6 @@ def __init__( on_validation_end: Optional[Callable] = None, on_test_start: Optional[Callable] = None, on_test_end: Optional[Callable] = None, - on_keyboard_interrupt: Optional[Callable] = None, on_exception: Optional[Callable] = None, on_save_checkpoint: Optional[Callable] = None, on_load_checkpoint: Optional[Callable] = None, diff --git a/src/pytorch_lightning/trainer/callback_hook.py b/src/pytorch_lightning/trainer/callback_hook.py index 904cbc872376f..1e455b3424606 100644 --- a/src/pytorch_lightning/trainer/callback_hook.py +++ b/src/pytorch_lightning/trainer/callback_hook.py @@ -553,16 +553,6 @@ def on_predict_end(self) -> None: for callback in self.callbacks: callback.on_predict_end(self, self.lightning_module) - def on_keyboard_interrupt(self): - r""" - .. deprecated:: v1.5 - This callback hook was deprecated in v1.5 in favor of `on_exception` and will be removed in v1.7. - - Called when any trainer execution is interrupted by KeyboardInterrupt. - """ - for callback in self.callbacks: - callback.on_keyboard_interrupt(self, self.lightning_module) - def on_exception(self, exception: BaseException) -> None: r""" .. deprecated:: v1.6 diff --git a/src/pytorch_lightning/trainer/configuration_validator.py b/src/pytorch_lightning/trainer/configuration_validator.py index f9820fe7d1d21..9a4288ce7b184 100644 --- a/src/pytorch_lightning/trainer/configuration_validator.py +++ b/src/pytorch_lightning/trainer/configuration_validator.py @@ -274,11 +274,6 @@ def _check_on_pretrain_routine(model: "pl.LightningModule") -> None: def _check_deprecated_callback_hooks(trainer: "pl.Trainer") -> None: for callback in trainer.callbacks: - if is_overridden(method_name="on_keyboard_interrupt", instance=callback): - rank_zero_deprecation( - "The `on_keyboard_interrupt` callback hook was deprecated in v1.5 and will be removed in v1.7." - " Please use the `on_exception` callback hook instead." - ) if is_overridden(method_name="on_init_start", instance=callback): rank_zero_deprecation( "The `on_init_start` callback hook was deprecated in v1.6 and will be removed in v1.8." diff --git a/src/pytorch_lightning/trainer/connectors/logger_connector/fx_validator.py b/src/pytorch_lightning/trainer/connectors/logger_connector/fx_validator.py index c3cc2885f407a..6f60ba6f1aa2f 100644 --- a/src/pytorch_lightning/trainer/connectors/logger_connector/fx_validator.py +++ b/src/pytorch_lightning/trainer/connectors/logger_connector/fx_validator.py @@ -124,7 +124,6 @@ class _LogOptions(TypedDict): ), "on_predict_batch_start": None, "on_predict_batch_end": None, - "on_keyboard_interrupt": None, "on_exception": None, "state_dict": None, "on_save_checkpoint": None, diff --git a/src/pytorch_lightning/trainer/trainer.py b/src/pytorch_lightning/trainer/trainer.py index 85de7acbe352a..b9c0a84cf97b8 100644 --- a/src/pytorch_lightning/trainer/trainer.py +++ b/src/pytorch_lightning/trainer/trainer.py @@ -649,7 +649,6 @@ def _call_and_handle_interrupt(self, trainer_fn: Callable, *args: Any, **kwargs: # user could press Ctrl+c many times... only shutdown once if not self.interrupted: self.state.status = TrainerStatus.INTERRUPTED - self._call_callback_hooks("on_keyboard_interrupt") self._call_callback_hooks("on_exception", exception) except BaseException as exception: self.state.status = TrainerStatus.INTERRUPTED From 16cbc44f2f3dc079999d53e97d0225fb1b71bf56 Mon Sep 17 00:00:00 2001 From: nitinramvelraj Date: Thu, 23 Jun 2022 07:47:45 -0700 Subject: [PATCH 02/10] removed on_keyboard_interrupt from tests; some commented not sure --- .../callbacks/test_lambda_function.py | 8 ---- .../deprecated_api/test_remove_1-7.py | 38 +++++++++---------- .../trainer/logging_/test_logger_connector.py | 1 - tests/tests_pytorch/trainer/test_states.py | 32 ++++++++-------- tests/tests_pytorch/trainer/test_trainer.py | 10 +---- 5 files changed, 34 insertions(+), 55 deletions(-) diff --git a/tests/tests_pytorch/callbacks/test_lambda_function.py b/tests/tests_pytorch/callbacks/test_lambda_function.py index df71295566095..ec438b8b3642a 100644 --- a/tests/tests_pytorch/callbacks/test_lambda_function.py +++ b/tests/tests_pytorch/callbacks/test_lambda_function.py @@ -48,8 +48,6 @@ def call(hook, *_, **__): limit_val_batches=1, callbacks=[LambdaCallback(**hooks_args)], ) - with pytest.deprecated_call(match="on_keyboard_interrupt` callback hook was deprecated in v1.5"): - trainer.fit(model) ckpt_path = trainer.checkpoint_callback.best_model_path @@ -63,11 +61,5 @@ def call(hook, *_, **__): limit_predict_batches=1, callbacks=[LambdaCallback(**hooks_args)], ) - with pytest.deprecated_call(match="on_keyboard_interrupt` callback hook was deprecated in v1.5"): - trainer.fit(model, ckpt_path=ckpt_path) - with pytest.deprecated_call(match="on_keyboard_interrupt` callback hook was deprecated in v1.5"): - trainer.test(model) - with pytest.deprecated_call(match="on_keyboard_interrupt` callback hook was deprecated in v1.5"): - trainer.predict(model) assert checker == hooks diff --git a/tests/tests_pytorch/deprecated_api/test_remove_1-7.py b/tests/tests_pytorch/deprecated_api/test_remove_1-7.py index 55030c14ec5b3..b4bc59061b9bf 100644 --- a/tests/tests_pytorch/deprecated_api/test_remove_1-7.py +++ b/tests/tests_pytorch/deprecated_api/test_remove_1-7.py @@ -36,27 +36,23 @@ from tests_pytorch.plugins.environments.test_lsf_environment import _make_rankfile -def test_v1_7_0_on_interrupt(tmpdir): - class HandleInterruptCallback(Callback): - def on_keyboard_interrupt(self, trainer, pl_module): - print("keyboard interrupt") - - model = BoringModel() - handle_interrupt_callback = HandleInterruptCallback() - - trainer = Trainer( - callbacks=[handle_interrupt_callback], - max_epochs=1, - limit_val_batches=0.1, - limit_train_batches=0.2, - enable_progress_bar=False, - logger=False, - default_root_dir=tmpdir, - ) - with pytest.deprecated_call( - match="The `on_keyboard_interrupt` callback hook was deprecated in v1.5 and will be removed in v1.7" - ): - trainer.fit(model) +# def test_v1_7_0_on_interrupt(tmpdir): +# class HandleInterruptCallback(Callback): +# def on_keyboard_interrupt(self, trainer, pl_module): +# print("keyboard interrupt") + +# model = BoringModel() +# handle_interrupt_callback = HandleInterruptCallback() + +# trainer = Trainer( +# callbacks=[handle_interrupt_callback], +# max_epochs=1, +# limit_val_batches=0.1, +# limit_train_batches=0.2, +# enable_progress_bar=False, +# logger=False, +# default_root_dir=tmpdir, +# ) class BoringCallbackDDPSpawnModel(BoringModel): diff --git a/tests/tests_pytorch/trainer/logging_/test_logger_connector.py b/tests/tests_pytorch/trainer/logging_/test_logger_connector.py index 681c4a7732f3d..2370ca96ad7cb 100644 --- a/tests/tests_pytorch/trainer/logging_/test_logger_connector.py +++ b/tests/tests_pytorch/trainer/logging_/test_logger_connector.py @@ -48,7 +48,6 @@ def test_fx_validator(): "on_fit_start", "on_init_end", "on_init_start", - "on_keyboard_interrupt", "on_exception", "on_load_checkpoint", "load_state_dict", diff --git a/tests/tests_pytorch/trainer/test_states.py b/tests/tests_pytorch/trainer/test_states.py index 25bd16ee0402c..8a7460e2c81be 100644 --- a/tests/tests_pytorch/trainer/test_states.py +++ b/tests/tests_pytorch/trainer/test_states.py @@ -75,19 +75,19 @@ def on_test_batch_start(self, *_): assert trainer.state.finished -@pytest.mark.parametrize( - "extra_params", - [pytest.param(dict(fast_dev_run=True), id="Fast-Run"), pytest.param(dict(max_steps=1), id="Single-Step")], -) -def test_interrupt_state_on_keyboard_interrupt(tmpdir, extra_params): - """Tests that state is set to INTERRUPTED on KeyboardInterrupt.""" - model = BoringModel() - - class InterruptCallback(Callback): - def on_train_batch_start(self, trainer, pl_module, batch, batch_idx): - raise KeyboardInterrupt - - trainer = Trainer(callbacks=[InterruptCallback()], default_root_dir=tmpdir, **extra_params) - - trainer.fit(model) - assert trainer.interrupted +# @pytest.mark.parametrize( +# "extra_params", +# [pytest.param(dict(fast_dev_run=True), id="Fast-Run"), pytest.param(dict(max_steps=1), id="Single-Step")], +# ) +# def test_interrupt_state_on_keyboard_interrupt(tmpdir, extra_params): +# """Tests that state is set to INTERRUPTED on KeyboardInterrupt.""" +# model = BoringModel() + +# class InterruptCallback(Callback): +# def on_train_batch_start(self, trainer, pl_module, batch, batch_idx): +# raise KeyboardInterrupt + +# trainer = Trainer(callbacks=[InterruptCallback()], default_root_dir=tmpdir, **extra_params) + +# trainer.fit(model) +# assert trainer.interrupted diff --git a/tests/tests_pytorch/trainer/test_trainer.py b/tests/tests_pytorch/trainer/test_trainer.py index 3069b589bb448..0f741aba6176b 100644 --- a/tests/tests_pytorch/trainer/test_trainer.py +++ b/tests/tests_pytorch/trainer/test_trainer.py @@ -1014,9 +1014,6 @@ def __init__(self): def on_exception(self, trainer, pl_module, exception): self.exception = exception - def on_keyboard_interrupt(self, trainer, pl_module): - self.exc_info = sys.exc_info() - interrupt_callback = InterruptCallback() handle_interrupt_callback = HandleInterruptCallback() @@ -1032,15 +1029,10 @@ def on_keyboard_interrupt(self, trainer, pl_module): assert not trainer.interrupted assert handle_interrupt_callback.exception is None assert handle_interrupt_callback.exc_info is None - with pytest.deprecated_call(match="on_keyboard_interrupt` callback hook was deprecated in v1.5"): - trainer.fit(model) assert trainer.interrupted assert isinstance(handle_interrupt_callback.exception, KeyboardInterrupt) assert isinstance(handle_interrupt_callback.exc_info[1], KeyboardInterrupt) - with pytest.raises(MisconfigurationException), pytest.deprecated_call( - match="on_keyboard_interrupt` callback hook was deprecated in v1.5" - ): - trainer.test(model) + assert trainer.interrupted assert isinstance(handle_interrupt_callback.exception, MisconfigurationException) From fed26d010bf6f6aa7872dad143f6bc4b2ecb6157 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 28 Jun 2022 19:29:25 +0000 Subject: [PATCH 03/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/tests_pytorch/deprecated_api/test_remove_1-7.py | 1 - tests/tests_pytorch/trainer/test_trainer.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/tests_pytorch/deprecated_api/test_remove_1-7.py b/tests/tests_pytorch/deprecated_api/test_remove_1-7.py index b4bc59061b9bf..571a610f16ee1 100644 --- a/tests/tests_pytorch/deprecated_api/test_remove_1-7.py +++ b/tests/tests_pytorch/deprecated_api/test_remove_1-7.py @@ -35,7 +35,6 @@ from tests_pytorch.deprecated_api import _soft_unimport_module from tests_pytorch.plugins.environments.test_lsf_environment import _make_rankfile - # def test_v1_7_0_on_interrupt(tmpdir): # class HandleInterruptCallback(Callback): # def on_keyboard_interrupt(self, trainer, pl_module): diff --git a/tests/tests_pytorch/trainer/test_trainer.py b/tests/tests_pytorch/trainer/test_trainer.py index 0f741aba6176b..edc716d58873d 100644 --- a/tests/tests_pytorch/trainer/test_trainer.py +++ b/tests/tests_pytorch/trainer/test_trainer.py @@ -1032,7 +1032,7 @@ def on_exception(self, trainer, pl_module, exception): assert trainer.interrupted assert isinstance(handle_interrupt_callback.exception, KeyboardInterrupt) assert isinstance(handle_interrupt_callback.exc_info[1], KeyboardInterrupt) - + assert trainer.interrupted assert isinstance(handle_interrupt_callback.exception, MisconfigurationException) From 1de1928929efa65d5f660c875116f3b79af9a9e9 Mon Sep 17 00:00:00 2001 From: nitinramvelraj Date: Tue, 28 Jun 2022 12:45:26 -0700 Subject: [PATCH 04/10] removed callback import --- docs/source-pytorch/extensions/callbacks.rst | 5 ----- .../deprecated_api/test_remove_1-7.py | 21 +------------------ .../trainer/logging_/test_logger_connector.py | 1 - 3 files changed, 1 insertion(+), 26 deletions(-) diff --git a/docs/source-pytorch/extensions/callbacks.rst b/docs/source-pytorch/extensions/callbacks.rst index 57342e7dd62d5..72f02fadb6af6 100644 --- a/docs/source-pytorch/extensions/callbacks.rst +++ b/docs/source-pytorch/extensions/callbacks.rst @@ -353,11 +353,6 @@ on_predict_end .. automethod:: pytorch_lightning.callbacks.Callback.on_predict_end :noindex: -on_keyboard_interrupt -^^^^^^^^^^^^^^^^^^^^^ - -.. automethod:: pytorch_lightning.callbacks.Callback.on_keyboard_interrupt - :noindex: on_exception ^^^^^^^^^^^^ diff --git a/tests/tests_pytorch/deprecated_api/test_remove_1-7.py b/tests/tests_pytorch/deprecated_api/test_remove_1-7.py index b4bc59061b9bf..586152ea9f82d 100644 --- a/tests/tests_pytorch/deprecated_api/test_remove_1-7.py +++ b/tests/tests_pytorch/deprecated_api/test_remove_1-7.py @@ -20,7 +20,7 @@ import pytest import torch -from pytorch_lightning import Callback, Trainer +from pytorch_lightning import Trainer from pytorch_lightning.callbacks.lr_monitor import LearningRateMonitor from pytorch_lightning.demos.boring_classes import BoringModel from pytorch_lightning.overrides.distributed import IndexBatchSamplerWrapper @@ -36,25 +36,6 @@ from tests_pytorch.plugins.environments.test_lsf_environment import _make_rankfile -# def test_v1_7_0_on_interrupt(tmpdir): -# class HandleInterruptCallback(Callback): -# def on_keyboard_interrupt(self, trainer, pl_module): -# print("keyboard interrupt") - -# model = BoringModel() -# handle_interrupt_callback = HandleInterruptCallback() - -# trainer = Trainer( -# callbacks=[handle_interrupt_callback], -# max_epochs=1, -# limit_val_batches=0.1, -# limit_train_batches=0.2, -# enable_progress_bar=False, -# logger=False, -# default_root_dir=tmpdir, -# ) - - class BoringCallbackDDPSpawnModel(BoringModel): def add_to_queue(self, queue): ... diff --git a/tests/tests_pytorch/trainer/logging_/test_logger_connector.py b/tests/tests_pytorch/trainer/logging_/test_logger_connector.py index 2370ca96ad7cb..760e8eea2a85c 100644 --- a/tests/tests_pytorch/trainer/logging_/test_logger_connector.py +++ b/tests/tests_pytorch/trainer/logging_/test_logger_connector.py @@ -92,7 +92,6 @@ def test_fx_validator(): "on_configure_sharded_model", "on_init_end", "on_init_start", - "on_keyboard_interrupt", "on_exception", "on_load_checkpoint", "load_state_dict", From 6f0d138c5da1ff7887090bf54c6a985b2115aee8 Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Thu, 30 Jun 2022 00:11:12 +0900 Subject: [PATCH 05/10] Revert test change --- tests/tests_pytorch/trainer/test_states.py | 32 +++++++++++----------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/tests/tests_pytorch/trainer/test_states.py b/tests/tests_pytorch/trainer/test_states.py index 8a7460e2c81be..25bd16ee0402c 100644 --- a/tests/tests_pytorch/trainer/test_states.py +++ b/tests/tests_pytorch/trainer/test_states.py @@ -75,19 +75,19 @@ def on_test_batch_start(self, *_): assert trainer.state.finished -# @pytest.mark.parametrize( -# "extra_params", -# [pytest.param(dict(fast_dev_run=True), id="Fast-Run"), pytest.param(dict(max_steps=1), id="Single-Step")], -# ) -# def test_interrupt_state_on_keyboard_interrupt(tmpdir, extra_params): -# """Tests that state is set to INTERRUPTED on KeyboardInterrupt.""" -# model = BoringModel() - -# class InterruptCallback(Callback): -# def on_train_batch_start(self, trainer, pl_module, batch, batch_idx): -# raise KeyboardInterrupt - -# trainer = Trainer(callbacks=[InterruptCallback()], default_root_dir=tmpdir, **extra_params) - -# trainer.fit(model) -# assert trainer.interrupted +@pytest.mark.parametrize( + "extra_params", + [pytest.param(dict(fast_dev_run=True), id="Fast-Run"), pytest.param(dict(max_steps=1), id="Single-Step")], +) +def test_interrupt_state_on_keyboard_interrupt(tmpdir, extra_params): + """Tests that state is set to INTERRUPTED on KeyboardInterrupt.""" + model = BoringModel() + + class InterruptCallback(Callback): + def on_train_batch_start(self, trainer, pl_module, batch, batch_idx): + raise KeyboardInterrupt + + trainer = Trainer(callbacks=[InterruptCallback()], default_root_dir=tmpdir, **extra_params) + + trainer.fit(model) + assert trainer.interrupted From 9e873b8240f604662728b21e2cd34d3f2c75afb9 Mon Sep 17 00:00:00 2001 From: nitinramvelraj Date: Wed, 29 Jun 2022 08:53:59 -0700 Subject: [PATCH 06/10] changelog entry for removing on keyboard interrupt --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 463a5f41c1e70..faa42e1239c3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3678,3 +3678,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). ## [0.2.x] - 2019-07-09 ## [0.1.x] - 2019-06-DD + +### Removed + +- Removed the `on_keyboard_interrupt` method from `Callback` From 4abf875a9005adf61cc3a3d9d4b21a35f635c56e Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Thu, 30 Jun 2022 01:17:31 +0900 Subject: [PATCH 07/10] Update CHANGELOG --- src/pytorch_lightning/CHANGELOG.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/pytorch_lightning/CHANGELOG.md b/src/pytorch_lightning/CHANGELOG.md index b892b1df50e33..db217d9ca26ef 100644 --- a/src/pytorch_lightning/CHANGELOG.md +++ b/src/pytorch_lightning/CHANGELOG.md @@ -262,6 +262,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Removed support for the `DDP2Strategy` ([#12705](https://github.com/PyTorchLightning/pytorch-lightning/pull/12705)) +- Removed deprecated `Callback.on_keyboard_interrupt` ([#13438](https://github.com/Lightning-AI/lightning/pull/13438)) + + ### Fixed @@ -3720,7 +3723,3 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). ## [0.2.x] - 2019-07-09 ## [0.1.x] - 2019-06-DD - -### Removed - -- Removed the `on_keyboard_interrupt` method from `Callback` From a1901684d7fc33cccc0396d467b9aa16ec7d4bc6 Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Thu, 30 Jun 2022 01:36:18 +0900 Subject: [PATCH 08/10] Fix tests --- .../callbacks/test_lambda_function.py | 17 ++++++++++++++++- tests/tests_pytorch/trainer/test_trainer.py | 8 +++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/tests/tests_pytorch/callbacks/test_lambda_function.py b/tests/tests_pytorch/callbacks/test_lambda_function.py index ec438b8b3642a..08fec1ebd2efb 100644 --- a/tests/tests_pytorch/callbacks/test_lambda_function.py +++ b/tests/tests_pytorch/callbacks/test_lambda_function.py @@ -48,7 +48,10 @@ def call(hook, *_, **__): limit_val_batches=1, callbacks=[LambdaCallback(**hooks_args)], ) - + with pytest.deprecated_call( + match="`on_init_start` callback hook was deprecated in v1.6 and will be removed in v1.8." + ): + trainer.fit(model) ckpt_path = trainer.checkpoint_callback.best_model_path # raises KeyboardInterrupt and loads from checkpoint @@ -61,5 +64,17 @@ def call(hook, *_, **__): limit_predict_batches=1, callbacks=[LambdaCallback(**hooks_args)], ) + with pytest.deprecated_call( + match="`on_init_start` callback hook was deprecated in v1.6 and will be removed in v1.8." + ): + trainer.fit(model, ckpt_path=ckpt_path) + with pytest.deprecated_call( + match="`on_init_start` callback hook was deprecated in v1.6 and will be removed in v1.8." + ): + trainer.test(model) + with pytest.deprecated_call( + match="`on_init_start` callback hook was deprecated in v1.6 and will be removed in v1.8." + ): + trainer.predict(model) assert checker == hooks diff --git a/tests/tests_pytorch/trainer/test_trainer.py b/tests/tests_pytorch/trainer/test_trainer.py index b10c439ca10a0..a0d20fc58ed1c 100644 --- a/tests/tests_pytorch/trainer/test_trainer.py +++ b/tests/tests_pytorch/trainer/test_trainer.py @@ -16,7 +16,6 @@ import math import os import pickle -import sys from argparse import Namespace from contextlib import nullcontext from copy import deepcopy @@ -1013,7 +1012,6 @@ class HandleInterruptCallback(Callback): def __init__(self): super().__init__() self.exception = None - self.exc_info = None def on_exception(self, trainer, pl_module, exception): self.exception = exception @@ -1032,11 +1030,11 @@ def on_exception(self, trainer, pl_module, exception): ) assert not trainer.interrupted assert handle_interrupt_callback.exception is None - assert handle_interrupt_callback.exc_info is None + trainer.fit(model) assert trainer.interrupted assert isinstance(handle_interrupt_callback.exception, KeyboardInterrupt) - assert isinstance(handle_interrupt_callback.exc_info[1], KeyboardInterrupt) - + with pytest.raises(MisconfigurationException): + trainer.test(model) assert trainer.interrupted assert isinstance(handle_interrupt_callback.exception, MisconfigurationException) From 82a2a17cc5018ed38e414de0f46bc0e09cebda51 Mon Sep 17 00:00:00 2001 From: Akihiro Nitta Date: Thu, 30 Jun 2022 04:54:16 +0900 Subject: [PATCH 09/10] Revert change in lightning_app --- src/lightning_app/utilities/introspection.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lightning_app/utilities/introspection.py b/src/lightning_app/utilities/introspection.py index 03f160a5ab47e..d31d0f896f642 100644 --- a/src/lightning_app/utilities/introspection.py +++ b/src/lightning_app/utilities/introspection.py @@ -200,6 +200,7 @@ class LightningCallbackVisitor(LightningVisitor): "on_validation_end", "on_test_start", "on_test_end", + "on_keyboard_interrupt", "on_save_checkpoint", "on_load_checkpoint", } From 6d31a957a5c51e54373dd51712cd03e952009507 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mochol=C3=AD?= Date: Fri, 1 Jul 2022 13:25:55 +0200 Subject: [PATCH 10/10] Update TODO --- src/pytorch_lightning/trainer/trainer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pytorch_lightning/trainer/trainer.py b/src/pytorch_lightning/trainer/trainer.py index a036890fe1fc4..846126777dc05 100644 --- a/src/pytorch_lightning/trainer/trainer.py +++ b/src/pytorch_lightning/trainer/trainer.py @@ -653,7 +653,7 @@ def _call_and_handle_interrupt(self, trainer_fn: Callable, *args: Any, **kwargs: return self.strategy.launcher.launch(trainer_fn, *args, trainer=self, **kwargs) else: return trainer_fn(*args, **kwargs) - # TODO: treat KeyboardInterrupt as BaseException (delete the code below) in v1.7 + # TODO(awaelchli): Unify both exceptions below, where `KeyboardError` doesn't re-raise except KeyboardInterrupt as exception: rank_zero_warn("Detected KeyboardInterrupt, attempting graceful shutdown...") # user could press Ctrl+c many times... only shutdown once