| 331 | |
| 332 | # Pretrained encoder from whisper-large-v3 |
| 333 | class PretrainedWhisperEncoder(WhisperEncoder): |
| 334 | @classmethod |
| 335 | def from_pretrained(cls, pretrained_path: str = None): |
| 336 | encoder = cls( |
| 337 | in_dim=128, |
| 338 | embed_dim=1280, |
| 339 | num_layers=32, |
| 340 | num_heads=20, |
| 341 | ffn_dim=5120, |
| 342 | attn_dropout=0.0, |
| 343 | max_positions=1500, |
| 344 | ) |
| 345 | if pretrained_path is not None: |
| 346 | ckpt = torch.load(pretrained_path, map_location="cpu") |
| 347 | encoder.load_state_dict(ckpt) |
| 348 | encoder.eval() |
| 349 | # Disable grad |
| 350 | for p in encoder.parameters(): |
| 351 | p.requires_grad_(False) |
| 352 | # Add Mel extractor |
| 353 | encoder.feature_extractor = WhisperMelExtractor( |
| 354 | num_mels=128, |
| 355 | sampling_rate=16000, |
| 356 | hop_length=160, |
| 357 | n_fft=400, |
| 358 | fmin=0, |
| 359 | fmax=8000, |
| 360 | ) |
| 361 | return encoder |
| 362 | |
| 363 | @torch.inference_mode() |
| 364 | def forward(self, audio16k: torch.Tensor, audio16k_length: torch.Tensor): |
| 365 | # Extract mel |
| 366 | mel, mel_length = self.feature_extractor(audio16k, audio16k_length) |
| 367 | # Forward model |
| 368 | semantic_feats, semantic_length = super().forward( |
| 369 | mel, mel_length, apply_position=True |
| 370 | ) |
| 371 | return semantic_feats, semantic_length |
| 372 | |
| 373 | |
| 374 | class WhisperAcousticEncoder(WhisperEncoder): |
nothing calls this directly
no outgoing calls
no test coverage detected