From f356c60c33533f6575230d7f26780af526c855e8 Mon Sep 17 00:00:00 2001 From: Boris Dayma Date: Mon, 4 Jan 2021 13:48:19 -0600 Subject: [PATCH 01/10] docs(wandb): add details to args --- pytorch_lightning/loggers/wandb.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pytorch_lightning/loggers/wandb.py b/pytorch_lightning/loggers/wandb.py index 0d147adee5ed4..d06843cca46f6 100644 --- a/pytorch_lightning/loggers/wandb.py +++ b/pytorch_lightning/loggers/wandb.py @@ -46,15 +46,15 @@ class WandbLogger(LightningLoggerBase): Args: name: Display name for the run. - save_dir: Path where data is saved. + save_dir: Path where data is saved (wandb dir by default). offline: Run offline (data can be streamed later to wandb servers). id: Sets the version, mainly used to resume a previous run. + version: Same as id. anonymous: Enables or explicitly disables anonymous logging. - version: Sets the version, mainly used to resume a previous run. project: The name of the project to which this run will belong. log_model: Save checkpoints in wandb dir to upload on W&B servers. - experiment: WandB experiment object. prefix: A string to put at the beginning of metric keys. + experiment: WandB experiment object. Automatically set when creating a run. \**kwargs: Additional arguments like `entity`, `group`, `tags`, etc. used by :func:`wandb.init` can be passed as keyword arguments in this logger. @@ -85,12 +85,12 @@ def __init__( save_dir: Optional[str] = None, offline: bool = False, id: Optional[str] = None, - anonymous: bool = False, version: Optional[str] = None, + anonymous: bool = False, project: Optional[str] = None, log_model: bool = False, - experiment=None, prefix: str = '', + experiment=None, **kwargs ): if wandb is None: From 5b0a769b4e87b6633ccc7b798efb6457b9cb927f Mon Sep 17 00:00:00 2001 From: Boris Dayma Date: Mon, 4 Jan 2021 13:59:51 -0600 Subject: [PATCH 02/10] feat(wandb): no sync between trainer and W&B steps --- pytorch_lightning/loggers/wandb.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/pytorch_lightning/loggers/wandb.py b/pytorch_lightning/loggers/wandb.py index d06843cca46f6..2189ddf99bb2e 100644 --- a/pytorch_lightning/loggers/wandb.py +++ b/pytorch_lightning/loggers/wandb.py @@ -54,6 +54,7 @@ class WandbLogger(LightningLoggerBase): project: The name of the project to which this run will belong. log_model: Save checkpoints in wandb dir to upload on W&B servers. prefix: A string to put at the beginning of metric keys. + sync_step: Sync Trainer step with wandb step. experiment: WandB experiment object. Automatically set when creating a run. \**kwargs: Additional arguments like `entity`, `group`, `tags`, etc. used by :func:`wandb.init` can be passed as keyword arguments in this logger. @@ -83,13 +84,14 @@ def __init__( self, name: Optional[str] = None, save_dir: Optional[str] = None, - offline: bool = False, + offline: Optional[bool] = False, id: Optional[str] = None, version: Optional[str] = None, - anonymous: bool = False, + anonymous: Optional[bool] = False, project: Optional[str] = None, - log_model: bool = False, - prefix: str = '', + log_model: Optional[bool] = False, + prefix: Optional[str] = '', + sync_step: Optional[bool] = True, experiment=None, **kwargs ): @@ -99,13 +101,14 @@ def __init__( super().__init__() self._name = name self._save_dir = save_dir - self._anonymous = 'allow' if anonymous else None + self._offline = offline self._id = version or id + self._anonymous = 'allow' if anonymous else None self._project = project - self._experiment = experiment - self._offline = offline self._log_model = log_model self._prefix = prefix + self._sync_step = sync_step + self._experiment = experiment self._kwargs = kwargs # logging multiple Trainer on a single W&B run (k-fold, resuming, etc) self._step_offset = 0 @@ -161,11 +164,14 @@ def log_metrics(self, metrics: Dict[str, float], step: Optional[int] = None) -> assert rank_zero_only.rank == 0, 'experiment tried to log from global_rank != 0' metrics = self._add_prefix(metrics) - if step is not None and step + self._step_offset < self.experiment.step: - self.warning_cache.warn( - 'Trying to log at a previous step. Use `commit=False` when logging metrics manually.' - ) - self.experiment.log(metrics, step=(step + self._step_offset) if step is not None else None) + if self._sync_step and step is not None and step + self._step_offset < self.experiment.step: + self.warning_cache.warn('Trying to log at a previous step. Use `sync_step=False` or try logging with `commit=False` when calling manually `wandb.log`.') + if self._sync_step: + self.experiment.log(metrics, step=(step + self._step_offset) if step is not None else None) + elif step is not None: + self.experiment.log({**metrics, 'trainer_step': (step + self._step_offset)}) + else: + self.experiment.log(metrics) @property def save_dir(self) -> Optional[str]: From 486c29b855500ec6e116d2810bf8d60f094ee627 Mon Sep 17 00:00:00 2001 From: Boris Dayma Date: Mon, 4 Jan 2021 14:35:10 -0600 Subject: [PATCH 03/10] style: pep8 --- pytorch_lightning/loggers/wandb.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pytorch_lightning/loggers/wandb.py b/pytorch_lightning/loggers/wandb.py index 2189ddf99bb2e..889c3584e8cbe 100644 --- a/pytorch_lightning/loggers/wandb.py +++ b/pytorch_lightning/loggers/wandb.py @@ -165,7 +165,8 @@ def log_metrics(self, metrics: Dict[str, float], step: Optional[int] = None) -> metrics = self._add_prefix(metrics) if self._sync_step and step is not None and step + self._step_offset < self.experiment.step: - self.warning_cache.warn('Trying to log at a previous step. Use `sync_step=False` or try logging with `commit=False` when calling manually `wandb.log`.') + self.warning_cache.warn( + 'Trying to log at a previous step. Use `sync_step=False` or try logging with `commit=False` when calling manually `wandb.log`.') if self._sync_step: self.experiment.log(metrics, step=(step + self._step_offset) if step is not None else None) elif step is not None: From 9f2a995bb262f02e5c225101929e5e9dd5b1ee80 Mon Sep 17 00:00:00 2001 From: Boris Dayma Date: Mon, 4 Jan 2021 15:59:37 -0600 Subject: [PATCH 04/10] tests(wandb): test sync_step --- tests/loggers/test_wandb.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/loggers/test_wandb.py b/tests/loggers/test_wandb.py index 398ee45ef4aa0..27f451d7dafbd 100644 --- a/tests/loggers/test_wandb.py +++ b/tests/loggers/test_wandb.py @@ -40,6 +40,18 @@ def test_wandb_logger_init(wandb, recwarn): wandb.init.assert_called_once() wandb.init().log.assert_called_once_with({'acc': 1.0}, step=None) + # test sync_step functionality + wandb.init().log.reset_mock() + wandb.init.reset_mock() + wandb.run = None + wandb.init().step = 0 + logger = WandbLogger(sync_step=False) + logger.log_metrics({'acc': 1.0}) + wandb.init().log.assert_called_once_with({'acc': 1.0}) + wandb.init().log.reset_mock() + logger.log_metrics({'acc': 1.0}, step=3) + wandb.init().log.assert_called_once_with({'acc': 1.0, 'trainer_step': 3}) + # mock wandb step wandb.init().step = 0 From 1ae326ad6fd8544d49dd8ba70d7b276e6b66a2d1 Mon Sep 17 00:00:00 2001 From: Boris Dayma Date: Mon, 4 Jan 2021 16:18:22 -0600 Subject: [PATCH 05/10] docs(wandb): add references --- pytorch_lightning/loggers/wandb.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pytorch_lightning/loggers/wandb.py b/pytorch_lightning/loggers/wandb.py index 889c3584e8cbe..70b0afd5bcc02 100644 --- a/pytorch_lightning/loggers/wandb.py +++ b/pytorch_lightning/loggers/wandb.py @@ -72,9 +72,9 @@ class WandbLogger(LightningLoggerBase): make sure to use `commit=False` so the logging step does not increase. See Also: - - `Tutorial `__ - on how to use W&B with Pytorch Lightning. + - `Tutorial `__ + on how to use W&B with Pytorch Lightning + - `W&B Documentation `__ """ From 223fcabef5df3481067c46c61fa8feac68a0dd84 Mon Sep 17 00:00:00 2001 From: Boris Dayma Date: Mon, 4 Jan 2021 16:53:17 -0600 Subject: [PATCH 06/10] docs(wandb): fix typo --- pytorch_lightning/loggers/wandb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pytorch_lightning/loggers/wandb.py b/pytorch_lightning/loggers/wandb.py index 70b0afd5bcc02..ca2182cb88043 100644 --- a/pytorch_lightning/loggers/wandb.py +++ b/pytorch_lightning/loggers/wandb.py @@ -59,7 +59,7 @@ class WandbLogger(LightningLoggerBase): \**kwargs: Additional arguments like `entity`, `group`, `tags`, etc. used by :func:`wandb.init` can be passed as keyword arguments in this logger. - Example:: + Example: .. code-block:: python From 121a8c06c6c990cf6b21332aa7babd30e96ca938 Mon Sep 17 00:00:00 2001 From: Boris Dayma Date: Tue, 5 Jan 2021 15:48:37 -0600 Subject: [PATCH 07/10] feat(wandb): more explicit warning --- pytorch_lightning/loggers/wandb.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pytorch_lightning/loggers/wandb.py b/pytorch_lightning/loggers/wandb.py index ca2182cb88043..c85579589e00a 100644 --- a/pytorch_lightning/loggers/wandb.py +++ b/pytorch_lightning/loggers/wandb.py @@ -166,7 +166,8 @@ def log_metrics(self, metrics: Dict[str, float], step: Optional[int] = None) -> metrics = self._add_prefix(metrics) if self._sync_step and step is not None and step + self._step_offset < self.experiment.step: self.warning_cache.warn( - 'Trying to log at a previous step. Use `sync_step=False` or try logging with `commit=False` when calling manually `wandb.log`.') + 'Trying to log at a previous step. ' + 'Use `WandbLogger(sync_step=False)` or try logging with `commit=False` when calling manually `wandb.log`.') if self._sync_step: self.experiment.log(metrics, step=(step + self._step_offset) if step is not None else None) elif step is not None: From e77cadd5cdf2844b53f21677b131acf92f0d0013 Mon Sep 17 00:00:00 2001 From: Boris Dayma Date: Thu, 7 Jan 2021 14:31:06 -0600 Subject: [PATCH 08/10] feat(wandb): order of args --- pytorch_lightning/loggers/wandb.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pytorch_lightning/loggers/wandb.py b/pytorch_lightning/loggers/wandb.py index c85579589e00a..fc20c80ef6165 100644 --- a/pytorch_lightning/loggers/wandb.py +++ b/pytorch_lightning/loggers/wandb.py @@ -86,13 +86,13 @@ def __init__( save_dir: Optional[str] = None, offline: Optional[bool] = False, id: Optional[str] = None, - version: Optional[str] = None, anonymous: Optional[bool] = False, + version: Optional[str] = None, project: Optional[str] = None, log_model: Optional[bool] = False, + experiment=None, prefix: Optional[str] = '', sync_step: Optional[bool] = True, - experiment=None, **kwargs ): if wandb is None: From 7b1b9e16b9a4e08e89421253eebcf3cd1d3ebf8a Mon Sep 17 00:00:00 2001 From: Boris Dayma Date: Fri, 8 Jan 2021 02:39:15 -0600 Subject: [PATCH 09/10] style: Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Adrian Wälchli --- pytorch_lightning/loggers/wandb.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pytorch_lightning/loggers/wandb.py b/pytorch_lightning/loggers/wandb.py index fc20c80ef6165..3e41c8c8a0ca7 100644 --- a/pytorch_lightning/loggers/wandb.py +++ b/pytorch_lightning/loggers/wandb.py @@ -73,7 +73,7 @@ class WandbLogger(LightningLoggerBase): See Also: - `Tutorial `__ - on how to use W&B with Pytorch Lightning + on how to use W&B with PyTorch Lightning - `W&B Documentation `__ """ @@ -166,8 +166,8 @@ def log_metrics(self, metrics: Dict[str, float], step: Optional[int] = None) -> metrics = self._add_prefix(metrics) if self._sync_step and step is not None and step + self._step_offset < self.experiment.step: self.warning_cache.warn( - 'Trying to log at a previous step. ' - 'Use `WandbLogger(sync_step=False)` or try logging with `commit=False` when calling manually `wandb.log`.') + 'Trying to log at a previous step.' + ' Use `WandbLogger(sync_step=False)` or try logging with `commit=False` when calling manually `wandb.log`.') if self._sync_step: self.experiment.log(metrics, step=(step + self._step_offset) if step is not None else None) elif step is not None: From e4a62451981ce573ed6206b195313a65d319477a Mon Sep 17 00:00:00 2001 From: Boris Dayma Date: Wed, 20 Jan 2021 07:55:37 -0600 Subject: [PATCH 10/10] style: long line --- pytorch_lightning/loggers/wandb.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pytorch_lightning/loggers/wandb.py b/pytorch_lightning/loggers/wandb.py index 1b01ae81b6a8f..247eb25bb5f43 100644 --- a/pytorch_lightning/loggers/wandb.py +++ b/pytorch_lightning/loggers/wandb.py @@ -168,8 +168,8 @@ def log_metrics(self, metrics: Dict[str, float], step: Optional[int] = None) -> metrics = self._add_prefix(metrics) if self._sync_step and step is not None and step + self._step_offset < self.experiment.step: self.warning_cache.warn( - 'Trying to log at a previous step.' - ' Use `WandbLogger(sync_step=False)` or try logging with `commit=False` when calling manually `wandb.log`.') + 'Trying to log at a previous step. Use `WandbLogger(sync_step=False)`' + ' or try logging with `commit=False` when calling manually `wandb.log`.') if self._sync_step: self.experiment.log(metrics, step=(step + self._step_offset) if step is not None else None) elif step is not None: