Repository navigation
fix(seaco): filter hotword candidates per utterance in a batch #3977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Arthur031221
wants to merge
3
commits into
modelscope:main
Choose a base branch
from
Arthur031221:fix/seaco-hotword-filter-per-utterance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,78 @@ | ||
| import unittest | ||
|
|
||
| import torch | ||
|
|
||
| from funasr.models.seaco_paraformer.model import SeacoParaformer | ||
|
|
||
| NUM_HOTWORDS = 6 # the last entry is the no-bias hotword | ||
|
|
||
|
|
||
| class _AttentionStub(torch.nn.Module): | ||
| """SeACo decoder stand-in: attention is the softmax of token/hotword dot products.""" | ||
|
|
||
| def __init__(self): | ||
| super().__init__() | ||
| self.seen = [] | ||
|
|
||
| def forward_asf6(self, contextual_info, contextual_length, tokens, token_lengths): | ||
| return torch.softmax(tokens @ contextual_info.transpose(1, 2), dim=-1).unsqueeze(1) | ||
|
|
||
| def forward(self, contextual_info, contextual_length, tokens, token_lengths): | ||
| self.seen.append((contextual_info, contextual_length)) | ||
| return torch.zeros_like(tokens), None | ||
|
|
||
|
|
||
| class _Seaco(SeacoParaformer): | ||
| def __init__(self): | ||
| torch.nn.Module.__init__(self) | ||
| self.seaco_decoder = _AttentionStub() | ||
| self.hotword_output_layer = torch.nn.Identity() | ||
| self.NO_BIAS = 0 | ||
|
|
||
| def decoder(self, encoder_out, encoder_out_lens, tokens, token_lengths, **kwargs): | ||
| return torch.zeros_like(tokens), tokens, None | ||
|
|
||
| def _hotword_representation(self, hotword_pad, hotword_lengths): | ||
| return torch.eye(NUM_HOTWORDS) | ||
|
|
||
|
|
||
| def _candidates(utterances): | ||
| """Run ASF on utterances given as lists of the hotword index each token attends to.""" | ||
| model = _Seaco() | ||
| lengths = torch.tensor([len(u) for u in utterances]) | ||
| # padded positions point at hotword 4, which no utterance contains | ||
| tokens = 5.0 * torch.eye(NUM_HOTWORDS)[4].repeat(len(utterances), int(lengths.max()), 1) | ||
| for i, utterance in enumerate(utterances): | ||
| tokens[i, : len(utterance)] = 5.0 * torch.eye(NUM_HOTWORDS)[utterance] | ||
| model._seaco_decode_with_ASF( | ||
| torch.zeros(len(utterances), 1, 1), | ||
| torch.ones(len(utterances)), | ||
| tokens, | ||
| lengths, | ||
| hw_list=[[1]] * NUM_HOTWORDS, | ||
| nfilter=2, | ||
| ) | ||
| contextual_info, contextual_length = model.seaco_decoder.seen[-1] | ||
| assert contextual_length.tolist() == [3] * len(utterances) | ||
| # the stub hotword embeddings are one-hot, so argmax recovers the hotword index | ||
| return [sorted(row) for row in contextual_info.argmax(-1).tolist()] | ||
|
|
||
|
|
||
| class TestSeacoHotwordFilterBatch(unittest.TestCase): | ||
| def test_each_utterance_keeps_its_own_hotword_candidates(self): | ||
| short = [0, 1] | ||
| long = [2, 2, 2, 3, 3, 3] | ||
| alone = _candidates([short]) + _candidates([long]) | ||
| self.assertEqual(alone, [[0, 1, 5], [2, 3, 5]]) | ||
| self.assertEqual(_candidates([short, long]), alone) | ||
| self.assertEqual(_candidates([long, short]), alone[::-1]) | ||
|
|
||
| def test_no_bias_does_not_consume_a_real_hotword_slot(self): | ||
| # Entry 5 is no-bias. It ranks first, but is appended separately. | ||
| first = [5, 5, 5, 1, 0] | ||
| second = [5, 5, 2, 3] | ||
| self.assertEqual(_candidates([first, second]), [[0, 1, 5], [2, 3, 5]]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both utterances below give the no-bias entry a high attention score; each must still retain two real hotwords.