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 AttributeError when using CombinedLoader in prediction #11111

Merged
merged 7 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -290,10 +290,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Avoid the deprecated `onnx.export(example_outputs=...)` in torch 1.10 ([#11116](https://github.com/PyTorchLightning/pytorch-lightning/pull/11116))



- Fixed an issue when torch-scripting a `LightningModule` after training with `Trainer(sync_batchnorm=True)` ([#11078](https://github.com/PyTorchLightning/pytorch-lightning/pull/11078))


- Fixed an `AttributeError` occuring when using a `CombinedLoader` (multiple dataloaders) for prediction ([#11111](https://github.com/PyTorchLightning/pytorch-lightning/pull/11111))


- Fixed bug where `Trainer(track_grad_norm=..., logger=False)' would fail ([#11114](https://github.com/PyTorchLightning/pytorch-lightning/pull/11114))


Expand Down
3 changes: 2 additions & 1 deletion pytorch_lightning/loops/epoch/prediction_epoch_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ def _build_kwargs(self, batch: Any, batch_idx: int, dataloader_idx: int) -> Dict
def _get_batch_indices(self, dataloader_idx: int) -> List[List[int]]:
"""Returns a reference to the seen batch indices if the dataloader has a batch sampler wrapped by our
:class:`~pytorch_lightning.overrides.distributed.IndexBatchSamplerWrapper`."""
batch_sampler = self.trainer.predict_dataloaders[dataloader_idx].batch_sampler
# the batch_sampler is not be defined in case of CombinedDataLoaders
batch_sampler = getattr(self.trainer.predict_dataloaders[dataloader_idx], "batch_sampler", None)
rohitgr7 marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(batch_sampler, IndexBatchSamplerWrapper) and self.should_store_predictions:
return batch_sampler.seen_batch_indices

Expand Down
34 changes: 34 additions & 0 deletions tests/callbacks/test_prediction_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import pytest
from torch.utils.data import DataLoader

import pytorch_lightning as pl
from pytorch_lightning import Trainer
from pytorch_lightning.callbacks import BasePredictionWriter
from pytorch_lightning.trainer.supporters import CombinedLoader
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from tests.helpers import BoringModel, RandomDataset
from tests.helpers.runif import RunIf
Expand Down Expand Up @@ -106,3 +108,35 @@ def test_prediction_writer_batch_indices(tmpdir, num_workers):
call(trainer, model, ANY, [[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]]]),
]
)


def test_prediction_writer_partial_support_for_combined_loader(tmpdir):
"""Test partial support for CombinedLoader: prediction works but sample indices don't get tracked."""
pl.loops.epoch.prediction_epoch_loop.warning_cache.clear()

class PredictionModel(BoringModel):
def predict_dataloader(self):
return CombinedLoader(
{
"a": DataLoader(RandomDataset(32, 8), batch_size=2),
"b": DataLoader(RandomDataset(32, 8), batch_size=4),
}
)

def predict_step(self, batch, *args, **kwargs):
return self(batch["a"])

DummyPredictionWriter.write_on_batch_end = Mock()
DummyPredictionWriter.write_on_epoch_end = Mock()

model = PredictionModel()
writer = DummyPredictionWriter("batch_and_epoch")
trainer = Trainer(callbacks=writer)
with pytest.warns(UserWarning, match="Lightning couldn't infer the indices fetched for your dataloader."):
trainer.predict(model)

writer.write_on_batch_end.assert_has_calls(
[call(trainer, model, ANY, [], ANY, 0, 0), call(trainer, model, ANY, [], ANY, 1, 0)]
)

writer.write_on_epoch_end.assert_has_calls([call(trainer, model, ANY, [[]])])