File size: 5,650 Bytes
4fe4e70 9c8f79c 4fe4e70 | 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 | """
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)
|