Skip to content

Commit 0d41771

Browse files
authored
llava : fix memory management bug (#5491)
* Fix memory management in llava and server code Fixes this error: llama_new_context_with_model: graph splits (measure): 3 Available slots: -> Slot 0 - max context: 6000 {"timestamp":1707926446,"level":"INFO","function":"main","line":2623,"message":"model loaded"} all slots are idle and system prompt is empty, clear the KV cache slot 0 - loaded image slot 0 is processing [task id: 0] slot 0 : kv cache rm - [0, end) slot 0 - encoding image [id: 1] munmap_chunk(): invalid pointer Aborted * Make it cleaner by checking size in batch free wrapper
1 parent 7930a8a commit 0d41771

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

examples/llava/clip.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,8 +1230,20 @@ struct clip_image_f32 * clip_image_f32_init() {
12301230
return new clip_image_f32();
12311231
}
12321232

1233-
void clip_image_u8_free (struct clip_image_u8 * img) { delete img; }
1233+
void clip_image_u8_free(struct clip_image_u8 * img) { delete img; }
12341234
void clip_image_f32_free(struct clip_image_f32 * img) { delete img; }
1235+
void clip_image_u8_batch_free(struct clip_image_u8_batch & batch) {
1236+
if (batch.size > 0) {
1237+
delete[] batch.data;
1238+
batch.size = 0;
1239+
}
1240+
}
1241+
void clip_image_f32_batch_free(struct clip_image_f32_batch & batch) {
1242+
if (batch.size > 0) {
1243+
delete[] batch.data;
1244+
batch.size = 0;
1245+
}
1246+
}
12351247

12361248
static void build_clip_img_from_data(const stbi_uc * data, int nx, int ny, clip_image_u8 * img) {
12371249
img->nx = nx;
@@ -1494,11 +1506,8 @@ bool clip_image_preprocess(struct clip_ctx * ctx, const clip_image_u8 * img, cli
14941506
pad_to_square = false;
14951507
}
14961508
// free the previous res_imgs if any set
1497-
if (res_imgs.size > 0 && res_imgs.size < 100) {
1498-
for (size_t i = 0; i < res_imgs.size; i++) {
1499-
clip_image_f32_free(&(res_imgs.data[i]));
1500-
}
1501-
delete[] res_imgs.data;
1509+
if (res_imgs.size > 0) {
1510+
clip_image_f32_batch_free(res_imgs);
15021511
}
15031512
res_imgs.data = nullptr;
15041513
res_imgs.size = 0;
@@ -1650,7 +1659,8 @@ bool clip_image_preprocess(struct clip_ctx * ctx, const clip_image_u8 * img, cli
16501659

16511660
res_imgs.size = 1;
16521661
res_imgs.data = new clip_image_f32[res_imgs.size];
1653-
res_imgs.data[0] = std::move(*res);
1662+
res_imgs.data[0] = *res;
1663+
clip_image_f32_free(res);
16541664

16551665
return true;
16561666
}

examples/llava/clip.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ CLIP_API struct clip_image_f32 * clip_image_f32_init();
6060

6161
CLIP_API void clip_image_u8_free (struct clip_image_u8 * img);
6262
CLIP_API void clip_image_f32_free(struct clip_image_f32 * img);
63+
CLIP_API void clip_image_u8_batch_free (struct clip_image_u8_batch & batch);
64+
CLIP_API void clip_image_f32_batch_free(struct clip_image_f32_batch & batch);
6365

6466
CLIP_API bool clip_image_load_from_file(const char * fname, struct clip_image_u8 * img);
6567

examples/server/server.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,12 @@ struct llama_server_context
975975
{
976976
LOG_TEE("Error processing the given image");
977977
clip_free(clp_ctx);
978-
clip_image_f32_free(img_res_v.data);
978+
clip_image_f32_batch_free(img_res_v);
979+
return false;
980+
}
981+
if (img_res_v.size == 0)
982+
{
983+
LOG_TEE("Error processing the given image");
979984
return false;
980985
}
981986

@@ -987,17 +992,19 @@ struct llama_server_context
987992
if (!img.image_embedding)
988993
{
989994
LOG_TEE("Unable to allocate memory for image embeddings\n");
995+
clip_image_f32_batch_free(img_res_v);
990996
clip_free(clp_ctx);
991997
return false;
992998
}
993999
LOG_TEE("slot %i - encoding image [id: %i]\n", slot.id, img.id);
9941000
if (!clip_image_encode(clp_ctx, params.n_threads, img_res, img.image_embedding))
9951001
{
9961002
LOG_TEE("Unable to encode image\n");
1003+
clip_image_f32_batch_free(img_res_v);
9971004
return false;
9981005
}
9991006

1000-
clip_image_f32_free(img_res_v.data);
1007+
clip_image_f32_batch_free(img_res_v);
10011008

10021009
img.request_encode_image = false;
10031010
}

0 commit comments

Comments
 (0)