Lightning-AI/pytorch-lightning · error · ValueError

`trainer.predict()` only supports the `CombinedLoader(mode="

Error message

`trainer.predict()` only supports the `CombinedLoader(mode="sequential")` mode.

What it means

Raised by _PredictionLoop.reset when the CombinedLoader used for prediction was created with any mode other than 'sequential'. Unlike training, prediction iterates each dataloader fully and independently, so only the sequential mode (run loaders one after another) is supported by trainer.predict.

Source

Thrown at src/lightning/pytorch/loops/prediction_loop.py:177

            dataloaders.append(dl)

            # determine number of batches
            length = len(dl) if has_len_all_ranks(dl, trainer.strategy, allow_zero_length) else float("inf")
            num_batches = _parse_num_batches(stage, length, trainer.limit_predict_batches)
            self.max_batches.append(num_batches)
        combined_loader.flattened = dataloaders
        self._combined_loader = combined_loader

    def reset(self) -> None:
        """Resets the internal state of the loop for a new run."""
        self.batch_progress.reset_on_run()

        assert self.trainer.state.stage is not None
        data_fetcher = _select_data_fetcher(self.trainer, self.trainer.state.stage)
        combined_loader = self._combined_loader
        assert combined_loader is not None
        if combined_loader._mode != "sequential":
            raise ValueError('`trainer.predict()` only supports the `CombinedLoader(mode="sequential")` mode.')

        # set the per-dataloader limits
        combined_loader.limits = self.max_batches
        data_fetcher.setup(combined_loader)
        iter(data_fetcher)  # creates the iterator inside the fetcher

        # add the previous `fetched` value to properly track `is_last_batch` with no prefetching
        data_fetcher.fetched += self.batch_progress.current.ready
        data_fetcher._start_profiler = self._on_before_fetch
        data_fetcher._stop_profiler = self._on_after_fetch
        self._data_fetcher = data_fetcher

        num_dataloaders = self.num_dataloaders
        self.epoch_batch_indices = [[] for _ in range(num_dataloaders)]
        self._predictions = [[] for _ in range(num_dataloaders)]

    def on_run_start(self) -> None:
        """Calls ``_on_predict_model_eval``, ``_on_predict_start`` and ``_on_predict_epoch_start`` hooks."""

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Use `CombinedLoader(dataloaders, mode='sequential')` for prediction
  2. Or call trainer.predict once per dataloader in a loop instead of combining them

Example fix

# before
loader = CombinedLoader([dl1, dl2], mode='max_size_cycle')
trainer.predict(model, loader)

# after
loader = CombinedLoader([dl1, dl2], mode='sequential')
trainer.predict(model, loader)
Defensive patterns

Strategy: validation

Validate before calling

assert combined_loader._mode == 'sequential', 'trainer.predict requires sequential mode'

Type guard

def is_sequential(cl) -> bool:
    return getattr(cl, '_mode', None) == 'sequential'

Prevention

When it happens

Trigger: Passing multiple dataloaders to `trainer.predict(model, dataloaders=[dl1, dl2])` where the resulting CombinedLoader uses 'max_size'/'min_size'/'max_size_cycle'; constructing a CombinedLoader manually with a cycling mode and handing it to predict.

Common situations: Reusing a multi-loader configuration built for training (cycling modes) at inference time; assuming symmetric mode support between fit and predict.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/d97c2644fe80d26d. Report an issue: GitHub.