-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Implement no_speech_thold #2625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -867,6 +867,7 @@ struct whisper_state { | |
whisper_token tid_last; | ||
|
||
std::vector<float> energy; // PCM signal energy | ||
float no_speech_prob = 0.0f; | ||
|
||
// [EXPERIMENTAL] Token-level timestamps with DTW | ||
whisper_aheads_masks aheads_masks; | ||
|
@@ -5647,6 +5648,35 @@ int whisper_full_with_state( | |
return -8; | ||
} | ||
|
||
// Calculate no_speech probability after first decode | ||
{ | ||
const float * logits = state->logits.data(); | ||
const int n_vocab = ctx->vocab.n_vocab; | ||
|
||
// Find max element for numerical stability | ||
float max_logit = -INFINITY; | ||
for (int i = 0; i < n_vocab; ++i) { | ||
max_logit = std::max(max_logit, logits[i]); | ||
} | ||
|
||
// Calculate softmax | ||
float sum_exp = 0.0f; | ||
std::vector<float> probs(n_vocab); | ||
for (int i = 0; i < n_vocab; ++i) { | ||
float exp_val = expf(logits[i] - max_logit); | ||
sum_exp += exp_val; | ||
probs[i] = exp_val; | ||
} | ||
|
||
// Normalize | ||
for (int i = 0; i < n_vocab; ++i) { | ||
probs[i] /= sum_exp; | ||
} | ||
|
||
// Get probability of no_speech token | ||
state->no_speech_prob = probs[whisper_token_nosp(ctx)]; | ||
} | ||
|
||
{ | ||
const int64_t t_start_sample_us = ggml_time_us(); | ||
|
||
|
@@ -6038,7 +6068,8 @@ int whisper_full_with_state( | |
if (it != (int) temperatures.size() - 1) { | ||
const auto & decoder = state->decoders[best_decoder_id]; | ||
|
||
if (decoder.failed || decoder.sequence.avg_logprobs < params.logprob_thold) { | ||
if (decoder.failed || | ||
(decoder.sequence.avg_logprobs < params.logprob_thold && state->no_speech_prob < params.no_speech_thold)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
WHISPER_LOG_DEBUG("%s: failed due to avg_logprobs %8.5f < %8.5f\n", __func__, decoder.sequence.avg_logprobs, params.logprob_thold); | ||
success = false; | ||
state->n_fail_p++; | ||
|
@@ -6068,6 +6099,9 @@ int whisper_full_with_state( | |
// [EXPERIMENTAL] Token-level timestamps with DTW | ||
const auto n_segments_before = state->result_all.size(); | ||
|
||
const bool is_no_speech = (state->no_speech_prob > params.no_speech_thold && | ||
best_decoder.sequence.avg_logprobs < params.logprob_thold); | ||
|
||
//WHISPER_LOG_DEBUG("prompt_init.size() = %d, prompt.size() = %d, result_len = %d, seek_delta = %d\n", prompt_init.size(), prompt.size(), result_len, seek_delta); | ||
|
||
// update prompt_past | ||
|
@@ -6076,11 +6110,11 @@ int whisper_full_with_state( | |
prompt_past.insert(prompt_past.end(), prompt.begin() + 1, prompt.end() - prompt_init.size()); | ||
} | ||
|
||
for (int i = 0; i < result_len; ++i) { | ||
for (int i = 0; i < result_len && !is_no_speech; ++i) { | ||
prompt_past.push_back(tokens_cur[i].id); | ||
} | ||
|
||
if (!tokens_cur.empty() && ctx->model.n_loaded > 0) { | ||
if (!tokens_cur.empty() && ctx->model.n_loaded > 0 && !is_no_speech) { | ||
int i0 = 0; | ||
auto t0 = seek + 2*(tokens_cur.front().tid - whisper_token_beg(ctx)); | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This likely has to be done inside
whisper_process_logits
in order to avoid computing the softmax again just for this probability.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately we cannot reuse the softmax computed inside
whisper_process_logits
sinceno_speech_prob
has to be calculated before any logits filtering. Otherwise we get some wrongno_speech_prob
values. The same method is followed in openai's whisper as well. https://github.com/openai/whisper/blob/main/whisper/decoding.py#L689-L703Since this
no_speech_prob
calculation is only for the first token in the sequence, it will not cause a big performance impact.On a related note, I have now modularized the probs calculation and now reusing the same code as
whisper_process_logits