Add vision support to LlamaLanguageModel via mtmd - #213
Conversation
|
Hi @james-333i. Thanks for these four — I read the whole stack today. #195 is in, so this needs a rebase first (
The |
303ba40 to
10607e9
Compare
|
@mattt Thanks for the thorough review. All four are rebased onto main and updated. #213: n_threads now follows the model's threads setting, and the bitmap helper call carries a note that it is pinned to the current llama.swift signature. #214: renderGemma4Prompt is static and internal now, with a small suite rendering a short transcript and the assistant prefill. #215: I went with the lock, but with checkout semantics rather than a bare mutex, since a lock around the accessor alone would not protect a context for the length of a generation. A generation checks the cached context out for its whole run. A concurrent generation for another session gets a transient context that is never cached, so nothing can free a context that is still decoding. clearCachedContext() during a run marks the context to be freed on release. The docs now say the context and its KV live as long as the model. #216: the boolean check is Darwin-conditional with an objCType fallback for corelibs-foundation, and the last snapshot no longer shrinks: the round's visible text is folded into the base before the .stop yield. One call per round is intentional and now documented on callTerminator. Models that want several calls issue them across consecutive rounds. Happy to widen it to multiple calls per round as a follow-up if you would rather have that. |
Image segments threw unsupportedFeature because the backend had no multimodal path, even though the prebuilt llama.cpp binaries ship the mtmd library and its helpers. Accept an mmprojPath at initialization and load the projector next to the model. When a projector is present, prompt formatting replaces each image segment with the mtmd media marker and collects payloads in order, then generation tokenizes the marker-annotated prompt with mtmd_tokenize and evaluates text and image chunks through mtmd_helper_eval_chunks before sampling continues from the resulting position. Both respond and streaming support images, and models without a projector keep rejecting image input. Adds live tests generating from an embedded test image through both paths.
10607e9 to
f6d1514
Compare
Image segments threw unsupportedFeature because the backend had no multimodal path, even though the prebuilt llama.cpp binaries ship the mtmd library. This accepts an mmprojPath at initialization, replaces image segments with the mtmd media marker during prompt formatting, and evaluates text and image chunks through mtmd_helper_eval_chunks before sampling continues. Both respond and streaming support images, and models without a projector keep rejecting image input. Stacks on #195; the squash-merge will sort out the shared commit.