Skip to content

Commit 61c28cb

Browse files
nitinramvelrajakihironittacarmoccaawaelchli
authored
Remove deprecated on_keyboard_interrupt (#13438)
Co-authored-by: Akihiro Nitta <[email protected]> Co-authored-by: Carlos Mocholí <[email protected]> Co-authored-by: Adrian Wälchli <[email protected]>
1 parent 61473c2 commit 61c28cb

File tree

12 files changed

+19
-75
lines changed

12 files changed

+19
-75
lines changed

docs/source-pytorch/extensions/callbacks.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,6 @@ on_predict_end
353353
.. automethod:: pytorch_lightning.callbacks.Callback.on_predict_end
354354
:noindex:
355355

356-
on_keyboard_interrupt
357-
^^^^^^^^^^^^^^^^^^^^^
358-
359-
.. automethod:: pytorch_lightning.callbacks.Callback.on_keyboard_interrupt
360-
:noindex:
361356

362357
on_exception
363358
^^^^^^^^^^^^

src/pytorch_lightning/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
269269
- Removed support for the `DDP2Strategy` ([#12705](https://github.com/PyTorchLightning/pytorch-lightning/pull/12705))
270270

271271

272+
- Removed deprecated `Callback.on_keyboard_interrupt` ([#13438](https://github.com/Lightning-AI/lightning/pull/13438))
273+
274+
272275
### Fixed
273276

274277

src/pytorch_lightning/callbacks/callback.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,6 @@ def on_predict_start(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule
272272
def on_predict_end(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
273273
"""Called when predict ends."""
274274

275-
def on_keyboard_interrupt(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule") -> None:
276-
r"""
277-
.. deprecated:: v1.5
278-
This callback hook was deprecated in v1.5 in favor of `on_exception` and will be removed in v1.7.
279-
280-
Called when any trainer execution is interrupted by KeyboardInterrupt.
281-
"""
282-
283275
def on_exception(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", exception: BaseException) -> None:
284276
"""Called when any trainer execution is interrupted by an exception."""
285277

src/pytorch_lightning/callbacks/lambda_function.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ def __init__(
7474
on_validation_end: Optional[Callable] = None,
7575
on_test_start: Optional[Callable] = None,
7676
on_test_end: Optional[Callable] = None,
77-
on_keyboard_interrupt: Optional[Callable] = None,
7877
on_exception: Optional[Callable] = None,
7978
on_save_checkpoint: Optional[Callable] = None,
8079
on_load_checkpoint: Optional[Callable] = None,

src/pytorch_lightning/trainer/callback_hook.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -553,16 +553,6 @@ def on_predict_end(self) -> None:
553553
for callback in self.callbacks:
554554
callback.on_predict_end(self, self.lightning_module)
555555

556-
def on_keyboard_interrupt(self):
557-
r"""
558-
.. deprecated:: v1.5
559-
This callback hook was deprecated in v1.5 in favor of `on_exception` and will be removed in v1.7.
560-
561-
Called when any trainer execution is interrupted by KeyboardInterrupt.
562-
"""
563-
for callback in self.callbacks:
564-
callback.on_keyboard_interrupt(self, self.lightning_module)
565-
566556
def on_exception(self, exception: BaseException) -> None:
567557
r"""
568558
.. deprecated:: v1.6

src/pytorch_lightning/trainer/configuration_validator.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,6 @@ def _check_on_pretrain_routine(model: "pl.LightningModule") -> None:
277277

278278
def _check_deprecated_callback_hooks(trainer: "pl.Trainer") -> None:
279279
for callback in trainer.callbacks:
280-
if is_overridden(method_name="on_keyboard_interrupt", instance=callback):
281-
rank_zero_deprecation(
282-
"The `on_keyboard_interrupt` callback hook was deprecated in v1.5 and will be removed in v1.7."
283-
" Please use the `on_exception` callback hook instead."
284-
)
285280
if is_overridden(method_name="on_init_start", instance=callback):
286281
rank_zero_deprecation(
287282
"The `on_init_start` callback hook was deprecated in v1.6 and will be removed in v1.8."

src/pytorch_lightning/trainer/connectors/logger_connector/fx_validator.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ class _LogOptions(TypedDict):
124124
),
125125
"on_predict_batch_start": None,
126126
"on_predict_batch_end": None,
127-
"on_keyboard_interrupt": None,
128127
"on_exception": None,
129128
"state_dict": None,
130129
"on_save_checkpoint": None,

src/pytorch_lightning/trainer/trainer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,13 +653,12 @@ def _call_and_handle_interrupt(self, trainer_fn: Callable, *args: Any, **kwargs:
653653
return self.strategy.launcher.launch(trainer_fn, *args, trainer=self, **kwargs)
654654
else:
655655
return trainer_fn(*args, **kwargs)
656-
# TODO: treat KeyboardInterrupt as BaseException (delete the code below) in v1.7
656+
# TODO(awaelchli): Unify both exceptions below, where `KeyboardError` doesn't re-raise
657657
except KeyboardInterrupt as exception:
658658
rank_zero_warn("Detected KeyboardInterrupt, attempting graceful shutdown...")
659659
# user could press Ctrl+c many times... only shutdown once
660660
if not self.interrupted:
661661
self.state.status = TrainerStatus.INTERRUPTED
662-
self._call_callback_hooks("on_keyboard_interrupt")
663662
self._call_callback_hooks("on_exception", exception)
664663
except BaseException as exception:
665664
self.state.status = TrainerStatus.INTERRUPTED

tests/tests_pytorch/callbacks/test_lambda_function.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ def call(hook, *_, **__):
4848
limit_val_batches=1,
4949
callbacks=[LambdaCallback(**hooks_args)],
5050
)
51-
with pytest.deprecated_call(match="on_keyboard_interrupt` callback hook was deprecated in v1.5"):
51+
with pytest.deprecated_call(
52+
match="`on_init_start` callback hook was deprecated in v1.6 and will be removed in v1.8."
53+
):
5254
trainer.fit(model)
53-
5455
ckpt_path = trainer.checkpoint_callback.best_model_path
5556

5657
# raises KeyboardInterrupt and loads from checkpoint
@@ -63,11 +64,17 @@ def call(hook, *_, **__):
6364
limit_predict_batches=1,
6465
callbacks=[LambdaCallback(**hooks_args)],
6566
)
66-
with pytest.deprecated_call(match="on_keyboard_interrupt` callback hook was deprecated in v1.5"):
67+
with pytest.deprecated_call(
68+
match="`on_init_start` callback hook was deprecated in v1.6 and will be removed in v1.8."
69+
):
6770
trainer.fit(model, ckpt_path=ckpt_path)
68-
with pytest.deprecated_call(match="on_keyboard_interrupt` callback hook was deprecated in v1.5"):
71+
with pytest.deprecated_call(
72+
match="`on_init_start` callback hook was deprecated in v1.6 and will be removed in v1.8."
73+
):
6974
trainer.test(model)
70-
with pytest.deprecated_call(match="on_keyboard_interrupt` callback hook was deprecated in v1.5"):
75+
with pytest.deprecated_call(
76+
match="`on_init_start` callback hook was deprecated in v1.6 and will be removed in v1.8."
77+
):
7178
trainer.predict(model)
7279

7380
assert checker == hooks

tests/tests_pytorch/deprecated_api/test_remove_1-7.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import pytest
2121
import torch
2222

23-
from pytorch_lightning import Callback, Trainer
23+
from pytorch_lightning import Trainer
2424
from pytorch_lightning.demos.boring_classes import BoringModel
2525
from pytorch_lightning.overrides.distributed import IndexBatchSamplerWrapper
2626
from pytorch_lightning.plugins.environments import (
@@ -35,29 +35,6 @@
3535
from tests_pytorch.plugins.environments.test_lsf_environment import _make_rankfile
3636

3737

38-
def test_v1_7_0_on_interrupt(tmpdir):
39-
class HandleInterruptCallback(Callback):
40-
def on_keyboard_interrupt(self, trainer, pl_module):
41-
print("keyboard interrupt")
42-
43-
model = BoringModel()
44-
handle_interrupt_callback = HandleInterruptCallback()
45-
46-
trainer = Trainer(
47-
callbacks=[handle_interrupt_callback],
48-
max_epochs=1,
49-
limit_val_batches=0.1,
50-
limit_train_batches=0.2,
51-
enable_progress_bar=False,
52-
logger=False,
53-
default_root_dir=tmpdir,
54-
)
55-
with pytest.deprecated_call(
56-
match="The `on_keyboard_interrupt` callback hook was deprecated in v1.5 and will be removed in v1.7"
57-
):
58-
trainer.fit(model)
59-
60-
6138
class BoringCallbackDDPSpawnModel(BoringModel):
6239
def add_to_queue(self, queue):
6340
...

0 commit comments

Comments
 (0)