Skip to content

Commit 307d77a

Browse files
jjenniferdaipre-commit-ci[bot]ananthsubawaelchlitchaton
authored andcommitted
Deprecate LightningLoggerBase.close (#9422)
* deprecate loggerbase.close * deprecate warning * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add to changelog * fix import * fix import alphabetize * spacing? * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * copy-paste avoid pre-commit.ci? * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * literally match the other comment * unindent * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * suggest finalize instead of save * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update tests/loggers/test_base.py * format but to be formatted * Update pytorch_lightning/loggers/base.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update pytorch_lightning/loggers/base.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update pytorch_lightning/loggers/base.py Co-authored-by: Jirka Borovec <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: ananthsub <[email protected]> Co-authored-by: Adrian Wälchli <[email protected]> Co-authored-by: thomas chaton <[email protected]> Co-authored-by: Jirka Borovec <[email protected]>
1 parent cae46f1 commit 307d77a

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
249249
- Deprecated passing `flush_logs_every_n_steps` as a Trainer argument, instead pass it to the logger init if supported ([#9366](https://github.com/PyTorchLightning/pytorch-lightning/pull/9366))
250250

251251

252+
- Deprecated `LightningLoggerBase.close`, `LoggerCollection.close` in favor of `LightningLoggerBase.finalize`, `LoggerCollection.finalize` ([#9422](https://github.com/PyTorchLightning/pytorch-lightning/pull/9422))
253+
254+
252255

253256
### Removed
254257

pytorch_lightning/loggers/base.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import pytorch_lightning as pl
2929
from pytorch_lightning.callbacks.model_checkpoint import ModelCheckpoint
3030
from pytorch_lightning.utilities import rank_zero_only
31+
from pytorch_lightning.utilities.warnings import rank_zero_deprecation
3132

3233

3334
def rank_zero_experiment(fn: Callable) -> Callable:
@@ -310,7 +311,18 @@ def finalize(self, status: str) -> None:
310311
self.save()
311312

312313
def close(self) -> None:
313-
"""Do any cleanup that is necessary to close an experiment."""
314+
"""Do any cleanup that is necessary to close an experiment.
315+
316+
See deprecation warning below.
317+
318+
.. deprecated:: v1.5
319+
This method is deprecated in v1.5 and will be removed in v1.7.
320+
Please use `LightningLoggerBase.finalize` instead.
321+
"""
322+
rank_zero_deprecation(
323+
"`LightningLoggerBase.close` method is deprecated in v1.5 and will be removed in v1.7."
324+
" Please use `LightningLoggerBase.finalize` instead."
325+
)
314326
self.save()
315327

316328
@property
@@ -392,6 +404,15 @@ def finalize(self, status: str) -> None:
392404
logger.finalize(status)
393405

394406
def close(self) -> None:
407+
"""
408+
.. deprecated:: v1.5
409+
This method is deprecated in v1.5 and will be removed in v1.7.
410+
Please use `LoggerCollection.finalize` instead.
411+
"""
412+
rank_zero_deprecation(
413+
"`LoggerCollection.close` method is deprecated in v1.5 and will be removed in v1.7."
414+
" Please use `LoggerCollection.finalize` instead."
415+
)
395416
for logger in self._logger_iterable:
396417
logger.close()
397418

tests/deprecated_api/test_remove_1-7.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
import torch
1919

2020
from pytorch_lightning import Callback, LightningDataModule, Trainer
21-
from pytorch_lightning.loggers import TestTubeLogger
21+
from pytorch_lightning.loggers import LoggerCollection, TestTubeLogger
2222
from tests.deprecated_api import _soft_unimport_module
2323
from tests.helpers import BoringModel
2424
from tests.helpers.datamodules import MNISTDataModule
2525
from tests.helpers.runif import RunIf
26+
from tests.loggers.test_base import CustomLogger
2627

2728

2829
def test_v1_7_0_deprecated_lightning_module_summarize(tmpdir):
@@ -224,3 +225,16 @@ def test_v1_7_0_deprecate_add_get_queue(tmpdir):
224225

225226
with pytest.deprecated_call(match=r"`LightningModule.get_from_queue` method was deprecated in v1.5"):
226227
trainer.fit(model)
228+
229+
230+
def test_v1_7_0_lightning_logger_base_close(tmpdir):
231+
logger = CustomLogger()
232+
with pytest.deprecated_call(
233+
match="`LightningLoggerBase.close` method is deprecated in v1.5 and will be removed in v1.7."
234+
):
235+
logger.close()
236+
with pytest.deprecated_call(
237+
match="`LoggerCollection.close` method is deprecated in v1.5 and will be removed in v1.7."
238+
):
239+
logger = LoggerCollection([logger])
240+
logger.close()

tests/loggers/test_base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ def test_logger_collection():
5151
mock1.agg_and_log_metrics.assert_called_once_with({"test": 2.0}, 4)
5252
mock2.agg_and_log_metrics.assert_called_once_with({"test": 2.0}, 4)
5353

54-
logger.close()
55-
mock1.close.assert_called_once()
56-
mock2.close.assert_called_once()
54+
logger.finalize("success")
55+
mock1.finalize.assert_called_once()
56+
mock2.finalize.assert_called_once()
5757

5858

5959
class CustomLogger(LightningLoggerBase):
@@ -211,7 +211,7 @@ def log_metrics(self, metrics, step):
211211
logger.agg_and_log_metrics({"loss": loss}, step=int(i / 5))
212212

213213
assert logger.history == {0: {"loss": 0.5623850983416314}}
214-
logger.close()
214+
logger.save()
215215
assert logger.history == {0: {"loss": 0.5623850983416314}, 1: {"loss": 0.4778883735637184}}
216216

217217

0 commit comments

Comments
 (0)