Skip to content

Commit 17722e0

Browse files
authored
Add fps info to samplers benchmark (#294)
1 parent d77d2d0 commit 17722e0

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

benchmarks/samplers/benchmark_samplers.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,22 @@ def bench(f, *args, num_exp=100, warmup=0, **kwargs):
1616
for _ in range(warmup):
1717
f(*args, **kwargs)
1818

19+
num_frames = None
1920
times = []
2021
for _ in range(num_exp):
2122
start = perf_counter_ns()
22-
f(*args, **kwargs)
23+
clips = f(*args, **kwargs)
2324
end = perf_counter_ns()
2425
times.append(end - start)
25-
return torch.tensor(times).float()
26+
num_frames = (
27+
clips.data.shape[0] * clips.data.shape[1]
28+
) # should be constant across calls
29+
return torch.tensor(times).float(), num_frames
30+
2631

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

28-
def report_stats(times, unit="ms"):
2935
mul = {
3036
"ns": 1,
3137
"µs": 1e-3,
@@ -35,13 +41,13 @@ def report_stats(times, unit="ms"):
3541
times = times * mul
3642
std = times.std().item()
3743
med = times.median().item()
38-
print(f"{med = :.2f}{unit} +- {std:.2f}")
39-
return med
44+
print(f"{med = :.2f}{unit} +- {std:.2f} med fps = {fps:.1f}")
45+
return med, fps
4046

4147

4248
def sample(sampler, **kwargs):
4349
decoder = VideoDecoder(VIDEO_PATH)
44-
sampler(
50+
return sampler(
4551
decoder,
4652
num_frames_per_clip=10,
4753
**kwargs,
@@ -56,34 +62,34 @@ def sample(sampler, **kwargs):
5662
print(f"{num_clips = }")
5763

5864
print("clips_at_random_indices ", end="")
59-
times = bench(
65+
times, num_frames = bench(
6066
sample, clips_at_random_indices, num_clips=num_clips, num_exp=NUM_EXP, warmup=2
6167
)
62-
report_stats(times, unit="ms")
68+
report_stats(times, num_frames, unit="ms")
6369

6470
print("clips_at_regular_indices ", end="")
65-
times = bench(
71+
times, num_frames = bench(
6672
sample, clips_at_regular_indices, num_clips=num_clips, num_exp=NUM_EXP, warmup=2
6773
)
68-
report_stats(times, unit="ms")
74+
report_stats(times, num_frames, unit="ms")
6975

7076
print("clips_at_random_timestamps ", end="")
71-
times = bench(
77+
times, num_frames = bench(
7278
sample,
7379
clips_at_random_timestamps,
7480
num_clips=num_clips,
7581
num_exp=NUM_EXP,
7682
warmup=2,
7783
)
78-
report_stats(times, unit="ms")
84+
report_stats(times, num_frames, unit="ms")
7985

8086
print("clips_at_regular_timestamps ", end="")
8187
seconds_between_clip_starts = 13 / num_clips # approximate. video is 13s long
82-
times = bench(
88+
times, num_frames = bench(
8389
sample,
8490
clips_at_regular_timestamps,
8591
seconds_between_clip_starts=seconds_between_clip_starts,
8692
num_exp=NUM_EXP,
8793
warmup=2,
8894
)
89-
report_stats(times, unit="ms")
95+
report_stats(times, num_frames, unit="ms")

0 commit comments

Comments
 (0)