Spaces:
Running
on
Zero
Running
on
Zero
File size: 20,950 Bytes
faadabf |
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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 |
from einops import rearrange
from torch import sin, pow
from torch.nn import Parameter
from torch.nn.utils import spectral_norm, weight_norm
import math
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchaudio
import typing as tp
import warnings
from .alias_free_torch import *
from .vector_quantization import VectorQuantization
CONV_NORMALIZATIONS = frozenset(
[
"none",
"weight_norm",
"spectral_norm",
"time_layer_norm",
"layer_norm",
"time_group_norm",
]
)
def init_weights(m):
if isinstance(m, nn.Conv1d):
nn.init.trunc_normal_(m.weight, std=0.02)
nn.init.constant_(m.bias, 0)
def apply_parametrization_norm(module: nn.Module, norm: str = "none") -> nn.Module:
assert norm in CONV_NORMALIZATIONS
if norm == "weight_norm":
return weight_norm(module)
elif norm == "spectral_norm":
return spectral_norm(module)
else:
return module
def get_norm_module(
module: nn.Module, causal: bool = False, norm: str = "none", **norm_kwargs
) -> nn.Module:
assert norm in CONV_NORMALIZATIONS
if norm == "time_group_norm":
if causal:
raise ValueError("GroupNorm doesn't support causal evaluation.")
assert isinstance(module, nn.modules.conv._ConvNd)
return nn.GroupNorm(1, module.out_channels, **norm_kwargs)
else:
return nn.Identity()
def get_extra_padding_for_conv1d(
x: torch.Tensor, kernel_size: int, stride: int, padding_total: int = 0
) -> int:
length = x.shape[-1]
n_frames = (length - kernel_size + padding_total) / stride + 1
ideal_length = (math.ceil(n_frames) - 1) * stride + (kernel_size - padding_total)
return ideal_length - length
def pad_for_conv1d(
x: torch.Tensor, kernel_size: int, stride: int, padding_total: int = 0
):
extra_padding = get_extra_padding_for_conv1d(x, kernel_size, stride, padding_total)
return F.pad(x, (0, extra_padding))
def pad1d(
x: torch.Tensor,
paddings: tp.Tuple[int, int],
mode: str = "zero",
value: float = 0.0,
):
length = x.shape[-1]
padding_left, padding_right = paddings
assert padding_left >= 0 and padding_right >= 0, (padding_left, padding_right)
if mode == "reflect":
max_pad = max(padding_left, padding_right)
extra_pad = 0
if length <= max_pad:
extra_pad = max_pad - length + 1
x = F.pad(x, (0, extra_pad))
padded = F.pad(x, paddings, mode, value)
end = padded.shape[-1] - extra_pad
return padded[..., :end]
else:
return F.pad(x, paddings, mode, value)
def unpad1d(x: torch.Tensor, paddings: tp.Tuple[int, int]):
padding_left, padding_right = paddings
assert padding_left >= 0 and padding_right >= 0, (padding_left, padding_right)
assert (padding_left + padding_right) <= x.shape[-1]
end = x.shape[-1] - padding_right
return x[..., padding_left:end]
class NormConv1d(nn.Module):
def __init__(
self,
*args,
causal: bool = False,
norm: str = "none",
norm_kwargs: tp.Dict[str, tp.Any] = {},
**kwargs,
):
super().__init__()
self.conv = apply_parametrization_norm(nn.Conv1d(*args, **kwargs), norm)
self.norm = get_norm_module(self.conv, causal, norm, **norm_kwargs)
self.norm_type = norm
def forward(self, x):
x = self.conv(x)
x = self.norm(x)
return x
class NormConvTranspose1d(nn.Module):
def __init__(
self,
*args,
causal: bool = False,
norm: str = "none",
norm_kwargs: tp.Dict[str, tp.Any] = {},
**kwargs,
):
super().__init__()
self.convtr = apply_parametrization_norm(
nn.ConvTranspose1d(*args, **kwargs), norm
)
self.norm = get_norm_module(self.convtr, causal, norm, **norm_kwargs)
self.norm_type = norm
def forward(self, x):
x = self.convtr(x)
x = self.norm(x)
return x
class SConv1d(nn.Module):
def __init__(
self,
in_channels: int,
out_channels: int,
kernel_size: int,
stride: int = 1,
dilation: int = 1,
groups: int = 1,
bias: bool = True,
causal: bool = False,
norm: str = "none",
norm_kwargs: tp.Dict[str, tp.Any] = {},
pad_mode: str = "reflect",
**kwargs,
):
super().__init__()
# warn user on unusual setup between dilation and stride
if stride > 1 and dilation > 1:
warnings.warn(
"SConv1d has been initialized with stride > 1 and dilation > 1"
f" (kernel_size={kernel_size} stride={stride}, dilation={dilation})."
)
self.conv = NormConv1d(
in_channels,
out_channels,
kernel_size,
stride,
dilation=dilation,
groups=groups,
bias=bias,
causal=causal,
norm=norm,
norm_kwargs=norm_kwargs,
)
self.causal = causal
self.pad_mode = pad_mode
def forward(self, x):
B, C, T = x.shape
kernel_size = self.conv.conv.kernel_size[0]
stride = self.conv.conv.stride[0]
dilation = self.conv.conv.dilation[0]
kernel_size = (
kernel_size - 1
) * dilation + 1 # effective kernel size with dilations
padding_total = kernel_size - stride
extra_padding = get_extra_padding_for_conv1d(
x, kernel_size, stride, padding_total
)
if self.causal:
# Left padding for causal
x = pad1d(x, (padding_total, extra_padding), mode=self.pad_mode)
else:
# Asymmetric padding required for odd strides
padding_right = padding_total // 2
padding_left = padding_total - padding_right
x = pad1d(
x, (padding_left, padding_right + extra_padding), mode=self.pad_mode
)
return self.conv(x)
class SConvTranspose1d(nn.Module):
def __init__(
self,
in_channels: int,
out_channels: int,
kernel_size: int,
stride: int = 1,
causal: bool = False,
norm: str = "none",
trim_right_ratio: float = 1.0,
norm_kwargs: tp.Dict[str, tp.Any] = {},
**kwargs,
):
super().__init__()
self.convtr = NormConvTranspose1d(
in_channels,
out_channels,
kernel_size,
stride,
causal=causal,
norm=norm,
norm_kwargs=norm_kwargs,
)
self.causal = causal
self.trim_right_ratio = trim_right_ratio
assert (
self.causal or self.trim_right_ratio == 1.0
), "`trim_right_ratio` != 1.0 only makes sense for causal convolutions"
assert self.trim_right_ratio >= 0.0 and self.trim_right_ratio <= 1.0
def forward(self, x):
kernel_size = self.convtr.convtr.kernel_size[0]
stride = self.convtr.convtr.stride[0]
padding_total = kernel_size - stride
y = self.convtr(x)
# We will only trim fixed padding. Extra padding from `pad_for_conv1d` would be
# removed at the very end, when keeping only the right length for the output,
# as removing it here would require also passing the length at the matching layer
# in the encoder.
if self.causal:
# Trim the padding on the right according to the specified ratio
# if trim_right_ratio = 1.0, trim everything from right
padding_right = math.ceil(padding_total * self.trim_right_ratio)
padding_left = padding_total - padding_right
y = unpad1d(y, (padding_left, padding_right))
else:
# Asymmetric padding required for odd strides
padding_right = padding_total // 2
padding_left = padding_total - padding_right
y = unpad1d(y, (padding_left, padding_right))
return y
def WNConv1d(*args, **kwargs):
if kwargs.get("causal", False):
kwargs["norm"] = "weight_norm"
conv1d = SConv1d(*args, **kwargs)
else:
kwargs.pop("causal")
conv1d = weight_norm(nn.Conv1d(*args, **kwargs))
return conv1d
def WNConvTranspose1d(*args, **kwargs):
if kwargs.get("causal", False):
kwargs["norm"] = "weight_norm"
transposed_conv1d = SConvTranspose1d(*args, **kwargs)
else:
kwargs.pop("causal")
transposed_conv1d = weight_norm(nn.ConvTranspose1d(*args, **kwargs))
return transposed_conv1d
class SnakeBeta(nn.Module):
def __init__(
self, in_features, alpha=1.0, alpha_trainable=True, alpha_logscale=False
):
super(SnakeBeta, self).__init__()
self.in_features = in_features
# initialize alpha
self.alpha_logscale = alpha_logscale
if self.alpha_logscale: # log scale alphas initialized to zeros
self.alpha = Parameter(torch.zeros(in_features) * alpha)
self.beta = Parameter(torch.zeros(in_features) * alpha)
else: # linear scale alphas initialized to ones
self.alpha = Parameter(torch.ones(in_features) * alpha)
self.beta = Parameter(torch.ones(in_features) * alpha)
self.alpha.requires_grad = alpha_trainable
self.beta.requires_grad = alpha_trainable
self.no_div_by_zero = 0.000000001
def forward(self, x):
alpha = self.alpha.unsqueeze(0).unsqueeze(-1) # line up with x to [B, C, T]
beta = self.beta.unsqueeze(0).unsqueeze(-1)
if self.alpha_logscale:
alpha = torch.exp(alpha)
beta = torch.exp(beta)
x = x + (1.0 / (beta + self.no_div_by_zero)) * pow(sin(x * alpha), 2)
return x
class ResidualUnit(nn.Module):
def __init__(self, dim: int = 16, dilation: int = 1, causal: bool = False):
super().__init__()
pad = ((7 - 1) * dilation) // 2
self.block = nn.Sequential(
Activation1d(activation=SnakeBeta(dim, alpha_logscale=True), causal=causal),
WNConv1d(
dim, dim, kernel_size=7, dilation=dilation, padding=pad, causal=causal
),
Activation1d(activation=SnakeBeta(dim, alpha_logscale=True), causal=causal),
WNConv1d(dim, dim, kernel_size=1, causal=causal),
)
def forward(self, x):
return x + self.block(x)
class EncoderBlock(nn.Module):
def __init__(
self, dim: int = 16, stride: int = 1, dilations=(1, 3, 9), causal: bool = False
):
super().__init__()
runits = [ResidualUnit(dim // 2, dilation=d, causal=causal) for d in dilations]
self.block = nn.Sequential(
*runits,
Activation1d(
activation=SnakeBeta(dim // 2, alpha_logscale=True), causal=causal
),
WNConv1d(
dim // 2,
dim,
kernel_size=2 * stride,
stride=stride,
padding=stride // 2 + stride % 2,
causal=causal,
),
)
def forward(self, x):
return self.block(x)
class DecoderBlock(nn.Module):
def __init__(
self,
input_dim: int = 16,
output_dim: int = 8,
stride: int = 1,
dilations=(1, 3, 9),
causal: bool = False,
):
super().__init__()
self.block = nn.Sequential(
Activation1d(
activation=SnakeBeta(input_dim, alpha_logscale=True), causal=causal
),
WNConvTranspose1d(
input_dim,
output_dim,
kernel_size=2 * stride,
stride=stride,
padding=stride // 2 + stride % 2,
output_padding=stride % 2,
causal=causal,
),
)
self.block.extend(
[ResidualUnit(output_dim, dilation=d, causal=causal) for d in dilations]
)
def forward(self, x):
return self.block(x)
class ResLSTM(nn.Module):
def __init__(
self,
dimension: int,
num_layers: int = 2,
bidirectional: bool = False,
skip: bool = True,
):
super().__init__()
self.skip = skip
self.lstm = nn.LSTM(
dimension,
dimension if not bidirectional else dimension // 2,
num_layers,
batch_first=True,
bidirectional=bidirectional,
)
def forward(self, x):
x = rearrange(x, "b f t -> b t f")
y, _ = self.lstm(x)
if self.skip:
y = y + x
y = rearrange(y, "b t f -> b f t")
return y
class Resampler(nn.Module):
def __init__(self, source_sr=24000, target_sr=24000):
super().__init__()
self.source_sr = source_sr
self.target_sr = target_sr
def forward(self, wav, wav_length):
if self.source_sr != self.target_sr:
wav = torchaudio.functional.resample(wav, self.source_sr, self.target_sr)
wav_length = (wav_length * (self.source_sr / self.target_sr)).int()
return wav, wav_length
class CodecEncoder(nn.Module):
def __init__(
self,
ngf=48,
use_rnn=True,
rnn_bidirectional=False,
rnn_num_layers=2,
up_ratios=(2, 2, 2, 5, 5),
dilations=(1, 3, 9),
out_channels=1024,
causal=False,
):
super().__init__()
self.hop_length = np.prod(up_ratios)
self.ngf = ngf
self.up_ratios = up_ratios
# Create first convolution
d_model = ngf
self.block = [WNConv1d(1, d_model, kernel_size=7, padding=3, causal=causal)]
# Create EncoderBlocks that double channels as they downsample by `stride`
for i, stride in enumerate(up_ratios):
d_model *= 2
self.block += [
EncoderBlock(d_model, stride=stride, dilations=dilations, causal=causal)
]
# RNN
if use_rnn:
self.block += [
ResLSTM(
d_model, num_layers=rnn_num_layers, bidirectional=rnn_bidirectional
)
]
# Create last convolution
self.block += [
Activation1d(
activation=SnakeBeta(d_model, alpha_logscale=True), causal=causal
),
WNConv1d(d_model, out_channels, kernel_size=3, padding=1, causal=causal),
]
# Wrap black into nn.Sequential
self.block = nn.Sequential(*self.block)
self.enc_dim = d_model
self.reset_parameters()
def forward(self, x):
out = self.block(x)
return out
def remove_weight_norm(self):
def _remove_weight_norm(m):
try:
torch.nn.utils.remove_weight_norm(m)
except ValueError: # this module didn't have weight norm
return
self.apply(_remove_weight_norm)
def apply_weight_norm(self):
def _apply_weight_norm(m):
if isinstance(m, nn.Conv1d):
torch.nn.utils.weight_norm(m)
self.apply(_apply_weight_norm)
def reset_parameters(self):
self.apply(init_weights)
class CodecDecoder(nn.Module):
def __init__(
self,
in_channels=1024,
upsample_initial_channel=1536,
ngf=48,
use_rnn=True,
rnn_bidirectional=False,
rnn_num_layers=2,
up_ratios=(5, 5, 2, 2, 2),
dilations=(1, 3, 9),
causal=False,
delay=0,
):
super().__init__()
self.hop_length = np.prod(up_ratios)
self.ngf = ngf
self.up_ratios = up_ratios
self.delay = delay
channels = upsample_initial_channel
layers = [
WNConv1d(in_channels, channels, kernel_size=7, padding=3, causal=causal)
]
if use_rnn:
layers += [
ResLSTM(
channels, num_layers=rnn_num_layers, bidirectional=rnn_bidirectional
)
]
for i, stride in enumerate(up_ratios):
input_dim = channels // 2**i
output_dim = channels // 2 ** (i + 1)
layers += [
DecoderBlock(input_dim, output_dim, stride, dilations, causal=causal)
]
layers += [
Activation1d(
activation=SnakeBeta(output_dim, alpha_logscale=True), causal=causal
),
WNConv1d(output_dim, 1, kernel_size=7, padding=3, causal=causal),
nn.Tanh(),
]
self.model = nn.Sequential(*layers)
self.reset_parameters()
def forward(self, x):
# Time delay
if self.delay > 0:
x = F.pad(x, (0, self.delay), mode="constant", value=0)
x = self.model(x)
# De-delay
if self.delay > 0:
x = x[..., self.delay :]
return x
def remove_weight_norm(self):
def _remove_weight_norm(m):
try:
torch.nn.utils.remove_weight_norm(m)
except ValueError: # this module didn't have weight norm
return
self.apply(_remove_weight_norm)
def apply_weight_norm(self):
def _apply_weight_norm(m):
if isinstance(m, nn.Conv1d) or isinstance(m, nn.ConvTranspose1d):
torch.nn.utils.weight_norm(m)
self.apply(_apply_weight_norm)
def reset_parameters(self):
self.apply(init_weights)
class BigCodec(nn.Module):
def __init__(
self,
n_model_size: int,
encoder_config: dict,
decoder_config: dict,
vq_config: dict,
resampler_config: dict = None,
):
super(BigCodec, self).__init__()
self.n_model_size = n_model_size
self.encoder = CodecEncoder(out_channels=n_model_size, **encoder_config)
self.decoder = CodecDecoder(in_channels=n_model_size, **decoder_config)
self.quantizer = VectorQuantization(n_model_size, **vq_config)
# Optional modules
if resampler_config:
self.resampler = Resampler(**resampler_config)
def forward(
self, wav, wav_length=None, enable_vq=True, decode=True, update_codebook=True
):
# Preprocess wav
if len(wav.shape) == 2:
wav = wav.unsqueeze(1)
if wav_length is None:
wav_length = torch.full([wav.shape[0]], max(wav.shape)).to(wav.device)
# (Optional) Resample
processed_wav, processed_wav_length = wav, wav_length
if hasattr(self, "resampler"):
processed_wav, processed_wav_length = self.resampler(
processed_wav, processed_wav_length
)
# Update VQ parameters
quant_length = torch.ceil(processed_wav_length / self.encoder.hop_length).int()
update_codebook = update_codebook and self.training
# Encode
encoder_outputs = self.encoder(processed_wav)
# Quantize
quant, diff, embed_ind = self.quantizer(
encoder_outputs.transpose(1, 2),
quant_length.clamp(max=encoder_outputs.shape[2]),
enable_vq=enable_vq,
update_codebook=update_codebook,
)
if decode:
# Decode
decoder_outputs = self.decoder(quant.transpose(1, 2))
else:
decoder_outputs = None
output_dict = {
"quant": quant,
"token": embed_ind,
"token_length": quant_length,
"encoder_diffs": diff,
"wav_pred": decoder_outputs,
}
return output_dict
@torch.cuda.amp.autocast(enabled=True, dtype=torch.float32)
def extract_speech_tokens(
self, wav, wav_length, serialize=True, extract_spk=True, shuffle=False
):
output_dict = self.forward(wav, wav_length, enable_vq=True, decode=False)
token_seqs, token_length = [output_dict["token"]], [output_dict["token_length"]]
output_dict.update(
{
"token": token_seqs,
"token_length": token_length,
}
)
return output_dict
@torch.cuda.amp.autocast(enabled=True, dtype=torch.float32)
def reconstruct_wav(self, token=None, quant=None, spk=None):
if token is not None:
# De-tokenization
quant = self.quantizer.decode(token)
# Speaker embedding
if hasattr(self, "global_encoder"):
quant = quant + spk.unsqueeze(2)
else:
assert quant is not None
# Decode
wav_pred = self.decoder(quant)
return {
"wav_pred": wav_pred,
}
|