@@ -323,36 +323,43 @@ struct clip_ctx {
323
323
std::vector<ggml_backend_t > backend_ptrs;
324
324
std::vector<ggml_backend_buffer_type_t > backend_buft;
325
325
326
- ggml_backend_ptr backend;
327
- ggml_backend_ptr backend_cpu;
326
+ ggml_backend_t backend;
327
+ ggml_backend_t backend_cpu;
328
328
ggml_backend_buffer_ptr buf;
329
329
330
330
ggml_backend_sched_ptr sched;
331
331
332
332
clip_image_size load_image_size;
333
333
334
334
clip_ctx (clip_context_params & ctx_params) {
335
- backend_cpu = ggml_backend_ptr ( ggml_backend_init_by_type (GGML_BACKEND_DEVICE_TYPE_CPU, nullptr ) );
336
- backend = ggml_backend_ptr ( ctx_params.use_gpu
335
+ backend_cpu = ggml_backend_init_by_type (GGML_BACKEND_DEVICE_TYPE_CPU, nullptr );
336
+ backend = ctx_params.use_gpu
337
337
? ggml_backend_init_by_type (GGML_BACKEND_DEVICE_TYPE_GPU, nullptr )
338
- : nullptr ) ;
338
+ : nullptr ;
339
339
340
340
if (backend) {
341
- LOG_INF (" %s: CLIP using %s backend\n " , __func__, ggml_backend_name (backend. get () ));
342
- backend_ptrs.push_back (backend. get () );
343
- backend_buft.push_back (ggml_backend_get_default_buffer_type (backend. get () ));
341
+ LOG_INF (" %s: CLIP using %s backend\n " , __func__, ggml_backend_name (backend));
342
+ backend_ptrs.push_back (backend);
343
+ backend_buft.push_back (ggml_backend_get_default_buffer_type (backend));
344
344
} else {
345
- backend = std::move ( backend_cpu) ;
345
+ backend = backend_cpu;
346
346
LOG_INF (" %s: CLIP using CPU backend\n " , __func__);
347
347
}
348
348
349
- backend_ptrs.push_back (backend_cpu. get () );
350
- backend_buft.push_back (ggml_backend_get_default_buffer_type (backend_cpu. get () ));
349
+ backend_ptrs.push_back (backend_cpu);
350
+ backend_buft.push_back (ggml_backend_get_default_buffer_type (backend_cpu));
351
351
352
352
sched.reset (
353
353
ggml_backend_sched_new (backend_ptrs.data (), backend_buft.data (), backend_ptrs.size (), 8192 , false )
354
354
);
355
355
}
356
+
357
+ ~clip_ctx () {
358
+ ggml_backend_free (backend);
359
+ if (backend != backend_cpu) {
360
+ ggml_backend_free (backend_cpu);
361
+ }
362
+ }
356
363
};
357
364
358
365
static ggml_cgraph * clip_image_build_graph_siglip (clip_ctx * ctx, const clip_image_f32_batch & imgs) {
@@ -1428,7 +1435,7 @@ struct clip_model_loader {
1428
1435
}
1429
1436
1430
1437
// alloc memory and offload data
1431
- ggml_backend_buffer_type_t buft = ggml_backend_get_default_buffer_type (ctx_clip.backend . get () );
1438
+ ggml_backend_buffer_type_t buft = ggml_backend_get_default_buffer_type (ctx_clip.backend );
1432
1439
ctx_clip.buf .reset (ggml_backend_alloc_ctx_tensors_from_buft (ctx_clip.ctx_data .get (), buft));
1433
1440
ggml_backend_buffer_set_usage (ctx_clip.buf .get (), GGML_BACKEND_BUFFER_USAGE_WEIGHTS);
1434
1441
for (auto & t : tensors_to_load) {
@@ -2610,7 +2617,7 @@ bool clip_image_batch_encode(clip_ctx * ctx, const int n_threads, const clip_ima
2610
2617
}
2611
2618
}
2612
2619
2613
- ggml_backend_cpu_set_n_threads (ctx->backend_cpu . get () , n_threads);
2620
+ ggml_backend_cpu_set_n_threads (ctx->backend_cpu , n_threads);
2614
2621
2615
2622
auto status = ggml_backend_sched_graph_compute (ctx->sched .get (), gf);
2616
2623
if (status != GGML_STATUS_SUCCESS) {
0 commit comments