Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test pickling #1636

Merged
merged 6 commits into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions pytorch_lightning/callbacks/early_stopping.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def __init__(self, monitor: str = 'val_loss', min_delta: float = 0.0, patience:
self.min_delta = min_delta
self.wait = 0
self.stopped_epoch = 0
self.mode = mode

mode_dict = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't like that this mode_dict is defined in two places now..

'min': torch.lt,
Expand All @@ -67,9 +68,8 @@ def __init__(self, monitor: str = 'val_loss', min_delta: float = 0.0, patience:
if mode not in mode_dict:
if self.verbose > 0:
log.info(f'EarlyStopping mode {mode} is unknown, fallback to auto mode.')
mode = 'auto'
self.mode = 'auto'

self.monitor_op = mode_dict[mode]
self.min_delta *= 1 if self.monitor_op == torch.gt else -1

def _validate_condition_metric(self, logs):
Expand All @@ -94,6 +94,15 @@ def _validate_condition_metric(self, logs):

return True

@property
def monitor_op(self):
mode_dict = {
'min': torch.lt,
'max': torch.gt,
'auto': torch.gt if 'acc' in self.monitor else torch.lt
}
return mode_dict[self.mode]

def on_train_start(self, trainer, pl_module):
# Allow instances to be re-used
self.wait = 0
Expand Down
9 changes: 9 additions & 0 deletions tests/callbacks/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ def training_step(self, *args, **kwargs):
assert trainer.current_epoch < trainer.max_epochs


def test_pickling(tmpdir):
import pickle
early_stopping = EarlyStopping()
ckpt = ModelCheckpoint(tmpdir)

pickle.dumps(ckpt)
pickle.dumps(early_stopping)


def test_model_checkpoint_with_non_string_input(tmpdir):
""" Test that None in checkpoint callback is valid and that chkp_path is
set correctly """
Expand Down
3 changes: 3 additions & 0 deletions tests/loggers/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ def test_loggers_pickle(tmpdir, monkeypatch, logger_class):
logger_args = _get_logger_args(logger_class, tmpdir)
logger = logger_class(**logger_args)

# test pickling loggers
pickle.dumps(logger)

trainer = Trainer(
max_epochs=1,
logger=logger
Expand Down
7 changes: 7 additions & 0 deletions tests/trainer/test_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
)


def test_model_pickle(tmpdir):
import pickle

model = TestModelBase(tutils.get_default_hparams())
pickle.dumps(model)


def test_hparams_save_load(tmpdir):
model = DictHparamsModel({'in_features': 28 * 28, 'out_features': 10, 'failed_key': lambda x: x})

Expand Down
7 changes: 4 additions & 3 deletions tests/trainer/test_trainer_cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import inspect
from argparse import ArgumentParser, Namespace
from unittest import mock
import pickle

import pytest

Expand Down Expand Up @@ -42,14 +43,14 @@ def test_add_argparse_args_redefined(cli_args):

args = parser.parse_args(cli_args)

# make sure we can pickle args
pickle.dumps(args)

# Check few deprecated args are not in namespace:
for depr_name in ('gradient_clip', 'nb_gpu_nodes', 'max_nb_epochs'):
assert depr_name not in args

trainer = Trainer.from_argparse_args(args=args)

# make sure trainer can be pickled
import pickle
pickle.dumps(trainer)

assert isinstance(trainer, Trainer)
Expand Down