common_utils.py 6.18 KB
Newer Older
1
import os
2
import shutil
3
import tempfile
4
import unittest
moto's avatar
moto committed
5
from typing import Union
6
from shutil import copytree
7

jamarshon's avatar
jamarshon committed
8
import torch
moto's avatar
moto committed
9
from torch.testing._internal.common_utils import TestCase as PytorchTestCase
10
import torchaudio
11
from torchaudio._internal.module_utils import is_module_available
12

13
_TEST_DIR_PATH = os.path.dirname(os.path.realpath(__file__))
14
BACKENDS = torchaudio.list_audio_backends()
15
16


17
18
19
20
21
def get_asset_path(*paths):
    """Return full path of a test asset"""
    return os.path.join(_TEST_DIR_PATH, 'assets', *paths)


22
23
24
25
26
27
28
def create_temp_assets_dir():
    """
    Creates a temporary directory and moves all files from test/assets there.
    Returns a Tuple[string, TemporaryDirectory] which is the folder path
    and object.
    """
    tmp_dir = tempfile.TemporaryDirectory()
29
    copytree(os.path.join(_TEST_DIR_PATH, "assets"),
30
31
             os.path.join(tmp_dir.name, "assets"))
    return tmp_dir.name, tmp_dir
jamarshon's avatar
jamarshon committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58


def random_float_tensor(seed, size, a=22695477, c=1, m=2 ** 32):
    """ Generates random tensors given a seed and size
    https://en.wikipedia.org/wiki/Linear_congruential_generator
    X_{n + 1} = (a * X_n + c) % m
    Using Borland C/C++ values

    The tensor will have values between [0,1)
    Inputs:
        seed (int): an int
        size (Tuple[int]): the size of the output tensor
        a (int): the multiplier constant to the generator
        c (int): the additive constant to the generator
        m (int): the modulus constant to the generator
    """
    num_elements = 1
    for s in size:
        num_elements *= s

    arr = [(a * seed + c) % m]
    for i in range(num_elements - 1):
        arr.append((a * arr[i] + c) % m)

    return torch.tensor(arr).float().view(size) / m


59
60
def filter_backends_with_mp3(backends):
    # Filter out backends that do not support mp3
61
    test_filepath = get_asset_path('steam-train-whistle-daniel_simon.mp3')
62
63

    def supports_mp3(backend):
moto's avatar
moto committed
64
        torchaudio.set_audio_backend(backend)
65
        try:
moto's avatar
moto committed
66
            torchaudio.load(test_filepath)
67
            return True
moto's avatar
moto committed
68
        except (RuntimeError, ImportError):
69
70
71
72
73
74
            return False

    return [backend for backend in backends if supports_mp3(backend)]


BACKENDS_MP3 = filter_backends_with_mp3(BACKENDS)
75
76


moto's avatar
moto committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def set_audio_backend(backend):
    """Allow additional backend value, 'default'"""
    if backend == 'default':
        if 'sox' in BACKENDS:
            be = 'sox'
        elif 'soundfile' in BACKENDS:
            be = 'soundfile'
        else:
            raise unittest.SkipTest('No default backend available')
    else:
        be = backend

    torchaudio.set_audio_backend(be)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
class TempDirMixin:
    """Mixin to provide easy access to temp dir"""
    temp_dir_ = None
    temp_dir = None

    def setUp(self):
        super().setUp()
        self._init_temp_dir()

    def tearDown(self):
        super().tearDownClass()
        self._clean_up_temp_dir()

    def _init_temp_dir(self):
        self.temp_dir_ = tempfile.TemporaryDirectory()
        self.temp_dir = self.temp_dir_.name

    def _clean_up_temp_dir(self):
        if self.temp_dir_ is not None:
            self.temp_dir_.cleanup()
            self.temp_dir_ = None
            self.temp_dir = None

    def get_temp_path(self, *paths):
        return os.path.join(self.temp_dir, *paths)


119
class TestBaseMixin:
moto's avatar
moto committed
120
    """Mixin to provide consistent way to define device/dtype/backend aware TestCase"""
121
122
    dtype = None
    device = None
moto's avatar
moto committed
123
    backend = None
124

moto's avatar
moto committed
125
126
127
    def setUp(self):
        super().setUp()
        set_audio_backend(self.backend)
128

moto's avatar
moto committed
129

moto's avatar
moto committed
130
131
class TorchaudioTestCase(TestBaseMixin, PytorchTestCase):
    pass
moto's avatar
moto committed
132

moto's avatar
moto committed
133

134
135
136
137
138
139
140
141
142
def skipIfNoExec(cmd):
    return unittest.skipIf(shutil.which(cmd) is None, f'`{cmd}` is not available')


def skipIfNoModule(module, display_name=None):
    display_name = display_name or module
    return unittest.skipIf(not is_module_available(module), f'"{display_name}" is not available')


moto's avatar
moto committed
143
144
skipIfNoSoxBackend = unittest.skipIf('sox' not in BACKENDS, 'Sox backend not available')
skipIfNoCuda = unittest.skipIf(not torch.cuda.is_available(), reason='CUDA not available')
145
skipIfNoExtension = skipIfNoModule('torchaudio._torchaudio', 'torchaudio C++ extension')
146
147


moto's avatar
moto committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
def get_whitenoise(
    *,
    sample_rate: int = 16000,
    duration: float = 1,  # seconds
    n_channels: int = 1,
    seed: int = 0,
    dtype: Union[str, torch.dtype] = "float32",
    device: Union[str, torch.device] = "cpu",
):
    """Generate pseudo audio data with whitenoise

    Args:
        sample_rate: Sampling rate
        duration: Length of the resulting Tensor in seconds.
        n_channels: Number of channels
        seed: Seed value used for random number generation.
            Note that this function does not modify global random generator state.
        dtype: Torch dtype
        device: device
    Returns:
        Tensor: shape of (n_channels, sample_rate * duration)
    """
    if isinstance(dtype, str):
        dtype = getattr(torch, dtype)
    shape = [n_channels, sample_rate * duration]
    # According to the doc, folking rng on all CUDA devices is slow when there are many CUDA devices,
    # so we only folk on CPU, generate values and move the data to the given device
    with torch.random.fork_rng([]):
        torch.random.manual_seed(seed)
        tensor = torch.randn(shape, dtype=dtype, device='cpu')
    tensor /= 2.0
    tensor.clamp_(-1.0, 1.0)
    return tensor.to(device=device)


def get_sinusoid(
    *,
    frequency: float = 300,
    sample_rate: int = 16000,
    duration: float = 1,  # seconds
    n_channels: int = 1,
    dtype: Union[str, torch.dtype] = "float32",
    device: Union[str, torch.device] = "cpu",
):
    """Generate pseudo audio data with sine wave.

    Args:
        frequency: Frequency of sine wave
        sample_rate: Sampling rate
        duration: Length of the resulting Tensor in seconds.
        n_channels: Number of channels
        dtype: Torch dtype
        device: device

    Returns:
        Tensor: shape of (n_channels, sample_rate * duration)
    """
    if isinstance(dtype, str):
        dtype = getattr(torch, dtype)
    pie2 = 2 * 3.141592653589793
    end = pie2 * frequency * duration
    theta = torch.linspace(0, end, sample_rate * duration, dtype=dtype, device=device)
    return torch.sin(theta, out=None).repeat([n_channels, 1])