Repository navigation
fix(whisper): make PUNCTUATION_ONLY_REGEX stateless - #1819
Conversation
The regex was built with the 'g' flag, so RegExp.prototype.test() kept lastIndex between calls and returned alternating results for consecutive inputs. In splitTokensOnSpaces this glued the second of two consecutive punctuation-only tokens onto the previous word, and the same regex is used when merging chunk timestamps. Drop 'g'. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XgUbmQfhDqN7tPMXC8wqpj
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XgUbmQfhDqN7tPMXC8wqpj
There was a problem hiding this comment.
🟢 Approval recommended
The narrow flag change fixes the reported behavior, and the regression test directly covers the word-splitting failure.
No open issues 🎉
What changed in this PR
Makes Whisper punctuation detection stateless, preventing consecutive punctuation tokens from being inconsistently classified during timestamp decoding and word splitting.
Changes:
- Removes the global regex flag while preserving Unicode punctuation matching.
- Adds a regression test verifying that three consecutive punctuation tokens remain separate words.
| File | Description |
|---|---|
packages/transformers/tests/models/whisper/test_tokenization_whisper.js |
Tests consecutive punctuation-token splitting. |
packages/transformers/src/models/whisper/tokenization_whisper.js |
Removes stateful matching from the punctuation-only regex. |
Review effort: Balanced
xenova
left a comment
There was a problem hiding this comment.
thanks for the PR! this looks pretty valid. one comment.
Transcribe the whisper_2 asset with word-level timestamps. With the old `g` flag the model's " done" "!" "!" came out as " done" plus a separate "!!" word; it must be " done!!", and a second run must match the first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XgUbmQfhDqN7tPMXC8wqpj
Replaced by the end-to-end ASR pipeline test, as suggested in review. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XgUbmQfhDqN7tPMXC8wqpj
|
Thanks for the review! Done. I removed the I couldn't get two runs to diverge on the old code (it's consistently wrong rather than flaky), so the assertion on |
|
Oh wow, I'm surprised you found an example which uses the full pipeline. By "end-to-end" I meant for the tokenizer itself, showing encoder <-> decode (instead of using internal functions), but I can see how my usage of that word was confusing. My bad! Since it's just a tokenization issue, I presume we'd be able to find a case where we tokenize a chosen sentence twice and get different behaviour. This should be a sufficient test. also, cc @nico-martin to make sure we have fixed this in tokenizers.js, if present. |
Tokenizer-level regression test, as suggested in review: encode "... and then ..." and decode it with word timestamps twice. With the old `g` flag the second decode differs because RegExp.test() state leaks from the first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XgUbmQfhDqN7tPMXC8wqpj
Replaced by the tokenizer-level test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XgUbmQfhDqN7tPMXC8wqpj
|
Thanks for clarifying! I've replaced the pipeline test with a tokenizer-level one. It encodes "... and then ..." ( One note: |
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
PUNCTUATION_ONLY_REGEXintokenization_whisper.jsis created with thegflag. Withg,RegExp.prototype.test()storeslastIndexafter a match, so the next call starts searching from that offset. Called on consecutive strings, it returns alternating results:The regex is used in two places:
splitTokensOnSpaces(word-level timestamps): the second of two consecutive punctuation-only tokens is treated as non-punctuation and glued onto the previous word, soHello . . .splits into["Hello", ".", ".."]instead of["Hello", ".", ".", "."]. Its timing is merged into the wrong word too._decode_asr(chunk timestamps, line ~250): the result depends on whatever string was tested before.The fix drops
g('u'only); the regex is only used with.test(), so nothing relies on global matching.Test: added "should split every punctuation-only token into its own word" to the Whisper tokenizer custom tests. It fails on
main(["Hello", ".", ".."]) and passes with the fix.Locally:
tests/tokenizers.test.jsandtests/utilspass (17 suites, 1738 tests);prettier --checkis clean on both files. I couldn't run the model/pipeline suites here because the onnxruntime-node binary download is blocked on my machine, so CI will cover those. I checked open issues and PRs (including #1809, #1742, #1816) and none of them touch this regex.AI disclosure: prepared with AI assistance (Claude, via Claude Code). I reviewed the diff and the reproduction above.