Skip to content

Add Gemma 4 vision (gemma4v) support to Flash-MoE fork#3

Open
Manojbhat09 wants to merge 1 commit into
Anemll:masterfrom
Manojbhat09:master
Open

Add Gemma 4 vision (gemma4v) support to Flash-MoE fork#3
Manojbhat09 wants to merge 1 commit into
Anemll:masterfrom
Manojbhat09:master

Conversation

@Manojbhat09

Copy link
Copy Markdown

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.

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.
@github-actions github-actions Bot added documentation Improvements or additions to documentation examples python model server labels Jun 8, 2026
@Anemll Anemll self-assigned this Jun 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation examples model python server

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants