2021-02-25 15:48:19 +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.
|
|
|
|
"""Test deprecated functionality which will be removed in v1.5.0"""
|
2021-02-27 01:52:23 +00:00
|
|
|
from unittest import mock
|
|
|
|
|
2021-02-25 15:48:19 +00:00
|
|
|
import pytest
|
2021-04-08 23:47:02 +00:00
|
|
|
import torch
|
2021-03-07 07:48:50 +00:00
|
|
|
from torch import optim
|
2021-02-25 15:48:19 +00:00
|
|
|
|
2021-03-01 12:17:09 +00:00
|
|
|
from pytorch_lightning import Callback, Trainer
|
2021-03-07 23:59:14 +00:00
|
|
|
from pytorch_lightning.callbacks import ModelCheckpoint
|
2021-02-27 01:52:23 +00:00
|
|
|
from pytorch_lightning.loggers import WandbLogger
|
2021-03-24 08:16:28 +00:00
|
|
|
from pytorch_lightning.profiler import AdvancedProfiler, BaseProfiler, PyTorchProfiler, SimpleProfiler
|
2021-03-16 16:15:16 +00:00
|
|
|
from pytorch_lightning.trainer.callback_hook import warning_cache as callback_warning_cache
|
|
|
|
from tests.deprecated_api import no_deprecated_call
|
2021-02-25 15:48:19 +00:00
|
|
|
from tests.helpers import BoringModel
|
|
|
|
from tests.helpers.utils import no_warning_call
|
|
|
|
|
|
|
|
|
2021-03-07 23:59:14 +00:00
|
|
|
def test_v1_5_0_model_checkpoint_save_checkpoint():
|
|
|
|
model_ckpt = ModelCheckpoint()
|
|
|
|
model_ckpt.save_function = lambda *_, **__: None
|
|
|
|
with pytest.deprecated_call(match="ModelCheckpoint.save_checkpoint` signature has changed"):
|
|
|
|
model_ckpt.save_checkpoint(Trainer(), object())
|
|
|
|
|
|
|
|
|
2021-02-27 01:52:23 +00:00
|
|
|
@mock.patch('pytorch_lightning.loggers.wandb.wandb')
|
|
|
|
def test_v1_5_0_wandb_unused_sync_step(tmpdir):
|
|
|
|
with pytest.deprecated_call(match=r"v1.2.1 and will be removed in v1.5"):
|
|
|
|
WandbLogger(sync_step=True)
|
|
|
|
|
|
|
|
|
2021-02-25 15:48:19 +00:00
|
|
|
def test_v1_5_0_old_callback_on_save_checkpoint(tmpdir):
|
2021-03-01 12:17:09 +00:00
|
|
|
|
2021-02-25 15:48:19 +00:00
|
|
|
class OldSignature(Callback):
|
2021-03-01 12:17:09 +00:00
|
|
|
|
2021-02-25 15:48:19 +00:00
|
|
|
def on_save_checkpoint(self, trainer, pl_module): # noqa
|
|
|
|
...
|
|
|
|
|
|
|
|
model = BoringModel()
|
|
|
|
trainer_kwargs = {
|
|
|
|
"default_root_dir": tmpdir,
|
|
|
|
"checkpoint_callback": False,
|
|
|
|
"max_epochs": 1,
|
|
|
|
}
|
|
|
|
filepath = tmpdir / "test.ckpt"
|
|
|
|
|
|
|
|
trainer = Trainer(**trainer_kwargs, callbacks=[OldSignature()])
|
|
|
|
trainer.fit(model)
|
|
|
|
|
|
|
|
with pytest.deprecated_call(match="old signature will be removed in v1.5"):
|
|
|
|
trainer.save_checkpoint(filepath)
|
|
|
|
|
|
|
|
class NewSignature(Callback):
|
2021-03-01 12:17:09 +00:00
|
|
|
|
2021-02-25 15:48:19 +00:00
|
|
|
def on_save_checkpoint(self, trainer, pl_module, checkpoint):
|
|
|
|
...
|
|
|
|
|
|
|
|
class ValidSignature1(Callback):
|
2021-03-01 12:17:09 +00:00
|
|
|
|
2021-02-25 15:48:19 +00:00
|
|
|
def on_save_checkpoint(self, trainer, *args):
|
|
|
|
...
|
|
|
|
|
|
|
|
class ValidSignature2(Callback):
|
2021-03-01 12:17:09 +00:00
|
|
|
|
2021-02-25 15:48:19 +00:00
|
|
|
def on_save_checkpoint(self, *args):
|
|
|
|
...
|
|
|
|
|
|
|
|
trainer.callbacks = [NewSignature(), ValidSignature1(), ValidSignature2()]
|
|
|
|
with no_warning_call(DeprecationWarning):
|
|
|
|
trainer.save_checkpoint(filepath)
|
2021-03-06 12:40:19 +00:00
|
|
|
|
|
|
|
|
2021-03-23 17:13:29 +00:00
|
|
|
def test_v1_5_0_legacy_profiler_argument():
|
|
|
|
with pytest.deprecated_call(match="renamed to `record_functions` in v1.3"):
|
|
|
|
PyTorchProfiler(profiled_functions=[])
|
|
|
|
|
|
|
|
|
2021-03-06 12:40:19 +00:00
|
|
|
def test_v1_5_0_running_sanity_check():
|
|
|
|
trainer = Trainer()
|
|
|
|
with pytest.deprecated_call(match='has been renamed to `Trainer.sanity_checking`'):
|
|
|
|
assert not trainer.running_sanity_check
|
2021-03-07 07:48:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_old_training_step_signature_with_opt_idx_manual_opt(tmpdir):
|
|
|
|
|
|
|
|
class OldSignatureModel(BoringModel):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.automatic_optimization = False
|
|
|
|
|
|
|
|
def training_step(self, batch, batch_idx, optimizer_idx):
|
|
|
|
assert optimizer_idx is not None
|
|
|
|
return super().training_step(batch, batch_idx)
|
|
|
|
|
|
|
|
def configure_optimizers(self):
|
|
|
|
return [optim.SGD(self.parameters(), lr=1e-2), optim.SGD(self.parameters(), lr=1e-2)]
|
|
|
|
|
|
|
|
model = OldSignatureModel()
|
|
|
|
trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=2)
|
|
|
|
|
|
|
|
with pytest.deprecated_call(match="`training_step` .* `optimizer_idx` .* manual .* will be removed in v1.5"):
|
|
|
|
trainer.fit(model)
|
2021-03-11 22:44:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_v1_5_0_model_checkpoint_period(tmpdir):
|
|
|
|
with no_warning_call(DeprecationWarning):
|
|
|
|
ModelCheckpoint(dirpath=tmpdir)
|
|
|
|
with pytest.deprecated_call(match="is deprecated in v1.3 and will be removed in v1.5"):
|
|
|
|
ModelCheckpoint(dirpath=tmpdir, period=1)
|
2021-03-16 16:15:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_v1_5_0_old_on_validation_epoch_end(tmpdir):
|
|
|
|
callback_warning_cache.clear()
|
|
|
|
|
|
|
|
class OldSignature(Callback):
|
|
|
|
|
|
|
|
def on_validation_epoch_end(self, trainer, pl_module): # noqa
|
|
|
|
...
|
|
|
|
|
|
|
|
model = BoringModel()
|
|
|
|
trainer = Trainer(default_root_dir=tmpdir, max_epochs=1, callbacks=OldSignature())
|
|
|
|
|
|
|
|
with pytest.deprecated_call(match="old signature will be removed in v1.5"):
|
|
|
|
trainer.fit(model)
|
|
|
|
|
|
|
|
class OldSignatureModel(BoringModel):
|
|
|
|
|
|
|
|
def on_validation_epoch_end(self): # noqa
|
|
|
|
...
|
|
|
|
|
|
|
|
model = OldSignatureModel()
|
|
|
|
|
|
|
|
with pytest.deprecated_call(match="old signature will be removed in v1.5"):
|
|
|
|
trainer.fit(model)
|
|
|
|
|
|
|
|
callback_warning_cache.clear()
|
|
|
|
|
|
|
|
class NewSignature(Callback):
|
|
|
|
|
|
|
|
def on_validation_epoch_end(self, trainer, pl_module, outputs):
|
|
|
|
...
|
|
|
|
|
|
|
|
trainer.callbacks = [NewSignature()]
|
|
|
|
with no_deprecated_call(match="`Callback.on_validation_epoch_end` signature has changed in v1.3."):
|
|
|
|
trainer.fit(model)
|
|
|
|
|
|
|
|
class NewSignatureModel(BoringModel):
|
|
|
|
|
|
|
|
def on_validation_epoch_end(self, outputs):
|
|
|
|
...
|
|
|
|
|
|
|
|
model = NewSignatureModel()
|
|
|
|
with no_deprecated_call(match="`ModelHooks.on_validation_epoch_end` signature has changed in v1.3."):
|
|
|
|
trainer.fit(model)
|
|
|
|
|
|
|
|
|
|
|
|
def test_v1_5_0_old_on_test_epoch_end(tmpdir):
|
|
|
|
callback_warning_cache.clear()
|
|
|
|
|
|
|
|
class OldSignature(Callback):
|
|
|
|
|
|
|
|
def on_test_epoch_end(self, trainer, pl_module): # noqa
|
|
|
|
...
|
|
|
|
|
|
|
|
model = BoringModel()
|
|
|
|
trainer = Trainer(default_root_dir=tmpdir, max_epochs=1, callbacks=OldSignature())
|
|
|
|
|
|
|
|
with pytest.deprecated_call(match="old signature will be removed in v1.5"):
|
|
|
|
trainer.test(model)
|
|
|
|
|
|
|
|
class OldSignatureModel(BoringModel):
|
|
|
|
|
|
|
|
def on_test_epoch_end(self): # noqa
|
|
|
|
...
|
|
|
|
|
|
|
|
model = OldSignatureModel()
|
|
|
|
|
|
|
|
with pytest.deprecated_call(match="old signature will be removed in v1.5"):
|
|
|
|
trainer.test(model)
|
|
|
|
|
|
|
|
callback_warning_cache.clear()
|
|
|
|
|
|
|
|
class NewSignature(Callback):
|
|
|
|
|
|
|
|
def on_test_epoch_end(self, trainer, pl_module, outputs):
|
|
|
|
...
|
|
|
|
|
|
|
|
trainer.callbacks = [NewSignature()]
|
|
|
|
with no_deprecated_call(match="`Callback.on_test_epoch_end` signature has changed in v1.3."):
|
|
|
|
trainer.test(model)
|
|
|
|
|
|
|
|
class NewSignatureModel(BoringModel):
|
|
|
|
|
|
|
|
def on_test_epoch_end(self, outputs):
|
|
|
|
...
|
|
|
|
|
|
|
|
model = NewSignatureModel()
|
|
|
|
with no_deprecated_call(match="`ModelHooks.on_test_epoch_end` signature has changed in v1.3."):
|
|
|
|
trainer.test(model)
|
2021-03-23 10:07:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("cls", (BaseProfiler, SimpleProfiler, AdvancedProfiler, PyTorchProfiler))
|
|
|
|
def test_v1_5_0_profiler_output_filename(tmpdir, cls):
|
|
|
|
filepath = str(tmpdir / "test.txt")
|
|
|
|
with pytest.deprecated_call(match="`output_filename` parameter has been removed"):
|
|
|
|
profiler = cls(output_filename=filepath)
|
|
|
|
assert profiler.dirpath == tmpdir
|
|
|
|
assert profiler.filename == "test"
|
2021-04-08 23:47:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_v1_5_0_trainer_training_trick_mixin(tmpdir):
|
|
|
|
model = BoringModel()
|
|
|
|
trainer = Trainer(default_root_dir=tmpdir, max_epochs=1, checkpoint_callback=False, logger=False)
|
|
|
|
trainer.fit(model)
|
|
|
|
with pytest.deprecated_call(match="is deprecated in v1.3 and will be removed in v1.5"):
|
|
|
|
trainer.print_nan_gradients()
|
|
|
|
|
|
|
|
dummy_loss = torch.tensor(1.0)
|
|
|
|
with pytest.deprecated_call(match="is deprecated in v1.3 and will be removed in v1.5"):
|
|
|
|
trainer.detect_nan_tensors(dummy_loss)
|