Lightning-AI/pytorch-lightning · error · MisconfigurationException
You are trying to `self.log()` but the loop's result collect
Error message
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
What it means
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.
Source
Thrown at src/lightning/pytorch/core/module.py:465
)
trainer = self._trainer
if trainer is None:
# not an error to support testing the `*_step` methods without a `Trainer` reference
rank_zero_warn(
"You are trying to `self.log()` but the `self.trainer` reference is not registered on the model yet."
" This is most likely because the model hasn't been passed to the `Trainer`"
)
return
if trainer.barebones:
rank_zero_warn(
"You are trying to `self.log()` but `Trainer(barebones=True)` is configured."
" Logging can impact raw speed so it is disabled under this setting."
)
return
results = trainer._results
if results is None:
raise MisconfigurationException(
"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"
)
if self._current_fx_name is None:
raise MisconfigurationException(
"You are trying to `self.log()` but it is not managed by the `Trainer` control flow"
)
on_step, on_epoch = _FxValidator.check_logging_and_get_default_levels(
self._current_fx_name, on_step=on_step, on_epoch=on_epoch
)
# make sure user doesn't introduce logic for multi-dataloaders
if add_dataloader_idx and "/dataloader_idx_" in name:
raise MisconfigurationException(
f"You called `self.log` with the key `{name}`"
" but it should not contain information about `dataloader_idx` when `add_dataloader_idx=True`"View on GitHub (pinned to 9fed5c27d2)
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
Example fix
# before
def predict_step(self, batch, batch_idx):
preds = self(batch)
self.log('acc', acc) # raises
return preds
# after
def predict_step(self, batch, batch_idx):
return self(batch)
# afterwards:
outs = trainer.predict(model)
acc = compute_accuracy(outs)
trainer.logger.log_metrics({'acc': acc}) Defensive patterns
Strategy: validation
Validate before calling
def safe_log(model, name, value, **kw):
if model.trainer is not None and model.trainer._results is not None:
model.log(name, value, **kw)
else:
print(f'[unlogged] {name}={value}') Type guard
def logging_supported(model) -> bool:
t = getattr(model, '_trainer', None)
return t is not None and getattr(t, '_results', None) is not None Try / catch
from lightning.pytorch.utilities.exceptions import MisconfigurationException
try:
self.log('m', v)
except MisconfigurationException:
pass # e.g. inside predict_step: collect and log later Prevention
- Never call self.log inside predict_step
- Accumulate predictions and compute/log metrics after trainer.predict returns
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- You are trying to `self.log()` but it is not managed by the
- Device should be CPU, got {device} instead.
- f"You can't `self.log()` inside `{fx_name}`. HINT: You can s
- m.format("on_step", on_step, fx_name, fx_config["allowed_on_
- m.format("on_epoch", on_epoch, fx_name, fx_config["allowed_o
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/19c50cbde4572db0.
Report an issue: GitHub.