ViuAI_TTS_200M / models /text_encoder.py
ViuAI's picture
Update: training fixes, LICENSE + requirements + scripts
9c8f79c verified
Raw History Blame Contribute Delete
5.65 kB
"""
Text & Prosody Encoder for ViuAI_TTS_200M.
Modern architecture utilizing Rotary Position Embeddings (RoPE) and Pre-LN Transformer blocks.
"""
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from typing import Optional, Tuple
class RotaryEmbedding(nn.Module):
"""Rotary Position Embedding (RoPE) for sequence modeling."""
def __init__(self, dim: int, max_seq_len: int = 4096, base: float = 10000.0):
super().__init__()
self.dim = dim
inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2).float() / dim))
self.register_buffer("inv_freq", inv_freq, persistent=False)
t = torch.arange(max_seq_len, dtype=torch.float)
freqs = torch.einsum("i,j->ij", t, inv_freq)
emb = torch.cat((freqs, freqs), dim=-1)
self.register_buffer("cos_cached", emb.cos(), persistent=False)
self.register_buffer("sin_cached", emb.sin(), persistent=False)
def _rotate_half(self, x: torch.Tensor) -> torch.Tensor:
x1 = x[..., : x.shape[-1] // 2]
x2 = x[..., x.shape[-1] // 2 :]
return torch.cat((-x2, x1), dim=-1)
def forward(self, x: torch.Tensor, seq_len: int) -> Tuple[torch.Tensor, torch.Tensor]:
return (
self.cos_cached[:seq_len, :].unsqueeze(0),
self.sin_cached[:seq_len, :].unsqueeze(0),
)
def apply_rope(self, x: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor) -> torch.Tensor:
# x: [batch, seq_len, num_heads, head_dim]
return (x * cos.unsqueeze(2)) + (self._rotate_half(x) * sin.unsqueeze(2))
class RoPEMultiheadAttention(nn.Module):
def __init__(self, hidden_dim: int, num_heads: int, dropout: float = 0.1):
super().__init__()
self.hidden_dim = hidden_dim
self.num_heads = num_heads
self.head_dim = hidden_dim // num_heads
assert hidden_dim % num_heads == 0
self.q_proj = nn.Linear(hidden_dim, hidden_dim, bias=False)
self.k_proj = nn.Linear(hidden_dim, hidden_dim, bias=False)
self.v_proj = nn.Linear(hidden_dim, hidden_dim, bias=False)
self.out_proj = nn.Linear(hidden_dim, hidden_dim, bias=False)
self.dropout = nn.Dropout(dropout)
def forward(
self,
x: torch.Tensor,
rope: RotaryEmbedding,
mask: Optional[torch.Tensor] = None,
) -> torch.Tensor:
B, T, C = x.shape
q = self.q_proj(x).view(B, T, self.num_heads, self.head_dim)
k = self.k_proj(x).view(B, T, self.num_heads, self.head_dim)
v = self.v_proj(x).view(B, T, self.num_heads, self.head_dim)
cos, sin = rope(x, T)
q = rope.apply_rope(q, cos, sin)
k = rope.apply_rope(k, cos, sin)
q = q.transpose(1, 2) # [B, H, T, D]
k = k.transpose(1, 2)
v = v.transpose(1, 2)
# Scaled dot-product attention
scores = torch.matmul(q, k.transpose(-2, -1)) / math.sqrt(self.head_dim)
if mask is not None:
scores = scores.masked_fill(mask.unsqueeze(1).unsqueeze(2) == 0, -1e9)
attn_weights = F.softmax(scores, dim=-1)
attn_weights = self.dropout(attn_weights)
out = torch.matmul(attn_weights, v) # [B, H, T, D]
out = out.transpose(1, 2).contiguous().view(B, T, C)
return self.out_proj(out)
class TransformerEncoderBlock(nn.Module):
def __init__(self, hidden_dim: int, num_heads: int, ffn_dim: int, dropout: float = 0.1):
super().__init__()
self.norm1 = nn.LayerNorm(hidden_dim)
self.attn = RoPEMultiheadAttention(hidden_dim, num_heads, dropout)
self.norm2 = nn.LayerNorm(hidden_dim)
self.ffn = nn.Sequential(
nn.Linear(hidden_dim, ffn_dim),
nn.GELU(),
nn.Dropout(dropout),
nn.Linear(ffn_dim, hidden_dim),
nn.Dropout(dropout),
)
def forward(
self,
x: torch.Tensor,
rope: RotaryEmbedding,
mask: Optional[torch.Tensor] = None,
) -> torch.Tensor:
h = self.attn(self.norm1(x), rope, mask)
x = x + h
x = x + self.ffn(self.norm2(x))
return x
class TextEncoder(nn.Module):
"""
High-Capacity Text & Prosody Encoder for ViuAI_TTS_200M.
Parameters: ~22 Million
"""
def __init__(
self,
vocab_size: int = 224,
hidden_dim: int = 512,
num_layers: int = 6,
num_heads: int = 8,
ffn_dim: int = 2048,
dropout: float = 0.1,
):
super().__init__()
self.embedding = nn.Embedding(vocab_size, hidden_dim)
self.rope = RotaryEmbedding(hidden_dim // num_heads)
self.layers = nn.ModuleList([
TransformerEncoderBlock(hidden_dim, num_heads, ffn_dim, dropout)
for _ in range(num_layers)
])
self.final_norm = nn.LayerNorm(hidden_dim)
self.proj_out = nn.Linear(hidden_dim, hidden_dim)
def forward(
self,
text_tokens: torch.Tensor,
lengths: Optional[torch.Tensor] = None,
) -> torch.Tensor:
"""
Args:
text_tokens: [Batch, SeqLen] (Token IDs)
lengths: [Batch] (Actual lengths without padding)
Returns:
text_latents: [Batch, SeqLen, HiddenDim]
"""
B, T = text_tokens.shape
mask = None
if lengths is not None:
mask = torch.arange(T, device=text_tokens.device).unsqueeze(0) < lengths.unsqueeze(1)
x = self.embedding(text_tokens)
for layer in self.layers:
x = layer(x, self.rope, mask)
x = self.final_norm(x)
return self.proj_out(x)