Skip to content

Commit 8704cc8

Browse files
awaelchliBorda
authored andcommitted
Error messages for removed Logger APIs (#15067)
Co-authored-by: Jirka Borovec <[email protected]>
1 parent 5eb97ef commit 8704cc8

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed

src/pytorch_lightning/_graveyard/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
# limitations under the License.
1414

1515
import pytorch_lightning._graveyard.callbacks
16+
import pytorch_lightning._graveyard.loggers
1617
import pytorch_lightning._graveyard.trainer
1718
import pytorch_lightning._graveyard.training_type # noqa: F401
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright The PyTorch Lightning team.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from typing import Any
16+
17+
import pytorch_lightning as pl
18+
from pytorch_lightning.loggers import Logger
19+
20+
21+
class LoggerCollection:
22+
# TODO: Remove in v2.0.0
23+
def __init__(self, _: Any):
24+
raise RuntimeError(
25+
"`LoggerCollection` was deprecated in v1.6 and removed in v1.8. Directly pass a list of loggers"
26+
" to the `Trainer` and access the list via the `trainer.loggers` attribute."
27+
)
28+
29+
30+
def _update_agg_funcs(logger: Logger, *__: Any, **___: Any) -> None:
31+
# TODO: Remove in v2.0.0
32+
raise NotImplementedError(
33+
f"`{type(logger).__name__}.update_agg_funcs` was deprecated in v1.6 and is no longer supported as of v1.8."
34+
)
35+
36+
37+
def _agg_and_log_metrics(logger: Logger, *__: Any, **___: Any) -> None:
38+
# TODO: Remove in v2.0.0
39+
raise NotImplementedError(
40+
f"`{type(logger).__name__}.update_agg_funcs` was deprecated in v1.6 and is no longer supported as of v1.8."
41+
)
42+
43+
44+
# Methods
45+
Logger.update_agg_funcs = _update_agg_funcs
46+
Logger.agg_and_log_metrics = _agg_and_log_metrics
47+
48+
# Classes
49+
pl.loggers.logger.LoggerCollection = LoggerCollection
50+
pl.loggers.base.LoggerCollection = LoggerCollection

src/pytorch_lightning/trainer/configuration_validator.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import pytorch_lightning as pl
1717
from lightning_lite.utilities.warnings import PossibleUserWarning
1818
from pytorch_lightning.accelerators.ipu import IPUAccelerator
19+
from pytorch_lightning.loggers import Logger
1920
from pytorch_lightning.strategies import DataParallelStrategy
2021
from pytorch_lightning.trainer.states import TrainerFn
2122
from pytorch_lightning.utilities.exceptions import MisconfigurationException
@@ -54,6 +55,8 @@ def verify_loop_configurations(trainer: "pl.Trainer") -> None:
5455
_check_on_epoch_start_end(model)
5556
# TODO: Delete this check in v2.0
5657
_check_on_pretrain_routine(model)
58+
# TODO: Delete this check in v2.0
59+
_check_deprecated_logger_methods(trainer)
5760

5861

5962
def __verify_train_val_loop_configuration(trainer: "pl.Trainer", model: "pl.LightningModule") -> None:
@@ -261,3 +264,17 @@ def _check_deprecated_callback_hooks(trainer: "pl.Trainer") -> None:
261264
raise RuntimeError(
262265
f"The `Callback.{hook}` hook was removed in v1.8. Please use `Callback.on_fit_start` instead."
263266
)
267+
268+
269+
def _check_deprecated_logger_methods(trainer: "pl.Trainer") -> None:
270+
for logger in trainer.loggers:
271+
if is_overridden(method_name="update_agg_funcs", instance=logger, parent=Logger):
272+
raise RuntimeError(
273+
f"`{type(logger).__name__}.update_agg_funcs` was deprecated in v1.6 and is no longer supported as of"
274+
" v1.8."
275+
)
276+
if is_overridden(method_name="agg_and_log_metrics", instance=logger, parent=Logger):
277+
raise RuntimeError(
278+
f"`{type(logger).__name__}.agg_and_log_metrics` was deprecated in v1.6 and is no longer supported as of"
279+
" v1.8."
280+
)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright The PyTorch Lightning team.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import pytest
15+
16+
from pytorch_lightning import Trainer
17+
from pytorch_lightning.demos.boring_classes import BoringModel
18+
from pytorch_lightning.loggers import CSVLogger
19+
20+
21+
def test_v2_0_0_unsupported_agg_and_log_metrics(tmpdir):
22+
class AggAndLogMetricsLogger(CSVLogger):
23+
def agg_and_log_metrics(self, metrics, step):
24+
pass
25+
26+
model = BoringModel()
27+
logger = AggAndLogMetricsLogger(tmpdir)
28+
29+
trainer = Trainer(logger=logger)
30+
31+
with pytest.raises(
32+
RuntimeError,
33+
match="`AggAndLogMetricsLogger.agg_and_log_metrics` was deprecated in v1.6 and is no longer supported",
34+
):
35+
trainer.fit(model)
36+
37+
38+
def test_v2_0_0_unsupported_update_agg_funcs(tmpdir):
39+
class UpdateAggFuncsLogger(CSVLogger):
40+
def update_agg_funcs(self, metrics, step):
41+
pass
42+
43+
model = BoringModel()
44+
logger = UpdateAggFuncsLogger(tmpdir)
45+
46+
trainer = Trainer(logger=logger)
47+
48+
with pytest.raises(
49+
RuntimeError,
50+
match="`UpdateAggFuncsLogger.update_agg_funcs` was deprecated in v1.6 and is no longer supported",
51+
):
52+
trainer.fit(model)
53+
54+
55+
def test_v2_0_0_unsupported_logger_collection_class():
56+
from pytorch_lightning.loggers.base import LoggerCollection
57+
58+
with pytest.raises(RuntimeError, match="`LoggerCollection` was deprecated in v1.6 and removed in v1.8."):
59+
LoggerCollection(None)
60+
61+
from pytorch_lightning.loggers.logger import LoggerCollection
62+
63+
with pytest.raises(RuntimeError, match="`LoggerCollection` was deprecated in v1.6 and removed in v1.8."):
64+
LoggerCollection(None)

0 commit comments

Comments
 (0)