Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom loggers without an experiment property #18093

Merged
merged 7 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/lightning/pytorch/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- `LightningCLI` not saving correctly `seed_everything` when `run=True` and `seed_everything=True` ([#18056](https://github.com/Lightning-AI/lightning/pull/18056))


- Fixed an issue that prevented the use of custom logger classes without an `experiment` property defined ([#18093](https://github.com/Lightning-AI/lightning/pull/18093))


## [2.0.5] - 2023-07-07

### Fixed
Expand Down
3 changes: 2 additions & 1 deletion src/lightning/pytorch/trainer/call.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def _call_setup_hook(trainer: "pl.Trainer") -> None:

# Trigger lazy creation of experiment in loggers so loggers have their metadata available
for logger in trainer.loggers:
_ = logger.experiment
if hasattr(logger, "experiment"):
carmocca marked this conversation as resolved.
Show resolved Hide resolved
_ = logger.experiment

trainer.strategy.barrier("pre_setup")

Expand Down
33 changes: 29 additions & 4 deletions tests/tests_pytorch/loggers/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
TensorBoardLogger,
WandbLogger,
)
from lightning.pytorch.loggers.logger import DummyExperiment
from lightning.pytorch.loggers.logger import DummyExperiment, Logger
from lightning.pytorch.loggers.tensorboard import _TENSORBOARD_AVAILABLE
from lightning.pytorch.tuner.tuning import Tuner
from tests_pytorch.helpers.runif import RunIf
Expand Down Expand Up @@ -236,7 +236,7 @@ def setup(self, trainer, pl_module, stage=None):
assert trainer.logger._mlflow_client
elif isinstance(trainer.logger, NeptuneLogger):
assert trainer.logger._run_instance
else:
elif hasattr(trainer.logger, "_experiment"):
assert trainer.logger._experiment


Expand All @@ -250,7 +250,29 @@ def on_train_batch_start(self, trainer, pl_module, batch, batch_idx):
assert pl_module.logger.experiment.something(foo="bar") is None


@pytest.mark.parametrize("logger_class", ALL_LOGGER_CLASSES_WO_NEPTUNE)
class CustomLoggerWithoutExperiment(Logger):
@property
def name(self):
return ""

@property
def version(self):
return None

def log_metrics(self, metrics, step=None) -> None:
pass

def log_hyperparams(self, params, *args, **kwargs) -> None:
pass


@pytest.mark.parametrize(
"logger_class",
(
*ALL_LOGGER_CLASSES_WO_NEPTUNE,
CustomLoggerWithoutExperiment,
),
)
@RunIf(skip_windows=True)
def test_logger_initialization(tmpdir, monkeypatch, logger_class):
"""Test that loggers get replaced by dummy loggers on global rank > 0 and that the experiment object is
Expand All @@ -265,6 +287,9 @@ def test_logger_initialization(tmpdir, monkeypatch, logger_class):
def _test_logger_initialization(tmpdir, logger_class):
logger_args = _get_logger_args(logger_class, tmpdir)
logger = logger_class(**logger_args)
callbacks = [LazyInitExperimentCheck()]
if not isinstance(logger, CustomLoggerWithoutExperiment):
callbacks.append(RankZeroLoggerCheck())
model = BoringModel()
trainer = Trainer(
logger=logger,
Expand All @@ -273,7 +298,7 @@ def _test_logger_initialization(tmpdir, logger_class):
accelerator="cpu",
devices=2,
max_steps=1,
callbacks=[RankZeroLoggerCheck(), LazyInitExperimentCheck()],
callbacks=callbacks,
)
trainer.fit(model)

Expand Down