Error messages for the remaining callback hooks (#15064)

This commit is contained in:
Adrian Wälchli 2022-10-11 15:18:47 -04:00 committed by GitHub
parent f1509537ec
commit fe32b39dbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 93 additions and 15 deletions

View File

@ -12,5 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import pytorch_lightning._graveyard.callbacks
import pytorch_lightning._graveyard.trainer
import pytorch_lightning._graveyard.training_type # noqa: F401

View File

@ -0,0 +1,30 @@
# 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 typing import Any
from pytorch_lightning.callbacks import ModelCheckpoint
def _save_checkpoint(_: ModelCheckpoint, __: Any) -> None:
# Remove in v2.0.0
raise NotImplementedError(
f"`{ModelCheckpoint.__name__}.save_checkpoint()` was deprecated in v1.6 and is no longer supported"
f" as of 1.8. Please use `trainer.save_checkpoint()` to manually save a checkpoint. This method will be"
f" removed completely in v2.0."
)
# Methods
ModelCheckpoint.save_checkpoint = _save_checkpoint

View File

@ -351,13 +351,6 @@ class ModelCheckpoint(Checkpoint):
self.best_model_path = state_dict["best_model_path"]
def save_checkpoint(self, trainer: "pl.Trainer") -> None:
raise NotImplementedError(
f"`{self.__class__.__name__}.save_checkpoint()` was deprecated in v1.6 and is no longer supported"
f" as of 1.8. Please use `trainer.save_checkpoint()` to manually save a checkpoint. This method will be"
f" removed completely in v2.0."
)
def _save_topk_checkpoint(self, trainer: "pl.Trainer", monitor_candidates: Dict[str, Tensor]) -> None:
if self.save_top_k == 0:
return

View File

@ -212,6 +212,14 @@ def _check_on_pretrain_routine(model: "pl.LightningModule") -> None:
def _check_deprecated_callback_hooks(trainer: "pl.Trainer") -> None:
for callback in trainer.callbacks:
if callable(getattr(callback, "on_init_start", None)):
raise RuntimeError(
"The `on_init_start` callback hook was deprecated in v1.6 and is no longer supported as of v1.8."
)
if callable(getattr(callback, "on_init_end", None)):
raise RuntimeError(
"The `on_init_end` callback hook was deprecated in v1.6 and is no longer supported as of v1.8."
)
if callable(getattr(callback, "on_configure_sharded_model", None)):
raise RuntimeError(
"The `on_configure_sharded_model` callback hook was removed in v1.8. Use `setup()` instead."

View File

@ -18,7 +18,6 @@ import pytest
import pytorch_lightning
from pytorch_lightning import Callback, Trainer
from pytorch_lightning.callbacks import ModelCheckpoint
from pytorch_lightning.demos.boring_classes import BoringModel
from tests_pytorch.callbacks.test_callbacks import OldStatefulCallback
from tests_pytorch.helpers.runif import RunIf
@ -292,11 +291,32 @@ def test_v2_0_0_callback_on_pretrain_routine_start_end(tmpdir):
trainer.fit(model)
def test_v2_0_0_deprecated_mc_save_checkpoint():
mc = ModelCheckpoint()
trainer = Trainer()
with mock.patch.object(trainer, "save_checkpoint"), pytest.raises(
NotImplementedError,
match=r"ModelCheckpoint.save_checkpoint\(\)` was deprecated in v1.6 and is no longer supported as of 1.8.",
class OnInitStartCallback(Callback):
def on_init_start(self, trainer):
print("Starting to init trainer!")
class OnInitEndCallback(Callback):
def on_init_end(self, trainer):
print("Trainer is init now")
@pytest.mark.parametrize("callback_class", [OnInitStartCallback, OnInitEndCallback])
def test_v2_0_0_unsupported_on_init_start_end(callback_class, tmpdir):
model = BoringModel()
trainer = Trainer(
callbacks=[callback_class()],
max_epochs=1,
fast_dev_run=True,
enable_progress_bar=False,
logger=False,
default_root_dir=tmpdir,
)
with pytest.raises(
RuntimeError, match="callback hook was deprecated in v1.6 and is no longer supported as of v1.8"
):
mc.save_checkpoint(trainer)
trainer.fit(model)
with pytest.raises(
RuntimeError, match="callback hook was deprecated in v1.6 and is no longer supported as of v1.8"
):
trainer.validate(model)

View File

@ -0,0 +1,26 @@
# 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.
import pytest
from pytorch_lightning.callbacks import ModelCheckpoint
def test_v2_0_0_deprecated_mc_save_checkpoint():
mc = ModelCheckpoint()
with pytest.raises(
NotImplementedError,
match=r"ModelCheckpoint.save_checkpoint\(\)` was deprecated in v1.6 and is no longer supported as of 1.8.",
):
mc.save_checkpoint(None)