Add Gemma 4 vision (gemma4v) support to Flash-MoE fork#3
Open
Manojbhat09 wants to merge 1 commit into
Open
Conversation
Ports upstream ggml-org/llama.cpp multimodal (mtmd) stack to enable Gemma 4 26B A4B vision inference within the Flash-MoE sidecar path. Five bugs fixed to reach working end-to-end vision: 1. Unknown projector type: sync tools/mtmd/ from upstream, registering PROJECTOR_TYPE_GEMMA4V in clip-impl.h and adding gemma4v.cpp model (SigLIP encoder + Gemma4VisionPooler + Gemma4MultimodalEmbedder). 2. Build break: mtmd_helper_bitmap_init_from_buf gained a 'placeholder' parameter upstream; update server-common.cpp call site to pass false. 3. Inline image injection: cli.cpp -p text now scans for <file.ext> patterns and loads them as media, replacing with <__media__> marker so mtmd_tokenize sees the image instead of literal angle-bracket text. 4. Per-layer embedding scale missing in vision path: gemma4-iswa.cpp get_per_layer_inputs() was missing ggml_scale(sqrtf(n_embd_per_layer)) after the padding-token cast in the image-embedding branch, causing ~100x under-scaled per-layer embeddings during image token decode. Matches upstream gemma4.cpp build_inp_per_layer() line 472. 5. Multi-tile encoding always fails: mtmd_encode() used clip_image_batch_ encode() for GEMMA4V, which rejects batches >1 tile. Image preprocessing always produces multiple tiles (min_pixels=92160 forces upscaling of any sub-threshold image). Fix: add PROJECTOR_TYPE_GEMMA4V to the per-entry clip_image_encode() loop, matching LLaVA/MiniCPMV/Granite pattern. Also adds docs/gemma4-vision-flash-moe.md covering architecture, all bugs, server setup, API usage, and performance/tuning reference for Apple Silicon.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Ports upstream ggml-org/llama.cpp multimodal (mtmd) stack to enable Gemma 4 26B A4B vision inference within the Flash-MoE sidecar path.
Five bugs fixed to reach working end-to-end vision:
Unknown projector type: sync tools/mtmd/ from upstream, registering PROJECTOR_TYPE_GEMMA4V in clip-impl.h and adding gemma4v.cpp model (SigLIP encoder + Gemma4VisionPooler + Gemma4MultimodalEmbedder).
Build break: mtmd_helper_bitmap_init_from_buf gained a 'placeholder' parameter upstream; update server-common.cpp call site to pass false.
Inline image injection: cli.cpp -p text now scans for <file.ext>
patterns and loads them as media, replacing with <media> marker so mtmd_tokenize sees the image instead of literal angle-bracket text.
Per-layer embedding scale missing in vision path: gemma4-iswa.cpp get_per_layer_inputs() was missing ggml_scale(sqrtf(n_embd_per_layer)) after the padding-token cast in the image-embedding branch, causing ~100x under-scaled per-layer embeddings during image token decode. Matches upstream gemma4.cpp build_inp_per_layer() line 472.
Multi-tile encoding always fails: mtmd_encode() used clip_image_batch_ encode() for GEMMA4V, which rejects batches >1 tile. Image preprocessing always produces multiple tiles (min_pixels=92160 forces upscaling of any sub-threshold image). Fix: add PROJECTOR_TYPE_GEMMA4V to the per-entry clip_image_encode() loop, matching LLaVA/MiniCPMV/Granite pattern.
Also adds docs/gemma4-vision-flash-moe.md covering architecture, all bugs, server setup, API usage, and performance/tuning reference for Apple Silicon.