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 5, 2023
1 parent 075dfb8 commit 7ed323e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 35 deletions.
1 change: 0 additions & 1 deletion test/torchaudio_unittest/backend/soundfile/info_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ class MockSoundFileInfo:
with patch("soundfile.info", _mock_info_func):
with warnings.catch_warnings(record=True) as w:
info = soundfile_backend.info("foo")
assert len(w) == 1
assert "UNSEEN_SUBTYPE subtype is unknown to TorchAudio" in str(w[-1].message)
assert info.bits_per_sample == 0

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 . import common, no_backend, soundfile_backend, sox_io_backend # noqa
from .utils import _init_backend, get_audio_backend, list_audio_backends, set_audio_backend

__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}")
File renamed without changes.
14 changes: 14 additions & 0 deletions torchaudio/backend/no_backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def __getattr__(name: str):
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 . import _no_backend

return getattr(_no_backend, name)
14 changes: 14 additions & 0 deletions torchaudio/backend/soundfile_backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def __getattr__(name: str):
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)
14 changes: 14 additions & 0 deletions torchaudio/backend/sox_io_backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def __getattr__(name: str):
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 . import _sox_io_backend

return getattr(_sox_io_backend, name)

0 comments on commit 7ed323e

Please sign in to comment.