2020-10-13 11:18:07 +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-01-11 11:36:32 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from pytorch_lightning import Callback, Trainer
|
2021-02-08 10:52:02 +00:00
|
|
|
from tests.helpers.boring_model import BoringModel
|
2020-10-08 01:48:38 +00:00
|
|
|
|
|
|
|
|
2021-01-11 11:36:32 +00:00
|
|
|
@pytest.mark.parametrize("single_cb", [False, True])
|
2021-03-09 11:27:15 +00:00
|
|
|
def test_train_step_no_return(tmpdir, single_cb: bool):
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Tests that only training_step can be used."""
|
2021-02-06 12:28:26 +00:00
|
|
|
|
2020-10-08 01:48:38 +00:00
|
|
|
class CB(Callback):
|
2021-10-07 10:18:11 +00:00
|
|
|
def on_train_batch_end(self, trainer, pl_module, outputs, batch, batch_idx):
|
2021-07-26 11:37:35 +00:00
|
|
|
assert "loss" in outputs
|
2020-10-08 01:48:38 +00:00
|
|
|
|
|
|
|
def on_validation_batch_end(self, trainer, pl_module, outputs, batch, batch_idx, dataloader_idx):
|
2021-07-26 11:37:35 +00:00
|
|
|
assert "x" in outputs
|
2020-10-08 01:48:38 +00:00
|
|
|
|
|
|
|
def on_test_batch_end(self, trainer, pl_module, outputs, batch, batch_idx, dataloader_idx):
|
2021-07-26 11:37:35 +00:00
|
|
|
assert "x" in outputs
|
2020-10-08 01:48:38 +00:00
|
|
|
|
|
|
|
class TestModel(BoringModel):
|
2021-10-07 10:18:11 +00:00
|
|
|
def on_train_batch_end(self, outputs, batch, batch_idx: int) -> None:
|
2021-07-26 11:37:35 +00:00
|
|
|
assert "loss" in outputs
|
2020-10-08 01:48:38 +00:00
|
|
|
|
|
|
|
def on_validation_batch_end(self, outputs, batch, batch_idx: int, dataloader_idx: int) -> None:
|
2021-07-26 11:37:35 +00:00
|
|
|
assert "x" in outputs
|
2020-10-08 01:48:38 +00:00
|
|
|
|
|
|
|
def on_test_batch_end(self, outputs, batch, batch_idx: int, dataloader_idx: int) -> None:
|
2021-07-26 11:37:35 +00:00
|
|
|
assert "x" in outputs
|
2020-10-08 01:48:38 +00:00
|
|
|
|
2021-05-05 15:18:16 +00:00
|
|
|
def training_epoch_end(self, outputs) -> None:
|
2021-04-13 14:16:21 +00:00
|
|
|
assert len(outputs) == self.trainer.num_training_batches
|
2020-10-08 02:27:36 +00:00
|
|
|
|
2020-10-08 01:48:38 +00:00
|
|
|
model = TestModel()
|
|
|
|
|
|
|
|
trainer = Trainer(
|
2021-01-11 11:36:32 +00:00
|
|
|
callbacks=CB() if single_cb else [CB()],
|
2020-10-08 01:48:38 +00:00
|
|
|
default_root_dir=tmpdir,
|
|
|
|
limit_train_batches=2,
|
|
|
|
limit_val_batches=2,
|
|
|
|
max_epochs=1,
|
2020-10-08 03:46:21 +00:00
|
|
|
log_every_n_steps=1,
|
2021-10-13 11:50:54 +00:00
|
|
|
enable_model_summary=False,
|
2020-10-08 01:48:38 +00:00
|
|
|
)
|
|
|
|
|
2021-01-11 11:36:32 +00:00
|
|
|
assert any(isinstance(c, CB) for c in trainer.callbacks)
|
|
|
|
|
2021-04-28 18:11:32 +00:00
|
|
|
trainer.fit(model)
|
2021-03-16 16:15:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_free_memory_on_eval_outputs(tmpdir):
|
|
|
|
class CB(Callback):
|
2022-02-07 14:15:27 +00:00
|
|
|
def on_train_epoch_end(self, trainer, pl_module):
|
2021-11-28 20:09:30 +00:00
|
|
|
assert not trainer._evaluation_loop._outputs
|
2021-03-16 16:15:16 +00:00
|
|
|
|
|
|
|
model = BoringModel()
|
|
|
|
|
|
|
|
trainer = Trainer(
|
|
|
|
callbacks=CB(),
|
|
|
|
default_root_dir=tmpdir,
|
|
|
|
limit_train_batches=2,
|
|
|
|
limit_val_batches=2,
|
|
|
|
max_epochs=1,
|
2021-10-13 11:50:54 +00:00
|
|
|
enable_model_summary=False,
|
2021-03-16 16:15:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
trainer.fit(model)
|