Lightning-AI/pytorch-lightning · error · MisconfigurationException

When using an IterableDataset for `train_dataloader`, `Train

Error message

When using an IterableDataset for `train_dataloader`, `Trainer(val_check_interval)` must be time based, `1.0` or an int. An int k specifies checking validation every k training batches.

What it means

Raised in FitLoop.setup_data when the train dataloader has no length on all ranks (typical for IterableDataset) and val_check_interval is neither time-based nor exactly 1.0 nor an int. With unlengthed iterable data Lightning cannot map a fractional interval to a batch count, so only 1.0 (validate every epoch, encoded as inf) or integer batch counts are accepted.

Source

Thrown at src/lightning/pytorch/loops/fit_loop.py:311

        elif isinstance(trainer.val_check_interval, int):
            trainer.val_check_batch = trainer.val_check_interval
            if (
                trainer.val_check_batch > self.max_batches
                and trainer.check_val_every_n_epoch is not None
                and trainer.limit_val_batches > 0
            ):
                raise ValueError(
                    f" `val_check_interval` ({trainer.val_check_interval}) must be less than or equal"
                    f" to the number of the training batches ({self.max_batches})."
                    " If you want to disable validation set `limit_val_batches` to 0.0 instead."
                    " If you want to validate based on the total training batches, set `check_val_every_n_epoch=None`."
                )
        else:
            if not has_len_all_ranks_:
                if trainer.val_check_interval == 1.0:
                    trainer.val_check_batch = float("inf")
                else:
                    raise MisconfigurationException(
                        "When using an IterableDataset for `train_dataloader`,"
                        " `Trainer(val_check_interval)` must be time based, `1.0` or an int. An int k specifies"
                        " checking validation every k training batches."
                    )
            else:
                trainer.val_check_batch = int(self.max_batches * trainer.val_check_interval)
                trainer.val_check_batch = max(1, trainer.val_check_batch)

        if trainer.loggers and self.max_batches < trainer.log_every_n_steps and not trainer.fast_dev_run:
            rank_zero_warn(
                f"The number of training batches ({self.max_batches}) is smaller than the logging interval"
                f" Trainer(log_every_n_steps={trainer.log_every_n_steps}). Set a lower value for log_every_n_steps if"
                " you want to see logs for the training epoch.",
                category=PossibleUserWarning,
            )

        if self.max_batches < trainer.accumulate_grad_batches:
            rank_zero_warn(

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Set val_check_interval=1.0 to validate once per epoch
  2. Use an integer val_check_interval (e.g. 500) to validate every k batches
  3. Use a time-based interval via `Trainer(val_check_interval=<timedelta>)` with a TQDMProgressBar with refresh_rate
  4. Wrap the iterable dataset in a length-aware dataset if a fraction is essential

Example fix

# before
trainer = pl.Trainer(val_check_interval=0.5)  # train_dataloader is an IterableDataset

# after
trainer = pl.Trainer(val_check_interval=500)  # validate every 500 batches
Defensive patterns

Strategy: validation

Validate before calling

from torch.utils.data import IterableDataset

is_iterable = isinstance(train_dataloader.dataset, IterableDataset) or len(hasattr(train_dataloader, '__len__') and train_dataloader) == 0
if is_iterable and isinstance(trainer_config['val_check_interval'], float) and trainer_config['val_check_interval'] != 1.0:
    trainer_config['val_check_interval'] = 1.0  # or int k / timedelta
trainer = pl.Trainer(**trainer_config)

Type guard

def vci_supported_for_iterable(vci) -> bool:
    if isinstance(vci, float):
        return vci == 1.0
    return isinstance(vci, int) or isinstance(vci, timedelta)

Prevention

When it happens

Trigger: Using an IterableDataset for train_dataloader together with `Trainer(val_check_interval=0.5)` or any fractional value other than 1.0; streaming datasets (TFRecords, Kafka, webdataset streams) that do not implement __len__.

Common situations: Migrating a map-style dataset pipeline (with val_check_interval=0.25) to a streaming pipeline; reusing Trainer flags from a standard DataLoader setup with an iterable-style dataset; large-scale data pipelines using StreamingDataset.

Related errors


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