|
| 1 | +from argparse import ArgumentParser |
| 2 | +from datetime import timedelta |
| 3 | +from pathlib import Path |
| 4 | +from time import perf_counter_ns |
| 5 | + |
| 6 | +import torch |
| 7 | +import torchaudio |
| 8 | +from torch import Tensor |
| 9 | +from torchaudio.io import StreamReader |
| 10 | +from torchcodec.decoders._audio_decoder import AudioDecoder |
| 11 | + |
| 12 | +DEFAULT_NUM_EXP = 30 |
| 13 | + |
| 14 | + |
| 15 | +def bench(f, *args, num_exp=DEFAULT_NUM_EXP, warmup=1, **kwargs) -> Tensor: |
| 16 | + |
| 17 | + for _ in range(warmup): |
| 18 | + f(*args, **kwargs) |
| 19 | + |
| 20 | + times = [] |
| 21 | + for _ in range(num_exp): |
| 22 | + start = perf_counter_ns() |
| 23 | + f(*args, **kwargs) |
| 24 | + end = perf_counter_ns() |
| 25 | + times.append(end - start) |
| 26 | + return torch.tensor(times).float() |
| 27 | + |
| 28 | + |
| 29 | +def report_stats(times: Tensor, unit: str = "ms", prefix: str = "") -> float: |
| 30 | + mul = { |
| 31 | + "ns": 1, |
| 32 | + "µs": 1e-3, |
| 33 | + "ms": 1e-6, |
| 34 | + "s": 1e-9, |
| 35 | + }[unit] |
| 36 | + times = times * mul |
| 37 | + std = times.std().item() |
| 38 | + med = times.median().item() |
| 39 | + mean = times.mean().item() |
| 40 | + min = times.min().item() |
| 41 | + max = times.max().item() |
| 42 | + print( |
| 43 | + f"{prefix:<40} {med = :.2f}, {mean = :.2f} +- {std:.2f}, {min = :.2f}, {max = :.2f} - in {unit}" |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +def decode_with_torchcodec(path: Path) -> None: |
| 48 | + AudioDecoder(path).get_all_samples() |
| 49 | + |
| 50 | + |
| 51 | +def decode_with_torchaudio_StreamReader(path: Path) -> None: |
| 52 | + reader = StreamReader(path) |
| 53 | + reader.add_audio_stream(frames_per_chunk=1024) |
| 54 | + for _ in reader.stream(): |
| 55 | + pass |
| 56 | + |
| 57 | + |
| 58 | +def decode_with_torchaudio_load(path: Path, backend: str) -> None: |
| 59 | + torchaudio.load(str(path), backend=backend) |
| 60 | + |
| 61 | + |
| 62 | +parser = ArgumentParser() |
| 63 | +parser.add_argument("--path", type=str, help="path to file", required=True) |
| 64 | +parser.add_argument( |
| 65 | + "--num-exp", |
| 66 | + type=int, |
| 67 | + default=DEFAULT_NUM_EXP, |
| 68 | + help="number of runs to average over", |
| 69 | +) |
| 70 | + |
| 71 | +args = parser.parse_args() |
| 72 | +path = Path(args.path) |
| 73 | + |
| 74 | +metadata = AudioDecoder(path).metadata |
| 75 | +duration = str(timedelta(seconds=metadata.duration_seconds_from_header)).split(".")[0] |
| 76 | + |
| 77 | +print( |
| 78 | + f"Benchmarking {path.name}, duration: {duration}, codec: {metadata.codec}, format: {metadata.sample_format}, averaging over {args.num_exp} runs:" |
| 79 | +) |
| 80 | + |
| 81 | +for decode_f, kwargs, prefix in ( |
| 82 | + (decode_with_torchcodec, {}, "torchcodec.AudioDecoder"), |
| 83 | + ( |
| 84 | + decode_with_torchaudio_load, |
| 85 | + {"backend": "ffmpeg"}, |
| 86 | + "torchaudio.load(backend='ffmpeg')", |
| 87 | + ), |
| 88 | + (decode_with_torchaudio_load, {"backend": "sox"}, "torchaudio.load(backend='sox')"), |
| 89 | + ( |
| 90 | + decode_with_torchaudio_load, |
| 91 | + {"backend": "soundfile"}, |
| 92 | + "torchaudio.load(backend='soundfile')", |
| 93 | + ), |
| 94 | + (decode_with_torchaudio_StreamReader, {}, "torchaudio.StreamReader"), |
| 95 | +): |
| 96 | + |
| 97 | + try: |
| 98 | + times = bench(decode_f, path, **kwargs, num_exp=args.num_exp) |
| 99 | + report_stats(times, prefix=prefix) |
| 100 | + except RuntimeError: |
| 101 | + print(f"{prefix:<40} Not supported") |
0 commit comments