From 1bda4b7c2cf90fbd53c0077cd6bc14c669298595 Mon Sep 17 00:00:00 2001 From: Mossaddique Ahsan Date: Sun, 25 Oct 2026 13:28:20 +0100 Subject: [PATCH 1/6] fix(whisper): make PUNCTUATION_ONLY_REGEX stateless 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 Claude-Session: https://claude.ai/code/session_01XgUbmQfhDqN7tPMXC8wqpj --- .../transformers/src/models/whisper/tokenization_whisper.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/transformers/src/models/whisper/tokenization_whisper.js b/packages/transformers/src/models/whisper/tokenization_whisper.js index 43aaaf990..414bbb5ed 100644 --- a/packages/transformers/src/models/whisper/tokenization_whisper.js +++ b/packages/transformers/src/models/whisper/tokenization_whisper.js @@ -6,7 +6,7 @@ import { Tensor } from '../../utils/tensor.js'; import { mergeArrays } from '../../utils/core.js'; const PUNCTUATION_REGEX = '\\p{P}\\u0021-\\u002F\\u003A-\\u0040\\u005B-\\u0060\\u007B-\\u007E'; -const PUNCTUATION_ONLY_REGEX = new RegExp(`^[${PUNCTUATION_REGEX}]+$`, 'gu'); +const PUNCTUATION_ONLY_REGEX = new RegExp(`^[${PUNCTUATION_REGEX}]+$`, 'u'); /** * Small tolerance (in seconds) for timestamp ordering in chunk overlap matching. From 3d36ac43e88a4fd0c3f34f2462e22d29d4f17b48 Mon Sep 17 00:00:00 2001 From: Mossaddique Ahsan Date: Sun, 25 Oct 2026 13:32:19 +0100 Subject: [PATCH 2/6] test(whisper): regression test for consecutive punctuation tokens Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XgUbmQfhDqN7tPMXC8wqpj --- .../models/whisper/test_tokenization_whisper.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/transformers/tests/models/whisper/test_tokenization_whisper.js b/packages/transformers/tests/models/whisper/test_tokenization_whisper.js index f082674c4..8663f2949 100644 --- a/packages/transformers/tests/models/whisper/test_tokenization_whisper.js +++ b/packages/transformers/tests/models/whisper/test_tokenization_whisper.js @@ -864,5 +864,20 @@ export const CUSTOM_TESTS = () => { }, MAX_EXECUTION_TIME, ); + + it( + "should split every punctuation-only token into its own word", + async () => { + // Regression test: PUNCTUATION_ONLY_REGEX had the `g` flag, so RegExp.test() kept + // `lastIndex` between calls and alternated true/false. The second of two + // consecutive punctuation tokens was then glued onto the previous word. + const tokenizer = await WhisperTokenizer.from_pretrained("onnx-community/whisper-tiny.en_timestamped"); + const tokens = tokenizer.encode("Hello", { add_special_tokens: false }); + const dot = tokenizer.encode(".", { add_special_tokens: false }); + const [words] = tokenizer.splitTokensOnSpaces([...tokens, ...dot, ...dot, ...dot]); + expect(words).toEqual(["Hello", ".", ".", "."]); + }, + MAX_EXECUTION_TIME, + ); }); }; From 14fd524e568c44e90340a89da75586a37b51f930 Mon Sep 17 00:00:00 2001 From: Mossaddique Ahsan Date: Mon, 26 Oct 2026 05:52:47 +0100 Subject: [PATCH 3/6] test(whisper): end-to-end pipeline test for consecutive punctuation 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 Claude-Session: https://claude.ai/code/session_01XgUbmQfhDqN7tPMXC8wqpj --- ..._pipelines_automatic_speech_recognition.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/transformers/tests/pipelines/test_pipelines_automatic_speech_recognition.js b/packages/transformers/tests/pipelines/test_pipelines_automatic_speech_recognition.js index 43e160a39..d07aceca3 100644 --- a/packages/transformers/tests/pipelines/test_pipelines_automatic_speech_recognition.js +++ b/packages/transformers/tests/pipelines/test_pipelines_automatic_speech_recognition.js @@ -256,6 +256,27 @@ export default () => { }, MAX_TEST_EXECUTION_TIME, ); + it( + "transcribe w/ return_timestamps='word' keeps consecutive punctuation with its word", + async () => { + // Regression test: PUNCTUATION_ONLY_REGEX had the `g` flag, so RegExp.test() kept + // `lastIndex` between calls and returned false for the second of two consecutive + // punctuation-only tokens. In this clip the model emits " done" + "!" + "!", and + // "!!" came out as a separate word instead of " done!!". + const audio = await load_cached_audio("whisper_2"); + const options = { return_timestamps: "word", chunk_length_s: 30 }; + + const output = await pipe(audio, options); + expect(output.chunks.some(({ text }) => text === "!!")).toBe(false); + const done = output.chunks.find(({ text }) => text === " done!!"); + expect(done).toBeDefined(); + expect(done.timestamp).toBeCloseToNested([47.52, 47.98], 1); + + // Running the same transcription again must give the same result. + expect(await pipe(audio, options)).toEqual(output); + }, + MAX_TEST_EXECUTION_TIME, + ); }); afterAll(async () => { From a27dfdacd4f8e1efa6d039e1735ea41e6277fd22 Mon Sep 17 00:00:00 2001 From: Mossaddique Ahsan Date: Mon, 26 Oct 2026 05:53:10 +0100 Subject: [PATCH 4/6] test(whisper): drop the internal splitTokensOnSpaces test Replaced by the end-to-end ASR pipeline test, as suggested in review. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XgUbmQfhDqN7tPMXC8wqpj --- .../models/whisper/test_tokenization_whisper.js | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/packages/transformers/tests/models/whisper/test_tokenization_whisper.js b/packages/transformers/tests/models/whisper/test_tokenization_whisper.js index 8663f2949..f082674c4 100644 --- a/packages/transformers/tests/models/whisper/test_tokenization_whisper.js +++ b/packages/transformers/tests/models/whisper/test_tokenization_whisper.js @@ -864,20 +864,5 @@ export const CUSTOM_TESTS = () => { }, MAX_EXECUTION_TIME, ); - - it( - "should split every punctuation-only token into its own word", - async () => { - // Regression test: PUNCTUATION_ONLY_REGEX had the `g` flag, so RegExp.test() kept - // `lastIndex` between calls and alternated true/false. The second of two - // consecutive punctuation tokens was then glued onto the previous word. - const tokenizer = await WhisperTokenizer.from_pretrained("onnx-community/whisper-tiny.en_timestamped"); - const tokens = tokenizer.encode("Hello", { add_special_tokens: false }); - const dot = tokenizer.encode(".", { add_special_tokens: false }); - const [words] = tokenizer.splitTokensOnSpaces([...tokens, ...dot, ...dot, ...dot]); - expect(words).toEqual(["Hello", ".", ".", "."]); - }, - MAX_EXECUTION_TIME, - ); }); }; From ff17ab899fcedb60965e8aeaeea42d716816d030 Mon Sep 17 00:00:00 2001 From: Mossaddique Ahsan Date: Mon, 26 Oct 2026 23:01:42 +0100 Subject: [PATCH 5/6] test(whisper): decode the same sentence twice 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 Claude-Session: https://claude.ai/code/session_01XgUbmQfhDqN7tPMXC8wqpj --- .../whisper/test_tokenization_whisper.js | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/packages/transformers/tests/models/whisper/test_tokenization_whisper.js b/packages/transformers/tests/models/whisper/test_tokenization_whisper.js index f082674c4..0ef4afe9a 100644 --- a/packages/transformers/tests/models/whisper/test_tokenization_whisper.js +++ b/packages/transformers/tests/models/whisper/test_tokenization_whisper.js @@ -864,5 +864,49 @@ export const CUSTOM_TESTS = () => { }, MAX_EXECUTION_TIME, ); + + it( + "should decode the same sentence the same way twice", + async () => { + // Regression test: PUNCTUATION_ONLY_REGEX had the `g` flag, so RegExp.test() kept `lastIndex` + // between calls, and that state leaked from one decode into the next. Decoding this sentence + // leaves `lastIndex` at 1 (after the trailing "..."), so the leading "..." of the next decode + // no longer matched and took the pause before " and" as its duration. + const tokenizer = await WhisperTokenizer.from_pretrained("onnx-community/whisper-tiny.en_timestamped"); + + const text = "... and then ..."; + const ids = tokenizer.encode(text, { add_special_tokens: false }); + expect(ids.map((id) => tokenizer.decode([id]))).toEqual(["...", " and", " then", "..."]); + + const model_outputs = [ + { + stride: [30, 0, 5], + tokens: [50257n, 50362n, ...ids.map(BigInt), 50256n], + token_timestamps: [0, 0, 0, 1, 2, 3, 4], + }, + ]; + const decode = () => + tokenizer._decode_asr(model_outputs, { + return_timestamps: "word", + time_precision: 0.02, + force_full_sequences: false, + }); + const target = [ + "... and then...", + { + chunks: [ + { text: "...", timestamp: [0, 0.02] }, + { text: " and", timestamp: [1, 2] }, + { text: " then", timestamp: [2, 3] }, + { text: "...", timestamp: [3, 3.02] }, + ], + }, + ]; + + expect(decode()).toBeCloseToNested(target, 2); + expect(decode()).toBeCloseToNested(target, 2); + }, + MAX_EXECUTION_TIME, + ); }); }; From dbe00f30f01b70cf7a9418837689a056156ca7ef Mon Sep 17 00:00:00 2001 From: Mossaddique Ahsan Date: Mon, 26 Oct 2026 23:02:01 +0100 Subject: [PATCH 6/6] test(whisper): drop the slow pipeline regression test Replaced by the tokenizer-level test. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XgUbmQfhDqN7tPMXC8wqpj --- ..._pipelines_automatic_speech_recognition.js | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/packages/transformers/tests/pipelines/test_pipelines_automatic_speech_recognition.js b/packages/transformers/tests/pipelines/test_pipelines_automatic_speech_recognition.js index d07aceca3..43e160a39 100644 --- a/packages/transformers/tests/pipelines/test_pipelines_automatic_speech_recognition.js +++ b/packages/transformers/tests/pipelines/test_pipelines_automatic_speech_recognition.js @@ -256,27 +256,6 @@ export default () => { }, MAX_TEST_EXECUTION_TIME, ); - it( - "transcribe w/ return_timestamps='word' keeps consecutive punctuation with its word", - async () => { - // Regression test: PUNCTUATION_ONLY_REGEX had the `g` flag, so RegExp.test() kept - // `lastIndex` between calls and returned false for the second of two consecutive - // punctuation-only tokens. In this clip the model emits " done" + "!" + "!", and - // "!!" came out as a separate word instead of " done!!". - const audio = await load_cached_audio("whisper_2"); - const options = { return_timestamps: "word", chunk_length_s: 30 }; - - const output = await pipe(audio, options); - expect(output.chunks.some(({ text }) => text === "!!")).toBe(false); - const done = output.chunks.find(({ text }) => text === " done!!"); - expect(done).toBeDefined(); - expect(done.timestamp).toBeCloseToNested([47.52, 47.98], 1); - - // Running the same transcription again must give the same result. - expect(await pipe(audio, options)).toEqual(output); - }, - MAX_TEST_EXECUTION_TIME, - ); }); afterAll(async () => {