2020-03-03 01:49:14 +00:00
|
|
|
import os
|
|
|
|
import pickle
|
2020-06-29 01:36:46 +00:00
|
|
|
from unittest import mock
|
2020-03-03 01:49:14 +00:00
|
|
|
|
|
|
|
from pytorch_lightning import Trainer
|
|
|
|
from pytorch_lightning.loggers import WandbLogger
|
2020-07-09 11:15:41 +00:00
|
|
|
from tests.base import EvalModelTemplate
|
2020-03-03 01:49:14 +00:00
|
|
|
|
|
|
|
|
2020-06-29 01:36:46 +00:00
|
|
|
@mock.patch('pytorch_lightning.loggers.wandb.wandb')
|
2020-03-03 01:49:14 +00:00
|
|
|
def test_wandb_logger(wandb):
|
|
|
|
"""Verify that basic functionality of wandb logger works.
|
|
|
|
Wandb doesn't work well with pytest so we have to mock it out here."""
|
|
|
|
logger = WandbLogger(anonymous=True, offline=True)
|
|
|
|
|
|
|
|
logger.log_metrics({'acc': 1.0})
|
2020-06-02 22:46:02 +00:00
|
|
|
wandb.init().log.assert_called_once_with({'acc': 1.0})
|
2020-03-03 01:49:14 +00:00
|
|
|
|
|
|
|
wandb.init().log.reset_mock()
|
|
|
|
logger.log_metrics({'acc': 1.0}, step=3)
|
2020-06-02 22:46:02 +00:00
|
|
|
wandb.init().log.assert_called_once_with({'global_step': 3, 'acc': 1.0})
|
2020-03-03 01:49:14 +00:00
|
|
|
|
2020-07-08 05:45:25 +00:00
|
|
|
logger.log_hyperparams({'test': None, 'nested': {'a': 1}, 'b': [2, 3, 4]})
|
|
|
|
wandb.init().config.update.assert_called_once_with(
|
|
|
|
{'test': 'None', 'nested/a': 1, 'b': [2, 3, 4]},
|
|
|
|
allow_val_change=True,
|
|
|
|
)
|
2020-07-09 22:36:36 +00:00
|
|
|
|
2020-03-03 01:49:14 +00:00
|
|
|
logger.watch('model', 'log', 10)
|
2020-04-03 19:02:38 +00:00
|
|
|
wandb.init().watch.assert_called_once_with('model', log='log', log_freq=10)
|
2020-03-03 01:49:14 +00:00
|
|
|
|
|
|
|
assert logger.name == wandb.init().project_name()
|
|
|
|
assert logger.version == wandb.init().id
|
|
|
|
|
|
|
|
|
2020-06-29 01:36:46 +00:00
|
|
|
@mock.patch('pytorch_lightning.loggers.wandb.wandb')
|
|
|
|
def test_wandb_pickle(wandb, tmpdir):
|
2020-07-09 11:15:41 +00:00
|
|
|
"""
|
|
|
|
Verify that pickling trainer with wandb logger works.
|
2020-04-15 00:32:33 +00:00
|
|
|
Wandb doesn't work well with pytest so we have to mock it out here.
|
|
|
|
"""
|
2020-03-03 01:49:14 +00:00
|
|
|
class Experiment:
|
2020-07-09 11:15:41 +00:00
|
|
|
""" """
|
2020-03-03 01:49:14 +00:00
|
|
|
id = 'the_id'
|
|
|
|
|
2020-06-29 01:36:46 +00:00
|
|
|
def project_name(self):
|
|
|
|
return 'the_project_name'
|
|
|
|
|
2020-03-03 01:49:14 +00:00
|
|
|
wandb.init.return_value = Experiment()
|
|
|
|
logger = WandbLogger(id='the_id', offline=True)
|
|
|
|
|
2020-06-29 01:36:46 +00:00
|
|
|
trainer = Trainer(
|
|
|
|
default_root_dir=tmpdir,
|
|
|
|
max_epochs=1,
|
|
|
|
logger=logger,
|
|
|
|
)
|
2020-04-03 19:03:00 +00:00
|
|
|
# Access the experiment to ensure it's created
|
2020-05-01 14:43:58 +00:00
|
|
|
assert trainer.logger.experiment, 'missing experiment'
|
2020-03-03 01:49:14 +00:00
|
|
|
pkl_bytes = pickle.dumps(trainer)
|
|
|
|
trainer2 = pickle.loads(pkl_bytes)
|
|
|
|
|
|
|
|
assert os.environ['WANDB_MODE'] == 'dryrun'
|
|
|
|
assert trainer2.logger.__class__.__name__ == WandbLogger.__name__
|
2020-05-01 14:43:58 +00:00
|
|
|
assert trainer2.logger.experiment, 'missing experiment'
|
2020-03-03 01:49:14 +00:00
|
|
|
|
|
|
|
wandb.init.assert_called()
|
|
|
|
assert 'id' in wandb.init.call_args[1]
|
|
|
|
assert wandb.init.call_args[1]['id'] == 'the_id'
|
|
|
|
|
|
|
|
del os.environ['WANDB_MODE']
|
2020-07-09 11:15:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock.patch('pytorch_lightning.loggers.wandb.wandb')
|
|
|
|
def test_wandb_logger_dirs_creation(wandb, tmpdir):
|
|
|
|
""" Test that the logger creates the folders and files in the right place. """
|
|
|
|
logger = WandbLogger(save_dir=str(tmpdir), offline=True)
|
|
|
|
assert logger.version is None
|
|
|
|
assert logger.name is None
|
|
|
|
|
|
|
|
# mock return values of experiment
|
|
|
|
logger.experiment.id = '1'
|
|
|
|
logger.experiment.project_name.return_value = 'project'
|
|
|
|
|
|
|
|
for _ in range(2):
|
|
|
|
_ = logger.experiment
|
|
|
|
|
|
|
|
assert logger.version == '1'
|
|
|
|
assert logger.name == 'project'
|
|
|
|
assert str(tmpdir) == logger.save_dir
|
|
|
|
assert not os.listdir(tmpdir)
|
|
|
|
|
|
|
|
version = logger.version
|
|
|
|
model = EvalModelTemplate()
|
|
|
|
trainer = Trainer(default_root_dir=tmpdir, logger=logger, max_epochs=1, limit_val_batches=3)
|
|
|
|
trainer.fit(model)
|
|
|
|
|
2020-07-27 16:53:11 +00:00
|
|
|
assert trainer.checkpoint_callback.dirpath == str(tmpdir / 'project' / version / 'checkpoints')
|
|
|
|
assert set(os.listdir(trainer.checkpoint_callback.dirpath)) == {'epoch=0.ckpt'}
|