whisper : allow non-CoreML fallback when Core ML cannot be loaded (#812)

if the Core ML model cannot be loaded, continue without Core ML instead of
returning. This allows a single build to transcribe using Core ML models
where available, and regular models when not.
This commit is contained in:
Canis Lupus 2023-04-29 08:49:02 +01:00 committed by GitHub
parent 3e82ff4747
commit 94a7cd2a07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -592,7 +592,7 @@ struct whisper_state {
std::string path_model; // populated by whisper_init_from_file() std::string path_model; // populated by whisper_init_from_file()
#ifdef WHISPER_USE_COREML #ifdef WHISPER_USE_COREML
whisper_coreml_context * ctx_coreml; whisper_coreml_context * ctx_coreml = nullptr;
#endif #endif
// [EXPERIMENTAL] token-level timestamps data // [EXPERIMENTAL] token-level timestamps data
@ -1385,9 +1385,16 @@ static bool whisper_encode_internal(
} }
} }
#ifndef WHISPER_USE_COREML
struct ggml_tensor * cur; struct ggml_tensor * cur;
#ifndef WHISPER_USE_COREML
const bool use_coreml = false;
#else
const bool use_coreml = wstate.ctx_coreml != nullptr;
#endif
if (!use_coreml)
{
// convolution + gelu // convolution + gelu
{ {
wstate.use_buf(ctx0, 1); wstate.use_buf(ctx0, 1);
@ -1693,12 +1700,16 @@ static bool whisper_encode_internal(
//ggml_graph_print(&gf); //ggml_graph_print(&gf);
} }
#else }
#ifdef WHISPER_USE_COREML
else
{
wstate.use_buf(ctx0, -1); wstate.use_buf(ctx0, -1);
struct ggml_tensor * cur = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_state, n_ctx); cur = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_state, n_ctx);
whisper_coreml_encode(wstate.ctx_coreml, (float *) mel->data, (float *) cur->data); whisper_coreml_encode(wstate.ctx_coreml, (float *) mel->data, (float *) cur->data);
}
#endif #endif
// cur // cur
@ -2569,10 +2580,12 @@ struct whisper_state * whisper_init_state(whisper_context * ctx) {
state->ctx_coreml = whisper_coreml_init(path_coreml.c_str()); state->ctx_coreml = whisper_coreml_init(path_coreml.c_str());
if (!state->ctx_coreml) { if (!state->ctx_coreml) {
fprintf(stderr, "%s: failed to load Core ML model from '%s'\n", __func__, path_coreml.c_str()); fprintf(stderr, "%s: failed to load Core ML model from '%s'\n", __func__, path_coreml.c_str());
#ifndef WHISPER_COREML_ALLOW_FALLBACK
return nullptr; return nullptr;
} #endif
} else {
fprintf(stderr, "%s: Core ML model loaded\n", __func__); fprintf(stderr, "%s: Core ML model loaded\n", __func__);
}
#endif #endif
state->logits.reserve(ctx->vocab.n_vocab * ctx->model.hparams.n_text_ctx); state->logits.reserve(ctx->vocab.n_vocab * ctx->model.hparams.n_text_ctx);
@ -2745,8 +2758,10 @@ void whisper_free_state(struct whisper_state * state)
} }
#ifdef WHISPER_USE_COREML #ifdef WHISPER_USE_COREML
if (state->ctx_coreml != nullptr) {
whisper_coreml_free(state->ctx_coreml); whisper_coreml_free(state->ctx_coreml);
state->ctx_coreml = nullptr; state->ctx_coreml = nullptr;
}
#endif #endif
delete state; delete state;