Skip to content

Commit

Permalink
[Cherry-pick] Fix backward compatibility layer in backend module
Browse files Browse the repository at this point in the history
The PR pytorch#3549 re-organized the backend implementations and deprecated
the direct access to torchaudio.backend.

The change was supposed to be BC-compatible while issuing a warning
to users, but the implementation of module-level `__getattr__` was
not quite right.

See an issue pyannote/pyannote-audio#1456.

This commit fixes it so that the following imports work;

```python
from torchaudio.backend.common import AudioMetaData

from torchaudio.backend import sox_io_backend
from torchaudio.backend.sox_io_backend import save, load, info

from torchaudio.backend import no_backend
from torchaudio.backend.no_backend import save, load, info

from torchaudio.backend import soundfile_backend
from torchaudio.backend.soundfile_backend import save, load, info
```
  • Loading branch information
mthrok committed Sep 4, 2023
1 parent 075dfb8 commit 94c7391
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 72 deletions.
5 changes: 3 additions & 2 deletions torchaudio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Initialize extension and backend first
from . import _extension # noqa # usort: skip
from ._backend.common import AudioMetaData # noqa # usort: skip
from . import ( # noqa: F401
_extension,
compliance,
datasets,
functional,
Expand All @@ -11,7 +13,6 @@
transforms,
utils,
)
from ._backend.common import AudioMetaData # noqa

try:
from .version import __version__, git_version # noqa: F401
Expand Down
36 changes: 2 additions & 34 deletions torchaudio/backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,8 @@
# New things should be added to `torchaudio._backend`.
# Only things related to backward compatibility should be placed here.

from .utils import _init_backend, get_audio_backend, list_audio_backends, set_audio_backend

from .utils import _init_backend, get_audio_backend, list_audio_backends, set_audio_backend
from . import common, no_backend, soundfile_backend, sox_io_backend # noqa

__all__ = ["_init_backend", "get_audio_backend", "list_audio_backends", "set_audio_backend"]


def __getattr__(name: str):
if name == "common":
from . import _common

return _common

if name in ["no_backend", "sox_io_backend", "soundfile_backend"]:
import warnings

warnings.warn(
"Torchaudio's I/O functions now support par-call bakcend dispatch. "
"Importing backend implementation directly is no longer guaranteed to work. "
"Please use `backend` keyword with load/save/info function, instead of "
"calling the udnerlying implementation directly.",
stacklevel=2,
)

if name == "sox_io_backend":
from . import _sox_io_backend

return _sox_io_backend
if name == "soundfile_backend":
from torchaudio._backend import soundfile_backend

return soundfile_backend

if name == "no_backend":
from . import _no_backend

return _no_backend
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
24 changes: 0 additions & 24 deletions torchaudio/backend/_no_backend.py

This file was deleted.

File renamed without changes.
49 changes: 49 additions & 0 deletions torchaudio/backend/no_backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from pathlib import Path
from typing import Callable, Optional, Tuple, Union

import torchaudio

from torch import Tensor


def __getattr__(name: str):
if name in ["info", "load", "save"]:
import warnings

warnings.warn(
"Torchaudio's I/O functions now support par-call bakcend dispatch. "
"Importing backend implementation directly is no longer guaranteed to work. "
"Please use `backend` keyword with load/save/info function, instead of "
"calling the udnerlying implementation directly.",
stacklevel=2,
)

if name == "info":
return _info

if name == "load":
return _load

if name == "save":
return _save
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")


def _load(
filepath: Union[str, Path],
out: Optional[Tensor] = None,
normalization: Union[bool, float, Callable] = True,
channels_first: bool = True,
num_frames: int = 0,
offset: int = 0,
filetype: Optional[str] = None,
) -> Tuple[Tensor, int]:
raise RuntimeError("No audio I/O backend is available.")


def _save(filepath: str, src: Tensor, sample_rate: int, precision: int = 16, channels_first: bool = True) -> None:
raise RuntimeError("No audio I/O backend is available.")


def _info(filepath: str) -> torchaudio.AudioMetaData:
raise RuntimeError("No audio I/O backend is available.")
17 changes: 17 additions & 0 deletions torchaudio/backend/soundfile_backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def __getattr__(name: str):
if name in ["info", "load", "save"]:
import warnings

warnings.warn(
"Torchaudio's I/O functions now support par-call bakcend dispatch. "
"Importing backend implementation directly is no longer guaranteed to work. "
"Please use `backend` keyword with load/save/info function, instead of "
"calling the udnerlying implementation directly.",
stacklevel=2,
)

from torchaudio._backend import soundfile_backend

return getattr(soundfile_backend, name)

raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,31 @@
from torchaudio import AudioMetaData


def __getattr__(name: str):
if name in ["info", "load", "save"]:
import warnings

warnings.warn(
"Torchaudio's I/O functions now support par-call bakcend dispatch. "
"Importing backend implementation directly is no longer guaranteed to work. "
"Please use `backend` keyword with load/save/info function, instead of "
"calling the udnerlying implementation directly.",
stacklevel=2,
)

if name == "info":
return _info

if name == "load":
return _load

if name == "save":
return _save
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")


@torchaudio._extension.fail_if_no_sox
def info(
def _info(
filepath: str,
format: Optional[str] = None,
) -> AudioMetaData:
Expand All @@ -34,7 +57,7 @@ def info(


@torchaudio._extension.fail_if_no_sox
def load(
def _load(
filepath: str,
frame_offset: int = 0,
num_frames: int = -1,
Expand Down Expand Up @@ -129,7 +152,7 @@ def load(


@torchaudio._extension.fail_if_no_sox
def save(
def _save(
filepath: str,
src: torch.Tensor,
sample_rate: int,
Expand Down
21 changes: 12 additions & 9 deletions torchaudio/backend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from torchaudio._backend import soundfile_backend
from torchaudio._internal import module_utils as _mod_utils

from . import _no_backend as no_backend, _sox_io_backend as sox_io_backend
from . import no_backend, sox_io_backend

__all__ = [
"list_audio_backends",
Expand Down Expand Up @@ -41,17 +41,20 @@ def set_audio_backend(backend: Optional[str]):
raise RuntimeError(f'Backend "{backend}" is not one of ' f"available backends: {list_audio_backends()}.")

if backend is None:
module = no_backend
setattr(torchaudio, "info", no_backend._info)
setattr(torchaudio, "load", no_backend._load)
setattr(torchaudio, "save", no_backend._save)
elif backend == "sox_io":
module = sox_io_backend
setattr(torchaudio, "info", sox_io_backend._info)
setattr(torchaudio, "load", sox_io_backend._load)
setattr(torchaudio, "save", sox_io_backend._save)
elif backend == "soundfile":
module = soundfile_backend
setattr(torchaudio, "info", soundfile_backend.info)
setattr(torchaudio, "load", soundfile_backend.load)
setattr(torchaudio, "save", soundfile_backend.save)
else:
raise NotImplementedError(f'Unexpected backend "{backend}"')

for func in ["save", "load", "info"]:
setattr(torchaudio, func, getattr(module, func))


def _init_backend():
warnings.warn(
Expand All @@ -75,9 +78,9 @@ def get_audio_backend() -> Optional[str]:
Returns:
Optional[str]: The name of the current backend or ``None`` if no backend is assigned.
"""
if torchaudio.load == no_backend.load:
if torchaudio.load == no_backend._load:
return None
if torchaudio.load == sox_io_backend.load:
if torchaudio.load == sox_io_backend._load:
return "sox_io"
if torchaudio.load == soundfile_backend.load:
return "soundfile"
Expand Down

0 comments on commit 94c7391

Please sign in to comment.