-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
using monkey patch to replace models
- Loading branch information
1 parent
84e4af9
commit bda4829
Showing
5 changed files
with
103 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
egs/aishell/ASR/whisper/whisper_encoder_forward_monkey_patch.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import torch | ||
import whisper | ||
|
||
def forward(self, x: torch.Tensor): | ||
""" | ||
x : torch.Tensor, shape = (batch_size, n_mels, n_ctx) | ||
the mel spectrogram of the audio | ||
""" | ||
x = F.gelu(self.conv1(x)) | ||
x = F.gelu(self.conv2(x)) | ||
x = x.permute(0, 2, 1) | ||
|
||
x = (x + self.positional_embedding[:x.shape[1],:]).to(x.dtype) | ||
|
||
for block in self.blocks: | ||
x = block(x) | ||
|
||
x = self.ln_post(x) | ||
return x | ||
|
||
def replace_whisper_encoder_forward(): | ||
""" | ||
This function monkey patches the forward method of the whisper encoder. | ||
To be called before the model is loaded, it changes whisper to process audio with any length < 30s. | ||
""" | ||
whisper.model.AudioEncoder.forward = forward |