test_jit.py 5.76 KB
Newer Older
1
from __future__ import absolute_import, division, print_function, unicode_literals
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
import torch
import torchaudio.functional as F
import torchaudio.transforms as transforms
import unittest

RUN_CUDA = torch.cuda.is_available()
print('Run test with cuda:', RUN_CUDA)


class Test_JIT(unittest.TestCase):
    def _get_script_module(self, f, *args):
        # takes a transform function `f` and wraps it in a script module
        class MyModule(torch.jit.ScriptModule):
            def __init__(self):
                super(MyModule, self).__init__()
                self.module = f(*args)
                self.module.eval()

            @torch.jit.script_method
            def forward(self, tensor):
                return self.module(tensor)

        return MyModule()

    def _test_script_module(self, tensor, f, *args):
        # tests a script module that wraps a transform function `f` by feeding
        # the tensor into the forward function
        jit_out = self._get_script_module(f, *args).cuda()(tensor)
        py_out = f(*args).cuda()(tensor)

        self.assertTrue(torch.allclose(jit_out, py_out))

    def test_torchscript_spectrogram(self):
        @torch.jit.script
        def jit_method(sig, pad, window, n_fft, hop, ws, power, normalize):
            # type: (Tensor, int, Tensor, int, int, int, int, bool) -> Tensor
            return F.spectrogram(sig, pad, window, n_fft, hop, ws, power, normalize)

        tensor = torch.rand((1, 1000))
        n_fft = 400
        ws = 400
        hop = 200
        pad = 0
        window = torch.hann_window(ws)
        power = 2
        normalize = False

        jit_out = jit_method(tensor, pad, window, n_fft, hop, ws, power, normalize)
        py_out = F.spectrogram(tensor, pad, window, n_fft, hop, ws, power, normalize)

        self.assertTrue(torch.allclose(jit_out, py_out))

    @unittest.skipIf(not RUN_CUDA, "no CUDA")
    def test_scriptmodule_Spectrogram(self):
        tensor = torch.rand((1, 1000), device="cuda")

        self._test_script_module(tensor, transforms.Spectrogram)

    def test_torchscript_create_fb_matrix(self):
        @torch.jit.script
engineerchuan's avatar
engineerchuan committed
62
63
64
        def jit_method(n_stft, f_min, f_max, n_mels, sample_rate):
            # type: (int, float, float, int, int) -> Tensor
            return F.create_fb_matrix(n_stft, f_min, f_max, n_mels, sample_rate)
65
66
67
68
69

        n_stft = 100
        f_min = 0.
        f_max = 20.
        n_mels = 10
engineerchuan's avatar
engineerchuan committed
70
        sample_rate = 16000
71

engineerchuan's avatar
engineerchuan committed
72
73
        jit_out = jit_method(n_stft, f_min, f_max, n_mels, sample_rate)
        py_out = F.create_fb_matrix(n_stft, f_min, f_max, n_mels, sample_rate)
74
75
76
77
78
79
80
81
82

        self.assertTrue(torch.allclose(jit_out, py_out))

    @unittest.skipIf(not RUN_CUDA, "no CUDA")
    def test_scriptmodule_MelScale(self):
        spec_f = torch.rand((1, 6, 201), device="cuda")

        self._test_script_module(spec_f, transforms.MelScale)

83
    def test_torchscript_amplitude_to_DB(self):
84
85
86
        @torch.jit.script
        def jit_method(spec, multiplier, amin, db_multiplier, top_db):
            # type: (Tensor, float, float, float, Optional[float]) -> Tensor
87
            return F.amplitude_to_DB(spec, multiplier, amin, db_multiplier, top_db)
88

89
        spec = torch.rand((6, 201))
90
91
92
93
94
95
        multiplier = 10.
        amin = 1e-10
        db_multiplier = 0.
        top_db = 80.

        jit_out = jit_method(spec, multiplier, amin, db_multiplier, top_db)
96
        py_out = F.amplitude_to_DB(spec, multiplier, amin, db_multiplier, top_db)
97
98
99
100

        self.assertTrue(torch.allclose(jit_out, py_out))

    @unittest.skipIf(not RUN_CUDA, "no CUDA")
101
    def test_scriptmodule_AmplitudeToDB(self):
102
        spec = torch.rand((6, 201), device="cuda")
103

104
        self._test_script_module(spec, transforms.AmplitudeToDB)
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

    def test_torchscript_create_dct(self):
        @torch.jit.script
        def jit_method(n_mfcc, n_mels, norm):
            # type: (int, int, Optional[str]) -> Tensor
            return F.create_dct(n_mfcc, n_mels, norm)

        n_mfcc = 40
        n_mels = 128
        norm = 'ortho'

        jit_out = jit_method(n_mfcc, n_mels, norm)
        py_out = F.create_dct(n_mfcc, n_mels, norm)

        self.assertTrue(torch.allclose(jit_out, py_out))

    @unittest.skipIf(not RUN_CUDA, "no CUDA")
    def test_scriptmodule_MFCC(self):
        tensor = torch.rand((1, 1000), device="cuda")

        self._test_script_module(tensor, transforms.MFCC)

    @unittest.skipIf(not RUN_CUDA, "no CUDA")
    def test_scriptmodule_MelSpectrogram(self):
        tensor = torch.rand((1, 1000), device="cuda")

        self._test_script_module(tensor, transforms.MelSpectrogram)

    def test_torchscript_mu_law_encoding(self):
        @torch.jit.script
        def jit_method(tensor, qc):
            # type: (Tensor, int) -> Tensor
            return F.mu_law_encoding(tensor, qc)

139
        tensor = torch.rand((1, 10))
140
141
142
143
144
145
146
147
148
        qc = 256

        jit_out = jit_method(tensor, qc)
        py_out = F.mu_law_encoding(tensor, qc)

        self.assertTrue(torch.allclose(jit_out, py_out))

    @unittest.skipIf(not RUN_CUDA, "no CUDA")
    def test_scriptmodule_MuLawEncoding(self):
149
        tensor = torch.rand((1, 10), device="cuda")
150
151
152

        self._test_script_module(tensor, transforms.MuLawEncoding)

153
    def test_torchscript_mu_law_decoding(self):
154
155
156
        @torch.jit.script
        def jit_method(tensor, qc):
            # type: (Tensor, int) -> Tensor
157
            return F.mu_law_decoding(tensor, qc)
158

159
        tensor = torch.rand((1, 10))
160
161
162
        qc = 256

        jit_out = jit_method(tensor, qc)
163
        py_out = F.mu_law_decoding(tensor, qc)
164
165
166
167

        self.assertTrue(torch.allclose(jit_out, py_out))

    @unittest.skipIf(not RUN_CUDA, "no CUDA")
168
    def test_scriptmodule_MuLawDecoding(self):
169
        tensor = torch.rand((1, 10), device="cuda")
170

171
        self._test_script_module(tensor, transforms.MuLawDecoding)
172
173
174
175


if __name__ == '__main__':
    unittest.main()