vision icon indicating copy to clipboard operation
vision copied to clipboard

datasets.samplers.DistributedSampler raises AssertionError when padding needs more than one copy of the dataset

Open mottopanikeiku opened this issue 1 month ago • 1 comments

🐛 Describe the bug

torchvision.datasets.samplers.DistributedSampler raises AssertionError when padding needs more than one copy of the dataset, for example two sample groups and five replicas. It pads with indices[: total_size - len(indices)], which is too short in that case. torch.utils.data.DistributedSampler repeats the indices instead.

from torchvision.datasets.samplers import DistributedSampler

for group_size in (1, 3):
    print("group_size:", group_size)
    for rank in range(5):
        sampler = DistributedSampler(
            range(2 * group_size), num_replicas=5, rank=rank, group_size=group_size
        )
        try:
            print("  rank", rank, list(sampler))
        except AssertionError:
            print("  rank", rank, "AssertionError")

Every rank prints AssertionError for both group sizes. Expected, as with the core sampler's repeated padding: [0], [1], [0], [1], [0] for group size 1, and [0, 1, 2], [3, 4, 5], [0, 1, 2], [3, 4, 5], [0, 1, 2] for group size 3.

This makes the video sampler unusable for small validation subsets or smoke tests on more GPUs than there are groups. I have a fix that repeats the indices like the core sampler, with tests for empty and small datasets, both group sizes, shuffling and all ranks, on my fork: https://github.com/pytorch/vision/compare/main...mottopanikeiku:vision:fix-vision-distributed-sampler-padding. Opening a PR is limited to collaborators right now, so I'm leaving it here in case it's useful.

I used AI assistance (Claude and GPT-6 Astra) to find, fix and test this bug.

Versions

torchvision main (268ddfce) Python sources on the 0.29.1+cpu wheel; torch 2.14.1+cpu; Python 3.12.13; Linux.

mottopanikeiku avatar Oct 17 '26 05:10 mottopanikeiku

Submitted fix with regression tests in #9712. We also applied the identical fix to references/classification/sampler.py (RASampler), which shared the same padding logic.

Rik-Freeman avatar Oct 21 '26 15:10 Rik-Freeman