File: model_test_impl.py

package info (click to toggle)
pytorch-audio 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,696 kB
  • sloc: python: 61,274; cpp: 10,031; sh: 128; ansic: 70; makefile: 34
file content (395 lines) | stat: -rw-r--r-- 14,866 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
from typing import Tuple

import torch
from parameterized import parameterized
from torch import Tensor
from torchaudio.models import Tacotron2
from torchaudio.models.tacotron2 import _Decoder, _Encoder
from torchaudio_unittest.common_utils import skipIfPy310, TestBaseMixin, torch_script


class Tacotron2InferenceWrapper(torch.nn.Module):
    def __init__(self, model):
        super().__init__()
        self.model = model

    def forward(self, text: Tensor, text_lengths: Tensor) -> Tuple[Tensor, Tensor, Tensor]:
        return self.model.infer(text, text_lengths)


class Tacotron2DecoderInferenceWrapper(torch.nn.Module):
    def __init__(self, model):
        super().__init__()
        self.model = model

    def forward(self, memory: Tensor, memory_lengths: Tensor) -> Tuple[Tensor, Tensor, Tensor, Tensor]:
        return self.model.infer(memory, memory_lengths)


class TorchscriptConsistencyMixin(TestBaseMixin):
    r"""Mixin to provide easy access assert torchscript consistency"""

    def _assert_torchscript_consistency(self, model, tensors):
        ts_func = torch_script(model)

        torch.random.manual_seed(40)
        output = model(*tensors)

        torch.random.manual_seed(40)
        ts_output = ts_func(*tensors)

        self.assertEqual(ts_output, output)


class Tacotron2EncoderTests(TorchscriptConsistencyMixin):
    @skipIfPy310
    def test_tacotron2_torchscript_consistency(self):
        r"""Validate the torchscript consistency of a Encoder."""
        n_batch, n_seq, encoder_embedding_dim = 16, 64, 512
        model = (
            _Encoder(encoder_embedding_dim=encoder_embedding_dim, encoder_n_convolution=3, encoder_kernel_size=5)
            .to(self.device)
            .eval()
        )

        x = torch.rand(n_batch, encoder_embedding_dim, n_seq, device=self.device, dtype=self.dtype)
        input_lengths = torch.ones(n_batch, device=self.device, dtype=torch.int32) * n_seq

        self._assert_torchscript_consistency(model, (x, input_lengths))

    def test_encoder_output_shape(self):
        r"""Feed tensors with specific shape to Tacotron2 Decoder and validate
        that it outputs with a tensor with expected shape.
        """
        n_batch, n_seq, encoder_embedding_dim = 16, 64, 512
        model = (
            _Encoder(encoder_embedding_dim=encoder_embedding_dim, encoder_n_convolution=3, encoder_kernel_size=5)
            .to(self.device)
            .eval()
        )

        x = torch.rand(n_batch, encoder_embedding_dim, n_seq, device=self.device, dtype=self.dtype)
        input_lengths = torch.ones(n_batch, device=self.device, dtype=torch.int32) * n_seq
        out = model(x, input_lengths)

        assert out.size() == (n_batch, n_seq, encoder_embedding_dim)


def _get_decoder_model(n_mels=80, encoder_embedding_dim=512, decoder_max_step=2000, gate_threshold=0.5):
    model = _Decoder(
        n_mels=n_mels,
        n_frames_per_step=1,
        encoder_embedding_dim=encoder_embedding_dim,
        decoder_rnn_dim=1024,
        decoder_max_step=decoder_max_step,
        decoder_dropout=0.1,
        decoder_early_stopping=True,
        attention_rnn_dim=1024,
        attention_hidden_dim=128,
        attention_location_n_filter=32,
        attention_location_kernel_size=31,
        attention_dropout=0.1,
        prenet_dim=256,
        gate_threshold=gate_threshold,
    )
    return model


class Tacotron2DecoderTests(TorchscriptConsistencyMixin):
    @parameterized.expand(
        [
            (1,),
            (16,),
        ]
    )
    def test_decoder_torchscript_consistency(self, n_batch):
        r"""Validate the torchscript consistency of a Decoder."""
        n_mels = 80
        n_seq = 200
        encoder_embedding_dim = 256
        n_time_steps = 150

        model = _get_decoder_model(n_mels=n_mels, encoder_embedding_dim=encoder_embedding_dim)
        model = model.to(self.device).eval()

        memory = torch.rand(n_batch, n_seq, encoder_embedding_dim, dtype=self.dtype, device=self.device)
        decoder_inputs = torch.rand(n_batch, n_mels, n_time_steps, dtype=self.dtype, device=self.device)
        memory_lengths = torch.ones(n_batch, dtype=torch.int32, device=self.device)

        self._assert_torchscript_consistency(model, (memory, decoder_inputs, memory_lengths))

    @parameterized.expand(
        [
            (1,),
            (16,),
        ]
    )
    def test_decoder_output_shape(self, n_batch):
        r"""Feed tensors with specific shape to Tacotron2 Decoder and validate
        that it outputs with a tensor with expected shape.
        """
        n_mels = 80
        n_seq = 200
        encoder_embedding_dim = 256
        n_time_steps = 150

        model = _get_decoder_model(n_mels=n_mels, encoder_embedding_dim=encoder_embedding_dim)
        model = model.to(self.device).eval()

        memory = torch.rand(n_batch, n_seq, encoder_embedding_dim, dtype=self.dtype, device=self.device)
        decoder_inputs = torch.rand(n_batch, n_mels, n_time_steps, dtype=self.dtype, device=self.device)
        memory_lengths = torch.ones(n_batch, dtype=torch.int32, device=self.device)

        mel_specgram, gate_outputs, alignments = model(memory, decoder_inputs, memory_lengths)

        assert mel_specgram.size() == (n_batch, n_mels, n_time_steps)
        assert gate_outputs.size() == (n_batch, n_time_steps)
        assert alignments.size() == (n_batch, n_time_steps, n_seq)

    @parameterized.expand(
        [
            (1,),
            (16,),
        ]
    )
    def test_decoder_inference_torchscript_consistency(self, n_batch):
        r"""Validate the torchscript consistency of a Decoder."""
        n_mels = 80
        n_seq = 200
        encoder_embedding_dim = 256
        decoder_max_step = 300  # make inference more efficient
        gate_threshold = 0.505  # make inference more efficient

        model = _get_decoder_model(
            n_mels=n_mels,
            encoder_embedding_dim=encoder_embedding_dim,
            decoder_max_step=decoder_max_step,
            gate_threshold=gate_threshold,
        )
        model = model.to(self.device).eval()

        memory = torch.rand(n_batch, n_seq, encoder_embedding_dim, dtype=self.dtype, device=self.device)
        memory_lengths = torch.ones(n_batch, dtype=torch.int32, device=self.device)

        model_wrapper = Tacotron2DecoderInferenceWrapper(model)

        self._assert_torchscript_consistency(model_wrapper, (memory, memory_lengths))

    @parameterized.expand(
        [
            (1,),
            (16,),
        ]
    )
    def test_decoder_inference_output_shape(self, n_batch):
        r"""Validate the torchscript consistency of a Decoder."""
        n_mels = 80
        n_seq = 200
        encoder_embedding_dim = 256
        decoder_max_step = 300  # make inference more efficient
        gate_threshold = 0.505  # if set to 0.5, the model will only run one step

        model = _get_decoder_model(
            n_mels=n_mels,
            encoder_embedding_dim=encoder_embedding_dim,
            decoder_max_step=decoder_max_step,
            gate_threshold=gate_threshold,
        )
        model = model.to(self.device).eval()

        memory = torch.rand(n_batch, n_seq, encoder_embedding_dim, dtype=self.dtype, device=self.device)
        memory_lengths = torch.ones(n_batch, dtype=torch.int32, device=self.device)

        mel_specgram, mel_specgram_lengths, gate_outputs, alignments = model.infer(memory, memory_lengths)

        assert len(mel_specgram.size()) == 3
        assert mel_specgram.size()[:-1] == (
            n_batch,
            n_mels,
        )
        assert mel_specgram.size()[2] == mel_specgram_lengths.max().item()
        assert len(mel_specgram_lengths.size()) == 1
        assert mel_specgram_lengths.size()[0] == n_batch
        assert mel_specgram_lengths.max().item() <= model.decoder_max_step
        assert len(gate_outputs.size()) == 2
        assert gate_outputs.size()[0] == n_batch
        assert gate_outputs.size()[1] == mel_specgram_lengths.max().item()
        assert len(alignments.size()) == 2
        assert alignments.size()[0] == n_seq
        assert alignments.size()[1] == mel_specgram_lengths.max().item() * n_batch


def _get_tacotron2_model(n_mels, decoder_max_step=2000, gate_threshold=0.5):
    return Tacotron2(
        mask_padding=False,
        n_mels=n_mels,
        n_symbol=148,
        n_frames_per_step=1,
        symbol_embedding_dim=512,
        encoder_embedding_dim=512,
        encoder_n_convolution=3,
        encoder_kernel_size=5,
        decoder_rnn_dim=1024,
        decoder_max_step=decoder_max_step,
        decoder_dropout=0.1,
        decoder_early_stopping=True,
        attention_rnn_dim=1024,
        attention_hidden_dim=128,
        attention_location_n_filter=32,
        attention_location_kernel_size=31,
        attention_dropout=0.1,
        prenet_dim=256,
        postnet_n_convolution=5,
        postnet_kernel_size=5,
        postnet_embedding_dim=512,
        gate_threshold=gate_threshold,
    )


class Tacotron2Tests(TorchscriptConsistencyMixin):
    def _get_inputs(self, n_mels: int, n_batch: int, max_mel_specgram_length: int, max_text_length: int):
        text = torch.randint(0, 148, (n_batch, max_text_length), dtype=torch.int32, device=self.device)
        text_lengths = max_text_length * torch.ones((n_batch,), dtype=torch.int32, device=self.device)
        mel_specgram = torch.rand(
            n_batch,
            n_mels,
            max_mel_specgram_length,
            dtype=self.dtype,
            device=self.device,
        )
        mel_specgram_lengths = max_mel_specgram_length * torch.ones((n_batch,), dtype=torch.int32, device=self.device)
        return text, text_lengths, mel_specgram, mel_specgram_lengths

    @parameterized.expand(
        [
            (1,),
            (16,),
        ]
    )
    @skipIfPy310
    def test_tacotron2_torchscript_consistency(self, n_batch):
        r"""Validate the torchscript consistency of a Tacotron2."""
        n_mels = 80
        max_mel_specgram_length = 300
        max_text_length = 100

        model = _get_tacotron2_model(n_mels).to(self.device).eval()
        inputs = self._get_inputs(n_mels, n_batch, max_mel_specgram_length, max_text_length)

        self._assert_torchscript_consistency(model, inputs)

    @parameterized.expand(
        [
            (1,),
            (16,),
        ]
    )
    def test_tacotron2_output_shape(self, n_batch):
        r"""Feed tensors with specific shape to Tacotron2 and validate
        that it outputs with a tensor with expected shape.
        """
        n_mels = 80
        max_mel_specgram_length = 300
        max_text_length = 100

        model = _get_tacotron2_model(n_mels).to(self.device).eval()
        inputs = self._get_inputs(n_mels, n_batch, max_mel_specgram_length, max_text_length)
        mel_out, mel_out_postnet, gate_outputs, alignments = model(*inputs)

        assert mel_out.size() == (n_batch, n_mels, max_mel_specgram_length)
        assert mel_out_postnet.size() == (n_batch, n_mels, max_mel_specgram_length)
        assert gate_outputs.size() == (n_batch, max_mel_specgram_length)
        assert alignments.size() == (n_batch, max_mel_specgram_length, max_text_length)

    @parameterized.expand(
        [
            (1,),
            (16,),
        ]
    )
    def test_tacotron2_backward(self, n_batch):
        r"""Make sure calling the backward function on Tacotron2's outputs does
        not error out. Following:
        https://github.com/pytorch/vision/blob/23b8760374a5aaed53c6e5fc83a7e83dbe3b85df/test/test_models.py#L255
        """
        n_mels = 80
        max_mel_specgram_length = 300
        max_text_length = 100

        model = _get_tacotron2_model(n_mels).to(self.device)
        inputs = self._get_inputs(n_mels, n_batch, max_mel_specgram_length, max_text_length)
        mel_out, mel_out_postnet, gate_outputs, _ = model(*inputs)

        mel_out.sum().backward(retain_graph=True)
        mel_out_postnet.sum().backward(retain_graph=True)
        gate_outputs.sum().backward()

    def _get_inference_inputs(self, n_batch: int, max_text_length: int):
        text = torch.randint(0, 148, (n_batch, max_text_length), dtype=torch.int32, device=self.device)
        text_lengths = max_text_length * torch.ones((n_batch,), dtype=torch.int32, device=self.device)
        return text, text_lengths

    @parameterized.expand(
        [
            (1,),
            (16,),
        ]
    )
    @skipIfPy310
    def test_tacotron2_inference_torchscript_consistency(self, n_batch):
        r"""Validate the torchscript consistency of Tacotron2 inference function."""
        n_mels = 40
        max_text_length = 100
        decoder_max_step = 200  # make inference more efficient
        gate_threshold = 0.51  # if set to 0.5, the model will only run one step

        model = (
            _get_tacotron2_model(n_mels, decoder_max_step=decoder_max_step, gate_threshold=gate_threshold)
            .to(self.device)
            .eval()
        )
        inputs = self._get_inference_inputs(n_batch, max_text_length)

        model_wrapper = Tacotron2InferenceWrapper(model)

        self._assert_torchscript_consistency(model_wrapper, inputs)

    @parameterized.expand(
        [
            (1,),
            (16,),
        ]
    )
    def test_tacotron2_inference_output_shape(self, n_batch):
        r"""Feed tensors with specific shape to Tacotron2 inference function and validate
        that it outputs with a tensor with expected shape.
        """
        n_mels = 40
        max_text_length = 100
        decoder_max_step = 200  # make inference more efficient
        gate_threshold = 0.51  # if set to 0.5, the model will only run one step

        model = (
            _get_tacotron2_model(n_mels, decoder_max_step=decoder_max_step, gate_threshold=gate_threshold)
            .to(self.device)
            .eval()
        )
        inputs = self._get_inference_inputs(n_batch, max_text_length)

        mel_out, mel_specgram_lengths, alignments = model.infer(*inputs)

        # There is no guarantee on exactly what max_mel_specgram_length should be
        # We only know that it should be smaller than model.decoder.decoder_max_step
        assert len(mel_out.size()) == 3
        assert mel_out.size()[:2] == (
            n_batch,
            n_mels,
        )
        assert mel_out.size()[2] == mel_specgram_lengths.max().item()
        assert len(mel_specgram_lengths.size()) == 1
        assert mel_specgram_lengths.size()[0] == n_batch
        assert mel_specgram_lengths.max().item() <= model.decoder.decoder_max_step
        assert len(alignments.size()) == 3
        assert alignments.size()[0] == n_batch
        assert alignments.size()[1] == mel_specgram_lengths.max().item()
        assert alignments.size()[2] == max_text_length