Lightning-AI/pytorch-lightning · error · MisconfigurationException

Epoch should be an int greater than or equal to 0. Got {list

Error message

Epoch should be an int greater than or equal to 0. Got {list(scheduling.keys())}.

What it means

All keys of the `scheduling` dict must be ints >= 0 (epochs). Any non-int key (float, string) or negative int raises MisconfigurationException listing the offending keys. Validated in `__init__` at construction time.

Source

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

        >>> 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
        self.epochs = sorted(scheduling.keys())

    def going_to_accumulate_grad_batches(self) -> bool:

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Convert keys to int: `{int(k): v for k, v in schedule.items()}`
  2. Fix epoch computation to produce non-negative integers
  3. Check the reported key list in the message to spot float/string/negative entries

Example fix

# before
schedule = json.loads('{"0": 8, "5": 2}')
GradientAccumulationScheduler(schedule)
# after
schedule = {int(k): v for k, v in json.loads(raw).items()}
GradientAccumulationScheduler(schedule)
Defensive patterns

Strategy: validation

Validate before calling

if any(not isinstance(k, int) or isinstance(k, bool) or k < 0 for k in scheduling):
    scheduling = {int(k): v for k, v in scheduling.items()}  # coerce from JSON/YAML strings
assert all(isinstance(k, int) and k >= 0 for k in scheduling)

Type guard

def valid_epoch_keys(s: dict) -> bool:
    return all(type(k) is int and k >= 0 for k in s)

Prevention

When it happens

Trigger: `GradientAccumulationScheduler({0.5: 4})`, `{'1': 8}`, or `{-1: 2}`. Common when the dict comes from JSON/YAML where keys are strings, or from division producing floats.

Common situations: Loading a schedule from JSON (keys always parse as strings); computing epochs with `total/2` yielding a float; negative epoch offsets from off-by-one math.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


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