{"record":{"id":"759ddbf690720969","repo":"Lightning-AI/pytorch-lightning","slug":"predict-dataloader-must-be-implemented-to-be-use","errorCode":null,"errorMessage":"`predict_dataloader` must be implemented to be used with the Lightning Trainer","messagePattern":"`predict_dataloader` must be implemented to be used with the Lightning Trainer","errorType":"exception","errorClass":"MisconfigurationException","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/core/hooks.py","lineNumber":561,"sourceCode":"        r\"\"\"An iterable or collection of iterables specifying prediction samples.\n\n        For more information about multiple dataloaders, see this :ref:`section <multiple-dataloaders>`.\n\n        It's recommended that all data downloads and preparation happen in :meth:`prepare_data`.\n\n        - :meth:`~lightning.pytorch.trainer.trainer.Trainer.predict`\n        - :meth:`prepare_data`\n        - :meth:`setup`\n\n        Note:\n            Lightning tries to add the correct sampler for distributed and arbitrary hardware\n            There is no need to set it yourself.\n\n        Return:\n            A :class:`torch.utils.data.DataLoader` or a sequence of them specifying prediction samples.\n\n        \"\"\"\n        raise MisconfigurationException(\n            \"`predict_dataloader` must be implemented to be used with the Lightning Trainer\"\n        )\n\n    def transfer_batch_to_device(self, batch: Any, device: torch.device, dataloader_idx: int) -> Any:\n        \"\"\"Override this hook if your :class:`~torch.utils.data.DataLoader` returns tensors wrapped in a custom data\n        structure.\n\n        The data types listed below (and any arbitrary nesting of them) are supported out of the box:\n\n        - :class:`torch.Tensor` or anything that implements `.to(...)`\n        - :class:`list`\n        - :class:`dict`\n        - :class:`tuple`\n\n        For anything else, you need to define how the data is moved to the target device (CPU, GPU, TPU, ...).\n\n        Note:\n            This hook should only transfer the data and not modify it, nor should it move the data to","sourceCodeStart":543,"sourceCodeEnd":579,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/core/hooks.py#L543-L579","documentation":"LightningModule.predict_dataloader is a stub hook that raises MisconfigurationException unless overridden. The Trainer's predict loop requires it to know what data to run prediction over. Invoking prediction without this method (and without passing dataloaders to trainer.predict) triggers the error.","triggerScenarios":"Calling trainer.predict(model) on a module that doesn't implement predict_dataloader, or calling model.predict_dataloader() directly.","commonSituations":"User only implemented train_dataloader/val_dataloader and assumed predict would reuse them; migrated a training script to run inference without adding a predict dataloader.","solutions":["Implement `def predict_dataloader(self)` returning a DataLoader or list of DataLoaders","Alternatively pass the dataloader directly: trainer.predict(model, dataloaders=pred_dl)","If you meant validation instead of prediction, use trainer.validate with val_dataloader"],"exampleFix":"// before\ntrainer.predict(model)\n\n// after\ntrainer.predict(model, dataloaders=DataLoader(pred_ds, batch_size=64))\n# or implement:\n# def predict_dataloader(self): return DataLoader(pred_ds)","handlingStrategy":"validation","validationCode":"if trainer.state.fn == trainer.stateFn.PREDICT and type(model).predict_dataloader is L.LightningModule.predict_dataloader and not dataloaders:\n    raise ValueError('pass dataloaders= or implement predict_dataloader')","typeGuard":"def has_predict_dataloader(model) -> bool:\n    return type(model).predict_dataloader is not L.LightningModule.predict_dataloader","tryCatchPattern":"try:\n    trainer.predict(model)\nexcept MisconfigurationException as e:\n    if 'predict_dataloader' in str(e):\n        preds = trainer.predict(model, dataloaders=DataLoader(ds))","preventionTips":["Prefer passing dataloaders explicitly to trainer.predict for one-off inference","Keep an inference checklist: model.eval(), predict_dataloader or dataloaders arg"],"tags":["pytorch-lightning","lightning-module","dataloader","inference","hook-not-implemented"],"backgroundTag":"required-method-not-implemented","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}