From c0b0b61df11177b1b0a5bb3ce80fb9c2f853f60f Mon Sep 17 00:00:00 2001 From: Arthur031221 <124417490+Arthur031221@users.noreply.github.com> Date: Sun, 29 Nov 2026 11:45:52 +0800 Subject: [PATCH 1/3] fix(seaco): filter hotword candidates per utterance in a batch With more than nfilter hotwords, the candidate set was ranked from the first item of the batch and reused for the others, so their hotwords could be dropped. Rank each utterance on its own token positions instead. --- funasr/models/seaco_paraformer/model.py | 16 +++--- tests/test_seaco_hotword_filter_batch.py | 72 ++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 9 deletions(-) create mode 100644 tests/test_seaco_hotword_filter_batch.py diff --git a/funasr/models/seaco_paraformer/model.py b/funasr/models/seaco_paraformer/model.py index c1c3ba55a..e4dfcb792 100644 --- a/funasr/models/seaco_paraformer/model.py +++ b/funasr/models/seaco_paraformer/model.py @@ -324,17 +324,15 @@ def _seaco_decode_with_ASF( hotword_scores = self.seaco_decoder.forward_asf6( contextual_info, _contextual_length, decoder_hidden, ys_pad_lens ) - hotword_scores = hotword_scores[0].sum(0).sum(0) + # rank hotwords per utterance, leaving out padded token positions + token_pad_mask = make_pad_mask(ys_pad_lens, maxlen=hotword_scores.shape[2]) + token_pad_mask = token_pad_mask[:, :, None].to(hotword_scores.device) + hotword_scores = hotword_scores.sum(1).masked_fill(token_pad_mask, 0).sum(1) # hotword_scores /= torch.sqrt(torch.tensor(hw_lengths)[:-1].float()).to(hotword_scores.device) - dec_filter = torch.topk(hotword_scores, min(nfilter, num_hot_word - 1))[1].tolist() - add_filter = dec_filter - add_filter.append(len(hw_list_pad) - 1) + dec_filter = torch.topk(hotword_scores, min(nfilter, num_hot_word - 1))[1] + no_bias = dec_filter.new_full((dec_filter.shape[0], 1), len(hw_list_pad) - 1) # filter hotword embedding - selected = selected[add_filter] - # again - contextual_info = ( - selected.squeeze(0).repeat(encoder_out.shape[0], 1, 1).to(encoder_out.device) - ) + contextual_info = selected[torch.cat([dec_filter, no_bias], dim=1)] num_hot_word = contextual_info.shape[1] _contextual_length = ( torch.Tensor([num_hot_word]) diff --git a/tests/test_seaco_hotword_filter_batch.py b/tests/test_seaco_hotword_filter_batch.py new file mode 100644 index 000000000..39313749b --- /dev/null +++ b/tests/test_seaco_hotword_filter_batch.py @@ -0,0 +1,72 @@ +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]) + + +if __name__ == "__main__": + unittest.main() From bafa87a65e71409c12ccce5d08b100e917a17f1f Mon Sep 17 00:00:00 2001 From: Arthur031221 <124417490+Arthur031221@users.noreply.github.com> Date: Wed, 2 Dec 2026 09:06:16 +0800 Subject: [PATCH 2/3] fix(seaco): keep the no-bias entry out of the hotword top-k The no-bias embedding is the last row, it competed with the user hotwords for a top-k slot and was then appended again. With exactly nfilter hotwords one of them was dropped and no-bias appeared twice. Rank only the real hotwords and append no-bias once. Co-authored-by: LauraGPT <18321252+LauraGPT@users.noreply.github.com> --- funasr/models/seaco_paraformer/model.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/funasr/models/seaco_paraformer/model.py b/funasr/models/seaco_paraformer/model.py index e4dfcb792..b5b8ffc28 100644 --- a/funasr/models/seaco_paraformer/model.py +++ b/funasr/models/seaco_paraformer/model.py @@ -329,7 +329,8 @@ def _seaco_decode_with_ASF( token_pad_mask = token_pad_mask[:, :, None].to(hotword_scores.device) hotword_scores = hotword_scores.sum(1).masked_fill(token_pad_mask, 0).sum(1) # hotword_scores /= torch.sqrt(torch.tensor(hw_lengths)[:-1].float()).to(hotword_scores.device) - dec_filter = torch.topk(hotword_scores, min(nfilter, num_hot_word - 1))[1] + # Reserve the final no-bias entry; only genuine hotwords compete for top-k. + dec_filter = hotword_scores[:, :-1].topk(nfilter, dim=-1).indices no_bias = dec_filter.new_full((dec_filter.shape[0], 1), len(hw_list_pad) - 1) # filter hotword embedding contextual_info = selected[torch.cat([dec_filter, no_bias], dim=1)] From c88167eca568796abc128f5ecf61203bec6e8f64 Mon Sep 17 00:00:00 2001 From: Arthur031221 <124417490+Arthur031221@users.noreply.github.com> Date: Wed, 2 Dec 2026 09:06:27 +0800 Subject: [PATCH 3/3] test(seaco): no-bias entry does not take a real hotword slot Co-authored-by: LauraGPT <18321252+LauraGPT@users.noreply.github.com> --- tests/test_seaco_hotword_filter_batch.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_seaco_hotword_filter_batch.py b/tests/test_seaco_hotword_filter_batch.py index 39313749b..270764d61 100644 --- a/tests/test_seaco_hotword_filter_batch.py +++ b/tests/test_seaco_hotword_filter_batch.py @@ -67,6 +67,12 @@ def test_each_utterance_keeps_its_own_hotword_candidates(self): 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()