Download models/convnext.py from ViuAI/TTS_10M: direct link, hf CLI and curl.
- Browser
- Download file 2.89 kB
-
https://huggingface.co/ViuAI/TTS_10M/resolve/main/models/convnext.py
- Command line
-
hf download hf://ViuAI/TTS_10M/models/convnext.py
-
curl -L -o convnext.py https://huggingface.co/ViuAI/TTS_10M/resolve/main/models/convnext.py
2.89 kB
| """ | |
| ConvNeXt-V2 1D Blocks with Global Response Normalization (GRN) | |
| State-of-the-Art modern CNN backbone for audio/spectrogram modeling. | |
| Mask-aware so variable-length batch padding never leaks into valid frames. | |
| """ | |
| import torch | |
| import torch.nn as nn | |
| from typing import Optional | |
| class ChannelLayerNorm1d(nn.Module): | |
| """Per-timestep LayerNorm across the channel dimension (B, C, T) -> zero padding leakage.""" | |
| def __init__(self, dim: int, eps: float = 1e-6): | |
| super().__init__() | |
| self.norm = nn.LayerNorm(dim, eps=eps) | |
| def forward(self, x: torch.Tensor) -> torch.Tensor: | |
| return self.norm(x.transpose(1, 2)).transpose(1, 2) | |
| class GRN1D(nn.Module): | |
| """Mask-aware Global Response Normalization (GRN) for 1D channels.""" | |
| def __init__(self, dim: int, eps: float = 1e-6): | |
| super().__init__() | |
| self.gamma = nn.Parameter(torch.zeros(1, dim, 1)) | |
| self.beta = nn.Parameter(torch.zeros(1, dim, 1)) | |
| self.eps = eps | |
| def forward(self, x: torch.Tensor, mask: Optional[torch.Tensor] = None) -> torch.Tensor: | |
| # x: (B, C, T), mask: (B, 1, T) | |
| if mask is not None: | |
| x = x * mask | |
| gx = torch.norm(x, p=2, dim=-1, keepdim=True) # (B, C, 1) | |
| nx = gx / (gx.mean(dim=1, keepdim=True) + self.eps) | |
| out = self.gamma * (x * nx) + self.beta + x | |
| if mask is not None: | |
| out = out * mask | |
| return out | |
| class ConvNeXtV2Block1D(nn.Module): | |
| """ | |
| 1D ConvNeXt-V2 Block: | |
| - Depthwise 7x7 Conv1D | |
| - Per-timestep Channel LayerNorm | |
| - Pointwise Conv 1x1 (expansion 4x) | |
| - GELU activation | |
| - Mask-aware GRN (Global Response Normalization) | |
| - Pointwise Conv 1x1 (projection back) | |
| - Residual connection | |
| """ | |
| def __init__(self, dim: int, intermediate_dim: int = None, kernel_size: int = 7, dropout: float = 0.0): | |
| super().__init__() | |
| if intermediate_dim is None: | |
| intermediate_dim = 4 * dim | |
| self.dwconv = nn.Conv1d( | |
| dim, dim, kernel_size=kernel_size, padding=kernel_size // 2, groups=dim | |
| ) | |
| self.norm = ChannelLayerNorm1d(dim) | |
| self.pwconv1 = nn.Conv1d(dim, intermediate_dim, kernel_size=1) | |
| self.act = nn.GELU() | |
| self.grn = GRN1D(intermediate_dim) | |
| self.pwconv2 = nn.Conv1d(intermediate_dim, dim, kernel_size=1) | |
| self.drop = nn.Dropout(dropout) if dropout > 0 else nn.Identity() | |
| def forward(self, x: torch.Tensor, mask: Optional[torch.Tensor] = None) -> torch.Tensor: | |
| residual = x | |
| if mask is not None: | |
| x = x * mask | |
| x = self.dwconv(x) | |
| x = self.norm(x) | |
| x = self.pwconv1(x) | |
| x = self.act(x) | |
| x = self.grn(x, mask=mask) | |
| x = self.pwconv2(x) | |
| x = self.drop(x) | |
| out = residual + x | |
| if mask is not None: | |
| out = out * mask | |
| return out | |