2021-06-14 12:20:01 +00:00
|
|
|
# Copyright The PyTorch Lightning team.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2021-02-13 00:27:44 +00:00
|
|
|
import logging
|
2021-02-03 21:40:57 +00:00
|
|
|
from unittest.mock import Mock
|
|
|
|
|
|
|
|
import torch
|
|
|
|
|
2021-02-05 23:33:12 +00:00
|
|
|
from pytorch_lightning import Callback, Trainer
|
2021-02-13 00:27:44 +00:00
|
|
|
from pytorch_lightning.callbacks import (
|
|
|
|
EarlyStopping,
|
|
|
|
GradientAccumulationScheduler,
|
|
|
|
LearningRateMonitor,
|
|
|
|
ModelCheckpoint,
|
|
|
|
ProgressBar,
|
|
|
|
)
|
|
|
|
from pytorch_lightning.trainer.connectors.callback_connector import CallbackConnector
|
2021-02-09 10:10:52 +00:00
|
|
|
from tests.helpers import BoringModel
|
2021-02-03 21:40:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_checkpoint_callbacks_are_last(tmpdir):
|
2021-07-26 11:37:35 +00:00
|
|
|
"""Test that checkpoint callbacks always get moved to the end of the list, with preserved order."""
|
2021-02-03 21:40:57 +00:00
|
|
|
checkpoint1 = ModelCheckpoint(tmpdir)
|
|
|
|
checkpoint2 = ModelCheckpoint(tmpdir)
|
2021-02-13 00:27:44 +00:00
|
|
|
early_stopping = EarlyStopping()
|
2021-02-03 21:40:57 +00:00
|
|
|
lr_monitor = LearningRateMonitor()
|
|
|
|
progress_bar = ProgressBar()
|
|
|
|
|
2021-02-13 00:27:44 +00:00
|
|
|
# no model callbacks
|
2021-02-03 21:40:57 +00:00
|
|
|
model = Mock()
|
|
|
|
model.configure_callbacks.return_value = []
|
|
|
|
trainer = Trainer(callbacks=[checkpoint1, progress_bar, lr_monitor, checkpoint2])
|
2021-08-04 15:43:34 +00:00
|
|
|
trainer.model = model
|
2021-02-13 00:27:44 +00:00
|
|
|
cb_connector = CallbackConnector(trainer)
|
2021-08-04 15:43:34 +00:00
|
|
|
cb_connector._attach_model_callbacks()
|
2021-02-03 21:40:57 +00:00
|
|
|
assert trainer.callbacks == [progress_bar, lr_monitor, checkpoint1, checkpoint2]
|
|
|
|
|
2021-02-13 00:27:44 +00:00
|
|
|
# with model-specific callbacks that substitute ones in Trainer
|
|
|
|
model = Mock()
|
|
|
|
model.configure_callbacks.return_value = [checkpoint1, early_stopping, checkpoint2]
|
|
|
|
trainer = Trainer(callbacks=[progress_bar, lr_monitor, ModelCheckpoint(tmpdir)])
|
2021-08-04 15:43:34 +00:00
|
|
|
trainer.model = model
|
2021-02-13 00:27:44 +00:00
|
|
|
cb_connector = CallbackConnector(trainer)
|
2021-08-04 15:43:34 +00:00
|
|
|
cb_connector._attach_model_callbacks()
|
2021-02-13 00:27:44 +00:00
|
|
|
assert trainer.callbacks == [progress_bar, lr_monitor, early_stopping, checkpoint1, checkpoint2]
|
|
|
|
|
2021-02-03 21:40:57 +00:00
|
|
|
|
|
|
|
class StatefulCallback0(Callback):
|
2021-02-25 15:48:19 +00:00
|
|
|
def on_save_checkpoint(self, *args):
|
2021-02-03 21:40:57 +00:00
|
|
|
return {"content0": 0}
|
|
|
|
|
|
|
|
|
|
|
|
class StatefulCallback1(Callback):
|
2021-02-25 15:48:19 +00:00
|
|
|
def on_save_checkpoint(self, *args):
|
2021-02-03 21:40:57 +00:00
|
|
|
return {"content1": 1}
|
|
|
|
|
|
|
|
|
|
|
|
def test_all_callback_states_saved_before_checkpoint_callback(tmpdir):
|
2021-07-26 11:37:35 +00:00
|
|
|
"""Test that all callback states get saved even if the ModelCheckpoint is not given as last."""
|
2021-02-03 21:40:57 +00:00
|
|
|
|
|
|
|
callback0 = StatefulCallback0()
|
|
|
|
callback1 = StatefulCallback1()
|
|
|
|
checkpoint_callback = ModelCheckpoint(dirpath=tmpdir, filename="all_states")
|
|
|
|
model = BoringModel()
|
|
|
|
trainer = Trainer(
|
2021-07-26 11:37:35 +00:00
|
|
|
default_root_dir=tmpdir, max_steps=1, limit_val_batches=1, callbacks=[callback0, checkpoint_callback, callback1]
|
2021-02-03 21:40:57 +00:00
|
|
|
)
|
|
|
|
trainer.fit(model)
|
|
|
|
|
|
|
|
ckpt = torch.load(str(tmpdir / "all_states.ckpt"))
|
2021-07-28 22:12:32 +00:00
|
|
|
state0 = ckpt["callbacks"]["StatefulCallback0"]
|
|
|
|
state1 = ckpt["callbacks"]["StatefulCallback1"]
|
2021-02-03 21:40:57 +00:00
|
|
|
assert "content0" in state0 and state0["content0"] == 0
|
|
|
|
assert "content1" in state1 and state1["content1"] == 1
|
2021-07-28 22:12:32 +00:00
|
|
|
assert "ModelCheckpoint" in ckpt["callbacks"]
|
2021-02-13 00:27:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_attach_model_callbacks():
|
2021-07-26 11:37:35 +00:00
|
|
|
"""Test that the callbacks defined in the model and through Trainer get merged correctly."""
|
2021-02-13 00:27:44 +00:00
|
|
|
|
|
|
|
def assert_composition(trainer_callbacks, model_callbacks, expected):
|
|
|
|
model = Mock()
|
|
|
|
model.configure_callbacks.return_value = model_callbacks
|
|
|
|
trainer = Trainer(checkpoint_callback=False, progress_bar_refresh_rate=0, callbacks=trainer_callbacks)
|
2021-08-04 15:43:34 +00:00
|
|
|
trainer.model = model
|
2021-02-13 00:27:44 +00:00
|
|
|
cb_connector = CallbackConnector(trainer)
|
2021-08-04 15:43:34 +00:00
|
|
|
cb_connector._attach_model_callbacks()
|
2021-02-13 00:27:44 +00:00
|
|
|
assert trainer.callbacks == expected
|
|
|
|
|
|
|
|
early_stopping = EarlyStopping()
|
|
|
|
progress_bar = ProgressBar()
|
|
|
|
lr_monitor = LearningRateMonitor()
|
|
|
|
grad_accumulation = GradientAccumulationScheduler({1: 1})
|
|
|
|
|
|
|
|
# no callbacks
|
|
|
|
assert_composition(trainer_callbacks=[], model_callbacks=[], expected=[])
|
|
|
|
|
|
|
|
# callbacks of different types
|
|
|
|
assert_composition(
|
|
|
|
trainer_callbacks=[early_stopping], model_callbacks=[progress_bar], expected=[early_stopping, progress_bar]
|
|
|
|
)
|
|
|
|
|
|
|
|
# same callback type twice, different instance
|
|
|
|
assert_composition(
|
|
|
|
trainer_callbacks=[progress_bar, EarlyStopping()],
|
|
|
|
model_callbacks=[early_stopping],
|
2021-07-26 11:37:35 +00:00
|
|
|
expected=[progress_bar, early_stopping],
|
2021-02-13 00:27:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# multiple callbacks of the same type in trainer
|
|
|
|
assert_composition(
|
2021-07-26 11:37:35 +00:00
|
|
|
trainer_callbacks=[LearningRateMonitor(), EarlyStopping(), LearningRateMonitor(), EarlyStopping()],
|
2021-02-13 00:27:44 +00:00
|
|
|
model_callbacks=[early_stopping, lr_monitor],
|
2021-07-26 11:37:35 +00:00
|
|
|
expected=[early_stopping, lr_monitor],
|
2021-02-13 00:27:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# multiple callbacks of the same type, in both trainer and model
|
|
|
|
assert_composition(
|
|
|
|
trainer_callbacks=[
|
2021-07-26 11:37:35 +00:00
|
|
|
LearningRateMonitor(),
|
|
|
|
progress_bar,
|
2021-02-13 00:27:44 +00:00
|
|
|
EarlyStopping(),
|
|
|
|
LearningRateMonitor(),
|
2021-07-26 11:37:35 +00:00
|
|
|
EarlyStopping(),
|
2021-02-13 00:27:44 +00:00
|
|
|
],
|
|
|
|
model_callbacks=[early_stopping, lr_monitor, grad_accumulation, early_stopping],
|
2021-07-26 11:37:35 +00:00
|
|
|
expected=[progress_bar, early_stopping, lr_monitor, grad_accumulation, early_stopping],
|
2021-02-13 00:27:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_attach_model_callbacks_override_info(caplog):
|
2021-07-26 11:37:35 +00:00
|
|
|
"""Test that the logs contain the info about overriding callbacks returned by configure_callbacks."""
|
2021-02-13 00:27:44 +00:00
|
|
|
model = Mock()
|
|
|
|
model.configure_callbacks.return_value = [LearningRateMonitor(), EarlyStopping()]
|
|
|
|
trainer = Trainer(checkpoint_callback=False, callbacks=[EarlyStopping(), LearningRateMonitor(), ProgressBar()])
|
2021-08-04 15:43:34 +00:00
|
|
|
trainer.model = model
|
2021-02-13 00:27:44 +00:00
|
|
|
cb_connector = CallbackConnector(trainer)
|
|
|
|
with caplog.at_level(logging.INFO):
|
2021-08-04 15:43:34 +00:00
|
|
|
cb_connector._attach_model_callbacks()
|
2021-02-13 00:27:44 +00:00
|
|
|
|
|
|
|
assert "existing callbacks passed to Trainer: EarlyStopping, LearningRateMonitor" in caplog.text
|