forked from pytorch/audio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix backward compatibility layer in backend module
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. 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
Showing
7 changed files
with
107 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters