Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/torchcodec/samplers/_time_based.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,22 @@ def _generic_time_based_sampler(
)
else:
assert seconds_between_clip_starts is not None # appease type-checker
# The torch.arange documentation warns that floating point rounding errors
# are possible for non-integer steps when comparing to end.
# docs.pytorch.org/docs/2.8/generated/torch.arange.html
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment can probably be removed now that we have the one below

clip_start_seconds = torch.arange(
sampling_range_start,
sampling_range_end, # excluded
seconds_between_clip_starts,
)
# As mentioned in the docs, torch.arange may return values
# equal to or above `end` because of floating precision errors.
# Here, we manually ensure all values are strictly lower than `sample_range_end`
if clip_start_seconds[-1] >= sampling_range_end:
clip_start_seconds = clip_start_seconds[
clip_start_seconds < sampling_range_end
]

num_clips = len(clip_start_seconds)

all_clips_timestamps = _build_all_clips_timestamps(
Expand Down
35 changes: 34 additions & 1 deletion test/test_samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from torchcodec.samplers._index_based import _build_all_clips_indices
from torchcodec.samplers._time_based import _build_all_clips_timestamps

from .utils import assert_frames_equal, NASA_VIDEO
from .utils import assert_frames_equal, H265_10BITS, NASA_VIDEO


def _assert_output_type_and_shapes(
Expand Down Expand Up @@ -698,3 +698,36 @@ def test_build_all_clips_timestamps(
assert all(isinstance(timestamp, float) for timestamp in all_clips_timestamps)
assert len(all_clips_timestamps) == len(clip_start_seconds) * NUM_FRAMES_PER_CLIP
assert all_clips_timestamps == expected_all_clips_timestamps


@pytest.mark.parametrize("policy", ("repeat_last", "wrap", "error"))
def test_floating_point_precision_in_clips_at_regular_timestamps(policy):
# Test that floating point precision errors in torch.arange do not return empty clips.
# Using 1/3 would cause arange to include sampling_range_end, which gets filtered out
# in _build_all_clips_timestamps, leaving clips with no frames.
# The fix rounds seconds_between_clip_starts to prevent this.
seconds_between_clip_starts = 1 / 3 - 1e-9

decoder = VideoDecoder(H265_10BITS.path) # Video is 1 second long
# Set sampling range so that last clip will have frame timestamp ≈ end_stream_seconds
sampling_range_start = 0
sampling_range_end = decoder.metadata.end_stream_seconds
seconds_between_frames = 1
num_frames_per_clip = 1

clips = clips_at_regular_timestamps(
decoder,
seconds_between_clip_starts=seconds_between_clip_starts,
sampling_range_start=sampling_range_start,
sampling_range_end=sampling_range_end,
num_frames_per_clip=num_frames_per_clip,
seconds_between_frames=seconds_between_frames,
policy=policy,
)

# Ensure frame PTS can be decoded
for clip in clips:
frames = decoder.get_frames_played_at(seconds=clip.pts_seconds.tolist())
assert isinstance(frames, FrameBatch)
assert frames.data.shape[0] == len(clip.pts_seconds)
assert len(clip.pts_seconds) == num_frames_per_clip
Loading