Skip to content

Commit 2e0dfaf

Browse files
mthrokfacebook-github-bot
authored andcommitted
Move essential backend implementations to _backend (#3549)
Summary: Move the actual I/O implementation to `_backend` submodule so that the existing `backend` submodule contains only what's related to legacy backend utilities. Pull Request resolved: #3549 Reviewed By: huangruizhe Differential Revision: D48253550 Pulled By: mthrok fbshipit-source-id: c23f1664458c723f63e134c7974b3f7cf17a1e98
1 parent c0f25f2 commit 2e0dfaf

File tree

14 files changed

+76
-16
lines changed

14 files changed

+76
-16
lines changed

torchaudio/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
transforms,
1212
utils,
1313
)
14-
from .backend.common import AudioMetaData
15-
14+
from ._backend.common import AudioMetaData # noqa
1615

1716
try:
1817
from .version import __version__, git_version # noqa: F401
@@ -34,6 +33,9 @@ def _is_backend_dispatcher_enabled():
3433

3534
_init_backend()
3635

36+
# for backward compatibility. This has to happen after _backend is imported.
37+
from . import backend # noqa: F401
38+
3739

3840
__all__ = [
3941
"AudioMetaData",

torchaudio/_backend/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import torchaudio
44
from torchaudio._internal.module_utils import deprecated
55

6-
from . import utils
7-
86

97
# TODO: Once legacy global backend is removed, move this to torchaudio.__init__
108
def _init_backend():
9+
from . import utils
10+
1111
torchaudio.info = utils.get_info_func()
1212
torchaudio.load = utils.get_load_func()
1313
torchaudio.save = utils.get_save_func()
@@ -24,6 +24,8 @@ def list_audio_backends() -> List[str]:
2424
- Dispatcher mode: ``"ffmpeg"``, ``"sox"`` and ``"soundfile"``.
2525
- Legacy backend mode: ``"sox_io"``, ``"soundfile"``.
2626
"""
27+
from . import utils
28+
2729
return list(utils.get_available_backends().keys())
2830

2931

torchaudio/_backend/backend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from torch import Tensor
66

7-
from torchaudio.backend.common import AudioMetaData
7+
from .common import AudioMetaData
88

99

1010
class Backend(ABC):
File renamed without changes.

torchaudio/_backend/ffmpeg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
import torch
77
import torchaudio
8-
from torchaudio.backend.common import AudioMetaData
98
from torchaudio.io import StreamWriter
109

1110
from .backend import Backend
11+
from .common import AudioMetaData
1212

1313
if torchaudio._extension._FFMPEG_EXT is not None:
1414
StreamReaderFileObj = torchaudio._extension._FFMPEG_EXT.StreamReaderFileObj

torchaudio/_backend/soundfile.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33

44
import torch
55

6-
from torchaudio.backend import soundfile_backend
7-
from torchaudio.backend.common import AudioMetaData
8-
6+
from . import soundfile_backend
97
from .backend import Backend
8+
from .common import AudioMetaData
109

1110

1211
class SoundfileBackend(Backend):

torchaudio/_backend/sox.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
from typing import BinaryIO, Optional, Tuple, Union
33

44
import torch
5-
from torchaudio.backend.common import AudioMetaData
65

76
from .backend import Backend
7+
from .common import AudioMetaData
88

99

1010
class SoXBackend(Backend):

torchaudio/_backend/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
import torch
66

77
from torchaudio._extension import _FFMPEG_EXT, _SOX_INITIALIZED
8-
from torchaudio.backend import soundfile_backend
9-
from torchaudio.backend.common import AudioMetaData
8+
9+
from . import soundfile_backend
1010

1111
from .backend import Backend
12+
from .common import AudioMetaData
1213
from .ffmpeg import FFmpegBackend
1314
from .soundfile import SoundfileBackend
1415
from .sox import SoXBackend

torchaudio/backend/__init__.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,42 @@
1+
# NOTE:
2+
# The entire `torchaudio.backend` module is deprecated.
3+
# New things should be added to `torchaudio._backend`.
4+
# Only things related to backward compatibility should be placed here.
5+
16
from .utils import _init_backend, get_audio_backend, list_audio_backends, set_audio_backend
27

38

49
__all__ = ["_init_backend", "get_audio_backend", "list_audio_backends", "set_audio_backend"]
10+
11+
12+
def __getattr__(name: str):
13+
if name == "common":
14+
from . import _common
15+
16+
return _common
17+
18+
if name in ["no_backend", "sox_io_backend", "soundfile_backend"]:
19+
import warnings
20+
21+
warnings.warn(
22+
"Torchaudio's I/O functions now support par-call bakcend dispatch. "
23+
"Importing backend implementation directly is no longer guaranteed to work. "
24+
"Please use `backend` keyword with load/save/info function, instead of "
25+
"calling the udnerlying implementation directly.",
26+
stacklevel=2,
27+
)
28+
29+
if name == "sox_io_backend":
30+
from . import _sox_io_backend
31+
32+
return _sox_io_backend
33+
if name == "soundfile_backend":
34+
from torchaudio._backend import soundfile_backend
35+
36+
return soundfile_backend
37+
38+
if name == "no_backend":
39+
from . import _no_backend
40+
41+
return _no_backend
42+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")

torchaudio/backend/_common.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def __getattr__(name: str):
2+
import warnings
3+
4+
if name == "AudioMetaData":
5+
warnings.warn(
6+
"`torchaudio.backend.common.AudioMetaData` has been moved to "
7+
"`torchaudio.AudioMetaData`. Please update the import path.",
8+
stacklevel=2,
9+
)
10+
from torchaudio._backend.common import AudioMetaData
11+
12+
return AudioMetaData
13+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
File renamed without changes.

torchaudio/backend/sox_io_backend.py renamed to torchaudio/backend/_sox_io_backend.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
import torch
55
import torchaudio
6-
7-
from .common import AudioMetaData
6+
from torchaudio import AudioMetaData
87

98

109
@torchaudio._extension.fail_if_no_sox

torchaudio/backend/utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
from typing import List, Optional
44

55
import torchaudio
6+
from torchaudio._backend import soundfile_backend
67
from torchaudio._internal import module_utils as _mod_utils
78

8-
from . import no_backend, soundfile_backend, sox_io_backend
9+
from . import _no_backend as no_backend, _sox_io_backend as sox_io_backend
910

1011
__all__ = [
1112
"list_audio_backends",
@@ -53,13 +54,18 @@ def set_audio_backend(backend: Optional[str]):
5354

5455

5556
def _init_backend():
57+
warnings.warn(
58+
"TorchAudio's global backend is now deprecated. "
59+
"Please enable distpatcher by setting `TORCHAUDIO_USE_BACKEND_DISPATCHER=1`, "
60+
"and specify backend when calling load/info/save function.",
61+
stacklevel=3,
62+
)
5663
backends = list_audio_backends()
5764
if "sox_io" in backends:
5865
set_audio_backend("sox_io")
5966
elif "soundfile" in backends:
6067
set_audio_backend("soundfile")
6168
else:
62-
warnings.warn("No audio backend is available.")
6369
set_audio_backend(None)
6470

6571

0 commit comments

Comments
 (0)