whisper : fix bug in prompt processing (close #705)

Was dereferencing a dangling pointer
This commit is contained in:
Georgi Gerganov 2023-04-14 19:16:34 +03:00
parent 6704a81255
commit 514cd04452
2 changed files with 25 additions and 23 deletions

View File

@ -208,8 +208,8 @@ void whisper_print_segment_callback(struct whisper_context * ctx, struct whisper
std::string speaker = ""; std::string speaker = "";
int64_t t0; int64_t t0 = 0;
int64_t t1; int64_t t1 = 0;
// print the last n_new segments // print the last n_new segments
const int s0 = n_segments - n_new; const int s0 = n_segments - n_new;

View File

@ -1260,12 +1260,10 @@ static bool whisper_model_load(struct whisper_model_loader * loader, whisper_con
break; break;
} }
int64_t nelements = 1; int32_t nelements = 1;
int64_t ne[3] = { 1, 1, 1 }; int32_t ne[3] = { 1, 1, 1 };
for (int i = 0; i < n_dims; ++i) { for (int i = 0; i < n_dims; ++i) {
int32_t ne_cur; read_safe(loader, ne[i]);
read_safe(loader, ne_cur);
ne[i] = ne_cur;
nelements *= ne[i]; nelements *= ne[i];
} }
@ -1286,15 +1284,15 @@ static bool whisper_model_load(struct whisper_model_loader * loader, whisper_con
} }
if (tensor->ne[0] != ne[0] || tensor->ne[1] != ne[1] || tensor->ne[2] != ne[2]) { if (tensor->ne[0] != ne[0] || tensor->ne[1] != ne[1] || tensor->ne[2] != ne[2]) {
fprintf(stderr, "%s: tensor '%s' has wrong shape in model file: got [%lld, %lld, %lld], expected [%lld, %lld, %lld]\n", fprintf(stderr, "%s: tensor '%s' has wrong shape in model file: got [%d, %d, %d], expected [%d, %d, %d]\n",
__func__, name.data(), tensor->ne[0], tensor->ne[1], tensor->ne[2], ne[0], ne[1], ne[2]); __func__, name.data(), (int) tensor->ne[0], (int) tensor->ne[1], (int) tensor->ne[2], ne[0], ne[1], ne[2]);
return false; return false;
} }
const size_t bpe = (ftype == 0) ? sizeof(float) : sizeof(ggml_fp16_t); const size_t bpe = (ftype == 0) ? sizeof(float) : sizeof(ggml_fp16_t);
if (nelements*bpe != ggml_nbytes(tensor)) { if (nelements*bpe != ggml_nbytes(tensor)) {
fprintf(stderr, "%s: tensor '%s' has wrong size in model file: got %zu, expected %llu\n", fprintf(stderr, "%s: tensor '%s' has wrong size in model file: got %zu, expected %zu\n",
__func__, name.data(), ggml_nbytes(tensor), nelements*bpe); __func__, name.data(), ggml_nbytes(tensor), nelements*bpe);
return false; return false;
} }
@ -3819,9 +3817,12 @@ int whisper_full_with_state(
prompt_past.clear(); prompt_past.clear();
} }
// prepare prompt
{
std::vector<whisper_token> prompt_tokens;
// initial prompt // initial prompt
if (!params.prompt_tokens && params.initial_prompt) { if (!params.prompt_tokens && params.initial_prompt) {
std::vector<whisper_token> prompt_tokens;
prompt_tokens.resize(1024); prompt_tokens.resize(1024);
prompt_tokens.resize(whisper_tokenize(ctx, params.initial_prompt, prompt_tokens.data(), prompt_tokens.size())); prompt_tokens.resize(whisper_tokenize(ctx, params.initial_prompt, prompt_tokens.data(), prompt_tokens.size()));
params.prompt_tokens = prompt_tokens.data(); params.prompt_tokens = prompt_tokens.data();
@ -3836,6 +3837,7 @@ int whisper_full_with_state(
} }
std::rotate(prompt_past.begin(), prompt_past.end() - params.prompt_n_tokens, prompt_past.end()); std::rotate(prompt_past.begin(), prompt_past.end() - params.prompt_n_tokens, prompt_past.end());
} }
}
// overwrite audio_ctx, max allowed is hparams.n_audio_ctx // overwrite audio_ctx, max allowed is hparams.n_audio_ctx
if (params.audio_ctx > whisper_n_audio_ctx(ctx)) { if (params.audio_ctx > whisper_n_audio_ctx(ctx)) {