Skip to content

Commit

Permalink
Fixed error when using W&B project name from environment variables (#…
Browse files Browse the repository at this point in the history
…16222)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Jirka <jirka.borovec@seznam.cz>
Co-authored-by: Jirka Borovec <6035284+Borda@users.noreply.github.com>
Co-authored-by: Adrian Wälchli <aedu.waelchli@gmail.com>

(cherry picked from commit 4d5df5f)
  • Loading branch information
manangoel99 authored and lantiga committed Jun 2, 2023
1 parent fc71e98 commit 3d07b51
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/lightning/pytorch/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- 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))


- Fixed `WandbLogger` ignoring the `WANDB_PROJECT` environment variable ([#16222](https://github.com/Lightning-AI/lightning/pull/16222))


## [2.0.2] - 2023-04-24

### Fixed
Expand Down
7 changes: 5 additions & 2 deletions src/lightning/pytorch/loggers/wandb.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ def any_lightning_module_function_or_hook(self):
dir: Same as save_dir.
id: Same as version.
anonymous: Enables or explicitly disables anonymous logging.
project: The name of the project to which this run will belong.
project: The name of the project to which this run will belong. If not set, the environment variable
`WANDB_PROJECT` will be used as a fallback. If both are not set, it defaults to ``'lightning_logs'``.
log_model: Log checkpoints created by :class:`~lightning.pytorch.callbacks.ModelCheckpoint`
as W&B artifacts. `latest` and `best` aliases are automatically set.
Expand Down Expand Up @@ -293,7 +294,7 @@ def __init__(
dir: Optional[_PATH] = None,
id: Optional[str] = None,
anonymous: Optional[bool] = None,
project: str = "lightning_logs",
project: Optional[str] = None,
log_model: Union[str, bool] = False,
experiment: Union[Run, RunDisabled, None] = None,
prefix: str = "",
Expand Down Expand Up @@ -334,6 +335,8 @@ def __init__(
elif dir is not None:
dir = os.fspath(dir)

project = project or os.environ.get("WANDB_PROJECT", "lightning_logs")

# set wandb init arguments
self._wandb_init: Dict[str, Any] = {
"name": name,
Expand Down
14 changes: 12 additions & 2 deletions tests/tests_pytorch/loggers/test_wandb.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,20 @@
@mock.patch("lightning.pytorch.loggers.wandb.Run", new=mock.Mock)
@mock.patch("lightning.pytorch.loggers.wandb.wandb")
def test_wandb_project_name(*_):
logger = WandbLogger()
with mock.patch.dict(os.environ, {}):
logger = WandbLogger()
assert logger.name == "lightning_logs"

logger = WandbLogger(project="project")
with mock.patch.dict(os.environ, {}):
logger = WandbLogger(project="project")
assert logger.name == "project"

with mock.patch.dict(os.environ, {"WANDB_PROJECT": "env_project"}):
logger = WandbLogger()
assert logger.name == "env_project"

with mock.patch.dict(os.environ, {"WANDB_PROJECT": "env_project"}):
logger = WandbLogger(project="project")
assert logger.name == "project"


Expand Down

0 comments on commit 3d07b51

Please sign in to comment.