Lightning-AI/pytorch-lightning · error · MisconfigurationException
Could not find the `LightningModule` attribute for the `torc
Error message
Could not find the `LightningModule` attribute for the `torchmetrics.Metric` logged. You can fix this by calling `self.log({name}, ..., metric_attribute=name)` where `name` is one of {list(self._metric_attributes.values)} What it means
The logged torchmetrics.Metric object was not found among the module's registered metric attributes (id lookup in _metric_attributes failed). Lightning needs an attribute path to checkpoint/restore the metric, so you must tell it where the metric lives via metric_attribute.
Source
Thrown at src/lightning/pytorch/core/module.py:507
# if we started a new epoch (running its first batch) the hook name has changed
# reset any tensors for the new hook name
results.reset(metrics=False, fx=self._current_fx_name)
if metric_attribute is None and isinstance(value, Metric):
if self._metric_attributes is None:
# compute once
self._metric_attributes = {
id(module): name for name, module in self.named_modules() if isinstance(module, Metric)
}
if not self._metric_attributes:
raise MisconfigurationException(
"Could not find the `LightningModule` attribute for the `torchmetrics.Metric` logged."
" You can fix this by setting an attribute for the metric in your `LightningModule`."
)
# try to find the passed metric in the LightningModule
metric_attribute = self._metric_attributes.get(id(value), None)
if metric_attribute is None:
raise MisconfigurationException(
"Could not find the `LightningModule` attribute for the `torchmetrics.Metric` logged."
f" You can fix this by calling `self.log({name}, ..., metric_attribute=name)` where `name` is one"
f" of {list(self._metric_attributes.values())}"
)
if (
trainer.training
and is_param_in_hook_signature(self.training_step, "dataloader_iter", explicit=True)
and batch_size is None
):
raise MisconfigurationException(
"With `def training_step(self, dataloader_iter)`, `self.log(..., batch_size=...)` should be provided."
)
if logger and trainer.logger is None:
rank_zero_warn(
f"You called `self.log({name!r}, ..., logger=True)` but have no logger configured. You can enable one"
" by doing `Trainer(logger=ALogger(...))`"View on GitHub (pinned to 9fed5c27d2)
Solutions
- Log the exact attribute object: self.log('acc', self.my_metric)
- Pass the attribute name: self.log('acc', metric, metric_attribute='my_metric')
- Store metrics in nn.ModuleDict so they are discoverable by named_modules
Example fix
# before
self.log('acc', my_local_metric) # not the registered attribute object
# after
self.log('acc', my_local_metric, metric_attribute='my_metric')
# where self.my_metric was defined in __init__ Defensive patterns
Strategy: validation
Validate before calling
attr = next((n for n, m in self.named_modules() if m is metric), None)
if attr is None:
self.log(name, metric.compute(), batch_size=bs) # log value instead
else:
self.log(name, metric) Type guard
def metric_attribute_of(module, metric):
return next((n for n, m in module.named_modules() if m is metric), None) Prevention
- Log the exact stored attribute object, not copies
- Pass metric_attribute explicitly when wrapping metrics in containers
When it happens
Trigger: Calling self.log('acc', metric) where metric is a duplicate object, a copy, or held in a structure whose attribute name doesn't match any registered Metric (e.g. created ad hoc but other metrics exist on the module).
Common situations: User deep-copied a metric, wrapped it in a custom container, or logs a metric stored in a plain python dict attribute; mismatch after reload/mutation of metric objects.
Related errors
- Could not find the `LightningModule` attribute for the `torc
- Device should be CPU, got {device} instead.
- `devices` selected with `CPUAccelerator` should be an int >
- You are trying to `self.log()` but the loop's result collect
- You are trying to `self.log()` but it is not managed by the
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/2b896a9ae37d798d.
Report an issue: GitHub.