MCPcopy Create free account
hub / github.com/FireRedTeam/FireRedTTS2 / WhisperEncoder

Class WhisperEncoder

fireredtts2/codec/whisper.py:195–272  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

193
194
195class WhisperEncoder(nn.Module):
196 def __init__(
197 self,
198 in_dim: int,
199 embed_dim: int,
200 num_layers: int,
201 num_heads: int,
202 ffn_dim: int = None,
203 attn_dropout: float = 0.0,
204 dropout: float = 0.0,
205 max_positions: int = 1500,
206 ):
207 super().__init__()
208 self.in_dim = in_dim
209 self.embed_dim = embed_dim
210 self.dropout = dropout
211 # Input downsampling
212 self.conv1 = nn.Conv1d(in_dim, embed_dim, kernel_size=3, padding=1)
213 self.conv2 = nn.Conv1d(embed_dim, embed_dim, kernel_size=3, stride=2, padding=1)
214 # Fixed positional embedding
215 self.max_positions = max_positions
216 self.embed_positions = nn.Embedding(self.max_positions, embed_dim)
217 self.embed_positions.requires_grad_(False)
218 # Transformer
219 self.layers = nn.ModuleList(
220 [
221 WhisperEncoderLayer(
222 embed_dim, num_heads, ffn_dim, attn_dropout, dropout
223 )
224 for _ in range(num_layers)
225 ]
226 )
227 # Output norm
228 self.layer_norm = nn.LayerNorm(embed_dim)
229 # Init weight
230 self.apply(self._init_weights)
231 # Init position embedding
232 self.embed_positions.weight.copy_(sinusoids(*self.embed_positions.weight.shape))
233
234 def forward(
235 self,
236 hidden_states: torch.Tensor,
237 hidden_length: torch.Tensor,
238 apply_position: bool = True,
239 ):
240 # Downsampling
241 hidden_states = hidden_states.transpose(1, 2)
242 hidden_states = F.gelu(self.conv1(hidden_states))
243 hidden_states = F.gelu(self.conv2(hidden_states))
244 hidden_states = hidden_states.transpose(1, 2)
245 hidden_length = hidden_length // 2 # from 100Hz -> 50Hz
246 # Pos encoding
247 if apply_position:
248 pos_embed = self.embed_positions(
249 torch.arange(0, hidden_states.shape[1], device=hidden_states.device)
250 )
251 hidden_states = hidden_states + pos_embed
252 hidden_states = nn.functional.dropout(

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected