Spaces:
Running
Running
File size: 8,493 Bytes
c968fc3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# Copyright (c) 2023 Amphion.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
from models.tts.naturalspeech2.diffusion import Diffusion
from models.tts.naturalspeech2.diffusion_flow import DiffusionFlow
from models.tts.naturalspeech2.wavenet import WaveNet
from models.tts.naturalspeech2.prior_encoder import PriorEncoder
from modules.naturalpseech2.transformers import TransformerEncoder
from encodec import EncodecModel
from einops import rearrange, repeat
import os
import json
class NaturalSpeech2(nn.Module):
def __init__(self, cfg):
super().__init__()
self.cfg = cfg
self.latent_dim = cfg.latent_dim
self.query_emb_num = cfg.query_emb.query_token_num
self.prior_encoder = PriorEncoder(cfg.prior_encoder)
if cfg.diffusion.diffusion_type == "diffusion":
self.diffusion = Diffusion(cfg.diffusion)
elif cfg.diffusion.diffusion_type == "flow":
self.diffusion = DiffusionFlow(cfg.diffusion)
self.prompt_encoder = TransformerEncoder(cfg=cfg.prompt_encoder)
if self.latent_dim != cfg.prompt_encoder.encoder_hidden:
self.prompt_lin = nn.Linear(
self.latent_dim, cfg.prompt_encoder.encoder_hidden
)
self.prompt_lin.weight.data.normal_(0.0, 0.02)
else:
self.prompt_lin = None
self.query_emb = nn.Embedding(self.query_emb_num, cfg.query_emb.hidden_size)
self.query_attn = nn.MultiheadAttention(
cfg.query_emb.hidden_size, cfg.query_emb.head_num, batch_first=True
)
codec_model = EncodecModel.encodec_model_24khz()
codec_model.set_target_bandwidth(12.0)
codec_model.requires_grad_(False)
self.quantizer = codec_model.quantizer
@torch.no_grad()
def code_to_latent(self, code):
latent = self.quantizer.decode(code.transpose(0, 1))
return latent
def latent_to_code(self, latent, nq=16):
residual = latent
all_indices = []
all_dist = []
for i in range(nq):
layer = self.quantizer.vq.layers[i]
x = rearrange(residual, "b d n -> b n d")
x = layer.project_in(x)
shape = x.shape
x = layer._codebook.preprocess(x)
embed = layer._codebook.embed.t()
dist = -(
x.pow(2).sum(1, keepdim=True)
- 2 * x @ embed
+ embed.pow(2).sum(0, keepdim=True)
)
indices = dist.max(dim=-1).indices
indices = layer._codebook.postprocess_emb(indices, shape)
dist = dist.reshape(*shape[:-1], dist.shape[-1])
quantized = layer.decode(indices)
residual = residual - quantized
all_indices.append(indices)
all_dist.append(dist)
out_indices = torch.stack(all_indices)
out_dist = torch.stack(all_dist)
return out_indices, out_dist # (nq, B, T); (nq, B, T, 1024)
@torch.no_grad()
def latent_to_latent(self, latent, nq=16):
codes, _ = self.latent_to_code(latent, nq)
latent = self.quantizer.vq.decode(codes)
return latent
def forward(
self,
code=None,
pitch=None,
duration=None,
phone_id=None,
phone_id_frame=None,
frame_nums=None,
ref_code=None,
ref_frame_nums=None,
phone_mask=None,
mask=None,
ref_mask=None,
):
ref_latent = self.code_to_latent(ref_code)
latent = self.code_to_latent(code)
if self.latent_dim is not None:
ref_latent = self.prompt_lin(ref_latent.transpose(1, 2))
ref_latent = self.prompt_encoder(ref_latent, ref_mask, condition=None)
spk_emb = ref_latent.transpose(1, 2) # (B, d, T')
spk_query_emb = self.query_emb(
torch.arange(self.query_emb_num).to(latent.device)
).repeat(
latent.shape[0], 1, 1
) # (B, query_emb_num, d)
spk_query_emb, _ = self.query_attn(
spk_query_emb,
spk_emb.transpose(1, 2),
spk_emb.transpose(1, 2),
key_padding_mask=~(ref_mask.bool()),
) # (B, query_emb_num, d)
prior_out = self.prior_encoder(
phone_id=phone_id,
duration=duration,
pitch=pitch,
phone_mask=phone_mask,
mask=mask,
ref_emb=spk_emb,
ref_mask=ref_mask,
is_inference=False,
)
prior_condition = prior_out["prior_out"] # (B, T, d)
diff_out = self.diffusion(latent, mask, prior_condition, spk_query_emb)
return diff_out, prior_out
@torch.no_grad()
def inference(
self, ref_code=None, phone_id=None, ref_mask=None, inference_steps=1000
):
ref_latent = self.code_to_latent(ref_code)
if self.latent_dim is not None:
ref_latent = self.prompt_lin(ref_latent.transpose(1, 2))
ref_latent = self.prompt_encoder(ref_latent, ref_mask, condition=None)
spk_emb = ref_latent.transpose(1, 2) # (B, d, T')
spk_query_emb = self.query_emb(
torch.arange(self.query_emb_num).to(ref_latent.device)
).repeat(
ref_latent.shape[0], 1, 1
) # (B, query_emb_num, d)
spk_query_emb, _ = self.query_attn(
spk_query_emb,
spk_emb.transpose(1, 2),
spk_emb.transpose(1, 2),
key_padding_mask=~(ref_mask.bool()),
) # (B, query_emb_num, d)
prior_out = self.prior_encoder(
phone_id=phone_id,
duration=None,
pitch=None,
phone_mask=None,
mask=None,
ref_emb=spk_emb,
ref_mask=ref_mask,
is_inference=True,
)
prior_condition = prior_out["prior_out"] # (B, T, d)
z = torch.randn(
prior_condition.shape[0], self.latent_dim, prior_condition.shape[1]
).to(ref_latent.device) / (1.20)
x0 = self.diffusion.reverse_diffusion(
z, None, prior_condition, inference_steps, spk_query_emb
)
return x0, prior_out
@torch.no_grad()
def reverse_diffusion_from_t(
self,
code=None,
pitch=None,
duration=None,
phone_id=None,
ref_code=None,
phone_mask=None,
mask=None,
ref_mask=None,
n_timesteps=None,
t=None,
):
# o Only for debug
ref_latent = self.code_to_latent(ref_code)
latent = self.code_to_latent(code)
if self.latent_dim is not None:
ref_latent = self.prompt_lin(ref_latent.transpose(1, 2))
ref_latent = self.prompt_encoder(ref_latent, ref_mask, condition=None)
spk_emb = ref_latent.transpose(1, 2) # (B, d, T')
spk_query_emb = self.query_emb(
torch.arange(self.query_emb_num).to(latent.device)
).repeat(
latent.shape[0], 1, 1
) # (B, query_emb_num, d)
spk_query_emb, _ = self.query_attn(
spk_query_emb,
spk_emb.transpose(1, 2),
spk_emb.transpose(1, 2),
key_padding_mask=~(ref_mask.bool()),
) # (B, query_emb_num, d)
prior_out = self.prior_encoder(
phone_id=phone_id,
duration=duration,
pitch=pitch,
phone_mask=phone_mask,
mask=mask,
ref_emb=spk_emb,
ref_mask=ref_mask,
is_inference=False,
)
prior_condition = prior_out["prior_out"] # (B, T, d)
diffusion_step = (
torch.ones(
latent.shape[0],
dtype=latent.dtype,
device=latent.device,
requires_grad=False,
)
* t
)
diffusion_step = torch.clamp(diffusion_step, 1e-5, 1.0 - 1e-5)
xt, _ = self.diffusion.forward_diffusion(
x0=latent, diffusion_step=diffusion_step
)
# print(torch.abs(xt-latent).max(), torch.abs(xt-latent).mean(), torch.abs(xt-latent).std())
x0 = self.diffusion.reverse_diffusion_from_t(
xt, mask, prior_condition, n_timesteps, spk_query_emb, t_start=t
)
return x0, prior_out, xt
|