{"record":{"id":"1f84c64401a51385","repo":"Lightning-AI/pytorch-lightning","slug":"return-predictions-should-be-set-to-false-when","errorCode":null,"errorMessage":"`return_predictions` should be set to `False` when using the strategies that spawn or fork. Found {return_predictions} with strategy {type(self.trainer.strategy)}.","messagePattern":"`return_predictions` should be set to `False` when using the strategies that spawn or fork\\. Found (.+?) with strategy (.+?)\\.","errorType":"exception","errorClass":"MisconfigurationException","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/loops/prediction_loop.py","lineNumber":78,"sourceCode":"        self._data_source = _DataLoaderSource(None, \"predict_dataloader\")\n        self._combined_loader: Optional[CombinedLoader] = None\n        self._data_fetcher: Optional[_DataFetcher] = None\n        self._results = None  # for `trainer._results` access\n        self._predictions: list[list[Any]] = []  # dataloaders x batches\n        self._return_predictions = False\n        self._module_mode = _ModuleMode()\n\n    @property\n    def return_predictions(self) -> bool:\n        \"\"\"Whether to return the predictions or not.\"\"\"\n        return self._return_predictions\n\n    @return_predictions.setter\n    def return_predictions(self, return_predictions: Optional[bool] = None) -> None:\n        # Strategies that spawn or fork don't support returning predictions\n        return_supported = not isinstance(self.trainer.strategy.launcher, _MultiProcessingLauncher)\n        if return_predictions and not return_supported:\n            raise MisconfigurationException(\n                \"`return_predictions` should be set to `False` when using the strategies that spawn or fork.\"\n                f\" Found {return_predictions} with strategy {type(self.trainer.strategy)}.\"\n            )\n        # For strategies that support it, `return_predictions` is True by default unless user decide otherwise.\n        self._return_predictions = return_supported if return_predictions is None else return_predictions\n\n    @property\n    def predictions(self) -> list[Any]:\n        \"\"\"The cached predictions.\"\"\"\n        if self._predictions == []:\n            return self._predictions\n        return self._predictions[0] if self.num_dataloaders == 1 else self._predictions\n\n    @property\n    def num_dataloaders(self) -> int:\n        \"\"\"Returns the number of prediction dataloaders.\"\"\"\n        combined_loader = self._combined_loader\n        assert combined_loader is not None","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/loops/prediction_loop.py#L60-L96","documentation":"Raised by the prediction loop's return_predictions setter when return_predictions is truthy and the strategy uses a subprocess launcher (_MultiProcessingLauncher, e.g. ddp_spawn or Colab/Boulder fork-based strategies). Spawned/forked worker processes cannot reliably send collected predictions back to the main process, so returning them is disallowed.","triggerScenarios":"`Trainer(strategy='ddp_spawn', devices=2)` with `trainer.predict(model, return_predictions=True)` (or relying on the True default); any launcher that spawns/forks processes combined with predict.","commonSituations":"Switching a prediction script from single-process to ddp_spawn for multi-GPU inference; notebook environments where spawn strategies are common defaults.","solutions":["Pass `return_predictions=False` and collect results via a prediction callback writing to disk","Or switch to a non-spawning strategy like 'ddp' for prediction","Write predictions inside predict_step or on_predict_epoch_end instead of returning them"],"exampleFix":"# before\ntrainer = pl.Trainer(strategy='ddp_spawn', devices=2)\npreds = trainer.predict(model, dataloaders=dl)  # default return_predictions=True\n\n# after\ntrainer = pl.Trainer(strategy='ddp_spawn', devices=2)\ntrainer.predict(model, dataloaders=dl, return_predictions=False)  # gather via callback","handlingStrategy":"validation","validationCode":"from lightning.pytorch.strategies.launchers import _MultiProcessingLauncher\n\nif isinstance(trainer.strategy.launcher, _MultiProcessingLauncher):\n    return_predictions = False\ntrainer.predict(model, dl, return_predictions=return_predictions)","typeGuard":"def can_return_predictions(trainer) -> bool:\n    return not isinstance(trainer.strategy.launcher, _MultiProcessingLauncher)","tryCatchPattern":null,"preventionTips":["Always pass return_predictions=False with spawn/fork strategies and gather via callbacks","Prefer non-spawn strategies (ddp) for long-running inference jobs"],"tags":["pytorch-lightning","predict","ddp-spawn","return-predictions"],"backgroundTag":"spawn-unsupported-return-value","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}