Skip to content

Commit 1b45588

Browse files
committed
fix: added n_ctx check for prompt requirements when embedding images in Gemma3ChatHandler
1 parent 25b2f8f commit 1b45588

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

Diff for: llama_cpp/llama_chat_format.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -3467,6 +3467,12 @@ def get_image_urls(messages: List[llama_types.ChatCompletionRequestMessage]):
34673467
def eval_image(self, llama: llama.Llama, image_url: str):
34683468
import llama_cpp
34693469

3470+
n_tokens = 256
3471+
if llama.n_tokens + n_tokens > llama.n_ctx():
3472+
raise ValueError(
3473+
f"Prompt exceeds n_ctx: {llama.n_tokens + n_tokens} > {llama.n_ctx()}"
3474+
)
3475+
34703476
img_bytes = self.load_image(image_url)
34713477
img_u8_p = self._llava_cpp.clip_image_u8_init()
34723478
if not self._llava_cpp.clip_image_load_from_bytes(
@@ -3485,7 +3491,6 @@ def eval_image(self, llama: llama.Llama, image_url: str):
34853491
raise ValueError("Failed to preprocess image.")
34863492

34873493
n_embd = llama_cpp.llama_model_n_embd(llama._model.model)
3488-
n_tokens = 256
34893494
embed = (ctypes.c_float * (n_tokens * n_embd))()
34903495
if not self._llava_cpp.clip_image_batch_encode(self.clip_ctx, llama.n_threads, img_f32_p, embed):
34913496
self._llava_cpp.clip_image_f32_batch_free(img_f32_p)

0 commit comments

Comments
 (0)