{"record":{"id":"cd2dca0613abe1b0","repo":"Lightning-AI/pytorch-lightning","slug":"you-seem-to-have-configured-a-sampler-in-your-data-cd2dca","errorCode":null,"errorMessage":"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.","messagePattern":"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\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/utilities/distributed.py","lineNumber":327,"sourceCode":"        return device_backend_map[device.type]\n    return \"gloo\"\n\n\nclass _DatasetSamplerWrapper(Dataset):\n    \"\"\"Dataset to create indexes from `Sampler` or `Iterable`\"\"\"\n\n    def __init__(self, sampler: Union[Sampler, Iterable]) -> None:\n        if not isinstance(sampler, Sized):\n            raise TypeError(\n                \"You seem to have configured a sampler in your DataLoader which\"\n                \" does not provide `__len__` method. The sampler was about to be\"\n                \" replaced by `DistributedSamplerWrapper` since `use_distributed_sampler`\"\n                \" is True and you are using distributed training. Either provide `__len__`\"\n                \" method in your sampler, remove it from DataLoader or set `use_distributed_sampler=False`\"\n                \" if you want to handle distributed sampling yourself.\"\n            )\n        if len(sampler) == float(\"inf\"):\n            raise TypeError(\n                \"You seem to have configured a sampler in your DataLoader which\"\n                \" does not provide finite `__len__` method. The sampler was about to be\"\n                \" replaced by `DistributedSamplerWrapper` since `use_distributed_sampler`\"\n                \" is True and you are using distributed training. Either provide `__len__`\"\n                \" method in your sampler which returns a finite number, remove it from DataLoader\"\n                \" or set `use_distributed_sampler=False` if you want to handle distributed sampling yourself.\"\n            )\n        self._sampler = sampler\n        # defer materializing an iterator until it is necessary\n        self._sampler_list: Optional[list[Any]] = None\n\n    @override\n    def __getitem__(self, index: int) -> Any:\n        if self._sampler_list is None:\n            self._sampler_list = list(self._sampler)\n        return self._sampler_list[index]\n\n    def __len__(self) -> int:","sourceCodeStart":309,"sourceCodeEnd":345,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/utilities/distributed.py#L309-L345","documentation":"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.","triggerScenarios":"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.","commonSituations":"Infinite streaming training loops; IterDataPipes without set_epoch/length configured; transferring single-process streaming code to DDP without disabling sampler wrapping.","solutions":["Set use_distributed_sampler=False and shard the data yourself (e.g. drop indices where idx % world_size != rank) inside your dataset/iterable","Or give the sampler a finite epoch length (e.g. steps_per_epoch * batch_size) instead of inf","Or wrap the infinite iterable in a dataset that yields a fixed number of batches per epoch"],"exampleFix":"# before\nclass InfiniteSampler(Sampler):\n    def __iter__(self): return itertools.cycle(range(10))\n    def __len__(self): return float(\"inf\")\n\nfabric.setup_dataloaders(DataLoader(ds, sampler=InfiniteSampler()))\n\n# after\nfabric.setup_dataloaders(\n    DataLoader(ds, sampler=InfiniteSampler()),\n    use_distributed_sampler=False,  # shard inside dataset instead\n)","handlingStrategy":"fallback","validationCode":"from collections.abc import Sized\n\nif isinstance(sampler, Sized) and len(sampler) == float(\"inf\"):\n    use_distributed_sampler = False  # handle sharding yourself","typeGuard":"def sampler_is_finite(s) -> bool:\n    return isinstance(s, Sized) and len(s) != float(\"inf\")","tryCatchPattern":null,"preventionTips":["Give streaming samplers a finite per-epoch length or disable Lightning's sampler wrapping","Shard infinite iterables by rank inside the dataset (skip items where idx % world_size != rank)"],"tags":["dataloader","sampler","infinite-iterator","distributed-training"],"backgroundTag":"infinite-sampler-length","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}