revert flat diffusion back...

This commit is contained in:
James Betker 2022-05-22 23:10:58 -06:00
parent 8f28404645
commit 4093e38717
2 changed files with 92 additions and 101 deletions

View File

@ -1,17 +1,13 @@
import os
import random import random
import torch import torch
import torch.nn as nn import torch.nn as nn
import torch.nn.functional as F import torch.nn.functional as F
import torchvision
from torch import autocast from torch import autocast
from models.arch_util import ResBlock from models.arch_util import ResBlock
from models.diffusion.nn import timestep_embedding, normalization, zero_module, conv_nd, linear from models.diffusion.nn import timestep_embedding, normalization, zero_module, conv_nd, linear
from models.diffusion.unet_diffusion import AttentionBlock, TimestepEmbedSequential, TimestepBlock from models.diffusion.unet_diffusion import AttentionBlock, TimestepEmbedSequential, TimestepBlock
from scripts.audio.gen.use_mel2vec_codes import collapse_codegroups
from trainer.injectors.audio_injectors import normalize_mel
from trainer.networks import register_model from trainer.networks import register_model
from utils.util import checkpoint from utils.util import checkpoint
@ -111,20 +107,6 @@ class DiffusionLayer(TimestepBlock):
return self.attn(y) return self.attn(y)
class NonTimestepResidualAttentionNorm(nn.Module):
def __init__(self, model_channels, dropout):
super().__init__()
self.resblk = ResBlock(dims=1, channels=model_channels, dropout=dropout)
self.attn = AttentionBlock(model_channels, num_heads=model_channels//64, relative_pos_embeddings=True)
self.norm = nn.GroupNorm(num_groups=8, num_channels=model_channels)
def forward(self, x):
h = self.resblk(x)
h = self.norm(h)
h = self.attn(h)
return h
class FlatDiffusion(nn.Module): class FlatDiffusion(nn.Module):
def __init__( def __init__(
self, self,
@ -141,6 +123,7 @@ class FlatDiffusion(nn.Module):
# Parameters for regularization. # Parameters for regularization.
layer_drop=.1, layer_drop=.1,
unconditioned_percentage=.1, # This implements a mechanism similar to what is used in classifier-free training. unconditioned_percentage=.1, # This implements a mechanism similar to what is used in classifier-free training.
train_mel_head=False,
): ):
super().__init__() super().__init__()
@ -154,7 +137,6 @@ class FlatDiffusion(nn.Module):
self.layer_drop = layer_drop self.layer_drop = layer_drop
self.inp_block = conv_nd(1, in_channels, model_channels, 3, 1, 1) self.inp_block = conv_nd(1, in_channels, model_channels, 3, 1, 1)
# TODO: I'd really like to see if this could be ablated. It seems useless to me: why can't the embedding just learn this mapping directly?
self.time_embed = nn.Sequential( self.time_embed = nn.Sequential(
linear(model_channels, model_channels), linear(model_channels, model_channels),
nn.SiLU(), nn.SiLU(),
@ -168,23 +150,32 @@ class FlatDiffusion(nn.Module):
self.embeddings = nn.ModuleList([nn.Embedding(in_vectors, model_channels//in_groups) for _ in range(in_groups)]) self.embeddings = nn.ModuleList([nn.Embedding(in_vectors, model_channels//in_groups) for _ in range(in_groups)])
self.latent_conditioner = nn.Sequential( self.latent_conditioner = nn.Sequential(
nn.Conv1d(in_latent_channels, model_channels, 3, padding=1), nn.Conv1d(in_latent_channels, model_channels, 3, padding=1),
NonTimestepResidualAttentionNorm(model_channels, dropout), AttentionBlock(model_channels, num_heads, relative_pos_embeddings=True),
NonTimestepResidualAttentionNorm(model_channels, dropout), AttentionBlock(model_channels, num_heads, relative_pos_embeddings=True),
nn.Conv1d(model_channels, model_channels, 3, padding=1), AttentionBlock(model_channels, num_heads, relative_pos_embeddings=True),
AttentionBlock(model_channels, num_heads, relative_pos_embeddings=True),
) )
self.latent_fade = nn.Parameter(torch.zeros(1,model_channels,1))
self.code_converter = nn.Sequential( self.code_converter = nn.Sequential(
NonTimestepResidualAttentionNorm(model_channels, dropout), ResBlock(dims=1, channels=model_channels, dropout=dropout),
NonTimestepResidualAttentionNorm(model_channels, dropout), AttentionBlock(model_channels, num_heads, relative_pos_embeddings=True),
NonTimestepResidualAttentionNorm(model_channels, dropout), ResBlock(dims=1, channels=model_channels, dropout=dropout),
nn.Conv1d(model_channels, model_channels, 3, padding=1), AttentionBlock(model_channels, num_heads, relative_pos_embeddings=True),
ResBlock(dims=1, channels=model_channels, dropout=dropout),
) )
self.conditioning_embedder = nn.Sequential(nn.Conv1d(in_channels, model_channels // 2, 3, padding=1, stride=2), self.code_norm = normalization(model_channels)
nn.Conv1d(model_channels//2, model_channels,3,padding=1,stride=2), self.contextual_embedder = nn.Sequential(nn.Conv1d(in_channels,model_channels,3,padding=1,stride=2),
NonTimestepResidualAttentionNorm(model_channels, dropout), nn.Conv1d(model_channels, model_channels*2,3,padding=1,stride=2),
NonTimestepResidualAttentionNorm(model_channels, dropout), AttentionBlock(model_channels*2, num_heads, relative_pos_embeddings=True, do_checkpoint=False),
NonTimestepResidualAttentionNorm(model_channels, dropout)) AttentionBlock(model_channels*2, num_heads, relative_pos_embeddings=True, do_checkpoint=False),
AttentionBlock(model_channels*2, num_heads, relative_pos_embeddings=True, do_checkpoint=False),
AttentionBlock(model_channels*2, num_heads, relative_pos_embeddings=True, do_checkpoint=False),
AttentionBlock(model_channels*2, num_heads, relative_pos_embeddings=True, do_checkpoint=False))
self.unconditioned_embedding = nn.Parameter(torch.randn(1,model_channels,1)) self.unconditioned_embedding = nn.Parameter(torch.randn(1,model_channels,1))
self.conditioning_timestep_integrator = TimestepEmbedSequential(
DiffusionLayer(model_channels, dropout, num_heads),
DiffusionLayer(model_channels, dropout, num_heads),
DiffusionLayer(model_channels, dropout, num_heads),
)
self.integrating_conv = nn.Conv1d(model_channels*2, model_channels, kernel_size=1) self.integrating_conv = nn.Conv1d(model_channels*2, model_channels, kernel_size=1)
self.mel_head = nn.Conv1d(model_channels, in_channels, kernel_size=3, padding=1) self.mel_head = nn.Conv1d(model_channels, in_channels, kernel_size=3, padding=1)
@ -197,97 +188,97 @@ class FlatDiffusion(nn.Module):
zero_module(conv_nd(1, model_channels, out_channels, 3, padding=1)), zero_module(conv_nd(1, model_channels, out_channels, 3, padding=1)),
) )
self.debug_codes = {} if train_mel_head:
for m in [self.conditioning_timestep_integrator, self.integrating_conv, self.layers,
self.out]:
for p in m.parameters():
p.requires_grad = False
p.DO_NOT_TRAIN = True
def get_grad_norm_parameter_groups(self): def get_grad_norm_parameter_groups(self):
groups = { groups = {
'contextual_embedder': list(self.conditioning_embedder.parameters()), 'minicoder': list(self.contextual_embedder.parameters()),
'layers': list(self.layers.parameters()) + list(self.integrating_conv.parameters()) + list(self.inp_block.parameters()), 'layers': list(self.layers.parameters()),
'code_converters': list(self.embeddings.parameters()) + list(self.code_converter.parameters()) + list(self.latent_conditioner.parameters()), 'code_converters': list(self.embeddings.parameters()) + list(self.code_converter.parameters()) + list(self.latent_conditioner.parameters()),
'timestep_integrator': list(self.conditioning_timestep_integrator.parameters()) + list(self.integrating_conv.parameters()),
'time_embed': list(self.time_embed.parameters()), 'time_embed': list(self.time_embed.parameters()),
} }
return groups return groups
def timestep_independent(self, codes, conditioning_input, expected_seq_len, prenet_latent=None, return_code_pred=False): def timestep_independent(self, aligned_conditioning, conditioning_input, expected_seq_len, return_code_pred):
cond_emb = self.conditioning_embedder(conditioning_input)[:, :, 0] # Shuffle aligned_latent to BxCxS format
if is_latent(aligned_conditioning):
aligned_conditioning = aligned_conditioning.permute(0, 2, 1)
# Shuffle prenet_latent to BxCxS format # Note: this block does not need to repeated on inference, since it is not timestep-dependent or x-dependent.
if prenet_latent is not None: speech_conditioning_input = conditioning_input.unsqueeze(1) if len(
prenet_latent = prenet_latent.permute(0, 2, 1) conditioning_input.shape) == 3 else conditioning_input
conds = []
code_emb = [embedding(codes[:, :, i]) for i, embedding in enumerate(self.embeddings)] for j in range(speech_conditioning_input.shape[1]):
code_emb = torch.cat(code_emb, dim=-1).permute(0,2,1) conds.append(self.contextual_embedder(speech_conditioning_input[:, j]))
if prenet_latent is not None: conds = torch.cat(conds, dim=-1)
latent_conditioning = self.latent_conditioner(prenet_latent) cond_emb = conds.mean(dim=-1)
code_emb = code_emb + latent_conditioning * self.latent_fade cond_scale, cond_shift = torch.chunk(cond_emb, 2, dim=1)
if is_latent(aligned_conditioning):
code_emb = self.latent_conditioner(aligned_conditioning)
else:
code_emb = [embedding(aligned_conditioning[:, :, i]) for i, embedding in enumerate(self.embeddings)]
code_emb = torch.cat(code_emb, dim=-1).permute(0,2,1)
unconditioned_batches = torch.zeros((code_emb.shape[0], 1, 1), device=code_emb.device) unconditioned_batches = torch.zeros((code_emb.shape[0], 1, 1), device=code_emb.device)
# Mask out the conditioning branch for whole batch elements, implementing something similar to classifier-free guidance. # Mask out the conditioning branch for whole batch elements, implementing something similar to classifier-free guidance.
if self.training and self.unconditioned_percentage > 0: if self.training and self.unconditioned_percentage > 0:
unconditioned_batches = torch.rand((code_emb.shape[0], 1, 1), unconditioned_batches = torch.rand((code_emb.shape[0], 1, 1),
device=code_emb.device) < self.unconditioned_percentage device=code_emb.device) < self.unconditioned_percentage
code_emb = torch.where(unconditioned_batches, self.unconditioned_embedding.repeat(codes.shape[0], 1, 1), code_emb = torch.where(unconditioned_batches, self.unconditioned_embedding.repeat(aligned_conditioning.shape[0], 1, 1),
code_emb) code_emb)
code_emb = self.code_converter(code_emb)
expanded_code_emb = F.interpolate(code_emb, size=expected_seq_len, mode='nearest') expanded_code_emb = F.interpolate(code_emb, size=expected_seq_len, mode='nearest')
expanded_code_emb = self.code_converter(expanded_code_emb)
expanded_code_emb = self.code_norm(expanded_code_emb) * (1 + cond_scale.unsqueeze(-1)) + cond_shift.unsqueeze(-1)
if not return_code_pred: if not return_code_pred:
return expanded_code_emb, cond_emb return expanded_code_emb
else: else:
# Perform the mel_head computation on the pre-exanded code embeddings, then interpolate it separately. mel_pred = self.mel_head(expanded_code_emb)
mel_pred = self.mel_head(code_emb) # Multiply mel_pred by !unconditioned_branches, which drops the gradient on unconditioned branches. This is because we don't want that gradient being used to train parameters through the codes_embedder as it unbalances contributions to that network from the MSE loss.
mel_pred = F.interpolate(mel_pred, size=expected_seq_len, mode='nearest')
# Multiply mel_pred by !unconditioned_branches, which drops the gradient on unconditioned branches.
# This is because we don't want that gradient being used to train parameters through the codes_embedder as
# it unbalances contributions to that network from the MSE loss.
mel_pred = mel_pred * unconditioned_batches.logical_not() mel_pred = mel_pred * unconditioned_batches.logical_not()
return expanded_code_emb, cond_emb, mel_pred return expanded_code_emb, mel_pred
def forward(self, x, timesteps, def forward(self, x, timesteps, aligned_conditioning=None, conditioning_input=None, precomputed_aligned_embeddings=None, conditioning_free=False, return_code_pred=False):
codes=None, conditioning_input=None, prenet_latent=None,
precomputed_code_embeddings=None, precomputed_cond_embeddings=None,
conditioning_free=False, return_code_pred=False):
""" """
Apply the model to an input batch. Apply the model to an input batch.
There are two ways to call this method:
1) Specify codes, conditioning_input and optionally prenet_latent
2) Specify precomputed_code_embeddings and precomputed_cond_embeddings, retrieved by calling timestep_independent yourself.
:param x: an [N x C x ...] Tensor of inputs. :param x: an [N x C x ...] Tensor of inputs.
:param timesteps: a 1-D batch of timesteps. :param timesteps: a 1-D batch of timesteps.
:param codes: an aligned latent or sequence of tokens providing useful data about the sample to be produced. :param aligned_conditioning: an aligned latent or sequence of tokens providing useful data about the sample to be produced.
:param conditioning_input: a full-resolution audio clip that is used as a reference to the style you want decoded. :param conditioning_input: a full-resolution audio clip that is used as a reference to the style you want decoded.
:param prenet_latent: optional latent vector aligned with codes derived from a prior network. :param precomputed_aligned_embeddings: Embeddings returned from self.timestep_independent()
:param precomputed_code_embeddings: Code embeddings returned from self.timestep_independent()
:param precomputed_cond_embeddings: Conditional embeddings returned from self.timestep_independent()
:param conditioning_free: When set, all conditioning inputs (including tokens and conditioning_input) will not be considered. :param conditioning_free: When set, all conditioning inputs (including tokens and conditioning_input) will not be considered.
:return: an [N x C x ...] Tensor of outputs. :return: an [N x C x ...] Tensor of outputs.
""" """
if precomputed_code_embeddings is not None: assert precomputed_aligned_embeddings is not None or (aligned_conditioning is not None and conditioning_input is not None)
assert precomputed_cond_embeddings is not None, "Must specify both precomputed embeddings if one is specified" assert not (return_code_pred and precomputed_aligned_embeddings is not None) # These two are mutually exclusive.
assert codes is None and conditioning_input is None and prenet_latent is None, "Do not provide precomputed embeddings and the other parameters. It is unclear what you want me to do here."
assert not (return_code_pred and precomputed_code_embeddings is not None), "I cannot compute a code_pred output for you."
unused_params = [] unused_params = []
if not return_code_pred:
unused_params.extend(list(self.mel_head.parameters()))
if conditioning_free: if conditioning_free:
code_emb = self.unconditioned_embedding.repeat(x.shape[0], 1, x.shape[-1]) code_emb = self.unconditioned_embedding.repeat(x.shape[0], 1, x.shape[-1])
unused_params.extend(list(self.code_converter.parameters()) + list(self.code_embedding.parameters())) unused_params.extend(list(self.code_converter.parameters()) + list(self.code_embedding.parameters()))
unused_params.extend(list(self.latent_conditioner.parameters())) unused_params.extend(list(self.latent_conditioner.parameters()))
else: else:
if precomputed_code_embeddings is not None: if precomputed_aligned_embeddings is not None:
code_emb = precomputed_code_embeddings code_emb = precomputed_aligned_embeddings
cond_emb = precomputed_cond_embeddings
else: else:
code_emb, cond_emb, mel_pred = self.timestep_independent(codes, conditioning_input, x.shape[-1], prenet_latent, True) code_emb, mel_pred = self.timestep_independent(aligned_conditioning, conditioning_input, x.shape[-1], True)
if prenet_latent is None: if is_latent(aligned_conditioning):
unused_params.extend(list(self.latent_conditioner.parameters()) + [self.latent_fade]) unused_params.extend(list(self.code_converter.parameters()) + list(self.code_embedding.parameters()))
else:
unused_params.extend(list(self.latent_conditioner.parameters()))
unused_params.append(self.unconditioned_embedding) unused_params.append(self.unconditioned_embedding)
blk_emb = self.time_embed(timestep_embedding(timesteps, self.model_channels)) + cond_emb time_emb = self.time_embed(timestep_embedding(timesteps, self.model_channels))
code_emb = self.conditioning_timestep_integrator(code_emb, time_emb)
x = self.inp_block(x) x = self.inp_block(x)
x = torch.cat([x, code_emb], dim=1) x = torch.cat([x, code_emb], dim=1)
x = self.integrating_conv(x) x = self.integrating_conv(x)
@ -298,7 +289,7 @@ class FlatDiffusion(nn.Module):
else: else:
# First and last blocks will have autocast disabled for improved precision. # First and last blocks will have autocast disabled for improved precision.
with autocast(x.device.type, enabled=self.enable_fp16 and i != 0): with autocast(x.device.type, enabled=self.enable_fp16 and i != 0):
x = lyr(x, blk_emb) x = lyr(x, time_emb)
x = x.float() x = x.float()
out = self.out(x) out = self.out(x)
@ -318,7 +309,7 @@ class FlatDiffusion(nn.Module):
conditioning_input.shape) == 3 else conditioning_input conditioning_input.shape) == 3 else conditioning_input
conds = [] conds = []
for j in range(speech_conditioning_input.shape[1]): for j in range(speech_conditioning_input.shape[1]):
conds.append(self.conditioning_embedder(speech_conditioning_input[:, j])) conds.append(self.contextual_embedder(speech_conditioning_input[:, j]))
conds = torch.cat(conds, dim=-1) conds = torch.cat(conds, dim=-1)
return conds.mean(dim=-1) return conds.mean(dim=-1)
@ -329,11 +320,13 @@ def register_flat_diffusion(opt_net, opt):
if __name__ == '__main__': if __name__ == '__main__':
clip = torch.randn(2, 256, 400) clip = torch.randn(2, 256, 400)
aligned_latent = torch.randn(2,100,512) aligned_latent = torch.randn(2,388,512)
aligned_sequence = torch.randint(0,8,(2,100,8)) aligned_sequence = torch.randint(0,8,(2,100,8))
cond = torch.randn(2, 256, 400) cond = torch.randn(2, 256, 400)
ts = torch.LongTensor([600, 600]) ts = torch.LongTensor([600, 600])
model = FlatDiffusion(512, layer_drop=.3, unconditioned_percentage=.5) model = FlatDiffusion(512, layer_drop=.3, unconditioned_percentage=.5, train_mel_head=True)
# Test with latent aligned conditioning
#o = model(clip, ts, aligned_latent, cond)
# Test with sequence aligned conditioning
o = model(clip, ts, aligned_sequence, cond, return_code_pred=True) o = model(clip, ts, aligned_sequence, cond, return_code_pred=True)
o = model(clip, ts, aligned_sequence, cond, aligned_latent)

View File

@ -68,7 +68,6 @@ class MusicDiffusionFid(evaluator.Evaluator):
self.diffusion_fn = self.perform_diffusion_from_codes self.diffusion_fn = self.perform_diffusion_from_codes
self.local_modules['codegen'] = get_music_codegen() self.local_modules['codegen'] = get_music_codegen()
self.spec_fn = TorchMelSpectrogramInjector({'n_mel_channels': 256, 'mel_fmax': 22000, 'normalize': True, 'in': 'in', 'out': 'out'}, {}) self.spec_fn = TorchMelSpectrogramInjector({'n_mel_channels': 256, 'mel_fmax': 22000, 'normalize': True, 'in': 'in', 'out': 'out'}, {})
self.spec_100_fn = TorchMelSpectrogramInjector({'n_mel_channels': 100, 'mel_fmax': 22000, 'normalize': True, 'in': 'in', 'out': 'out'}, {})
def load_data(self, path): def load_data(self, path):
return list(glob(f'{path}/*.wav')) return list(glob(f'{path}/*.wav'))
@ -167,21 +166,20 @@ class MusicDiffusionFid(evaluator.Evaluator):
mel = self.spec_fn({'in': audio})['out'] mel = self.spec_fn({'in': audio})['out']
codegen = self.local_modules['codegen'].to(mel.device) codegen = self.local_modules['codegen'].to(mel.device)
codes = codegen.get_codes(mel) codes = codegen.get_codes(mel)
mel100 = self.spec_100_fn({'in': audio})['out'] mel_norm = normalize_mel(mel)
mel100_norm = normalize_mel(mel100) precomputed = self.model.timestep_independent(aligned_conditioning=codes, conditioning_input=mel[:,:,:112],
precomputed_codes, precomputed_cond = self.model.timestep_independent(codes=codes, conditioning_input=mel100_norm[:,:,:112], expected_seq_len=mel_norm.shape[-1], return_code_pred=False)
expected_seq_len=mel100_norm.shape[-1], return_code_pred=False) gen_mel = self.diffuser.p_sample_loop(self.model, mel_norm.shape, noise=torch.zeros_like(mel_norm),
gen_mel = self.diffuser.p_sample_loop(self.model, mel100_norm.shape, model_kwargs={'precomputed_aligned_embeddings': precomputed})
model_kwargs={'precomputed_code_embeddings': precomputed_codes, 'precomputed_cond_embeddings': precomputed_cond})
#gen_mel_denorm = denormalize_mel(gen_mel) gen_mel_denorm = denormalize_mel(gen_mel)
#output_shape = (1,16,audio.shape[-1]//16) output_shape = (1,16,audio.shape[-1]//16)
#self.spec_decoder = self.spec_decoder.to(audio.device) self.spec_decoder = self.spec_decoder.to(audio.device)
#gen_wav = self.diffuser.p_sample_loop(self.spec_decoder, output_shape, model_kwargs={'aligned_conditioning': gen_mel_denorm}) gen_wav = self.diffuser.p_sample_loop(self.spec_decoder, output_shape, model_kwargs={'aligned_conditioning': gen_mel_denorm})
#gen_wav = pixel_shuffle_1d(gen_wav, 16) gen_wav = pixel_shuffle_1d(gen_wav, 16)
return gen_wav, real_resampled, gen_mel, mel_norm, sample_rate
#return gen_wav, real_resampled, gen_mel, mel_norm, sample_rate
return real_resampled.unsqueeze(0), real_resampled, gen_mel, mel100_norm, sample_rate
def project(self, sample, sample_rate): def project(self, sample, sample_rate):
sample = torchaudio.functional.resample(sample, sample_rate, 22050) sample = torchaudio.functional.resample(sample, sample_rate, 22050)