Skip to content

Commit 7e2da5d

Browse files
authored
Set thread_count to a value set by FFMPEG in single-video benchmarks and to 1 in concurrent benchmarks (#406)
1 parent fd4c288 commit 7e2da5d

File tree

3 files changed

+457
-408
lines changed

3 files changed

+457
-408
lines changed

benchmarks/decoders/benchmark_decoders_library.py

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def get_consecutive_frames_from_video(self, video_file, numFramesToDecode):
123123

124124
def decode_and_transform(self, video_file, pts_list, height, width, device):
125125
self.torchvision.set_video_backend(self._backend)
126-
reader = self.torchvision.io.VideoReader(video_file, "video")
126+
reader = self.torchvision.io.VideoReader(video_file, "video", num_threads=1)
127127
frames = []
128128
for pts in pts_list:
129129
reader.seek(pts)
@@ -177,15 +177,20 @@ def get_consecutive_frames_from_video(self, video_file, numFramesToDecode):
177177

178178
class TorchCodecCoreNonBatch(AbstractDecoder):
179179
def __init__(self, num_threads=None, color_conversion_library=None, device="cpu"):
180-
self._num_threads = int(num_threads) if num_threads else None
180+
self._num_threads = num_threads
181181
self._color_conversion_library = color_conversion_library
182182
self._device = device
183183

184+
from torchvision.transforms import v2 as transforms_v2
185+
186+
self.transforms_v2 = transforms_v2
187+
184188
def get_frames_from_video(self, video_file, pts_list):
185189
decoder = create_from_file(video_file)
190+
num_threads = int(self._num_threads) if self._num_threads else 0
186191
_add_video_stream(
187192
decoder,
188-
num_threads=self._num_threads,
193+
num_threads=num_threads,
189194
color_conversion_library=self._color_conversion_library,
190195
device=self._device,
191196
)
@@ -199,10 +204,11 @@ def get_frames_from_video(self, video_file, pts_list):
199204
return frames
200205

201206
def get_consecutive_frames_from_video(self, video_file, numFramesToDecode):
207+
num_threads = int(self._num_threads) if self._num_threads else 0
202208
decoder = create_from_file(video_file)
203209
_add_video_stream(
204210
decoder,
205-
num_threads=self._num_threads,
211+
num_threads=num_threads,
206212
color_conversion_library=self._color_conversion_library,
207213
device=self._device,
208214
)
@@ -214,6 +220,29 @@ def get_consecutive_frames_from_video(self, video_file, numFramesToDecode):
214220

215221
return frames
216222

223+
def decode_and_transform(self, video_file, pts_list, height, width, device):
224+
num_threads = int(self._num_threads) if self._num_threads else 1
225+
decoder = create_from_file(video_file)
226+
_add_video_stream(
227+
decoder,
228+
num_threads=num_threads,
229+
color_conversion_library=self._color_conversion_library,
230+
device=self._device,
231+
)
232+
233+
frames = []
234+
for pts in pts_list:
235+
seek_to_pts(decoder, pts)
236+
frame, *_ = get_next_frame(decoder)
237+
frames.append(frame)
238+
239+
frames = [
240+
self.transforms_v2.functional.resize(frame.to(device), (height, width))
241+
for frame in frames
242+
]
243+
244+
return frames
245+
217246

218247
class TorchCodecCoreBatch(AbstractDecoder):
219248
def __init__(self, num_threads=None, color_conversion_library=None, device="cpu"):
@@ -258,22 +287,28 @@ def get_consecutive_frames_from_video(self, video_file, numFramesToDecode):
258287

259288
class TorchCodecPublic(AbstractDecoder):
260289
def __init__(self, num_ffmpeg_threads=None, device="cpu"):
261-
self._num_ffmpeg_threads = int(num_ffmpeg_threads) if num_ffmpeg_threads else 1
290+
self._num_ffmpeg_threads = num_ffmpeg_threads
262291
self._device = device
263292

264293
from torchvision.transforms import v2 as transforms_v2
265294

266295
self.transforms_v2 = transforms_v2
267296

268297
def get_frames_from_video(self, video_file, pts_list):
298+
num_ffmpeg_threads = (
299+
int(self._num_ffmpeg_threads) if self._num_ffmpeg_threads else 0
300+
)
269301
decoder = VideoDecoder(
270-
video_file, num_ffmpeg_threads=self._num_ffmpeg_threads, device=self._device
302+
video_file, num_ffmpeg_threads=num_ffmpeg_threads, device=self._device
271303
)
272304
return decoder.get_frames_played_at(pts_list)
273305

274306
def get_consecutive_frames_from_video(self, video_file, numFramesToDecode):
307+
num_ffmpeg_threads = (
308+
int(self._num_ffmpeg_threads) if self._num_ffmpeg_threads else 0
309+
)
275310
decoder = VideoDecoder(
276-
video_file, num_ffmpeg_threads=self._num_ffmpeg_threads, device=self._device
311+
video_file, num_ffmpeg_threads=num_ffmpeg_threads, device=self._device
277312
)
278313
frames = []
279314
count = 0
@@ -285,8 +320,11 @@ def get_consecutive_frames_from_video(self, video_file, numFramesToDecode):
285320
return frames
286321

287322
def decode_and_transform(self, video_file, pts_list, height, width, device):
323+
num_ffmpeg_threads = (
324+
int(self._num_ffmpeg_threads) if self._num_ffmpeg_threads else 1
325+
)
288326
decoder = VideoDecoder(
289-
video_file, num_ffmpeg_threads=self._num_ffmpeg_threads, device=self._device
327+
video_file, num_ffmpeg_threads=num_ffmpeg_threads, device=self._device
290328
)
291329
frames = decoder.get_frames_played_at(pts_list)
292330
frames = self.transforms_v2.functional.resize(frames.data, (height, width))
@@ -339,7 +377,9 @@ def __init__(self):
339377

340378
def get_frames_from_video(self, video_file, pts_list):
341379
stream_reader = self.torchaudio.io.StreamReader(src=video_file)
342-
stream_reader.add_basic_video_stream(frames_per_chunk=1)
380+
stream_reader.add_basic_video_stream(
381+
frames_per_chunk=1, decoder_option={"threads": "0"}
382+
)
343383
frames = []
344384
for pts in pts_list:
345385
stream_reader.seek(pts)
@@ -350,7 +390,9 @@ def get_frames_from_video(self, video_file, pts_list):
350390

351391
def get_consecutive_frames_from_video(self, video_file, numFramesToDecode):
352392
stream_reader = self.torchaudio.io.StreamReader(src=video_file)
353-
stream_reader.add_basic_video_stream(frames_per_chunk=1)
393+
stream_reader.add_basic_video_stream(
394+
frames_per_chunk=1, decoder_option={"threads": "0"}
395+
)
354396
frames = []
355397
frame_cnt = 0
356398
for vframe in stream_reader.stream():
@@ -363,7 +405,9 @@ def get_consecutive_frames_from_video(self, video_file, numFramesToDecode):
363405

364406
def decode_and_transform(self, video_file, pts_list, height, width, device):
365407
stream_reader = self.torchaudio.io.StreamReader(src=video_file)
366-
stream_reader.add_basic_video_stream(frames_per_chunk=1)
408+
stream_reader.add_basic_video_stream(
409+
frames_per_chunk=1, decoder_option={"threads": "1"}
410+
)
367411
frames = []
368412
for pts in pts_list:
369413
stream_reader.seek(pts)
@@ -634,7 +678,10 @@ def run_benchmarks(
634678
},
635679
label=f"video={video_file_path} {metadata_label}",
636680
sub_label=decoder_name,
637-
description=f"dataloader[threads={bp.num_threads},batch_size={bp.batch_size}] {num_samples} decode_and_transform()",
681+
description=f"concurrent[threads={bp.num_threads},batch_size={bp.batch_size}] {num_samples} decode_and_transform()",
682+
)
683+
print(
684+
f"{decoder_name} concurrent[threads={bp.num_threads} batch_size={bp.batch_size}]"
638685
)
639686
results.append(
640687
dataloader_result.blocked_autorange(
@@ -647,7 +694,7 @@ def run_benchmarks(
647694
decoder_name,
648695
video_file_path,
649696
num_samples * dataloader_parameters.batch_parameters.batch_size,
650-
f"dataloader[threads={bp.num_threads} batch_size={bp.batch_size}] {num_samples} x decode_and_transform()",
697+
f"concurrent[threads={bp.num_threads} batch_size={bp.batch_size}] {num_samples} x decode_and_transform()",
651698
)
652699
)
653700

@@ -670,6 +717,7 @@ def run_benchmarks(
670717
sub_label=decoder_name,
671718
description=f"{kind} {num_samples} seek()+next()",
672719
)
720+
print(f"{decoder_name} {kind} {num_samples} seek()+next()")
673721
results.append(
674722
seeked_result.blocked_autorange(min_run_time=min_runtime_seconds)
675723
)
@@ -697,6 +745,7 @@ def run_benchmarks(
697745
sub_label=decoder_name,
698746
description=f"batch {kind} {num_samples} seek()+next()",
699747
)
748+
print(f"{decoder_name} batch {kind} {num_samples} seek()+next()")
700749
results.append(
701750
seeked_result.blocked_autorange(
702751
min_run_time=min_runtime_seconds
@@ -724,6 +773,7 @@ def run_benchmarks(
724773
sub_label=decoder_name,
725774
description=f"{num_consecutive_nexts} next()",
726775
)
776+
print(f"{decoder_name} {num_consecutive_nexts} next()")
727777
results.append(
728778
consecutive_frames_result.blocked_autorange(
729779
min_run_time=min_runtime_seconds
@@ -753,6 +803,7 @@ def run_benchmarks(
753803
sub_label=decoder_name,
754804
description=f"batch {num_consecutive_nexts} next()",
755805
)
806+
print(f"{decoder_name} batch {num_consecutive_nexts} next()")
756807
results.append(
757808
consecutive_frames_result.blocked_autorange(
758809
min_run_time=min_runtime_seconds
-2.73 KB
Loading

0 commit comments

Comments
 (0)