Skip to content

Commit

Permalink
whisper : abort callback improvements (ggerganov#1345)
Browse files Browse the repository at this point in the history
* whisper : initialize abort_callback to null

* whisper : add example how to use abort_callback
  • Loading branch information
mkiol authored Oct 8, 2023
1 parent 1b775cd commit 940cdb1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
16 changes: 14 additions & 2 deletions examples/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -944,8 +944,9 @@ int main(int argc, char ** argv) {
wparams.progress_callback_user_data = &user_data;
}

// example for abort mechanism
// in this example, we do not abort the processing, but we could if the flag is set to true
// examples for abort mechanism
// in examples below, we do not abort the processing, but we could if the flag is set to true

// the callback is called before every encoder run - if it returns false, the processing is aborted
{
static bool is_aborted = false; // NOTE: this should be atomic to avoid data race
Expand All @@ -957,6 +958,17 @@ int main(int argc, char ** argv) {
wparams.encoder_begin_callback_user_data = &is_aborted;
}

// the callback is called before every computation - if it returns true, the computation is aborted
{
static bool is_aborted = false; // NOTE: this should be atomic to avoid data race

wparams.abort_callback = [](void * user_data) {
bool is_aborted = *(bool*)user_data;
return is_aborted;
};
wparams.abort_callback_user_data = &is_aborted;
}

if (whisper_full_parallel(ctx, wparams, pcmf32.data(), pcmf32.size(), params.n_processors) != 0) {
fprintf(stderr, "%s: failed to process audio\n", argv[0]);
return 10;
Expand Down
3 changes: 3 additions & 0 deletions whisper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3773,6 +3773,9 @@ struct whisper_full_params whisper_full_default_params(enum whisper_sampling_str
/*.encoder_begin_callback =*/ nullptr,
/*.encoder_begin_callback_user_data =*/ nullptr,

/*.abort_callback =*/ nullptr,
/*.abort_callback_user_data =*/ nullptr,

/*.logits_filter_callback =*/ nullptr,
/*.logits_filter_callback_user_data =*/ nullptr,
};
Expand Down

0 comments on commit 940cdb1

Please sign in to comment.