pytorch / pytorch/audio

torchaudio.load slows down with increasing frame_offset

Open
#2,897 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

triaged
Dominant language
Python
Stars
2.9k
Forks
799
Avg merge
58m
Merged PRs (30d)
3

Description

🐛 Describe the bug

The execution time of torchaudio.load increases when increasing frame_offset. This can be observed in particular when loading a WAV file sequentially in chunks.

I assume that here load reads the whole file up until frame_offset and then reads the requested chunk, which is inefficient. While this might be compilcated to fix for compressed file formats, for WAV files this could be solved by simply seeking into the file and reading the file starting at frame_offset.

Here's a minimal example to reproduce the issue:

from time import time
import torchaudio


WAV_FILE = "path/to/WAV/file.wav"
CHUNK_SIZE = 2**16


def load_benchmark():
    try:
        info = torchaudio.info(str(WAV_FILE))
    except RuntimeError:
        print(WAV_FILE)
        raise
    length = info.num_frames

    for offset in range(0, length, CHUNK_SIZE):
        start = time()
        _, _ = torchaudio.load(WAV_FILE, frame_offset=offset, num_frames=CHUNK_SIZE)
        end = time()
        print(f"Loading chunk at offset {offset} took {(end - start) * 1000}ms.")


if __name__ == '__main__':
    load_benchmark()

which outputs something like

Loading chunk at offset 0 took 8.82411003112793ms.
Loading chunk at offset 65536 took 5.040168762207031ms.
[...]
Loading chunk at offset 9437184 took 72.36099243164062ms.
Loading chunk at offset 9502720 took 72.80373573303223ms.
Versions
PyTorch version: 1.13.0
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 12.5 (x86_64)
GCC version: Could not collect
Clang version: 14.0.0 (clang-1400.0.29.102)
CMake version: version 3.20.5
Libc version: N/A

Python version: 3.9.15 (main, Nov 24 2022, 08:29:02)  [Clang 14.0.6 ] (64-bit runtime)
Python platform: macOS-10.16-x86_64-i386-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] mypy==0.991
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.23.5
[pip3] torch==1.13.0
[pip3] torchaudio==0.13.0
[pip3] torchmetrics==0.11.0
[conda] numpy                     1.23.5                   pypi_0    pypi
[conda] torch                     1.13.0                   pypi_0    pypi
[conda] torchaudio                0.13.0                   pypi_0    pypi
[conda] torchmetrics              0.11.0                   pypi_0    pypi

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the torchaudio.load entry point and reproduce the behavior with the provided sequential WAV-file benchmark using increasing frame_offset values. Trace how WAV loading handles frame_offset and compare the elapsed times across chunks; done means WAV chunks can be loaded without time increasing with the offset, while preserving the requested frame range.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
audio-video-rtc, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.