Clip samplers crash on iteration when no video clips are available

Open Beginner friendly
#9,733 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@raunakpandey1 is already working on this.

Since Nov 2, 2026.

  • #9734 by @raunakpandey1 — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
65/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
data

Research direction

Start with the UniformClipSampler and RandomClipSampler classes, where torch.cat is called on a possibly empty list of indices, and with VideoClips for how it skips short videos. Run the reproduction script in the issue to confirm the ValueError for each sampler. Done means all four sampler and empty-input combinations yield [] with no exception, and a regression test covers both the short-video and empty-video cases. The reporter says they plan to open a PR, so check for one before starting.

Written by the indexing model from the issue text.

Description

🐛 Describe the bug

UniformClipSampler reports length zero but raises an exception when every video is shorter than clip_length_in_frames. VideoClips warns that these videos will be skipped, so iterating the sampler should produce no indices.

Both UniformClipSampler and RandomClipSampler also fail when the video list is empty (for example, after VideoClips.subset([])). Both failures come from calling torch.cat(idxs) with an empty list.

Reproduction using precomputed timestamps, without video files or a decoder:

import torch
from torchvision.datasets.samplers import RandomClipSampler, UniformClipSampler
from torchvision.datasets.video_utils import VideoClips

for frame_counts in ([4], []):
    paths = [f"video_{i}.mp4" for i in range(len(frame_counts))]
    clips = VideoClips(
        paths,
        clip_length_in_frames=5,
        frames_between_clips=5,
        _precomputed_metadata={
            "video_paths": paths,
            "video_pts": [torch.arange(n) for n in frame_counts],
            "video_fps": [30.0] * len(paths),
        },
    )
    for sampler_cls in (UniformClipSampler, RandomClipSampler):
        sampler = sampler_cls(clips, 3)
        print(frame_counts, sampler_cls.__name__, "length:", len(sampler))
        try:
            print(list(sampler))
        except ValueError as error:
            print(type(error).__name__ + ": " + str(error))

Observed (in addition to the expected short-video warning):

[4] UniformClipSampler length: 0
ValueError: torch.cat(): expected a non-empty list of Tensors
[4] RandomClipSampler length: 0
[]
[] UniformClipSampler length: 0
ValueError: torch.cat(): expected a non-empty list of Tensors
[] RandomClipSampler length: 0
ValueError: torch.cat(): expected a non-empty list of Tensors

Expected: all four samplers yield [], consistent with their lengths. This also affects iteration through DistributedSampler wrapping these clip samplers.

I have a small fix and regression tests prepared and will submit a PR. Searches for both sampler names and empty-clip sampling did not find an existing report or competing fix.

Versions
  • Torchvision source: main at 134f6382956071833f05f072763c03f9858453f2 (0.30.0a0+134f638), built locally with native ops.
  • PyTorch: 2.16.0.dev20261102
  • Python: 3.13.3
  • macOS 27.2, arm64; CPU reproduction.
Dominant language
Python
Stars
18k
Forks
7.3k
Avg merge
1h
Merged PRs (30d)
3

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pytorch/vision

All issues in pytorch/vision

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.