|
| 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 | + |
| 14 | + temp_dir_ = None |
| 15 | + |
| 16 | + @classmethod |
| 17 | + def get_base_temp_dir(cls): |
| 18 | + # If TORCHAUDIO_TEST_TEMP_DIR is set, use it instead of temporary directory. |
| 19 | + # this is handy for debugging. |
| 20 | + key = "TORCHAUDIO_TEST_TEMP_DIR" |
| 21 | + if key in os.environ: |
| 22 | + return os.environ[key] |
| 23 | + if cls.temp_dir_ is None: |
| 24 | + cls.temp_dir_ = tempfile.TemporaryDirectory() |
| 25 | + return cls.temp_dir_.name |
| 26 | + |
| 27 | + @classmethod |
| 28 | + def tearDownClass(cls): |
| 29 | + super().tearDownClass() |
| 30 | + if cls.temp_dir_ is not None: |
| 31 | + cls.temp_dir_.cleanup() |
| 32 | + cls.temp_dir_ = None |
| 33 | + |
| 34 | + def get_temp_path(self, *paths): |
| 35 | + temp_dir = os.path.join(self.get_base_temp_dir(), self.id()) |
| 36 | + path = os.path.join(temp_dir, *paths) |
| 37 | + os.makedirs(os.path.dirname(path), exist_ok=True) |
| 38 | + return path |
| 39 | + |
| 40 | + |
| 41 | +class Tacotron2LossInputMixin(TempDirMixin): |
| 42 | + |
| 43 | + def _get_inputs(self, n_mel=80, n_batch=16, max_mel_specgram_length=300): |
| 44 | + mel_specgram = torch.rand( |
| 45 | + n_batch, n_mel, max_mel_specgram_length, dtype=self.dtype, device=self.device |
| 46 | + ) |
| 47 | + mel_specgram_postnet = torch.rand( |
| 48 | + n_batch, n_mel, max_mel_specgram_length, dtype=self.dtype, device=self.device |
| 49 | + ) |
| 50 | + gate_out = torch.rand(n_batch, dtype=self.dtype, device=self.device) |
| 51 | + truth_mel_specgram = torch.rand( |
| 52 | + n_batch, n_mel, max_mel_specgram_length, dtype=self.dtype, device=self.device |
| 53 | + ) |
| 54 | + truth_gate_out = torch.rand(n_batch, dtype=self.dtype, device=self.device) |
| 55 | + |
| 56 | + truth_mel_specgram.requires_grad = False |
| 57 | + truth_gate_out.requires_grad = False |
| 58 | + |
| 59 | + return ( |
| 60 | + mel_specgram, |
| 61 | + mel_specgram_postnet, |
| 62 | + gate_out, |
| 63 | + truth_mel_specgram, |
| 64 | + truth_gate_out, |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +class Tacotron2LossShapeTests(Tacotron2LossInputMixin): |
| 69 | + |
| 70 | + def test_tacotron2_loss_shape(self): |
| 71 | + f"""Validate the output shape of Tacotron2Loss.""" |
| 72 | + n_batch = 16 |
| 73 | + |
| 74 | + ( |
| 75 | + mel_specgram, |
| 76 | + mel_specgram_postnet, |
| 77 | + gate_out, |
| 78 | + truth_mel_specgram, |
| 79 | + truth_gate_out, |
| 80 | + ) = self._get_inputs(n_batch=n_batch) |
| 81 | + |
| 82 | + mel_loss, mel_postnet_loss, gate_loss = Tacotron2Loss()( |
| 83 | + (mel_specgram, mel_specgram_postnet, gate_out), |
| 84 | + (truth_mel_specgram, truth_gate_out) |
| 85 | + ) |
| 86 | + |
| 87 | + self.assertEqual(mel_loss.size(), torch.Size([])) |
| 88 | + self.assertEqual(mel_postnet_loss.size(), torch.Size([])) |
| 89 | + self.assertEqual(gate_loss.size(), torch.Size([])) |
| 90 | + |
| 91 | + |
| 92 | +class Tacotron2LossTorchscriptTests(Tacotron2LossInputMixin): |
| 93 | + |
| 94 | + def _assert_torchscript_consistency(self, fn, tensors): |
| 95 | + path = self.get_temp_path("func.zip") |
| 96 | + torch.jit.script(fn).save(path) |
| 97 | + ts_func = torch.jit.load(path) |
| 98 | + |
| 99 | + output = fn(tensors[:3], tensors[3:]) |
| 100 | + ts_output = ts_func(tensors[:3], tensors[3:]) |
| 101 | + |
| 102 | + self.assertEqual(ts_output, output) |
| 103 | + |
| 104 | + def test_tacotron2_loss_torchscript_consistency(self): |
| 105 | + f"""Validate the torchscript consistency of Tacotron2Loss.""" |
| 106 | + |
| 107 | + loss_fn = Tacotron2Loss() |
| 108 | + self._assert_torchscript_consistency(loss_fn, self._get_inputs()) |
| 109 | + |
| 110 | + |
| 111 | +class Tacotron2LossGradcheckTests(Tacotron2LossInputMixin): |
| 112 | + |
| 113 | + def test_tacotron2_loss_gradcheck(self): |
| 114 | + f"""Performing gradient check on Tacotron2Loss.""" |
| 115 | + ( |
| 116 | + mel_specgram, |
| 117 | + mel_specgram_postnet, |
| 118 | + gate_out, |
| 119 | + truth_mel_specgram, |
| 120 | + truth_gate_out, |
| 121 | + ) = self._get_inputs() |
| 122 | + |
| 123 | + mel_specgram.requires_grad_(True) |
| 124 | + mel_specgram_postnet.requires_grad_(True) |
| 125 | + gate_out.requires_grad_(True) |
| 126 | + |
| 127 | + def _fn(mel_specgram, mel_specgram_postnet, gate_out, truth_mel_specgram, truth_gate_out): |
| 128 | + loss_fn = Tacotron2Loss() |
| 129 | + return loss_fn( |
| 130 | + (mel_specgram, mel_specgram_postnet, gate_out), |
| 131 | + (truth_mel_specgram, truth_gate_out), |
| 132 | + ) |
| 133 | + |
| 134 | + gradcheck( |
| 135 | + _fn, |
| 136 | + (mel_specgram, mel_specgram_postnet, gate_out, truth_mel_specgram, truth_gate_out), |
| 137 | + fast_mode=True, |
| 138 | + ) |
| 139 | + gradgradcheck( |
| 140 | + _fn, |
| 141 | + (mel_specgram, mel_specgram_postnet, gate_out, truth_mel_specgram, truth_gate_out), |
| 142 | + fast_mode=True, |
| 143 | + ) |
0 commit comments