Lightning-AI/pytorch-lightning · error · TypeError

Empty dict cannot be interpreted correct

Error message

Empty dict cannot be interpreted correct

What it means

GradientAccumulationScheduler requires a non-empty `scheduling` dict mapping epoch -> accumulation factor. Passing `{}` raises TypeError('Empty dict cannot be interpreted correct') in `__init__` because there is no default schedule to fall back on.

Source

Thrown at src/lightning/pytorch/callbacks/gradient_accumulation_scheduler.py:71

            If ``minimal_epoch`` is less than 0.

    Example::

        >>> from lightning.pytorch import Trainer
        >>> from lightning.pytorch.callbacks import GradientAccumulationScheduler

        # from epoch 5, it starts accumulating every 2 batches. Here we have 4 instead of 5
        # because epoch (key) should be zero-indexed.
        >>> accumulator = GradientAccumulationScheduler(scheduling={4: 2})
        >>> trainer = Trainer(callbacks=[accumulator])

    """

    def __init__(self, scheduling: dict[int, int]):
        super().__init__()

        if not scheduling:  # empty dict error
            raise TypeError("Empty dict cannot be interpreted correct")

        if any(not isinstance(key, int) or key < 0 for key in scheduling):
            raise MisconfigurationException(
                f"Epoch should be an int greater than or equal to 0. Got {list(scheduling.keys())}."
            )

        if any(not isinstance(value, int) or value < 1 for value in scheduling.values()):
            raise MisconfigurationException(
                f"Accumulation factor should be an int greater than 0. Got {list(scheduling.values())}."
            )

        minimal_epoch = min(scheduling.keys())
        if minimal_epoch < 0:
            raise IndexError(f"Epochs indexing from 1, epoch {minimal_epoch} cannot be interpreted correct")
        if minimal_epoch != 0:  # if user didn't define first epoch accumulation factor
            scheduling.update({0: 1})

        self.scheduling = scheduling

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Provide at least one entry, e.g. `{0: 1}` (accumulate 1 from epoch 0)
  2. If you want no accumulation, remove the callback entirely instead of passing an empty dict
  3. Guard programmatically-built dicts: `scheduling or {0: 1}`

Example fix

# before
GradientAccumulationScheduler({})
# after
GradientAccumulationScheduler({0: 1})
Defensive patterns

Strategy: validation

Validate before calling

scheduling = scheduling or {0: 1}
assert scheduling, 'scheduling must not be empty'

Type guard

def is_valid_schedule(scheduling: dict) -> bool:
    return bool(scheduling) and all(isinstance(k, int) and k >= 0 for k in scheduling) and all(isinstance(v, int) and v >= 1 for v in scheduling.values())

Prevention

When it happens

Trigger: Calling `GradientAccumulationScheduler({})`, commonly when the schedule dict is built programmatically (loop/filter/hyperparameter search) and ends up empty.

Common situations: Sweep configs where accumulation schedule is parameterized and collapses to empty; filtering a schedule dict conditionally; YAML config omission parsed to empty mapping.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/4236c4a042170594. Report an issue: GitHub.