{"record":{"id":"44d0ac2cee4be372","repo":"Lightning-AI/pytorch-lightning","slug":"you-seem-to-have-configured-a-sampler-in-your-data","errorCode":null,"errorMessage":"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.","messagePattern":"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\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/utilities/distributed.py","lineNumber":318,"sourceCode":"    if _distributed_is_initialized():\n        torch.distributed.destroy_process_group()\n    signal.signal(signal.SIGINT, signal.SIG_DFL)\n\n\ndef _get_default_process_group_backend_for_device(device: torch.device) -> str:\n    \"\"\"Return corresponding distributed backend for a given device.\"\"\"\n    device_backend_map = torch.distributed.Backend.default_device_backend_map\n    if device.type in device_backend_map:\n        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","sourceCodeStart":300,"sourceCodeEnd":336,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/utilities/distributed.py#L300-L336","documentation":"When use_distributed_sampler=True (the default) and you supply a custom sampler to your DataLoader, Lightning wraps it in DistributedSamplerWrapper backed by _DatasetSamplerWrapper, which needs the sampler's length. If the sampler isn't collections.abc.Sized (no __len__), TypeError is raised telling you to add __len__, drop the sampler, or disable the wrapping.","triggerScenarios":"Passing an iterable-style/streams sampler (e.g. torch.utils.data.IterableSampler-like or a custom iterator without __len__) as DataLoader(dataset, sampler=...) with Fabric/Trainer distributed training and default use_distributed_sampler=True.","commonSituations":"Streaming/infinite datasets with custom samplers; migrating single-GPU code that never needed __len__; using BatchSampler or weight-jittered samplers that skip __len__.","solutions":["Add def __len__(self) returning the number of samples to your sampler","Or set use_distributed_sampler=False in DataLoader kwargs (Fabric(...setup_dataloaders(dl, use_distributed_sampler=False))) and shard manually","Or remove the sampler and let Lightning's default DistributedSampler handle sharding"],"exampleFix":"# before\nclass MySampler(Sampler):\n    def __iter__(self): ...\n\ndl = DataLoader(ds, sampler=MySampler())\nfabric.setup_dataloaders(dl)\n\n# after\nclass MySampler(Sampler):\n    def __iter__(self): ...\n    def __len__(self):\n        return len(self.data_source)\n\ndl = DataLoader(ds, sampler=MySampler())\nfabric.setup_dataloaders(dl)","handlingStrategy":"validation","validationCode":"from collections.abc import Sized\n\nif sampler is not None and not isinstance(sampler, Sized):\n    raise ValueError(\"sampler needs __len__; or pass use_distributed_sampler=False\")","typeGuard":"def sampler_is_sized(s) -> bool:\n    return isinstance(s, Sized) and len(s) != float(\"inf\")","tryCatchPattern":null,"preventionTips":["Implement __len__ on custom samplers used with distributed training","Know the default use_distributed_sampler=True wraps user samplers"],"tags":["dataloader","sampler","distributed-training","lightning"],"backgroundTag":"sampler-missing-length","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}