|
| 1 | +import torch |
| 2 | +import torch.nn as nn |
| 3 | +import torch.nn.functional as F |
| 4 | + |
| 5 | +from modules.commons.common_layers import SinusoidalPosEmb, SwiGLU, Conv1d, Transpose |
| 6 | +from utils.hparams import hparams |
| 7 | + |
| 8 | + |
| 9 | +class LYNXNet2Block(nn.Module): |
| 10 | + def __init__(self, dim, expansion_factor, kernel_size=31, dropout=0.): |
| 11 | + super().__init__() |
| 12 | + inner_dim = int(dim * expansion_factor) |
| 13 | + if float(dropout) > 0.: |
| 14 | + _dropout = nn.Dropout(dropout) |
| 15 | + else: |
| 16 | + _dropout = nn.Identity() |
| 17 | + self.net = nn.Sequential( |
| 18 | + nn.LayerNorm(dim), |
| 19 | + Transpose((1, 2)), |
| 20 | + nn.Conv1d(dim, dim, kernel_size=kernel_size, padding=kernel_size // 2, groups=dim), |
| 21 | + Transpose((1, 2)), |
| 22 | + nn.Linear(dim, inner_dim * 2), |
| 23 | + SwiGLU(), |
| 24 | + nn.Linear(inner_dim, inner_dim * 2), |
| 25 | + SwiGLU(), |
| 26 | + nn.Linear(inner_dim, dim), |
| 27 | + _dropout |
| 28 | + ) |
| 29 | + |
| 30 | + def forward(self, x): |
| 31 | + return x + self.net(x) |
| 32 | + |
| 33 | + |
| 34 | +class LYNXNet2(nn.Module): |
| 35 | + def __init__(self, in_dims, n_feats, *, num_layers=6, num_channels=512, expansion_factor=1, kernel_size=31, |
| 36 | + dropout=0.0): |
| 37 | + """ |
| 38 | + LYNXNet2(Linear Gated Depthwise Separable Convolution Network Version 2) |
| 39 | + """ |
| 40 | + super().__init__() |
| 41 | + self.in_dims = in_dims |
| 42 | + self.n_feats = n_feats |
| 43 | + self.input_projection = nn.Linear(in_dims * n_feats, num_channels) |
| 44 | + self.conditioner_projection = nn.Linear(hparams['hidden_size'], num_channels) |
| 45 | + self.diffusion_embedding = nn.Sequential( |
| 46 | + SinusoidalPosEmb(num_channels), |
| 47 | + nn.Linear(num_channels, num_channels * 4), |
| 48 | + nn.GELU(), |
| 49 | + nn.Linear(num_channels * 4, num_channels), |
| 50 | + ) |
| 51 | + self.residual_layers = nn.ModuleList( |
| 52 | + [ |
| 53 | + LYNXNet2Block( |
| 54 | + dim=num_channels, |
| 55 | + expansion_factor=expansion_factor, |
| 56 | + kernel_size=kernel_size, |
| 57 | + dropout=dropout |
| 58 | + ) |
| 59 | + for i in range(num_layers) |
| 60 | + ] |
| 61 | + ) |
| 62 | + self.norm = nn.LayerNorm(num_channels) |
| 63 | + self.output_projection = nn.Linear(num_channels, in_dims * n_feats) |
| 64 | + nn.init.kaiming_normal_(self.input_projection.weight) |
| 65 | + nn.init.kaiming_normal_(self.conditioner_projection.weight) |
| 66 | + nn.init.zeros_(self.output_projection.weight) |
| 67 | + |
| 68 | + def forward(self, spec, diffusion_step, cond): |
| 69 | + """ |
| 70 | + :param spec: [B, F, M, T] |
| 71 | + :param diffusion_step: [B, 1] |
| 72 | + :param cond: [B, H, T] |
| 73 | + :return: |
| 74 | + """ |
| 75 | + |
| 76 | + if self.n_feats == 1: |
| 77 | + x = spec[:, 0] # [B, M, T] |
| 78 | + else: |
| 79 | + x = spec.flatten(start_dim=1, end_dim=2) # [B, F x M, T] |
| 80 | + |
| 81 | + x = self.input_projection(x.transpose(1, 2)) # [B, T, F x M] |
| 82 | + x = x + self.conditioner_projection(cond.transpose(1, 2)) |
| 83 | + x = x + self.diffusion_embedding(diffusion_step).unsqueeze(1) |
| 84 | + |
| 85 | + for layer in self.residual_layers: |
| 86 | + x = layer(x) |
| 87 | + |
| 88 | + # post-norm |
| 89 | + x = self.norm(x) |
| 90 | + |
| 91 | + # output projection |
| 92 | + x = self.output_projection(x).transpose(1, 2) # [B, 128, T] |
| 93 | + |
| 94 | + if self.n_feats == 1: |
| 95 | + x = x[:, None, :, :] |
| 96 | + else: |
| 97 | + # This is the temporary solution since PyTorch 1.13 |
| 98 | + # does not support exporting aten::unflatten to ONNX |
| 99 | + # x = x.unflatten(dim=1, sizes=(self.n_feats, self.in_dims)) |
| 100 | + x = x.reshape(-1, self.n_feats, self.in_dims, x.shape[2]) |
| 101 | + return x |
0 commit comments