File size: 15,754 Bytes
22d5f88 |
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 |
# Copyright (c) Kyutai, all rights reserved.
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
import typing as tp
import numpy as np
import torch.nn as nn
from .conv import StreamingConv1d, StreamingConvTranspose1d
from .streaming import StreamingContainer, StreamingAdd
from ..utils.compile import torch_compile_lazy
class SEANetResnetBlock(StreamingContainer):
"""Residual block from SEANet model.
Args:
dim (int): Dimension of the input/output.
kernel_sizes (list): List of kernel sizes for the convolutions.
dilations (list): List of dilations for the convolutions.
activation (str): Activation function.
activation_params (dict): Parameters to provide to the activation function.
norm (str): Normalization method.
norm_params (dict): Parameters to provide to the underlying normalization used along with the convolution.
causal (bool): Whether to use fully causal convolution.
pad_mode (str): Padding mode for the convolutions.
compress (int): Reduced dimensionality in residual branches (from Demucs v3).
true_skip (bool): Whether to use true skip connection or a simple
(streamable) convolution as the skip connection.
"""
def __init__(
self,
dim: int,
kernel_sizes: tp.List[int] = [3, 1],
dilations: tp.List[int] = [1, 1],
activation: str = "ELU",
activation_params: dict = {"alpha": 1.0},
norm: str = "none",
norm_params: tp.Dict[str, tp.Any] = {},
causal: bool = False,
pad_mode: str = "reflect",
compress: int = 2,
true_skip: bool = True,
):
super().__init__()
assert len(kernel_sizes) == len(
dilations
), "Number of kernel sizes should match number of dilations"
act = getattr(nn, activation)
hidden = dim // compress
block = []
for i, (kernel_size, dilation) in enumerate(zip(kernel_sizes, dilations)):
in_chs = dim if i == 0 else hidden
out_chs = dim if i == len(kernel_sizes) - 1 else hidden
block += [
act(**activation_params),
StreamingConv1d(
in_chs,
out_chs,
kernel_size=kernel_size,
dilation=dilation,
norm=norm,
norm_kwargs=norm_params,
causal=causal,
pad_mode=pad_mode,
),
]
self.block = nn.Sequential(*block)
self.add = StreamingAdd()
self.shortcut: nn.Module
if true_skip:
self.shortcut = nn.Identity()
else:
self.shortcut = StreamingConv1d(
dim,
dim,
kernel_size=1,
norm=norm,
norm_kwargs=norm_params,
causal=causal,
pad_mode=pad_mode,
)
def forward(self, x):
u, v = self.shortcut(x), self.block(x)
return self.add(u, v)
class SEANetEncoder(StreamingContainer):
"""SEANet encoder.
Args:
channels (int): Audio channels.
dimension (int): Intermediate representation dimension.
n_filters (int): Base width for the model.
n_residual_layers (int): nb of residual layers.
ratios (Sequence[int]): kernel size and stride ratios. The encoder uses downsampling ratios instead of
upsampling ratios, hence it will use the ratios in the reverse order to the ones specified here
that must match the decoder order. We use the decoder order as some models may only employ the decoder.
activation (str): Activation function.
activation_params (dict): Parameters to provide to the activation function.
norm (str): Normalization method.
norm_params (dict): Parameters to provide to the underlying normalization used along with the convolution.
kernel_size (int): Kernel size for the initial convolution.
last_kernel_size (int): Kernel size for the initial convolution.
residual_kernel_size (int): Kernel size for the residual layers.
dilation_base (int): How much to increase the dilation with each layer.
causal (bool): Whether to use fully causal convolution.
pad_mode (str): Padding mode for the convolutions.
true_skip (bool): Whether to use true skip connection or a simple
(streamable) convolution as the skip connection in the residual network blocks.
compress (int): Reduced dimensionality in residual branches (from Demucs v3).
disable_norm_outer_blocks (int): Number of blocks for which we don't apply norm.
For the encoder, it corresponds to the N first blocks.
mask_fn (nn.Module): Optional mask function to apply after convolution layers.
mask_position (int): Position of the mask function, with mask_position == 0 for the first convolution layer,
mask_position == 1 for the first conv block, etc.
"""
def __init__(
self,
channels: int = 1,
dimension: int = 128,
n_filters: int = 32,
n_residual_layers: int = 3,
ratios: tp.List[int] = [8, 5, 4, 2],
activation: str = "ELU",
activation_params: dict = {"alpha": 1.0},
norm: str = "none",
norm_params: tp.Dict[str, tp.Any] = {},
kernel_size: int = 7,
last_kernel_size: int = 7,
residual_kernel_size: int = 3,
dilation_base: int = 2,
causal: bool = False,
pad_mode: str = "reflect",
true_skip: bool = True,
compress: int = 2,
disable_norm_outer_blocks: int = 0,
mask_fn: tp.Optional[nn.Module] = None,
mask_position: tp.Optional[int] = None,
):
super().__init__()
self.channels = channels
self.dimension = dimension
self.n_filters = n_filters
self.ratios = list(reversed(ratios))
del ratios
self.n_residual_layers = n_residual_layers
self.hop_length = int(np.prod(self.ratios))
self.n_blocks = len(self.ratios) + 2 # first and last conv + residual blocks
self.disable_norm_outer_blocks = disable_norm_outer_blocks
assert (
self.disable_norm_outer_blocks >= 0 and self.disable_norm_outer_blocks <= self.n_blocks
), (
"Number of blocks for which to disable norm is invalid."
"It should be lower or equal to the actual number of blocks in the network and greater or equal to 0."
)
act = getattr(nn, activation)
mult = 1
model: tp.List[nn.Module] = [
StreamingConv1d(
channels,
mult * n_filters,
kernel_size,
norm="none" if self.disable_norm_outer_blocks >= 1 else norm,
norm_kwargs=norm_params,
causal=causal,
pad_mode=pad_mode,
)
]
if mask_fn is not None and mask_position == 0:
model += [mask_fn]
# Downsample to raw audio scale
for i, ratio in enumerate(self.ratios):
block_norm = "none" if self.disable_norm_outer_blocks >= i + 2 else norm
# Add residual layers
for j in range(n_residual_layers):
model += [
SEANetResnetBlock(
mult * n_filters,
kernel_sizes=[residual_kernel_size, 1],
dilations=[dilation_base**j, 1],
norm=block_norm,
norm_params=norm_params,
activation=activation,
activation_params=activation_params,
causal=causal,
pad_mode=pad_mode,
compress=compress,
true_skip=true_skip,
)
]
# Add downsampling layers
model += [
act(**activation_params),
StreamingConv1d(
mult * n_filters,
mult * n_filters * 2,
kernel_size=ratio * 2,
stride=ratio,
norm=block_norm,
norm_kwargs=norm_params,
causal=causal,
pad_mode=pad_mode,
),
]
mult *= 2
if mask_fn is not None and mask_position == i + 1:
model += [mask_fn]
model += [
act(**activation_params),
StreamingConv1d(
mult * n_filters,
dimension,
last_kernel_size,
norm=(
"none" if self.disable_norm_outer_blocks == self.n_blocks else norm
),
norm_kwargs=norm_params,
causal=causal,
pad_mode=pad_mode,
),
]
self.model = nn.Sequential(*model)
@torch_compile_lazy
def forward(self, x):
return self.model(x)
class SEANetDecoder(StreamingContainer):
"""SEANet decoder.
Args:
channels (int): Audio channels.
dimension (int): Intermediate representation dimension.
n_filters (int): Base width for the model.
n_residual_layers (int): nb of residual layers.
ratios (Sequence[int]): kernel size and stride ratios.
activation (str): Activation function.
activation_params (dict): Parameters to provide to the activation function.
final_activation (str): Final activation function after all convolutions.
final_activation_params (dict): Parameters to provide to the activation function.
norm (str): Normalization method.
norm_params (dict): Parameters to provide to the underlying normalization used along with the convolution.
kernel_size (int): Kernel size for the initial convolution.
last_kernel_size (int): Kernel size for the initial convolution.
residual_kernel_size (int): Kernel size for the residual layers.
dilation_base (int): How much to increase the dilation with each layer.
causal (bool): Whether to use fully causal convolution.
pad_mode (str): Padding mode for the convolutions.
true_skip (bool): Whether to use true skip connection or a simple.
(streamable) convolution as the skip connection in the residual network blocks.
compress (int): Reduced dimensionality in residual branches (from Demucs v3).
disable_norm_outer_blocks (int): Number of blocks for which we don't apply norm.
For the decoder, it corresponds to the N last blocks.
trim_right_ratio (float): Ratio for trimming at the right of the transposed convolution under the causal setup.
If equal to 1.0, it means that all the trimming is done at the right.
"""
def __init__(
self,
channels: int = 1,
dimension: int = 128,
n_filters: int = 32,
n_residual_layers: int = 3,
ratios: tp.List[int] = [8, 5, 4, 2],
activation: str = "ELU",
activation_params: dict = {"alpha": 1.0},
final_activation: tp.Optional[str] = None,
final_activation_params: tp.Optional[dict] = None,
norm: str = "none",
norm_params: tp.Dict[str, tp.Any] = {},
kernel_size: int = 7,
last_kernel_size: int = 7,
residual_kernel_size: int = 3,
dilation_base: int = 2,
causal: bool = False,
pad_mode: str = "reflect",
true_skip: bool = True,
compress: int = 2,
disable_norm_outer_blocks: int = 0,
trim_right_ratio: float = 1.0,
):
super().__init__()
self.dimension = dimension
self.channels = channels
self.n_filters = n_filters
self.ratios = ratios
del ratios
self.n_residual_layers = n_residual_layers
self.hop_length = int(np.prod(self.ratios))
self.n_blocks = len(self.ratios) + 2 # first and last conv + residual blocks
self.disable_norm_outer_blocks = disable_norm_outer_blocks
assert (
self.disable_norm_outer_blocks >= 0 and self.disable_norm_outer_blocks <= self.n_blocks
), (
"Number of blocks for which to disable norm is invalid."
"It should be lower or equal to the actual number of blocks in the network and greater or equal to 0."
)
act = getattr(nn, activation)
mult = int(2 ** len(self.ratios))
model: tp.List[nn.Module] = [
StreamingConv1d(
dimension,
mult * n_filters,
kernel_size,
norm=(
"none" if self.disable_norm_outer_blocks == self.n_blocks else norm
),
norm_kwargs=norm_params,
causal=causal,
pad_mode=pad_mode,
)
]
# Upsample to raw audio scale
for i, ratio in enumerate(self.ratios):
block_norm = (
"none"
if self.disable_norm_outer_blocks >= self.n_blocks - (i + 1)
else norm
)
# Add upsampling layers
model += [
act(**activation_params),
StreamingConvTranspose1d(
mult * n_filters,
mult * n_filters // 2,
kernel_size=ratio * 2,
stride=ratio,
norm=block_norm,
norm_kwargs=norm_params,
causal=causal,
trim_right_ratio=trim_right_ratio,
),
]
# Add residual layers
for j in range(n_residual_layers):
model += [
SEANetResnetBlock(
mult * n_filters // 2,
kernel_sizes=[residual_kernel_size, 1],
dilations=[dilation_base**j, 1],
activation=activation,
activation_params=activation_params,
norm=block_norm,
norm_params=norm_params,
causal=causal,
pad_mode=pad_mode,
compress=compress,
true_skip=true_skip,
)
]
mult //= 2
# Add final layers
model += [
act(**activation_params),
StreamingConv1d(
n_filters,
channels,
last_kernel_size,
norm="none" if self.disable_norm_outer_blocks >= 1 else norm,
norm_kwargs=norm_params,
causal=causal,
pad_mode=pad_mode,
),
]
# Add optional final activation to decoder (eg. tanh)
if final_activation is not None:
final_act = getattr(nn, final_activation)
final_activation_params = final_activation_params or {}
model += [final_act(**final_activation_params)]
self.model = nn.Sequential(*model)
@torch_compile_lazy
def forward(self, z):
y = self.model(z)
return y
|