diff --git a/codes/models/audio/music/transformer_diffusion3.py b/codes/models/audio/music/transformer_diffusion3.py index f8a860f1..df2929c7 100644 --- a/codes/models/audio/music/transformer_diffusion3.py +++ b/codes/models/audio/music/transformer_diffusion3.py @@ -54,7 +54,6 @@ class TransformerDiffusion(nn.Module): """ A diffusion model composed entirely of stacks of transformer layers. Why would you do it any other way? """ - def __init__( self, model_channels=512, diff --git a/codes/models/audio/music/transformer_diffusion5.py b/codes/models/audio/music/transformer_diffusion5.py new file mode 100644 index 00000000..5bde0bb5 --- /dev/null +++ b/codes/models/audio/music/transformer_diffusion5.py @@ -0,0 +1,256 @@ +import torch +import torch.nn as nn +import torch.nn.functional as F + +from models.diffusion.nn import timestep_embedding, normalization, zero_module, conv_nd, linear +from models.diffusion.unet_diffusion import TimestepEmbedSequential, TimestepBlock +from models.lucidrains.x_transformers import Encoder, Attention, FeedForward, RMSScaleShiftNorm, RotaryEmbedding +from trainer.networks import register_model +from utils.util import checkpoint + + +def is_latent(t): + return t.dtype == torch.float + +def is_sequence(t): + return t.dtype == torch.long + + +class MultiGroupEmbedding(nn.Module): + def __init__(self, tokens, groups, dim): + super().__init__() + self.m = nn.ModuleList([nn.Embedding(tokens, dim // groups) for _ in range(groups)]) + + def forward(self, x): + h = [embedding(x[:, :, i]) for i, embedding in enumerate(self.m)] + return torch.cat(h, dim=-1) + + +class TimestepRotaryEmbedSequential(nn.Sequential, TimestepBlock): + def forward(self, x, emb, rotary_emb): + for layer in self: + if isinstance(layer, TimestepBlock): + x = layer(x, emb, rotary_emb) + else: + x = layer(x, rotary_emb) + return x + + +class AttentionBlock(TimestepBlock): + def __init__(self, dim, heads, dropout): + super().__init__() + self.attn = Attention(dim, heads=heads, causal=False, dropout=dropout, zero_init_output=False) + self.ff = FeedForward(dim, mult=1, dropout=dropout, zero_init_output=True) + self.rms_scale_norm = RMSScaleShiftNorm(dim) + + def forward(self, x, timestep_emb, rotary_emb): + h = self.rms_scale_norm(x, norm_scale_shift_inp=timestep_emb) + h, _, _, _ = checkpoint(self.attn, h, None, None, None, None, None, rotary_emb) + h = checkpoint(self.ff, h) + return h + x + + +class TransformerDiffusion(nn.Module): + """ + A diffusion model composed entirely of stacks of transformer layers. Why would you do it any other way? + """ + def __init__( + self, + model_channels=512, + num_layers=8, + in_channels=256, + in_latent_channels=512, + clvp_in_dim=768, + rotary_emb_dim=32, + token_count=8, + in_groups=None, + out_channels=512, # mean and variance + dropout=0, + use_fp16=False, + # Parameters for regularization. + unconditioned_percentage=.1, # This implements a mechanism similar to what is used in classifier-free training. + ): + super().__init__() + + self.in_channels = in_channels + self.model_channels = model_channels + self.out_channels = out_channels + self.dropout = dropout + self.unconditioned_percentage = unconditioned_percentage + self.enable_fp16 = use_fp16 + heads = model_channels//64 + + self.inp_block = conv_nd(1, in_channels, model_channels, 3, 1, 1) + + self.time_embed = nn.Sequential( + linear(model_channels, model_channels), + nn.SiLU(), + linear(model_channels, model_channels), + ) + self.conditioning_embedder = nn.Sequential(nn.Conv1d(in_channels, model_channels // 2, 3, padding=1, stride=2), + nn.Conv1d(model_channels//2, model_channels,3,padding=1,stride=2)) + self.conditioning_encoder = Encoder( + dim=model_channels, + depth=4, + heads=heads, + ff_dropout=dropout, + attn_dropout=dropout, + use_rmsnorm=True, + ff_glu=True, + rotary_pos_emb=True, + ) + self.clvp_encoder = nn.Linear(clvp_in_dim, model_channels) + + # Either code_converter or latent_converter is used, depending on what type of conditioning data is fed. + # This model is meant to be able to be trained on both for efficiency purposes - it is far less computationally + # complex to generate tokens, while generating latents will normally mean propagating through a deep autoregressive + # transformer network. + if in_groups is None: + self.embeddings = nn.Embedding(token_count, model_channels) + else: + self.embeddings = MultiGroupEmbedding(token_count, in_groups, model_channels) + self.latent_conditioner = nn.Sequential( + nn.Conv1d(in_latent_channels, model_channels, 3, padding=1), + Encoder( + dim=model_channels, + depth=2, + heads=heads, + ff_dropout=dropout, + attn_dropout=dropout, + use_rmsnorm=True, + ff_glu=True, + rotary_pos_emb=True, + ) + ) + self.latent_fade = nn.Parameter(torch.zeros(1,1,model_channels)) + self.code_converter = Encoder( + dim=model_channels, + depth=3, + heads=heads, + ff_dropout=dropout, + attn_dropout=dropout, + use_rmsnorm=True, + ff_glu=True, + rotary_pos_emb=True, + ) + + self.unconditioned_embedding = nn.Parameter(torch.randn(1,1,model_channels)) + self.mel_head = nn.Conv1d(model_channels, in_channels, kernel_size=3, padding=1) + + self.rotary_embeddings = RotaryEmbedding(rotary_emb_dim) + self.intg = nn.Linear(model_channels*2, model_channels) + self.layers = TimestepRotaryEmbedSequential(*[AttentionBlock(model_channels, model_channels//64, dropout) for _ in range(num_layers)]) + + self.out = nn.Sequential( + normalization(model_channels), + nn.SiLU(), + zero_module(conv_nd(1, model_channels, out_channels, 3, padding=1)), + ) + + self.debug_codes = {} + + def get_grad_norm_parameter_groups(self): + groups = { + 'contextual_embedder': list(self.conditioning_embedder.parameters()), + 'layers': list(self.layers.parameters()) + list(self.inp_block.parameters()), + 'code_converters': list(self.embeddings.parameters()) + list(self.code_converter.parameters()) + list(self.latent_conditioner.parameters()), + 'time_embed': list(self.time_embed.parameters()), + } + return groups + + def timestep_independent(self, codes, conditioning_input, expected_seq_len, prenet_latent=None, return_code_pred=False): + cond_emb = self.conditioning_embedder(conditioning_input).permute(0,2,1) + cond_emb = self.conditioning_encoder(cond_emb)[:, 0] + + code_emb = self.embeddings(codes) + if prenet_latent is not None: + latent_conditioning = self.latent_conditioner(prenet_latent) + code_emb = code_emb + latent_conditioning * self.latent_fade + + 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. + if self.training and self.unconditioned_percentage > 0: + unconditioned_batches = torch.rand((code_emb.shape[0], 1, 1), + device=code_emb.device) < self.unconditioned_percentage + code_emb = torch.where(unconditioned_batches, self.unconditioned_embedding.repeat(codes.shape[0], 1, 1), + code_emb) + code_emb = self.code_converter(code_emb) + + expanded_code_emb = F.interpolate(code_emb.permute(0,2,1), size=expected_seq_len, mode='nearest').permute(0,2,1) + if not return_code_pred: + return expanded_code_emb, cond_emb + else: + # Perform the mel_head computation on the pre-exanded code embeddings, then interpolate it separately. + mel_pred = self.mel_head(code_emb.permute(0,2,1)) + 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() + return expanded_code_emb, cond_emb, mel_pred + + + def forward(self, x, timesteps, codes=None, conditioning_input=None, clvp_input=None, prenet_latent=None, precomputed_code_embeddings=None, + precomputed_cond_embeddings=None, conditioning_free=False, return_code_pred=False): + if precomputed_code_embeddings is not None: + assert precomputed_cond_embeddings is not None, "Must specify both precomputed embeddings if one is specified" + 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 = [] + if not return_code_pred: + unused_params.extend(list(self.mel_head.parameters())) + if conditioning_free: + 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.latent_conditioner.parameters())) + else: + if precomputed_code_embeddings is not None: + code_emb = precomputed_code_embeddings + cond_emb = precomputed_cond_embeddings + else: + code_emb, cond_emb, mel_pred = self.timestep_independent(codes, conditioning_input, x.shape[-1], prenet_latent, True) + if prenet_latent is None: + unused_params.extend(list(self.latent_conditioner.parameters()) + [self.latent_fade]) + unused_params.append(self.unconditioned_embedding) + + clvp_emb = torch.zeros_like(cond_emb) if clvp_input is None else self.clvp_encoder(clvp_input) + if clvp_input is None: + unused_params.extend(self.clvp_encoder.parameters()) + blk_emb = self.time_embed(timestep_embedding(timesteps, self.model_channels)) + cond_emb + clvp_emb + x = self.inp_block(x).permute(0,2,1) + + rotary_pos_emb = self.rotary_embeddings(x.shape[1], x.device) + x = self.intg(torch.cat([x, code_emb], dim=-1)) + x = self.layers(x, blk_emb, rotary_pos_emb) + + x = x.float().permute(0,2,1) + out = self.out(x) + + # Involve probabilistic or possibly unused parameters in loss so we don't get DDP errors. + extraneous_addition = 0 + for p in unused_params: + extraneous_addition = extraneous_addition + p.mean() + out = out + extraneous_addition * 0 + + if return_code_pred: + return out, mel_pred + return out + + +@register_model +def register_transformer_diffusion5(opt_net, opt): + return TransformerDiffusion(**opt_net['kwargs']) + + +if __name__ == '__main__': + clip = torch.randn(2, 256, 400) + aligned_latent = torch.randn(2,100,512) + aligned_sequence = torch.randint(0,8,(2,100,8)) + cond = torch.randn(2, 256, 400) + ts = torch.LongTensor([600, 600]) + clvp = torch.randn(2,768) + model = TransformerDiffusion(512, unconditioned_percentage=.5, in_groups=8) + o = model(clip, ts, aligned_sequence, cond, clvp_input=clvp, return_code_pred=True) + #o = model(clip, ts, aligned_sequence, cond, aligned_latent) + diff --git a/codes/models/clip/text_voice_clip.py b/codes/models/clip/text_voice_clip.py index 59e8d7dd..297e71dc 100644 --- a/codes/models/clip/text_voice_clip.py +++ b/codes/models/clip/text_voice_clip.py @@ -108,6 +108,14 @@ class VoiceCLIP(nn.Module): self.text_pos_emb = nn.Embedding(text_seq_len, dim_text) self.speech_pos_emb = nn.Embedding(num_speech_tokens, dim_speech) + def embed_text(self, text): + text_mask = torch.ones_like(text.float()).bool() + text_emb = self.text_emb(text) + enc_text = self.text_transformer(text_emb, mask=text_mask) + text_latents = masked_mean(enc_text, text_mask, dim=1) + text_latents = self.to_text_latent(text_latents) + return text_latents + def forward( self, text, diff --git a/codes/trainer/injectors/audio_injectors.py b/codes/trainer/injectors/audio_injectors.py index a9769c24..a2232c99 100644 --- a/codes/trainer/injectors/audio_injectors.py +++ b/codes/trainer/injectors/audio_injectors.py @@ -331,9 +331,10 @@ class Mel2vecCodesInjector(Injector): from models.audio.mel2vec import ContrastiveTrainingWrapper self.m2v = ContrastiveTrainingWrapper(mel_input_channels=256, inner_dim=1024, layers=24, dropout=0, mask_time_prob=0, - mask_time_length=6, num_negatives=100, codebook_size=8, codebook_groups=8, - disable_custom_linear_init=True) + mask_time_length=6, num_negatives=100, codebook_size=16, codebook_groups=4, + disable_custom_linear_init=True, do_reconstruction_loss=True) self.m2v.load_state_dict(torch.load(f"../experiments/m2v_{for_what}.pth", map_location=torch.device('cpu'))) + self.m2v = self.m2v.eval() del self.m2v.m2v.encoder # This is a big memory sink which will not get used. self.needs_move = True @@ -344,3 +345,25 @@ class Mel2vecCodesInjector(Injector): self.m2v = self.m2v.to(mels.device) codes = self.m2v.get_codes(mels) return {self.output: codes} + + +class ClvpTextInjector(Injector): + def __init__(self, opt, env): + super().__init__(opt, env) + from models.clip.text_voice_clip import VoiceCLIP + self.clvp = VoiceCLIP(dim_text=768, dim_speech=768, dim_latent=768, num_text_tokens=256, text_enc_depth=20, + text_seq_len=350, text_heads=12, num_speech_tokens=8192, speech_enc_depth=20, + speech_heads=12, speech_seq_len=430, text_mask_percentage=0, voice_mask_percentage=0, + use_xformers=True) + self.clvp.load_state_dict(torch.load(f"../experiments/clvp_md.pth", map_location=torch.device('cpu'))) + self.clvp = self.clvp.eval() + del self.clvp.speech_transformer # We will only be using the text transformer. + self.needs_move = True + + def forward(self, state): + codes = state[self.input] + with torch.no_grad(): + if self.needs_move: + self.clvp = self.clvp.to(codes.device) + latents = self.clvp.embed_text(codes) + return {self.output: latents} \ No newline at end of file