@@ -1693,11 +1693,11 @@ static bool whisper_encode_internal(
16931693 // ggml_graph_print(&gf);
16941694 }
16951695#else
1696- wctx .use_buf (ctx0, -1 );
1696+ wstate .use_buf (ctx0, -1 );
16971697
16981698 struct ggml_tensor * cur = ggml_new_tensor_2d (ctx0, GGML_TYPE_F32, n_state, n_ctx);
16991699
1700- whisper_coreml_encode (wctx .ctx_coreml , (float *) mel->data , (float *) cur->data );
1700+ whisper_coreml_encode (wstate .ctx_coreml , (float *) mel->data , (float *) cur->data );
17011701#endif
17021702
17031703 // cur
@@ -2491,6 +2491,20 @@ static std::vector<whisper_vocab::id> tokenize(const whisper_vocab & vocab, cons
24912491// interface implementation
24922492//
24932493
2494+ #ifdef WHISPER_USE_COREML
2495+ // replace .bin with .mlmodelc
2496+ static std::string whisper_get_coreml_path (std::string path_bin) {
2497+ auto pos = path_bin.rfind (' .' );
2498+ if (pos != std::string::npos) {
2499+ path_bin = path_bin.substr (0 , pos);
2500+ }
2501+
2502+ path_bin += " .mlmodelc" ;
2503+
2504+ return path_bin;
2505+ }
2506+ #endif
2507+
24942508struct whisper_state * whisper_init_state (whisper_context * ctx) {
24952509 whisper_state * state = new whisper_state;
24962510
@@ -2518,6 +2532,21 @@ struct whisper_state * whisper_init_state(whisper_context * ctx) {
25182532 fprintf (stderr, " %s: kv cross size = %7.2f MB\n " , __func__, memory_size / 1024.0 / 1024.0 );
25192533 }
25202534
2535+ #ifdef WHISPER_USE_COREML
2536+ const auto path_coreml = whisper_get_coreml_path (ctx->path_model );
2537+
2538+ fprintf (stderr, " %s: loading Core ML model from '%s'\n " , __func__, path_coreml.c_str ());
2539+ fprintf (stderr, " %s: first run on a device may take a while ...\n " , __func__);
2540+
2541+ state->ctx_coreml = whisper_coreml_init (path_coreml.c_str ());
2542+ if (!state->ctx_coreml ) {
2543+ fprintf (stderr, " %s: failed to load Core ML model from '%s'\n " , __func__, path_coreml.c_str ());
2544+ return nullptr ;
2545+ }
2546+
2547+ fprintf (stderr, " %s: Core ML model loaded\n " , __func__);
2548+ #endif
2549+
25212550 state->logits .reserve (ctx->vocab .n_vocab * ctx->model .hparams .n_text_ctx );
25222551
25232552 state->logits_id .reserve (ctx->model .hparams .n_vocab );
@@ -2540,20 +2569,6 @@ struct whisper_state * whisper_init_state(whisper_context * ctx) {
25402569 return state;
25412570}
25422571
2543- #ifdef WHISPER_USE_COREML
2544- // replace .bin with .mlmodelc
2545- static std::string whisper_get_coreml_path (std::string path_bin) {
2546- auto pos = path_bin.rfind (' .' );
2547- if (pos != std::string::npos) {
2548- path_bin = path_bin.substr (0 , pos);
2549- }
2550-
2551- path_bin += " .mlmodelc" ;
2552-
2553- return path_bin;
2554- }
2555- #endif
2556-
25572572struct whisper_context * whisper_init_from_file_no_state (const char * path_model) {
25582573 whisper_model_loader loader = {};
25592574
@@ -2587,19 +2602,6 @@ struct whisper_context * whisper_init_from_file_no_state(const char * path_model
25872602
25882603 if (ctx) {
25892604 ctx->path_model = path_model;
2590- #ifdef WHISPER_USE_COREML
2591- const auto path_coreml = whisper_get_coreml_path (ctx->path_model );
2592- fprintf (stderr, " %s: loading Core ML model from '%s'\n " , __func__, path_coreml.c_str ());
2593- fprintf (stderr, " %s: first run on a device may take a while ...\n " , __func__);
2594-
2595- ctx->ctx_coreml = whisper_coreml_init (path_coreml.c_str ());
2596- if (!ctx->ctx_coreml ) {
2597- fprintf (stderr, " %s: failed to load Core ML model from '%s'\n " , __func__, path_coreml.c_str ());
2598- return nullptr ;
2599- }
2600-
2601- fprintf (stderr, " %s: Core ML model loaded\n " , __func__);
2602- #endif
26032605 }
26042606
26052607 return ctx;
@@ -2712,6 +2714,11 @@ void whisper_free_state(struct whisper_state * state)
27122714 kv_cache_free (state->decoders [i].kv_self );
27132715 }
27142716
2717+ #ifdef WHISPER_USE_COREML
2718+ whisper_coreml_free (state->ctx_coreml );
2719+ state->ctx_coreml = nullptr ;
2720+ #endif
2721+
27152722 delete state;
27162723 }
27172724}
@@ -2727,10 +2734,6 @@ void whisper_free(struct whisper_context * ctx) {
27272734
27282735 whisper_free_state (ctx->state );
27292736
2730- #ifdef WHISPER_USE_COREML
2731- whisper_coreml_free (ctx->ctx_coreml );
2732- ctx->ctx_coreml = nullptr ;
2733- #endif
27342737 delete ctx;
27352738 }
27362739}
0 commit comments