Skip to content

Commit 252266f

Browse files
kossumokaris
authored andcommitted
fix: added n_ctx check for prompt requirements when embedding images in Gemma3ChatHandler
1 parent 7bdfb83 commit 252266f

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

llama_cpp/llama_chat_format.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3631,6 +3631,12 @@ def get_image_urls(messages: List[llama_types.ChatCompletionRequestMessage]):
36313631
def eval_image(self, llama: llama.Llama, image_url: str):
36323632
import llama_cpp
36333633

3634+
n_tokens = 256
3635+
if llama.n_tokens + n_tokens > llama.n_ctx():
3636+
raise ValueError(
3637+
f"Prompt exceeds n_ctx: {llama.n_tokens + n_tokens} > {llama.n_ctx()}"
3638+
)
3639+
36343640
img_bytes = self.load_image(image_url)
36353641
img_u8_p = self._llava_cpp.clip_image_u8_init()
36363642
if not self._llava_cpp.clip_image_load_from_bytes(
@@ -3649,7 +3655,6 @@ def eval_image(self, llama: llama.Llama, image_url: str):
36493655
raise ValueError("Failed to preprocess image.")
36503656

36513657
n_embd = llama_cpp.llama_model_n_embd(llama._model.model)
3652-
n_tokens = 256
36533658
embed = (ctypes.c_float * (n_tokens * n_embd))()
36543659
if not self._llava_cpp.clip_image_batch_encode(self.clip_ctx, llama.n_threads, img_f32_p, embed):
36553660
self._llava_cpp.clip_image_f32_batch_free(img_f32_p)

0 commit comments

Comments
 (0)