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

Fix: Train loop config validation was run during trainer.predict #6541

Merged
merged 7 commits into from
Mar 16, 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
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).


## [1.2.x] - YYYY-MM-DD

### Fixed

- Fixed when Train loop config was run during `Trainer.predict` ([#6541](https://github.com/PyTorchLightning/pytorch-lightning/pull/6541))


## [1.2.3] - 2021-03-09


Expand Down Expand Up @@ -52,7 +59,6 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed priority of plugin/accelerator when setting distributed mode ([#6089](https://github.com/PyTorchLightning/pytorch-lightning/pull/6089))
- Fixed error message for AMP + CPU incompatibility ([#6107](https://github.com/PyTorchLightning/pytorch-lightning/pull/6107))


## [1.2.0] - 2021-02-18

### Added
Expand Down
10 changes: 9 additions & 1 deletion pytorch_lightning/trainer/configuration_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ def verify_loop_configurations(self, model: LightningModule):
model: The model to check the configuration.

"""
if not self.trainer.testing:
if self.trainer.predicting:
self.__verify_predict_loop_configuration(model)
elif not self.trainer.testing:
self.__verify_train_loop_configuration(model)
self.__verify_eval_loop_configuration(model, 'validation')
else:
Expand Down Expand Up @@ -98,3 +100,9 @@ def __verify_eval_loop_configuration(self, model, eval_loop_name):
rank_zero_warn(f'you passed in a {loader_name} but have no {step_name}. Skipping {eval_loop_name} loop')
if has_step and not has_loader:
rank_zero_warn(f'you defined a {step_name} but have no {loader_name}. Skipping {eval_loop_name} loop')

def __verify_predict_loop_configuration(self, model):

has_predict_dataloader = is_overridden('predict_dataloader', model)
if not has_predict_dataloader:
raise MisconfigurationException('Dataloader not found for `Trainer.predict`')
29 changes: 29 additions & 0 deletions tests/trainer/test_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1850,3 +1850,32 @@ def compare_optimizers():
trainer.max_epochs = 2 # simulate multiple fit calls
trainer.fit(model)
compare_optimizers()


@pytest.mark.parametrize("data", [
dict(datamodule=TestLightningDataModule(dataloaders)),
dict(dataloaders=[torch.utils.data.DataLoader(RandomDataset(32, 2)), torch.utils.data.DataLoader(RandomDataset(32, 2)),
])])
def test_trainer_predict_verify_config(tmpdir, data):

class TestModel(LightningModule):

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

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

model = TestModel()
trainer = Trainer(default_root_dir=tmpdir)

results = trainer.predict(model, **data)

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)