Spaces:
Sleeping
Sleeping
Bader-eddine Ouaich
commited on
whisper : fix potential memory leaks (#740)
Browse files* fix potential memory leak if whisper_init_state failed
* fix potential memory leak if gpt2_init failed
- examples/talk.wasm/gpt-2.cpp +1 -0
- whisper.cpp +2 -0
examples/talk.wasm/gpt-2.cpp
CHANGED
|
@@ -841,6 +841,7 @@ struct gpt2_context * gpt2_init(const char * path_model) {
|
|
| 841 |
|
| 842 |
if (!gpt2_model_load(path_model, ctx->model, ctx->vocab)) {
|
| 843 |
fprintf(stderr, "%s: failed to load model from '%s'\n", __func__, "gpt-2.bin");
|
|
|
|
| 844 |
return nullptr;
|
| 845 |
}
|
| 846 |
|
|
|
|
| 841 |
|
| 842 |
if (!gpt2_model_load(path_model, ctx->model, ctx->vocab)) {
|
| 843 |
fprintf(stderr, "%s: failed to load model from '%s'\n", __func__, "gpt-2.bin");
|
| 844 |
+
delete ctx;
|
| 845 |
return nullptr;
|
| 846 |
}
|
| 847 |
|
whisper.cpp
CHANGED
|
@@ -2487,6 +2487,7 @@ struct whisper_state * whisper_init_state(whisper_context * ctx) {
|
|
| 2487 |
|
| 2488 |
if (!kv_cache_init(ctx->model.hparams, scale * MEM_REQ_KV_SELF.at(ctx->model.type), state->decoders[0].kv_self, ctx->wtype, ctx->model.hparams.n_text_ctx)) {
|
| 2489 |
fprintf(stderr, "%s: kv_cache_init() failed for self-attention cache\n", __func__);
|
|
|
|
| 2490 |
return nullptr;
|
| 2491 |
}
|
| 2492 |
|
|
@@ -2497,6 +2498,7 @@ struct whisper_state * whisper_init_state(whisper_context * ctx) {
|
|
| 2497 |
|
| 2498 |
if (!kv_cache_init(ctx->model.hparams, scale * MEM_REQ_KV_CROSS.at(ctx->model.type), state->kv_cross, ctx->wtype, ctx->model.hparams.n_audio_ctx)) {
|
| 2499 |
fprintf(stderr, "%s: kv_cache_init() failed for cross-attention cache\n", __func__);
|
|
|
|
| 2500 |
return nullptr;
|
| 2501 |
}
|
| 2502 |
|
|
|
|
| 2487 |
|
| 2488 |
if (!kv_cache_init(ctx->model.hparams, scale * MEM_REQ_KV_SELF.at(ctx->model.type), state->decoders[0].kv_self, ctx->wtype, ctx->model.hparams.n_text_ctx)) {
|
| 2489 |
fprintf(stderr, "%s: kv_cache_init() failed for self-attention cache\n", __func__);
|
| 2490 |
+
delete state;
|
| 2491 |
return nullptr;
|
| 2492 |
}
|
| 2493 |
|
|
|
|
| 2498 |
|
| 2499 |
if (!kv_cache_init(ctx->model.hparams, scale * MEM_REQ_KV_CROSS.at(ctx->model.type), state->kv_cross, ctx->wtype, ctx->model.hparams.n_audio_ctx)) {
|
| 2500 |
fprintf(stderr, "%s: kv_cache_init() failed for cross-attention cache\n", __func__);
|
| 2501 |
+
delete state;
|
| 2502 |
return nullptr;
|
| 2503 |
}
|
| 2504 |
|