Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trainer.predict config validation #6543

Merged
merged 5 commits into from
Mar 21, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Added no return warning to predict ([#6139](https://github.com/PyTorchLightning/pytorch-lightning/pull/6139))


- Added `outputs` parameter to callback's `on_validation_epoch_end` & `on_test_epoch_end` hooks ([#6120](https://github.com/PyTorchLightning/pytorch-lightning/pull/6120))
- Added `Trainer.predict` config validation ([#6543]https://github.com/PyTorchLightning/pytorch-lightning/pull/6543)
carmocca marked this conversation as resolved.
Show resolved Hide resolved


- Added `outputs` parameter to callback's `on_validation_epoch_end` & `on_test_epoch_end` hooks ([#6120](https://github.com/PyTorchLightning/pytorch-lightning/pull/6120))


### Changed
Expand Down
9 changes: 8 additions & 1 deletion pytorch_lightning/trainer/configuration_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def verify_loop_configurations(self, model: LightningModule) -> None:
self.__verify_eval_loop_configuration(model, 'val')
elif self.trainer.state == TrainerState.TESTING:
self.__verify_eval_loop_configuration(model, 'test')
# TODO: add predict
elif self.trainer.state == TrainerState.PREDICTING:
self.__verify_predict_loop_configuration(model)

def __verify_train_loop_configuration(self, model):
# -----------------------------------
Expand Down Expand Up @@ -99,3 +100,9 @@ def __verify_eval_loop_configuration(self, model: LightningModule, stage: str) -
rank_zero_warn(f'you passed in a {loader_name} but have no {step_name}. Skipping {stage} loop')
if has_step and not has_loader:
rank_zero_warn(f'you defined a {step_name} but have no {loader_name}. Skipping {stage} loop')

def __verify_predict_loop_configuration(self, model: LightningModule) -> None:

has_predict_dataloader = is_overridden('predict_dataloader', model)
if not has_predict_dataloader:
raise MisconfigurationException('Dataloader not found for `Trainer.predict`')
50 changes: 48 additions & 2 deletions tests/trainer/test_config_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest
import torch

from pytorch_lightning import Trainer
from pytorch_lightning import LightningDataModule, LightningModule, Trainer
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from tests.helpers import BoringModel
from tests.helpers import BoringModel, RandomDataset


def test_wrong_train_setting(tmpdir):
Expand Down Expand Up @@ -101,3 +102,48 @@ def test_val_loop_config(tmpdir):
model = BoringModel()
model.validation_step = None
trainer.validate(model)


@pytest.mark.parametrize("datamodule", [False, True])
def test_trainer_predict_verify_config(tmpdir, datamodule):

class TestModel(LightningModule):

def __init__(self):
super().__init__()
self.layer = torch.nn.Linear(32, 2)

def forward(self, x):
return self.layer(x)

class TestLightningDataModule(LightningDataModule):

def __init__(self, dataloaders):
super().__init__()
self._dataloaders = dataloaders

def test_dataloader(self):
return self._dataloaders

def predict_dataloader(self):
return self._dataloaders

dataloaders = [torch.utils.data.DataLoader(RandomDataset(32, 2)), torch.utils.data.DataLoader(RandomDataset(32, 2))]

model = TestModel()

trainer = Trainer(default_root_dir=tmpdir)

if datamodule:
datamodule = TestLightningDataModule(dataloaders)
results = trainer.predict(model, datamodule=datamodule)
else:
results = trainer.predict(model, dataloaders=dataloaders)

assert len(results) == 2
assert results[0][0].shape == torch.Size([1, 2])

model.predict_dataloader = None

with pytest.raises(MisconfigurationException, match="Dataloader not found for `Trainer.predict`"):
trainer.predict(model)