Commit 454418d2 authored by moto's avatar moto Committed by Facebook GitHub Bot
Browse files

[BC-Breaking] Remove legacy global backend switch (#3559)

Summary:
This PR removes the legacy backend switch mechanism.
The implementation itself is still available.

Merge after v2.1 release

Pull Request resolved: https://github.com/pytorch/audio/pull/3559

Reviewed By: nateanl

Differential Revision: D48353764

Pulled By: mthrok

fbshipit-source-id: 4d3924dbe6f334ecebe2b12fcd4591c61c4aa656
parent 7d37f69c
......@@ -17,6 +17,7 @@ it easy to handle audio data.
info
load
save
list_audio_backends
.. _backend:
......@@ -91,30 +92,12 @@ please refer to https://github.com/pytorch/audio/issues/2950
* In 2.0, audio I/O backend dispatcher was introduced.
Users can opt-in to using dispatcher by setting the environment variable
``TORCHAUDIO_USE_BACKEND_DISPATCHER=1``.
* In 2.1, the disptcher becomes the default mechanism for I/O.
Those who need to keep using the previous mechanism (global backend) can do
so by setting ``TORCHAUDIO_USE_BACKEND_DISPATCHER=0``.
* In 2.2, the legacy global backend mechanism will be removed.
* In 2.1, the disptcher became the default mechanism for I/O.
* In 2.2, the legacy global backend mechanism is removed.
Utility functions :py:func:`get_audio_backend` and :py:func:`set_audio_backend`
become no-op.
became no-op.
Furthermore, we are removing file-like object support from libsox backend, as this
Furthermore, we removed file-like object support from libsox backend, as this
is better supported by FFmpeg backend and makes the build process simpler.
Therefore, beginning with 2.1, FFmpeg and Soundfile are the sole backends that support
file-like objects.
Backend Utilities
-----------------
The following functions are effective only when backend dispatcher is disabled.
Note that the changes in 2.1 marks :py:func:`get_audio_backend` and
:py:func:`set_audio_backend` deprecated.
.. autosummary::
:toctree: generated
:nosignatures:
list_audio_backends
get_audio_backend
set_audio_backend
import torchaudio
from torchaudio_unittest import common_utils
class BackendSwitchMixin:
"""Test set/get_audio_backend works"""
backend = None
backend_module = None
def test_switch(self):
torchaudio.backend.utils.set_audio_backend(self.backend)
if self.backend is None:
assert torchaudio.backend.utils.get_audio_backend() is None
else:
assert torchaudio.backend.utils.get_audio_backend() == self.backend
assert torchaudio.load == self.backend_module.load
assert torchaudio.save == self.backend_module.save
assert torchaudio.info == self.backend_module.info
class TestBackendSwitch_NoBackend(BackendSwitchMixin, common_utils.TorchaudioTestCase):
backend = None
backend_module = torchaudio.backend.no_backend
@common_utils.skipIfNoSox
class TestBackendSwitch_SoXIO(BackendSwitchMixin, common_utils.TorchaudioTestCase):
backend = "sox_io"
backend_module = torchaudio.backend.sox_io_backend
@common_utils.skipIfNoModule("soundfile")
class TestBackendSwitch_soundfile(BackendSwitchMixin, common_utils.TorchaudioTestCase):
backend = "soundfile"
backend_module = torchaudio.backend.soundfile_backend
from . import ( # noqa: F401
# Initialize extension and backend first
from . import ( # noqa # usort: skip
_extension,
_backend,
)
from . import ( # noqa: F401
backend, # For BC
compliance,
datasets,
functional,
......@@ -11,7 +16,7 @@ from . import ( # noqa: F401
transforms,
utils,
)
from ._backend.common import AudioMetaData # noqa
from ._backend import AudioMetaData, get_audio_backend, info, list_audio_backends, load, save, set_audio_backend
try:
from .version import __version__, git_version # noqa: F401
......@@ -19,26 +24,11 @@ except ImportError:
pass
def _is_backend_dispatcher_enabled():
import os
return os.getenv("TORCHAUDIO_USE_BACKEND_DISPATCHER", default="1") == "1"
if _is_backend_dispatcher_enabled():
from ._backend import _init_backend, get_audio_backend, list_audio_backends, set_audio_backend
else:
from .backend import _init_backend, get_audio_backend, list_audio_backends, set_audio_backend
_init_backend()
# for backward compatibility. This has to happen after _backend is imported.
from . import backend # noqa: F401
__all__ = [
"AudioMetaData",
"load",
"info",
"save",
"io",
"compliance",
"datasets",
......
from typing import List, Optional
import torchaudio
from torchaudio._internal.module_utils import deprecated
from . import utils
from .common import AudioMetaData
# TODO: Once legacy global backend is removed, move this to torchaudio.__init__
def _init_backend():
from . import utils
__all__ = [
"AudioMetaData",
"load",
"info",
"save",
"list_audio_backends",
"get_audio_backend",
"set_audio_backend",
]
torchaudio.info = utils.get_info_func()
torchaudio.load = utils.get_load_func()
torchaudio.save = utils.get_save_func()
info = utils.get_info_func()
load = utils.get_load_func()
save = utils.get_save_func()
def list_audio_backends() -> List[str]:
......@@ -24,7 +32,6 @@ def list_audio_backends() -> List[str]:
- Dispatcher mode: ``"ffmpeg"``, ``"sox"`` and ``"soundfile"``.
- Legacy backend mode: ``"sox_io"``, ``"soundfile"``.
"""
from . import utils
return list(utils.get_available_backends().keys())
......
......@@ -3,11 +3,6 @@
# 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
__all__ = ["_init_backend", "get_audio_backend", "list_audio_backends", "set_audio_backend"]
def __getattr__(name: str):
if name == "common":
......
......@@ -7,7 +7,7 @@ def __getattr__(name: str):
"`torchaudio.AudioMetaData`. Please update the import path.",
stacklevel=2,
)
from torchaudio._backend.common import AudioMetaData
from torchaudio import AudioMetaData
return AudioMetaData
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
"""Defines utilities for switching audio backends"""
import warnings
from typing import List, Optional
import torchaudio
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
__all__ = [
"list_audio_backends",
"get_audio_backend",
"set_audio_backend",
]
def list_audio_backends() -> List[str]:
"""List available backends
Returns:
List[str]: The list of available backends.
"""
backends = []
if _mod_utils.is_module_available("soundfile"):
backends.append("soundfile")
if torchaudio._extension._SOX_INITIALIZED:
backends.append("sox_io")
return backends
def set_audio_backend(backend: Optional[str]):
"""Set the backend for I/O operation
Args:
backend (str or None): Name of the backend.
One of ``"sox_io"`` or ``"soundfile"`` based on availability
of the system. If ``None`` is provided the current backend is unassigned.
"""
if backend is not None and backend not in list_audio_backends():
raise RuntimeError(f'Backend "{backend}" is not one of ' f"available backends: {list_audio_backends()}.")
if backend is None:
module = no_backend
elif backend == "sox_io":
module = sox_io_backend
elif backend == "soundfile":
module = soundfile_backend
else:
raise NotImplementedError(f'Unexpected backend "{backend}"')
for func in ["save", "load", "info"]:
setattr(torchaudio, func, getattr(module, func))
def _init_backend():
warnings.warn(
"TorchAudio's global backend is now deprecated. "
"Please enable distpatcher by setting `TORCHAUDIO_USE_BACKEND_DISPATCHER=1`, "
"and specify backend when calling load/info/save function.",
stacklevel=3,
)
backends = list_audio_backends()
if "sox_io" in backends:
set_audio_backend("sox_io")
elif "soundfile" in backends:
set_audio_backend("soundfile")
else:
set_audio_backend(None)
def get_audio_backend() -> Optional[str]:
"""Get the name of the current backend
Returns:
Optional[str]: The name of the current backend or ``None`` if no backend is assigned.
"""
if torchaudio.load == no_backend.load:
return None
if torchaudio.load == sox_io_backend.load:
return "sox_io"
if torchaudio.load == soundfile_backend.load:
return "soundfile"
raise ValueError("Unknown backend.")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment