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 all 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed to avoid common hook warning if no hook is overridden ([#12131](https://github.com/PyTorchLightning/pytorch-lightning/pull/12131))


- Fixed the case where logger=None is passed to the Trainer ([#12249](https://github.com/PyTorchLightning/pytorch-lightning/pull/12249))


## [1.5.10] - 2022-02-08

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ def should_update_logs(self) -> bool:
return should_log 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
26 changes: 20 additions & 6 deletions tests/trainer/properties/test_loggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ def test_trainer_loggers_property():
assert trainer.logger == logger1
assert trainer.loggers == [logger1]

# trainer.loggers should be an empty list
trainer = Trainer(logger=False)

assert trainer.logger is None
assert trainer.loggers == []

# trainer.loggers should be a list of size 1 holding the default logger
trainer = Trainer(logger=True)

Expand Down Expand Up @@ -95,3 +89,23 @@ def test_trainer_loggers_setters():
trainer.loggers = None
assert trainer.loggers == []
assert trainer.logger is None


@pytest.mark.parametrize(
"logger_value",
[
None,
False,
[],
],
)
def test_no_logger(tmpdir, logger_value):
"""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