From de61bb8870595c06bb70b926a1e70e63c75c0778 Mon Sep 17 00:00:00 2001 From: Alex Senkevich Date: Tue, 27 Oct 2026 20:08:52 +0100 Subject: [PATCH] Fix `max_initial_timestamp_index` never being applied in WhisperTimeStampLogitsProcessor The early `continue` at `begin_index` made the `max_initial_timestamp` block unreachable, so the first timestamp was never restricted. Mirror the reference implementation by nesting the check inside the first-step branch, and align the `WhisperGenerationConfig` default with transformers (unset means no restriction) instead of the previous `1` (= 0.02s). --- .../src/generation/logits_process.js | 15 +++--- .../src/models/whisper/generation_whisper.js | 6 +-- .../tests/utils/logits_process.test.js | 52 +++++++++++++++++++ 3 files changed, 62 insertions(+), 11 deletions(-) diff --git a/packages/transformers/src/generation/logits_process.js b/packages/transformers/src/generation/logits_process.js index 330322b3e..a5605f1c2 100644 --- a/packages/transformers/src/generation/logits_process.js +++ b/packages/transformers/src/generation/logits_process.js @@ -309,11 +309,6 @@ export class WhisperTimeStampLogitsProcessor extends LogitsProcessor { // suppress <|notimestamps|> which is handled by without_timestamps batch_logits_data[this.no_timestamps_token_id] = -Infinity; - if (input_ids[i].length === this.begin_index) { - batch_logits_data.subarray(0, this.timestamp_begin).fill(-Infinity); - continue; - } - // timestamps have to appear in pairs, except directly before eos_token; mask logits accordingly const seq = input_ids[i].slice(this.begin_index); const last_was_timestamp = seq.length >= 1 && seq[seq.length - 1] >= this.timestamp_begin; @@ -330,9 +325,13 @@ export class WhisperTimeStampLogitsProcessor extends LogitsProcessor { } // apply the `max_initial_timestamp` option - if (input_ids[i].length === this.begin_index && this.max_initial_timestamp_index !== null) { - const last_allowed = this.timestamp_begin + this.max_initial_timestamp_index; - batch_logits_data.subarray(last_allowed + 1).fill(-Infinity); + if (input_ids[i].length === this.begin_index) { + batch_logits_data.subarray(0, this.timestamp_begin).fill(-Infinity); + + if (this.max_initial_timestamp_index != null) { + const last_allowed = this.timestamp_begin + this.max_initial_timestamp_index; + batch_logits_data.subarray(last_allowed + 1).fill(-Infinity); + } } // if sum of probability over timestamps is above any other token, sample timestamp diff --git a/packages/transformers/src/models/whisper/generation_whisper.js b/packages/transformers/src/models/whisper/generation_whisper.js index 0fd1daa7d..22f4995d5 100644 --- a/packages/transformers/src/models/whisper/generation_whisper.js +++ b/packages/transformers/src/models/whisper/generation_whisper.js @@ -77,10 +77,10 @@ export class WhisperGenerationConfig extends GenerationConfig { /** * Used to set the maximum value of the initial timestamp. This is used to prevent the model from - * predicting timestamps that are too far in the future. - * @type {number} + * predicting timestamps that are too far in the future. If `null`, no restriction is applied. + * @type {number|null} */ - max_initial_timestamp_index = 1; + max_initial_timestamp_index = null; } /** diff --git a/packages/transformers/tests/utils/logits_process.test.js b/packages/transformers/tests/utils/logits_process.test.js index 86ab06f2a..4e4fb560c 100644 --- a/packages/transformers/tests/utils/logits_process.test.js +++ b/packages/transformers/tests/utils/logits_process.test.js @@ -2,6 +2,12 @@ import { // Pipelines pipeline, TextGenerationPipeline, + + // Logits processors + WhisperTimeStampLogitsProcessor, + + // Other + Tensor, } from "../../src/transformers.js"; import { init } from "../init.js"; @@ -110,3 +116,49 @@ describe("Logits Processors", () => { }, MAX_MODEL_DISPOSE_TIME); }); }); + +describe("WhisperTimeStampLogitsProcessor", () => { + // Small synthetic vocabulary: [0..8] text tokens, 9 = <|endoftext|>, 10 = <|notimestamps|>, [11..18] timestamp tokens + const eos_token_id = 9; + const no_timestamps_token_id = 10; + const timestamp_begin = no_timestamps_token_id + 1; + const vocab_size = 19; + const init_tokens = [1, 2, 3]; // begin_index = 3 + + const create_logits = () => new Tensor("float32", new Float32Array(vocab_size).fill(0), [1, vocab_size]); + const finite_ids = (data) => Array.from(data, (x, i) => (Number.isFinite(x) ? i : -1)).filter((i) => i >= 0); + + it("should apply `max_initial_timestamp_index` at the first decoding step", () => { + const max_initial_timestamp_index = 2; + const processor = new WhisperTimeStampLogitsProcessor({ eos_token_id, no_timestamps_token_id, max_initial_timestamp_index }, init_tokens); + + const logits = processor([init_tokens.map(BigInt)], create_logits()); + + // Only the first `max_initial_timestamp_index + 1` timestamp tokens may be sampled at the first step + const last_allowed = timestamp_begin + max_initial_timestamp_index; + expect(finite_ids(logits.data)).toEqual([timestamp_begin, timestamp_begin + 1, last_allowed]); + }); + + it("should not restrict the initial timestamp when `max_initial_timestamp_index` is unset", () => { + const processor = new WhisperTimeStampLogitsProcessor({ eos_token_id, no_timestamps_token_id, max_initial_timestamp_index: null }, init_tokens); + + const logits = processor([init_tokens.map(BigInt)], create_logits()); + + // All timestamp tokens remain available (text tokens are still suppressed at the first step) + const all_timestamps = Array.from({ length: vocab_size - timestamp_begin }, (_, i) => timestamp_begin + i); + expect(finite_ids(logits.data)).toEqual(all_timestamps); + }); + + it("should only apply `max_initial_timestamp_index` at the first decoding step", () => { + const max_initial_timestamp_index = 2; + const processor = new WhisperTimeStampLogitsProcessor({ eos_token_id, no_timestamps_token_id, max_initial_timestamp_index }, init_tokens); + + // One text token has already been sampled after the prompt + const logits = processor([[...init_tokens, 5].map(BigInt)], create_logits()); + + const last_allowed = timestamp_begin + max_initial_timestamp_index; + for (let i = last_allowed + 1; i < vocab_size; ++i) { + expect(logits.data[i]).toBe(0); + } + }); +});