From 971d109f2244e08ce47622d1bc5583280886bc7a Mon Sep 17 00:00:00 2001 From: Markus Mattinen Date: Tue, 30 May 2023 22:09:56 +0300 Subject: [PATCH] Apply endless loop fixes Ref https://github.com/openai/whisper/pull/1155 and https://github.com/openai/whisper/pull/1219 --- whisper.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/whisper.cpp b/whisper.cpp index 6b159830db2..c530c5c5f81 100644 --- a/whisper.cpp +++ b/whisper.cpp @@ -3488,12 +3488,12 @@ static void whisper_process_logits( } } + const bool last_was_timestamp = tokens_cur.size() > 0 && tokens_cur.back().id >= vocab.token_beg; + const bool penultimate_was_timestamp = tokens_cur.size() < 2 || tokens_cur[tokens_cur.size() - 2].id >= vocab.token_beg; + // timestamps have to appear in pairs, except directly before EOT; mask logits accordingly // https://github.com/openai/whisper/blob/0b1ba3d46ebf7fe6f953acfd8cad62a4f851b49f/whisper/decoding.py#L414-L424 { - const bool last_was_timestamp = tokens_cur.size() > 0 && tokens_cur.back().id >= vocab.token_beg; - const bool penultimate_was_timestamp = tokens_cur.size() < 2 || tokens_cur[tokens_cur.size() - 2].id >= vocab.token_beg; - //fprintf(stderr, "last_was_timestamp=%d penultimate_was_timestamp=%d\n", last_was_timestamp, penultimate_was_timestamp); if (last_was_timestamp) { @@ -3521,9 +3521,11 @@ static void whisper_process_logits( } // condition timestamp tokens to be increasing - // ref: https://github.com/openai/whisper/pull/831#issuecomment-1385910556 + // ref: https://github.com/openai/whisper/blob/c09a7ae299c4c34c5839a76380ae407e7d785914/whisper/decoding.py#L471-L477 if (decoder.has_ts) { - const int tid0 = decoder.seek_delta/2; + const int tid0 = last_was_timestamp && !penultimate_was_timestamp + ? decoder.seek_delta / 2 + : decoder.seek_delta / 2 + 1; for (int i = vocab.token_beg; i < vocab.token_beg + tid0; ++i) { logits[i] = -INFINITY;