diff --git a/tests/base/__init__.py b/tests/base/__init__.py index 0d602c35bf235..ee9069354be9c 100644 --- a/tests/base/__init__.py +++ b/tests/base/__init__.py @@ -3,4 +3,3 @@ from tests.base.boring_model import BoringDataModule, BoringModel, RandomDataset # noqa: F401 from tests.base.datasets import TrialMNIST # noqa: F401 from tests.base.model_template import EvalModelTemplate, GenericEvalModelTemplate # noqa: F401 -from tests.base.simple_model import SimpleModule # noqa: F401 diff --git a/tests/base/simple_model.py b/tests/base/simple_model.py deleted file mode 100644 index 06cda4a29007f..0000000000000 --- a/tests/base/simple_model.py +++ /dev/null @@ -1,98 +0,0 @@ -# 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. -from typing import Optional - -import torch -from torch.utils.data import Dataset - -from pytorch_lightning import LightningModule - - -class RandomDataset(Dataset): - - def __init__(self, size, length): - self.len = length - self.data = torch.randn(length, size) - - def __getitem__(self, index): - return self.data[index] - - def __len__(self): - return self.len - - -class SimpleModule(LightningModule): - - def __init__(self, epoch_min_loss_override: Optional[int] = None): - """LightningModule for testing purposes - Args: - epoch_min_loss_override (int, optional): Pass in an epoch that will be set to the minimum - validation loss for testing purposes (zero based). If None this is ignored. Defaults to None. - """ - super().__init__() - self.layer = torch.nn.Linear(32, 2) - self.epoch_min_loss_override = epoch_min_loss_override - - def forward(self, x): - return self.layer(x) - - def loss(self, batch, prediction): - # An arbitrary loss to have a loss that updates the model weights during `Trainer.fit` calls - return torch.nn.functional.mse_loss(prediction, torch.ones_like(prediction)) - - def training_step(self, batch, batch_idx): - output = self.forward(batch) - loss = self.loss(batch, output) - return {"output": output, "loss": loss, "checkpoint_on": loss} - - def validation_step(self, batch, batch_idx): - output = self.forward(batch) - loss = self.loss(batch, output) - return {"output": output, "loss": loss, "checkpoint_on": loss} - - def test_step(self, batch, batch_idx): - output = self.forward(batch) - loss = self.loss(batch, output) - return {"output": output, "loss": loss} - - def training_epoch_end(self, outputs) -> None: - avg_loss = torch.stack([x["loss"] for x in outputs]).mean() - self.log("avg_loss", avg_loss) - - def validation_epoch_end(self, outputs) -> None: - avg_val_loss = torch.stack([torch.randn(1, requires_grad=True) for _ in outputs]).mean() - # For testing purposes allow a nominated epoch to have a low loss - if self.current_epoch == self.epoch_min_loss_override: - avg_val_loss -= 1e10 - - self.log("avg_val_loss", avg_val_loss) - self.log("checkpoint_on", avg_val_loss) - - def test_epoch_end(self, outputs) -> None: - avg_loss = torch.stack([torch.randn(1, requires_grad=True) for _ in outputs]).mean() - self.log("test_loss", avg_loss) - - def configure_optimizers(self): - optimizer = torch.optim.SGD(self.layer.parameters(), lr=0.1) - lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=1) - return [optimizer], [lr_scheduler] - - def train_dataloader(self): - return torch.utils.data.DataLoader(RandomDataset(32, 64)) - - def val_dataloader(self): - return torch.utils.data.DataLoader(RandomDataset(32, 64)) - - def test_dataloader(self): - return torch.utils.data.DataLoader(RandomDataset(32, 64)) diff --git a/tests/models/test_cpu.py b/tests/models/test_cpu.py index 8380ff7178f6c..6a0138653a920 100644 --- a/tests/models/test_cpu.py +++ b/tests/models/test_cpu.py @@ -142,7 +142,7 @@ def test_multi_cpu_model_ddp(tmpdir): ) model = BoringModel() - tpipes.run_model_test(trainer_options, model, on_gpu=False, min_acc=0.05) + tpipes.run_model_test(trainer_options, model, on_gpu=False) def test_lbfgs_cpu_model(tmpdir): diff --git a/tests/trainer/flags/test_val_check_interval.py b/tests/trainer/flags/test_val_check_interval.py index 384f5eb9acbbc..d1055695dd341 100644 --- a/tests/trainer/flags/test_val_check_interval.py +++ b/tests/trainer/flags/test_val_check_interval.py @@ -14,13 +14,13 @@ import pytest from pytorch_lightning.trainer import Trainer -from tests.base import SimpleModule +from tests.base import BoringModel @pytest.mark.parametrize('max_epochs', [1, 2, 3]) def test_val_check_interval_1(tmpdir, max_epochs): - class TestModel(SimpleModule): + class TestModel(BoringModel): def __init__(self): super().__init__() @@ -48,7 +48,7 @@ def on_validation_epoch_start(self) -> None: @pytest.mark.parametrize('max_epochs', [1, 2, 3]) def test_val_check_interval_quarter(tmpdir, max_epochs): - class TestModel(SimpleModule): + class TestModel(BoringModel): def __init__(self): super().__init__() @@ -76,7 +76,7 @@ def on_validation_epoch_start(self) -> None: @pytest.mark.parametrize('max_epochs', [1, 2, 3]) def test_val_check_interval_third(tmpdir, max_epochs): - class TestModel(SimpleModule): + class TestModel(BoringModel): def __init__(self): super().__init__() diff --git a/tests/trainer/logging_/test_eval_loop_logging_1_0.py b/tests/trainer/logging_/test_eval_loop_logging_1_0.py index fcc24871e7570..5b3337ef8d7d1 100644 --- a/tests/trainer/logging_/test_eval_loop_logging_1_0.py +++ b/tests/trainer/logging_/test_eval_loop_logging_1_0.py @@ -28,7 +28,7 @@ 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 +from tests.base import BoringModel, RandomDataset from tests.base.deterministic_model import DeterministicModel @@ -358,7 +358,7 @@ def test_epoch_end(self, outputs): def test_monitor_val_epoch_end(tmpdir): epoch_min_loss_override = 0 - model = SimpleModule() + model = BoringModel() checkpoint_callback = callbacks.ModelCheckpoint(dirpath=tmpdir, save_top_k=1, monitor="avg_val_loss") trainer = Trainer( max_epochs=epoch_min_loss_override + 2,