Delete src/miocodec/module/convnext.py with huggingface_hub
Browse files- src/miocodec/module/convnext.py +0 -125
src/miocodec/module/convnext.py
DELETED
|
@@ -1,125 +0,0 @@
|
|
| 1 |
-
# Adapted from: https://github.com/gemelo-ai/vocos/blob/main/vocos/models.py
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import torch
|
| 5 |
-
from torch import nn
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
class ConvNeXtBlock(nn.Module):
|
| 9 |
-
"""ConvNeXt Block adapted from https://github.com/facebookresearch/ConvNeXt to 1D audio signal.
|
| 10 |
-
|
| 11 |
-
Args:
|
| 12 |
-
dim (int): Number of input channels.
|
| 13 |
-
intermediate_dim (int): Dimensionality of the intermediate layer.
|
| 14 |
-
layer_scale_init_value (float, optional): Initial value for the layer scale. None means no scaling.
|
| 15 |
-
Defaults to None.
|
| 16 |
-
"""
|
| 17 |
-
|
| 18 |
-
def __init__(
|
| 19 |
-
self,
|
| 20 |
-
dim: int,
|
| 21 |
-
intermediate_dim: int,
|
| 22 |
-
layer_scale_init_value: float,
|
| 23 |
-
):
|
| 24 |
-
super().__init__()
|
| 25 |
-
self.dwconv = nn.Conv1d(dim, dim, kernel_size=7, padding=3, groups=dim) # depthwise conv
|
| 26 |
-
self.norm = nn.LayerNorm(dim, eps=1e-6)
|
| 27 |
-
self.pwconv1 = nn.Linear(dim, intermediate_dim) # pointwise/1x1 convs, implemented with linear layers
|
| 28 |
-
self.act = nn.GELU()
|
| 29 |
-
self.pwconv2 = nn.Linear(intermediate_dim, dim)
|
| 30 |
-
self.gamma = (
|
| 31 |
-
nn.Parameter(layer_scale_init_value * torch.ones(dim), requires_grad=True)
|
| 32 |
-
if layer_scale_init_value > 0
|
| 33 |
-
else None
|
| 34 |
-
)
|
| 35 |
-
|
| 36 |
-
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 37 |
-
residual = x
|
| 38 |
-
x = self.dwconv(x)
|
| 39 |
-
x = x.transpose(1, 2) # (B, C, T) -> (B, T, C)
|
| 40 |
-
x = self.norm(x)
|
| 41 |
-
x = self.pwconv1(x)
|
| 42 |
-
x = self.act(x)
|
| 43 |
-
x = self.pwconv2(x)
|
| 44 |
-
if self.gamma is not None:
|
| 45 |
-
x = self.gamma * x
|
| 46 |
-
x = x.transpose(1, 2) # (B, T, C) -> (B, C, T)
|
| 47 |
-
|
| 48 |
-
x = residual + x
|
| 49 |
-
return x
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
class ConvNextBackbone(nn.Module):
|
| 53 |
-
"""
|
| 54 |
-
Backbone module built with ConvNeXt blocks.
|
| 55 |
-
|
| 56 |
-
Args:
|
| 57 |
-
input_channels (int): Number of input features channels.
|
| 58 |
-
dim (int): Hidden dimension of the model.
|
| 59 |
-
intermediate_dim (int): Intermediate dimension used in ConvNeXtBlock.
|
| 60 |
-
num_layers (int): Number of ConvNeXtBlock layers.
|
| 61 |
-
layer_scale_init_value (float, optional): Initial value for layer scaling. Defaults to `1 / num_layers`.
|
| 62 |
-
"""
|
| 63 |
-
|
| 64 |
-
def __init__(
|
| 65 |
-
self,
|
| 66 |
-
input_channels: int,
|
| 67 |
-
dim: int,
|
| 68 |
-
intermediate_dim: int,
|
| 69 |
-
num_layers: int,
|
| 70 |
-
output_channels: int | None = None,
|
| 71 |
-
layer_scale_init_value: float | None = None,
|
| 72 |
-
skip_embed: bool = False,
|
| 73 |
-
):
|
| 74 |
-
super().__init__()
|
| 75 |
-
self.input_channels = input_channels
|
| 76 |
-
self.output_channels = output_channels
|
| 77 |
-
self.dim = dim
|
| 78 |
-
self.embed = nn.Conv1d(input_channels, dim, kernel_size=7, padding=3) if not skip_embed else nn.Identity()
|
| 79 |
-
self.norm = nn.LayerNorm(dim, eps=1e-6)
|
| 80 |
-
layer_scale_init_value = layer_scale_init_value or 1 / num_layers
|
| 81 |
-
self.convnext = nn.ModuleList(
|
| 82 |
-
[
|
| 83 |
-
ConvNeXtBlock(
|
| 84 |
-
dim=dim,
|
| 85 |
-
intermediate_dim=intermediate_dim,
|
| 86 |
-
layer_scale_init_value=layer_scale_init_value,
|
| 87 |
-
)
|
| 88 |
-
for _ in range(num_layers)
|
| 89 |
-
]
|
| 90 |
-
)
|
| 91 |
-
self.proj_out = nn.Linear(dim, output_channels) if output_channels else nn.Identity()
|
| 92 |
-
self.final_layer_norm = nn.LayerNorm(dim, eps=1e-6)
|
| 93 |
-
self.apply(self._init_weights)
|
| 94 |
-
|
| 95 |
-
@property
|
| 96 |
-
def input_dim(self) -> int:
|
| 97 |
-
return self.input_channels
|
| 98 |
-
|
| 99 |
-
@property
|
| 100 |
-
def output_dim(self) -> int:
|
| 101 |
-
return self.output_channels if self.output_channels else self.dim
|
| 102 |
-
|
| 103 |
-
def _init_weights(self, m):
|
| 104 |
-
if isinstance(m, (nn.Conv1d, nn.Linear)):
|
| 105 |
-
nn.init.trunc_normal_(m.weight, std=0.02)
|
| 106 |
-
nn.init.constant_(m.bias, 0)
|
| 107 |
-
|
| 108 |
-
def forward(self, x: torch.Tensor, **kwargs) -> torch.Tensor:
|
| 109 |
-
"""
|
| 110 |
-
Args:
|
| 111 |
-
x (Tensor): Input tensor of shape (B, L, C), where B is the batch size,
|
| 112 |
-
C denotes output features, and L is the sequence length.
|
| 113 |
-
Returns:
|
| 114 |
-
Tensor: Output of shape (B, L, H), where B is the batch size, L is the sequence length,
|
| 115 |
-
and H denotes the model dimension.
|
| 116 |
-
"""
|
| 117 |
-
x = x.transpose(1, 2) # (B, L, C) -> (B, C, L)
|
| 118 |
-
x = self.embed(x)
|
| 119 |
-
x = self.norm(x.transpose(1, 2))
|
| 120 |
-
x = x.transpose(1, 2)
|
| 121 |
-
for conv_block in self.convnext:
|
| 122 |
-
x = conv_block(x)
|
| 123 |
-
x = self.final_layer_norm(x.transpose(1, 2))
|
| 124 |
-
x = self.proj_out(x)
|
| 125 |
-
return x
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|