Lightning-AI/pytorch-lightning · error · TypeError

You seem to have configured a sampler in your DataLoader whi

Error message

You seem to have configured a sampler in your DataLoader which does not provide finite `__len__` method. The sampler was about to be replaced by `DistributedSamplerWrapper` since `use_distributed_sampler` is True and you are using distributed training. Either provide `__len__` method in your sampler which returns a finite number, remove it from DataLoader or set `use_distributed_sampler=False` if you want to handle distributed sampling yourself.

What it means

Same wrapping path as the missing-__len__ case, but here the sampler implements __len__ yet reports float('inf') (allowed by the Sampler protocol for infinite samplers such as IterDataPipe-based ones). Because DistributedSamplerWrapper must compute per-rank finite subsets, an infinite length cannot be sharded, so TypeError is raised demanding a finite length or manual sharding.

Source

Thrown at src/lightning/fabric/utilities/distributed.py:327

        return device_backend_map[device.type]
    return "gloo"


class _DatasetSamplerWrapper(Dataset):
    """Dataset to create indexes from `Sampler` or `Iterable`"""

    def __init__(self, sampler: Union[Sampler, Iterable]) -> None:
        if not isinstance(sampler, Sized):
            raise TypeError(
                "You seem to have configured a sampler in your DataLoader which"
                " does not provide `__len__` method. The sampler was about to be"
                " replaced by `DistributedSamplerWrapper` since `use_distributed_sampler`"
                " is True and you are using distributed training. Either provide `__len__`"
                " method in your sampler, remove it from DataLoader or set `use_distributed_sampler=False`"
                " if you want to handle distributed sampling yourself."
            )
        if len(sampler) == float("inf"):
            raise TypeError(
                "You seem to have configured a sampler in your DataLoader which"
                " does not provide finite `__len__` method. The sampler was about to be"
                " replaced by `DistributedSamplerWrapper` since `use_distributed_sampler`"
                " is True and you are using distributed training. Either provide `__len__`"
                " method in your sampler which returns a finite number, remove it from DataLoader"
                " or set `use_distributed_sampler=False` if you want to handle distributed sampling yourself."
            )
        self._sampler = sampler
        # defer materializing an iterator until it is necessary
        self._sampler_list: Optional[list[Any]] = None

    @override
    def __getitem__(self, index: int) -> Any:
        if self._sampler_list is None:
            self._sampler_list = list(self._sampler)
        return self._sampler_list[index]

    def __len__(self) -> int:

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Set use_distributed_sampler=False and shard the data yourself (e.g. drop indices where idx % world_size != rank) inside your dataset/iterable
  2. Or give the sampler a finite epoch length (e.g. steps_per_epoch * batch_size) instead of inf
  3. Or wrap the infinite iterable in a dataset that yields a fixed number of batches per epoch

Example fix

# before
class InfiniteSampler(Sampler):
    def __iter__(self): return itertools.cycle(range(10))
    def __len__(self): return float("inf")

fabric.setup_dataloaders(DataLoader(ds, sampler=InfiniteSampler()))

# after
fabric.setup_dataloaders(
    DataLoader(ds, sampler=InfiniteSampler()),
    use_distributed_sampler=False,  # shard inside dataset instead
)
Defensive patterns

Strategy: fallback

Validate before calling

from collections.abc import Sized

if isinstance(sampler, Sized) and len(sampler) == float("inf"):
    use_distributed_sampler = False  # handle sharding yourself

Type guard

def sampler_is_finite(s) -> bool:
    return isinstance(s, Sized) and len(s) != float("inf")

Prevention

When it happens

Trigger: A custom or IterDataPipe sampler whose __len__ returns float('inf') (e.g. default infinite IterDataPipe length) passed to a DataLoader under Fabric/Trainer with use_distributed_sampler=True.

Common situations: Infinite streaming training loops; IterDataPipes without set_epoch/length configured; transferring single-process streaming code to DDP without disabling sampler wrapping.

Related errors


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