@@ -380,6 +380,9 @@ struct clip_ctx {
380380 // for debugging
381381 bool debug_graph = false ;
382382 std::vector<ggml_tensor *> debug_print_tensors;
383+
384+ // ANE model path for iOS
385+ std::string ane_model_path;
383386
384387 clip_ctx (clip_context_params & ctx_params) {
385388 debug_graph = std::getenv (" MTMD_DEBUG_GRAPH" ) != nullptr ;
@@ -3803,15 +3806,27 @@ static std::vector<std::vector<float>> get_2d_sincos_pos_embed(int embed_dim, co
38033806}
38043807
38053808#ifdef __APPLE__
3806- static bool clip_image_encode_ane (float * data, float * vec) {
3809+ static bool clip_image_encode_ane (float * data, float * vec, const char * ane_model_path ) {
38073810
38083811 static int flag = 0 ;
38093812 static const void * coremlEncoder = NULL ;
3810- if (flag == 0 ) {
3811- coremlEncoder = loadModel ();
3813+ static std::string cached_model_path = " " ;
3814+
3815+ // Check if we need to load a new model
3816+ if (flag == 0 || (ane_model_path && cached_model_path != ane_model_path)) {
3817+ if (coremlEncoder) {
3818+ closeModel (coremlEncoder);
3819+ }
3820+ coremlEncoder = loadModel (ane_model_path);
3821+ if (!coremlEncoder) {
3822+ printf (" Failed to load ANE model from: %s\n " , ane_model_path ? ane_model_path : " null" );
3823+ return false ;
3824+ }
3825+ cached_model_path = ane_model_path ? ane_model_path : " " ;
38123826 flag = 1 ;
38133827 }
38143828 predictWith (coremlEncoder, data, vec);
3829+ return true ;
38153830}
38163831#endif
38173832
@@ -3829,7 +3844,7 @@ bool clip_image_encode(struct clip_ctx * ctx, const int n_threads, clip_image_f3
38293844 float * vit_embedding2 = (float *)malloc (1100 *1152 *sizeof (float ));
38303845
38313846 ane_embedding (ctx, n_threads, &imgs, vit_embedding1);
3832- clip_image_encode_ane (vit_embedding1, vit_embedding2);
3847+ clip_image_encode_ane (vit_embedding1, vit_embedding2, ctx-> ane_model_path . c_str () );
38333848 ane_resampler (ctx, n_threads, &imgs, vit_embedding2, vec);
38343849 free (vit_embedding1);
38353850 free (vit_embedding2);
@@ -4634,3 +4649,9 @@ void clip_image_f32_batch_add_mel(struct clip_image_f32_batch * batch, int n_mel
46344649 batch->entries .push_back (clip_image_f32_ptr (audio));
46354650 batch->is_audio = true ;
46364651}
4652+
4653+ void clip_set_ane_model_path (struct clip_ctx * ctx, const char * ane_model_path) {
4654+ if (ctx && ane_model_path) {
4655+ ctx->ane_model_path = ane_model_path;
4656+ }
4657+ }
0 commit comments