Lightning-AI/pytorch-lightning · error · MisconfigurationException
With `def training_step(self, dataloader_iter)`, `self.log(.
Error message
With `def training_step(self, dataloader_iter)`, `self.log(..., batch_size=...)` should be provided.
What it means
When training_step is declared as def training_step(self, dataloader_iter), Lightning cannot infer the batch size from a batch argument. self.log uses batch size for correct metric aggregation/logging, so you must pass it explicitly whenever logging in that mode.
Source
Thrown at src/lightning/pytorch/core/module.py:518
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(...))`"
)
if logger is None:
# we could set false here if there's no configured logger, however, we still need to compute the "logged"
# metrics anyway because that's what the evaluation loops use as return value
logger = True
results.log(
self._current_fx_name,
name,
value,
prog_bar=prog_bar,View on GitHub (pinned to 9fed5c27d2)
Solutions
- Pass the batch size explicitly: self.log('loss', loss, batch_size=batch_size) where batch_size comes from your extracted batch
- Extract batch via batch, _ = next(dataloader_iter) and compute its size, then pass it to every self.log call in training_step
- Alternatively revert to def training_step(self, batch, batch_idx) so the Trainer infers batch_size
Example fix
# before
def training_step(self, dataloader_iter):
batch, _ = next(dataloader_iter)
loss = self.step(batch)
self.log('loss', loss) # raises
# after
def training_step(self, dataloader_iter):
batch, _ = next(dataloader_iter)
loss = self.step(batch)
self.log('loss', loss, batch_size=len(batch['x'])) Defensive patterns
Strategy: validation
Validate before calling
if batch_size is None:
batch_size = infer_batch_size(batch) # e.g. batch['x'].shape[0] or len(next(iter))[0])
self.log('loss', loss, batch_size=batch_size) Type guard
def uses_dataloader_iter(module) -> bool:
from lightning.pytorch.utilities.model_helpers import is_param_in_hook_signature
return is_param_in_hook_signature(module.training_step, 'dataloader_iter', explicit=True) Prevention
- Always pass batch_size to self.log when using dataloader_iter-style training_step
- Write a helper that computes batch_size once per step and reuses it for all log calls
When it happens
Trigger: Defining training_step(self, dataloader_iter) (iterable-style dataloader) and calling self.log('loss', loss) without batch_size=... during training.
Common situations: User switched to dataloader_iter signature to unpack data lazily (e.g. streaming, fused dataloaders) and kept old self.log calls; tutorial code migrated from batch-style signature.
Related errors
- Device should be CPU, got {device} instead.
- The `PredictionWriterCallback` does not support using `datal
- 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
- You called `self.log` with the key `{name}` but it should no
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/1ebe823b5389192b.
Report an issue: GitHub.