Skip to content

Commit

Permalink
Add fps info to samplers benchmark (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasHug authored Oct 25, 2024
1 parent d77d2d0 commit 17722e0
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions benchmarks/samplers/benchmark_samplers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@ def bench(f, *args, num_exp=100, warmup=0, **kwargs):
for _ in range(warmup):
f(*args, **kwargs)

num_frames = None
times = []
for _ in range(num_exp):
start = perf_counter_ns()
f(*args, **kwargs)
clips = f(*args, **kwargs)
end = perf_counter_ns()
times.append(end - start)
return torch.tensor(times).float()
num_frames = (
clips.data.shape[0] * clips.data.shape[1]
) # should be constant across calls
return torch.tensor(times).float(), num_frames


def report_stats(times, num_frames, unit="ms"):
fps = num_frames * 1e9 / torch.median(times)

def report_stats(times, unit="ms"):
mul = {
"ns": 1,
"µs": 1e-3,
Expand All @@ -35,13 +41,13 @@ def report_stats(times, unit="ms"):
times = times * mul
std = times.std().item()
med = times.median().item()
print(f"{med = :.2f}{unit} +- {std:.2f}")
return med
print(f"{med = :.2f}{unit} +- {std:.2f} med fps = {fps:.1f}")
return med, fps


def sample(sampler, **kwargs):
decoder = VideoDecoder(VIDEO_PATH)
sampler(
return sampler(
decoder,
num_frames_per_clip=10,
**kwargs,
Expand All @@ -56,34 +62,34 @@ def sample(sampler, **kwargs):
print(f"{num_clips = }")

print("clips_at_random_indices ", end="")
times = bench(
times, num_frames = bench(
sample, clips_at_random_indices, num_clips=num_clips, num_exp=NUM_EXP, warmup=2
)
report_stats(times, unit="ms")
report_stats(times, num_frames, unit="ms")

print("clips_at_regular_indices ", end="")
times = bench(
times, num_frames = bench(
sample, clips_at_regular_indices, num_clips=num_clips, num_exp=NUM_EXP, warmup=2
)
report_stats(times, unit="ms")
report_stats(times, num_frames, unit="ms")

print("clips_at_random_timestamps ", end="")
times = bench(
times, num_frames = bench(
sample,
clips_at_random_timestamps,
num_clips=num_clips,
num_exp=NUM_EXP,
warmup=2,
)
report_stats(times, unit="ms")
report_stats(times, num_frames, unit="ms")

print("clips_at_regular_timestamps ", end="")
seconds_between_clip_starts = 13 / num_clips # approximate. video is 13s long
times = bench(
times, num_frames = bench(
sample,
clips_at_regular_timestamps,
seconds_between_clip_starts=seconds_between_clip_starts,
num_exp=NUM_EXP,
warmup=2,
)
report_stats(times, unit="ms")
report_stats(times, num_frames, unit="ms")

0 comments on commit 17722e0

Please sign in to comment.