Skip to content

Commit 4d5df5f

Browse files
manangoel99pre-commit-ci[bot]Bordaawaelchli
authored
Fixed error when using W&B project name from environment variables (Lightning-AI#16222)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Jirka <[email protected]> Co-authored-by: Jirka Borovec <[email protected]> Co-authored-by: Adrian Wälchli <[email protected]>
1 parent 61246c3 commit 4d5df5f

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

src/lightning/pytorch/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
126126
- Fixed a formatting issue when the filename in `ModelCheckpoint` contained metrics that were substrings of each other ([#17610](https://github.com/Lightning-AI/lightning/pull/17610))
127127

128128

129+
- Fixed `WandbLogger` ignoring the `WANDB_PROJECT` environment variable ([#16222](https://github.com/Lightning-AI/lightning/pull/16222))
130+
131+
129132
## [2.0.1.post0] - 2023-04-11
130133

131134
### Fixed

src/lightning/pytorch/loggers/wandb.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ def any_lightning_module_function_or_hook(self):
260260
dir: Same as save_dir.
261261
id: Same as version.
262262
anonymous: Enables or explicitly disables anonymous logging.
263-
project: The name of the project to which this run will belong.
263+
project: The name of the project to which this run will belong. If not set, the environment variable
264+
`WANDB_PROJECT` will be used as a fallback. If both are not set, it defaults to ``'lightning_logs'``.
264265
log_model: Log checkpoints created by :class:`~lightning.pytorch.callbacks.ModelCheckpoint`
265266
as W&B artifacts. `latest` and `best` aliases are automatically set.
266267
@@ -293,7 +294,7 @@ def __init__(
293294
dir: Optional[_PATH] = None,
294295
id: Optional[str] = None,
295296
anonymous: Optional[bool] = None,
296-
project: str = "lightning_logs",
297+
project: Optional[str] = None,
297298
log_model: Union[str, bool] = False,
298299
experiment: Union[Run, RunDisabled, None] = None,
299300
prefix: str = "",
@@ -334,6 +335,8 @@ def __init__(
334335
elif dir is not None:
335336
dir = os.fspath(dir)
336337

338+
project = project or os.environ.get("WANDB_PROJECT", "lightning_logs")
339+
337340
# set wandb init arguments
338341
self._wandb_init: Dict[str, Any] = {
339342
"name": name,

tests/tests_pytorch/loggers/test_wandb.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,20 @@
2929
@mock.patch("lightning.pytorch.loggers.wandb.Run", new=mock.Mock)
3030
@mock.patch("lightning.pytorch.loggers.wandb.wandb")
3131
def test_wandb_project_name(*_):
32-
logger = WandbLogger()
32+
with mock.patch.dict(os.environ, {}):
33+
logger = WandbLogger()
3334
assert logger.name == "lightning_logs"
3435

35-
logger = WandbLogger(project="project")
36+
with mock.patch.dict(os.environ, {}):
37+
logger = WandbLogger(project="project")
38+
assert logger.name == "project"
39+
40+
with mock.patch.dict(os.environ, {"WANDB_PROJECT": "env_project"}):
41+
logger = WandbLogger()
42+
assert logger.name == "env_project"
43+
44+
with mock.patch.dict(os.environ, {"WANDB_PROJECT": "env_project"}):
45+
logger = WandbLogger(project="project")
3646
assert logger.name == "project"
3747

3848

0 commit comments

Comments
 (0)