{"record":{"id":"19c50cbde4572db0","repo":"Lightning-AI/pytorch-lightning","slug":"you-are-trying-to-self-log-but-the-loop-s-resu","errorCode":null,"errorMessage":"You are trying to `self.log()` but the loop's result collection is not registered yet. This is most likely because you are trying to log in a `predict` hook, but it doesn't support logging","messagePattern":"You are trying to `self\\.log\\(\\)` but the loop's result collection is not registered yet\\. This is most likely because you are trying to log in a `predict` hook, but it doesn't support logging","errorType":"exception","errorClass":"MisconfigurationException","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/core/module.py","lineNumber":465,"sourceCode":"        )\n\n        trainer = self._trainer\n        if trainer is None:\n            # not an error to support testing the `*_step` methods without a `Trainer` reference\n            rank_zero_warn(\n                \"You are trying to `self.log()` but the `self.trainer` reference is not registered on the model yet.\"\n                \" This is most likely because the model hasn't been passed to the `Trainer`\"\n            )\n            return\n        if trainer.barebones:\n            rank_zero_warn(\n                \"You are trying to `self.log()` but `Trainer(barebones=True)` is configured.\"\n                \" Logging can impact raw speed so it is disabled under this setting.\"\n            )\n            return\n        results = trainer._results\n        if results is None:\n            raise MisconfigurationException(\n                \"You are trying to `self.log()` but the loop's result collection is not registered\"\n                \" yet. This is most likely because you are trying to log in a `predict` hook,\"\n                \" but it doesn't support logging\"\n            )\n        if self._current_fx_name is None:\n            raise MisconfigurationException(\n                \"You are trying to `self.log()` but it is not managed by the `Trainer` control flow\"\n            )\n\n        on_step, on_epoch = _FxValidator.check_logging_and_get_default_levels(\n            self._current_fx_name, on_step=on_step, on_epoch=on_epoch\n        )\n\n        # make sure user doesn't introduce logic for multi-dataloaders\n        if add_dataloader_idx and \"/dataloader_idx_\" in name:\n            raise MisconfigurationException(\n                f\"You called `self.log` with the key `{name}`\"\n                \" but it should not contain information about `dataloader_idx` when `add_dataloader_idx=True`\"","sourceCodeStart":447,"sourceCodeEnd":483,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/core/module.py#L447-L483","documentation":"self.log requires trainer._results (the loop's result collection) to be registered, which only happens inside Trainer-run hooks. The predict loop does not register a result collection, so logging inside predict_step (or any hook outside managed training/validation flow) raises MisconfigurationException.","triggerScenarios":"Calling self.log(...) inside predict_step, or calling self.log manually outside the Trainer loop (e.g. in __init__ or a plain function before training starts).","commonSituations":"User copied a validation_step containing self.log into predict_step; tried logging metrics during inference; called model.log in a callback before the loop registered results.","solutions":["Remove self.log calls from predict_step; collect outputs from predict_step and compute metrics afterwards from trainer.predict return value","If logging during training/validation, ensure self.log is called within those hooks under Trainer control","Return predictions from predict_step and log via a Logger explicitly (e.g. fabric/loggers or trainer.logger) after inference"],"exampleFix":"# before\ndef predict_step(self, batch, batch_idx):\n    preds = self(batch)\n    self.log('acc', acc)  # raises\n    return preds\n\n# after\ndef predict_step(self, batch, batch_idx):\n    return self(batch)\n# afterwards:\nouts = trainer.predict(model)\nacc = compute_accuracy(outs)\ntrainer.logger.log_metrics({'acc': acc})","handlingStrategy":"validation","validationCode":"def safe_log(model, name, value, **kw):\n    if model.trainer is not None and model.trainer._results is not None:\n        model.log(name, value, **kw)\n    else:\n        print(f'[unlogged] {name}={value}')","typeGuard":"def logging_supported(model) -> bool:\n    t = getattr(model, '_trainer', None)\n    return t is not None and getattr(t, '_results', None) is not None","tryCatchPattern":"from lightning.pytorch.utilities.exceptions import MisconfigurationException\ntry:\n    self.log('m', v)\nexcept MisconfigurationException:\n    pass  # e.g. inside predict_step: collect and log later","preventionTips":["Never call self.log inside predict_step","Accumulate predictions and compute/log metrics after trainer.predict returns"],"tags":["pytorch-lightning","self-log","predict-step","logging","misconfiguration"],"backgroundTag":"logging-outside-training-loop","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}