Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set thread_count to a value set by FFMPEG in single-video benchmarks and to 1 in concurrent benchmarks #406

Merged
merged 5 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
42 changes: 32 additions & 10 deletions benchmarks/decoders/benchmark_decoders_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def get_consecutive_frames_from_video(self, video_file, numFramesToDecode):

def decode_and_transform(self, video_file, pts_list, height, width, device):
self.torchvision.set_video_backend(self._backend)
reader = self.torchvision.io.VideoReader(video_file, "video")
reader = self.torchvision.io.VideoReader(video_file, "video", num_threads=1)
frames = []
for pts in pts_list:
reader.seek(pts)
Expand Down Expand Up @@ -258,22 +258,28 @@ def get_consecutive_frames_from_video(self, video_file, numFramesToDecode):

class TorchCodecPublic(AbstractDecoder):
def __init__(self, num_ffmpeg_threads=None, device="cpu"):
self._num_ffmpeg_threads = int(num_ffmpeg_threads) if num_ffmpeg_threads else 1
self._num_ffmpeg_threads = num_ffmpeg_threads
self._device = device

from torchvision.transforms import v2 as transforms_v2

self.transforms_v2 = transforms_v2

def get_frames_from_video(self, video_file, pts_list):
num_ffmpeg_threads = (
int(self._num_ffmpeg_threads) if self._num_ffmpeg_threads else 0
)
decoder = VideoDecoder(
video_file, num_ffmpeg_threads=self._num_ffmpeg_threads, device=self._device
video_file, num_ffmpeg_threads=num_ffmpeg_threads, device=self._device
)
return decoder.get_frames_played_at(pts_list)

def get_consecutive_frames_from_video(self, video_file, numFramesToDecode):
num_ffmpeg_threads = (
int(self._num_ffmpeg_threads) if self._num_ffmpeg_threads else 0
)
decoder = VideoDecoder(
video_file, num_ffmpeg_threads=self._num_ffmpeg_threads, device=self._device
video_file, num_ffmpeg_threads=num_ffmpeg_threads, device=self._device
)
frames = []
count = 0
Expand All @@ -285,8 +291,11 @@ def get_consecutive_frames_from_video(self, video_file, numFramesToDecode):
return frames

def decode_and_transform(self, video_file, pts_list, height, width, device):
num_ffmpeg_threads = (
int(self._num_ffmpeg_threads) if self._num_ffmpeg_threads else 1
)
decoder = VideoDecoder(
video_file, num_ffmpeg_threads=self._num_ffmpeg_threads, device=self._device
video_file, num_ffmpeg_threads=num_ffmpeg_threads, device=self._device
)
frames = decoder.get_frames_played_at(pts_list)
frames = self.transforms_v2.functional.resize(frames.data, (height, width))
Expand Down Expand Up @@ -339,7 +348,9 @@ def __init__(self):

def get_frames_from_video(self, video_file, pts_list):
stream_reader = self.torchaudio.io.StreamReader(src=video_file)
stream_reader.add_basic_video_stream(frames_per_chunk=1)
stream_reader.add_basic_video_stream(
frames_per_chunk=1, decoder_option={"threads": "0"}
)
frames = []
for pts in pts_list:
stream_reader.seek(pts)
Expand All @@ -350,7 +361,9 @@ def get_frames_from_video(self, video_file, pts_list):

def get_consecutive_frames_from_video(self, video_file, numFramesToDecode):
stream_reader = self.torchaudio.io.StreamReader(src=video_file)
stream_reader.add_basic_video_stream(frames_per_chunk=1)
stream_reader.add_basic_video_stream(
frames_per_chunk=1, decoder_option={"threads": "0"}
)
frames = []
frame_cnt = 0
for vframe in stream_reader.stream():
Expand All @@ -363,7 +376,9 @@ def get_consecutive_frames_from_video(self, video_file, numFramesToDecode):

def decode_and_transform(self, video_file, pts_list, height, width, device):
stream_reader = self.torchaudio.io.StreamReader(src=video_file)
stream_reader.add_basic_video_stream(frames_per_chunk=1)
stream_reader.add_basic_video_stream(
frames_per_chunk=1, decoder_option={"threads": "1"}
)
frames = []
for pts in pts_list:
stream_reader.seek(pts)
Expand Down Expand Up @@ -634,7 +649,10 @@ def run_benchmarks(
},
label=f"video={video_file_path} {metadata_label}",
sub_label=decoder_name,
description=f"dataloader[threads={bp.num_threads},batch_size={bp.batch_size}] {num_samples} decode_and_transform()",
description=f"concurrent[threads={bp.num_threads},batch_size={bp.batch_size}] {num_samples} decode_and_transform()",
)
print(
f"{decoder_name} concurrent[threads={bp.num_threads} batch_size={bp.batch_size}]"
)
results.append(
dataloader_result.blocked_autorange(
Expand All @@ -647,7 +665,7 @@ def run_benchmarks(
decoder_name,
video_file_path,
num_samples * dataloader_parameters.batch_parameters.batch_size,
f"dataloader[threads={bp.num_threads} batch_size={bp.batch_size}] {num_samples} x decode_and_transform()",
f"concurrent[threads={bp.num_threads} batch_size={bp.batch_size}] {num_samples} x decode_and_transform()",
)
)

Expand All @@ -670,6 +688,7 @@ def run_benchmarks(
sub_label=decoder_name,
description=f"{kind} {num_samples} seek()+next()",
)
print(f"{decoder_name} {kind} {num_samples} seek()+next()")
results.append(
seeked_result.blocked_autorange(min_run_time=min_runtime_seconds)
)
Expand Down Expand Up @@ -697,6 +716,7 @@ def run_benchmarks(
sub_label=decoder_name,
description=f"batch {kind} {num_samples} seek()+next()",
)
print(f"{decoder_name} batch {kind} {num_samples} seek()+next()")
results.append(
seeked_result.blocked_autorange(
min_run_time=min_runtime_seconds
Expand Down Expand Up @@ -724,6 +744,7 @@ def run_benchmarks(
sub_label=decoder_name,
description=f"{num_consecutive_nexts} next()",
)
print(f"{decoder_name} {num_consecutive_nexts} next()")
results.append(
consecutive_frames_result.blocked_autorange(
min_run_time=min_runtime_seconds
Expand Down Expand Up @@ -753,6 +774,7 @@ def run_benchmarks(
sub_label=decoder_name,
description=f"batch {num_consecutive_nexts} next()",
)
print(f"{decoder_name} batch {num_consecutive_nexts} next()")
results.append(
consecutive_frames_result.blocked_autorange(
min_run_time=min_runtime_seconds
Expand Down
Binary file modified benchmarks/decoders/benchmark_readme_chart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading