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

Fix the case where logger=None is passed to Trainer #12249

Merged
merged 8 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,14 @@ def should_update_logs(self) -> bool:
return should_log_every_n_steps or self.trainer.should_stop

def configure_logger(self, logger: Union[bool, LightningLoggerBase, Iterable[LightningLoggerBase]]) -> None:
if isinstance(logger, bool):
if not logger:
# logger is None or logger is False
self.trainer.loggers = []
elif logger is True:
# default logger
self.trainer.loggers = (
[TensorBoardLogger(save_dir=self.trainer.default_root_dir, version=SLURMEnvironment.job_id())]
if logger
else []
)
self.trainer.loggers = [
TensorBoardLogger(save_dir=self.trainer.default_root_dir, version=SLURMEnvironment.job_id())
]
elif isinstance(logger, Iterable):
self.trainer.loggers = list(logger)
else:
Expand Down
20 changes: 20 additions & 0 deletions tests/loggers/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,26 @@ def _test_logger_created_on_rank_zero_only(tmpdir, logger_class):
assert trainer.state.finished, f"Training failed with {trainer.state}"


@pytest.mark.parametrize(
"logger_value",
[
None,
False,
[],
],
)
def test_no_logger(tmpdir, logger_value):
daniellepintz marked this conversation as resolved.
Show resolved Hide resolved
"""Test the cases where logger=None, logger=False, logger=[] are passed to Trainer."""
trainer = Trainer(
logger=logger_value,
default_root_dir=tmpdir,
max_steps=1,
)
assert trainer.logger is None
assert trainer.loggers == []
assert trainer.log_dir == tmpdir


def test_logger_with_prefix_all(tmpdir, monkeypatch):
"""Test that prefix is added at the beginning of the metric keys."""
prefix = "tmp"
Expand Down