Skip to content

Commit 26c9ccb

Browse files
committed
Benchmark code formatting, more refactoring
1 parent 6ddfa6c commit 26c9ccb

File tree

2 files changed

+35
-32
lines changed

2 files changed

+35
-32
lines changed

benchmarks/decoders/benchmark_decoders.py

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
class DecoderKind:
3232
display_name: str
3333
kind: typing.Type[AbstractDecoder]
34-
default_options: dict = field(default_factory=dict)
34+
default_options: dict[str, str] = field(default_factory=dict)
3535

3636

3737
decoder_registry = {
3838
"decord": DecoderKind("DecordAccurate", DecordAccurate),
3939
"decord_batch": DecoderKind("DecordAccurateBatch", DecordAccurateBatch),
40-
"torchcodec_core": DecoderKind("TorchCodecCore:", TorchCodecCore),
40+
"torchcodec_core": DecoderKind("TorchCodecCore", TorchCodecCore),
4141
"torchcodec_core_batch": DecoderKind("TorchCodecCoreBatch", TorchCodecCoreBatch),
4242
"torchcodec_core_nonbatch": DecoderKind(
4343
"TorchCodecCoreNonBatch", TorchCodecCoreNonBatch
@@ -47,7 +47,11 @@ class DecoderKind:
4747
),
4848
"torchcodec_public": DecoderKind("TorchCodecPublic", TorchCodecPublic),
4949
"torchvision": DecoderKind(
50-
"TorchVision[backend=video_reader]", TorchVision, {"backend": "video_reader"}
50+
# We don't compare against TorchVision's "pyav" backend because it doesn't support
51+
# accurate seeks.
52+
"TorchVision[backend=video_reader]",
53+
TorchVision,
54+
{"backend": "video_reader"},
5155
),
5256
"torchaudio": DecoderKind("TorchAudio", TorchAudioDecoder),
5357
}
@@ -66,6 +70,16 @@ def get_test_resource_path(filename: str) -> str:
6670
return str(Path(__file__).parent / f"../../test/resources/{filename}")
6771

6872

73+
def parse_options_code(options_code: str) -> dict[str, str]:
74+
options = {}
75+
for item in options_code.split("+"):
76+
if item.strip() == "":
77+
continue
78+
k, v = item.split("=")
79+
options[k] = v
80+
return options
81+
82+
6983
def main() -> None:
7084
"""Benchmarks the performance of a few video decoders"""
7185

@@ -98,19 +112,17 @@ def main() -> None:
98112
"--decoders",
99113
help=(
100114
"Comma-separated list of decoders to benchmark. "
101-
"Choices are: "
102-
+ ", ".join(decoder_registry.keys())
103-
+ ". "
104-
+ "To specify options, append a ':' and then value pairs seperated by a '+'. "
105-
"For example, torchcodec:num_threads=1+color_conversion_library=filtergraph."
115+
"Choices are: " + ", ".join(decoder_registry.keys()) + ". "
116+
"To specify options, append a ':' and then value pairs seperated by a '+'. "
117+
"For example, torchcodec_core:num_threads=1+color_conversion_library=filtergraph."
106118
),
107119
type=str,
108120
default=(
109121
"decord,decord_batch,"
110-
+ "torchvision,"
111-
+ "torchaudio,"
112-
+ "torchcodec_core,torchcodec_core:num_threads=1,torchcodec_core_batch,torchcodec_core_nonbatch,"
113-
+ "torchcodec_public"
122+
"torchvision,"
123+
"torchaudio,"
124+
"torchcodec_core,torchcodec_core:num_threads=1,torchcodec_core_batch,torchcodec_core_nonbatch,"
125+
"torchcodec_public"
114126
),
115127
)
116128
parser.add_argument(
@@ -135,26 +147,17 @@ def main() -> None:
135147
decoders_to_run = {}
136148
for decoder in specified_decoders:
137149
if ":" in decoder:
138-
decoder_name, _, options = decoder.partition(":")
139-
assert decoder_name in decoder_registry
140-
141-
kwargs_dict = {}
142-
for item in options.split("+"):
143-
if item.strip() == "":
144-
continue
145-
k, v = item.split("=")
146-
kwargs_dict[k] = v
147-
148-
display_name = decoder_registry[decoder_name].display_name
149-
kind = decoder_registry[decoder_name].kind
150-
decoders_to_run[display_name + options] = kind(**kwargs_dict)
151-
elif decoder in decoder_registry:
152-
display_name = decoder_registry[decoder].display_name
153-
kind = decoder_registry[decoder].kind
154-
default_options = decoder_registry[decoder].default_options
155-
decoders_to_run[display_name] = kind(**default_options)
150+
decoder, _, options_code = decoder.partition(":")
151+
assert decoder in decoder_registry, f"Unknown decoder: {decoder}"
152+
display = decoder_registry[decoder].display_name + ":" + options_code
153+
options = parse_options_code(options_code)
156154
else:
157-
raise ValueError(f"Unknown decoder: {decoder}")
155+
assert decoder in decoder_registry, f"Unknown decoder: {decoder}"
156+
display = decoder_registry[decoder].display_name
157+
options = decoder_registry[decoder].default_options
158+
159+
kind = decoder_registry[decoder].kind
160+
decoders_to_run[display] = kind(**options)
158161

159162
video_paths = args.bm_video_paths.split(",")
160163
if args.bm_video_dir:

benchmarks/decoders/benchmark_decoders_library.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ def run_benchmarks(
580580
"create_torchcodec_decoder_from_file": create_torchcodec_decoder_from_file,
581581
},
582582
label=f"video={first_video_file_path} {metadata_label}",
583-
sub_label="TorchCodecCore:",
583+
sub_label="TorchCodecCore",
584584
description="create()+next()",
585585
)
586586
results.append(

0 commit comments

Comments
 (0)