From 2b7a5c5f1fe677d6170af89c2422aa59aeb2a6ae Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Tue, 25 Mar 2025 11:02:41 +0000 Subject: [PATCH 1/3] Add soundfile backaend to audio benchmarks --- .../decoders/benchmark_audio_decoders.py | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/benchmarks/decoders/benchmark_audio_decoders.py b/benchmarks/decoders/benchmark_audio_decoders.py index bfffbf12..e1da83a3 100644 --- a/benchmarks/decoders/benchmark_audio_decoders.py +++ b/benchmarks/decoders/benchmark_audio_decoders.py @@ -102,20 +102,24 @@ def decode_with_torchaudio_load(path: Path, backend: str) -> None: f"Benchmarking {path.name}, duration: {get_duration(path)}, averaging over {args.num_exp} runs:" ) -times = bench(decode_with_torchcodec, path, num_exp=args.num_exp) -report_stats(times, prefix="torchcodec.AudioDecoder") +for decode_f, kwargs, prefix in ( + (decode_with_torchcodec, {}, "torchcodec.AudioDecoder"), + ( + decode_with_torchaudio_load, + {"backend": "ffmpeg"}, + "torchaudio.load(backend='ffmpeg')", + ), + (decode_with_torchaudio_load, {"backend": "sox"}, "torchaudio.load(backend='sox')"), + ( + decode_with_torchaudio_load, + {"backend": "soundfile"}, + "torchaudio.load(backend='soundfile')", + ), + (decode_with_torchaudio_StreamReader, {}, "torchaudio.StreamReader"), +): -times = bench(decode_with_torchaudio_load, path, backend="ffmpeg", num_exp=args.num_exp) -report_stats(times, prefix="torchaudio.load(backend='ffmpeg')") - -prefix = "torchaudio.load(backend='sox')" -try: - times = bench( - decode_with_torchaudio_load, path, backend="sox", num_exp=args.num_exp - ) - report_stats(times, prefix=prefix) -except RuntimeError: - print(f"{prefix:<40} Not supported") - -times = bench(decode_with_torchaudio_StreamReader, path, num_exp=args.num_exp) -report_stats(times, prefix="torchaudio.StreamReader") + try: + times = bench(decode_f, path, **kwargs, num_exp=args.num_exp) + report_stats(times, prefix=prefix) + except RuntimeError: + print(f"{prefix:<40} Not supported") From 1880b01a52835bbdaa9ac64312412efeef481255 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Tue, 25 Mar 2025 11:28:07 +0000 Subject: [PATCH 2/3] Add codec and format info --- benchmarks/decoders/benchmark_audio_decoders.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/benchmarks/decoders/benchmark_audio_decoders.py b/benchmarks/decoders/benchmark_audio_decoders.py index e1da83a3..5d7c4eea 100644 --- a/benchmarks/decoders/benchmark_audio_decoders.py +++ b/benchmarks/decoders/benchmark_audio_decoders.py @@ -97,9 +97,11 @@ def decode_with_torchaudio_load(path: Path, backend: str) -> None: args = parser.parse_args() path = Path(args.path) +metadata = AudioDecoder(path).metadata +duration = str(timedelta(seconds=metadata.duration_seconds_from_header)).split(".")[0] print( - f"Benchmarking {path.name}, duration: {get_duration(path)}, averaging over {args.num_exp} runs:" + f"Benchmarking {path.name}, duration: {duration}, codec: {metadata.codec}, format: {metadata.sample_format}, averaging over {args.num_exp} runs:" ) for decode_f, kwargs, prefix in ( From b21f9162d5d0c74adcab37b337f5d1d28ad7ffcc Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Tue, 25 Mar 2025 11:29:05 +0000 Subject: [PATCH 3/3] Remove get_duration --- .../decoders/benchmark_audio_decoders.py | 26 ------------------- 1 file changed, 26 deletions(-) diff --git a/benchmarks/decoders/benchmark_audio_decoders.py b/benchmarks/decoders/benchmark_audio_decoders.py index 5d7c4eea..f2cdaf36 100644 --- a/benchmarks/decoders/benchmark_audio_decoders.py +++ b/benchmarks/decoders/benchmark_audio_decoders.py @@ -1,5 +1,3 @@ -import subprocess - from argparse import ArgumentParser from datetime import timedelta from pathlib import Path @@ -46,30 +44,6 @@ def report_stats(times: Tensor, unit: str = "ms", prefix: str = "") -> float: ) -def get_duration(path: Path) -> str: - try: - result = subprocess.run( - [ - "ffprobe", - "-v", - "error", - "-show_entries", - "format=duration", - "-of", - "default=noprint_wrappers=1:nokey=1", - str(path), - ], - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - text=True, - ) - - # Remove microseconds - return str(timedelta(seconds=float(result.stdout.strip()))).split(".")[0] - except Exception: - return "?" - - def decode_with_torchcodec(path: Path) -> None: AudioDecoder(path).get_all_samples()