Skip to content

fix(postprocess): keep a trailing @@ piece instead of dropping it - #3836

Merged
LauraGPT merged 3 commits into
modelscope:mainfrom
ANDROIDHASSAN:fix/keep-trailing-bpe-piece
Nov 25, 2026
Merged

LauraGPT merged 3 commits into
modelscope:mainfrom
ANDROIDHASSAN:fix/keep-trailing-bpe-piece

Conversation

@ANDROIDHASSAN

@ANDROIDHASSAN ANDROIDHASSAN commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Problem

sentence_postprocess collects a BPE word in word_item until a token without @@ closes it. When the token list ends on a @@ piece, nothing closes it and the piece is dropped:

>>> sentence_postprocess(["hello", "wor@@"])
('hello', ['hello'])
>>> sentence_postprocess(["你", "好", "wor@@"])
('你好', ['你', '好'])

Streaming Paraformer hits this when a word spans two chunks: paraformer_streaming/model.py and funasr_onnx/paraformer_online_bin.py postprocess each chunk on its own, so the first part of that word disappears from the transcript.

The buffer is also left open across a token that cannot continue the word. On main, ["hel@@", "你"] drops hel and gives 你 the span [0, 100], and ["hel@@", "你", "wor@@", "ld"] becomes 你helworld.

Fix

  • In the mixed branch, an open @@ word is closed before a Chinese or symbol token, at its own place and with its own [begin, end].
  • After the loop, a non-empty word_item is added as a word in the same way.

The same lines go into all three copies of postprocess_utils.py (funasr, funasr_onnx, funasr_torch). Token lists without an open @@ piece are unchanged.

Tests

Added to tests/test_sentence_postprocess_timestamps.py, over all three copies:

  • trailing: ["hello", "wor@@"], ["你", "好", "wor@@"], and the timestamped ["hello", "wor@@", "l@@"];
  • boundaries, timed and untimed: ["hel@@", "你"], ["hel@@", "l@@", "你"], ["hel@@", "你", "wor@@"], ["hel@@", "你", "wor@@", "ld"], ["hel@@", "%"].

Without the source change each boundary case fails in each copy, timed and untimed. With the trailing flush alone (84c7b76) they fail too. With both, on b5c05ab with main merged, test_postprocess_hotwords.py, test_rich_transcription_postprocess.py and test_sentence_postprocess_timestamps.py: 47 passed, 6 skipped, 96 subtests passed (the 6 fuzzy hotword tests need pypinyin/rapidfuzz).

Scope

This does not join a word across streaming chunks: ["wor@@"] then ["ld"] gives wor and ld as two words, because each call is stateless.

A token list that ends on a BPE piece (a streaming chunk that ends inside
a word) left the piece in word_item and never emitted it.

@LauraGPT LauraGPT left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trailing-only examples reproduce on main, and all 32 selected text-postprocessing tests pass with this patch. However, the unconditional flush also emits an older buffered piece after a later Chinese token, so the mixed-text case called out as unchanged in the PR is changed by these lines.

At 84c7b76b5622caf803f7838be6e908769b22f417, in all three copies:

sentence_postprocess(["hel@@", "你"])
# ('你hel', ['你', 'hel'])
sentence_postprocess(["hel@@", "你"], [[0, 100], [100, 200]])
# ('你 hel', [[0, 100], [100, 100]], ['你', 'hel'])

Main already drops that partial piece and has a timestamp bug for the Chinese token, but this patch newly appends the piece out of order and attaches a zero-length timestamp to it. ["hel@@", "你", "wor@@"] also produces 你helwor, combining pieces separated by Chinese text.

Please address the stale buffer at mixed-language boundaries before merging: preserve token order and each retained piece's own span, or keep a strictly scoped trailing-only fix from flushing stale pieces across a Chinese boundary. Add timed and untimed regressions for these boundary cases in all three implementations alongside the existing trailing-only cases. The separate streaming cross-chunk word-joining semantics should not be claimed solved by a stateless text flush.

Validation here is real pure-Python text/timestamp execution (baseline 2 failing tests, candidate 32 passing selected tests plus 96 boundary observations), not GPU inference or ASR quality. No production files were changed during review.

@ANDROIDHASSAN

Copy link
Copy Markdown
Contributor Author

b5c05ab closes an open @@ word before a Chinese or symbol token, in all three copies, so each piece keeps its place and its own span:

sentence_postprocess(["hel@@", "你"])                          # ('hel你', ['hel', '你'])
sentence_postprocess(["hel@@", "你"], [[0, 100], [100, 200]])  # ('hel 你', [[0, 100], [100, 200]], ['hel', '你'])
sentence_postprocess(["hel@@", "你", "wor@@"])                 # ('hel你wor', ['hel', '你', 'wor'])

["hel@@", "%"] had the same order problem after the trailing flush and is covered too. test_open_bpe_piece_keeps_its_place_before_a_token_that_cannot_continue_it runs five boundary cases timed and untimed over the three implementations, and fails at 84c7b76. The description now says plainly that this does not join words across streaming chunks. main is merged in.

@LauraGPT LauraGPT left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The revised boundary handling addresses my earlier ordering/span concern at b5c05ab. I reviewed all three implementations and the new tests: an interrupted BPE piece is emitted before the Chinese/symbol token, with its own timestamp, and the trailing piece is retained. The description correctly excludes cross-chunk word joining.

Independent validation: new regressions fail on main (3 failures); the related local text suite passes 53/53. Downloaded CI evidence confirms Python 3.10/3.12 text matrices at 76/76 each, Torch wheel/sdist tests and runtime-evaluation tests. ONNX CI uses the merge preview, whose full tree equals the reviewed head; the built Torch and ONNX postprocessor bytes match that source.

A bounded L20Z integration reused existing synthetic CTC weights and constructed hidden inputs: actual CUDA softmax/greedy decoding feeds all three CPU postprocessors, with explicit timed/untimed outputs. Across 72 observations per version, baseline has 63 expected mismatches and this head has none. This is not pretrained audio recognition, ASR quality/performance, streaming-continuity or production validation. An initial validation-harness spacing assumption failed and was corrected against the existing two interface formats; no production changes were made for that check.

No weights were downloaded and no production service or protection setting was changed.

@LauraGPT
LauraGPT merged commit 04bf966 into modelscope:main Nov 25, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants