Skip to content

fix(generation): apply max_initial_timestamp on the first Whisper step - #1875

Closed
laoyyu wants to merge 2 commits into
huggingface:mainfrom
laoyyu:fix/whisper-max-initial-timestamp
Closed

laoyyu wants to merge 2 commits into
huggingface:mainfrom
laoyyu:fix/whisper-max-initial-timestamp

Conversation

@laoyyu

@laoyyu laoyyu commented Nov 6, 2026 •

Copy link
Copy Markdown

Problem

WhisperTimeStampLogitsProcessor returns early on the first decoding step:

if (input_ids[i].length === this.begin_index) {
    batch_logits_data.subarray(0, this.timestamp_begin).fill(-Infinity);
    continue;
}

but the cap for max_initial_timestamp sits after that continue, guarded by the same condition, so it is unreachable:

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);
}

max_initial_timestamp_index is therefore silently ignored: for every Whisper checkpoint whose generation_config.json exports it (the official conversions set 50, i.e. 1.0 s), a segment is free to start at an arbitrarily late timestamp.

Fix

  1. logits_process.js: move the cap into the first-step branch, before the continue. Later steps keep their pair-wise timestamp masking unchanged.
  2. generation_whisper.js: max_initial_timestamp_index now defaults to null instead of 1. Now that the branch is actually reachable, the old port-artifact default would pin the first timestamp of every model that does not set the key to 0.02 s. The Python reference reads the option with getattr(generate_config, "max_initial_timestamp_index", None), i.e. a missing key means "no restriction".
  3. Guard with != null instead of !== null: a config that omits the key yields undefined, and timestamp_begin + undefined is NaN, so subarray(last_allowed + 1) becomes subarray(NaN) -> subarray(0), masking every logit in the row (text tokens included); the row softmax then returns NaN. != null covers both null and undefined, matching the Python getattr semantics.

Verification

packages/transformers/tests/utils/logits_process.test.js, synthetic vocabulary (no model download):

$ node --experimental-vm-modules node_modules/jest/bin/jest.js tests/utils/logits_process.test.js -t "WhisperTimeStampLogitsProcessor"
Tests: 5 passed, 3 skipped

Before the change, limits the first timestamp by max_initial_timestamp_index fails, because every timestamp stays available. Before the != null guard, keeps every timestamp when the config omits max_initial_timestamp_index fails with Received length: 0 (all 99 timestamp logits masked).

The file's three bad_word_ids cases fail locally both before and after this change: they load a TextGenerationPipeline from the Hub and this machine has no network access, so they are unrelated.

Notes

  • Expected outputs under tests/models/whisper/ that were recorded while the cap was disabled will move once it is enforced for checkpoints that set the key. I could not regenerate them here for the same network reason; happy to update them once CI reports the new ids.
  • Fix max_initial_timestamp_index never being applied in WhisperTimeStampLogitsProcessor #1823 (opened earlier) reaches the same conclusion, including the default change, so the two PRs are near-identical. I am happy to close this one if you would rather review that diff.

WhisperTimeStampLogitsProcessor masked the text tokens with

    if (input_ids[i].length === this.begin_index) { ...; continue; }

and then, after that continue, tried to cap the allowed timestamps with

    if (input_ids[i].length === this.begin_index && max_initial_timestamp_index !== null)

The second condition can never be true, so max_initial_timestamp_index (1 by
default in WhisperGenerationConfig) was silently ignored and the model was
free to emit any timestamp as the first one. Move the cap into the branch
that handles the first step, before the continue.
Two follow-ups to the unreachable `max_initial_timestamp` branch:

- `WhisperGenerationConfig.max_initial_timestamp_index` defaulted to `1`.
  Now that the cap can actually be reached, that would pin the first
  timestamp of every model that does not set the key to 0.02 s. The Python
  reference reads it with `getattr(generate_config, ..., None)`, i.e. a
  missing key means no restriction, so the default is now `null`.
- The guard used `!== null`, so a config without the key (`undefined`)
  computed `subarray(NaN)`, which is `subarray(0)` and masked *every*
  logit in the row, text tokens included. `!= null` matches the
  `getattr(..., None)` semantics.

Tests cover the missing-key case, the new default, and that later decoding
steps stay uncapped.
@laoyyu

laoyyu commented Nov 7, 2026

Copy link
Copy Markdown
Author

Pushed 7f56063, which closes two gaps in the first commit:

  • the guard is now != null rather than !== null, so a config that omits max_initial_timestamp_index (undefined) no longer computes subarray(NaN) -> subarray(0) and masks every logit in the row (text tokens included), which turned the row softmax into NaN;
  • WhisperGenerationConfig.max_initial_timestamp_index now defaults to null, matching getattr(generate_config, "max_initial_timestamp_index", None) in Python. With the branch finally reachable, the old default of 1 would have clamped every model that does not set the key to a 0.02 s first timestamp.

Tests: Tests: 5 passed, 3 skipped for -t "WhisperTimeStampLogitsProcessor"; the first case fails on main and the missing-key case fails with either of the two issues above unfixed.

For the record: #1823 (opened earlier) fixes the same unreachable branch and independently lands on the same null default, so the two PRs are near-identical. If you would rather review that diff, say so and I will close this one.

@nico-martin

Copy link
Copy Markdown
Collaborator

looks like PR farming

@nico-martin nico-martin closed this Nov 9, 2026
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