Skip to content

Commit 001de25

Browse files
committed
whisper : fix bug in prompt processing (close ggml-org#705)
Was dereferencing a dangling pointer
1 parent 99870cc commit 001de25

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

examples/main/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ void whisper_print_segment_callback(struct whisper_context * ctx, struct whisper
208208

209209
std::string speaker = "";
210210

211-
int64_t t0;
212-
int64_t t1;
211+
int64_t t0 = 0;
212+
int64_t t1 = 0;
213213

214214
// print the last n_new segments
215215
const int s0 = n_segments - n_new;

whisper.cpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,12 +1260,10 @@ static bool whisper_model_load(struct whisper_model_loader * loader, whisper_con
12601260
break;
12611261
}
12621262

1263-
int64_t nelements = 1;
1264-
int64_t ne[3] = { 1, 1, 1 };
1263+
int32_t nelements = 1;
1264+
int32_t ne[3] = { 1, 1, 1 };
12651265
for (int i = 0; i < n_dims; ++i) {
1266-
int32_t ne_cur;
1267-
read_safe(loader, ne_cur);
1268-
ne[i] = ne_cur;
1266+
read_safe(loader, ne[i]);
12691267
nelements *= ne[i];
12701268
}
12711269

@@ -1286,15 +1284,15 @@ static bool whisper_model_load(struct whisper_model_loader * loader, whisper_con
12861284
}
12871285

12881286
if (tensor->ne[0] != ne[0] || tensor->ne[1] != ne[1] || tensor->ne[2] != ne[2]) {
1289-
fprintf(stderr, "%s: tensor '%s' has wrong shape in model file: got [%lld, %lld, %lld], expected [%lld, %lld, %lld]\n",
1290-
__func__, name.data(), tensor->ne[0], tensor->ne[1], tensor->ne[2], ne[0], ne[1], ne[2]);
1287+
fprintf(stderr, "%s: tensor '%s' has wrong shape in model file: got [%d, %d, %d], expected [%d, %d, %d]\n",
1288+
__func__, name.data(), (int) tensor->ne[0], (int) tensor->ne[1], (int) tensor->ne[2], ne[0], ne[1], ne[2]);
12911289
return false;
12921290
}
12931291

12941292
const size_t bpe = (ftype == 0) ? sizeof(float) : sizeof(ggml_fp16_t);
12951293

12961294
if (nelements*bpe != ggml_nbytes(tensor)) {
1297-
fprintf(stderr, "%s: tensor '%s' has wrong size in model file: got %zu, expected %llu\n",
1295+
fprintf(stderr, "%s: tensor '%s' has wrong size in model file: got %zu, expected %zu\n",
12981296
__func__, name.data(), ggml_nbytes(tensor), nelements*bpe);
12991297
return false;
13001298
}
@@ -3819,22 +3817,26 @@ int whisper_full_with_state(
38193817
prompt_past.clear();
38203818
}
38213819

3822-
// initial prompt
3823-
if (!params.prompt_tokens && params.initial_prompt) {
3820+
// prepare prompt
3821+
{
38243822
std::vector<whisper_token> prompt_tokens;
3825-
prompt_tokens.resize(1024);
3826-
prompt_tokens.resize(whisper_tokenize(ctx, params.initial_prompt, prompt_tokens.data(), prompt_tokens.size()));
3827-
params.prompt_tokens = prompt_tokens.data();
3828-
params.prompt_n_tokens = prompt_tokens.size();
3829-
}
38303823

3831-
// prepend the prompt tokens to the prompt_past
3832-
if (params.prompt_tokens && params.prompt_n_tokens > 0) {
3833-
// parse tokens from the pointer
3834-
for (int i = 0; i < params.prompt_n_tokens; i++) {
3835-
prompt_past.push_back(params.prompt_tokens[i]);
3824+
// initial prompt
3825+
if (!params.prompt_tokens && params.initial_prompt) {
3826+
prompt_tokens.resize(1024);
3827+
prompt_tokens.resize(whisper_tokenize(ctx, params.initial_prompt, prompt_tokens.data(), prompt_tokens.size()));
3828+
params.prompt_tokens = prompt_tokens.data();
3829+
params.prompt_n_tokens = prompt_tokens.size();
3830+
}
3831+
3832+
// prepend the prompt tokens to the prompt_past
3833+
if (params.prompt_tokens && params.prompt_n_tokens > 0) {
3834+
// parse tokens from the pointer
3835+
for (int i = 0; i < params.prompt_n_tokens; i++) {
3836+
prompt_past.push_back(params.prompt_tokens[i]);
3837+
}
3838+
std::rotate(prompt_past.begin(), prompt_past.end() - params.prompt_n_tokens, prompt_past.end());
38363839
}
3837-
std::rotate(prompt_past.begin(), prompt_past.end() - params.prompt_n_tokens, prompt_past.end());
38383840
}
38393841

38403842
// overwrite audio_ctx, max allowed is hparams.n_audio_ctx

0 commit comments

Comments
 (0)