Repository navigation
Fix Whisper ASR timestamp regression - #1742
anshusaurav wants to merge 4 commits into
Conversation
|
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. |
nico-martin
left a comment
There was a problem hiding this comment.
Hi @anshusaurav, thank you so much for looking into this! The diagnosis and overall direction make sense. One edge case remains: num_frames can be 0, so the truthy check falls back to the padded tensor. Could you use a nullish check and add direct tests for short audio, timestamp masking, and the non-progress case? I'm holding off on merging until the zero-frame bug and untested seek-loop changes are covered.
| // input_features shape: [batch=1, n_mels, total_frames] | ||
| const input_features = inputs; | ||
| const total_frames = input_features.dims[2]; | ||
| const total_frames = generation_config.num_frames |
There was a problem hiding this comment.
num_frames can legitimately be 0. This truthiness check then falls back to the padded 3,000-frame tensor and reintroduces padding processing. Please distinguish an omitted value from zero, for example with generation_config.num_frames != null, and add a zero/short-frame regression test.
|
Thanks for the thorough review @nico-martin! Just pushed the requested changes:
|
|
Thanks, the nullish check correctly fixes the num_frames=0 case. The added tests still do not exercise the changed seek-loop behavior, however: each passes max_new_tokens, and generate() explicitly bypasses _generate_with_seek() whenever that option is present. Could you adjust the coverage so that:
The current timestamp-presence assertion and short-input smoke test would pass with the corresponding fixes reverted. Also, the added Tensor import is currently unused. |
|
We ran into this independently and have been carrying a patched build of transformers.js Where it bites. Electron app, WebGPU, The test. It drives describe("seek loop", () => {
const NO_TIMESTAMPS = 50363;
const TIMESTAMP_BEGIN = NO_TIMESTAMPS + 1;
const EOS = 50257;
const INIT = [50258, 50260, 50360];
/** Runs the seek loop over 30 s of features with a decoder that returns `tokens` each pass. */
const runSeek = async (tokens, cap = 50) => {
const whisperProto = Object.getPrototypeOf(WhisperForConditionalGeneration.prototype);
const original = Object.getOwnPropertyDescriptor(whisperProto, "generate");
let passes = 0;
whisperProto.generate = async ({ decoder_input_ids }) => {
if (++passes > cap) throw new Error(`the seek loop is still decoding after ${cap} passes`);
const ids = [...decoder_input_ids, ...tokens, EOS].map(BigInt);
return new Tensor("int64", BigInt64Array.from(ids), [1, ids.length]);
};
try {
const model = Object.create(WhisperForConditionalGeneration.prototype);
model.config = { max_source_positions: 1500 };
const output = await model._generate_with_seek({
inputs: new Tensor("float32", new Float32Array(80 * 3000), [1, 80, 3000]),
generation_config: { no_timestamps_token_id: NO_TIMESTAMPS, eos_token_id: EOS, return_timestamps: true },
logits_processor: [],
init_tokens: INIT,
kwargs: {},
});
return { passes, tokens: output[0].tolist().map(Number) };
} finally {
if (original) Object.defineProperty(whisperProto, "generate", original);
else delete whisperProto.generate;
}
};
it(
"terminates when a segment ends at <|0.00|>",
async () => {
// What a real decode produced: a complete segment whose end timestamp is 0.00.
const { passes } = await runSeek([TIMESTAMP_BEGIN, 1654, TIMESTAMP_BEGIN, TIMESTAMP_BEGIN + 1200]);
expect(passes).toBe(1);
},
MAX_TEST_EXECUTION_TIME,
);
it(
"still advances through ordinary multi-segment output",
async () => {
// A segment ending at <|10.00|> is 1000 frames a pass: three passes over 3000.
const { passes } = await runSeek([TIMESTAMP_BEGIN, 1654, TIMESTAMP_BEGIN + 500, TIMESTAMP_BEGIN + 1200]);
expect(passes).toBe(3);
},
MAX_TEST_EXECUTION_TIME,
);
});Measured against the 4.3.0 dist, which carries the same loop as | build | terminates at So the guard in this PR does fix the hang, and the first test fails without it. Two notes on the fix itself, take or leave:
We apply both as a build-time source transform (we can only patch the bundle, not — txscribe.ai |
6bb512b to
5765208
Compare
|
Thanks @nico-martin and @txscribe. I rebased this branch onto the current
The focused tests pass locally (4 seek-loop tests and 1 timestamp-processor test). Could you please take another look when the refreshed CI completes? |
|
Hi @anshusaurav, thanks for updating this! The new tests address my earlier coverage requests. Two things still need fixing:
Could you handle those and add a test where a zero-offset window is followed by more audio? We should either continue safely or report an error rather than return an incomplete transcript as success. |
Three fixes for the timestamp regression between v3.8.1 and v4.2.0: 1. Use actual audio length for seek loop bounds: The seek loop used input_features.dims[2] (always 3000 = padded 30s) instead of generation_config.num_frames (actual audio length). For clips shorter than 30s, this caused the loop to process silence as real audio, producing hallucinated text and wrong timestamps. 2. Apply max_initial_timestamp_index constraint: A `continue` statement in WhisperTimeStampLogitsProcessor skipped the max_initial_timestamp_index check, allowing the model to generate any timestamp as its first token. The Python reference implementation has no such early exit. 3. Guard against infinite seek loop: If segment_offset is zero (e.g., from a degenerate timestamp pair), the loop ran forever.
Address review feedback: - Change truthy check to nullish check (num_frames != null) so that num_frames=0 is not mistaken for an omitted value, which would incorrectly fall back to the padded tensor length - Add tests for zero num_frames, short audio with timestamps, and timestamp token presence
5765208 to
12a9b7b
Compare
|
Addressed the November 11 feedback and rebased onto the current
The focused timestamp-processor tests pass, and direct seek-loop verification completes two passes while retaining the second-window token. |
Summary
Fixes #1684 — Whisper ASR timestamp regression between v3.8.1 and v4.2.0.
Three fixes targeting the seek loop and logits processor introduced in commit
43b2662("Overdue Whisper fixes #1594"):Use actual audio length for seek loop bounds (
modeling_whisper.js): The seek loop usedinput_features.dims[2](always 3000 = padded 30s) astotal_framesinstead ofgeneration_config.num_frames(set by the ASR pipeline to the real mel frame count). For clips shorter than 30 seconds, this caused the loop to process silence/padding as real audio, producing hallucinated text and incorrect timestamps.Apply
max_initial_timestamp_indexconstraint (logits_process.js): Acontinuestatement inWhisperTimeStampLogitsProcessor._call()skipped themax_initial_timestamp_indexcheck at line 326, making it dead code. This allowed the model to generate any timestamp as its first token instead of being constrained to the allowed range. The Python reference implementation has no such early exit — both blocks execute sequentially.Guard against infinite seek loop (
modeling_whisper.js): Ifsegment_offsetcomputes to zero (e.g., from a degenerate timestamp pair), the seek loop would run forever. Added abreakwhensegment_offset <= 0.Test plan
[applause]segment is no longer droppedtest_modeling_whisper.js) still pass — they usemax_new_tokenswhich bypasses the seek loop