From 74c260fe34b93c5996497eeaccae23217dcce800 Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Mon, 12 Feb 2024 15:19:59 +0200 Subject: [PATCH] whisper : fix usage of extenral encoders (e.g. CoreML) --- whisper.cpp | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/whisper.cpp b/whisper.cpp index dec99570..d8d3b84c 100644 --- a/whisper.cpp +++ b/whisper.cpp @@ -1659,25 +1659,19 @@ static struct ggml_cgraph * whisper_build_graph_conv( ggml_set_name(cur, "embd_conv"); wstate.embd_conv = cur; } else { -#ifdef WHISPER_USE_COREML + // keep the "mel" tensor alive - we will use it to store the input data for the external encoders + // TODO: is there a better way to do this + mel = ggml_scale(ctx0, mel, 1.0f); + ggml_build_forward_expand(gf, mel); + cur = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_state, n_ctx); - ggml_allocr_alloc(alloc, cur); - - if (!ggml_allocr_is_measure(alloc)) { - whisper_coreml_encode(wstate.ctx_coreml, mel->ne[0], mel->ne[1], (float *) mel->data, (float *) cur->data); - } -#endif -#ifdef WHISPER_USE_OPENVINO - cur = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_state, n_ctx); - ggml_allocr_alloc(alloc, cur); - - if (!ggml_allocr_is_measure(alloc)) { - whisper_openvino_encode(wstate.ctx_openvino, mel, cur); - } -#endif - ggml_set_name(cur, "embd_enc"); + ggml_set_output(cur); wstate.embd_enc = cur; + + // TODO: without this op, the "embd_enc" tensor ends up being not allocated + // is there a better fix? + cur = ggml_scale(ctx0, cur, 1.0f); } ggml_build_forward_expand(gf, cur); @@ -2037,13 +2031,13 @@ static bool whisper_encode_internal( return false; } + struct ggml_tensor * mel = ggml_graph_get_tensor(gf, "mel"); + // set the input { const auto & mel_inp = wstate.mel; const int n_ctx = wstate.exp_n_audio_ctx > 0 ? wstate.exp_n_audio_ctx : wctx.model.hparams.n_audio_ctx; - struct ggml_tensor * mel = ggml_graph_get_tensor(gf, "mel"); - assert(mel->type == GGML_TYPE_F32); assert(mel_inp.n_mel == wctx.model.hparams.n_mels); @@ -2068,6 +2062,12 @@ static bool whisper_encode_internal( if (!ggml_graph_compute_helper(wstate.backend, gf, n_threads)) { return false; } + } else { +#if defined(WHISPER_USE_COREML) + whisper_coreml_encode(wstate.ctx_coreml, mel->ne[0], mel->ne[1], (float *) mel->data, (float *) wstate.embd_enc->data); +#elif defined(WHISPER_USE_OPENVINO) + whisper_openvino_encode(wstate.ctx_openvino, mel, wstate.embd_enc); +#endif } }