Skip to content

fix(whisper): make PUNCTUATION_ONLY_REGEX stateless - #1819

Merged
xenova merged 6 commits into
huggingface:mainfrom
mossaddique:fix-whisper-punctuation-regex
Oct 28, 2026
Merged

xenova merged 6 commits into
huggingface:mainfrom
mossaddique:fix-whisper-punctuation-regex

Conversation

@mossaddique

Copy link
Copy Markdown
Contributor

PUNCTUATION_ONLY_REGEX in tokenization_whisper.js is created with the g flag. With g, RegExp.prototype.test() stores lastIndex after a match, so the next call starts searching from that offset. Called on consecutive strings, it returns alternating results:

['।', '!', '?', '.'].map(s => PUNCTUATION_ONLY_REGEX.test(s)) // true false true false

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, so Hello . . . 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.js and tests/utils pass (17 suites, 1738 tests); prettier --check is 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.

mossaddique and others added 2 commits October 25, 2026 13:28
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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 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 xenova 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.

thanks for the PR! this looks pretty valid. one comment.

mossaddique and others added 2 commits October 26, 2026 05:52
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
@mossaddique

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Done. I removed the splitTokensOnSpaces test and added an end-to-end one in the ASR pipeline tests. It transcribes the existing whisper_2 asset with onnx-community/whisper-tiny.en_timestamped and return_timestamps: "word". With the old g flag the model's " done" "!" "!" comes out as " done" plus a separate "!!" word; with the fix it's " done!!" (47.52-47.98 s). The test runs the same transcription twice and checks the results are identical.

I couldn't get two runs to diverge on the old code (it's consistently wrong rather than flaky), so the assertion on " done!!" is what catches the regression. It takes about 25 s per run on the full clip, since shorter excerpts don't reproduce the !!; happy to drop the second run if that's too slow.

@xenova

xenova commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator

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.

mossaddique and others added 2 commits October 26, 2026 23:01
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
@mossaddique

Copy link
Copy Markdown
Contributor Author

Thanks for clarifying! I've replaced the pipeline test with a tokenizer-level one. It encodes "... and then ..." (... | and | then | ...) and decodes it with word timestamps twice. With the old g flag, the first decode leaves lastIndex at 1 after the trailing "...", so on the second decode the leading "..." no longer matches and takes the pause before " and" as its duration: [0, 1] instead of [0, 0.02]. The test checks both decodes against the expected output, so it fails whichever decode goes wrong. I removed the slow pipeline test.

One note: decode() itself never reaches PUNCTUATION_ONLY_REGEX, only the word-timestamp path in _decode_asr does, so the test encodes with encode() and decodes through _decode_asr, like the other "Decode ASR" tests.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The narrow fix addresses both regex call sites and includes a targeted regression test, with no unresolved issues identified.

No open issues 🎉

Review effort: Balanced

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

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.

@xenova xenova 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.

thanks!

@xenova
xenova merged commit 656d627 into huggingface:main Oct 28, 2026
5 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.

4 participants