Skip to content

Avoid repeated upsampling of spectrogram #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/diffwave/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def predict(spectrogram=None, model_dir=None, params=None, device=torch.device('
if len(spectrogram.shape) == 2:# Expand rank 2 tensors by adding a batch dimension.
spectrogram = spectrogram.unsqueeze(0)
spectrogram = spectrogram.to(device)
spectrogram = model.spectrogram_upsampler(spectrogram)
audio = torch.randn(spectrogram.shape[0], model.params.hop_samples * spectrogram.shape[-1], device=device)
else:
audio = torch.randn(1, params.audio_len, device=device)
Expand All @@ -80,7 +81,7 @@ def predict(spectrogram=None, model_dir=None, params=None, device=torch.device('
for n in range(len(alpha) - 1, -1, -1):
c1 = 1 / alpha[n]**0.5
c2 = beta[n] / (1 - alpha_cum[n])**0.5
audio = c1 * (audio - c2 * model(audio, torch.tensor([T[n]], device=audio.device), spectrogram).squeeze(1))
audio = c1 * (audio - c2 * model(audio, torch.tensor([T[n]], device=audio.device), spectrogram, infer=True).squeeze(1))
if n > 0:
noise = torch.randn_like(audio)
sigma = ((1.0 - alpha_cum[n-1]) / (1.0 - alpha_cum[n]) * beta[n])**0.5
Expand Down
7 changes: 4 additions & 3 deletions src/diffwave/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,17 @@ def __init__(self, params):
self.output_projection = Conv1d(params.residual_channels, 1, 1)
nn.init.zeros_(self.output_projection.weight)

def forward(self, audio, diffusion_step, spectrogram=None):
def forward(self, audio, diffusion_step, spectrogram=None, infer=False):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent change - thanks for sending a PR.

Instead of adding a boolean flag, can we have both the training and inference code run the upsampler before passing it in?

assert (spectrogram is None and self.spectrogram_upsampler is None) or \
(spectrogram is not None and self.spectrogram_upsampler is not None)
x = audio.unsqueeze(1)
x = self.input_projection(x)
x = F.relu(x)

diffusion_step = self.diffusion_embedding(diffusion_step)
if self.spectrogram_upsampler: # use conditional model
spectrogram = self.spectrogram_upsampler(spectrogram)
if not infer:
if self.spectrogram_upsampler: # use conditional model
spectrogram = self.spectrogram_upsampler(spectrogram)

skip = None
for layer in self.residual_layers:
Expand Down