Skip to content

Commit

Permalink
Wandb bug/wandb multi (Lightning-AI#1360)
Browse files Browse the repository at this point in the history
* Allow reinits in sub procs

* Dont create an experiment on pickle, name, or project

* Comments consistency

* Fix test

* Apply suggestions from code review

Co-authored-by: Chris Van Pelt <vanpelt@gmail.com>
Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
  • Loading branch information
3 people authored and tullie committed May 6, 2020
1 parent 24274d7 commit 63ebf0d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 9 additions & 5 deletions pytorch_lightning/loggers/wandb.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ def __init__(self, name: Optional[str] = None, save_dir: Optional[str] = None,

def __getstate__(self):
state = self.__dict__.copy()
# args needed to reload correct experiment
state['_id'] = self._experiment.id if self._experiment is not None else None

# cannot be pickled
state['_experiment'] = None
# args needed to reload correct experiment
state['_id'] = self.experiment.id
return state

@property
Expand All @@ -87,7 +88,7 @@ def experiment(self) -> Run:
os.environ['WANDB_MODE'] = 'dryrun'
self._experiment = wandb.init(
name=self._name, dir=self._save_dir, project=self._project, anonymous=self._anonymous,
id=self._id, resume='allow', tags=self._tags, entity=self._entity)
reinit=True, id=self._id, resume='allow', tags=self._tags, entity=self._entity)
# save checkpoints in wandb dir to upload on W&B servers
if self._log_model:
self.save_dir = self._experiment.dir
Expand All @@ -109,8 +110,11 @@ def log_metrics(self, metrics: Dict[str, float], step: Optional[int] = None) ->

@property
def name(self) -> str:
return self.experiment.project_name()
# don't create an experiment if we don't have one
name = self._experiment.project_name() if self._experiment else None
return name

@property
def version(self) -> str:
return self.experiment.id
# don't create an experiment if we don't have one
return self._experiment.id if self._experiment else None
2 changes: 2 additions & 0 deletions tests/loggers/test_wandb.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class Experiment:
trainer_options = dict(max_epochs=1, logger=logger)

trainer = Trainer(**trainer_options)
# Access the experiment to ensure it's created
trainer.logger.experiment
pkl_bytes = pickle.dumps(trainer)
trainer2 = pickle.loads(pkl_bytes)

Expand Down

0 comments on commit 63ebf0d

Please sign in to comment.