Make it cleaner by checking size in batch free wrapper

This commit is contained in:
Elbios 2024-02-14 19:35:23 +01:00
parent ea74ba9116
commit 098ab943d8
3 changed files with 20 additions and 22 deletions

View file

@ -1230,10 +1230,20 @@ struct clip_image_f32 * clip_image_f32_init() {
return new clip_image_f32(); return new clip_image_f32();
} }
void clip_image_u8_free (struct clip_image_u8 * img) { delete img; } void clip_image_u8_free(struct clip_image_u8 * img) { delete img; }
void clip_image_f32_free(struct clip_image_f32 * img) { delete img; } void clip_image_f32_free(struct clip_image_f32 * img) { delete img; }
void clip_image_u8_batch_free (struct clip_image_u8 * data) { delete[] data; } void clip_image_u8_batch_free(struct clip_image_u8_batch & batch) {
void clip_image_f32_batch_free(struct clip_image_f32 * data) { delete[] data; } if (batch.size > 0) {
delete[] batch.data;
batch.size = 0;
}
}
void clip_image_f32_batch_free(struct clip_image_f32_batch & batch) {
if (batch.size > 0) {
delete[] batch.data;
batch.size = 0;
}
}
static void build_clip_img_from_data(const stbi_uc * data, int nx, int ny, clip_image_u8 * img) { static void build_clip_img_from_data(const stbi_uc * data, int nx, int ny, clip_image_u8 * img) {
img->nx = nx; img->nx = nx;
@ -1497,7 +1507,7 @@ bool clip_image_preprocess(struct clip_ctx * ctx, const clip_image_u8 * img, cli
} }
// free the previous res_imgs if any set // free the previous res_imgs if any set
if (res_imgs.size > 0) { if (res_imgs.size > 0) {
clip_image_f32_batch_free(res_imgs.data); clip_image_f32_batch_free(res_imgs);
} }
res_imgs.data = nullptr; res_imgs.data = nullptr;
res_imgs.size = 0; res_imgs.size = 0;

View file

@ -60,8 +60,8 @@ CLIP_API struct clip_image_f32 * clip_image_f32_init();
CLIP_API void clip_image_u8_free (struct clip_image_u8 * img); CLIP_API void clip_image_u8_free (struct clip_image_u8 * img);
CLIP_API void clip_image_f32_free(struct clip_image_f32 * img); CLIP_API void clip_image_f32_free(struct clip_image_f32 * img);
CLIP_API void clip_image_u8_batch_free (struct clip_image_u8 * data); CLIP_API void clip_image_u8_batch_free (struct clip_image_u8_batch & batch);
CLIP_API void clip_image_f32_batch_free(struct clip_image_f32 * data); CLIP_API void clip_image_f32_batch_free(struct clip_image_f32_batch & batch);
CLIP_API bool clip_image_load_from_file(const char * fname, struct clip_image_u8 * img); CLIP_API bool clip_image_load_from_file(const char * fname, struct clip_image_u8 * img);

View file

@ -975,10 +975,7 @@ struct llama_server_context
{ {
LOG_TEE("Error processing the given image"); LOG_TEE("Error processing the given image");
clip_free(clp_ctx); clip_free(clp_ctx);
if (img_res_v.size > 0) clip_image_f32_batch_free(img_res_v);
{
clip_image_f32_batch_free(img_res_v.data);
}
return false; return false;
} }
if (img_res_v.size == 0) if (img_res_v.size == 0)
@ -995,10 +992,7 @@ struct llama_server_context
if (!img.image_embedding) if (!img.image_embedding)
{ {
LOG_TEE("Unable to allocate memory for image embeddings\n"); LOG_TEE("Unable to allocate memory for image embeddings\n");
if (img_res_v.size > 0) clip_image_f32_batch_free(img_res_v);
{
clip_image_f32_batch_free(img_res_v.data);
}
clip_free(clp_ctx); clip_free(clp_ctx);
return false; return false;
} }
@ -1006,17 +1000,11 @@ struct llama_server_context
if (!clip_image_encode(clp_ctx, params.n_threads, img_res, img.image_embedding)) if (!clip_image_encode(clp_ctx, params.n_threads, img_res, img.image_embedding))
{ {
LOG_TEE("Unable to encode image\n"); LOG_TEE("Unable to encode image\n");
if (img_res_v.size > 0) clip_image_f32_batch_free(img_res_v);
{
clip_image_f32_batch_free(img_res_v.data);
}
return false; return false;
} }
if (img_res_v.size > 0) clip_image_f32_batch_free(img_res_v);
{
clip_image_f32_batch_free(img_res_v.data);
}
img.request_encode_image = false; img.request_encode_image = false;
} }