Skip to content

Commit b0e9772

Browse files
author
Daniel Flores
committed
drop bad included index in range
1 parent aa4c960 commit b0e9772

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/torchcodec/samplers/_time_based.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,18 @@ def _generic_time_based_sampler(
196196
)
197197
else:
198198
assert seconds_between_clip_starts is not None # appease type-checker
199+
# The torch.arange documentation warns that floating point rounding errors
200+
# are possible for non-integer steps when comparing to end.
201+
# To prevent this, we check if the last clip_start_seconds value is
202+
# equal to the end value, and remove it.
203+
# docs.pytorch.org/docs/2.8/generated/torch.arange.html
199204
clip_start_seconds = torch.arange(
200205
sampling_range_start,
201206
sampling_range_end, # excluded
202-
round(seconds_between_clip_starts, 6),
207+
seconds_between_clip_starts,
203208
)
209+
if clip_start_seconds[-1] >= sampling_range_end:
210+
clip_start_seconds = clip_start_seconds[:-1]
204211
num_clips = len(clip_start_seconds)
205212

206213
all_clips_timestamps = _build_all_clips_timestamps(

0 commit comments

Comments
 (0)