torchaudio 2.9.1__cp311-cp311-manylinux_2_28_aarch64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- torchaudio/__init__.py +204 -0
- torchaudio/_extension/__init__.py +61 -0
- torchaudio/_extension/utils.py +133 -0
- torchaudio/_internal/__init__.py +10 -0
- torchaudio/_internal/module_utils.py +171 -0
- torchaudio/_torchcodec.py +340 -0
- torchaudio/compliance/__init__.py +5 -0
- torchaudio/compliance/kaldi.py +813 -0
- torchaudio/datasets/__init__.py +47 -0
- torchaudio/datasets/cmuarctic.py +157 -0
- torchaudio/datasets/cmudict.py +186 -0
- torchaudio/datasets/commonvoice.py +86 -0
- torchaudio/datasets/dr_vctk.py +121 -0
- torchaudio/datasets/fluentcommands.py +108 -0
- torchaudio/datasets/gtzan.py +1118 -0
- torchaudio/datasets/iemocap.py +147 -0
- torchaudio/datasets/librilight_limited.py +111 -0
- torchaudio/datasets/librimix.py +133 -0
- torchaudio/datasets/librispeech.py +174 -0
- torchaudio/datasets/librispeech_biasing.py +189 -0
- torchaudio/datasets/libritts.py +168 -0
- torchaudio/datasets/ljspeech.py +107 -0
- torchaudio/datasets/musdb_hq.py +139 -0
- torchaudio/datasets/quesst14.py +136 -0
- torchaudio/datasets/snips.py +157 -0
- torchaudio/datasets/speechcommands.py +183 -0
- torchaudio/datasets/tedlium.py +218 -0
- torchaudio/datasets/utils.py +54 -0
- torchaudio/datasets/vctk.py +143 -0
- torchaudio/datasets/voxceleb1.py +309 -0
- torchaudio/datasets/yesno.py +89 -0
- torchaudio/functional/__init__.py +130 -0
- torchaudio/functional/_alignment.py +128 -0
- torchaudio/functional/filtering.py +1685 -0
- torchaudio/functional/functional.py +2505 -0
- torchaudio/lib/__init__.py +0 -0
- torchaudio/lib/_torchaudio.so +0 -0
- torchaudio/lib/libtorchaudio.so +0 -0
- torchaudio/models/__init__.py +85 -0
- torchaudio/models/_hdemucs.py +1008 -0
- torchaudio/models/conformer.py +293 -0
- torchaudio/models/conv_tasnet.py +330 -0
- torchaudio/models/decoder/__init__.py +64 -0
- torchaudio/models/decoder/_ctc_decoder.py +568 -0
- torchaudio/models/decoder/_cuda_ctc_decoder.py +187 -0
- torchaudio/models/deepspeech.py +84 -0
- torchaudio/models/emformer.py +884 -0
- torchaudio/models/rnnt.py +816 -0
- torchaudio/models/rnnt_decoder.py +339 -0
- torchaudio/models/squim/__init__.py +11 -0
- torchaudio/models/squim/objective.py +326 -0
- torchaudio/models/squim/subjective.py +150 -0
- torchaudio/models/tacotron2.py +1046 -0
- torchaudio/models/wav2letter.py +72 -0
- torchaudio/models/wav2vec2/__init__.py +45 -0
- torchaudio/models/wav2vec2/components.py +1167 -0
- torchaudio/models/wav2vec2/model.py +1579 -0
- torchaudio/models/wav2vec2/utils/__init__.py +7 -0
- torchaudio/models/wav2vec2/utils/import_fairseq.py +213 -0
- torchaudio/models/wav2vec2/utils/import_huggingface.py +134 -0
- torchaudio/models/wav2vec2/wavlm_attention.py +214 -0
- torchaudio/models/wavernn.py +409 -0
- torchaudio/pipelines/__init__.py +102 -0
- torchaudio/pipelines/_source_separation_pipeline.py +109 -0
- torchaudio/pipelines/_squim_pipeline.py +156 -0
- torchaudio/pipelines/_tts/__init__.py +16 -0
- torchaudio/pipelines/_tts/impl.py +385 -0
- torchaudio/pipelines/_tts/interface.py +255 -0
- torchaudio/pipelines/_tts/utils.py +230 -0
- torchaudio/pipelines/_wav2vec2/__init__.py +0 -0
- torchaudio/pipelines/_wav2vec2/aligner.py +87 -0
- torchaudio/pipelines/_wav2vec2/impl.py +1699 -0
- torchaudio/pipelines/_wav2vec2/utils.py +346 -0
- torchaudio/pipelines/rnnt_pipeline.py +380 -0
- torchaudio/transforms/__init__.py +78 -0
- torchaudio/transforms/_multi_channel.py +467 -0
- torchaudio/transforms/_transforms.py +2138 -0
- torchaudio/utils/__init__.py +4 -0
- torchaudio/utils/download.py +89 -0
- torchaudio/version.py +2 -0
- torchaudio-2.9.1.dist-info/METADATA +133 -0
- torchaudio-2.9.1.dist-info/RECORD +85 -0
- torchaudio-2.9.1.dist-info/WHEEL +5 -0
- torchaudio-2.9.1.dist-info/licenses/LICENSE +25 -0
- torchaudio-2.9.1.dist-info/top_level.txt +1 -0
torchaudio/__init__.py
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from typing import BinaryIO, Optional, Tuple, Union
|
|
3
|
+
|
|
4
|
+
import torch
|
|
5
|
+
|
|
6
|
+
# Initialize extension and backend first
|
|
7
|
+
from . import _extension # noqa # usort: skip
|
|
8
|
+
from . import compliance, datasets, functional, models, pipelines, transforms, utils # noqa: F401
|
|
9
|
+
from ._torchcodec import load_with_torchcodec, save_with_torchcodec
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
try:
|
|
13
|
+
from .version import __version__, git_version # noqa: F401
|
|
14
|
+
except ImportError:
|
|
15
|
+
pass
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def load(
|
|
19
|
+
uri: Union[BinaryIO, str, os.PathLike],
|
|
20
|
+
frame_offset: int = 0,
|
|
21
|
+
num_frames: int = -1,
|
|
22
|
+
normalize: bool = True,
|
|
23
|
+
channels_first: bool = True,
|
|
24
|
+
format: Optional[str] = None,
|
|
25
|
+
buffer_size: int = 4096,
|
|
26
|
+
backend: Optional[str] = None,
|
|
27
|
+
) -> Tuple[torch.Tensor, int]:
|
|
28
|
+
"""Load audio data from source using TorchCodec's AudioDecoder.
|
|
29
|
+
|
|
30
|
+
.. note::
|
|
31
|
+
|
|
32
|
+
As of TorchAudio 2.9, this function relies on TorchCodec's decoding capabilities under the hood. It is
|
|
33
|
+
provided for convenience, but we do recommend that you port your code to
|
|
34
|
+
natively use ``torchcodec``'s ``AudioDecoder`` class for better
|
|
35
|
+
performance:
|
|
36
|
+
https://docs.pytorch.org/torchcodec/stable/generated/torchcodec.decoders.AudioDecoder.
|
|
37
|
+
Because of the reliance on Torchcodec, the parameters ``normalize``, ``buffer_size``, and
|
|
38
|
+
``backend`` are ignored and accepted only for backwards compatibility.
|
|
39
|
+
To install torchcodec, follow the instructions at https://github.com/pytorch/torchcodec#installing-torchcodec.
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
uri (path-like object or file-like object):
|
|
44
|
+
Source of audio data. The following types are accepted:
|
|
45
|
+
|
|
46
|
+
* ``path-like``: File path or URL.
|
|
47
|
+
* ``file-like``: Object with ``read(size: int) -> bytes`` method.
|
|
48
|
+
|
|
49
|
+
frame_offset (int, optional):
|
|
50
|
+
Number of samples to skip before start reading data.
|
|
51
|
+
num_frames (int, optional):
|
|
52
|
+
Maximum number of samples to read. ``-1`` reads all the remaining samples,
|
|
53
|
+
starting from ``frame_offset``.
|
|
54
|
+
normalize (bool, optional):
|
|
55
|
+
TorchCodec always returns normalized float32 samples. This parameter
|
|
56
|
+
is ignored and a warning is issued if set to False.
|
|
57
|
+
Default: ``True``.
|
|
58
|
+
channels_first (bool, optional):
|
|
59
|
+
When True, the returned Tensor has dimension `[channel, time]`.
|
|
60
|
+
Otherwise, the returned Tensor's dimension is `[time, channel]`.
|
|
61
|
+
format (str or None, optional):
|
|
62
|
+
Format hint for the decoder. May not be supported by all TorchCodec
|
|
63
|
+
decoders. (Default: ``None``)
|
|
64
|
+
buffer_size (int, optional):
|
|
65
|
+
Not used by TorchCodec AudioDecoder. Provided for API compatibility.
|
|
66
|
+
backend (str or None, optional):
|
|
67
|
+
Not used by TorchCodec AudioDecoder. Provided for API compatibility.
|
|
68
|
+
|
|
69
|
+
Returns:
|
|
70
|
+
(torch.Tensor, int): Resulting Tensor and sample rate.
|
|
71
|
+
Always returns float32 tensors. If ``channels_first=True``, shape is
|
|
72
|
+
`[channel, time]`, otherwise `[time, channel]`.
|
|
73
|
+
|
|
74
|
+
Raises:
|
|
75
|
+
ImportError: If torchcodec is not available.
|
|
76
|
+
ValueError: If unsupported parameters are used.
|
|
77
|
+
RuntimeError: If TorchCodec fails to decode the audio.
|
|
78
|
+
|
|
79
|
+
Note:
|
|
80
|
+
- TorchCodec always returns normalized float32 samples, so the ``normalize``
|
|
81
|
+
parameter has no effect.
|
|
82
|
+
- The ``buffer_size`` and ``backend`` parameters are ignored.
|
|
83
|
+
- Not all audio formats supported by torchaudio backends may be supported
|
|
84
|
+
by TorchCodec.
|
|
85
|
+
"""
|
|
86
|
+
return load_with_torchcodec(
|
|
87
|
+
uri,
|
|
88
|
+
frame_offset=frame_offset,
|
|
89
|
+
num_frames=num_frames,
|
|
90
|
+
normalize=normalize,
|
|
91
|
+
channels_first=channels_first,
|
|
92
|
+
format=format,
|
|
93
|
+
buffer_size=buffer_size,
|
|
94
|
+
backend=backend,
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def save(
|
|
99
|
+
uri: Union[str, os.PathLike],
|
|
100
|
+
src: torch.Tensor,
|
|
101
|
+
sample_rate: int,
|
|
102
|
+
channels_first: bool = True,
|
|
103
|
+
format: Optional[str] = None,
|
|
104
|
+
encoding: Optional[str] = None,
|
|
105
|
+
bits_per_sample: Optional[int] = None,
|
|
106
|
+
buffer_size: int = 4096,
|
|
107
|
+
backend: Optional[str] = None,
|
|
108
|
+
compression: Optional[Union[float, int]] = None,
|
|
109
|
+
) -> None:
|
|
110
|
+
"""Save audio data to file using TorchCodec's AudioEncoder.
|
|
111
|
+
|
|
112
|
+
.. note::
|
|
113
|
+
|
|
114
|
+
As of TorchAudio 2.9, this function relies on TorchCodec's encoding capabilities under the hood.
|
|
115
|
+
It is provided for convenience, but we do recommend that you port your code to
|
|
116
|
+
natively use ``torchcodec``'s ``AudioEncoder`` class for better
|
|
117
|
+
performance:
|
|
118
|
+
https://docs.pytorch.org/torchcodec/stable/generated/torchcodec.encoders.AudioEncoder.
|
|
119
|
+
Because of the reliance on Torchcodec, the parameters ``format``, ``encoding``,
|
|
120
|
+
``bits_per_sample``, ``buffer_size``, and ``backend``, are ignored and accepted only for
|
|
121
|
+
backwards compatibility.
|
|
122
|
+
To install torchcodec, follow the instructions at https://github.com/pytorch/torchcodec#installing-torchcodec.
|
|
123
|
+
|
|
124
|
+
Args:
|
|
125
|
+
uri (path-like object):
|
|
126
|
+
Path to save the audio file. The file extension determines the format.
|
|
127
|
+
|
|
128
|
+
src (torch.Tensor):
|
|
129
|
+
Audio data to save. Must be a 1D or 2D tensor with float32 values
|
|
130
|
+
in the range [-1, 1]. If 2D, shape should be [channel, time] when
|
|
131
|
+
channels_first=True, or [time, channel] when channels_first=False.
|
|
132
|
+
|
|
133
|
+
sample_rate (int):
|
|
134
|
+
Sample rate of the audio data.
|
|
135
|
+
|
|
136
|
+
channels_first (bool, optional):
|
|
137
|
+
Indicates whether the input tensor has channels as the first dimension.
|
|
138
|
+
If True, expects [channel, time]. If False, expects [time, channel].
|
|
139
|
+
Default: True.
|
|
140
|
+
|
|
141
|
+
format (str or None, optional):
|
|
142
|
+
Audio format hint. Not used by TorchCodec (format is determined by
|
|
143
|
+
file extension). A warning is issued if provided.
|
|
144
|
+
Default: None.
|
|
145
|
+
|
|
146
|
+
encoding (str or None, optional):
|
|
147
|
+
Audio encoding. Not fully supported by TorchCodec AudioEncoder.
|
|
148
|
+
A warning is issued if provided. Default: None.
|
|
149
|
+
|
|
150
|
+
bits_per_sample (int or None, optional):
|
|
151
|
+
Bits per sample. Not directly supported by TorchCodec AudioEncoder.
|
|
152
|
+
A warning is issued if provided. Default: None.
|
|
153
|
+
|
|
154
|
+
buffer_size (int, optional):
|
|
155
|
+
Not used by TorchCodec AudioEncoder. Provided for API compatibility.
|
|
156
|
+
A warning is issued if not default value. Default: 4096.
|
|
157
|
+
|
|
158
|
+
backend (str or None, optional):
|
|
159
|
+
Not used by TorchCodec AudioEncoder. Provided for API compatibility.
|
|
160
|
+
A warning is issued if provided. Default: None.
|
|
161
|
+
|
|
162
|
+
compression (float, int or None, optional):
|
|
163
|
+
Compression level or bit rate. Maps to bit_rate parameter in
|
|
164
|
+
TorchCodec AudioEncoder. Default: None.
|
|
165
|
+
|
|
166
|
+
Raises:
|
|
167
|
+
ImportError: If torchcodec is not available.
|
|
168
|
+
ValueError: If input parameters are invalid.
|
|
169
|
+
RuntimeError: If TorchCodec fails to encode the audio.
|
|
170
|
+
|
|
171
|
+
Note:
|
|
172
|
+
- TorchCodec AudioEncoder expects float32 samples in [-1, 1] range.
|
|
173
|
+
- Some parameters (format, encoding, bits_per_sample, buffer_size, backend)
|
|
174
|
+
are not used by TorchCodec but are provided for API compatibility.
|
|
175
|
+
- The output format is determined by the file extension in the uri.
|
|
176
|
+
- TorchCodec uses FFmpeg under the hood for encoding.
|
|
177
|
+
"""
|
|
178
|
+
return save_with_torchcodec(
|
|
179
|
+
uri,
|
|
180
|
+
src,
|
|
181
|
+
sample_rate,
|
|
182
|
+
channels_first=channels_first,
|
|
183
|
+
format=format,
|
|
184
|
+
encoding=encoding,
|
|
185
|
+
bits_per_sample=bits_per_sample,
|
|
186
|
+
buffer_size=buffer_size,
|
|
187
|
+
backend=backend,
|
|
188
|
+
compression=compression,
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
__all__ = [
|
|
193
|
+
"load",
|
|
194
|
+
"load_with_torchcodec",
|
|
195
|
+
"save_with_torchcodec",
|
|
196
|
+
"save",
|
|
197
|
+
"compliance",
|
|
198
|
+
"datasets",
|
|
199
|
+
"functional",
|
|
200
|
+
"models",
|
|
201
|
+
"pipelines",
|
|
202
|
+
"utils",
|
|
203
|
+
"transforms",
|
|
204
|
+
]
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import os
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
from torchaudio._internal.module_utils import fail_with_message, is_module_available, no_op
|
|
6
|
+
|
|
7
|
+
from .utils import _check_cuda_version, _init_dll_path, _load_lib
|
|
8
|
+
|
|
9
|
+
_LG = logging.getLogger(__name__)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# Note:
|
|
13
|
+
# `_check_cuda_version` is not meant to be used by regular users.
|
|
14
|
+
# Builder uses it for debugging purpose, so we export it.
|
|
15
|
+
# https://github.com/pytorch/builder/blob/e2e4542b8eb0bdf491214451a1a4128bd606cce2/test/smoke_test/smoke_test.py#L80
|
|
16
|
+
__all__ = [
|
|
17
|
+
"_check_cuda_version",
|
|
18
|
+
"_IS_TORCHAUDIO_EXT_AVAILABLE",
|
|
19
|
+
"_IS_RIR_AVAILABLE",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
if os.name == "nt" and (3, 8) <= sys.version_info < (3, 9):
|
|
24
|
+
_init_dll_path()
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
# When the extension module is built, we initialize it.
|
|
28
|
+
# In case of an error, we do not catch the failure as it suggests there is something
|
|
29
|
+
# wrong with the installation.
|
|
30
|
+
_IS_TORCHAUDIO_EXT_AVAILABLE = is_module_available("torchaudio.lib._torchaudio")
|
|
31
|
+
# RIR features are implemented in _torchaudio extension, but they can be individually
|
|
32
|
+
# turned on/off at build time. Available means that _torchaudio is loaded properly, and
|
|
33
|
+
# RIR features are found there.
|
|
34
|
+
_IS_RIR_AVAILABLE = False
|
|
35
|
+
_IS_ALIGN_AVAILABLE = False
|
|
36
|
+
if _IS_TORCHAUDIO_EXT_AVAILABLE:
|
|
37
|
+
_load_lib("libtorchaudio")
|
|
38
|
+
|
|
39
|
+
import torchaudio.lib._torchaudio # noqa
|
|
40
|
+
|
|
41
|
+
_check_cuda_version()
|
|
42
|
+
_IS_RIR_AVAILABLE = torchaudio.lib._torchaudio.is_rir_available()
|
|
43
|
+
_IS_ALIGN_AVAILABLE = torchaudio.lib._torchaudio.is_align_available()
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
fail_if_no_rir = (
|
|
47
|
+
no_op
|
|
48
|
+
if _IS_RIR_AVAILABLE
|
|
49
|
+
else fail_with_message(
|
|
50
|
+
"requires RIR extension, but TorchAudio is not compiled with it. Please build TorchAudio with RIR support."
|
|
51
|
+
)
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
fail_if_no_align = (
|
|
55
|
+
no_op
|
|
56
|
+
if _IS_ALIGN_AVAILABLE
|
|
57
|
+
else fail_with_message(
|
|
58
|
+
"Requires alignment extension, but TorchAudio is not compiled with it. \
|
|
59
|
+
Please build TorchAudio with alignment support."
|
|
60
|
+
)
|
|
61
|
+
)
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"""Module to implement logics used for initializing extensions.
|
|
2
|
+
|
|
3
|
+
The implementations here should be stateless.
|
|
4
|
+
They should not depend on external state.
|
|
5
|
+
Anything that depends on external state should happen in __init__.py
|
|
6
|
+
"""
|
|
7
|
+
import logging
|
|
8
|
+
import os
|
|
9
|
+
import types
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
import torch
|
|
13
|
+
|
|
14
|
+
_LG = logging.getLogger(__name__)
|
|
15
|
+
_LIB_DIR = Path(__file__).parent.parent / "lib"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _get_lib_path(lib: str):
|
|
19
|
+
suffix = "pyd" if os.name == "nt" else "so"
|
|
20
|
+
path = _LIB_DIR / f"{lib}.{suffix}"
|
|
21
|
+
return path
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _load_lib(lib: str) -> bool:
|
|
25
|
+
"""Load extension module
|
|
26
|
+
|
|
27
|
+
Note:
|
|
28
|
+
In case `torchaudio` is deployed with `pex` format, the library file
|
|
29
|
+
is not in a standard location.
|
|
30
|
+
In this case, we expect that `libtorchaudio` is available somewhere
|
|
31
|
+
in the search path of dynamic loading mechanism, so that importing
|
|
32
|
+
`_torchaudio` will have library loader find and load `libtorchaudio`.
|
|
33
|
+
This is the reason why the function should not raising an error when the library
|
|
34
|
+
file is not found.
|
|
35
|
+
|
|
36
|
+
Returns:
|
|
37
|
+
bool:
|
|
38
|
+
True if the library file is found AND the library loaded without failure.
|
|
39
|
+
False if the library file is not found (like in the case where torchaudio
|
|
40
|
+
is deployed with pex format, thus the shared library file is
|
|
41
|
+
in a non-standard location.).
|
|
42
|
+
If the library file is found but there is an issue loading the library,
|
|
43
|
+
(such as missing dependency) then this function raises the exception as-is.
|
|
44
|
+
|
|
45
|
+
Raises:
|
|
46
|
+
Exception:
|
|
47
|
+
If the library file is found, but there is an issue loading the library file,
|
|
48
|
+
(when underlying `ctype.DLL` throws an exception), this function will pass
|
|
49
|
+
the exception as-is, instead of catching it and returning bool.
|
|
50
|
+
The expected case is `OSError` thrown by `ctype.DLL` when a dynamic dependency
|
|
51
|
+
is not found.
|
|
52
|
+
This behavior was chosen because the expected failure case is not recoverable.
|
|
53
|
+
If a dependency is missing, then users have to install it.
|
|
54
|
+
"""
|
|
55
|
+
path = _get_lib_path(lib)
|
|
56
|
+
if not path.exists():
|
|
57
|
+
return False
|
|
58
|
+
torch.ops.load_library(path)
|
|
59
|
+
return True
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class _LazyImporter(types.ModuleType):
|
|
63
|
+
"""Lazily import module/extension."""
|
|
64
|
+
|
|
65
|
+
def __init__(self, name, import_func):
|
|
66
|
+
super().__init__(name)
|
|
67
|
+
self.import_func = import_func
|
|
68
|
+
self.module = None
|
|
69
|
+
|
|
70
|
+
# Note:
|
|
71
|
+
# Python caches what was retrieved with `__getattr__`, so this method will not be
|
|
72
|
+
# called again for the same item.
|
|
73
|
+
def __getattr__(self, item):
|
|
74
|
+
self._import_once()
|
|
75
|
+
return getattr(self.module, item)
|
|
76
|
+
|
|
77
|
+
def __repr__(self):
|
|
78
|
+
if self.module is None:
|
|
79
|
+
return f"<module '{self.__module__}.{self.__class__.__name__}(\"{self.name}\")'>"
|
|
80
|
+
return repr(self.module)
|
|
81
|
+
|
|
82
|
+
def __dir__(self):
|
|
83
|
+
self._import_once()
|
|
84
|
+
return dir(self.module)
|
|
85
|
+
|
|
86
|
+
def _import_once(self):
|
|
87
|
+
if self.module is None:
|
|
88
|
+
self.module = self.import_func()
|
|
89
|
+
# Note:
|
|
90
|
+
# By attaching the module attributes to self,
|
|
91
|
+
# module attributes are directly accessible.
|
|
92
|
+
# This allows to avoid calling __getattr__ for every attribute access.
|
|
93
|
+
self.__dict__.update(self.module.__dict__)
|
|
94
|
+
|
|
95
|
+
def is_available(self):
|
|
96
|
+
try:
|
|
97
|
+
self._import_once()
|
|
98
|
+
except Exception:
|
|
99
|
+
return False
|
|
100
|
+
return True
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _init_dll_path():
|
|
104
|
+
# On Windows Python-3.8+ has `os.add_dll_directory` call,
|
|
105
|
+
# which is called to configure dll search path.
|
|
106
|
+
# To find cuda related dlls we need to make sure the
|
|
107
|
+
# conda environment/bin path is configured Please take a look:
|
|
108
|
+
# https://stackoverflow.com/questions/59330863/cant-import-dll-module-in-python
|
|
109
|
+
# Please note: if some path can't be added using add_dll_directory we simply ignore this path
|
|
110
|
+
for path in os.environ.get("PATH", "").split(";"):
|
|
111
|
+
if os.path.exists(path):
|
|
112
|
+
try:
|
|
113
|
+
os.add_dll_directory(path)
|
|
114
|
+
except Exception:
|
|
115
|
+
pass
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _check_cuda_version():
|
|
119
|
+
import torchaudio.lib._torchaudio
|
|
120
|
+
|
|
121
|
+
version = torchaudio.lib._torchaudio.cuda_version()
|
|
122
|
+
if version is not None and torch.version.cuda is not None:
|
|
123
|
+
version_str = str(version)
|
|
124
|
+
ta_version = f"{version_str[:-3]}.{version_str[-2]}"
|
|
125
|
+
t_version = torch.version.cuda.split(".")
|
|
126
|
+
t_version = f"{t_version[0]}.{t_version[1]}"
|
|
127
|
+
if ta_version != t_version:
|
|
128
|
+
raise RuntimeError(
|
|
129
|
+
"Detected that PyTorch and TorchAudio were compiled with different CUDA versions. "
|
|
130
|
+
f"PyTorch has CUDA version {t_version} whereas TorchAudio has CUDA version {ta_version}. "
|
|
131
|
+
"Please install the TorchAudio version that matches your PyTorch version."
|
|
132
|
+
)
|
|
133
|
+
return version
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import importlib.util
|
|
2
|
+
import os
|
|
3
|
+
import warnings
|
|
4
|
+
from functools import partial, wraps
|
|
5
|
+
from typing import Optional
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def eval_env(var, default):
|
|
9
|
+
"""Check if environment varable has True-y value"""
|
|
10
|
+
if var not in os.environ:
|
|
11
|
+
return default
|
|
12
|
+
|
|
13
|
+
val = os.environ.get(var, "0")
|
|
14
|
+
trues = ["1", "true", "TRUE", "on", "ON", "yes", "YES"]
|
|
15
|
+
falses = ["0", "false", "FALSE", "off", "OFF", "no", "NO"]
|
|
16
|
+
if val in trues:
|
|
17
|
+
return True
|
|
18
|
+
if val not in falses:
|
|
19
|
+
# fmt: off
|
|
20
|
+
raise RuntimeError(
|
|
21
|
+
f"Unexpected environment variable value `{var}={val}`. "
|
|
22
|
+
f"Expected one of {trues + falses}")
|
|
23
|
+
# fmt: on
|
|
24
|
+
return False
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def is_module_available(*modules: str) -> bool:
|
|
28
|
+
r"""Returns if a top-level module with :attr:`name` exists *without**
|
|
29
|
+
importing it. This is generally safer than try-catch block around a
|
|
30
|
+
`import X`. It avoids third party libraries breaking assumptions of some of
|
|
31
|
+
our tests, e.g., setting multiprocessing start method when imported
|
|
32
|
+
(see librosa/#747, torchvision/#544).
|
|
33
|
+
"""
|
|
34
|
+
return all(importlib.util.find_spec(m) is not None for m in modules)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def requires_module(*modules: str):
|
|
38
|
+
"""Decorate function to give error message if invoked without required optional modules.
|
|
39
|
+
|
|
40
|
+
This decorator is to give better error message to users rather
|
|
41
|
+
than raising ``NameError: name 'module' is not defined`` at random places.
|
|
42
|
+
"""
|
|
43
|
+
missing = [m for m in modules if not is_module_available(m)]
|
|
44
|
+
|
|
45
|
+
if not missing:
|
|
46
|
+
# fall through. If all the modules are available, no need to decorate
|
|
47
|
+
def decorator(func):
|
|
48
|
+
return func
|
|
49
|
+
|
|
50
|
+
else:
|
|
51
|
+
req = f"module: {missing[0]}" if len(missing) == 1 else f"modules: {missing}"
|
|
52
|
+
|
|
53
|
+
def decorator(func):
|
|
54
|
+
@wraps(func)
|
|
55
|
+
def wrapped(*args, **kwargs):
|
|
56
|
+
raise RuntimeError(f"{func.__module__}.{func.__name__} requires {req}")
|
|
57
|
+
|
|
58
|
+
return wrapped
|
|
59
|
+
|
|
60
|
+
return decorator
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
UNSUPPORTED = []
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def wrap_deprecated(func, name, direction: str, version: Optional[str] = None, remove: bool = False):
|
|
67
|
+
@wraps(func)
|
|
68
|
+
def wrapped(*args, **kwargs):
|
|
69
|
+
message = f"{name} has been deprecated. {direction}"
|
|
70
|
+
if remove:
|
|
71
|
+
message += f' It will be removed from {"a future" if version is None else "the " + str(version)} release. '
|
|
72
|
+
warnings.warn(message, stacklevel=2)
|
|
73
|
+
return func(*args, **kwargs)
|
|
74
|
+
|
|
75
|
+
return wrapped
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def deprecated(direction: str, version: Optional[str] = None, remove: bool = False):
|
|
79
|
+
"""Decorator to add deprecation message
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
direction (str): Migration steps to be given to users.
|
|
83
|
+
version (str or int): The version when the object will be removed
|
|
84
|
+
remove (bool): If enabled, append future removal message.
|
|
85
|
+
"""
|
|
86
|
+
|
|
87
|
+
def decorator(func):
|
|
88
|
+
wrapped = wrap_deprecated(func, f"{func.__module__}.{func.__name__}", direction, version=version, remove=remove)
|
|
89
|
+
|
|
90
|
+
message = "This function has been deprecated. "
|
|
91
|
+
if remove:
|
|
92
|
+
message += f'It will be removed from {"future" if version is None else version} release. '
|
|
93
|
+
|
|
94
|
+
wrapped.__doc__ = f"""DEPRECATED
|
|
95
|
+
|
|
96
|
+
.. warning::
|
|
97
|
+
|
|
98
|
+
{message}
|
|
99
|
+
{direction}
|
|
100
|
+
|
|
101
|
+
{func.__doc__}
|
|
102
|
+
"""
|
|
103
|
+
|
|
104
|
+
return wrapped
|
|
105
|
+
|
|
106
|
+
return decorator
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
DEPRECATION_MSG = (
|
|
110
|
+
"This deprecation is part of a large refactoring effort to transition TorchAudio into a maintenance phase. "
|
|
111
|
+
"Please see https://github.com/pytorch/audio/issues/3902 for more information."
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
IO_DEPRECATION_MSG = (
|
|
115
|
+
"This deprecation is part of a large refactoring effort to transition TorchAudio into a maintenance phase. "
|
|
116
|
+
"The decoding and encoding capabilities of PyTorch for both audio"
|
|
117
|
+
" and video are being consolidated into TorchCodec. "
|
|
118
|
+
"Please see https://github.com/pytorch/audio/issues/3902 for more information."
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
dropping_support = deprecated(DEPRECATION_MSG, version="2.9", remove=True)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def dropping_class_support(c, msg=DEPRECATION_MSG):
|
|
125
|
+
c.__init__ = wrap_deprecated(c.__init__, f"{c.__module__}.{c.__name__}", msg, version="2.9", remove=True)
|
|
126
|
+
c.__doc__ = f"""DEPRECATED
|
|
127
|
+
|
|
128
|
+
.. warning::
|
|
129
|
+
|
|
130
|
+
This class is deprecated from version 2.8. It will be removed in the 2.9 release.
|
|
131
|
+
{msg}
|
|
132
|
+
{c.__doc__}
|
|
133
|
+
"""
|
|
134
|
+
|
|
135
|
+
UNSUPPORTED.append(c)
|
|
136
|
+
return c
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def dropping_const_support(c, msg=DEPRECATION_MSG, name=None):
|
|
140
|
+
c.__doc__ = f"""[DEPRECATED]
|
|
141
|
+
|
|
142
|
+
.. warning::
|
|
143
|
+
|
|
144
|
+
This object is deprecated deprecated from version 2.8. It will be removed in the 2.9 release.
|
|
145
|
+
{msg}
|
|
146
|
+
{c.__doc__}
|
|
147
|
+
"""
|
|
148
|
+
return c
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
dropping_class_io_support = partial(dropping_class_support, msg=IO_DEPRECATION_MSG)
|
|
152
|
+
|
|
153
|
+
dropping_io_support = deprecated(IO_DEPRECATION_MSG, version="2.9", remove=True)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def fail_with_message(message):
|
|
157
|
+
"""Generate decorator to give users message about missing TorchAudio extension."""
|
|
158
|
+
|
|
159
|
+
def decorator(func):
|
|
160
|
+
@wraps(func)
|
|
161
|
+
def wrapped(*args, **kwargs):
|
|
162
|
+
raise RuntimeError(f"{func.__module__}.{func.__name__} {message}")
|
|
163
|
+
|
|
164
|
+
return wrapped
|
|
165
|
+
|
|
166
|
+
return decorator
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def no_op(func):
|
|
170
|
+
"""Op-op decorator. Used in place of fail_with_message when a functionality that requires extension works fine."""
|
|
171
|
+
return func
|