|
| 1 | +import os |
| 2 | +import unittest |
| 3 | +import tempfile |
| 4 | + |
| 5 | +import torch |
| 6 | +from torch.autograd import gradcheck, gradgradcheck |
| 7 | + |
| 8 | +from loss_function import Tacotron2Loss |
| 9 | + |
| 10 | + |
| 11 | +class TempDirMixin: |
| 12 | + """Mixin to provide easy access to temp dir""" |
| 13 | + temp_dir_ = None |
| 14 | + |
| 15 | + @classmethod |
| 16 | + def get_base_temp_dir(cls): |
| 17 | + # If TORCHAUDIO_TEST_TEMP_DIR is set, use it instead of temporary directory. |
| 18 | + # this is handy for debugging. |
| 19 | + key = 'TORCHAUDIO_TEST_TEMP_DIR' |
| 20 | + if key in os.environ: |
| 21 | + return os.environ[key] |
| 22 | + if cls.temp_dir_ is None: |
| 23 | + cls.temp_dir_ = tempfile.TemporaryDirectory() |
| 24 | + return cls.temp_dir_.name |
| 25 | + |
| 26 | + @classmethod |
| 27 | + def tearDownClass(cls): |
| 28 | + super().tearDownClass() |
| 29 | + if cls.temp_dir_ is not None: |
| 30 | + cls.temp_dir_.cleanup() |
| 31 | + cls.temp_dir_ = None |
| 32 | + |
| 33 | + def get_temp_path(self, *paths): |
| 34 | + temp_dir = os.path.join(self.get_base_temp_dir(), self.id()) |
| 35 | + path = os.path.join(temp_dir, *paths) |
| 36 | + os.makedirs(os.path.dirname(path), exist_ok=True) |
| 37 | + return path |
| 38 | + |
| 39 | + |
| 40 | +class Tacotron2LossTest(unittest.TestCase, TempDirMixin): |
| 41 | + |
| 42 | + dtype = torch.float64 |
| 43 | + device = "cpu" |
| 44 | + |
| 45 | + def _assert_torchscript_consistency(self, fn, tensors): |
| 46 | + path = self.get_temp_path('func.zip') |
| 47 | + torch.jit.script(fn).save(path) |
| 48 | + ts_func = torch.jit.load(path) |
| 49 | + |
| 50 | + torch.random.manual_seed(40) |
| 51 | + output = fn(*tensors) |
| 52 | + |
| 53 | + torch.random.manual_seed(40) |
| 54 | + ts_output = ts_func(*tensors) |
| 55 | + |
| 56 | + self.assertEqual(ts_output, output) |
| 57 | + |
| 58 | + def _get_inputs(self): |
| 59 | + n_mel, n_batch, max_mel_specgram_length = 10, 8, 20 |
| 60 | + mel_specgram = torch.rand(n_batch, n_mel, max_mel_specgram_length, dtype=self.dtype, device=self.device) |
| 61 | + mel_specgram_postnet = torch.rand(n_batch, n_mel, max_mel_specgram_length, dtype=self.dtype, device=self.device) |
| 62 | + gate_out = torch.rand(n_batch, dtype=self.dtype, device=self.device) |
| 63 | + truth_mel_specgram = torch.rand(n_batch, n_mel, max_mel_specgram_length, dtype=self.dtype, device=self.device) |
| 64 | + truth_gate_out = torch.rand(n_batch, dtype=self.dtype, device=self.device) |
| 65 | + |
| 66 | + return mel_specgram, mel_specgram_postnet, gate_out, truth_mel_specgram, truth_gate_out |
| 67 | + |
| 68 | + def test_torchscript_consistency(self): |
| 69 | + f"""Validate the torchscript consistency of Tacotron2Loss.""" |
| 70 | + |
| 71 | + def _fn(mel_specgram, mel_specgram_postnet, gate_out, truth_mel_specgram, truth_gate_out): |
| 72 | + loss_fn = Tacotron2Loss() |
| 73 | + return loss_fn((mel_specgram, mel_specgram_postnet, gate_out), (truth_mel_specgram, truth_gate_out)) |
| 74 | + |
| 75 | + self._assert_torchscript_consistency(_fn, self._get_inputs()) |
| 76 | + |
| 77 | + def test_gradcheck(self): |
| 78 | + f"""Performing gradient check on Tacotron2Loss.""" |
| 79 | + |
| 80 | + mel_specgram, mel_specgram_postnet, gate_out, truth_mel_specgram, truth_gate_out = self._get_inputs() |
| 81 | + |
| 82 | + mel_specgram.requires_grad_(True) |
| 83 | + mel_specgram_postnet.requires_grad_(True) |
| 84 | + gate_out.requires_grad_(True) |
| 85 | + |
| 86 | + def _fn(mel_specgram, mel_specgram_postnet, gate_out, truth_mel_specgram, truth_gate_out): |
| 87 | + loss_fn = Tacotron2Loss() |
| 88 | + return loss_fn((mel_specgram, mel_specgram_postnet, gate_out), (truth_mel_specgram, truth_gate_out)) |
| 89 | + |
| 90 | + gradcheck(_fn, (mel_specgram, mel_specgram_postnet, gate_out, truth_mel_specgram, truth_gate_out)) |
| 91 | + gradgradcheck(_fn, (mel_specgram, mel_specgram_postnet, gate_out, truth_mel_specgram, truth_gate_out)) |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + unittest.main() |
0 commit comments