Skip to content

Commit

Permalink
Added batch parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadsharif1 committed Nov 12, 2024
1 parent 9ed1992 commit 85ec39e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
3 changes: 2 additions & 1 deletion benchmarks/decoders/benchmark_decoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from benchmark_decoders_library import (
AbstractDecoder,
BatchParameters,
DecordAccurate,
DecordAccurateBatch,
plot_data,
Expand Down Expand Up @@ -173,7 +174,7 @@ def main() -> None:
num_sequential_frames_from_start=[1, 10, 100],
min_runtime_seconds=args.bm_video_speed_min_run_seconds,
benchmark_video_creation=args.bm_video_creation,
batch_size=40,
batch_parameters=BatchParameters(num_threads=8, batch_size=40),
)
plot_data(df_data, args.plot_path)

Expand Down
34 changes: 22 additions & 12 deletions benchmarks/decoders/benchmark_decoders_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from concurrent.futures import ThreadPoolExecutor, wait
from itertools import product
from pathlib import Path
from typing import NamedTuple

import matplotlib.pyplot as plt
import numpy as np
Expand Down Expand Up @@ -479,10 +480,19 @@ def get_metadata(video_file_path: str) -> VideoStreamMetadata:
return VideoDecoder(video_file_path).metadata


def run_batch_using_threads(function, *args, num_threads=10, batch_size=40):
executor = ThreadPoolExecutor(max_workers=10)
class BatchParameters(NamedTuple):
num_threads: int
batch_size: int


def run_batch_using_threads(
function,
*args,
batch_parameters: BatchParameters = BatchParameters(num_threads=8, batch_size=40),
):
executor = ThreadPoolExecutor(max_workers=batch_parameters.num_threads)
futures = []
for _ in range(batch_size):
for _ in range(batch_parameters.batch_size):
futures.append(executor.submit(function, *args))
for f in futures:
assert f.result()
Expand Down Expand Up @@ -513,7 +523,7 @@ def run_benchmarks(
num_sequential_frames_from_start: list[int],
min_runtime_seconds: float,
benchmark_video_creation: bool,
batch_size: int = 0,
batch_parameters: BatchParameters = None,
) -> list[dict[str, str | float | int]]:
# Ensure that we have the same seed across benchmark runs.
torch.manual_seed(0)
Expand Down Expand Up @@ -570,15 +580,15 @@ def run_benchmarks(
)
)

if batch_size > 0:
if batch_parameters:
seeked_result = benchmark.Timer(
stmt="run_batch_using_threads(decoder.get_frames_from_video, video_file, pts_list, batch_size=batch_size)",
stmt="run_batch_using_threads(decoder.get_frames_from_video, video_file, pts_list, batch_parameters=batch_parameters)",
globals={
"video_file": str(video_file_path),
"pts_list": pts_list,
"decoder": decoder,
"run_batch_using_threads": run_batch_using_threads,
"batch_size": batch_size,
"batch_parameters": batch_parameters,
},
label=f"video={video_file_path} {metadata_label}",
sub_label=decoder_name,
Expand All @@ -594,7 +604,7 @@ def run_benchmarks(
results[-1],
decoder_name,
video_file_path,
num_samples * batch_size,
num_samples * batch_parameters.batch_size,
f"batch {kind} seek()+next()",
)
)
Expand Down Expand Up @@ -626,15 +636,15 @@ def run_benchmarks(
)
)

if batch_size > 0:
if batch_parameters:
consecutive_frames_result = benchmark.Timer(
stmt="run_batch_using_threads(decoder.get_consecutive_frames_from_video, video_file, consecutive_frames_to_extract, batch_size=batch_size)",
stmt="run_batch_using_threads(decoder.get_consecutive_frames_from_video, video_file, consecutive_frames_to_extract, batch_parameters=batch_parameters)",
globals={
"video_file": str(video_file_path),
"consecutive_frames_to_extract": num_consecutive_nexts,
"decoder": decoder,
"run_batch_using_threads": run_batch_using_threads,
"batch_size": batch_size,
"batch_parameters": batch_parameters,
},
label=f"video={video_file_path} {metadata_label}",
sub_label=decoder_name,
Expand All @@ -650,7 +660,7 @@ def run_benchmarks(
results[-1],
decoder_name,
video_file_path,
num_consecutive_nexts * batch_size,
num_consecutive_nexts * batch_parameters.batch_size,
f"batch {num_consecutive_nexts} next()",
)
)
Expand Down
1 change: 0 additions & 1 deletion benchmarks/decoders/generate_readme_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ def main() -> None:
num_sequential_frames_from_start=[100],
min_runtime_seconds=30,
benchmark_video_creation=False,
batch_size=0,
)
df_data.append(
{
Expand Down

0 comments on commit 85ec39e

Please sign in to comment.