From a4ac8608a5ef74aabc1846204ebdfe7dfa65ffce Mon Sep 17 00:00:00 2001 From: Jirka Borovec Date: Mon, 7 Dec 2020 23:54:22 +0100 Subject: [PATCH 1/3] drop usage of deprecated checkpoint_callback --- tests/backends/test_tpu_backend.py | 7 ++++++- tests/callbacks/test_early_stopping.py | 3 +-- .../test_checkpoint_callback_frequency.py | 2 +- tests/checkpointing/test_model_checkpoint.py | 4 ++-- tests/core/test_datamodules.py | 2 +- tests/models/data/horovod/train_default_model.py | 2 +- tests/models/test_amp.py | 2 +- tests/models/test_cpu.py | 8 ++++---- tests/models/test_restore.py | 14 +++++++------- .../logging_tests/test_eval_loop_logging_1_0.py | 5 +++-- .../logging_tests/test_train_loop_logging_1_0.py | 3 ++- tests/trainer/test_trainer.py | 12 ++++++------ 12 files changed, 35 insertions(+), 29 deletions(-) diff --git a/tests/backends/test_tpu_backend.py b/tests/backends/test_tpu_backend.py index cb8ffce38913a..63729f86ce862 100644 --- a/tests/backends/test_tpu_backend.py +++ b/tests/backends/test_tpu_backend.py @@ -39,7 +39,12 @@ def test_resume_training_on_cpu(tmpdir): assert weight_tensor.device == torch.device("cpu") # Verify that training is resumed on CPU - trainer = Trainer(resume_from_checkpoint=model_path, checkpoint_callback=True, max_epochs=1, default_root_dir=tmpdir) + trainer = Trainer( + resume_from_checkpoint=model_path, + checkpoint_callback=True, + max_epochs=1, + default_root_dir=tmpdir, + ) result = trainer.fit(model) assert result == 1 diff --git a/tests/callbacks/test_early_stopping.py b/tests/callbacks/test_early_stopping.py index b7711e8aae3fe..7cecefad03276 100644 --- a/tests/callbacks/test_early_stopping.py +++ b/tests/callbacks/test_early_stopping.py @@ -56,8 +56,7 @@ def test_resume_early_stopping_from_checkpoint(tmpdir): early_stop_callback = EarlyStoppingTestRestore() trainer = Trainer( default_root_dir=tmpdir, - checkpoint_callback=checkpoint_callback, - callbacks=[early_stop_callback], + callbacks=[early_stop_callback, checkpoint_callback], num_sanity_val_steps=0, max_epochs=4, ) diff --git a/tests/checkpointing/test_checkpoint_callback_frequency.py b/tests/checkpointing/test_checkpoint_callback_frequency.py index 0662cf7677431..857877f8239ba 100644 --- a/tests/checkpointing/test_checkpoint_callback_frequency.py +++ b/tests/checkpointing/test_checkpoint_callback_frequency.py @@ -129,7 +129,7 @@ def training_step(self, batch, batch_idx): model = TestModel() trainer = Trainer( - checkpoint_callback=callbacks.ModelCheckpoint(dirpath=tmpdir, monitor='my_loss', save_top_k=k), + callbacks=[callbacks.ModelCheckpoint(dirpath=tmpdir, monitor='my_loss', save_top_k=k)], default_root_dir=tmpdir, max_epochs=epochs, weights_summary=None, diff --git a/tests/checkpointing/test_model_checkpoint.py b/tests/checkpointing/test_model_checkpoint.py index 6d1d3edea5be9..5dbeba54d06b3 100644 --- a/tests/checkpointing/test_model_checkpoint.py +++ b/tests/checkpointing/test_model_checkpoint.py @@ -897,12 +897,12 @@ def test_configure_model_checkpoint(tmpdir): assert trainer.checkpoint_callbacks == [callback1, callback2] with pytest.warns(DeprecationWarning, match='will no longer be supported in v1.3'): - trainer = Trainer(checkpoint_callback=callback1, callbacks=[], **kwargs) + trainer = Trainer(callbacks=[callback1], **kwargs) assert [c for c in trainer.callbacks if isinstance(c, ModelCheckpoint)] == [callback1] assert trainer.checkpoint_callback == callback1 with pytest.warns(DeprecationWarning, match="will no longer be supported in v1.3"): - trainer = Trainer(checkpoint_callback=callback1, callbacks=[callback2], **kwargs) + trainer = Trainer(callbacks=[callback1, callback2], **kwargs) assert trainer.checkpoint_callback == callback2 assert trainer.checkpoint_callbacks == [callback2, callback1] diff --git a/tests/core/test_datamodules.py b/tests/core/test_datamodules.py index 3e683025e8867..c05d9172f0095 100644 --- a/tests/core/test_datamodules.py +++ b/tests/core/test_datamodules.py @@ -243,7 +243,7 @@ def test_dm_checkpoint_save(tmpdir): default_root_dir=tmpdir, max_epochs=3, weights_summary=None, - checkpoint_callback=ModelCheckpoint(dirpath=tmpdir, monitor='early_stop_on') + callbacks=[ModelCheckpoint(dirpath=tmpdir, monitor='early_stop_on')], ) # fit model diff --git a/tests/models/data/horovod/train_default_model.py b/tests/models/data/horovod/train_default_model.py index d5a05057a42dd..d6f3f063d40e2 100644 --- a/tests/models/data/horovod/train_default_model.py +++ b/tests/models/data/horovod/train_default_model.py @@ -49,7 +49,7 @@ def run_test_from_config(trainer_options): reset_seed() ckpt_path = trainer_options['weights_save_path'] - trainer_options.update(checkpoint_callback=ModelCheckpoint(dirpath=ckpt_path)) + trainer_options.update(callbacks=[ModelCheckpoint(dirpath=ckpt_path)]) model = EvalModelTemplate() diff --git a/tests/models/test_amp.py b/tests/models/test_amp.py index 9c2ce2ccfffcf..51e1677e1f6df 100644 --- a/tests/models/test_amp.py +++ b/tests/models/test_amp.py @@ -129,7 +129,7 @@ def test_amp_gpu_ddp_slurm_managed(tmpdir): gpus=[0], distributed_backend='ddp_spawn', precision=16, - checkpoint_callback=checkpoint, + callbacks=[checkpoint], logger=logger, ) trainer.is_slurm_managing_tasks = True diff --git a/tests/models/test_cpu.py b/tests/models/test_cpu.py index 19bc5f063faf9..312c961f3f7a1 100644 --- a/tests/models/test_cpu.py +++ b/tests/models/test_cpu.py @@ -43,7 +43,7 @@ def test_cpu_slurm_save_load(enable_pl_optimizer, tmpdir): logger=logger, limit_train_batches=0.2, limit_val_batches=0.2, - checkpoint_callback=ModelCheckpoint(dirpath=tmpdir), + callbacks=[ModelCheckpoint(dirpath=tmpdir)], enable_pl_optimizer=enable_pl_optimizer, ) result = trainer.fit(model) @@ -80,7 +80,7 @@ def test_cpu_slurm_save_load(enable_pl_optimizer, tmpdir): default_root_dir=tmpdir, max_epochs=1, logger=logger, - checkpoint_callback=ModelCheckpoint(dirpath=tmpdir), + callbacks=[ModelCheckpoint(dirpath=tmpdir)], enable_pl_optimizer=enable_pl_optimizer, ) model = EvalModelTemplate(**hparams) @@ -208,7 +208,7 @@ def test_running_test_after_fitting(tmpdir): limit_train_batches=0.4, limit_val_batches=0.2, limit_test_batches=0.2, - checkpoint_callback=checkpoint, + callbacks=[checkpoint], logger=logger, ) result = trainer.fit(model) @@ -239,7 +239,7 @@ def test_running_test_no_val(tmpdir): limit_train_batches=0.4, limit_val_batches=0.2, limit_test_batches=0.2, - checkpoint_callback=checkpoint, + callbacks=[checkpoint], logger=logger, ) result = trainer.fit(model) diff --git a/tests/models/test_restore.py b/tests/models/test_restore.py index b350e8391143d..98153748882d2 100644 --- a/tests/models/test_restore.py +++ b/tests/models/test_restore.py @@ -159,7 +159,7 @@ def test_running_test_pretrained_model_distrib_dp(tmpdir): max_epochs=2, limit_train_batches=0.4, limit_val_batches=0.2, - checkpoint_callback=checkpoint, + callbacks=[checkpoint], logger=logger, gpus=[0, 1], distributed_backend='dp', @@ -209,7 +209,7 @@ def test_running_test_pretrained_model_distrib_ddp_spawn(tmpdir): max_epochs=2, limit_train_batches=0.4, limit_val_batches=0.2, - checkpoint_callback=checkpoint, + callbacks=[checkpoint], logger=logger, gpus=[0, 1], distributed_backend='ddp_spawn', @@ -257,7 +257,7 @@ def test_running_test_pretrained_model_cpu(tmpdir): max_epochs=3, limit_train_batches=0.4, limit_val_batches=0.2, - checkpoint_callback=checkpoint, + callbacks=[checkpoint], logger=logger, default_root_dir=tmpdir, ) @@ -288,7 +288,7 @@ def test_load_model_from_checkpoint(tmpdir, model_template): max_epochs=2, limit_train_batches=0.4, limit_val_batches=0.2, - checkpoint_callback=ModelCheckpoint(dirpath=tmpdir, monitor='early_stop_on', save_top_k=-1), + callbacks=[ModelCheckpoint(dirpath=tmpdir, monitor='early_stop_on', save_top_k=-1)], default_root_dir=tmpdir, ) @@ -405,7 +405,7 @@ def test_model_saving_loading(tmpdir): # fit model trainer = Trainer( max_epochs=1, logger=logger, - checkpoint_callback=ModelCheckpoint(dirpath=tmpdir), default_root_dir=tmpdir, + callbacks=[ModelCheckpoint(dirpath=tmpdir), default_root_dir=tmpdir], ) result = trainer.fit(model) @@ -460,7 +460,7 @@ def test_strict_model_load_more_params(monkeypatch, tmpdir, tmpdir_server, url_c # fit model trainer = Trainer( default_root_dir=tmpdir, max_epochs=1, logger=logger, - checkpoint_callback=ModelCheckpoint(dirpath=tmpdir), + callbacks=[ModelCheckpoint(dirpath=tmpdir)], ) result = trainer.fit(model) @@ -500,7 +500,7 @@ def test_strict_model_load_less_params(monkeypatch, tmpdir, tmpdir_server, url_c # fit model trainer = Trainer( default_root_dir=tmpdir, max_epochs=1, logger=logger, - checkpoint_callback=ModelCheckpoint(dirpath=tmpdir), + callbacks=[ModelCheckpoint(dirpath=tmpdir)], ) result = trainer.fit(model) diff --git a/tests/trainer/logging_tests/test_eval_loop_logging_1_0.py b/tests/trainer/logging_tests/test_eval_loop_logging_1_0.py index 1a928913228f2..76baa9237955f 100644 --- a/tests/trainer/logging_tests/test_eval_loop_logging_1_0.py +++ b/tests/trainer/logging_tests/test_eval_loop_logging_1_0.py @@ -26,6 +26,7 @@ from torch.utils.data import DataLoader, Dataset from pytorch_lightning import Trainer, callbacks, seed_everything +from pytorch_lightning.callbacks import ModelCheckpoint from pytorch_lightning.core.lightning import LightningModule from pytorch_lightning.loggers import TensorBoardLogger from tests.base import BoringModel, RandomDataset, SimpleModule @@ -291,7 +292,7 @@ def validation_epoch_end(self, outputs) -> None: max_epochs=1, log_every_n_steps=1, weights_summary=None, - checkpoint_callback=callbacks.ModelCheckpoint(dirpath='val_loss') + callbacks=[ModelCheckpoint(dirpath='val_loss')], ) trainer.fit(model) @@ -358,7 +359,7 @@ def test_monitor_val_epoch_end(tmpdir): trainer = Trainer( max_epochs=epoch_min_loss_override + 2, logger=False, - checkpoint_callback=checkpoint_callback, + callbacks=[checkpoint_callback], ) trainer.fit(model) diff --git a/tests/trainer/logging_tests/test_train_loop_logging_1_0.py b/tests/trainer/logging_tests/test_train_loop_logging_1_0.py index c148748888af4..0c27d8909d760 100644 --- a/tests/trainer/logging_tests/test_train_loop_logging_1_0.py +++ b/tests/trainer/logging_tests/test_train_loop_logging_1_0.py @@ -27,6 +27,7 @@ import pytorch_lightning as pl from pytorch_lightning import Trainer, callbacks +from pytorch_lightning.callbacks import ModelCheckpoint from pytorch_lightning.core.lightning import LightningModule from tests.base.boring_model import BoringModel, RandomDictDataset, RandomDictStringDataset from tests.base.deterministic_model import DeterministicModel @@ -88,7 +89,7 @@ def backward(self, loss, optimizer, optimizer_idx): max_epochs=2, log_every_n_steps=1, weights_summary=None, - checkpoint_callback=callbacks.ModelCheckpoint(monitor='l_se') + callbacks=[ModelCheckpoint(monitor='l_se')], ) trainer.fit(model) diff --git a/tests/trainer/test_trainer.py b/tests/trainer/test_trainer.py index 328b2c0a0f859..77b2d36561dbf 100644 --- a/tests/trainer/test_trainer.py +++ b/tests/trainer/test_trainer.py @@ -55,7 +55,7 @@ def test_no_val_module(monkeypatch, tmpdir, tmpdir_server, url_ckpt): default_root_dir=tmpdir, max_epochs=1, logger=logger, - checkpoint_callback=ModelCheckpoint(dirpath=tmpdir), + callbacks=[ModelCheckpoint(dirpath=tmpdir)], ) # fit model result = trainer.fit(model) @@ -101,7 +101,7 @@ def test_no_val_end_module(monkeypatch, tmpdir, tmpdir_server, url_ckpt): default_root_dir=tmpdir, max_epochs=1, logger=logger, - checkpoint_callback=ModelCheckpoint(dirpath=tmpdir), + callbacks=[ModelCheckpoint(dirpath=tmpdir)], ) result = trainer.fit(model) @@ -145,7 +145,7 @@ def test_strict_model_load(monkeypatch, tmpdir, tmpdir_server, url_ckpt): default_root_dir=tmpdir, max_epochs=1, logger=logger, - checkpoint_callback=ModelCheckpoint(dirpath=tmpdir), + callbacks=[ModelCheckpoint(dirpath=tmpdir)], ) result = trainer.fit(model) @@ -462,7 +462,7 @@ def test_model_checkpoint_only_weights(tmpdir): trainer = Trainer( default_root_dir=tmpdir, max_epochs=1, - checkpoint_callback=ModelCheckpoint(dirpath=tmpdir, monitor='early_stop_on', save_weights_only=True), + callbacks=[ModelCheckpoint(dirpath=tmpdir, monitor='early_stop_on', save_weights_only=True)], ) # fit model result = trainer.fit(model) @@ -539,7 +539,7 @@ def increment_on_load_checkpoint(self, _): max_epochs=2, limit_train_batches=0.65, limit_val_batches=1, - checkpoint_callback=ModelCheckpoint(dirpath=tmpdir, monitor='early_stop_on', save_top_k=-1), + callbacks=[ModelCheckpoint(dirpath=tmpdir, monitor='early_stop_on', save_top_k=-1)], default_root_dir=tmpdir, val_check_interval=1.0, enable_pl_optimizer=enable_pl_optimizer, @@ -718,7 +718,7 @@ def test_test_checkpoint_path(tmpdir, ckpt_path, save_top_k): max_epochs=2, progress_bar_refresh_rate=0, default_root_dir=tmpdir, - checkpoint_callback=ModelCheckpoint(monitor="early_stop_on", save_top_k=save_top_k), + callbacks=[ModelCheckpoint(monitor="early_stop_on", save_top_k=save_top_k)], ) trainer.fit(model) if ckpt_path == "best": From a369179b7e686bcbd84b4bccd5138c5eb6f3bcd9 Mon Sep 17 00:00:00 2001 From: Jirka Borovec Date: Tue, 8 Dec 2020 00:49:12 +0100 Subject: [PATCH 2/3] fix --- tests/models/test_restore.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/models/test_restore.py b/tests/models/test_restore.py index 98153748882d2..040f1b9e650c2 100644 --- a/tests/models/test_restore.py +++ b/tests/models/test_restore.py @@ -404,8 +404,10 @@ def test_model_saving_loading(tmpdir): # fit model trainer = Trainer( - max_epochs=1, logger=logger, - callbacks=[ModelCheckpoint(dirpath=tmpdir), default_root_dir=tmpdir], + max_epochs=1, + logger=logger, + callbacks=[ModelCheckpoint(dirpath=tmpdir)], + default_root_dir=tmpdir, ) result = trainer.fit(model) From 5aa1eb59196a619177e4eef8220c3f87e43a9084 Mon Sep 17 00:00:00 2001 From: Jirka Borovec Date: Tue, 8 Dec 2020 01:11:02 +0100 Subject: [PATCH 3/3] fix --- tests/checkpointing/test_model_checkpoint.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/checkpointing/test_model_checkpoint.py b/tests/checkpointing/test_model_checkpoint.py index 5dbeba54d06b3..31154eac1bf0d 100644 --- a/tests/checkpointing/test_model_checkpoint.py +++ b/tests/checkpointing/test_model_checkpoint.py @@ -897,12 +897,12 @@ def test_configure_model_checkpoint(tmpdir): assert trainer.checkpoint_callbacks == [callback1, callback2] with pytest.warns(DeprecationWarning, match='will no longer be supported in v1.3'): - trainer = Trainer(callbacks=[callback1], **kwargs) + trainer = Trainer(checkpoint_callback=callback1, **kwargs) assert [c for c in trainer.callbacks if isinstance(c, ModelCheckpoint)] == [callback1] assert trainer.checkpoint_callback == callback1 with pytest.warns(DeprecationWarning, match="will no longer be supported in v1.3"): - trainer = Trainer(callbacks=[callback1, callback2], **kwargs) + trainer = Trainer(checkpoint_callback=callback1, callbacks=[callback2], **kwargs) assert trainer.checkpoint_callback == callback2 assert trainer.checkpoint_callbacks == [callback2, callback1]