Skip to content

Commit 925915a

Browse files
authored
whisper : move progress calculation out of whisper.cpp (#1081)
Current `progress_step` was hardcoded into whisper.cpp, this resulted in bindings having to access progress only at that step even if progress callback was being called at every iteration. With this change we get greater granularity progress reporting from whisper.cpp and bindings/implementations can define their own progress step.
1 parent 97f4a7f commit 925915a

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

examples/main/main.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ struct whisper_params {
5959
int32_t offset_t_ms = 0;
6060
int32_t offset_n = 0;
6161
int32_t duration_ms = 0;
62+
int32_t progress_step = 5;
6263
int32_t max_context = -1;
6364
int32_t max_len = 0;
6465
int32_t best_of = 2;
@@ -218,6 +219,7 @@ struct whisper_print_user_data {
218219
const whisper_params * params;
219220

220221
const std::vector<std::vector<float>> * pcmf32s;
222+
int progress_prev;
221223
};
222224

223225
std::string estimate_diarization_speaker(std::vector<std::vector<float>> pcmf32s, int64_t t0, int64_t t1, bool id_only = false) {
@@ -252,6 +254,14 @@ std::string estimate_diarization_speaker(std::vector<std::vector<float>> pcmf32s
252254

253255
return speaker;
254256
}
257+
void whisper_print_progress_callback(struct whisper_context * ctx, struct whisper_state * /*state*/, int progress, void * user_data) {
258+
int progress_step = ((whisper_print_user_data *) user_data)->params->progress_step;
259+
int * progress_prev = &(((whisper_print_user_data *) user_data)->progress_prev);
260+
if (progress >= *progress_prev + progress_step) {
261+
*progress_prev += progress_step;
262+
fprintf(stderr, "%s: progress = %3d%%\n", __func__, progress);
263+
}
264+
}
255265

256266
void whisper_print_segment_callback(struct whisper_context * ctx, struct whisper_state * /*state*/, int n_new, void * user_data) {
257267
const auto & params = *((whisper_print_user_data *) user_data)->params;
@@ -895,14 +905,19 @@ int main(int argc, char ** argv) {
895905
wparams.entropy_thold = params.entropy_thold;
896906
wparams.logprob_thold = params.logprob_thold;
897907

898-
whisper_print_user_data user_data = { &params, &pcmf32s };
908+
whisper_print_user_data user_data = { &params, &pcmf32s, 0 };
899909

900910
// this callback is called on each new segment
901911
if (!wparams.print_realtime) {
902912
wparams.new_segment_callback = whisper_print_segment_callback;
903913
wparams.new_segment_callback_user_data = &user_data;
904914
}
905915

916+
if (wparams.print_progress) {
917+
wparams.progress_callback = whisper_print_progress_callback;
918+
wparams.progress_callback_user_data = &user_data;
919+
}
920+
906921
// example for abort mechanism
907922
// in this example, we do not abort the processing, but we could if the flag is set to true
908923
// the callback is called before every encoder run - if it returns false, the processing is aborted

whisper.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4163,9 +4163,6 @@ int whisper_full_with_state(
41634163
}
41644164
}
41654165

4166-
int progress_prev = 0;
4167-
int progress_step = 5;
4168-
41694166
int seek = seek_start;
41704167

41714168
std::vector<whisper_token> prompt;
@@ -4193,15 +4190,9 @@ int whisper_full_with_state(
41934190
// main loop
41944191
while (true) {
41954192
const int progress_cur = (100*(seek - seek_start))/(seek_end - seek_start);
4196-
while (progress_cur >= progress_prev + progress_step) {
4197-
progress_prev += progress_step;
4198-
if (params.print_progress) {
4199-
fprintf(stderr, "%s: progress = %3d%%\n", __func__, progress_prev);
4200-
}
4201-
}
42024193
if (params.progress_callback) {
42034194
params.progress_callback(
4204-
ctx, ctx->state, progress_prev, params.progress_callback_user_data);
4195+
ctx, ctx->state, progress_cur, params.progress_callback_user_data);
42054196
}
42064197

42074198
// of only 1 second left, then stop

0 commit comments

Comments
 (0)